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