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