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