]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
253b200bcfea7d7607c1bb0bb2ec58d9b7cb50bd
[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 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
62         virtual void OnPostCommand(const std::string &command, const char** parameters, int pcnt, User *user, CmdResult result, const std::string &original_line)
63         {
64                 if ((NoisyOverride) && (OverriddenMode) && (irc::string(command.c_str()) == "MODE") && (result == CMD_SUCCESS))
65                 {
66                         int Total = OverOps + OverDeops + OverVoices + OverDevoices + OverHalfops + OverDehalfops;
67
68                         ServerInstance->SNO->WriteToSnoMask('O',std::string(user->nick)+" Overriding modes: "+ServerInstance->Modes->GetLastParse()+" "+(Total ? "[Detail: " : "")+
69                                         (OverOps ? ConvToStr(OverOps)+" op"+(OverOps != 1 ? "s" : "")+" " : "")+
70                                         (OverDeops ? ConvToStr(OverDeops)+" deop"+(OverDeops != 1 ? "s" : "")+" " : "")+
71                                         (OverVoices ? ConvToStr(OverVoices)+" voice"+(OverVoices != 1 ? "s" : "")+" " : "")+
72                                         (OverDevoices ? ConvToStr(OverDevoices)+" devoice"+(OverDevoices != 1 ? "s" : "")+" " : "")+
73                                         (OverHalfops ? ConvToStr(OverHalfops)+" halfop"+(OverHalfops != 1 ? "s" : "")+" " : "")+
74                                         (OverDehalfops ? ConvToStr(OverDehalfops)+" dehalfop"+(OverDehalfops != 1 ? "s" : "") : "")
75                                         +(Total ? "]" : ""));
76
77                         OverriddenMode = false;
78                         OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
79                 }
80         }
81
82         virtual void On005Numeric(std::string &output)
83         {
84                 output.append(" OVERRIDE");
85         }
86
87         virtual bool CanOverride(User* source, const char* token)
88         {
89                 // checks to see if the oper's type has <type:override>
90                 override_t::iterator j = overrides.find(source->oper);
91
92                 if (j != overrides.end())
93                 {
94                         // its defined or * is set, return its value as a boolean for if the token is set
95                         return ((j->second.find(token, 0) != std::string::npos) || (j->second.find("*", 0) != std::string::npos));
96                 }
97
98                 // its not defined at all, count as false
99                 return false;
100         }
101
102         virtual int OnUserPreKick(User* source, User* user, Channel* chan, const std::string &reason)
103         {
104                 if (IS_OPER(source) && CanOverride(source,"KICK"))
105                 {
106                         if (((chan->GetStatus(source) == STATUS_HOP) && (chan->GetStatus(user) == STATUS_OP)) || (chan->GetStatus(source) < STATUS_VOICE))
107                         {
108                                 ServerInstance->SNO->WriteToSnoMask('O',std::string(source->nick)+" Override-Kicked "+std::string(user->nick)+" on "+std::string(chan->name)+" ("+reason+")");
109                         }
110                         /* Returning -1 explicitly allows the kick */
111                         return -1;
112                 }
113                 return 0;
114         }
115         
116         virtual int OnAccessCheck(User* source,User* dest,Channel* channel,int access_type)
117         {
118                 if (IS_OPER(source))
119                 {
120                         if (source && channel)
121                         {
122                                 // Fix by brain - allow the change if they arent on channel - rely on boolean short-circuit
123                                 // to not check the other items in the statement if they arent on the channel
124                                 int mode = channel->GetStatus(source);
125                                 switch (access_type)
126                                 {
127                                         case AC_DEOP:
128                                                 if (CanOverride(source,"MODEDEOP"))
129                                                 {
130                                                         if (NoisyOverride)
131                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
132                                                                 OverDeops++;
133                                                         return ACR_ALLOW;
134                                                 }
135                                                 else
136                                                 {
137                                                         return ACR_DEFAULT;
138                                                 }
139                                         break;
140                                         case AC_OP:
141                                                 if (CanOverride(source,"MODEOP"))
142                                                 {
143                                                         if (NoisyOverride)
144                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
145                                                                 OverOps++;
146                                                         return ACR_ALLOW;
147                                                 }
148                                                 else
149                                                 {
150                                                         return ACR_DEFAULT;
151                                                 }
152                                         break;
153                                         case AC_VOICE:
154                                                 if (CanOverride(source,"MODEVOICE"))
155                                                 {
156                                                         if (NoisyOverride)
157                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
158                                                                 OverVoices++;
159                                                         return ACR_ALLOW;
160                                                 }
161                                                 else
162                                                 {
163                                                         return ACR_DEFAULT;
164                                                 }
165                                         break;
166                                         case AC_DEVOICE:
167                                                 if (CanOverride(source,"MODEDEVOICE"))
168                                                 {
169                                                         if (NoisyOverride)
170                                                         if ((!channel->HasUser(source)) || (mode < STATUS_HOP))
171                                                                 OverDevoices++;
172                                                         return ACR_ALLOW;
173                                                 }
174                                                 else
175                                                 {
176                                                         return ACR_DEFAULT;
177                                                 }
178                                         break;
179                                         case AC_HALFOP:
180                                                 if (CanOverride(source,"MODEHALFOP"))
181                                                 {
182                                                         if (NoisyOverride)
183                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
184                                                                 OverHalfops++;
185                                                         return ACR_ALLOW;
186                                                 }
187                                                 else
188                                                 {
189                                                         return ACR_DEFAULT;
190                                                 }
191                                         break;
192                                         case AC_DEHALFOP:
193                                                 if (CanOverride(source,"MODEDEHALFOP"))
194                                                 {
195                                                         if (NoisyOverride)
196                                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
197                                                                 OverDehalfops++;
198                                                         return ACR_ALLOW;
199                                                 }
200                                                 else
201                                                 {
202                                                         return ACR_DEFAULT;
203                                                 }
204                                         break;
205                                 }
206                         
207                                 if (CanOverride(source,"OTHERMODE"))
208                                 {
209                                         if (NoisyOverride)
210                                         if ((!channel->HasUser(source)) || (mode < STATUS_OP))
211                                         {
212                                                 OverriddenMode = true;
213                                                 OverOps = OverDeops = OverVoices = OverDevoices = OverHalfops = OverDehalfops = 0;
214                                         }
215                                         return ACR_ALLOW;
216                                 }
217                                 else
218                                 {
219                                         return ACR_DEFAULT;
220                                 }
221                         }
222                 }
223
224                 return ACR_DEFAULT;
225         }
226         
227         virtual int OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs)
228         {
229                 if (IS_OPER(user))
230                 {
231                         if (chan)
232                         {
233                                 if ((chan->modes[CM_INVITEONLY]) && (CanOverride(user,"INVITE")))
234                                 {
235                                         irc::string x = chan->name;
236                                         if (!user->IsInvited(x))
237                                         {
238                                                 /* XXX - Ugly cast for a parameter that isn't used? :< - Om */
239                                                 if (NoisyOverride)
240                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper-override to bypass invite-only", cname, user->nick);
241                                                 ServerInstance->SNO->WriteToSnoMask('O',std::string(user->nick)+" used operoverride to bypass +i on "+std::string(cname));
242                                         }
243                                         return -1;
244                                 }
245                                 
246                                 if ((*chan->key) && (CanOverride(user,"KEY")))
247                                 {
248                                         if (NoisyOverride)
249                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper-override to bypass the channel key", cname, user->nick);
250                                         ServerInstance->SNO->WriteToSnoMask('O',std::string(user->nick)+" used operoverride to bypass +k on "+std::string(cname));
251                                         return -1;
252                                 }
253                                         
254                                 if ((chan->limit > 0) && (chan->GetUserCounter() >=  chan->limit) && (CanOverride(user,"LIMIT")))
255                                 {
256                                         if (NoisyOverride)
257                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper-override to bypass the channel limit", cname, user->nick);
258                                         ServerInstance->SNO->WriteToSnoMask('O',std::string(user->nick)+" used operoverride to bypass +l on "+std::string(cname));
259                                         return -1;
260                                 }
261
262                                 if (CanOverride(user,"BANWALK"))
263                                 {
264                                         if (chan->IsBanned(user))
265                                         {
266                                                 if (NoisyOverride)
267                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper-override to bypass channel ban", cname, user->nick);
268                                                 ServerInstance->SNO->WriteToSnoMask('O',"%s used oper-override to bypass channel ban on %s", user->nick, cname);
269                                         }
270                                         return -1;
271                                 }
272                         }
273                 }
274                 return 0;
275         }
276         
277         virtual ~ModuleOverride()
278         {
279                 ServerInstance->SNO->DisableSnomask('O');
280         }
281         
282         virtual Version GetVersion()
283         {
284                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
285         }
286 };
287
288 MODULE_INIT(ModuleOverride)