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