]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
e436e922526e436833b7d8262cfb75135b30370c
[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://wiki.inspircd.org/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                         mode_junk.insert(mode_junk.end(), stackresult.begin(), stackresult.end());
92                         MyInstance->SendMode(mode_junk, MyInstance->FakeClient);
93                         mode_junk.erase(mode_junk.begin() + 1, mode_junk.end());
94                 }
95         }
96
97         void DisplayList(User* user, Channel* channel)
98         {
99                 CUList* cl = channel->GetUsers();
100                 std::string item = extend+std::string(channel->name);
101                 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
102                 {
103                         if (i->first->GetExt(item))
104                         {
105                                 user->WriteServ("%d %s %s %s", list, user->nick.c_str(), channel->name.c_str(), i->first->nick.c_str());
106                         }
107                 }
108                 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick.c_str(), channel->name.c_str(), type.c_str());
109         }
110
111         User* FindAndVerify(std::string &parameter, Channel* channel)
112         {
113                 User* theuser = MyInstance->FindNick(parameter);
114                 if ((!theuser) || (!channel->HasUser(theuser)))
115                 {
116                         parameter.clear();
117                         return NULL;
118                 }
119                 return theuser;
120         }
121
122         bool CanRemoveOthers(User* u1, User* u2, Channel* c)
123         {
124                 std::string item = extend+std::string(c->name);
125                 return (remove_other_privs && u1->GetExt(item) && u2->GetExt(item));
126         }
127
128         ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string &parameter)
129         {
130                 std::string item = extend+std::string(channel->name);
131
132                 if (adding)
133                 {
134                         if (!theuser->GetExt(item))
135                         {
136                                 theuser->Extend(item);
137                                 parameter = theuser->nick;
138                                 return MODEACTION_ALLOW;
139                         }
140                 }
141                 else
142                 {
143                         if (theuser->GetExt(item))
144                         {
145                                 theuser->Shrink(item);
146                                 parameter = theuser->nick;
147                                 return MODEACTION_ALLOW;
148                         }
149                 }
150                 return MODEACTION_DENY;
151         }
152 };
153
154 /** Abstraction of FounderProtectBase for channel mode +q
155  */
156 class ChanFounder : public ModeHandler, public FounderProtectBase
157 {
158  public:
159         ChanFounder(InspIRCd* Instance, Module* Creator, char my_prefix, bool &depriv_self, bool &depriv_others)
160                 : ModeHandler(Instance, Creator, 'q', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0, TR_NICK),
161                   FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
162
163         unsigned int GetPrefixRank()
164         {
165                 return FOUNDER_VALUE;
166         }
167
168         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
169         {
170                 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
171         }
172
173         void RemoveMode(Channel* channel, irc::modestacker* stack)
174         {
175                 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
176         }
177
178         void RemoveMode(User* user, irc::modestacker* stack)
179         {
180         }
181
182         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
183         {
184                 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
185
186                 if (!theuser)
187                 {
188                         return MODEACTION_DENY;
189                 }
190
191                 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
192                 {
193                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
194                 }
195
196                 char isoverride=0;
197                 Module *Override = ServerInstance->Modules->FindFeature("Override");
198                 if (Override)
199                 {
200                         OVRrequest ovr(NULL,Override,source,"OTHERMODE");
201                         const char * tmp = ovr.Send();
202                         isoverride = tmp[0];
203                 }
204                  // source is a server, or ulined, we'll let them +-q the user.
205                 if (source == ServerInstance->FakeClient ||
206                                 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
207                                 (ServerInstance->ULine(source->nick.c_str())) ||
208                                 (ServerInstance->ULine(source->server)) ||
209                                 (!*source->server) ||
210                                 (!IS_LOCAL(source)) ||
211                                 isoverride)
212                 {
213                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
214                 }
215                 else
216                 {
217                         // whoops, someones being naughty!
218                         source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q", source->nick.c_str(), channel->name.c_str());
219                         parameter.clear();
220                         return MODEACTION_DENY;
221                 }
222         }
223
224         void DisplayList(User* user, Channel* channel)
225         {
226                 FounderProtectBase::DisplayList(user,channel);
227         }
228 };
229
230 /** Abstraction of FounderProtectBase for channel mode +a
231  */
232 class ChanProtect : public ModeHandler, public FounderProtectBase
233 {
234  public:
235         ChanProtect(InspIRCd* Instance, Module* Creator, char my_prefix, bool &depriv_self, bool &depriv_others)
236                 : ModeHandler(Instance, Creator, 'a', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0, TR_NICK),
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(User* source, User* dest, Channel* channel, const std::string &parameter)
245         {
246                 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
247         }
248
249         void RemoveMode(Channel* channel, irc::modestacker* stack)
250         {
251                 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
252         }
253
254         void RemoveMode(User* user, irc::modestacker* stack)
255         {
256         }
257
258         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
259         {
260                 User* 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
272                 char isoverride=0;
273                 Module *Override = ServerInstance->Modules->FindFeature("Override");
274                 if (Override)
275                 {
276                         OVRrequest ovr(NULL,Override,source,"OTHERMODE");
277                         const char * tmp = ovr.Send();
278                         isoverride = tmp[0];
279                 }
280                 // source has +q, is a server, or ulined, we'll let them +-a the user.
281                 if (source == ServerInstance->FakeClient ||
282                         ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
283                         (ServerInstance->ULine(source->nick.c_str())) ||
284                         (ServerInstance->ULine(source->server)) ||
285                         (!*source->server) ||
286                         (source->GetExt(founder)) ||
287                         (!IS_LOCAL(source)) ||
288                         isoverride
289                         )
290                 {
291                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
292                 }
293                 else
294                 {
295                         // bzzzt, wrong answer!
296                         source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
297                         return MODEACTION_DENY;
298                 }
299         }
300
301         virtual void DisplayList(User* user, Channel* channel)
302         {
303                 FounderProtectBase::DisplayList(user, channel);
304         }
305
306 };
307
308 class ModuleChanProtect : public Module
309 {
310
311         bool FirstInGetsFounder;
312         char QPrefix;
313         char APrefix;
314         bool DeprivSelf;
315         bool DeprivOthers;
316         bool booting;
317         ChanProtect* cp;
318         ChanFounder* cf;
319
320  public:
321
322         ModuleChanProtect(InspIRCd* Me)
323                 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true), cp(NULL), cf(NULL)
324         {
325                 /* Load config stuff */
326                 LoadSettings();
327                 booting = false;
328
329                 /* Initialise module variables */
330
331                 cp = new ChanProtect(ServerInstance, this, APrefix, DeprivSelf, DeprivOthers);
332                 cf = new ChanFounder(ServerInstance, this, QPrefix, DeprivSelf, DeprivOthers);
333
334                 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
335                 {
336                         delete cp;
337                         delete cf;
338                         throw ModuleException("Could not add new modes!");
339                 }
340
341                 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnUserPreJoin, I_OnAccessCheck };
342                 ServerInstance->Modules->Attach(eventlist, this, 4);
343         }
344
345         virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
346         {
347                 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
348                 user->Shrink("cm_founder_"+std::string(chan->name));
349                 user->Shrink("cm_protect_"+std::string(chan->name));
350         }
351
352         virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
353         {
354                 // FIX: when someone parts a channel we must remove their Extensibles!
355                 user->Shrink("cm_founder_"+std::string(channel->name));
356                 user->Shrink("cm_protect_"+std::string(channel->name));
357         }
358
359         void LoadSettings()
360         {
361                 ConfigReader Conf(ServerInstance);
362
363                 FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
364
365                 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
366                 QPrefix = qpre.empty() ? 0 : qpre[0];
367
368                 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
369                 APrefix = apre.empty() ? 0 : apre[0];
370
371                 if ((APrefix && QPrefix) && APrefix == QPrefix)
372                         throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
373
374                 if (cp && ServerInstance->Modes->FindPrefix(APrefix) == cp)
375                         throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
376
377                 if (cf && ServerInstance->Modes->FindPrefix(QPrefix) == cf)
378                         throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
379
380                 DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
381                 DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
382         }
383
384         virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
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 = std::string(1, QPrefix) + "@";
391
392                 return 0;
393         }
394
395         virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
396         {
397                 // here we perform access checks, this is the important bit that actually stops kicking/deopping
398                 // etc of protected users. There are many types of access check, we're going to handle
399                 // a relatively small number of them relevent to our module using a switch statement.
400                 // don't allow action if:
401                 // (A) Theyre founder (no matter what)
402                 // (B) Theyre protected, unless you're founder or are protected and DeprivOthers is enabled
403                 // always allow the action if:
404                 // (A) The source is ulined
405
406                 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
407                 // without any access checks, we're not worthy :p
408                 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server))
409                         return ACR_ALLOW;
410
411                 std::string founder("cm_founder_"+channel->name);
412                 std::string protect("cm_protect_"+channel->name);
413
414                 // Can do anything to yourself if deprotectself is enabled.
415                 if (DeprivSelf && source == dest)
416                         return ACR_DEFAULT;
417
418                 bool candepriv_founder = (DeprivOthers && source->GetExt(founder));
419                 bool candepriv_protected = (source->GetExt(founder) || (DeprivOthers && source->GetExt(protect))); // Can the source remove +a?
420
421                 switch (access_type)
422                 {
423                         // a user has been deopped. Do we let them? hmmm...
424                         case AC_DEOP:
425                                 if (dest->GetExt(founder) && !candepriv_founder)
426                                 {
427                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're a channel founder");
428                                         return ACR_DENY;
429                                 }
430                                 if ((dest->GetExt(protect)) && !candepriv_protected)
431                                 {
432                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're protected (+a)");
433                                         return ACR_DENY;
434                                 }
435                         break;
436
437                         // a user is being kicked. do we chop off the end of the army boot?
438                         case AC_KICK:
439                                 if (dest->GetExt(founder) && !candepriv_founder)
440                                 {
441                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're a channel founder");
442                                         return ACR_DENY;
443                                 }
444                                 if ((dest->GetExt(protect)) && !candepriv_protected)
445                                 {
446                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're protected (+a)");
447                                         return ACR_DENY;
448                                 }
449                         break;
450
451                         // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
452                         case AC_DEHALFOP:
453                                 if (dest->GetExt(founder) && !candepriv_founder)
454                                 {
455                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're a channel founder");
456                                         return ACR_DENY;
457                                 }
458                                 if ((dest->GetExt(protect)) && !candepriv_protected)
459                                 {
460                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're protected (+a)");
461                                         return ACR_DENY;
462                                 }
463                         break;
464
465                         // same with devoice.
466                         case AC_DEVOICE:
467                                 if (dest->GetExt(founder) && !candepriv_founder)
468                                 {
469                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're a channel founder");
470                                         return ACR_DENY;
471                                 }
472                                 if ((dest->GetExt(protect)) && !candepriv_protected)
473                                 {
474                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're protected (+a)");
475                                         return ACR_DENY;
476                                 }
477                         break;
478                 }
479
480                 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
481                 return ACR_DEFAULT;
482         }
483
484         virtual ~ModuleChanProtect()
485         {
486                 ServerInstance->Modes->DelMode(cp);
487                 ServerInstance->Modes->DelMode(cf);
488                 delete cp;
489                 delete cf;
490         }
491
492         virtual Version GetVersion()
493         {
494                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
495         }
496 };
497
498 MODULE_INIT(ModuleChanProtect)