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