]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
Remove ancient comment.
[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                 ConfigReader Conf(ServerInstance);
359
360                 FirstInGetsFounder = Conf.ReadFlag("chanprotect", "noservices", 0);
361
362                 std::string qpre = Conf.ReadValue("chanprotect", "qprefix", 0);
363                 QPrefix = qpre.empty() ? 0 : qpre[0];
364
365                 std::string apre = Conf.ReadValue("chanprotect", "aprefix", 0);
366                 APrefix = apre.empty() ? 0 : apre[0];
367
368                 if ((APrefix && QPrefix) && APrefix == QPrefix)
369                         throw ModuleException("What the smeg, why are both your +q and +a prefixes the same character?");
370
371                 if (cp && ServerInstance->Modes->FindPrefix(APrefix) == cp)
372                         throw ModuleException("Looks like the +a prefix you picked for m_chanprotect is already in use. Pick another.");
373
374                 if (cf && ServerInstance->Modes->FindPrefix(QPrefix) == cf)
375                         throw ModuleException("Looks like the +q prefix you picked for m_chanprotect is already in use. Pick another.");
376
377                 DeprivSelf = Conf.ReadFlag("chanprotect","deprotectself", "yes", 0);
378                 DeprivOthers = Conf.ReadFlag("chanprotect","deprotectothers", "yes", 0);
379                 DeprivOthers = Conf.ReadFlag("chanprotect","setprivsonothers", "yes", 0);
380         }
381
382         virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs, const std::string &keygiven)
383         {
384                 // if the user is the first user into the channel, mark them as the founder, but only if
385                 // the config option for it is set
386
387                 if (FirstInGetsFounder && !chan)
388                         privs = std::string(1, QPrefix) + "@";
389
390                 return 0;
391         }
392
393         virtual void OnPostJoin(User *user, Channel *channel)
394         {
395                 // This *must* be in PostJoin, not UserJoin - the former will make it appear to happen
396                 // before the client is in the channel
397
398                 // This notice was here originally because it was all done prior to the creation of
399                 // privs in OnUserPreJoin. I've left it because it might still be wanted, but i'm
400                 // not sure it really should be here - ops don't get shown, obviously, and the prefix
401                 // will appear in the names list for the user.. remove if desired -Special
402
403                 if (FirstInGetsFounder && channel->GetUserCounter() == 1)
404                         user->WriteServ("MODE %s +q %s", channel->name.c_str(), user->nick.c_str());
405         }
406
407         virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
408         {
409                 // here we perform access checks, this is the important bit that actually stops kicking/deopping
410                 // etc of protected users. There are many types of access check, we're going to handle
411                 // a relatively small number of them relevent to our module using a switch statement.
412                 // don't allow action if:
413                 // (A) Theyre founder (no matter what)
414                 // (B) Theyre protected, unless you're founder or are protected and DeprivOthers is enabled
415                 // always allow the action if:
416                 // (A) The source is ulined
417
418                 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
419                 // without any access checks, we're not worthy :p
420                 if ((ServerInstance->ULine(source->nick.c_str())) || (ServerInstance->ULine(source->server)) || (!*source->server))
421                         return ACR_ALLOW;
422
423                 std::string founder("cm_founder_"+channel->name);
424                 std::string protect("cm_protect_"+channel->name);
425
426                 // Can do anything to yourself if deprotectself is enabled.
427                 if (DeprivSelf && source == dest)
428                         return ACR_DEFAULT;
429
430                 bool candepriv_founder = (DeprivOthers && source->GetExt(founder));
431                 bool candepriv_protected = (source->GetExt(founder) || (DeprivOthers && source->GetExt(protect))); // Can the source remove +a?
432
433                 switch (access_type)
434                 {
435                         // a user has been deopped. Do we let them? hmmm...
436                         case AC_DEOP:
437                                 if (dest->GetExt(founder) && !candepriv_founder)
438                                 {
439                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're a channel founder");
440                                         return ACR_DENY;
441                                 }
442                                 if ((dest->GetExt(protect)) && !candepriv_protected)
443                                 {
444                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't deop "+dest->nick+" as they're protected (+a)");
445                                         return ACR_DENY;
446                                 }
447                         break;
448
449                         // a user is being kicked. do we chop off the end of the army boot?
450                         case AC_KICK:
451                                 if (dest->GetExt(founder) && !candepriv_founder)
452                                 {
453                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're a channel founder");
454                                         return ACR_DENY;
455                                 }
456                                 if ((dest->GetExt(protect)) && !candepriv_protected)
457                                 {
458                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't kick "+dest->nick+" as they're protected (+a)");
459                                         return ACR_DENY;
460                                 }
461                         break;
462
463                         // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
464                         case AC_DEHALFOP:
465                                 if (dest->GetExt(founder) && !candepriv_founder)
466                                 {
467                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're a channel founder");
468                                         return ACR_DENY;
469                                 }
470                                 if ((dest->GetExt(protect)) && !candepriv_protected)
471                                 {
472                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't de-halfop "+dest->nick+" as they're protected (+a)");
473                                         return ACR_DENY;
474                                 }
475                         break;
476
477                         // same with devoice.
478                         case AC_DEVOICE:
479                                 if (dest->GetExt(founder) && !candepriv_founder)
480                                 {
481                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're a channel founder");
482                                         return ACR_DENY;
483                                 }
484                                 if ((dest->GetExt(protect)) && !candepriv_protected)
485                                 {
486                                         source->WriteNumeric(484, source->nick+" "+channel->name+" :Can't devoice "+dest->nick+" as they're protected (+a)");
487                                         return ACR_DENY;
488                                 }
489                         break;
490                 }
491
492                 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
493                 return ACR_DEFAULT;
494         }
495
496         virtual ~ModuleChanProtect()
497         {
498                 ServerInstance->Modes->DelMode(cp);
499                 ServerInstance->Modes->DelMode(cf);
500                 delete cp;
501                 delete cf;
502         }
503
504         virtual Version GetVersion()
505         {
506                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
507         }
508 };
509
510 MODULE_INIT(ModuleChanProtect)