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