]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
Fix typo opermoth -> opermotd. Thanks Ankit.
[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('G', "GODMODE");
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 ((OverriddenMode) && (irc::string(command.c_str()) == "MODE") && (result == CMD_SUCCESS))
67                 {
68                         int Total = OverOps + OverDeops + OverVoices + OverDevoices + OverHalfops + OverDehalfops;
69                         if (Total == 0)
70                                 return;
71
72                         ServerInstance->SNO->WriteToSnoMask('G',std::string(user->nick)+" Overriding modes: "+ServerInstance->Modes->GetLastParse()+" "+(Total ? "[Detail: " : "")+
73                                         (OverOps ? ConvToStr(OverOps)+" op"+(OverOps != 1 ? "s" : "")+" " : "")+
74                                         (OverDeops ? ConvToStr(OverDeops)+" deop"+(OverDeops != 1 ? "s" : "")+" " : "")+
75                                         (OverVoices ? ConvToStr(OverVoices)+" voice"+(OverVoices != 1 ? "s" : "")+" " : "")+
76                                         (OverDevoices ? ConvToStr(OverDevoices)+" devoice"+(OverDevoices != 1 ? "s" : "")+" " : "")+
77                                         (OverHalfops ? ConvToStr(OverHalfops)+" halfop"+(OverHalfops != 1 ? "s" : "")+" " : "")+
78                                         (OverDehalfops ? ConvToStr(OverDehalfops)+" dehalfop"+(OverDehalfops != 1 ? "s" : "") : "")
79                                         +(Total ? "]" : ""));
80
81                         OverriddenMode = false;
82                         OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
83                 }
84         }
85
86         virtual void On005Numeric(std::string &output)
87         {
88                 output.append(" OVERRIDE");
89         }
90
91         virtual bool CanOverride(User* source, const char* token)
92         {
93                 // checks to see if the oper's type has <type:override>
94                 override_t::iterator j = overrides.find(source->oper);
95
96                 if (j != overrides.end())
97                 {
98                         // its defined or * is set, return its value as a boolean for if the token is set
99                         return ((j->second.find(token, 0) != std::string::npos) || (j->second.find("*", 0) != std::string::npos));
100                 }
101
102                 // its not defined at all, count as false
103                 return false;
104         }
105
106
107         virtual int OnLocalTopicChange(User *source, Channel *channel, const std::string &topic)
108         {
109                 if (IS_OPER(source) && CanOverride(source, "TOPIC"))
110                 {
111                         if (!channel->HasUser(source) || (channel->IsModeSet('t') && channel->GetStatus(source) < STATUS_HOP))
112                         {
113                                 ServerInstance->SNO->WriteToSnoMask('G',std::string(source->nick)+"  used oper override to change a topic on "+std::string(channel->name));
114                         }
115
116                         // Explicit allow
117                         return -1;
118                 }
119
120                 return 0;
121         }
122
123         virtual int OnUserPreKick(User* source, User* user, Channel* chan, const std::string &reason)
124         {
125                 if (IS_OPER(source) && CanOverride(source,"KICK"))
126                 {
127                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
128                         if ((chan->GetStatus(source) < chan->GetStatus(user))                   || (chan->GetStatus(source) <= STATUS_VOICE))
129                         {                               
130                                 ServerInstance->SNO->WriteToSnoMask('G',std::string(source->nick)+" used oper override to kick "+std::string(user->nick)+" on "+std::string(chan->name)+" ("+reason+")");
131                         }
132                         /* Returning -1 explicitly allows the kick */
133                         return -1;
134                 }
135                 return 0;
136         }
137
138         virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
139         {
140                 if (IS_OPER(source))
141                 {
142                         if (source && channel)
143                         {
144                                 // Fix by brain - allow the change if they arent on channel - rely on boolean short-circuit
145                                 // to not check the other items in the statement if they arent on the channel
146                                 int mode = channel->GetStatus(source);
147                                 switch (access_type)
148                                 {
149                                         case AC_DEOP:
150                                                 if (CanOverride(source,"MODEDEOP"))
151                                                 {
152                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
153                                                                 OverDeops++;
154                                                         return ACR_ALLOW;
155                                                 }
156                                                 else
157                                                 {
158                                                         return ACR_DEFAULT;
159                                                 }
160                                         break;
161                                         case AC_OP:
162                                                 if (CanOverride(source,"MODEOP"))
163                                                 {
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 ((!channel->HasUser(source)) || (mode < STATUS_HOP))
177                                                                 OverVoices++;
178                                                         return ACR_ALLOW;
179                                                 }
180                                                 else
181                                                 {
182                                                         return ACR_DEFAULT;
183                                                 }
184                                         break;
185                                         case AC_DEVOICE:
186                                                 if (CanOverride(source,"MODEDEVOICE"))
187                                                 {
188                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
189                                                                 OverDevoices++;
190                                                         return ACR_ALLOW;
191                                                 }
192                                                 else
193                                                 {
194                                                         return ACR_DEFAULT;
195                                                 }
196                                         break;
197                                         case AC_HALFOP:
198                                                 if (CanOverride(source,"MODEHALFOP"))
199                                                 {
200                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
201                                                                 OverHalfops++;
202                                                         return ACR_ALLOW;
203                                                 }
204                                                 else
205                                                 {
206                                                         return ACR_DEFAULT;
207                                                 }
208                                         break;
209                                         case AC_DEHALFOP:
210                                                 if (CanOverride(source,"MODEDEHALFOP"))
211                                                 {
212                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
213                                                                 OverDehalfops++;
214                                                         return ACR_ALLOW;
215                                                 }
216                                                 else
217                                                 {
218                                                         return ACR_DEFAULT;
219                                                 }
220                                         break;
221                                 }
222
223                                 if (CanOverride(source,"OTHERMODE"))
224                                 {
225                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
226                                         {
227                                                 OverriddenMode = true;
228                                                 OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
229                                         }
230                                         return ACR_ALLOW;
231                                 }
232                                 else
233                                 {
234                                         return ACR_DEFAULT;
235                                 }
236                         }
237                 }
238
239                 return ACR_DEFAULT;
240         }
241
242         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
243         {
244                 if (IS_LOCAL(user) && IS_OPER(user))
245                 {
246                         if (chan)
247                         {
248                                 if ((chan->modes[CM_INVITEONLY]) && (CanOverride(user,"INVITE")))
249                                 {
250                                         irc::string x(chan->name.c_str());
251                                         if (!user->IsInvited(x))
252                                         {
253                                                 if (RequireKey && keygiven != "override")
254                                                 {
255                                                         // Can't join normally -- must use a special key to bypass restrictions
256                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
257                                                         return 1;
258                                                 }
259
260                                                 if (NoisyOverride)
261                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass invite-only", cname, user->nick.c_str());
262                                                 ServerInstance->SNO->WriteToSnoMask('G', user->nick+" used oper override to bypass +i on "+std::string(cname));
263                                         }
264                                         return -1;
265                                 }
266
267                                 if ((chan->modes[CM_KEY]) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter('k'))
268                                 {
269                                         if (RequireKey && keygiven != "override")
270                                         {
271                                                 // Can't join normally -- must use a special key to bypass restrictions
272                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
273                                                 return 1;
274                                         }
275
276                                         if (NoisyOverride)
277                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel key", cname, user->nick.c_str());
278                                         ServerInstance->SNO->WriteToSnoMask('G', user->nick+" used oper override to bypass +k on "+std::string(cname));
279                                         return -1;
280                                 }
281
282                                 if ((chan->modes[CM_LIMIT]) && (chan->GetUserCounter() >=  atoi(chan->GetModeParameter('l').c_str())) && (CanOverride(user,"LIMIT")))
283                                 {
284                                         if (RequireKey && keygiven != "override")
285                                         {
286                                                 // Can't join normally -- must use a special key to bypass restrictions
287                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
288                                                 return 1;
289                                         }
290
291                                         if (NoisyOverride)
292                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel limit", cname, user->nick.c_str());
293                                         ServerInstance->SNO->WriteToSnoMask('G', user->nick+" used oper override to bypass +l on "+std::string(cname));
294                                         return -1;
295                                 }
296
297                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
298                                 {
299                                         if (RequireKey && keygiven != "override")
300                                         {
301                                                 // Can't join normally -- must use a special key to bypass restrictions
302                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
303                                                 return 1;
304                                         }
305
306                                         if (NoisyOverride)
307                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname, user->nick.c_str());
308                                         ServerInstance->SNO->WriteToSnoMask('G',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname);
309                                         return -1;
310                                 }
311                         }
312                 }
313                 return 0;
314         }
315
316         virtual ~ModuleOverride()
317         {
318                 ServerInstance->SNO->DisableSnomask('G');
319         }
320
321         virtual Version GetVersion()
322         {
323                 return Version("$Id$",VF_VENDOR,API_VERSION);
324         }
325 };
326
327 MODULE_INIT(ModuleOverride)