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