]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
c3acc40858251c08a742e2d1bbabc989a2fab18b
[user/henk/code/inspircd.git] / src / modules / m_chanprotect.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                     E-mail:
7  *              <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *          the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #include "users.h"
18 #include "channels.h"
19 #include "modules.h"
20 #include "inspircd.h"
21
22 /* $ModDesc: Provides channel modes +a and +q */
23 /* $ModDep: ../../include/u_listmode.h */
24
25 #define PROTECT_VALUE 40000
26 #define FOUNDER_VALUE 50000
27
28 const char* fakevalue = "on";
29
30 /* When this is set to true, no restrictions apply to setting or
31  * removal of +qa. This is used while unloading so that the server
32  * can freely clear all of its users of the modes.
33  */
34 bool unload_kludge = false;
35
36 /** Handles basic operation of +qa channel modes
37  */
38 class FounderProtectBase
39 {
40  private:
41         InspIRCd* MyInstance;
42         std::string extend;
43         std::string type;
44         int list;
45         int end;
46         char* dummyptr;
47  public:
48         FounderProtectBase(InspIRCd* Instance, const std::string &ext, const std::string &mtype, int l, int e) : MyInstance(Instance), extend(ext), type(mtype), list(l), end(e)
49         {
50         }
51
52         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
53         {
54                 userrec* x = MyInstance->FindNick(parameter);
55                 if (x)
56                 {
57                         if (!channel->HasUser(x))
58                         {
59                                 return std::make_pair(false, parameter);
60                         }
61                         else
62                         {
63                                 std::string item = extend+std::string(channel->name);
64                                 if (x->GetExt(item,dummyptr))
65                                 {
66                                         return std::make_pair(true, x->nick);
67                                 }
68                                 else
69                                 {
70                                         return std::make_pair(false, parameter);
71                                 }
72                         }
73                 }
74                 return std::make_pair(false, parameter);
75         }
76
77         void RemoveMode(chanrec* channel, char mc)
78         {
79                 unload_kludge = true;
80                 CUList* cl = channel->GetUsers();
81                 std::string item = extend + std::string(channel->name);
82                 const char* mode_junk[MAXMODES+1];
83                 userrec* n = new userrec(MyInstance);
84                 n->SetFd(FD_MAGIC_NUMBER);
85                 mode_junk[0] = channel->name;
86                 irc::modestacker modestack(false);
87                 std::deque<std::string> stackresult;                            
88                 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
89                 {
90                         if (i->second->GetExt(item, dummyptr))
91                         {
92                                 modestack.Push(mc, i->second->nick);
93                         }
94                 }
95
96                 while (modestack.GetStackedLine(stackresult))
97                 {
98                         for (size_t j = 0; j < stackresult.size(); j++)
99                         {
100                                 mode_junk[j+1] = stackresult[j].c_str();
101                         }
102                         MyInstance->SendMode(mode_junk, stackresult.size() + 1, n);
103                 }
104                 
105                 delete n;
106                 unload_kludge = false;
107         }
108
109         void DisplayList(userrec* user, chanrec* channel)
110         {
111                 CUList* cl = channel->GetUsers();
112                 std::string item = extend+std::string(channel->name);
113                 for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
114                 {
115                         if (i->second->GetExt(item, dummyptr))
116                         {
117                                 user->WriteServ("%d %s %s %s", list, user->nick, channel->name,i->second->nick);
118                         }
119                 }
120                 user->WriteServ("%d %s %s :End of channel %s list", end, user->nick, channel->name, type.c_str());
121         }
122
123         userrec* FindAndVerify(std::string &parameter, chanrec* channel)
124         {
125                 userrec* theuser = MyInstance->FindNick(parameter);
126                 if ((!theuser) || (!channel->HasUser(theuser)))
127                 {
128                         parameter = "";
129                         return NULL;
130                 }
131                 return theuser;
132         }
133
134         ModeAction HandleChange(userrec* source, userrec* theuser, bool adding, chanrec* channel, std::string &parameter)
135         {
136                 std::string item = extend+std::string(channel->name);
137
138                 if (adding)
139                 {
140                         if (!theuser->GetExt(item, dummyptr))
141                         {
142                                 theuser->Extend(item, fakevalue);
143                                 parameter = theuser->nick;
144                                 return MODEACTION_ALLOW;
145                         }
146                 }
147                 else
148                 {
149                         if (theuser->GetExt(item, dummyptr))
150                         {
151                                 theuser->Shrink(item);
152                                 parameter = theuser->nick;
153                                 return MODEACTION_ALLOW;
154                         }
155                 }
156                 return MODEACTION_DENY;
157         }
158 };
159
160 /** Abstraction of FounderProtectBase for channel mode +q
161  */
162 class ChanFounder : public ModeHandler, public FounderProtectBase
163 {
164         char* dummyptr;
165  public:
166         ChanFounder(InspIRCd* Instance, bool using_prefixes)
167                 : ModeHandler(Instance, 'q', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '~' : 0),
168                   FounderProtectBase(Instance, "cm_founder_", "founder", 386, 387) { }
169
170         unsigned int GetPrefixRank()
171         {
172                 return FOUNDER_VALUE;
173         }
174
175         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
176         {
177                 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
178         }
179
180         void RemoveMode(chanrec* channel)
181         {
182                 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
183         }
184
185         void RemoveMode(userrec* user)
186         {
187         }
188
189         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
190         {
191                 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
192
193                 if (!theuser)
194                 {
195                         return MODEACTION_DENY;
196                 }
197
198                  // source is a server, or ulined, we'll let them +-q the user.
199                 if ((unload_kludge) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (!IS_LOCAL(source)))
200                 {
201                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
202                 }
203                 else
204                 {
205                         // whoops, someones being naughty!
206                         source->WriteServ("468 %s %s :Only servers may set channel mode +q",source->nick, channel->name);
207                         parameter = "";
208                         return MODEACTION_DENY;
209                 }
210         }
211
212         void DisplayList(userrec* user, chanrec* channel)
213         {
214                 FounderProtectBase::DisplayList(user,channel);
215         }
216 };
217
218 /** Abstraction of FounderProtectBase for channel mode +a
219  */
220 class ChanProtect : public ModeHandler, public FounderProtectBase
221 {
222         char* dummyptr;
223  public:
224         ChanProtect(InspIRCd* Instance, bool using_prefixes)
225                 : ModeHandler(Instance, 'a', 1, 1, true, MODETYPE_CHANNEL, false, using_prefixes ? '&' : 0),
226                   FounderProtectBase(Instance,"cm_protect_","protected user", 388, 389) { }
227
228         unsigned int GetPrefixRank()
229         {
230                 return PROTECT_VALUE;
231         }
232
233         ModePair ModeSet(userrec* source, userrec* dest, chanrec* channel, const std::string &parameter)
234         {
235                 return FounderProtectBase::ModeSet(source, dest, channel, parameter);
236         }
237
238         void RemoveMode(chanrec* channel)
239         {
240                 FounderProtectBase::RemoveMode(channel, this->GetModeChar());
241         }
242
243         void RemoveMode(userrec* user)
244         {
245         }
246
247         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
248         {
249                 userrec* theuser = FounderProtectBase::FindAndVerify(parameter, channel);
250
251                 if (!theuser)
252                         return MODEACTION_DENY;
253
254                 std::string founder = "cm_founder_"+std::string(channel->name);
255
256                 // source has +q, is a server, or ulined, we'll let them +-a the user.
257                 if ((unload_kludge) || (ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server) || (source->GetExt(founder,dummyptr)) || (!IS_LOCAL(source)))
258                 {
259                         return FounderProtectBase::HandleChange(source, theuser, adding, channel, parameter);
260                 }
261                 else
262                 {
263                         // bzzzt, wrong answer!
264                         source->WriteServ("482 %s %s :You are not a channel founder",source->nick, channel->name);
265                         return MODEACTION_DENY;
266                 }
267         }
268
269         virtual void DisplayList(userrec* user, chanrec* channel)
270         {
271                 FounderProtectBase::DisplayList(user, channel);
272         }
273
274 };
275
276 class ModuleChanProtect : public Module
277 {
278         
279         bool FirstInGetsFounder;
280         bool QAPrefixes;
281         ChanProtect* cp;
282         ChanFounder* cf;
283         char* dummyptr;
284         
285  public:
286  
287         ModuleChanProtect(InspIRCd* Me) : Module::Module(Me)
288         {       
289                 /* Load config stuff */
290                 OnRehash("");
291
292                 /* Initialise module variables */
293
294                 cp = new ChanProtect(ServerInstance,QAPrefixes);
295                 cf = new ChanFounder(ServerInstance,QAPrefixes);
296
297                 ServerInstance->AddMode(cp, 'a');
298                 ServerInstance->AddMode(cf, 'q');
299         }
300
301         void Implements(char* List)
302         {
303                 List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnSyncChannel] = 1;
304         }
305
306         virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
307         {
308                 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
309                 user->Shrink("cm_founder_"+std::string(chan->name));
310                 user->Shrink("cm_protect_"+std::string(chan->name));
311         }
312
313         virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
314         {
315                 // FIX: when someone parts a channel we must remove their Extensibles!
316                 user->Shrink("cm_founder_"+std::string(channel->name));
317                 user->Shrink("cm_protect_"+std::string(channel->name));
318         }
319
320         virtual void OnRehash(const std::string &parameter)
321         {
322                 /* Create a configreader class and read our flag,
323                  * in old versions this was heap-allocated and the
324                  * object was kept between rehashes...now we just
325                  * stack-allocate it locally.
326                  */
327                 ConfigReader Conf(ServerInstance);
328                 
329                 FirstInGetsFounder = Conf.ReadFlag("options","noservices",0);
330                 QAPrefixes = Conf.ReadFlag("options","qaprefixes",0);
331         }
332         
333         virtual void OnUserJoin(userrec* user, chanrec* channel)
334         {
335                 // if the user is the first user into the channel, mark them as the founder, but only if
336                 // the config option for it is set
337                 if (FirstInGetsFounder)
338                 {
339                         if (channel->GetUserCounter() == 1)
340                         {
341                                 // we're using Extensible::Extend to add data into user objects.
342                                 // this way is best as it adds data thats accessible to other modules
343                                 // (so long as you document your code properly) without breaking anything
344                                 // because its encapsulated neatly in a map.
345
346                                 // Change requested by katsklaw... when the first in is set to get founder,
347                                 // to make it clearer that +q has been given, send that one user the +q notice
348                                 // so that their client's syncronization and their sanity are left intact.
349                                 user->WriteServ("MODE %s +q %s",channel->name,user->nick);
350                                 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
351                                 {
352                                         ServerInstance->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
353                                 }
354                         }
355                 }
356         }
357         
358         virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
359         {
360                 // here we perform access checks, this is the important bit that actually stops kicking/deopping
361                 // etc of protected users. There are many types of access check, we're going to handle
362                 // a relatively small number of them relevent to our module using a switch statement.
363         
364                 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck %d",access_type);
365                 // don't allow action if:
366                 // (A) Theyre founder (no matter what)
367                 // (B) Theyre protected, and you're not
368                 // always allow the action if:
369                 // (A) The source is ulined
370                 
371                 
372                 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
373                 // without any access checks, we're not worthy :p
374                 if ((ServerInstance->ULine(source->nick)) || (ServerInstance->ULine(source->server)) || (!*source->server))
375                 {
376                         ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns ALLOW");
377                         return ACR_ALLOW;
378                 }
379
380                 std::string founder = "cm_founder_"+std::string(channel->name);
381                 std::string protect = "cm_protect_"+std::string(channel->name);
382
383                 switch (access_type)
384                 {
385                         // a user has been deopped. Do we let them? hmmm...
386                         case AC_DEOP:
387                                 ServerInstance->Log(DEBUG,"OnAccessCheck AC_DEOP");
388                                 if (dest->GetExt(founder,dummyptr))
389                                 {
390                                         ServerInstance->Log(DEBUG,"Has %s",founder.c_str());
391                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're a channel founder");
392                                         return ACR_DENY;
393                                 }
394                                 else
395                                 {
396                                         ServerInstance->Log(DEBUG,"Doesnt have %s",founder.c_str());
397                                 }
398                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
399                                 {
400                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as they're protected (+a)");
401                                         return ACR_DENY;
402                                 }
403                         break;
404
405                         // a user is being kicked. do we chop off the end of the army boot?
406                         case AC_KICK:
407                                 ServerInstance->Log(DEBUG,"OnAccessCheck AC_KICK");
408                                 if (dest->GetExt(founder,dummyptr))
409                                 {
410                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're a channel founder");
411                                         return ACR_DENY;
412                                 }
413                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
414                                 {
415                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as they're protected (+a)");
416                                         return ACR_DENY;
417                                 }
418                         break;
419
420                         // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
421                         case AC_DEHALFOP:
422                                 if (dest->GetExt(founder,dummyptr))
423                                 {
424                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're a channel founder");
425                                         return ACR_DENY;
426                                 }
427                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
428                                 {
429                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as they're protected (+a)");
430                                         return ACR_DENY;
431                                 }
432                         break;
433
434                         // same with devoice.
435                         case AC_DEVOICE:
436                                 if (dest->GetExt(founder,dummyptr))
437                                 {
438                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're a channel founder");
439                                         return ACR_DENY;
440                                 }
441                                 if ((dest->GetExt(protect,dummyptr)) && (!source->GetExt(protect,dummyptr)))
442                                 {
443                                         source->WriteServ("484 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as they're protected (+a)");
444                                         return ACR_DENY;
445                                 }
446                         break;
447                 }
448                 
449                 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
450                 ServerInstance->Log(DEBUG,"chanprotect OnAccessCheck returns DEFAULT");
451                 return ACR_DEFAULT;
452         }
453         
454         virtual ~ModuleChanProtect()
455         {
456                 ServerInstance->Modes->DelMode(cp);
457                 ServerInstance->Modes->DelMode(cf);
458                 DELETE(cp);
459                 DELETE(cf);
460         }
461         
462         virtual Version GetVersion()
463         {
464                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
465         }
466         
467         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
468         {
469                 /* NOTE: If +qa prefix is on, this is propogated by the channel join,
470                  * so we dont need to propogate it manually
471                  */
472                 if (!QAPrefixes)
473                 {
474                         // this is called when the server is linking into a net and wants to sync channel data.
475                         // we should send our mode changes for the channel here to ensure that other servers
476                         // know whos +q/+a on the channel.
477                         CUList* cl = chan->GetUsers();
478                         string_list commands;
479                         std::string founder = "cm_founder_"+std::string(chan->name);
480                         std::string protect = "cm_protect_"+std::string(chan->name);
481                         irc::modestacker modestack(true);
482                         std::deque<std::string> stackresult;
483                         for (CUList::iterator i = cl->begin(); i != cl->end(); i++)
484                         {
485                                 if (i->second->GetExt(founder,dummyptr))
486                                 {
487                                         modestack.Push('q',i->second->nick);
488                                 }
489                                 if (i->second->GetExt(protect,dummyptr))
490                                 {
491                                         modestack.Push('a',i->second->nick);
492                                 }
493                         }
494                         while (modestack.GetStackedLine(stackresult))
495                         {
496                                 irc::stringjoiner mode_join(" ", stackresult, 0, stackresult.size() - 1);
497                                 std::string line = mode_join.GetJoined();
498                                 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan, line);
499                         }
500                 }
501         }
502
503 };
504
505
506 class ModuleChanProtectFactory : public ModuleFactory
507 {
508  public:
509         ModuleChanProtectFactory()
510         {
511         }
512         
513         ~ModuleChanProtectFactory()
514         {
515         }
516         
517         virtual Module * CreateModule(InspIRCd* Me)
518         {
519                 return new ModuleChanProtect(Me);
520         }
521         
522 };
523
524
525 extern "C" void * init_module( void )
526 {
527         return new ModuleChanProtectFactory;
528 }