]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
a88f5de384a71cbad4f24a2160a666c507918961
[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 /** Handles basic operation of +qa channel modes
22  */
23 class FounderProtectBase
24 {
25  private:
26         InspIRCd* MyInstance;
27         std::string extend;
28         std::string type;
29         int list;
30         int end;
31  protected:
32         bool& remove_own_privs;
33         bool& remove_other_privs;
34         bool& add_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, bool &add_others) :
37                 MyInstance(Instance), extend(ext), type(mtype), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others), add_other_privs(add_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         bool CanAddOthers(User* u1, User* u2, Channel* c)
131         {
132                 std::string item = extend+std::string(c->name);
133                 return (add_other_privs && u1->GetExt(item));
134         }
135
136         ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string &parameter)
137         {
138                 std::string item = extend+std::string(channel->name);
139
140                 if (adding)
141                 {
142                         if (!theuser->GetExt(item))
143                         {
144                                 theuser->Extend(item);
145                                 parameter = theuser->nick;
146                                 return MODEACTION_ALLOW;
147                         }
148                 }
149                 else
150                 {
151                         if (theuser->GetExt(item))
152                         {
153                                 theuser->Shrink(item);
154                                 parameter = theuser->nick;
155                                 return MODEACTION_ALLOW;
156                         }
157                 }
158                 return MODEACTION_DENY;
159         }
160 };
161
162 /** Abstraction of FounderProtectBase for channel mode +q
163  */
164 class ChanFounder : public ModeHandler, public FounderProtectBase
165 {
166  public:
167         ChanFounder(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others, bool &priv_others)
168                 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
169                   FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others, priv_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, irc::modestacker* stack)
182         {
183                 FounderProtectBase::RemoveMode(channel, this->GetModeChar(), stack);
184         }
185
186         void RemoveMode(User* user, irc::modestacker* stack)
187         {
188         }
189
190         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
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 (source == ServerInstance->FakeClient ||
205                                 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
206                                 (ServerInstance->ULine(source->nick.c_str())) ||
207                                 (ServerInstance->ULine(source->server)) ||
208                                 (!*source->server) ||
209                                 (!IS_LOCAL(source)))
210                 {
211                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
212                 }
213
214                 // If they have it, allow them to give it.
215                 if (adding && FounderProtectBase::CanAddOthers(source, theuser, channel))
216                 {
217                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
218                 }
219
220                 // whoops, someones being naughty!
221                 source->WriteNumeric(468, "%s %s :You are not permitted to set additional founders", source->nick.c_str(), channel->name.c_str());
222                 parameter.clear();
223                 return MODEACTION_DENY;
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, bool &priv_others)
238                 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
239                   FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others, priv_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                 // source has +q, is a server, or ulined, we'll let them +-a the user.
275                 if (source == ServerInstance->FakeClient ||
276                         ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
277                         (ServerInstance->ULine(source->nick.c_str())) ||
278                         (ServerInstance->ULine(source->server)) ||
279                         (!*source->server) ||
280                         (source->GetExt(founder)) ||
281                         (!IS_LOCAL(source)))
282                 {
283                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
284                 }
285
286                 // If they have it, allow them to give it.
287                 if (adding && FounderProtectBase::CanAddOthers(source, theuser, channel))
288                 {
289                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
290                 }
291                 
292                 // bzzzt, wrong answer!
293                 source->WriteNumeric(482, "%s %s :You are not a channel founder", source->nick.c_str(), channel->name.c_str());
294                 return MODEACTION_DENY;
295         }
296
297         virtual void DisplayList(User* user, Channel* channel)
298         {
299                 FounderProtectBase::DisplayList(user, channel);
300         }
301
302 };
303
304 class ModuleChanProtect : public Module
305 {
306
307         bool FirstInGetsFounder;
308         char QPrefix;
309         char APrefix;
310         bool DeprivSelf;
311         bool DeprivOthers;
312         bool PrivOthers;
313         bool booting;
314         ChanProtect* cp;
315         ChanFounder* cf;
316
317  public:
318
319         ModuleChanProtect(InspIRCd* Me)
320                 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true), cp(NULL), cf(NULL)
321         {
322                 /* Load config stuff */
323                 LoadSettings();
324                 booting = false;
325
326                 /* Initialise module variables */
327
328                 cp = new ChanProtect(ServerInstance, APrefix, DeprivSelf, DeprivOthers, PrivOthers);
329                 cf = new ChanFounder(ServerInstance, QPrefix, DeprivSelf, DeprivOthers, PrivOthers);
330
331                 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
332                 {
333                         delete cp;
334                         delete cf;
335                         throw ModuleException("Could not add new modes!");
336                 }
337
338                 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnUserPreJoin, I_OnPostJoin, I_OnAccessCheck };
339                 ServerInstance->Modules->Attach(eventlist, this, 5);
340         }
341
342         virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
343         {
344                 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
345                 user->Shrink("cm_founder_"+std::string(chan->name));
346                 user->Shrink("cm_protect_"+std::string(chan->name));
347         }
348
349         virtual void OnUserPart(User* user, Channel* channel, std::string &partreason, bool &silent)
350         {
351                 // FIX: when someone parts a channel we must remove their Extensibles!
352                 user->Shrink("cm_founder_"+std::string(channel->name));
353                 user->Shrink("cm_protect_"+std::string(channel->name));
354         }
355
356         void LoadSettings()
357         {
358                 /* Create a configreader class and read our flag,
359                  * in old versions this was heap-allocated and the
360                  * object was kept between rehashes...now we just
361                  * stack-allocate it locally.
362                  */
363                 ConfigReader Conf(ServerInstance);
364
365                 FirstInGetsFounder = Conf.ReadFlag("options", "noservices", 0);
366
367                 std::string qpre = Conf.ReadValue("options", "qprefix", 0);
368                 QPrefix = qpre.empty() ? 0 : qpre[0];
369
370                 std::string apre = Conf.ReadValue("options", "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("options","deprotectself", "yes", 0);
383                 DeprivOthers = Conf.ReadFlag("options","deprotectothers", "yes", 0);
384                 DeprivOthers = Conf.ReadFlag("options","setprivsonothers", "yes", 0);
385         }
386
387         virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
388         {
389                 // if the user is the first user into the channel, mark them as the founder, but only if
390                 // the config option for it is set
391
392                 if (FirstInGetsFounder && !chan)
393                         privs = std::string(1, QPrefix) + "@";
394
395                 return 0;
396         }
397
398         virtual void OnPostJoin(User *user, Channel *channel)
399         {
400                 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
401                 // before the client is in the channel
402
403                 // This notice was here originally because it was all done prior to the creation of
404                 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
405                 // not sure it really should be here - ops don't get shown, obviously, and the prefix
406                 // will appear in the names list for the user.. remove if desired -Special
407
408                 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
409                         user->WriteServ("MODE %s +q %s", channel->name.c_str(), user->nick.c_str());
410         }
411
412         virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
413         {
414                 // here we perform access checks, this is the important bit that actually stops kicking/deopping
415                 // etc of protected users. There are many types of access check, we're going to handle
416                 // a relatively small number of them relevent to our module using a switch statement.
417                 // don't allow action if:
418                 // (A) Theyre founder (no matter what)
419                 // (B) Theyre protected, unless you're founder or are protected and DeprivOthers is enabled
420                 // always allow the action if:
421                 // (A) The source is ulined
422
423                 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
424                 // without any access checks, we're not worthy :p
425                 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server))
426                         return ACR_ALLOW;
427
428                 std::string founder("cm_founder_"+channel->name);
429                 std::string protect("cm_protect_"+channel->name);
430
431                 // Can do anything to yourself if deprotectself is enabled.
432                 if (DeprivSelf && source == dest)
433                         return ACR_DEFAULT;
434
435                 bool candepriv_founder = (DeprivOthers && source->GetExt(founder));
436                 bool candepriv_protected = (source->GetExt(founder) || (DeprivOthers && source->GetExt(protect))); // Can the source remove +a?
437
438                 switch (access_type)
439                 {
440                         // a user has been deopped. Do we let them? hmmm...
441                         case AC_DEOP:
442                                 if (dest->GetExt(founder) && !candepriv_founder)
443                                 {
444                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're a channel founder");
445                                         return ACR_DENY;
446                                 }
447                                 if ((dest->GetExt(protect)) && !candepriv_protected)
448                                 {
449                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're protected (+a)");
450                                         return ACR_DENY;
451                                 }
452                         break;
453
454                         // a user is being kicked. do we chop off the end of the army boot?
455                         case AC_KICK:
456                                 if (dest->GetExt(founder) && !candepriv_founder)
457                                 {
458                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're a channel founder");
459                                         return ACR_DENY;
460                                 }
461                                 if ((dest->GetExt(protect)) && !candepriv_protected)
462                                 {
463                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're protected (+a)");
464                                         return ACR_DENY;
465                                 }
466                         break;
467
468                         // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
469                         case AC_DEHALFOP:
470                                 if (dest->GetExt(founder) && !candepriv_founder)
471                                 {
472                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're a channel founder");
473                                         return ACR_DENY;
474                                 }
475                                 if ((dest->GetExt(protect)) && !candepriv_protected)
476                                 {
477                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're protected (+a)");
478                                         return ACR_DENY;
479                                 }
480                         break;
481
482                         // same with devoice.
483                         case AC_DEVOICE:
484                                 if (dest->GetExt(founder) && !candepriv_founder)
485                                 {
486                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're a channel founder");
487                                         return ACR_DENY;
488                                 }
489                                 if ((dest->GetExt(protect)) && !candepriv_protected)
490                                 {
491                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're protected (+a)");
492                                         return ACR_DENY;
493                                 }
494                         break;
495                 }
496
497                 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
498                 return ACR_DEFAULT;
499         }
500
501         virtual ~ModuleChanProtect()
502         {
503                 ServerInstance->Modes->DelMode(cp);
504                 ServerInstance->Modes->DelMode(cf);
505                 delete cp;
506                 delete cf;
507         }
508
509         virtual Version GetVersion()
510         {
511                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
512         }
513 };
514
515 MODULE_INIT(ModuleChanProtect)