]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
Note to self, cast time_t to long int for printf... thanks Ankit for pointing this...
[user/henk/code/inspircd.git] / src / modules / m_override.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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 #include "m_override.h"
16
17 /* $ModDesc: Provides support for unreal-style oper-override */
18
19 typedef std::map<std::string,std::string> override_t;
20
21 class ModuleOverride : public Module
22 {
23         override_t overrides;
24         bool RequireKey;
25         bool NoisyOverride;
26         bool OverriddenMode;
27         int OverOps, OverDeops, OverVoices, OverDevoices, OverHalfops, OverDehalfops;
28
29  public:
30
31         ModuleOverride(InspIRCd* Me)
32                 : Module(Me)
33         {
34                 // read our config options (main config file)
35                 OnRehash(NULL,"");
36                 ServerInstance->SNO->EnableSnomask('G', "GODMODE");
37                 OverriddenMode = false;
38                 if (!ServerInstance->Modules->PublishFeature("Override", this))
39                 {
40                         throw ModuleException("m_override: Unable to publish feature 'Override'");
41                 }
42                 OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
43                 Implementation eventlist[] = { I_OnRehash, I_OnAccessCheck, I_On005Numeric, I_OnUserPreJoin, I_OnUserPreKick, I_OnPostCommand, I_OnLocalTopicChange, I_OnRequest };
44                 ServerInstance->Modules->Attach(eventlist, this, 8);
45         }
46
47         virtual void OnRehash(User* user, const std::string &parameter)
48         {
49                 // on a rehash we delete our classes for good measure and create them again.
50                 ConfigReader* Conf = new ConfigReader(ServerInstance);
51
52                 // re-read our config options on a rehash
53                 NoisyOverride = Conf->ReadFlag("override", "noisy", 0);
54                 RequireKey = Conf->ReadFlag("override", "requirekey", 0);
55
56                 overrides.clear();
57
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
69         virtual void OnPostCommand(const std::string &command, const std::vector<std::string> &parameters, User *user, CmdResult result, const std::string &original_line)
70         {
71                 if ((OverriddenMode) && (irc::string(command.c_str()) == "MODE") && (result == CMD_SUCCESS))
72                 {
73                         int Total = OverOps + OverDeops + OverVoices + OverDevoices + OverHalfops + OverDehalfops;
74                         if (Total == 0 || ServerInstance->Modes->GetLastParse().empty())
75                                 return;
76
77                         ServerInstance->SNO->WriteToSnoMask('G',std::string(user->nick)+" Overriding modes: "+ServerInstance->Modes->GetLastParse()+" "+(Total ? "[Detail: " : "")+
78                                         (OverOps ? ConvToStr(OverOps)+" op"+(OverOps != 1 ? "s" : "")+" " : "")+
79                                         (OverDeops ? ConvToStr(OverDeops)+" deop"+(OverDeops != 1 ? "s" : "")+" " : "")+
80                                         (OverVoices ? ConvToStr(OverVoices)+" voice"+(OverVoices != 1 ? "s" : "")+" " : "")+
81                                         (OverDevoices ? ConvToStr(OverDevoices)+" devoice"+(OverDevoices != 1 ? "s" : "")+" " : "")+
82                                         (OverHalfops ? ConvToStr(OverHalfops)+" halfop"+(OverHalfops != 1 ? "s" : "")+" " : "")+
83                                         (OverDehalfops ? ConvToStr(OverDehalfops)+" dehalfop"+(OverDehalfops != 1 ? "s" : "") : "")
84                                         +(Total ? "]" : ""));
85
86                         OverriddenMode = false;
87                         OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
88                 }
89         }
90
91         virtual void On005Numeric(std::string &output)
92         {
93                 output.append(" OVERRIDE");
94         }
95
96         virtual bool CanOverride(User* source, const char* token)
97         {
98                 // checks to see if the oper's type has <type:override>
99                 override_t::iterator j = overrides.find(source->oper);
100
101                 if (j != overrides.end())
102                 {
103                         // its defined or * is set, return its value as a boolean for if the token is set
104                         return ((j->second.find(token, 0) != std::string::npos) || (j->second.find("*", 0) != std::string::npos));
105                 }
106
107                 // its not defined at all, count as false
108                 return false;
109         }
110
111
112         virtual int OnLocalTopicChange(User *source, Channel *channel, const std::string &topic)
113         {
114                 if (IS_OPER(source) && CanOverride(source, "TOPIC"))
115                 {
116                         if (!channel->HasUser(source) || (channel->IsModeSet('t') && channel->GetStatus(source) < STATUS_HOP))
117                         {
118                                 ServerInstance->SNO->WriteToSnoMask('G',std::string(source->nick)+" used oper override to change a topic on "+std::string(channel->name));
119                         }
120
121                         // Explicit allow
122                         return -1;
123                 }
124
125                 return 0;
126         }
127
128         virtual int OnUserPreKick(User* source, User* user, Channel* chan, const std::string &reason)
129         {
130                 if (IS_OPER(source) && CanOverride(source,"KICK"))
131                 {
132                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
133                         if ((chan->GetStatus(source) < chan->GetStatus(user))                   || (chan->GetStatus(source) <= STATUS_VOICE))
134                         {                               
135                                 ServerInstance->SNO->WriteToSnoMask('G',std::string(source->nick)+" used oper override to kick "+std::string(user->nick)+" on "+std::string(chan->name)+" ("+reason+")");
136                         }
137                         /* Returning -1 explicitly allows the kick */
138                         return -1;
139                 }
140                 return 0;
141         }
142
143         virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
144         {
145                 if (IS_OPER(source))
146                 {
147                         if (source && channel)
148                         {
149                                 // Fix by brain - allow the change if they arent on channel - rely on boolean short-circuit
150                                 // to not check the other items in the statement if they arent on the channel
151                                 int mode = channel->GetStatus(source);
152                                 switch (access_type)
153                                 {
154                                         case AC_DEOP:
155                                                 if (CanOverride(source,"MODEDEOP"))
156                                                 {
157                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
158                                                                 OverDeops++;
159                                                         return ACR_ALLOW;
160                                                 }
161                                                 else
162                                                 {
163                                                         return ACR_DEFAULT;
164                                                 }
165                                         break;
166                                         case AC_OP:
167                                                 if (CanOverride(source,"MODEOP"))
168                                                 {
169                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
170                                                                 OverOps++;
171                                                         return ACR_ALLOW;
172                                                 }
173                                                 else
174                                                 {
175                                                         return ACR_DEFAULT;
176                                                 }
177                                         break;
178                                         case AC_VOICE:
179                                                 if (CanOverride(source,"MODEVOICE"))
180                                                 {
181                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
182                                                                 OverVoices++;
183                                                         return ACR_ALLOW;
184                                                 }
185                                                 else
186                                                 {
187                                                         return ACR_DEFAULT;
188                                                 }
189                                         break;
190                                         case AC_DEVOICE:
191                                                 if (CanOverride(source,"MODEDEVOICE"))
192                                                 {
193                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
194                                                                 OverDevoices++;
195                                                         return ACR_ALLOW;
196                                                 }
197                                                 else
198                                                 {
199                                                         return ACR_DEFAULT;
200                                                 }
201                                         break;
202                                         case AC_HALFOP:
203                                                 if (CanOverride(source,"MODEHALFOP"))
204                                                 {
205                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
206                                                                 OverHalfops++;
207                                                         return ACR_ALLOW;
208                                                 }
209                                                 else
210                                                 {
211                                                         return ACR_DEFAULT;
212                                                 }
213                                         break;
214                                         case AC_DEHALFOP:
215                                                 if (CanOverride(source,"MODEDEHALFOP"))
216                                                 {
217                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
218                                                                 OverDehalfops++;
219                                                         return ACR_ALLOW;
220                                                 }
221                                                 else
222                                                 {
223                                                         return ACR_DEFAULT;
224                                                 }
225                                         break;
226                                 }
227
228                                 if (CanOverride(source,"OTHERMODE"))
229                                 {
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('G', 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('G', 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('G', user->nick+" used oper override to bypass +l on "+std::string(cname));
299                                         return -1;
300                                 }
301
302                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
303                                 {
304                                         if (RequireKey && keygiven != "override")
305                                         {
306                                                 // Can't join normally -- must use a special key to bypass restrictions
307                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
308                                                 return 1;
309                                         }
310
311                                         if (NoisyOverride)
312                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname, user->nick.c_str());
313                                         ServerInstance->SNO->WriteToSnoMask('G',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname);
314                                         return -1;
315                                 }
316                         }
317                 }
318                 return 0;
319         }
320
321         virtual const char* OnRequest(Request* request)
322         {
323                 if(strcmp(OVRREQID, request->GetId()) == 0)
324                 {
325                         OVRrequest* req = static_cast<OVRrequest*>(request);
326                         return this->CanOverride(req->requser,req->reqtoken.c_str()) ? "yes":"";
327                 }
328                 return NULL;
329         }
330
331         virtual ~ModuleOverride()
332         {
333                 ServerInstance->Modules->UnpublishFeature("Override");
334                 ServerInstance->SNO->DisableSnomask('G');
335         }
336
337         virtual Version GetVersion()
338         {
339                 return Version("$Id$",VF_VENDOR,API_VERSION);
340         }
341 };
342
343 MODULE_INIT(ModuleOverride)