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