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