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