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