]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
dca0b53d29970c8b333107d39ca51146c2ab391a
[user/henk/code/inspircd.git] / src / modules / m_chanprotect.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 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 "users.h"
18 #include "channels.h"
19 #include "modules.h"
20 #include "helperfuncs.h"
21
22 /* $ModDesc: Provides channel modes +a and +q */
23
24 const char fakevalue* = "on";
25
26 class ModuleChanProtect : public Module
27 {
28         Server *Srv;
29         bool FirstInGetsFounder;
30         ConfigReader *Conf;
31         
32  public:
33  
34         ModuleChanProtect(Server* Me)
35         : Module::Module(Me), Srv(Me)
36         {       
37                 /* Initialise module variables */
38                 Conf = new ConfigReader;
39                 
40                 // set up our modes. We're using listmodes and not normal extmodes here.
41                 // listmodes only need one parameter as everything else is assumed by the
42                 // nature of the mode thats being created.
43                 Srv->AddExtendedListMode('a');
44                 Srv->AddExtendedListMode('q');
45                 
46                 // read our config options (main config file)
47                 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
48         }
49
50         void Implements(char* List)
51         {
52                 List[I_On005Numeric] = List[I_OnUserKick] = List[I_OnUserPart] = List[I_OnRehash] = List[I_OnUserJoin] = List[I_OnAccessCheck] = List[I_OnExtendedMode] = List[I_OnSendList] = List[I_OnSyncChannel] = 1;
53         }
54         
55         virtual void On005Numeric(std::string &output)
56         {
57                 InsertMode(output,"qa",1);
58         }
59
60         virtual void OnUserKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
61         {
62                 // FIX: when someone gets kicked from a channel we must remove their Extensibles!
63                 user->Shrink("cm_founder_"+std::string(chan->name));
64                 user->Shrink("cm_protect_"+std::string(chan->name));
65         }
66
67         virtual void OnUserPart(userrec* user, chanrec* channel, const std::string &partreason)
68         {
69                 // FIX: when someone parts a channel we must remove their Extensibles!
70                 user->Shrink("cm_founder_"+std::string(channel->name));
71                 user->Shrink("cm_protect_"+std::string(channel->name));
72         }
73
74         virtual void OnRehash(const std::string &parameter)
75         {
76                 // on a rehash we delete our classes for good measure and create them again.
77                 DELETE(Conf);
78                 Conf = new ConfigReader;
79                 // re-read our config options on a rehash
80                 FirstInGetsFounder = Conf->ReadFlag("options","noservices",0);
81         }
82         
83         virtual void OnUserJoin(userrec* user, chanrec* channel)
84         {
85                 // if the user is the first user into the channel, mark them as the founder, but only if
86                 // the config option for it is set
87                 if (FirstInGetsFounder)
88                 {
89                         if (Srv->CountUsers(channel) == 1)
90                         {
91                                 // we're using Extensible::Extend to add data into user objects.
92                                 // this way is best as it adds data thats accessible to other modules
93                                 // (so long as you document your code properly) without breaking anything
94                                 // because its encapsulated neatly in a map.
95
96                                 // Change requested by katsklaw... when the first in is set to get founder,
97                                 // to make it clearer that +q has been given, send that one user the +q notice
98                                 // so that their client's syncronization and their sanity are left intact.
99                                 WriteServ(user->fd,"MODE %s +q %s",channel->name,user->nick);
100                                 if (user->Extend("cm_founder_"+std::string(channel->name),fakevalue))
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 they'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 they'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 they'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 they'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 they'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 they'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 they'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 they'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 (!chan->HasUser(theuser))
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),fakevalue);
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 (!chan->HasUser(theuser))
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),fakevalue);
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 void OnSendList(userrec* user, chanrec* channel, char mode)
284         {
285                 if (mode == 'q')
286                 {
287                         chanuserlist cl = Srv->GetUsers(channel);
288                         for (unsigned int i = 0; i < cl.size(); i++)
289                         {
290                                 if (cl[i]->GetExt("cm_founder_"+std::string(channel->name)))
291                                 {
292                                         WriteServ(user->fd,"386 %s %s %s",user->nick, channel->name,cl[i]->nick);
293                                 }
294                         }
295                         WriteServ(user->fd,"387 %s %s :End of channel founder list",user->nick, channel->name);
296                 }
297                 if (mode == 'a')
298                 {
299                         chanuserlist cl = Srv->GetUsers(channel);
300                         for (unsigned int i = 0; i < cl.size(); i++)
301                         {
302                                 if (cl[i]->GetExt("cm_protect_"+std::string(channel->name)))
303                                 {
304                                         WriteServ(user->fd,"388 %s %s %s",user->nick, channel->name,cl[i]->nick);
305                                 }
306                         }
307                         WriteServ(user->fd,"389 %s %s :End of channel protected user list",user->nick, channel->name);
308                 }
309
310         }
311         
312         virtual ~ModuleChanProtect()
313         {
314                 DELETE(Conf);
315         }
316         
317         virtual Version GetVersion()
318         {
319                 return Version(1,0,0,0,VF_STATIC|VF_VENDOR);
320         }
321         
322         virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
323         {
324                 // this is called when the server is linking into a net and wants to sync channel data.
325                 // we should send our mode changes for the channel here to ensure that other servers
326                 // know whos +q/+a on the channel.
327                 chanuserlist cl = Srv->GetUsers(chan);
328                 string_list commands;
329                 for (unsigned int i = 0; i < cl.size(); i++)
330                 {
331                         if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
332                         {
333                                 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+q "+std::string(cl[i]->nick));
334                         }
335                         if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
336                         {
337                                 proto->ProtoSendMode(opaque,TYPE_CHANNEL,chan,"+a "+std::string(cl[i]->nick));
338                         }
339                 }
340         }
341
342 };
343
344
345 class ModuleChanProtectFactory : public ModuleFactory
346 {
347  public:
348         ModuleChanProtectFactory()
349         {
350         }
351         
352         ~ModuleChanProtectFactory()
353         {
354         }
355         
356         virtual Module * CreateModule(Server* Me)
357         {
358                 return new ModuleChanProtect(Me);
359         }
360         
361 };
362
363
364 extern "C" void * init_module( void )
365 {
366         return new ModuleChanProtectFactory;
367 }