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