]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
41b57b32716232b4716d20e356bb1ff8046032a6
[user/henk/code/inspircd.git] / src / modules / m_override.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 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         }
40         
41         virtual void OnRehash(User* user, const std::string &parameter)
42         {
43                 // on a rehash we delete our classes for good measure and create them again.
44                 ConfigReader* Conf = new ConfigReader(ServerInstance);
45                 
46                 // re-read our config options on a rehash
47                 NoisyOverride = Conf->ReadFlag("override","noisy",0);
48                 overrides.clear();
49                 for (int j =0; j < Conf->Enumerate("type"); j++)
50                 {
51                         std::string typen = Conf->ReadValue("type","name",j);
52                         std::string tokenlist = Conf->ReadValue("type","override",j);
53                         overrides[typen] = tokenlist;
54                 }
55                 
56                 DELETE(Conf);
57         }
58
59         void Implements(char* List)
60         {
61                 List[I_OnRehash] = List[I_OnAccessCheck] = List[I_On005Numeric] = List[I_OnUserPreJoin] = List[I_OnUserPreKick] = List[I_OnPostCommand] = 1;
62         }
63
64         virtual void OnPostCommand(const std::string &command, const char** parameters, int pcnt, 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         virtual int OnUserPreKick(User* source, User* user, Channel* chan, const std::string &reason)
105         {
106                 if (IS_OPER(source) && CanOverride(source,"KICK"))
107                 {
108                         if (((chan->GetStatus(source) == STATUS_HOP) && (chan->GetStatus(user) == STATUS_OP)) || (chan->GetStatus(source) < STATUS_VOICE))
109                         {
110                                 ServerInstance->SNO->WriteToSnoMask('O',std::string(source->nick)+" Override-Kicked "+std::string(user->nick)+" on "+std::string(chan->name)+" ("+reason+")");
111                         }
112                         /* Returning -1 explicitly allows the kick */
113                         return -1;
114                 }
115                 return 0;
116         }
117         
118         virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
119         {
120                 if (IS_OPER(source))
121                 {
122                         if (source && channel)
123                         {
124                                 // Fix by brain - allow the change if they arent on channel - rely on boolean short-circuit
125                                 // to not check the other items in the statement if they arent on the channel
126                                 int mode = channel->GetStatus(source);
127                                 switch (access_type)
128                                 {
129                                         case AC_DEOP:
130                                                 if (CanOverride(source,"MODEDEOP"))
131                                                 {
132                                                         if (NoisyOverride)
133                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
134                                                                 OverDeops++;
135                                                         return ACR_ALLOW;
136                                                 }
137                                                 else
138                                                 {
139                                                         return ACR_DEFAULT;
140                                                 }
141                                         break;
142                                         case AC_OP:
143                                                 if (CanOverride(source,"MODEOP"))
144                                                 {
145                                                         if (NoisyOverride)
146                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
147                                                                 OverOps++;
148                                                         return ACR_ALLOW;
149                                                 }
150                                                 else
151                                                 {
152                                                         return ACR_DEFAULT;
153                                                 }
154                                         break;
155                                         case AC_VOICE:
156                                                 if (CanOverride(source,"MODEVOICE"))
157                                                 {
158                                                         if (NoisyOverride)
159                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
160                                                                 OverVoices++;
161                                                         return ACR_ALLOW;
162                                                 }
163                                                 else
164                                                 {
165                                                         return ACR_DEFAULT;
166                                                 }
167                                         break;
168                                         case AC_DEVOICE:
169                                                 if (CanOverride(source,"MODEDEVOICE"))
170                                                 {
171                                                         if (NoisyOverride)
172                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
173                                                                 OverDevoices++;
174                                                         return ACR_ALLOW;
175                                                 }
176                                                 else
177                                                 {
178                                                         return ACR_DEFAULT;
179                                                 }
180                                         break;
181                                         case AC_HALFOP:
182                                                 if (CanOverride(source,"MODEHALFOP"))
183                                                 {
184                                                         if (NoisyOverride)
185                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
186                                                                 OverHalfops++;
187                                                         return ACR_ALLOW;
188                                                 }
189                                                 else
190                                                 {
191                                                         return ACR_DEFAULT;
192                                                 }
193                                         break;
194                                         case AC_DEHALFOP:
195                                                 if (CanOverride(source,"MODEDEHALFOP"))
196                                                 {
197                                                         if (NoisyOverride)
198                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
199                                                                 OverDehalfops++;
200                                                         return ACR_ALLOW;
201                                                 }
202                                                 else
203                                                 {
204                                                         return ACR_DEFAULT;
205                                                 }
206                                         break;
207                                 }
208                         
209                                 if (CanOverride(source,"OTHERMODE"))
210                                 {
211                                         if (NoisyOverride)
212                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
213                                         {
214                                                 OverriddenMode = true;
215                                                 OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
216                                         }
217                                         return ACR_ALLOW;
218                                 }
219                                 else
220                                 {
221                                         return ACR_DEFAULT;
222                                 }
223                         }
224                 }
225
226                 return ACR_DEFAULT;
227         }
228         
229         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs)
230         {
231                 if (IS_OPER(user))
232                 {
233                         if (chan)
234                         {
235                                 if ((chan->modes[CM_INVITEONLY]) && (CanOverride(user,"INVITE")))
236                                 {
237                                         irc::string x = chan->name;
238                                         if (!user->IsInvited(x))
239                                         {
240                                                 /* XXX - Ugly cast for a parameter that isn't used? :< - Om */
241                                                 if (NoisyOverride)
242                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper-override to bypass invite-only", cname, user->nick);
243                                                 ServerInstance->SNO->WriteToSnoMask('O',std::string(user->nick)+" used operoverride to bypass +i on "+std::string(cname));
244                                         }
245                                         return -1;
246                                 }
247                                 
248                                 if ((*chan->key) && (CanOverride(user,"KEY")))
249                                 {
250                                         if (NoisyOverride)
251                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper-override to bypass the channel key", cname, user->nick);
252                                         ServerInstance->SNO->WriteToSnoMask('O',std::string(user->nick)+" used operoverride to bypass +k on "+std::string(cname));
253                                         return -1;
254                                 }
255                                         
256                                 if ((chan->limit > 0) && (chan->GetUserCounter() >=  chan->limit) && (CanOverride(user,"LIMIT")))
257                                 {
258                                         if (NoisyOverride)
259                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper-override to bypass the channel limit", cname, user->nick);
260                                         ServerInstance->SNO->WriteToSnoMask('O',std::string(user->nick)+" used operoverride to bypass +l on "+std::string(cname));
261                                         return -1;
262                                 }
263
264                                 if (CanOverride(user,"BANWALK"))
265                                 {
266                                         if (chan->IsBanned(user))
267                                         {
268                                                 if (NoisyOverride)
269                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper-override to bypass channel ban", cname, user->nick);
270                                                 ServerInstance->SNO->WriteToSnoMask('O',"%s used oper-override to bypass channel ban on %s", user->nick, cname);
271                                         }
272                                         return -1;
273                                 }
274                         }
275                 }
276                 return 0;
277         }
278         
279         virtual ~ModuleOverride()
280         {
281                 ServerInstance->SNO->DisableSnomask('O');
282         }
283         
284         virtual Version GetVersion()
285         {
286                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
287         }
288 };
289
290 MODULE_INIT(ModuleOverride)