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