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