]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
daa728e61211d1f9bdc657a9e0ab31f6f5ae2221
[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  public:
35         FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e, bool &remove_own, bool &remove_others) :
36                 MyInstance(Instance), extend(ext), type(mtype), list(l), end(e), remove_own_privs(remove_own), remove_other_privs(remove_others)
37         {
38         }
39
40         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
41         {
42                 User* x = MyInstance->FindNick(parameter);
43                 if (x)
44                 {
45                         if (!channel->HasUser(x))
46                         {
47                                 return std::make_pair(false, parameter);
48                         }
49                         else
50                         {
51                                 std::string item = extend+std::string(channel->name);
52                                 if (x->GetExt(item))
53                                 {
54                                         return std::make_pair(true, x->nick);
55                                 }
56                                 else
57                                 {
58                                         return std::make_pair(false, parameter);
59                                 }
60                         }
61                 }
62                 return std::make_pair(false, parameter);
63         }
64
65         void RemoveMode(Channel* channel, char mc)
66         {
67                 CUList* cl = channel->GetUsers();
68                 std::string item = extend + std::string(channel->name);
69                 const char* mode_junk[MAXMODES+2];
70                 mode_junk[0] = channel->name;
71                 irc::modestacker modestack(false);
72                 std::deque<std::string> stackresult;                            
73
74                 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
75                 {
76                         if (i->first->GetExt(item))
77                         {
78                                 modestack.Push(mc, i->first->nick);
79                         }
80                 }
81
82                 while (modestack.GetStackedLine(stackresult))
83                 {
84                         for (size_t j = 0; j < stackresult.size(); j++)
85                         {
86                                 mode_junk[j+1] = stackresult[j].c_str();
87                         }
88                         MyInstance->SendMode(mode_junk, stackresult.size() + 1, MyInstance->FakeClient);
89                 }
90         }
91
92         void DisplayList(User* user, Channel* channel)
93         {
94                 CUList* cl = channel->GetUsers();
95                 std::string item = extend+std::string(channel->name);
96                 for (CUList::reverse_iterator i = cl->rbegin(); i != cl->rend(); ++i)
97                 {
98                         if (i->first->GetExt(item))
99                         {
100                                 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->first->nick);
101                         }
102                 }
103                 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
104         }
105
106         User* FindAndVerify(std::string &parameter, Channel* channel)
107         {
108                 User* theuser = MyInstance->FindNick(parameter);
109                 if ((!theuser) || (!channel->HasUser(theuser)))
110                 {
111                         parameter.clear();
112                         return NULL;
113                 }
114                 return theuser;
115         }
116
117         bool CanRemoveOthers(User* u1, User* u2, Channel* c)
118         {
119                 std::string item = extend+std::string(c->name);
120                 return (u1->GetExt(item) && u2->GetExt(item));
121         }
122
123         ModeAction HandleChange(User* source, User* theuser, bool adding, Channel* channel, std::string &parameter)
124         {
125                 std::string item = extend+std::string(channel->name);
126
127                 if (adding)
128                 {
129                         if (!theuser->GetExt(item))
130                         {
131                                 theuser->Extend(item);
132                                 parameter = theuser->nick;
133                                 return MODEACTION_ALLOW;
134                         }
135                 }
136                 else
137                 {
138                         if (theuser->GetExt(item))
139                         {
140                                 theuser->Shrink(item);
141                                 parameter = theuser->nick;
142                                 return MODEACTION_ALLOW;
143                         }
144                 }
145                 return MODEACTION_DENY;
146         }
147 };
148
149 /** Abstraction of FounderProtectBase for channel mode +q
150  */
151 class ChanFounder : public ModeHandler, public FounderProtectBase
152 {
153  public:
154         ChanFounder(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
155                 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
156                   FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387, depriv_self, depriv_others) { }
157
158         unsigned int GetPrefixRank()
159         {
160                 return FOUNDER_VALUE;
161         }
162
163         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
164         {
165                 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
166         }
167
168         void RemoveMode(Channel* channel)
169         {
170                 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
171         }
172
173         void RemoveMode(User* user)
174         {
175         }
176
177         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
178         {
179                 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
180
181                 if (!theuser)
182                 {
183                         return MODEACTION_DENY;
184                 }
185
186                 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
187                 {
188                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
189                 }
190                  // source is a server, or ulined, we'll let them +-q the user.
191                 if (source == ServerInstance->FakeClient ||
192                                 ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) ||
193                                 (ServerInstance->ULine(source->nick)) ||
194                                 (ServerInstance->ULine(source->server)) ||
195                                 (!*source->server) ||
196                                 (!IS_LOCAL(source)))
197                 {
198                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
199                 }
200                 else
201                 {
202                         // whoops, someones being naughty!
203                         source->WriteNumeric(468, "%s %s :Only servers may set channel mode +q",source->nick, channel->name);
204                         parameter.clear();
205                         return MODEACTION_DENY;
206                 }
207         }
208
209         void DisplayList(User* user, Channel* channel)
210         {
211                 FounderProtectBase::DisplayList(user,channel);
212         }
213 };
214
215 /** Abstraction of FounderProtectBase for channel mode +a
216  */
217 class ChanProtect : public ModeHandler, public FounderProtectBase
218 {
219  public:
220         ChanProtect(InspIRCd* Instance, char my_prefix, bool &depriv_self, bool &depriv_others)
221                 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, my_prefix, 0),
222                   FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389, depriv_self, depriv_others) { }
223
224         unsigned int GetPrefixRank()
225         {
226                 return PROTECT_VALUE;
227         }
228
229         ModePair ModeSet(User* source, User* dest, Channel* channel, const std::string &parameter)
230         {
231                 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
232         }
233
234         void RemoveMode(Channel* channel)
235         {
236                 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
237         }
238
239         void RemoveMode(User* user)
240         {
241         }
242
243         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
244         {
245                 User* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
246
247                 if (!theuser)
248                         return MODEACTION_DENY;
249
250                 std::string founder = "cm_founder_"+std::string(channel->name);
251
252                 if ((!adding) && FounderProtectBase::CanRemoveOthers(source, theuser, channel))
253                 {
254                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
255                 }
256                 // source has +q, is a server, or ulined, we'll let them +-a the user.
257                 if (source == ServerInstance->FakeClient ||
258                         ((source == theuser) && (!adding) && (FounderProtectBase::remove_own_privs)) || 
259                         (ServerInstance->ULine(source->nick)) ||
260                         (ServerInstance->ULine(source->server)) ||
261                         (!*source->server) ||
262                         (source->GetExt(founder)) ||
263                         (!IS_LOCAL(source)))
264                 {
265                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
266                 }
267                 else
268                 {
269                         // bzzzt, wrong answer!
270                         source->WriteNumeric(482, "%s %s :You are not a channel founder",source->nick, channel->name);
271                         return MODEACTION_DENY;
272                 }
273         }
274
275         virtual void DisplayList(User* user, Channel* channel)
276         {
277                 FounderProtectBase::DisplayList(user, channel);
278         }
279
280 };
281
282 class ModuleChanProtect : public Module
283 {
284         
285         bool FirstInGetsFounder;
286         char QPrefix;
287         char APrefix;
288         bool DeprivSelf;
289         bool DeprivOthers;
290         bool booting;
291         ChanProtect* cp;
292         ChanFounder* cf;
293         
294  public:
295  
296         ModuleChanProtect(InspIRCd* Me)
297                 : Module(Me), FirstInGetsFounder(false), QPrefix(0), APrefix(0), DeprivSelf(false), DeprivOthers(false), booting(true)
298         {       
299                 /* Load config stuff */
300                 OnRehash(NULL,"");
301                 booting = false;
302
303                 /* Initialise module variables */
304
305                 cp = new ChanProtect(ServerInstance, APrefix, DeprivSelf, DeprivOthers);
306                 cf = new ChanFounder(ServerInstance, QPrefix, DeprivSelf, DeprivOthers);
307
308                 if (!ServerInstance->Modes->AddMode(cp) || !ServerInstance->Modes->AddMode(cf))
309                 {
310                         delete cp;
311                         delete cf;
312                         throw ModuleException("Could not add new modes!");
313                 }
314
315                 Implementation eventlist[] = { I_OnUserKick, I_OnUserPart, I_OnRehash, I_OnUserPreJoin, I_OnPostJoin, I_OnAccessCheck };
316                 ServerInstance->Modules->Attach(eventlist, this, 6);
317         }
318
319         virtual void OnUserKick(User* source, User* user, Channel* chan, const std::string &reason, bool &silent)
320         {
321                 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
322                 user->Shrink("cm_founder_"+std::string(chan->name));
323                 user->Shrink("cm_protect_"+std::string(chan->name));
324         }
325
326         virtual void OnUserPart(User* user, Channel* channel, const std::string &partreason, bool &silent)
327         {
328                 // FIX: when someone parts a channel we must remove their Extensibles!
329                 user->Shrink("cm_founder_"+std::string(channel->name));
330                 user->Shrink("cm_protect_"+std::string(channel->name));
331         }
332
333         virtual void OnRehash(User* user, const std::string &parameter)
334         {
335                 /* Create a configreader class and read our flag,
336                  * in old versions this was heap-allocated and the
337                  * object was kept between rehashes...now we just
338                  * stack-allocate it locally.
339                  */
340                 ConfigReader Conf(ServerInstance);
341
342                 char old_q = QPrefix;
343                 char old_a = APrefix;
344
345                 FirstInGetsFounder = Conf.ReadFlag("options", "noservices", 0);
346
347                 std::string qpre = Conf.ReadValue("options", "qprefix", 0);
348                 QPrefix = qpre.empty() ? 0 : qpre[0];
349
350                 std::string apre = Conf.ReadValue("options", "aprefix", 0);
351                 APrefix = apre.empty() ? 0 : apre[0];
352
353                 DeprivSelf = Conf.ReadFlag("options","deprotectself",0);
354                 DeprivOthers = Conf.ReadFlag("options","deprotectothers",0);
355
356                 ServerInstance->Logs->Log("chanprotect", DEBUG, "qprefix is %c and aprefix is %c", QPrefix, APrefix);
357
358                 /* Did the user change the QA prefixes on the fly?
359                  * If so, remove all instances of the mode, and reinit
360                  * the module with prefixes enabled.
361                  */
362                 if ((old_q != QPrefix) && (!booting))
363                 {
364                         ServerInstance->Modes->DelMode(cf);
365                         delete cf;
366                         cf = new ChanFounder(ServerInstance, QPrefix, DeprivSelf, DeprivOthers);
367                         /* These wont fail, we already owned the mode characters before */
368                         ServerInstance->Modes->AddMode(cf);
369                         ServerInstance->SNO->WriteToSnoMask('A', "WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
370                 }
371
372                 if ((old_a != APrefix) && (!booting))
373                 {
374                         ServerInstance->Modes->DelMode(cp);
375                         delete cp;
376                         cp = new ChanProtect(ServerInstance, APrefix, DeprivSelf, DeprivOthers);
377                         ServerInstance->Modes->AddMode(cp);
378                         ServerInstance->SNO->WriteToSnoMask('A', "WARNING: +qa prefixes were enabled or disabled via a REHASH. Clients will probably need to reconnect to pick up this change.");
379                 }
380         }
381         
382         virtual int OnUserPreJoin(User *user, Channel *chan, const char *cname, std::string &privs)
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 = 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, user->nick);
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, and you're not
415                 // always allow the action if:
416                 // (A) The source is ulined
417                 
418                 
419                 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
420                 // without any access checks, we're not worthy :p
421                 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
422                         return ACR_ALLOW;
423
424                 std::string founder = "cm_founder_"+std::string(channel->name);
425                 std::string protect = "cm_protect_"+std::string(channel->name);
426
427                 switch (access_type)
428                 {
429                         // a user has been deopped. Do we let them? hmmm...
430                         case AC_DEOP:
431                                 if (dest->GetExt(founder))
432                                 {
433                                         source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
434                                         return ACR_DENY;
435                                 }
436                                 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
437                                 {
438                                         source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
439                                         return ACR_DENY;
440                                 }
441                         break;
442
443                         // a user is being kicked. do we chop off the end of the army boot?
444                         case AC_KICK:
445                                 if (dest->GetExt(founder))
446                                 {
447                                         source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
448                                         return ACR_DENY;
449                                 }
450                                 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
451                                 {
452                                         source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
453                                         return ACR_DENY;
454                                 }
455                         break;
456
457                         // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
458                         case AC_DEHALFOP:
459                                 if (dest->GetExt(founder))
460                                 {
461                                         source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're a channel founder");
462                                         return ACR_DENY;
463                                 }
464                                 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
465                                 {
466                                         source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
467                                         return ACR_DENY;
468                                 }
469                         break;
470
471                         // same with devoice.
472                         case AC_DEVOICE:
473                                 if (dest->GetExt(founder))
474                                 {
475                                         source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
476                                         return ACR_DENY;
477                                 }
478                                 if ((dest->GetExt(protect)) && (!source->GetExt(protect)))
479                                 {
480                                         source->WriteNumeric(484, ""+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
481                                         return ACR_DENY;
482                                 }
483                         break;
484                 }
485                 
486                 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
487                 return ACR_DEFAULT;
488         }
489         
490         virtual ~ModuleChanProtect()
491         {
492                 ServerInstance->Modes->DelMode(cp);
493                 ServerInstance->Modes->DelMode(cf);
494                 delete cp;
495                 delete cf;
496         }
497         
498         virtual Version GetVersion()
499         {
500                 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
501         }
502 };
503
504 MODULE_INIT(ModuleChanProtect)