]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
e1b363cf3b277b0c0f75f20d2c4b5ab78f3f884b
[user/henk/code/inspircd.git] / src / modules / m_chanprotect.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "users.h"
18 #include "channels.h"
19 #include "modules.h"
20 #include "helperfuncs.h"
21
22 /* $ModDesc: Provides channel modes +a and +q */
23
24 const char fakevalue* = "on";
25
26 class ChanFounder : public ModeHandler
27 {
28  public:
29         ChanFounder() : ModeHandler('q', 1, 1, true, MODETYPE_CHANNEL, false) { }
30
31         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
32         {
33                 userrec* theuser = Srv->FindNick(parameter);
34
35                 // cant find the user given as the parameter, eat the mode change.
36                 if (!theuser)
37                 {
38                         parameter = "";
39                         return MODEACTION_DENY;
40                 }
41
42                 // given user isnt even on the channel, eat the mode change
43                 if (!channel->HasUser(theuser))
44                 {
45                         parameter = "";
46                         return MODEACTION_DENY;
47                 }
48
49                  // source is a server, or ulined, we'll let them +-q the user.
50                 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server))
51                 {
52                         if (adding)
53                         {
54                                 if (!theuser->GetExt("cm_founder_"+std::string(channel->name)))
55                                 {
56                                         theuser->Extend("cm_founder_"+std::string(channel->name),fakevalue);
57                                         // Tidy the nickname (make case match etc)
58                                         parameter = theuser->nick;
59                                         return MODEACTION_ALLOW;
60                                 }
61                         }
62                         else
63                         {
64                                 if (theuser->GetExt("cm_founder_"+std::string(channel->name)))
65                                 {
66                                         theuser->Shrink("cm_founder_"+std::string(channel->name));
67                                         // Tidy the nickname (make case match etc)
68                                         parameter = theuser->nick;
69                                         return MODEACTION_ALLOW;
70                                 }
71                         }
72                         return MODEACTION_DENY;
73                 }
74                 else
75                 {
76                         // whoops, someones being naughty!
77                         WriteServ(source->fd,"468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
78                         parameter = "";
79                         return MODEACTION_DENY;
80                 }
81         }
82
83         void DisplayList(userrec* user, chanrec* channel)
84         {
85                 chanuserlist cl = Srv->GetUsers(channel);
86                 for (unsigned int i = 0; i < cl.size(); i++)
87                 {
88                         if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
89                         {
90                                 WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
91                         }
92                 }
93                 WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
94         }
95
96 };
97
98 class ChanProtect : public ModeHandler
99 {
100  public:
101         ChanProtect() : ModeHandler('a', 1, 1, true, MODETYPE_CHANNEL, false) { }
102
103         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
104         {
105                 userrec* theuser = Srv->FindNick(parameter);
106
107                 // cant find the user given as the parameter, eat the mode change.
108                 if (!theuser)
109                 {
110                         parameter = "";
111                         return MODEACTION_DENY;
112                 }
113
114                 // given user isnt even on the channel, eat the mode change
115                 if (!chan->HasUser(theuser))
116                 {
117                         parameter = "";
118                         return MODEACTION_DENY;
119                 }
120
121                 // source has +q, is a server, or ulined, we'll let them +-a the user.
122                 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!*source->server) || (source->GetExt("cm_founder_"+std::string(channel->name))))
123                 {
124                         if (adding)
125                         {
126                                 if (!theuser->GetExt("cm_protect_"+std::string(channel->name)))
127                                 {
128                                         theuser->Extend("cm_protect_"+std::string(channel->name),fakevalue);
129                                         // Tidy the nickname (make case match etc)
130                                         parameter = theuser->nick;
131                                         return MODEACTION_ALLOW;
132                                 }
133                         }
134                         else
135                         {
136                                 if (theuser->GetExt("cm_protect_"+std::string(channel->name)))
137                                 {
138                                         theuser->Shrink("cm_protect_"+std::string(channel->name));
139                                         // Tidy the nickname (make case match etc)
140                                         parameter = theuser->nick;
141                                         return MODEACTION_ALLOW;
142                                 }
143                         }
144                         return MODEACTION_DENY;
145                 }
146                 else
147                 {
148                         // bzzzt, wrong answer!
149                         WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
150                         return MODEACTION_DENY;
151                 }
152         }
153
154         virtual void DisplayList(userrec* user, chanrec* channel)
155         {
156                 chanuserlist cl = Srv->GetUsers(channel);
157                 for (unsigned int i = 0; i < cl.size(); i++)
158                 {
159                         if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
160                         {
161                                 WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
162                         }
163                 }
164                 WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
165         }
166
167 };
168
169 class ModuleChanProtect : public Module
170 {
171         Server *Srv;
172         bool FirstInGetsFounder;
173         ConfigReader *Conf;
174         ChanProtect* cp;
175         ChanFounder* cf;
176         
177  public:
178  
179         ModuleChanProtect(Server* Me)
180         : Module::Module(Me), Srv(Me)
181         {       
182                 /* Initialise module variables */
183                 Conf = new ConfigReader;
184
185                 cp = new ChanProtect();
186                 cf = new ChanFounder();
187                 
188                 Srv->AddMode(cp, 'a');
189                 Srv->AdDMode(cf, 'q');
190                 
191                 // read our config options (main config file)
192                 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
193         }
194
195         void Implements(char* List)
196         {
197                 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
198         }
199         
200         virtual void On005Numeric(std::string &output)
201         {
202                 InsertMode(output,"qa",1);
203         }
204
205         virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
206         {
207                 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
208                 user->Shrink("cm_founder_"+std::string(chan->name));
209                 user->Shrink("cm_protect_"+std::string(chan->name));
210         }
211
212         virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
213         {
214                 // FIX: when someone parts a channel we must remove their Extensibles!
215                 user->Shrink("cm_founder_"+std::string(channel->name));
216                 user->Shrink("cm_protect_"+std::string(channel->name));
217         }
218
219         virtual void OnRehash(const std::string &parameter)
220         {
221                 // on a rehash we delete our classes for good measure and create them again.
222                 DELETE(Conf);
223                 Conf = new ConfigReader;
224                 // re-read our config options on a rehash
225                 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
226         }
227         
228         virtual void OnUserJoin(userrec* user, chanrec* channel)
229         {
230                 // if the user is the first user into the channel, mark them as the founder, but only if
231                 // the config option for it is set
232                 if (FirstInGetsFounder)
233                 {
234                         if (Srv->CountUsers(channel) == 1)
235                         {
236                                 // we're using Extensible::Extend to add data into user objects.
237                                 // this way is best as it adds data thats accessible to other modules
238                                 // (so long as you document your code properly) without breaking anything
239                                 // because its encapsulated neatly in a map.
240
241                                 // Change requested by katsklaw... when the first in is set to get founder,
242                                 // to make it clearer that +q has been given, send that one user the +q notice
243                                 // so that their client's syncronization and their sanity are left intact.
244                                 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
245                                 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
246                                 {
247                                         Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
248                                 }
249                         }
250                 }
251         }
252         
253         virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
254         {
255                 // here we perform access checks, this is the important bit that actually stops kicking/deopping
256                 // etc of protected users. There are many types of access check, we're going to handle
257                 // a relatively small number of them relevent to our module using a switch statement.
258         
259                 // don't allow action if:
260                 // (A) Theyre founder (no matter what)
261                 // (B) Theyre protected, and you're not
262                 // always allow the action if:
263                 // (A) The source is ulined
264                 
265                 
266                 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
267                 // without any access checks, we're not worthy :p
268                 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
269                 {
270                         return ACR_ALLOW;
271                 }
272
273                 switch (access_type)
274                 {
275                         // a user has been deopped. Do we let them? hmmm...
276                         case AC_DEOP:
277                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
278                                 {
279                                         Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
280                                         return ACR_DENY;
281                                 }
282                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
283                                 {
284                                         Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
285                                         return ACR_DENY;
286                                 }
287                         break;
288
289                         // a user is being kicked. do we chop off the end of the army boot?
290                         case AC_KICK:
291                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
292                                 {
293                                         Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
294                                         return ACR_DENY;
295                                 }
296                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
297                                 {
298                                         Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
299                                         return ACR_DENY;
300                                 }
301                         break;
302
303                         // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
304                         case AC_DEHALFOP:
305                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
306                                 {
307                                         Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're a channel founder");
308                                         return ACR_DENY;
309                                 }
310                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
311                                 {
312                                         Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
313                                         return ACR_DENY;
314                                 }
315                         break;
316
317                         // same with devoice.
318                         case AC_DEVOICE:
319                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
320                                 {
321                                         Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
322                                         return ACR_DENY;
323                                 }
324                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
325                                 {
326                                         Srv->SendServ(source->fd,"484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
327                                         return ACR_DENY;
328                                 }
329                         break;
330                 }
331                 
332                 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
333                 return ACR_DEFAULT;
334         }
335         
336         virtual ~ModuleChanProtect()
337         {
338                 DELETE(Conf);
339                 DELETE(cp);
340                 DELETE(cf);
341         }
342         
343         virtual Version GetVersion()
344         {
345                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
346         }
347         
348         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
349         {
350                 // this is called when the server is linking into a net and wants to sync channel data.
351                 // we should send our mode changes for the channel here to ensure that other servers
352                 // know whos +q/+a on the channel.
353                 chanuserlist cl = Srv->GetUsers(chan);
354                 string_list commands;
355                 for (unsigned int i = 0; i < cl.size(); i++)
356                 {
357                         if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
358                         {
359                                 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(cl[i]->nick));
360                         }
361                         if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
362                         {
363                                 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(cl[i]->nick));
364                         }
365                 }
366         }
367
368 };
369
370
371 class ModuleChanProtectFactory : public ModuleFactory
372 {
373  public:
374         ModuleChanProtectFactory()
375         {
376         }
377         
378         ~ModuleChanProtectFactory()
379         {
380         }
381         
382         virtual Module * CreateModule(Server* Me)
383         {
384                 return new ModuleChanProtect(Me);
385         }
386         
387 };
388
389
390 extern "C" void * init_module( void )
391 {
392         return new ModuleChanProtectFactory;
393 }