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