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