]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
8cf91d45cda6e1fcd5c527e27a480bb1633ba1a9
[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()
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
111                                 // Change requested by katsklaw... when the first in is set to get founder,
112                                 // to make it clearer that +q has been given, send that one user the +q notice
113                                 // so that their client's syncronization and their sanity are left intact.
114                                 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
115                                 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
116                                 {
117                                         Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
118                                 }
119                         }
120                 }
121         }
122         
123         virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
124         {
125                 // here we perform access checks, this is the important bit that actually stops kicking/deopping
126                 // etc of protected users. There are many types of access check, we're going to handle
127                 // a relatively small number of them relevent to our module using a switch statement.
128         
129                 // don't allow action if:
130                 // (A) Theyre founder (no matter what)
131                 // (B) Theyre protected, and you're not
132                 // always allow the action if:
133                 // (A) The source is ulined
134                 
135                 
136                 // firstly, if a ulined nick, or a server, is setting the mode, then allow them to set the mode
137                 // without any access checks, we're not worthy :p
138                 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)) || (!strcmp(source->server,"")))
139                 {
140                         return ACR_ALLOW;
141                 }
142
143                 switch (access_type)
144                 {
145                         // a user has been deopped. Do we let them? hmmm...
146                         case AC_DEOP:
147                                 if (dest->GetExt("cm_founder_"+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 a channel founder");
150                                         return ACR_DENY;
151                                 }
152                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
153                                 {
154                                         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)");
155                                         return ACR_DENY;
156                                 }
157                         break;
158
159                         // a user is being kicked. do we chop off the end of the army boot?
160                         case AC_KICK:
161                                 if (dest->GetExt("cm_founder_"+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 a channel founder");
164                                         return ACR_DENY;
165                                 }
166                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
167                                 {
168                                         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)");
169                                         return ACR_DENY;
170                                 }
171                         break;
172
173                         // a user is being dehalfopped. Yes, we do disallow -h of a +ha user
174                         case AC_DEHALFOP:
175                                 if (dest->GetExt("cm_founder_"+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 a channel founder");
178                                         return ACR_DENY;
179                                 }
180                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
181                                 {
182                                         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)");
183                                         return ACR_DENY;
184                                 }
185                         break;
186
187                         // same with devoice.
188                         case AC_DEVOICE:
189                                 if (dest->GetExt("cm_founder_"+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 a channel founder");
192                                         return ACR_DENY;
193                                 }
194                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
195                                 {
196                                         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)");
197                                         return ACR_DENY;
198                                 }
199                         break;
200                 }
201                 
202                 // we dont know what this access check is, or dont care. just carry on, nothing to see here.
203                 return ACR_DEFAULT;
204         }
205         
206         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
207         {
208                 // not out mode, bail
209                 if ((modechar == 'q') && (type == MT_CHANNEL))
210                 {
211                         // set up parameters
212                         chanrec* chan = (chanrec*)target;
213                         userrec* theuser = Srv->FindNick(params[0]);
214                 
215                         // cant find the user given as the parameter, eat the mode change.
216                         if (!theuser)
217                                 return -1;
218                         
219                         // given user isnt even on the channel, eat the mode change
220                         if (!Srv->IsOnChannel(theuser,chan))
221                                 return -1;
222                         
223                         // source is a server, or ulined, we'll let them +-q the user.
224                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
225                         {
226                                 if (mode_on)
227                                 {
228                                         if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
229                                         {
230                                                 theuser->Extend("cm_founder_"+std::string(chan->name),fakevalue);
231                                                 return 1;
232                                         }
233                                 }
234                                 else
235                                 {
236                                         if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
237                                         {
238                                                 theuser->Shrink("cm_founder_"+std::string(chan->name));
239                                                 return 1;
240                                         }
241                                 }       
242
243                                 return -1;
244                         }
245                         else
246                         {
247                                 // whoops, someones being naughty!
248                                 WriteServ(user->fd,"468 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
249                                 return -1;
250                         }
251                 }
252                 if ((modechar == 'a') && (type == MT_CHANNEL))
253                 {
254                         // set up parameters
255                         chanrec* chan = (chanrec*)target;
256                         userrec* theuser = Srv->FindNick(params[0]);
257                 
258                         // cant find the user given as the parameter, eat the mode change.
259                         if (!theuser)
260                                 return -1;
261                         
262                         // given user isnt even on the channel, eat the mode change
263                         if (!Srv->IsOnChannel(theuser,chan))
264                                 return -1;
265
266                         // source has +q, is a server, or ulined, we'll let them +-a the user.
267                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
268                         {
269                                 if (mode_on)
270                                 {
271                                         if (!theuser->GetExt("cm_protect_"+std::string(chan->name)))
272                                         {
273                                                 theuser->Extend("cm_protect_"+std::string(chan->name),fakevalue);
274                                                 return 1;
275                                         }
276                                 }
277                                 else
278                                 {
279                                         if (theuser->GetExt("cm_protect_"+std::string(chan->name)))
280                                         {
281                                                 theuser->Shrink("cm_protect_"+std::string(chan->name));
282                                                 return 1;
283                                         }
284                                 }       
285
286                                 return -1;
287                         }
288                         else
289                         {
290                                 // bzzzt, wrong answer!
291                                 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
292                                 return -1;
293                         }
294                 }
295                 return 0;
296         }
297
298         virtual void OnSendList(userrec* user, chanrec* channel, char mode)
299         {
300                 if (mode == 'q')
301                 {
302                         chanuserlist cl = Srv->GetUsers(channel);
303                         for (int i = 0; i < cl.size(); i++)
304                         {
305                                 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
306                                 {
307                                         WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
308                                 }
309                         }
310                         WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
311                 }
312                 if (mode == 'a')
313                 {
314                         chanuserlist cl = Srv->GetUsers(channel);
315                         for (int i = 0; i < cl.size(); i++)
316                         {
317                                 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
318                                 {
319                                         WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
320                                 }
321                         }
322                         WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
323                 }
324
325         }
326         
327         virtual ~ModuleChanProtect()
328         {
329                 delete Conf;
330                 delete Srv;
331         }
332         
333         virtual Version GetVersion()
334         {
335                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
336         }
337         
338         virtual string_list OnChannelSync(chanrec* chan)
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 (int i = 0; i < cl.size(); i++)
346                 {
347                         if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
348                         {
349                                 commands.push_back("M "+std::string(chan->name)+" +q "+std::string(cl[i]->nick));
350                         }
351                         if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
352                         {
353                                 commands.push_back("M "+std::string(chan->name)+" +a "+std::string(cl[i]->nick));
354                         }
355                 }
356                 return commands;
357         }
358
359 };
360
361
362 class ModuleChanProtectFactory : public ModuleFactory
363 {
364  public:
365         ModuleChanProtectFactory()
366         {
367         }
368         
369         ~ModuleChanProtectFactory()
370         {
371         }
372         
373         virtual Module * CreateModule()
374         {
375                 return new ModuleChanProtect;
376         }
377         
378 };
379
380
381 extern "C" void * init_module( void )
382 {
383         return new ModuleChanProtectFactory;
384 }
385