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