]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chanprotect.cpp
a37b4d325fb8786eb735069d33b947e213b2f107
[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                 std::string val = Conf->ReadValue("options","noservices",0);
36                 FirstInGetsFounder = ((val == "yes") || (val == "1") || (val == "true"));
37         }
38         
39         virtual void OnRehash()
40         {
41                 // on a rehash we delete our classes for good measure and create them again.
42                 delete Conf;
43                 Conf = new ConfigReader;
44                 // re-read our config options on a rehash
45                 std::string val = Conf->ReadValue("options","noservices",0);
46                 FirstInGetsFounder = ((val == "yes") || (val == "1") || (val == "true"));
47         }
48         
49         virtual void OnUserJoin(userrec* user, chanrec* channel)
50         {
51                 // if the user is the first user into the channel, mark them as the founder, but only if
52                 // the config option for it is set
53                 if (FirstInGetsFounder)
54                 {
55                         if (Srv->CountUsers(channel) == 1)
56                         {
57                                 if (user->Extend("cm_founder_"+std::string(channel->name),dummyvalue))
58                                 {
59                                         Srv->Log(DEBUG,"Marked user "+std::string(user->nick)+" as founder for "+std::string(channel->name));
60                                 }
61                         }
62                 }
63         }
64         
65         virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
66         {
67                 // don't allow action if:
68                 // (A) Theyre founder (no matter what)
69                 // (B) Theyre protected, and you're not
70                 // always allow the action if:
71                 // (A) The source is ulined
72                 
73                 if ((Srv->IsUlined(source->nick)) || (Srv->IsUlined(source->server)))
74                 {
75                         return ACR_ALLOW;
76                 }
77
78                 switch (access_type)
79                 {
80                         case AC_DEOP:
81                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
82                                 {
83                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as the're a channel founder");
84                                         return ACR_DENY;
85                                 }
86                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
87                                 {
88                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't deop "+std::string(dest->nick)+" as the're protected (+a)");
89                                         return ACR_DENY;
90                                 }
91                         break;
92
93                         case AC_KICK:
94                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
95                                 {
96                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as the're a channel founder");
97                                         return ACR_DENY;
98                                 }
99                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
100                                 {
101                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't kick "+std::string(dest->nick)+" as the're protected (+a)");
102                                         return ACR_DENY;
103                                 }
104                         break;
105
106                         case AC_DEHALFOP:
107                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
108                                 {
109                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as the're a channel founder");
110                                         return ACR_DENY;
111                                 }
112                                 if ((dest->GetExt("cm_protect_"+std::string(channel->name))) && (!source->GetExt("cm_protect_"+std::string(channel->name))))
113                                 {
114                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't de-halfop "+std::string(dest->nick)+" as the're protected (+a)");
115                                         return ACR_DENY;
116                                 }
117                         break;
118
119                         case AC_DEVOICE:
120                                 if (dest->GetExt("cm_founder_"+std::string(channel->name)))
121                                 {
122                                         Srv->SendServ(source->fd,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+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,"482 "+std::string(source->nick)+" "+std::string(channel->name)+" :Can't devoice "+std::string(dest->nick)+" as the're protected (+a)");
128                                         return ACR_DENY;
129                                 }
130                         break;
131                 }
132                 return ACR_DEFAULT;
133         }
134         
135         virtual int OnExtendedMode(userrec* user, void* target, char modechar, int type, bool mode_on, string_list &params)
136         {
137                 // not out mode, bail
138                 if ((modechar == 'q') && (type == MT_CHANNEL))
139                 {
140                         // set up parameters
141                         chanrec* chan = (chanrec*)target;
142                         userrec* theuser = Srv->FindNick(params[0]);
143                 
144                         // cant find the user given as the parameter, eat the mode change.
145                         if (!theuser)
146                                 return -1;
147                         
148                         // given user isnt even on the channel, eat the mode change
149                         if (!Srv->IsOnChannel(theuser,chan))
150                                 return -1;
151                         
152                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")))
153                         {
154                                 if (mode_on)
155                                 {
156                                         if (!theuser->GetExt("cm_founder_"+std::string(chan->name)))
157                                         {
158                                                 theuser->Extend("cm_founder_"+std::string(chan->name),dummyvalue);
159                                                 return 1;
160                                         }
161                                 }
162                                 else
163                                 {
164                                         if (theuser->GetExt("cm_founder_"+std::string(chan->name)))
165                                         {
166                                                 theuser->Shrink("cm_founder_"+std::string(chan->name));
167                                                 return 1;
168                                         }
169                                 }       
170
171                                 return -1;
172                         }
173                         else
174                         {
175                                 WriteServ(user->fd,"482 %s %s :Only servers may set channel mode +q",user->nick, chan->name);
176                                 return -1;
177                         }
178                 }
179                 if ((modechar == 'a') && (type == MT_CHANNEL))
180                 {
181                         // set up parameters
182                         chanrec* chan = (chanrec*)target;
183                         userrec* theuser = Srv->FindNick(params[0]);
184                 
185                         // cant find the user given as the parameter, eat the mode change.
186                         if (!theuser)
187                                 return -1;
188                         
189                         // given user isnt even on the channel, eat the mode change
190                         if (!Srv->IsOnChannel(theuser,chan))
191                                 return -1;
192                         
193                         if ((Srv->IsUlined(user->nick)) || (Srv->IsUlined(user->server)) || (!strcmp(user->server,"")) || (user->GetExt("cm_founder_"+std::string(chan->name))))
194                         {
195                                 if (mode_on)
196                                 {
197                                         if (!theuser->GetExt("cm_protect_"+std::string(chan->name)))
198                                         {
199                                                 theuser->Extend("cm_protect_"+std::string(chan->name),dummyvalue);
200                                                 return 1;
201                                         }
202                                 }
203                                 else
204                                 {
205                                         if (theuser->GetExt("cm_protect_"+std::string(chan->name)))
206                                         {
207                                                 theuser->Shrink("cm_protect_"+std::string(chan->name));
208                                                 return 1;
209                                         }
210                                 }       
211
212                                 return -1;
213                         }
214                         else
215                         {
216                                 WriteServ(user->fd,"482 %s %s :You are not a channel founder",user->nick, chan->name);
217                                 return -1;
218                         }
219                 }
220                 return 0;
221         }
222         
223         virtual ~ModuleChanProtect()
224         {
225                 delete Conf;
226                 delete Srv;
227         }
228         
229         virtual Version GetVersion()
230         {
231                 return Version(1,0,0,0);
232         }
233         
234         virtual string_list OnChannelSync(chanrec* chan)
235         {
236                 chanuserlist cl = Srv->GetUsers(chan);
237                 string_list commands;
238                 for (int i = 0; i < cl.size(); i++)
239                 {
240                         if (cl[i]->GetExt("cm_founder_"+std::string(chan->name)))
241                         {
242                                 commands.push_back("M "+std::string(chan->name)+" +q "+std::string(cl[i]->nick));
243                         }
244                         if (cl[i]->GetExt("cm_protect_"+std::string(chan->name)))
245                         {
246                                 commands.push_back("M "+std::string(chan->name)+" +a "+std::string(cl[i]->nick));
247                         }
248                 }
249                 return commands;
250         }
251
252 };
253
254
255 class ModuleChanProtectFactory : public ModuleFactory
256 {
257  public:
258         ModuleChanProtectFactory()
259         {
260         }
261         
262         ~ModuleChanProtectFactory()
263         {
264         }
265         
266         virtual Module * CreateModule()
267         {
268                 return new ModuleChanProtect;
269         }
270         
271 };
272
273
274 extern "C" void * init_module( void )
275 {
276         return new ModuleChanProtectFactory;
277 }
278