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