]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
Fix crash on propogation after routed squit has reached it's destination. Thx HiroP.
[user/henk/code/inspircd.git] / src / modules / m_chanprotect.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 "inspircd.h"
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18
19 /* $ModDesc: Provides channel modes +a and +q */
20 /* $ModDep: ../../include/u_listmode.h */
21
22 #define PROTECT_VALUE 40000
23 #define FOUNDER_VALUE 50000
24
25 const char* fakevalue = "on";
26
27 /* When this is set to true, no restrictions apply to setting or
28  * removal of +qa. This is used while unloading so that the server
29  * can freely clear all of its users of the modes.
30  */
31 bool unload_kludge = false;
32
33 /** Handles basic operation of +qa channel modes
34  */
35 class FounderProtectBase
36 {
37  private:
38         InspIRCd* MyInstance;
39         std::string extend;
40         std::string type;
41         int list;
42         int end;
43         char* dummyptr;
44  protected:
45         bool& remove_own_privs;
46         bool& remove_other_privs;
47  public:
48         FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
49                 MyInstance(Instance), extend(ext), type(mtype), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others)
50         {
51         }
52
53         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
54         {
55                 userrec* x = MyInstance->FindNick(parameter);
56                 if (x)
57                 {
58                         if (!channel->HasUser(x))
59                         {
60                                 return std::make_pair(false, parameter);
61                         }
62                         else
63                         {
64                                 std::string item = extend+std::string(channel->name);
65                                 if (x->GetExt(item,dummyptr))
66                                 {
67                                         return std::make_pair(true, x->nick);
68                                 }
69                                 else
70                                 {
71                                         return std::make_pair(false, parameter);
72                                 }
73                         }
74                 }
75                 return std::make_pair(false, parameter);
76         }
77
78         void RemoveMode(chanrec* channel, char mc)
79         {
80                 unload_kludge = true;
81                 CUList* cl = channel->GetUsers();
82                 std::string item = extend + std::string(channel->name);
83                 const char* mode_junk[MAXMODES+2];
84                 userrec* n = new userrec(MyInstance);
85                 n->SetFd(FD_MAGIC_NUMBER);
86                 mode_junk[0] = channel->name;
87                 irc::modestacker modestack(false);
88                 std::deque<std::string> stackresult;                            
89                 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
90                 {
91                         if (i->first->GetExt(item, dummyptr))
92                         {
93                                 modestack.Push(mc, i->first->nick);
94                         }
95                 }
96
97                 while (modestack.GetStackedLine(stackresult))
98                 {
99                         for (size_t j = 0; j < stackresult.size(); j++)
100                         {
101                                 mode_junk[j+1] = stackresult[j].c_str();
102                         }
103                         MyInstance->SendMode(mode_junk, stackresult.size() + 1, n);
104                 }
105                 
106                 delete n;
107                 unload_kludge = false;
108         }
109
110         void DisplayList(userrec* user, chanrec* channel)
111         {
112                 CUList* cl = channel->GetUsers();
113                 std::string item = extend+std::string(channel->name);
114                 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
115                 {
116                         if (i->first->GetExt(item, dummyptr))
117                         {
118                                 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->first->nick);
119                         }
120                 }
121                 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
122         }
123
124         userrec* FindAndVerify(std::string &parameter, chanrec* channel)
125         {
126                 userrec* theuser = MyInstance->FindNick(parameter);
127                 if ((!theuser) || (!channel->HasUser(theuser)))
128                 {
129                         parameter.clear();
130                         return NULL;
131                 }
132                 return theuser;
133         }
134
135         bool CanRemoveOthers(userrec* u1, userrec* u2, chanrec* c)
136         {
137                 std::string item = extend+std::string(c->name);
138                 return (u1->GetExt(item, dummyptr) && u2->GetExt(item, dummyptr));
139         }
140
141         ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string &parameter)
142         {
143                 std::string item = extend+std::string(channel->name);
144
145                 if (adding)
146                 {
147                         if (!theuser->GetExt(item, dummyptr))
148                         {
149                                 theuser->Extend(item, fakevalue);
150                                 parameter = theuser->nick;
151                                 return MODEACTION_ALLOW;
152                         }
153                 }
154                 else
155                 {
156                         if (theuser->GetExt(item, dummyptr))
157                         {
158                                 theuser->Shrink(item);
159                                 parameter = theuser->nick;
160                                 return MODEACTION_ALLOW;
161                         }
162                 }
163                 return MODEACTION_DENY;
164         }
165 };
166
167 /** Abstraction of FounderProtectBase for channel mode +q
168  */
169 class ChanFounder : public ModeHandler, public FounderProtectBase
170 {
171         char* dummyptr;
172  public:
173         ChanFounder(InspIRCd* Instance, bool using_prefixes, bool &depriv_self, bool &depriv_others)
174                 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
175                   FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
176
177         unsigned int GetPrefixRank()
178         {
179                 return FOUNDER_VALUE;
180         }
181
182         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
183         {
184                 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
185         }
186
187         void RemoveMode(chanrec* channel)
188         {
189                 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
190         }
191
192         void RemoveMode(userrec* user)
193         {
194         }
195
196         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
197         {
198                 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
199
200                 if (!theuser)
201                 {
202                         return MODEACTION_DENY;
203                 }
204
205                 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
206                 {
207                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
208                 }
209                  // source is a server, or ulined, we'll let them +-q the user.
210                 if ((unload_kludge) || ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
211                 {
212                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
213                 }
214                 else
215                 {
216                         // whoops, someones being naughty!
217                         source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
218                         parameter.clear();
219                         return MODEACTION_DENY;
220                 }
221         }
222
223         void DisplayList(userrec* user, chanrec* channel)
224         {
225                 FounderProtectBase::DisplayList(user,channel);
226         }
227 };
228
229 /** Abstraction of FounderProtectBase for channel mode +a
230  */
231 class ChanProtect : public ModeHandler, public FounderProtectBase
232 {
233         char* dummyptr;
234  public:
235         ChanProtect(InspIRCd* Instance, bool using_prefixes, bool &depriv_self, bool &depriv_others)
236                 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
237                   FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
238
239         unsigned int GetPrefixRank()
240         {
241                 return PROTECT_VALUE;
242         }
243
244         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
245         {
246                 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
247         }
248
249         void RemoveMode(chanrec* channel)
250         {
251                 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
252         }
253
254         void RemoveMode(userrec* user)
255         {
256         }
257
258         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
259         {
260                 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
261
262                 if (!theuser)
263                         return MODEACTION_DENY;
264
265                 std::string founder = "cm_founder_"+std::string(channel->name);
266
267                 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
268                 {
269                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
270                 }
271                 // source has +q, is a server, or ulined, we'll let them +-a the user.
272                 if ((unload_kludge) || ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
273                 {
274                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
275                 }
276                 else
277                 {
278                         // bzzzt, wrong answer!
279                         source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
280                         return MODEACTION_DENY;
281                 }
282         }
283
284         virtual void DisplayList(userrec* user, chanrec* channel)
285         {
286                 FounderProtectBase::DisplayList(user, channel);
287         }
288
289 };
290
291 class ModuleChanProtect : public Module
292 {
293         
294         bool FirstInGetsFounder;
295         bool QAPrefixes;
296         bool DeprivSelf;
297         bool DeprivOthers;
298         bool booting;
299         ChanProtect* cp;
300         ChanFounder* cf;
301         char* dummyptr;
302         
303  public:
304  
305         ModuleChanProtect(InspIRCd* Me)
306                 : Module(Me), FirstInGetsFounder(false), QAPrefixes(false), DeprivSelf(false), DeprivOthers(false), booting(true)
307         {       
308                 /* Load config stuff */
309                 OnRehash(NULL,"");
310                 booting = false;
311
312                 /* Initialise module variables */
313
314                 cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
315                 cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
316
317                 if (!ServerInstance->AddMode(cp, 'a') || !ServerInstance->AddMode(cf, 'q'))
318                         throw ModuleException("Could not add new modes!");
319         }
320
321         void Implements(char* List)
322         {
323                 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
324         }
325
326         virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason, bool &silent)
327         {
328                 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
329                 user->Shrink("cm_founder_"+std::string(chan->name));
330                 user->Shrink("cm_protect_"+std::string(chan->name));
331         }
332
333         virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason, bool &silent)
334         {
335                 // FIX: when someone parts a channel we must remove their Extensibles!
336                 user->Shrink("cm_founder_"+std::string(channel->name));
337                 user->Shrink("cm_protect_"+std::string(channel->name));
338         }
339
340         virtual void OnRehash(userrec* user, const std::string &parameter)
341         {
342                 /* Create a configreader class and read our flag,
343                  * in old versions this was heap-allocated and the
344                  * object was kept between rehashes...now we just
345                  * stack-allocate it locally.
346                  */
347                 ConfigReader Conf(ServerInstance);
348
349                 bool old_qa = QAPrefixes;
350
351                 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
352                 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
353                 DeprivSelf = Conf.ReadFlag("options","deprotectself",0);
354                 DeprivOthers = Conf.ReadFlag("options","deprotectothers",0);
355
356                 /* Did the user change the QA prefixes on the fly?
357                  * If so, remove all instances of the mode, and reinit
358                  * the module with prefixes enabled.
359                  */
360                 if ((old_qa != QAPrefixes) && (!booting))
361                 {
362                         ServerInstance->Modes->DelMode(cp);
363                         ServerInstance->Modes->DelMode(cf);
364                         DELETE(cp);
365                         DELETE(cf);
366                         cp = new ChanProtect(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
367                         cf = new ChanFounder(ServerInstance,QAPrefixes,DeprivSelf,DeprivOthers);
368                         /* These wont fail, we already owned the mode characters before */
369                         ServerInstance->AddMode(cp, 'a');
370                         ServerInstance->AddMode(cf, 'q');
371                         ServerInstance->WriteOpers("*** WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
372                 }
373         }
374         
375         virtual void OnUserJoin(userrec* user, chanrec* channel, bool &silent)
376         {
377                 // if the user is the first user into the channel, mark them as the founder, but only if
378                 // the config option for it is set
379                 if (FirstInGetsFounder)
380                 {
381                         if (channel->GetUserCounter() == 1)
382                         {
383                                 // we're using Extensible::Extend to add data into user objects.
384                                 // this way is best as it adds data thats accessible to other modules
385                                 // (so long as you document your code properly) without breaking anything
386                                 // because its encapsulated neatly in a map.
387
388                                 // Change requested by katsklaw... when the first in is set to get founder,
389                                 // to make it clearer that +q has been given, send that one user the +q notice
390                                 // so that their client's syncronization and their sanity are left intact.
391                                 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
392                                 user->Extend("cm_founder_"+std::string(channel->name),fakevalue);
393                         }
394                 }
395         }
396         
397         virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
398         {
399                 // here we perform access checks, this is the important bit that actually stops kicking/deopping
400                 // etc of protected users. There are many types of access check, we're going to handle
401                 // a relatively small number of them relevent to our module using a switch statement.
402                 // don't allow action if:
403                 // (A) Theyre founder (no matter what)
404                 // (B) Theyre protected, and you're not
405                 // always allow the action if:
406                 // (A) The source is ulined
407                 
408                 
409                 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
410                 // without any access checks, we're not worthy :p
411                 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
412                         return ACR_ALLOW;
413
414                 std::string founder = "cm_founder_"+std::string(channel->name);
415                 std::string protect = "cm_protect_"+std::string(channel->name);
416
417                 switch (access_type)
418                 {
419                         // a user has been deopped. Do we let them? hmmm...
420                         case AC_DEOP:
421                                 if (dest->GetExt(founder,dummyptr))
422                                 {
423                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
424                                         return ACR_DENY;
425                                 }
426                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
427                                 {
428                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
429                                         return ACR_DENY;
430                                 }
431                         break;
432
433                         // a user is being kicked. do we chop off the end of the army boot?
434                         case AC_KICK:
435                                 if (dest->GetExt(founder,dummyptr))
436                                 {
437                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
438                                         return ACR_DENY;
439                                 }
440                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
441                                 {
442                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
443                                         return ACR_DENY;
444                                 }
445                         break;
446
447                         // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
448                         case AC_DEHALFOP:
449                                 if (dest->GetExt(founder,dummyptr))
450                                 {
451                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're a channel founder");
452                                         return ACR_DENY;
453                                 }
454                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
455                                 {
456                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
457                                         return ACR_DENY;
458                                 }
459                         break;
460
461                         // same with devoice.
462                         case AC_DEVOICE:
463                                 if (dest->GetExt(founder,dummyptr))
464                                 {
465                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
466                                         return ACR_DENY;
467                                 }
468                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
469                                 {
470                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
471                                         return ACR_DENY;
472                                 }
473                         break;
474                 }
475                 
476                 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
477                 return ACR_DEFAULT;
478         }
479         
480         virtual ~ModuleChanProtect()
481         {
482                 ServerInstance->Modes->DelMode(cp);
483                 ServerInstance->Modes->DelMode(cf);
484                 DELETE(cp);
485                 DELETE(cf);
486         }
487         
488         virtual Version GetVersion()
489         {
490                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
491         }
492         
493         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
494         {
495                 /* NOTE: If +qa prefix is on, this is propogated by the channel join,
496                  * so we dont need to propogate it manually
497                  */
498                 if (!QAPrefixes)
499                 {
500                         // this is called when the server is linking into a net and wants to sync channel data.
501                         // we should send our mode changes for the channel here to ensure that other servers
502                         // know whos +q/+a on the channel.
503                         CUList* cl = chan->GetUsers();
504                         string_list commands;
505                         std::string founder = "cm_founder_"+std::string(chan->name);
506                         std::string protect = "cm_protect_"+std::string(chan->name);
507                         irc::modestacker modestack(true);
508                         std::deque<std::string> stackresult;
509                         for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
510                         {
511                                 if (i->first->GetExt(founder,dummyptr))
512                                 {
513                                         modestack.Push('q',i->first->nick);
514                                 }
515                                 if (i->first->GetExt(protect,dummyptr))
516                                 {
517                                         modestack.Push('a',i->first->nick);
518                                 }
519                         }
520                         while (modestack.GetStackedLine(stackresult))
521                         {
522                                 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
523                                 std::string line = mode_join.GetJoined();
524                                 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
525                         }
526                 }
527         }
528
529 };
530
531
532 class ModuleChanProtectFactory : public ModuleFactory
533 {
534  public:
535         ModuleChanProtectFactory()
536         {
537         }
538         
539         ~ModuleChanProtectFactory()
540         {
541         }
542         
543         virtual Module * CreateModule(InspIRCd* Me)
544         {
545                 return new ModuleChanProtect(Me);
546         }
547         
548 };
549
550
551 extern "C" DllExport void * init_module( void )
552 {
553         return new ModuleChanProtectFactory;
554 }