]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
Move unused code to the bottom of the file
[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
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('O', "OVERRIDE");
37                 OverriddenMode = false;
38                 OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
39                 Implementation eventlist[] = { I_OnRehash, I_OnAccessCheck, I_On005Numeric, I_OnUserPreJoin, I_OnUserPreKick, I_OnPostCommand, I_OnLocalTopicChange };
40                 ServerInstance->Modules->Attach(eventlist, this, 7);
41         }
42
43         virtual void OnRehash(User* user, 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                 RequireKey = Conf->ReadFlag("override", "requirekey", 0);
51
52                 overrides.clear();
53
54                 for (int j =0; j < Conf->Enumerate("type"); j++)
55                 {
56                         std::string typen = Conf->ReadValue("type","name",j);
57                         std::string tokenlist = Conf->ReadValue("type","override",j);
58                         overrides[typen] = tokenlist;
59                 }
60
61                 delete Conf;
62         }
63
64
65         virtual void OnPostCommand(const std::string &command, const std::vector<std::string> &parameters, User *user, CmdResult result, const std::string &original_line)
66         {
67                 if ((NoisyOverride) && (OverriddenMode) && (irc::string(command.c_str()) == "MODE") && (result == CMD_SUCCESS))
68                 {
69                         int Total = OverOps + OverDeops + OverVoices + OverDevoices + OverHalfops + OverDehalfops;
70
71                         ServerInstance->SNO->WriteToSnoMask('O',std::string(user->nick)+" Overriding modes: "+ServerInstance->Modes->GetLastParse()+" "+(Total ? "[Detail: " : "")+
72                                         (OverOps ? ConvToStr(OverOps)+" op"+(OverOps != 1 ? "s" : "")+" " : "")+
73                                         (OverDeops ? ConvToStr(OverDeops)+" deop"+(OverDeops != 1 ? "s" : "")+" " : "")+
74                                         (OverVoices ? ConvToStr(OverVoices)+" voice"+(OverVoices != 1 ? "s" : "")+" " : "")+
75                                         (OverDevoices ? ConvToStr(OverDevoices)+" devoice"+(OverDevoices != 1 ? "s" : "")+" " : "")+
76                                         (OverHalfops ? ConvToStr(OverHalfops)+" halfop"+(OverHalfops != 1 ? "s" : "")+" " : "")+
77                                         (OverDehalfops ? ConvToStr(OverDehalfops)+" dehalfop"+(OverDehalfops != 1 ? "s" : "") : "")
78                                         +(Total ? "]" : ""));
79
80                         OverriddenMode = false;
81                         OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
82                 }
83         }
84
85         virtual void On005Numeric(std::string &output)
86         {
87                 output.append(" OVERRIDE");
88         }
89
90         virtual bool CanOverride(User* source, const char* token)
91         {
92                 // checks to see if the oper's type has <type:override>
93                 override_t::iterator j = overrides.find(source->oper);
94
95                 if (j != overrides.end())
96                 {
97                         // its defined or * is set, return its value as a boolean for if the token is set
98                         return ((j->second.find(token, 0) != std::string::npos) || (j->second.find("*", 0) != std::string::npos));
99                 }
100
101                 // its not defined at all, count as false
102                 return false;
103         }
104
105
106         virtual int OnLocalTopicChange(User *source, Channel *channel, const std::string &topic)
107         {
108                 if (IS_OPER(source) && CanOverride(source, "TOPIC"))
109                 {
110                         if (!channel->HasUser(source) || (channel->IsModeSet('t') && channel->GetStatus(source) < STATUS_HOP))
111                         {
112                                 ServerInstance->SNO->WriteToSnoMask('O',std::string(source->nick)+"  used oper override to change a topic on "+std::string(channel->name));
113                         }
114
115                         // Explicit allow
116                         return -1;
117                 }
118
119                 return 0;
120         }
121
122         virtual int OnUserPreKick(User* source, User* user, Channel* chan, const std::string &reason)
123         {
124                 if (IS_OPER(source) && CanOverride(source,"KICK"))
125                 {
126                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
127                         if ((chan->GetStatus(source) < chan->GetStatus(user))                   || (chan->GetStatus(source) <= STATUS_VOICE))
128                         {                               
129                                 ServerInstance->SNO->WriteToSnoMask('O',std::string(source->nick)+" used oper override to kick "+std::string(user->nick)+" on "+std::string(chan->name)+" ("+reason+")");
130                         }
131                         /* Returning -1 explicitly allows the kick */
132                         return -1;
133                 }
134                 return 0;
135         }
136
137         virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
138         {
139                 if (IS_OPER(source))
140                 {
141                         if (source && channel)
142                         {
143                                 // Fix by brain - allow the change if they arent on channel - rely on boolean short-circuit
144                                 // to not check the other items in the statement if they arent on the channel
145                                 int mode = channel->GetStatus(source);
146                                 switch (access_type)
147                                 {
148                                         case AC_DEOP:
149                                                 if (CanOverride(source,"MODEDEOP"))
150                                                 {
151                                                         if (NoisyOverride)
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 (NoisyOverride)
165                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
166                                                                 OverOps++;
167                                                         return ACR_ALLOW;
168                                                 }
169                                                 else
170                                                 {
171                                                         return ACR_DEFAULT;
172                                                 }
173                                         break;
174                                         case AC_VOICE:
175                                                 if (CanOverride(source,"MODEVOICE"))
176                                                 {
177                                                         if (NoisyOverride)
178                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
179                                                                 OverVoices++;
180                                                         return ACR_ALLOW;
181                                                 }
182                                                 else
183                                                 {
184                                                         return ACR_DEFAULT;
185                                                 }
186                                         break;
187                                         case AC_DEVOICE:
188                                                 if (CanOverride(source,"MODEDEVOICE"))
189                                                 {
190                                                         if (NoisyOverride)
191                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
192                                                                 OverDevoices++;
193                                                         return ACR_ALLOW;
194                                                 }
195                                                 else
196                                                 {
197                                                         return ACR_DEFAULT;
198                                                 }
199                                         break;
200                                         case AC_HALFOP:
201                                                 if (CanOverride(source,"MODEHALFOP"))
202                                                 {
203                                                         if (NoisyOverride)
204                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
205                                                                 OverHalfops++;
206                                                         return ACR_ALLOW;
207                                                 }
208                                                 else
209                                                 {
210                                                         return ACR_DEFAULT;
211                                                 }
212                                         break;
213                                         case AC_DEHALFOP:
214                                                 if (CanOverride(source,"MODEDEHALFOP"))
215                                                 {
216                                                         if (NoisyOverride)
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 (NoisyOverride)
231                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
232                                         {
233                                                 OverriddenMode = true;
234                                                 OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
235                                         }
236                                         return ACR_ALLOW;
237                                 }
238                                 else
239                                 {
240                                         return ACR_DEFAULT;
241                                 }
242                         }
243                 }
244
245                 return ACR_DEFAULT;
246         }
247
248         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
249         {
250                 if (IS_LOCAL(user) && IS_OPER(user))
251                 {
252                         if (chan)
253                         {
254                                 if ((chan->modes[CM_INVITEONLY]) && (CanOverride(user,"INVITE")))
255                                 {
256                                         irc::string x(chan->name.c_str());
257                                         if (!user->IsInvited(x))
258                                         {
259                                                 if (RequireKey && keygiven != "override")
260                                                 {
261                                                         // Can't join normally -- must use a special key to bypass restrictions
262                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
263                                                         return 1;
264                                                 }
265
266                                                 if (NoisyOverride)
267                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass invite-only", cname, user->nick.c_str());
268                                                 ServerInstance->SNO->WriteToSnoMask('O', user->nick+" used oper override to bypass +i on "+std::string(cname));
269                                         }
270                                         return -1;
271                                 }
272
273                                 if ((chan->modes[CM_KEY]) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter('k'))
274                                 {
275                                         if (RequireKey && keygiven != "override")
276                                         {
277                                                 // Can't join normally -- must use a special key to bypass restrictions
278                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
279                                                 return 1;
280                                         }
281
282                                         if (NoisyOverride)
283                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel key", cname, user->nick.c_str());
284                                         ServerInstance->SNO->WriteToSnoMask('O', user->nick+" used oper override to bypass +k on "+std::string(cname));
285                                         return -1;
286                                 }
287
288                                 if ((chan->modes[CM_LIMIT]) && (chan->GetUserCounter() >=  atoi(chan->GetModeParameter('l').c_str())) && (CanOverride(user,"LIMIT")))
289                                 {
290                                         if (RequireKey && keygiven != "override")
291                                         {
292                                                 // Can't join normally -- must use a special key to bypass restrictions
293                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
294                                                 return 1;
295                                         }
296
297                                         if (NoisyOverride)
298                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel limit", cname, user->nick.c_str());
299                                         ServerInstance->SNO->WriteToSnoMask('O', user->nick+" used oper override to bypass +l on "+std::string(cname));
300                                         return -1;
301                                 }
302
303                                 if (CanOverride(user,"BANWALK"))
304                                 {
305                                         if (chan->IsBanned(user))
306                                         {
307                                                 if (RequireKey && keygiven != "override")
308                                                 {
309                                                         // Can't join normally -- must use a special key to bypass restrictions
310                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
311                                                         return 1;
312                                                 }
313
314                                                 if (NoisyOverride)
315                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname, user->nick.c_str());
316                                                 ServerInstance->SNO->WriteToSnoMask('O',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname);
317                                         }
318                                         return -1;
319                                 }
320                         }
321                 }
322                 return 0;
323         }
324
325         virtual ~ModuleOverride()
326         {
327                 ServerInstance->SNO->DisableSnomask('O');
328         }
329
330         virtual Version GetVersion()
331         {
332                 return Version(1,2,0,1,VF_VENDOR,API_VERSION);
333         }
334 };
335
336 MODULE_INIT(ModuleOverride)