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