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