]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
Change allocation of UserManager::clientlist to be physically part of the object...
[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 class ModuleOverride : public Module
30 {
31         bool RequireKey;
32         bool NoisyOverride;
33         ChanModeReference topiclock;
34         ChanModeReference inviteonly;
35         ChanModeReference key;
36         ChanModeReference limit;
37
38         static bool IsOverride(unsigned int userlevel, const std::string& modeline)
39         {
40                 for (std::string::const_iterator i = modeline.begin(); i != modeline.end(); ++i)
41                 {
42                         ModeHandler* mh = ServerInstance->Modes->FindMode(*i, MODETYPE_CHANNEL);
43                         if (!mh)
44                                 continue;
45
46                         if (mh->GetLevelRequired() > userlevel)
47                                 return true;
48                 }
49                 return false;
50         }
51
52  public:
53         ModuleOverride()
54                 : topiclock(this, "topiclock")
55                 , inviteonly(this, "inviteonly")
56                 , key(this, "key")
57                 , limit(this, "limit")
58         {
59         }
60
61         void init() CXX11_OVERRIDE
62         {
63                 ServerInstance->SNO->EnableSnomask('v', "OVERRIDE");
64         }
65
66         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
67         {
68                 // re-read our config options
69                 ConfigTag* tag = ServerInstance->Config->ConfValue("override");
70                 NoisyOverride = tag->getBool("noisy");
71                 RequireKey = tag->getBool("requirekey");
72         }
73
74         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
75         {
76                 tokens["OVERRIDE"];
77         }
78
79         bool CanOverride(User* source, const char* token)
80         {
81                 std::string tokenlist = source->oper->getConfig("override");
82
83                 // its defined or * is set, return its value as a boolean for if the token is set
84                 return ((tokenlist.find(token, 0) != std::string::npos) || (tokenlist.find("*", 0) != std::string::npos));
85         }
86
87
88         ModResult OnPreTopicChange(User *source, Channel *channel, const std::string &topic) CXX11_OVERRIDE
89         {
90                 if (IS_LOCAL(source) && source->IsOper() && CanOverride(source, "TOPIC"))
91                 {
92                         if (!channel->HasUser(source) || (channel->IsModeSet(topiclock) && channel->GetPrefixValue(source) < HALFOP_VALUE))
93                         {
94                                 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to change a topic on "+channel->name);
95                         }
96
97                         // Explicit allow
98                         return MOD_RES_ALLOW;
99                 }
100
101                 return MOD_RES_PASSTHRU;
102         }
103
104         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason) CXX11_OVERRIDE
105         {
106                 if (source->IsOper() && CanOverride(source,"KICK"))
107                 {
108                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
109                         if ((memb->chan->GetPrefixValue(source) < memb->getRank()) || (memb->chan->GetPrefixValue(source) <= VOICE_VALUE))
110                         {
111                                 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to kick "+memb->user->nick+" on "+memb->chan->name+" ("+reason+")");
112                                 return MOD_RES_ALLOW;
113                         }
114                 }
115                 return MOD_RES_PASSTHRU;
116         }
117
118         ModResult OnPreMode(User* source,User* dest,Channel* channel, const std::vector<std::string>& parameters) CXX11_OVERRIDE
119         {
120                 if (!channel)
121                         return MOD_RES_PASSTHRU;
122                 if (!source->IsOper() || !IS_LOCAL(source))
123                         return MOD_RES_PASSTHRU;
124
125                 unsigned int mode = channel->GetPrefixValue(source);
126
127                 if (!IsOverride(mode, parameters[1]))
128                         return MOD_RES_PASSTHRU;
129
130                 if (CanOverride(source, "MODE"))
131                 {
132                         std::string msg = source->nick+" overriding modes:";
133                         for(unsigned int i=0; i < parameters.size(); i++)
134                                 msg += " " + parameters[i];
135                         ServerInstance->SNO->WriteGlobalSno('v',msg);
136                         return MOD_RES_ALLOW;
137                 }
138                 return MOD_RES_PASSTHRU;
139         }
140
141         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
142         {
143                 if (user->IsOper())
144                 {
145                         if (chan)
146                         {
147                                 if (chan->IsModeSet(inviteonly) && (CanOverride(user,"INVITE")))
148                                 {
149                                         if (!IS_LOCAL(user)->IsInvited(chan))
150                                         {
151                                                 if (RequireKey && keygiven != "override")
152                                                 {
153                                                         // Can't join normally -- must use a special key to bypass restrictions
154                                                         user->WriteNotice("*** You may not join normally. You must join with a key of 'override' to oper override.");
155                                                         return MOD_RES_PASSTHRU;
156                                                 }
157
158                                                 if (NoisyOverride)
159                                                         chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass invite-only", cname.c_str(), user->nick.c_str());
160                                                 ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +i on " + cname);
161                                         }
162                                         return MOD_RES_ALLOW;
163                                 }
164
165                                 if (chan->IsModeSet(key) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter(key))
166                                 {
167                                         if (RequireKey && keygiven != "override")
168                                         {
169                                                 // Can't join normally -- must use a special key to bypass restrictions
170                                                 user->WriteNotice("*** You may not join normally. You must join with a key of 'override' to oper override.");
171                                                 return MOD_RES_PASSTHRU;
172                                         }
173
174                                         if (NoisyOverride)
175                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel key", cname.c_str(), user->nick.c_str());
176                                         ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +k on " + cname);
177                                         return MOD_RES_ALLOW;
178                                 }
179
180                                 if (chan->IsModeSet(limit) && (chan->GetUserCounter() >= ConvToInt(chan->GetModeParameter(limit))) && (CanOverride(user,"LIMIT")))
181                                 {
182                                         if (RequireKey && keygiven != "override")
183                                         {
184                                                 // Can't join normally -- must use a special key to bypass restrictions
185                                                 user->WriteNotice("*** You may not join normally. You must join with a key of 'override' to oper override.");
186                                                 return MOD_RES_PASSTHRU;
187                                         }
188
189                                         if (NoisyOverride)
190                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass the channel limit", cname.c_str(), user->nick.c_str());
191                                         ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass +l on " + cname);
192                                         return MOD_RES_ALLOW;
193                                 }
194
195                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
196                                 {
197                                         if (RequireKey && keygiven != "override")
198                                         {
199                                                 // Can't join normally -- must use a special key to bypass restrictions
200                                                 user->WriteNotice("*** You may not join normally. You must join with a key of 'override' to oper override.");
201                                                 return MOD_RES_PASSTHRU;
202                                         }
203
204                                         if (NoisyOverride)
205                                                 chan->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s used oper override to bypass channel ban", cname.c_str(), user->nick.c_str());
206                                         ServerInstance->SNO->WriteGlobalSno('v',"%s used oper override to bypass channel ban on %s", user->nick.c_str(), cname.c_str());
207                                         return MOD_RES_ALLOW;
208                                 }
209                         }
210                 }
211                 return MOD_RES_PASSTHRU;
212         }
213
214         Version GetVersion() CXX11_OVERRIDE
215         {
216                 return Version("Provides support for allowing opers to override certain things",VF_VENDOR);
217         }
218 };
219
220 MODULE_INIT(ModuleOverride)