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