]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
2c2d6a3a201c32028892766dabd999f097f7bbf9
[user/henk/code/inspircd.git] / src / modules / m_override.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2009 Uli Schlachter <psychon@znc.in>
6  *   Copyright (C) 2007-2009 Robin Burchell <robin+git@viroteck.net>
7  *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
8  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
9  *   Copyright (C) 2008 Geoff Bricker <geoff.bricker@gmail.com>
10  *   Copyright (C) 2004-2006 Craig Edwards <craigedwards@brainbox.cc>
11  *   Copyright (C) 2006 Oliver Lupton <oliverlupton@gmail.com>
12  *
13  * This file is part of InspIRCd.  InspIRCd is free software: you can
14  * redistribute it and/or modify it under the terms of the GNU General Public
15  * License as published by the Free Software Foundation, version 2.
16  *
17  * This program is distributed in the hope that it will be useful, but WITHOUT
18  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
20  * details.
21  *
22  * You should have received a copy of the GNU General Public License
23  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
24  */
25
26
27 #include "inspircd.h"
28
29 /* $ModDesc: Provides support for allowing opers to override certain things. */
30
31 class ModuleOverride : public Module
32 {
33         bool RequireKey;
34         bool NoisyOverride;
35
36         static bool IsOverride(unsigned int userlevel, const std::string& modeline)
37         {
38                 for (std::string::const_iterator i = modeline.begin(); i != modeline.end(); ++i)
39                 {
40                         ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
41                         if (!mh)
42                                 continue;
43
44                         if (mh->GetLevelRequired() > userlevel)
45                                 return true;
46                 }
47                 return false;
48         }
49
50  public:
51
52         void init()
53         {
54                 // read our config options (main config file)
55                 OnRehash(NULL);
56                 ServerInstance->SNO->EnableSnomask('v', "OVERRIDE");
57                 Implementation eventlist[] = { I_OnRehash, I_OnPreMode, I_On005Numeric, I_OnUserPreJoin, I_OnUserPreKick, I_OnPreTopicChange };
58                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
59         }
60
61         void OnRehash(User* user)
62         {
63                 // re-read our config options on a rehash
64                 ConfigTag* tag = ServerInstance->Config->ConfValue("override");
65                 NoisyOverride = tag->getBool("noisy");
66                 RequireKey = tag->getBool("requirekey");
67         }
68
69         void On005Numeric(std::string &output)
70         {
71                 output.append(" OVERRIDE");
72         }
73
74         bool CanOverride(User* source, const char* token)
75         {
76                 std::string tokenlist = source->oper->getConfig("override");
77
78                 // its defined or * is set, return its value as a boolean for if the token is set
79                 return ((tokenlist.find(token, 0) != std::string::npos) || (tokenlist.find("*", 0) != std::string::npos));
80         }
81
82
83         ModResult OnPreTopicChange(User *source, Channel *channel, const std::string &topic)
84         {
85                 if (IS_LOCAL(source) && IS_OPER(source) && CanOverride(source, "TOPIC"))
86                 {
87                         if (!channel->HasUser(source) || (channel->IsModeSet('t') && channel->GetPrefixValue(source) < HALFOP_VALUE))
88                         {
89                                 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to change a topic on "+channel->name);
90                         }
91
92                         // Explicit allow
93                         return MOD_RES_ALLOW;
94                 }
95
96                 return MOD_RES_PASSTHRU;
97         }
98
99         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason)
100         {
101                 if (IS_OPER(source) && CanOverride(source,"KICK"))
102                 {
103                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
104                         if ((memb->chan->GetPrefixValue(source) < memb->getRank()) || (memb->chan->GetPrefixValue(source) <= VOICE_VALUE) ||
105                             (memb->chan->GetPrefixValue(source) == HALFOP_VALUE) && (memb->getRank() == HALFOP_VALUE))
106                         {
107                                 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to kick "+memb->user->nick+" on "+memb->chan->name+" ("+reason+")");
108                                 return MOD_RES_ALLOW;
109                         }
110                 }
111                 return MOD_RES_PASSTHRU;
112         }
113
114         ModResult OnPreMode(User* source,User* dest,Channel* channel, const std::vector<std::string>& parameters)
115         {
116                 if (!source || !channel)
117                         return MOD_RES_PASSTHRU;
118                 if (!IS_OPER(source) || !IS_LOCAL(source))
119                         return MOD_RES_PASSTHRU;
120
121                 unsigned int mode = channel->GetPrefixValue(source);
122
123                 if (!IsOverride(mode, parameters[1]))
124                         return MOD_RES_PASSTHRU;
125
126                 if (CanOverride(source, "MODE"))
127                 {
128                         std::string msg = source->nick+" overriding modes:";
129                         for(unsigned int i=0; i < parameters.size(); i++)
130                                 msg += " " + parameters[i];
131                         ServerInstance->SNO->WriteGlobalSno('v',msg);
132                         return MOD_RES_ALLOW;
133                 }
134                 return MOD_RES_PASSTHRU;
135         }
136
137         ModResult OnUserPreJoin(User* user, Channel* chan, const char* cname, std::string &privs, const std::string &keygiven)
138         {
139                 if (IS_LOCAL(user) && IS_OPER(user))
140                 {
141                         if (chan)
142                         {
143                                 if (chan->IsModeSet('i') && (CanOverride(user,"INVITE")))
144                                 {
145                                         irc::string x(chan->name.c_str());
146                                         if (!IS_LOCAL(user)->IsInvited(x))
147                                         {
148                                                 if (RequireKey && keygiven != "override")
149                                                 {
150                                                         // Can't join normally -- must use a special key to bypass restrictions
151                                                         user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
152                                                         return MOD_RES_PASSTHRU;
153                                                 }
154
155                                                 if (NoisyOverride)
156                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass invite-only", cname, user->nick.c_str());
157                                                 ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +i on "+std::string(cname));
158                                         }
159                                         return MOD_RES_ALLOW;
160                                 }
161
162                                 if (chan->IsModeSet('k') && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter('k'))
163                                 {
164                                         if (RequireKey && keygiven != "override")
165                                         {
166                                                 // Can't join normally -- must use a special key to bypass restrictions
167                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
168                                                 return MOD_RES_PASSTHRU;
169                                         }
170
171                                         if (NoisyOverride)
172                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel key", cname, user->nick.c_str());
173                                         ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +k on "+std::string(cname));
174                                         return MOD_RES_ALLOW;
175                                 }
176
177                                 if (chan->IsModeSet('l') && (chan->GetUserCounter() >= ConvToInt(chan->GetModeParameter('l'))) && (CanOverride(user,"LIMIT")))
178                                 {
179                                         if (RequireKey && keygiven != "override")
180                                         {
181                                                 // Can't join normally -- must use a special key to bypass restrictions
182                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
183                                                 return MOD_RES_PASSTHRU;
184                                         }
185
186                                         if (NoisyOverride)
187                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel limit", cname, user->nick.c_str());
188                                         ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +l on "+std::string(cname));
189                                         return MOD_RES_ALLOW;
190                                 }
191
192                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
193                                 {
194                                         if (RequireKey && keygiven != "override")
195                                         {
196                                                 // Can't join normally -- must use a special key to bypass restrictions
197                                                 user->WriteServ("NOTICE %s :*** You may not join normally. You must join with a key of 'override' to oper override.", user->nick.c_str());
198                                                 return MOD_RES_PASSTHRU;
199                                         }
200
201                                         if (NoisyOverride)
202                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname, user->nick.c_str());
203                                         ServerInstance->SNO->WriteGlobalSno('v',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname);
204                                         return MOD_RES_ALLOW;
205                                 }
206                         }
207                 }
208                 return MOD_RES_PASSTHRU;
209         }
210
211         Version GetVersion()
212         {
213                 return Version("Provides support for allowing opers to override certain things",VF_VENDOR);
214         }
215 };
216
217 MODULE_INIT(ModuleOverride)