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