]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
Started on this
[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         
175  public:
176  
177         ModuleChanProtect(Server* Me)
178         : Module::Module(Me), Srv(Me)
179         {       
180                 /* Initialise module variables */
181                 Conf = new ConfigReader;
182                 
183                 // set up our modes. We're using listmodes and not normal extmodes here.
184                 // listmodes only need one parameter as everything else is assumed by the
185                 // nature of the mode thats being created.
186                 Srv->AddExtendedListMode('a');
187                 Srv->AddExtendedListMode('q');
188                 
189                 // read our config options (main config file)
190                 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
191         }
192
193         void Implements(char* List)
194         {
195                 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnExtendedMode] = List[I_OnSendList] = List[I_OnSyncChannel] = 1;
196         }
197         
198         virtual void On005Numeric(std::string &output)
199         {
200                 InsertMode(output,"qa",1);
201         }
202
203         virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
204         {
205                 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
206                 user->Shrink("cm_founder_"+std::string(chan->name));
207                 user->Shrink("cm_protect_"+std::string(chan->name));
208         }
209
210         virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
211         {
212                 // FIX: when someone parts a channel we must remove their Extensibles!
213                 user->Shrink("cm_founder_"+std::string(channel->name));
214                 user->Shrink("cm_protect_"+std::string(channel->name));
215         }
216
217         virtual void OnRehash(const std::string &parameter)
218         {
219                 // on a rehash we delete our classes for good measure and create them again.
220                 DELETE(Conf);
221                 Conf = new ConfigReader;
222                 // re-read our config options on a rehash
223                 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
224         }
225         
226         virtual void OnUserJoin(userrec* user, chanrec* channel)
227         {
228                 // if the user is the first user into the channel, mark them as the founder, but only if
229                 // the config option for it is set
230                 if (FirstInGetsFounder)
231                 {
232                         if (Srv->CountUsers(channel) == 1)
233                         {
234                                 // we're using Extensible::Extend to add data into user objects.
235                                 // this way is best as it adds data thats accessible to other modules
236                                 // (so long as you document your code properly) without breaking anything
237                                 // because its encapsulated neatly in a map.
238
239                                 // Change requested by katsklaw... when the first in is set to get founder,
240                                 // to make it clearer that +q has been given, send that one user the +q notice
241                                 // so that their client's syncronization and their sanity are left intact.
242                                 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
243                                 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
244                                 {
245                                         Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
246                                 }
247                         }
248                 }
249         }
250         
251         virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
252         {
253                 // here we perform access checks, this is the important bit that actually stops kicking/deopping
254                 // etc of protected users. There are many types of access check, we're going to handle
255                 // a relatively small number of them relevent to our module using a switch statement.
256         
257                 // don't allow action if:
258                 // (A) Theyre founder (no matter what)
259                 // (B) Theyre protected, and you're not
260                 // always allow the action if:
261                 // (A) The source is ulined
262                 
263                 
264                 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
265                 // without any access checks, we're not worthy :p
266                 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
267                 {
268                         return ACR_ALLOW;
269                 }
270
271                 switch (access_type)
272                 {
273                         // a user has been deopped. Do we let them? hmmm...
274                         case AC_DEOP:
275                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
276                                 {
277                                         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");
278                                         return ACR_DENY;
279                                 }
280                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
281                                 {
282                                         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)");
283                                         return ACR_DENY;
284                                 }
285                         break;
286
287                         // a user is being kicked. do we chop off the end of the army boot?
288                         case AC_KICK:
289                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
290                                 {
291                                         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");
292                                         return ACR_DENY;
293                                 }
294                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
295                                 {
296                                         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)");
297                                         return ACR_DENY;
298                                 }
299                         break;
300
301                         // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
302                         case AC_DEHALFOP:
303                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
304                                 {
305                                         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");
306                                         return ACR_DENY;
307                                 }
308                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
309                                 {
310                                         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)");
311                                         return ACR_DENY;
312                                 }
313                         break;
314
315                         // same with devoice.
316                         case AC_DEVOICE:
317                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
318                                 {
319                                         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");
320                                         return ACR_DENY;
321                                 }
322                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
323                                 {
324                                         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)");
325                                         return ACR_DENY;
326                                 }
327                         break;
328                 }
329                 
330                 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
331                 return ACR_DEFAULT;
332         }
333         
334         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
335         {
336                 // not out mode, bail
337                 if ((modechar == 'q') && (type == MT_CHANNEL))
338                 {
339                         // set up parameters
340                         chanrec* chan = (chanrec*)target;
341                         userrec* theuser = Srv->FindNick(params[0]);
342                 
343                         // cant find the user given as the parameter, eat the mode change.
344                         if (!theuser)
345                                 return -1;
346                         
347                         // given user isnt even on the channel, eat the mode change
348                         if (!chan->HasUser(theuser))
349                                 return -1;
350                         
351                         // source is a server, or ulined, we'll let them +-q the user.
352                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
353                         {
354                                 if (mode_on)
355                                 {
356                                         if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
357                                         {
358                                                 theuser->Extend("cm_founder_"+std::string(chan->name),fakevalue);
359                                                 return 1;
360                                         }
361                                 }
362                                 else
363                                 {
364                                         if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
365                                         {
366                                                 theuser->Shrink("cm_founder_"+std::string(chan->name));
367                                                 return 1;
368                                         }
369                                 }       
370
371                                 return -1;
372                         }
373                         else
374                         {
375                                 // whoops, someones being naughty!
376                                 WriteServ(user->fd,"468 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
377                                 return -1;
378                         }
379                 }
380                 if ((modechar == 'a') && (type == MT_CHANNEL))
381                 {
382                         // set up parameters
383                         chanrec* chan = (chanrec*)target;
384                         userrec* theuser = Srv->FindNick(params[0]);
385                 
386                         // cant find the user given as the parameter, eat the mode change.
387                         if (!theuser)
388                                 return -1;
389                         
390                         // given user isnt even on the channel, eat the mode change
391                         if (!chan->HasUser(theuser))
392                                 return -1;
393
394                         // source has +q, is a server, or ulined, we'll let them +-a the user.
395                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
396                         {
397                                 if (mode_on)
398                                 {
399                                         if (!theuser->GetExt("cm_protect_"+std::string(chan->name)))
400                                         {
401                                                 theuser->Extend("cm_protect_"+std::string(chan->name),fakevalue);
402                                                 return 1;
403                                         }
404                                 }
405                                 else
406                                 {
407                                         if (theuser->GetExt("cm_protect_"+std::string(chan->name)))
408                                         {
409                                                 theuser->Shrink("cm_protect_"+std::string(chan->name));
410                                                 return 1;
411                                         }
412                                 }       
413
414                                 return -1;
415                         }
416                         else
417                         {
418                                 // bzzzt, wrong answer!
419                                 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
420                                 return -1;
421                         }
422                 }
423                 return 0;
424         }
425
426         virtual void OnSendList(userrec* user, chanrec* channel, char mode)
427         {
428                 if (mode == 'q')
429                 {
430                         chanuserlist cl = Srv->GetUsers(channel);
431                         for (unsigned int i = 0; i < cl.size(); i++)
432                         {
433                                 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
434                                 {
435                                         WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
436                                 }
437                         }
438                         WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
439                 }
440                 if (mode == 'a')
441                 {
442                         chanuserlist cl = Srv->GetUsers(channel);
443                         for (unsigned int i = 0; i < cl.size(); i++)
444                         {
445                                 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
446                                 {
447                                         WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
448                                 }
449                         }
450                         WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
451                 }
452
453         }
454         
455         virtual ~ModuleChanProtect()
456         {
457                 DELETE(Conf);
458         }
459         
460         virtual Version GetVersion()
461         {
462                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
463         }
464         
465         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
466         {
467                 // this is called when the server is linking into a net and wants to sync channel data.
468                 // we should send our mode changes for the channel here to ensure that other servers
469                 // know whos +q/+a on the channel.
470                 chanuserlist cl = Srv->GetUsers(chan);
471                 string_list commands;
472                 for (unsigned int i = 0; i < cl.size(); i++)
473                 {
474                         if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
475                         {
476                                 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(cl[i]->nick));
477                         }
478                         if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
479                         {
480                                 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(cl[i]->nick));
481                         }
482                 }
483         }
484
485 };
486
487
488 class ModuleChanProtectFactory : public ModuleFactory
489 {
490  public:
491         ModuleChanProtectFactory()
492         {
493         }
494         
495         ~ModuleChanProtectFactory()
496         {
497         }
498         
499         virtual Module * CreateModule(Server* Me)
500         {
501                 return new ModuleChanProtect(Me);
502         }
503         
504 };
505
506
507 extern "C" void * init_module( void )
508 {
509         return new ModuleChanProtectFactory;
510 }