]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_auditorium.cpp
67670134e8fedfa0c932981581a1eb9256ba8424
[user/henk/code/inspircd.git] / src / modules / m_auditorium.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "users.h"
15 #include "channels.h"
16 #include "modules.h"
17 #include "inspircd.h"
18
19 /* $ModDesc: Allows for auditorium channels (+u) where nobody can see others joining and parting or the nick list */
20
21 class AuditoriumMode : public ModeHandler
22 {
23  public:
24         AuditoriumMode(InspIRCd* Instance) : ModeHandler(Instance, 'u', 0, 0, false, MODETYPE_CHANNEL, false) { }
25
26         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
27         {
28                 if (channel->IsModeSet('u') != adding)
29                 {
30                         if (IS_LOCAL(source) && (channel->GetStatus(source) < STATUS_OP))
31                         {
32                                 source->WriteServ("482 %s %s :Only channel operators may %sset channel mode +u", source->nick, channel->name, adding ? "" : "un");
33                                 return MODEACTION_DENY;
34                         }
35                         else
36                         {
37                                 channel->SetMode('u', adding);
38                                 return MODEACTION_ALLOW;
39                         }
40                 }
41                 else
42                 {
43                         return MODEACTION_DENY;
44                 }
45         }
46 };
47
48 class ModuleAuditorium : public Module
49 {
50  private:
51         AuditoriumMode* aum;
52  public:
53         ModuleAuditorium(InspIRCd* Me)
54                 : Module::Module(Me)
55         {
56                 aum = new AuditoriumMode(ServerInstance);
57                 if (!ServerInstance->AddMode(aum, 'u'))
58                         throw ModuleException("Could not add new modes!");
59         }
60         
61         virtual ~ModuleAuditorium()
62         {
63                 ServerInstance->Modes->DelMode(aum);
64                 DELETE(aum);
65         }
66
67         Priority Prioritize()
68         {
69                 /* To ensure that we get priority over namesx for names list generation on +u channels */
70                 return (Priority)ServerInstance->PriorityBefore("m_namesx.so");
71         }
72
73         virtual Version GetVersion()
74         {
75                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
76         }
77
78         void Implements(char* List)
79         {
80                 List[I_OnUserJoin] = List[I_OnUserPart] = List[I_OnUserKick] = List[I_OnUserQuit] = List[I_OnUserList] = 1;
81         }
82
83         virtual int OnUserList(userrec* user, chanrec* Ptr)
84         {
85                 if (Ptr->IsModeSet('u'))
86                 {
87                         /* HELLOOO, IS ANYBODY THERE? -- nope, just us. */
88                         user->WriteServ("353 %s = %s :%s", user->nick, Ptr->name, user->nick);
89                         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
90                         return 1;
91                 }
92                 return 0;
93         }
94         
95         virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
96         {
97                 if (channel->IsModeSet('u'))
98                 {
99                         silent = true;
100                         /* Because we silenced the event, make sure it reaches the user whos joining (but only them of course) */
101                         user->WriteFrom(user, "JOIN %s", channel->name);
102                 }
103         }
104
105         void OnUserPart(userrec* user, chanrec* channel, const std::string &partmessage, bool &silent)
106         {
107                 if (channel->IsModeSet('u'))
108                 {
109                         silent = true;
110                         /* Because we silenced the event, make sure it reaches the user whos leaving (but only them of course) */
111                         user->WriteFrom(user, "PART %s%s%s", channel->name,
112                                         partmessage.empty() ? "" : " :",
113                                         partmessage.empty() ? "" : partmessage.c_str());
114                 }
115         }
116
117         void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)
118         {
119                 if (chan->IsModeSet('u'))
120                         silent = true;
121         }
122
123         void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
124         {
125                 command_t* parthandler = ServerInstance->Parser->GetHandler("PART");
126                 std::vector<std::string> to_leave;
127                 const char* parameters[2];
128                 if (parthandler)
129                 {
130                         for (UCListIter f = user->chans.begin(); f != user->chans.end(); f++)
131                         {
132                                 if (f->first->IsModeSet('u'))
133                                         to_leave.push_back(f->first->name);
134                         }
135                         /* We cant do this neatly in one loop, as we are modifying the map we are iterating */
136                         for (std::vector<std::string>::iterator n = to_leave.begin(); n != to_leave.end(); n++)
137                         {
138                                 parameters[0] = n->c_str();
139                                 /* This triggers our OnUserPart, above, making the PART silent */
140                                 parthandler->Handle(parameters, 1, user);
141                         }
142                 }
143         }
144 };
145
146 class ModuleAuditoriumFactory : public ModuleFactory
147 {
148  public:
149         ModuleAuditoriumFactory()
150         {
151         }
152         
153         ~ModuleAuditoriumFactory()
154         {
155         }
156         
157         virtual Module * CreateModule(InspIRCd* Me)
158         {
159                 return new ModuleAuditorium(Me);
160         }
161         
162 };
163
164 extern "C" void * init_module( void )
165 {
166         return new ModuleAuditoriumFactory;
167 }
168