]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
Revert automated conversion by Special, as it (unfortunately) neglects some details...
[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 #include "wildcard.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
24         override_t overrides;
25         bool RequireKey;
26         bool NoisyOverride;
27         bool OverriddenMode;
28         int OverOps, OverDeops, OverVoices, OverDevoices, OverHalfops, OverDehalfops;
29
30  public:
31
32         ModuleOverride(InspIRCd* Me)
33                 : Module(Me)
34         {
35                 // read our config options (main config file)
36                 OnRehash(NULL,"");
37                 ServerInstance->SNO->EnableSnomask('O', "OVERRIDE");
38                 OverriddenMode = false;
39                 OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
40                 Implementation eventlist[] = { I_OnRehash, I_OnAccessCheck, I_On005Numeric, I_OnUserPreJoin, I_OnUserPreKick, I_OnPostCommand, I_OnLocalTopicChange };
41                 ServerInstance->Modules->Attach(eventlist, this, 7);
42         }
43
44         virtual void OnRehash(User* user, const std::string &parameter)
45         {
46                 // on a rehash we delete our classes for good measure and create them again.
47                 ConfigReader* Conf = new ConfigReader(ServerInstance);
48
49                 // re-read our config options on a rehash
50                 NoisyOverride = Conf->ReadFlag("override", "noisy", 0);
51                 RequireKey = Conf->ReadFlag("override", "requirekey", 0);
52
53                 overrides.clear();
54
55                 for (int j =0; j < Conf->Enumerate("type"); j++)
56                 {
57                         std::string typen = Conf->ReadValue("type","name",j);
58                         std::string tokenlist = Conf->ReadValue("type","override",j);
59                         overrides[typen] = tokenlist;
60                 }
61
62                 delete Conf;
63         }
64
65
66         virtual void OnPostCommand(const std::string &command, const std::vector<std::string> &parameters, User *user, CmdResult result, const std::string &original_line)
67         {
68                 if ((NoisyOverride) && (OverriddenMode) && (irc::string(command.c_str()) == "MODE") && (result == CMD_SUCCESS))
69                 {
70                         int Total = OverOps + OverDeops + OverVoices + OverDevoices + OverHalfops + OverDehalfops;
71
72                         ServerInstance->SNO->WriteToSnoMask('O',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('O',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('O',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 (NoisyOverride)
153                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
154                                                                 OverDeops++;
155                                                         return ACR_ALLOW;
156                                                 }
157                                                 else
158                                                 {
159                                                         return ACR_DEFAULT;
160                                                 }
161                                         break;
162                                         case AC_OP:
163                                                 if (CanOverride(source,"MODEOP"))
164                                                 {
165                                                         if (NoisyOverride)
166                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
167                                                                 OverOps++;
168                                                         return ACR_ALLOW;
169                                                 }
170                                                 else
171                                                 {
172                                                         return ACR_DEFAULT;
173                                                 }
174                                         break;
175                                         case AC_VOICE:
176                                                 if (CanOverride(source,"MODEVOICE"))
177                                                 {
178                                                         if (NoisyOverride)
179                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
180                                                                 OverVoices++;
181                                                         return ACR_ALLOW;
182                                                 }
183                                                 else
184                                                 {
185                                                         return ACR_DEFAULT;
186                                                 }
187                                         break;
188                                         case AC_DEVOICE:
189                                                 if (CanOverride(source,"MODEDEVOICE"))
190                                                 {
191                                                         if (NoisyOverride)
192                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
193                                                                 OverDevoices++;
194                                                         return ACR_ALLOW;
195                                                 }
196                                                 else
197                                                 {
198                                                         return ACR_DEFAULT;
199                                                 }
200                                         break;
201                                         case AC_HALFOP:
202                                                 if (CanOverride(source,"MODEHALFOP"))
203                                                 {
204                                                         if (NoisyOverride)
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 (NoisyOverride)
218                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
219                                                                 OverDehalfops++;
220                                                         return ACR_ALLOW;
221                                                 }
222                                                 else
223                                                 {
224                                                         return ACR_DEFAULT;
225                                                 }
226                                         break;
227                                 }
228
229                                 if (CanOverride(source,"OTHERMODE"))
230                                 {
231                                         if (NoisyOverride)
232                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
233                                         {
234                                                 OverriddenMode = true;
235                                                 OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
236                                         }
237                                         return ACR_ALLOW;
238                                 }
239                                 else
240                                 {
241                                         return ACR_DEFAULT;
242                                 }
243                         }
244                 }
245
246                 return ACR_DEFAULT;
247         }
248
249         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
250         {
251                 if (IS_LOCAL(user) && IS_OPER(user))
252                 {
253                         if (chan)
254                         {
255                                 if ((chan->modes[CM_INVITEONLY]) && (CanOverride(user,"INVITE")))
256                                 {
257                                         irc::string x(chan->name.c_str());
258                                         if (!user->IsInvited(x))
259                                         {
260                                                 if (RequireKey && keygiven != "override")
261                                                 {
262                                                         // Can't join normally -- must use a special key to bypass restrictions
263                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
264                                                         return 1;
265                                                 }
266
267                                                 if (NoisyOverride)
268                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass invite-only", cname, user->nick.c_str());
269                                                 ServerInstance->SNO->WriteToSnoMask('O', user->nick+" used oper override to bypass +i on "+std::string(cname));
270                                         }
271                                         return -1;
272                                 }
273
274                                 if ((chan->modes[CM_KEY]) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter('k'))
275                                 {
276                                         if (RequireKey && keygiven != "override")
277                                         {
278                                                 // Can't join normally -- must use a special key to bypass restrictions
279                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
280                                                 return 1;
281                                         }
282
283                                         if (NoisyOverride)
284                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel key", cname, user->nick.c_str());
285                                         ServerInstance->SNO->WriteToSnoMask('O', user->nick+" used oper override to bypass +k on "+std::string(cname));
286                                         return -1;
287                                 }
288
289                                 if ((chan->modes[CM_LIMIT]) && (chan->GetUserCounter() >=  atoi(chan->GetModeParameter('l').c_str())) && (CanOverride(user,"LIMIT")))
290                                 {
291                                         if (RequireKey && keygiven != "override")
292                                         {
293                                                 // Can't join normally -- must use a special key to bypass restrictions
294                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
295                                                 return 1;
296                                         }
297
298                                         if (NoisyOverride)
299                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel limit", cname, user->nick.c_str());
300                                         ServerInstance->SNO->WriteToSnoMask('O', user->nick+" used oper override to bypass +l on "+std::string(cname));
301                                         return -1;
302                                 }
303
304                                 if (CanOverride(user,"BANWALK"))
305                                 {
306                                         if (chan->IsBanned(user))
307                                         {
308                                                 if (RequireKey && keygiven != "override")
309                                                 {
310                                                         // Can't join normally -- must use a special key to bypass restrictions
311                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
312                                                         return 1;
313                                                 }
314
315                                                 if (NoisyOverride)
316                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname, user->nick.c_str());
317                                                 ServerInstance->SNO->WriteToSnoMask('O',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname);
318                                         }
319                                         return -1;
320                                 }
321                         }
322                 }
323                 return 0;
324         }
325
326         virtual ~ModuleOverride()
327         {
328                 ServerInstance->SNO->DisableSnomask('O');
329         }
330
331         virtual Version GetVersion()
332         {
333                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
334         }
335 };
336
337 MODULE_INIT(ModuleOverride)