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