]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
c21b7f41329272511b3c38120794d097bf6f639e
[user/henk/code/inspircd.git] / src / modules / m_override.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 #include "configreader.h"
22 #include "inspircd.h"
23
24 /* $ModDesc: Provides support for unreal-style oper-override */
25
26 extern InspIRCd* ServerInstance;
27
28 typedef std::map<std::string,std::string> override_t;
29
30 class ModuleOverride : public Module
31 {
32         Server *Srv;
33         override_t overrides;
34         bool NoisyOverride;
35  public:
36  
37         ModuleOverride(InspIRCd* Me)
38                 : Module::Module(Me)
39         {
40         
41                 // here we initialise our module. Use new to create new instances of the required
42                 // classes.
43                 
44                 
45                 
46                 // read our config options (main config file)
47                 OnRehash("");
48         }
49         
50         virtual void OnRehash(const std::string &parameter)
51         {
52                 // on a rehash we delete our classes for good measure and create them again.
53                 ConfigReader* Conf = new ConfigReader;
54                 
55                 // re-read our config options on a rehash
56                 NoisyOverride = Conf->ReadFlag("override","noisy",0);
57                 overrides.clear();
58                 for (int j =0; j < Conf->Enumerate("type"); j++)
59                 {
60                         std::string typen = Conf->ReadValue("type","name",j);
61                         std::string tokenlist = Conf->ReadValue("type","override",j);
62                         overrides[typen] = tokenlist;
63                 }
64                 
65                 DELETE(Conf);
66         }
67
68         void Implements(char* List)
69         {
70                 List[I_OnRehash] = List[I_OnAccessCheck] = List[I_On005Numeric] = List[I_OnUserPreJoin] = List[I_OnUserPreKick] = 1;
71         }
72
73         virtual void On005Numeric(std::string &output)
74         {
75                 output.append(" OVERRIDE");
76         }
77
78         virtual bool CanOverride(userrec* source, char* token)
79         {
80                 // checks to see if the oper's type has <type:override>
81                 override_t::iterator j = overrides.find(source->oper);
82                 
83                 if (j != overrides.end())
84                 {
85                         // its defined, return its value as a boolean for if the token is set
86                         return (j->second.find(token, 0) != std::string::npos);
87                 }
88                 
89                 // its not defined at all, count as false
90                 return false;
91         }
92
93         virtual int OnUserPreKick(userrec* source, userrec* user, chanrec* chan, const std::string &reason)
94         {
95                 if ((*source->oper) && (CanOverride(source,"KICK")))
96                 {
97                         if (((chan->GetStatus(source) == STATUS_HOP) && (chan->GetStatus(user) == STATUS_OP)) || (chan->GetStatus(source) < STATUS_VOICE))
98                         {
99                                 ServerInstance->WriteOpers("*** NOTICE: "+std::string(source->nick)+" Override-Kicked "+std::string(user->nick)+" on "+std::string(chan->name)+" ("+reason+")");
100                         }
101                         /* Returning -1 explicitly allows the kick */
102                         return -1;
103                 }
104                 return 0;
105         }
106         
107         virtual int OnAccessCheck(userrec* source,userrec* dest,chanrec* channel,int access_type)
108         {
109                 if (*source->oper)
110                 {
111                         if ((Srv) && (source) && (channel))
112                         {
113                                 // Fix by brain - allow the change if they arent on channel - rely on boolean short-circuit
114                                 // to not check the other items in the statement if they arent on the channel
115                                 int mode = channel->GetStatus(source);
116                                 if ((!channel->HasUser(source)) || ((mode != STATUS_HOP) && (mode != STATUS_OP)))
117                                 {
118                                         switch (access_type)
119                                         {
120                                                 case AC_DEOP:
121                                                         if (CanOverride(source,"MODEDEOP"))
122                                                         {
123                                                                 ServerInstance->WriteOpers("*** NOTICE: "+std::string(source->nick)+" Override-Deopped "+std::string(dest->nick)+" on "+std::string(channel->name));
124                                                                 return ACR_ALLOW;
125                                                         }
126                                                         else
127                                                         {
128                                                                 return ACR_DEFAULT;
129                                                         }
130
131                                                 case AC_OP:
132                                                         if (CanOverride(source,"MODEOP"))
133                                                         {
134                                                                 ServerInstance->WriteOpers("*** NOTICE: "+std::string(source->nick)+" Override-Opped "+std::string(dest->nick)+" on "+std::string(channel->name));
135                                                                 return ACR_ALLOW;
136                                                         }
137                                                         else
138                                                         {
139                                                                 return ACR_DEFAULT;
140                                                         }
141
142                                                 case AC_VOICE:
143                                                         if (CanOverride(source,"MODEVOICE"))
144                                                         {
145                                                                 ServerInstance->WriteOpers("*** NOTICE: "+std::string(source->nick)+" Override-Voiced "+std::string(dest->nick)+" on "+std::string(channel->name));
146                                                                 return ACR_ALLOW;
147                                                         }
148                                                         else
149                                                         {
150                                                                 return ACR_DEFAULT;
151                                                         }
152
153                                                 case AC_DEVOICE:
154                                                         if (CanOverride(source,"MODEDEVOICE"))
155                                                         {
156                                                                 ServerInstance->WriteOpers("*** NOTICE: "+std::string(source->nick)+" Override-Devoiced "+std::string(dest->nick)+" on "+std::string(channel->name));
157                                                                 return ACR_ALLOW;
158                                                         }
159                                                         else
160                                                         {
161                                                                 return ACR_DEFAULT;
162                                                         }
163
164                                                 case AC_HALFOP:
165                                                         if (CanOverride(source,"MODEHALFOP"))
166                                                         {
167                                                                 ServerInstance->WriteOpers("*** NOTICE: "+std::string(source->nick)+" Override-Halfopped "+std::string(dest->nick)+" on "+std::string(channel->name));
168                                                                 return ACR_ALLOW;
169                                                         }
170                                                         else
171                                                         {
172                                                                 return ACR_DEFAULT;
173                                                         }
174
175                                                 case AC_DEHALFOP:
176                                                         if (CanOverride(source,"MODEDEHALFOP"))
177                                                         {
178                                                                 ServerInstance->WriteOpers("*** NOTICE: "+std::string(source->nick)+" Override-Dehalfopped "+std::string(dest->nick)+" on "+std::string(channel->name));
179                                                                 return ACR_ALLOW;
180                                                         }
181                                                         else
182                                                         {
183                                                                 return ACR_DEFAULT;
184                                                         }
185                                         }
186                                 }
187                         }
188                         
189                         if (CanOverride(source,"OTHERMODE"))
190                         {
191                                 return ACR_ALLOW;
192                         }
193                         else
194                         {
195                                 return ACR_DEFAULT;
196                         }
197                 }
198
199                 return ACR_DEFAULT;
200         }
201         
202         virtual int OnUserPreJoin(userrec* user, chanrec* chan, const char* cname)
203         {
204                 if (*user->oper)
205                 {
206                         if (chan)
207                         {
208                                 if ((chan->modes[CM_INVITEONLY]) && (CanOverride(user,"INVITE")))
209                                 {
210                                         if (NoisyOverride)
211                                         {
212                                                 irc::string x = chan->name;
213                                                 if (!user->IsInvited(x))
214                                                 {
215                                                         /* XXX - Ugly cast for a parameter that isn't used? :< - Om */
216                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s invited himself into the channel", cname, user->nick);
217                                                 }
218                                         }
219                                         ServerInstance->WriteOpers("*** "+std::string(user->nick)+" used operoverride to bypass +i on "+std::string(cname));
220                                         return -1;
221                                 }
222                                 
223                                 if ((chan->key[0]) && (CanOverride(user,"KEY")))
224                                 {
225                                         if (NoisyOverride)
226                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s bypassed the channel key", cname, user->nick);
227                                         ServerInstance->WriteOpers("*** "+std::string(user->nick)+" used operoverride to bypass +k on "+std::string(cname));
228                                         return -1;
229                                 }
230                                         
231                                 if ((chan->limit > 0) && (chan->GetUserCounter() >=  chan->limit) && (CanOverride(user,"LIMIT")))
232                                 {
233                                         if (NoisyOverride)
234                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s passed through your channel limit", cname, user->nick);
235                                         ServerInstance->WriteOpers("*** "+std::string(user->nick)+" used operoverride to bypass +l on "+std::string(cname));
236                                         return -1;
237                                 }
238
239                                 if (CanOverride(user,"BANWALK"))
240                                 {
241                                         // other join
242                                         return -1;
243                                 }
244                         }
245                 }
246                 return 0;
247         }
248         
249         virtual ~ModuleOverride()
250         {
251         }
252         
253         virtual Version GetVersion()
254         {
255                 return Version(1,0,0,1,VF_VENDOR);
256         }
257 };
258
259
260 class ModuleOverrideFactory : public ModuleFactory
261 {
262  public:
263         ModuleOverrideFactory()
264         {
265         }
266         
267         ~ModuleOverrideFactory()
268         {
269         }
270         
271         virtual Module * CreateModule(InspIRCd* Me)
272         {
273                 return new ModuleOverride(Me);
274         }
275         
276 };
277
278
279 extern "C" void * init_module( void )
280 {
281         return new ModuleOverrideFactory;
282 }