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