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