]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_override.cpp
eeb693a2c98b6e47cbe0c9010d45b7cef91fff06
[user/henk/code/inspircd.git] / src / modules / m_override.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2020 satmd <satmd@users.noreply.github.com>
5  *   Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
6  *   Copyright (C) 2017 Adam <Adam@anope.org>
7  *   Copyright (C) 2016 Sheogorath <sheogorath@shivering-isles.com>
8  *   Copyright (C) 2013, 2017, 2020 Sadie Powell <sadie@witchery.services>
9  *   Copyright (C) 2013 Daniel Vassdal <shutter@canternet.org>
10  *   Copyright (C) 2012-2015 Attila Molnar <attilamolnar@hush.com>
11  *   Copyright (C) 2012 Shawn Smith <ShawnSmith0828@gmail.com>
12  *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
13  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
14  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
15  *   Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
16  *   Copyright (C) 2007-2008 Dennis Friis <peavey@inspircd.org>
17  *   Copyright (C) 2006, 2008, 2010 Craig Edwards <brain@inspircd.org>
18  *
19  * This file is part of InspIRCd.  InspIRCd is free software: you can
20  * redistribute it and/or modify it under the terms of the GNU General Public
21  * License as published by the Free Software Foundation, version 2.
22  *
23  * This program is distributed in the hope that it will be useful, but WITHOUT
24  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
25  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
26  * details.
27  *
28  * You should have received a copy of the GNU General Public License
29  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
30  */
31
32
33 #include "inspircd.h"
34 #include "modules/invite.h"
35
36 class Override : public SimpleUserModeHandler
37 {
38  public:
39         Override(Module* Creator) : SimpleUserModeHandler(Creator, "override", 'O')
40         {
41                 oper = true;
42                 if (!ServerInstance->Config->ConfValue("override")->getBool("enableumode"))
43                         DisableAutoRegister();
44         }
45 };
46
47 class ModuleOverride : public Module
48 {
49         bool RequireKey;
50         bool NoisyOverride;
51         bool UmodeEnabled;
52         Override ou;
53         ChanModeReference topiclock;
54         ChanModeReference inviteonly;
55         ChanModeReference key;
56         ChanModeReference limit;
57         Invite::API invapi;
58
59         static bool IsOverride(unsigned int userlevel, const Modes::ChangeList::List& list)
60         {
61                 for (Modes::ChangeList::List::const_iterator i = list.begin(); i != list.end(); ++i)
62                 {
63                         ModeHandler* mh = i->mh;
64                         if (mh->GetLevelRequired(i->adding) > userlevel)
65                                 return true;
66                 }
67                 return false;
68         }
69
70         ModResult HandleJoinOverride(LocalUser* user, Channel* chan, const std::string& keygiven, const char* bypasswhat, const char* mode)
71         {
72                 if (RequireKey && keygiven != "override")
73                 {
74                         // Can't join normally -- must use a special key to bypass restrictions
75                         user->WriteNotice("*** You may not join normally. You must join with a key of 'override' to oper override.");
76                         return MOD_RES_PASSTHRU;
77                 }
78
79                 if (NoisyOverride)
80                         chan->WriteRemoteNotice(InspIRCd::Format("%s used oper override to bypass %s", user->nick.c_str(), bypasswhat));
81                 ServerInstance->SNO->WriteGlobalSno('v', user->nick+" used oper override to bypass " + mode + " on " + chan->name);
82                 return MOD_RES_ALLOW;
83         }
84
85  public:
86         ModuleOverride()
87                 : UmodeEnabled(false)
88                 , ou(this)
89                 , topiclock(this, "topiclock")
90                 , inviteonly(this, "inviteonly")
91                 , key(this, "key")
92                 , limit(this, "limit")
93                 , invapi(this)
94         {
95         }
96
97         void init() CXX11_OVERRIDE
98         {
99                 ServerInstance->SNO->EnableSnomask('v', "OVERRIDE");
100                 UmodeEnabled = ServerInstance->Config->ConfValue("override")->getBool("enableumode");
101         }
102
103         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
104         {
105                 // re-read our config options
106                 ConfigTag* tag = ServerInstance->Config->ConfValue("override");
107                 NoisyOverride = tag->getBool("noisy");
108                 RequireKey = tag->getBool("requirekey");
109         }
110
111         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
112         {
113                 tokens["OVERRIDE"];
114         }
115
116         bool CanOverride(User* source, const char* token)
117         {
118                 // If we require oper override umode (+O) but it is not set
119                 if (UmodeEnabled && !source->IsModeSet(ou))
120                         return false;
121
122                 std::string tokenlist = source->oper->getConfig("override");
123                 // its defined or * is set, return its value as a boolean for if the token is set
124                 return ((tokenlist.find(token, 0) != std::string::npos) || (tokenlist.find("*", 0) != std::string::npos));
125         }
126
127
128         ModResult OnPreTopicChange(User *source, Channel *channel, const std::string &topic) CXX11_OVERRIDE
129         {
130                 if (IS_LOCAL(source) && source->IsOper() && CanOverride(source, "TOPIC"))
131                 {
132                         if (!channel->HasUser(source) || (channel->IsModeSet(topiclock) && channel->GetPrefixValue(source) < HALFOP_VALUE))
133                         {
134                                 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to change a topic on "+channel->name);
135                         }
136
137                         // Explicit allow
138                         return MOD_RES_ALLOW;
139                 }
140
141                 return MOD_RES_PASSTHRU;
142         }
143
144         ModResult OnUserPreKick(User* source, Membership* memb, const std::string &reason) CXX11_OVERRIDE
145         {
146                 if (source->IsOper() && CanOverride(source,"KICK"))
147                 {
148                         // If the kicker's status is less than the target's,                    or      the kicker's status is less than or equal to voice
149                         if ((memb->chan->GetPrefixValue(source) < memb->getRank()) || (memb->chan->GetPrefixValue(source) <= VOICE_VALUE) ||
150                                 (memb->chan->GetPrefixValue(source) == HALFOP_VALUE && memb->getRank() == HALFOP_VALUE))
151                         {
152                                 ServerInstance->SNO->WriteGlobalSno('v',source->nick+" used oper override to kick "+memb->user->nick+" on "+memb->chan->name+" ("+reason+")");
153                                 return MOD_RES_ALLOW;
154                         }
155                 }
156                 return MOD_RES_PASSTHRU;
157         }
158
159         ModResult OnPreMode(User* source, User* dest, Channel* channel, Modes::ChangeList& modes) CXX11_OVERRIDE
160         {
161                 if (!channel)
162                         return MOD_RES_PASSTHRU;
163                 if (!source->IsOper() || !IS_LOCAL(source))
164                         return MOD_RES_PASSTHRU;
165
166                 const Modes::ChangeList::List& list = modes.getlist();
167                 unsigned int mode = channel->GetPrefixValue(source);
168
169                 if (!IsOverride(mode, list))
170                         return MOD_RES_PASSTHRU;
171
172                 if (CanOverride(source, "MODE"))
173                 {
174                         std::string msg = source->nick + " used oper override to set modes on " + channel->name + ": ";
175
176                         // Construct a MODE string in the old format for sending it as a snotice
177                         std::string params;
178                         char pm = 0;
179                         for (Modes::ChangeList::List::const_iterator i = list.begin(); i != list.end(); ++i)
180                         {
181                                 const Modes::Change& item = *i;
182                                 if (!item.param.empty())
183                                         params.append(1, ' ').append(item.param);
184
185                                 char wanted_pm = (item.adding ? '+' : '-');
186                                 if (wanted_pm != pm)
187                                 {
188                                         pm = wanted_pm;
189                                         msg += pm;
190                                 }
191
192                                 msg += item.mh->GetModeChar();
193                         }
194                         msg += params;
195                         ServerInstance->SNO->WriteGlobalSno('v',msg);
196                         return MOD_RES_ALLOW;
197                 }
198                 return MOD_RES_PASSTHRU;
199         }
200
201         ModResult OnUserPreJoin(LocalUser* user, Channel* chan, const std::string& cname, std::string& privs, const std::string& keygiven) CXX11_OVERRIDE
202         {
203                 if (user->IsOper())
204                 {
205                         if (chan)
206                         {
207                                 if (chan->IsModeSet(inviteonly) && (CanOverride(user,"INVITE")))
208                                 {
209                                         if (!invapi->IsInvited(user, chan))
210                                                 return HandleJoinOverride(user, chan, keygiven, "invite-only", "+i");
211                                         return MOD_RES_ALLOW;
212                                 }
213
214                                 if (chan->IsModeSet(key) && (CanOverride(user,"KEY")) && keygiven != chan->GetModeParameter(key))
215                                         return HandleJoinOverride(user, chan, keygiven, "the channel key", "+k");
216
217                                 if (chan->IsModeSet(limit) && (chan->GetUserCounter() >= ConvToNum<size_t>(chan->GetModeParameter(limit))) && (CanOverride(user,"LIMIT")))
218                                         return HandleJoinOverride(user, chan, keygiven, "the channel limit", "+l");
219
220                                 if (chan->IsBanned(user) && CanOverride(user,"BANWALK"))
221                                         return HandleJoinOverride(user, chan, keygiven, "channel ban", "channel ban");
222                         }
223                 }
224                 return MOD_RES_PASSTHRU;
225         }
226
227         Version GetVersion() CXX11_OVERRIDE
228         {
229                 return Version("Allows server operators to be given privileges that allow them to ignore various channel-level restrictions.",VF_VENDOR);
230         }
231 };
232
233 MODULE_INIT(ModuleOverride)