]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
This will royally fuck 1.2's linking right now, but..
[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_OnUserPreJoin] = List[I_OnPostJoin] = 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 int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname, std::string &privs)
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
380                 if (FirstInGetsFounder && !chan)
381                         privs = "~@";
382                 
383                 return 0;
384         }
385         
386         virtual void OnPostJoin(userrec *user, chanrec *channel)
387         {
388                 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
389                 // before the client is in the channel
390                 
391                 // This notice was here originally because it was all done prior to the creation of
392                 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
393                 // not sure it really should be here - ops don't get shown, obviously, and the prefix
394                 // will appear in the names list for the user.. remove if desired -Special
395
396                 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
397                         user->WriteServ("MODE %s +q %s", channel->name, user->nick);
398         }
399         
400         virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
401         {
402                 // here we perform access checks, this is the important bit that actually stops kicking/deopping
403                 // etc of protected users. There are many types of access check, we're going to handle
404                 // a relatively small number of them relevent to our module using a switch statement.
405                 // don't allow action if:
406                 // (A) Theyre founder (no matter what)
407                 // (B) Theyre protected, and you're not
408                 // always allow the action if:
409                 // (A) The source is ulined
410                 
411                 
412                 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
413                 // without any access checks, we're not worthy :p
414                 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
415                         return ACR_ALLOW;
416
417                 std::string founder = "cm_founder_"+std::string(channel->name);
418                 std::string protect = "cm_protect_"+std::string(channel->name);
419
420                 switch (access_type)
421                 {
422                         // a user has been deopped. Do we let them? hmmm...
423                         case AC_DEOP:
424                                 if (dest->GetExt(founder,dummyptr))
425                                 {
426                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
427                                         return ACR_DENY;
428                                 }
429                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
430                                 {
431                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
432                                         return ACR_DENY;
433                                 }
434                         break;
435
436                         // a user is being kicked. do we chop off the end of the army boot?
437                         case AC_KICK:
438                                 if (dest->GetExt(founder,dummyptr))
439                                 {
440                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
441                                         return ACR_DENY;
442                                 }
443                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
444                                 {
445                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
446                                         return ACR_DENY;
447                                 }
448                         break;
449
450                         // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
451                         case AC_DEHALFOP:
452                                 if (dest->GetExt(founder,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 a channel founder");
455                                         return ACR_DENY;
456                                 }
457                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
458                                 {
459                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
460                                         return ACR_DENY;
461                                 }
462                         break;
463
464                         // same with devoice.
465                         case AC_DEVOICE:
466                                 if (dest->GetExt(founder,dummyptr))
467                                 {
468                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
469                                         return ACR_DENY;
470                                 }
471                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
472                                 {
473                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
474                                         return ACR_DENY;
475                                 }
476                         break;
477                 }
478                 
479                 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
480                 return ACR_DEFAULT;
481         }
482         
483         virtual ~ModuleChanProtect()
484         {
485                 ServerInstance->Modes->DelMode(cp);
486                 ServerInstance->Modes->DelMode(cf);
487                 DELETE(cp);
488                 DELETE(cf);
489         }
490         
491         virtual Version GetVersion()
492         {
493                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
494         }
495         
496         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
497         {
498                 /* NOTE: If +qa prefix is on, this is propogated by the channel join,
499                  * so we dont need to propogate it manually
500                  */
501                 if (!QAPrefixes)
502                 {
503                         // this is called when the server is linking into a net and wants to sync channel data.
504                         // we should send our mode changes for the channel here to ensure that other servers
505                         // know whos +q/+a on the channel.
506                         CUList* cl = chan->GetUsers();
507                         string_list commands;
508                         std::string founder = "cm_founder_"+std::string(chan->name);
509                         std::string protect = "cm_protect_"+std::string(chan->name);
510                         irc::modestacker modestack(true);
511                         std::deque<std::string> stackresult;
512                         for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
513                         {
514                                 if (i->first->GetExt(founder,dummyptr))
515                                 {
516                                         modestack.Push('q',i->first->nick);
517                                 }
518                                 if (i->first->GetExt(protect,dummyptr))
519                                 {
520                                         modestack.Push('a',i->first->nick);
521                                 }
522                         }
523                         while (modestack.GetStackedLine(stackresult))
524                         {
525                                 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
526                                 std::string line = mode_join.GetJoined();
527                                 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
528                         }
529                 }
530         }
531
532 };
533
534 MODULE_INIT(ModuleChanProtect)