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