]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
Fix IO hooking modules to use the new (not old) hooking call
[user/henk/code/inspircd.git] / src / modules / m_override.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides support for unreal-style oper-override */
17
18 typedef std::map<std::string,std::string> override_t;
19
20 class ModuleOverride : public Module
21 {
22         override_t overrides;
23         bool RequireKey;
24         bool NoisyOverride;
25         bool OverriddenMode;
26         int OverOps, OverDeops, OverVoices, OverDevoices, OverHalfops, OverDehalfops;
27
28  public:
29
30         ModuleOverride(InspIRCd* Me)
31                 : Module(Me)
32         {
33                 // read our config options (main config file)
34                 OnRehash(NULL,"");
35                 ServerInstance->SNO->EnableSnomask('O', "OVERRIDE");
36                 OverriddenMode = false;
37                 OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
38                 Implementation eventlist[] = { I_OnRehash, I_OnAccessCheck, I_On005Numeric, I_OnUserPreJoin, I_OnUserPreKick, I_OnPostCommand, I_OnLocalTopicChange };
39                 ServerInstance->Modules->Attach(eventlist, this, 7);
40         }
41
42         virtual void OnRehash(User* user, const std::string &parameter)
43         {
44                 // on a rehash we delete our classes for good measure and create them again.
45                 ConfigReader* Conf = new ConfigReader(ServerInstance);
46
47                 // re-read our config options on a rehash
48                 NoisyOverride = Conf->ReadFlag("override", "noisy", 0);
49                 RequireKey = Conf->ReadFlag("override", "requirekey", 0);
50
51                 overrides.clear();
52
53                 for (int j =0; j < Conf->Enumerate("type"); j++)
54                 {
55                         std::string typen = Conf->ReadValue("type","name",j);
56                         std::string tokenlist = Conf->ReadValue("type","override",j);
57                         overrides[typen] = tokenlist;
58                 }
59
60                 delete Conf;
61         }
62
63
64         virtual void OnPostCommand(const std::string &command, const std::vector<std::string> &parameters, User *user, CmdResult result, const std::string &original_line)
65         {
66                 if ((NoisyOverride) && (OverriddenMode) && (irc::string(command.c_str()) == "MODE") && (result == CMD_SUCCESS))
67                 {
68                         int Total = OverOps + OverDeops + OverVoices + OverDevoices + OverHalfops + OverDehalfops;
69
70                         ServerInstance->SNO->WriteToSnoMask('O',std::string(user->nick)+" Overriding modes: "+ServerInstance->Modes->GetLastParse()+" "+(Total ? "[Detail: " : "")+
71                                         (OverOps ? ConvToStr(OverOps)+" op"+(OverOps != 1 ? "s" : "")+" " : "")+
72                                         (OverDeops ? ConvToStr(OverDeops)+" deop"+(OverDeops != 1 ? "s" : "")+" " : "")+
73                                         (OverVoices ? ConvToStr(OverVoices)+" voice"+(OverVoices != 1 ? "s" : "")+" " : "")+
74                                         (OverDevoices ? ConvToStr(OverDevoices)+" devoice"+(OverDevoices != 1 ? "s" : "")+" " : "")+
75                                         (OverHalfops ? ConvToStr(OverHalfops)+" halfop"+(OverHalfops != 1 ? "s" : "")+" " : "")+
76                                         (OverDehalfops ? ConvToStr(OverDehalfops)+" dehalfop"+(OverDehalfops != 1 ? "s" : "") : "")
77                                         +(Total ? "]" : ""));
78
79                         OverriddenMode = false;
80                         OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
81                 }
82         }
83
84         virtual void On005Numeric(std::string &output)
85         {
86                 output.append(" OVERRIDE");
87         }
88
89         virtual bool CanOverride(User* source, const char* token)
90         {
91                 // checks to see if the oper's type has <type:override>
92                 override_t::iterator j = overrides.find(source->oper);
93
94                 if (j != overrides.end())
95                 {
96                         // its defined or * is set, return its value as a boolean for if the token is set
97                         return ((j->second.find(token, 0) != std::string::npos) || (j->second.find("*", 0) != std::string::npos));
98                 }
99
100                 // its not defined at all, count as false
101                 return false;
102         }
103
104
105         virtual int OnLocalTopicChange(User *source, Channel *channel, const std::string &topic)
106         {
107                 if (IS_OPER(source) && CanOverride(source, "TOPIC"))
108                 {
109                         if (!channel->HasUser(source) || (channel->IsModeSet('t') && channel->GetStatus(source) < STATUS_HOP))
110                         {
111                                 ServerInstance->SNO->WriteToSnoMask('O',std::string(source->nick)+"  used oper override to change a topic on "+std::string(channel->name));
112                         }
113
114                         // Explicit allow
115                         return -1;
116                 }
117
118                 return 0;
119         }
120
121         virtual int OnUserPreKick(User* source, User* user, Channel* chan, const std::string &reason)
122         {
123                 if (IS_OPER(source) && CanOverride(source,"KICK"))
124                 {
125                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
126                         if ((chan->GetStatus(source) < chan->GetStatus(user))                   || (chan->GetStatus(source) <= STATUS_VOICE))
127                         {                               
128                                 ServerInstance->SNO->WriteToSnoMask('O',std::string(source->nick)+" used oper override to kick "+std::string(user->nick)+" on "+std::string(chan->name)+" ("+reason+")");
129                         }
130                         /* Returning -1 explicitly allows the kick */
131                         return -1;
132                 }
133                 return 0;
134         }
135
136         virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
137         {
138                 if (IS_OPER(source))
139                 {
140                         if (source && channel)
141                         {
142                                 // Fix by brain - allow the change if they arent on channel - rely on boolean short-circuit
143                                 // to not check the other items in the statement if they arent on the channel
144                                 int mode = channel->GetStatus(source);
145                                 switch (access_type)
146                                 {
147                                         case AC_DEOP:
148                                                 if (CanOverride(source,"MODEDEOP"))
149                                                 {
150                                                         if (NoisyOverride)
151                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
152                                                                 OverDeops++;
153                                                         return ACR_ALLOW;
154                                                 }
155                                                 else
156                                                 {
157                                                         return ACR_DEFAULT;
158                                                 }
159                                         break;
160                                         case AC_OP:
161                                                 if (CanOverride(source,"MODEOP"))
162                                                 {
163                                                         if (NoisyOverride)
164                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
165                                                                 OverOps++;
166                                                         return ACR_ALLOW;
167                                                 }
168                                                 else
169                                                 {
170                                                         return ACR_DEFAULT;
171                                                 }
172                                         break;
173                                         case AC_VOICE:
174                                                 if (CanOverride(source,"MODEVOICE"))
175                                                 {
176                                                         if (NoisyOverride)
177                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
178                                                                 OverVoices++;
179                                                         return ACR_ALLOW;
180                                                 }
181                                                 else
182                                                 {
183                                                         return ACR_DEFAULT;
184                                                 }
185                                         break;
186                                         case AC_DEVOICE:
187                                                 if (CanOverride(source,"MODEDEVOICE"))
188                                                 {
189                                                         if (NoisyOverride)
190                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
191                                                                 OverDevoices++;
192                                                         return ACR_ALLOW;
193                                                 }
194                                                 else
195                                                 {
196                                                         return ACR_DEFAULT;
197                                                 }
198                                         break;
199                                         case AC_HALFOP:
200                                                 if (CanOverride(source,"MODEHALFOP"))
201                                                 {
202                                                         if (NoisyOverride)
203                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
204                                                                 OverHalfops++;
205                                                         return ACR_ALLOW;
206                                                 }
207                                                 else
208                                                 {
209                                                         return ACR_DEFAULT;
210                                                 }
211                                         break;
212                                         case AC_DEHALFOP:
213                                                 if (CanOverride(source,"MODEDEHALFOP"))
214                                                 {
215                                                         if (NoisyOverride)
216                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
217                                                                 OverDehalfops++;
218                                                         return ACR_ALLOW;
219                                                 }
220                                                 else
221                                                 {
222                                                         return ACR_DEFAULT;
223                                                 }
224                                         break;
225                                 }
226
227                                 if (CanOverride(source,"OTHERMODE"))
228                                 {
229                                         if (NoisyOverride)
230                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
231                                         {
232                                                 OverriddenMode = true;
233                                                 OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
234                                         }
235                                         return ACR_ALLOW;
236                                 }
237                                 else
238                                 {
239                                         return ACR_DEFAULT;
240                                 }
241                         }
242                 }
243
244                 return ACR_DEFAULT;
245         }
246
247         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
248         {
249                 if (IS_LOCAL(user) && IS_OPER(user))
250                 {
251                         if (chan)
252                         {
253                                 if ((chan->modes[CM_INVITEONLY]) && (CanOverride(user,"INVITE")))
254                                 {
255                                         irc::string x(chan->name.c_str());
256                                         if (!user->IsInvited(x))
257                                         {
258                                                 if (RequireKey && keygiven != "override")
259                                                 {
260                                                         // Can't join normally -- must use a special key to bypass restrictions
261                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
262                                                         return 1;
263                                                 }
264
265                                                 if (NoisyOverride)
266                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass invite-only", cname, user->nick.c_str());
267                                                 ServerInstance->SNO->WriteToSnoMask('O', user->nick+" used oper override to bypass +i on "+std::string(cname));
268                                         }
269                                         return -1;
270                                 }
271
272                                 if ((chan->modes[CM_KEY]) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter('k'))
273                                 {
274                                         if (RequireKey && keygiven != "override")
275                                         {
276                                                 // Can't join normally -- must use a special key to bypass restrictions
277                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
278                                                 return 1;
279                                         }
280
281                                         if (NoisyOverride)
282                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel key", cname, user->nick.c_str());
283                                         ServerInstance->SNO->WriteToSnoMask('O', user->nick+" used oper override to bypass +k on "+std::string(cname));
284                                         return -1;
285                                 }
286
287                                 if ((chan->modes[CM_LIMIT]) && (chan->GetUserCounter() >=  atoi(chan->GetModeParameter('l').c_str())) && (CanOverride(user,"LIMIT")))
288                                 {
289                                         if (RequireKey && keygiven != "override")
290                                         {
291                                                 // Can't join normally -- must use a special key to bypass restrictions
292                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
293                                                 return 1;
294                                         }
295
296                                         if (NoisyOverride)
297                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel limit", cname, user->nick.c_str());
298                                         ServerInstance->SNO->WriteToSnoMask('O', user->nick+" used oper override to bypass +l on "+std::string(cname));
299                                         return -1;
300                                 }
301
302                                 if (CanOverride(user,"BANWALK"))
303                                 {
304                                         if (chan->IsBanned(user))
305                                         {
306                                                 if (RequireKey && keygiven != "override")
307                                                 {
308                                                         // Can't join normally -- must use a special key to bypass restrictions
309                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
310                                                         return 1;
311                                                 }
312
313                                                 if (NoisyOverride)
314                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname, user->nick.c_str());
315                                                 ServerInstance->SNO->WriteToSnoMask('O',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname);
316                                         }
317                                         return -1;
318                                 }
319                         }
320                 }
321                 return 0;
322         }
323
324         virtual ~ModuleOverride()
325         {
326                 ServerInstance->SNO->DisableSnomask('O');
327         }
328
329         virtual Version GetVersion()
330         {
331                 return Version("$Id$",VF_VENDOR,API_VERSION);
332         }
333 };
334
335 MODULE_INIT(ModuleOverride)