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