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