2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
5 * Copyright (C) 2017 Adam <Adam@anope.org>
6 * Copyright (C) 2016 Sheogorath <sheogorath@shivering-isles.com>
7 * Copyright (C) 2013, 2017, 2020 Sadie Powell <sadie@witchery.services>
8 * Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
9 * Copyright (C) 2012-2015 Attila Molnar <attilamolnar@hush.com>
10 * Copyright (C) 2012 Shawn Smith <ShawnSmith0828@gmail.com>
11 * Copyright (C) 2012 Robby <robby@chatbelgie.be>
12 * Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
13 * Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
14 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
15 * Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
16 * Copyright (C) 2006, 2008, 2010 Craig Edwards <brain@inspircd.org>
18 * This file is part of InspIRCd. InspIRCd is free software: you can
19 * redistribute it and/or modify it under the terms of the GNU General Public
20 * License as published by the Free Software Foundation, version 2.
22 * This program is distributed in the hope that it will be useful, but WITHOUT
23 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
24 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
27 * You should have received a copy of the GNU General Public License
28 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #include "modules/invite.h"
35 class Override : public SimpleUserModeHandler
38 Override(Module* Creator) : SimpleUserModeHandler(Creator, "override", 'O')
41 if (!ServerInstance->Config->ConfValue("override")->getBool("enableumode"))
42 DisableAutoRegister();
46 class ModuleOverride : public Module
52 ChanModeReference topiclock;
53 ChanModeReference inviteonly;
54 ChanModeReference key;
55 ChanModeReference limit;
58 static bool IsOverride(unsigned int userlevel, const Modes::ChangeList::List& list)
60 for (Modes::ChangeList::List::const_iterator i = list.begin(); i != list.end(); ++i)
62 ModeHandler* mh = i->mh;
63 if (mh->GetLevelRequired(i->adding) > userlevel)
69 ModResult HandleJoinOverride(LocalUser* user, Channel* chan, const std::string& keygiven, const char* bypasswhat, const char* mode)
71 if (RequireKey && keygiven != "override")
73 // Can't join normally -- must use a special key to bypass restrictions
74 user->WriteNotice("*** You may not join normally. You must join with a key of 'override' to oper override.");
75 return MOD_RES_PASSTHRU;
79 chan->WriteRemoteNotice(InspIRCd::Format("%s used oper override to bypass %s", user->nick.c_str(), bypasswhat));
80 ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass " + mode + " on " + chan->name);
88 , topiclock(this, "topiclock")
89 , inviteonly(this, "inviteonly")
91 , limit(this, "limit")
96 void init() CXX11_OVERRIDE
98 ServerInstance->SNO->EnableSnomask('v', "OVERRIDE");
99 UmodeEnabled = ServerInstance->Config->ConfValue("override")->getBool("enableumode");
102 void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
104 // re-read our config options
105 ConfigTag* tag = ServerInstance->Config->ConfValue("override");
106 NoisyOverride = tag->getBool("noisy");
107 RequireKey = tag->getBool("requirekey");
110 void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
115 bool CanOverride(User* source, const char* token)
117 // If we require oper override umode (+O) but it is not set
118 if (UmodeEnabled && !source->IsModeSet(ou))
121 std::string tokenlist = source->oper->getConfig("override");
122 // its defined or * is set, return its value as a boolean for if the token is set
123 return ((tokenlist.find(token, 0) != std::string::npos) || (tokenlist.find("*", 0) != std::string::npos));
127 ModResult OnPreTopicChange(User *source, Channel *channel, const std::string &topic) CXX11_OVERRIDE
129 if (IS_LOCAL(source) && source->IsOper() && CanOverride(source, "TOPIC"))
131 if (!channel->HasUser(source) || (channel->IsModeSet(topiclock) && channel->GetPrefixValue(source) < HALFOP_VALUE))
133 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to change a topic on "+channel->name);
137 return MOD_RES_ALLOW;
140 return MOD_RES_PASSTHRU;
143 ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason) CXX11_OVERRIDE
145 if (source->IsOper() && CanOverride(source,"KICK"))
147 // If the kicker's status is less than the target's, or the kicker's status is less than or equal to voice
148 if ((memb->chan->GetPrefixValue(source) < memb->getRank()) || (memb->chan->GetPrefixValue(source) <= VOICE_VALUE) ||
149 (memb->chan->GetPrefixValue(source) == HALFOP_VALUE && memb->getRank() == HALFOP_VALUE))
151 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to kick "+memb->user->nick+" on "+memb->chan->name+" ("+reason+")");
152 return MOD_RES_ALLOW;
155 return MOD_RES_PASSTHRU;
158 ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes) CXX11_OVERRIDE
161 return MOD_RES_PASSTHRU;
162 if (!source->IsOper() || !IS_LOCAL(source))
163 return MOD_RES_PASSTHRU;
165 const Modes::ChangeList::List& list = modes.getlist();
166 unsigned int mode = channel->GetPrefixValue(source);
168 if (!IsOverride(mode, list))
169 return MOD_RES_PASSTHRU;
171 if (CanOverride(source, "MODE"))
173 std::string msg = source->nick + " used oper override to set modes on " + channel->name + ": ";
175 // Construct a MODE string in the old format for sending it as a snotice
178 for (Modes::ChangeList::List::const_iterator i = list.begin(); i != list.end(); ++i)
180 const Modes::Change& item = *i;
181 if (!item.param.empty())
182 params.append(1, ' ').append(item.param);
184 char wanted_pm = (item.adding ? '+' : '-');
191 msg += item.mh->GetModeChar();
194 ServerInstance->SNO->WriteGlobalSno('v',msg);
195 return MOD_RES_ALLOW;
197 return MOD_RES_PASSTHRU;
200 ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
206 if (chan->IsModeSet(inviteonly) && (CanOverride(user,"INVITE")))
208 if (!invapi->IsInvited(user, chan))
209 return HandleJoinOverride(user, chan, keygiven, "invite-only", "+i");
210 return MOD_RES_ALLOW;
213 if (chan->IsModeSet(key) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter(key))
214 return HandleJoinOverride(user, chan, keygiven, "the channel key", "+k");
216 if (chan->IsModeSet(limit) && (chan->GetUserCounter() >= ConvToNum<size_t>(chan->GetModeParameter(limit))) && (CanOverride(user,"LIMIT")))
217 return HandleJoinOverride(user, chan, keygiven, "the channel limit", "+l");
219 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
220 return HandleJoinOverride(user, chan, keygiven, "channel ban", "channel ban");
223 return MOD_RES_PASSTHRU;
226 Version GetVersion() CXX11_OVERRIDE
228 return Version("Allows server operators to be given privileges that allow them to ignore various channel-level restrictions.",VF_VENDOR);
232 MODULE_INIT(ModuleOverride)