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