]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
Fix a bunch of weird indentation and spacing issues.
[user/henk/code/inspircd.git] / src / modules / m_remove.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2017 B00mX0r <b00mx0r@aureus.pw>
5  *   Copyright (C) 2013, 2018, 2020 Sadie Powell <sadie@witchery.services>
6  *   Copyright (C) 2012-2014, 2016 Attila Molnar <attilamolnar@hush.com>
7  *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
8  *   Copyright (C) 2012 Justin Crawford <Justasic@Gmail.com>
9  *   Copyright (C) 2009 Uli Schlachter <psychon@inspircd.org>
10  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
11  *   Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
12  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
13  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
14  *   Copyright (C) 2006 Oliver Lupton <om@inspircd.org>
15  *   Copyright (C) 2005-2006, 2010 Craig Edwards <brain@inspircd.org>
16  *
17  * This file is part of InspIRCd.  InspIRCd is free software: you can
18  * redistribute it and/or modify it under the terms of the GNU General Public
19  * License as published by the Free Software Foundation, version 2.
20  *
21  * This program is distributed in the hope that it will be useful, but WITHOUT
22  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
23  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
24  * details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  */
29
30
31 #include "inspircd.h"
32
33 /*
34  * This module supports the use of the +q and +a usermodes, but should work without them too.
35  * Usage of the command is restricted to +hoaq, and you cannot remove a user with a "higher" level than yourself.
36  * eg: +h can remove +hv and users with no modes. +a can remove +aohv and users with no modes.
37 */
38
39 /** Base class for /FPART and /REMOVE
40  */
41 class RemoveBase : public Command
42 {
43         bool& supportnokicks;
44         ChanModeReference& nokicksmode;
45
46  public:
47         unsigned int protectedrank;
48
49         RemoveBase(Module* Creator, bool& snk, ChanModeReference& nkm, const char* cmdn)
50                 : Command(Creator, cmdn, 2, 3)
51                 , supportnokicks(snk)
52                 , nokicksmode(nkm)
53         {
54         }
55
56         CmdResult HandleRMB(User* user, const CommandBase::Params& parameters,  bool fpart)
57         {
58                 User* target;
59                 Channel* channel;
60                 std::string reason;
61
62                 // If the command is a /REMOVE then detect the parameter order
63                 bool neworder = ((fpart) || (parameters[0][0] == '#'));
64
65                 /* Set these to the parameters needed, the new version of this module switches it's parameters around
66                  * supplying a new command with the new order while keeping the old /remove with the older order.
67                  * /remove <nick> <channel> [reason ...]
68                  * /fpart <channel> <nick> [reason ...]
69                  */
70                 const std::string& channame = parameters[neworder ? 0 : 1];
71                 const std::string& username = parameters[neworder ? 1 : 0];
72
73                 /* Look up the user we're meant to be removing from the channel */
74                 if (IS_LOCAL(user))
75                         target = ServerInstance->FindNickOnly(username);
76                 else
77                         target = ServerInstance->FindNick(username);
78
79                 /* And the channel we're meant to be removing them from */
80                 channel = ServerInstance->FindChan(channame);
81
82                 /* Fix by brain - someone needs to learn to validate their input! */
83                 if (!channel)
84                 {
85                         user->WriteNumeric(Numerics::NoSuchChannel(channame));
86                         return CMD_FAILURE;
87                 }
88                 if ((!target) || (target->registered != REG_ALL))
89                 {
90                         user->WriteNumeric(Numerics::NoSuchNick(username));
91                         return CMD_FAILURE;
92                 }
93
94                 if (!channel->HasUser(target))
95                 {
96                         user->WriteNotice(InspIRCd::Format("*** User %s is not on channel %s", target->nick.c_str(), channel->name.c_str()));
97                         return CMD_FAILURE;
98                 }
99
100                 if (target->server->IsULine())
101                 {
102                         user->WriteNumeric(ERR_CHANOPRIVSNEEDED, channame, "Only a U-line may remove a U-line from a channel.");
103                         return CMD_FAILURE;
104                 }
105
106                 /* We support the +Q channel mode via. the m_nokicks module, if the module is loaded and the mode is set then disallow the /remove */
107                 if ((!IS_LOCAL(user)) || (!supportnokicks) || (!channel->IsModeSet(nokicksmode)))
108                 {
109                         /* We'll let everyone remove their level and below, eg:
110                          * ops can remove ops, halfops, voices, and those with no mode (no moders actually are set to 1)
111                          * a ulined target will get a higher level than it's possible for a /remover to get..so they're safe.
112                          * Nobody may remove people with >= protectedrank rank.
113                          */
114                         unsigned int ulevel = channel->GetPrefixValue(user);
115                         unsigned int tlevel = channel->GetPrefixValue(target);
116                         if ((!IS_LOCAL(user)) || ((ulevel > VOICE_VALUE) && (ulevel >= tlevel) && ((protectedrank == 0) || (tlevel < protectedrank))))
117                         {
118                                 // REMOVE will be sent to the target's server and it will reply with a PART (or do nothing if it doesn't understand the command)
119                                 if (!IS_LOCAL(target))
120                                 {
121                                         // Send an ENCAP REMOVE with parameters being in the old <user> <chan> order which is
122                                         // compatible with both 2.0 and 3.0. This also turns FPART into REMOVE.
123                                         CommandBase::Params p;
124                                         p.push_back(target->uuid);
125                                         p.push_back(channel->name);
126                                         if (parameters.size() > 2)
127                                                 p.push_back(":" + parameters[2]);
128                                         ServerInstance->PI->SendEncapsulatedData(target->server->GetName(), "REMOVE", p, user);
129
130                                         return CMD_SUCCESS;
131                                 }
132
133                                 std::string reasonparam;
134
135                                 /* If a reason is given, use it */
136                                 if(parameters.size() > 2)
137                                         reasonparam = parameters[2];
138                                 else
139                                         reasonparam = "No reason given";
140
141                                 /* Build up the part reason string. */
142                                 reason = "Removed by " + user->nick + ": " + reasonparam;
143
144                                 channel->WriteRemoteNotice(InspIRCd::Format("%s removed %s from the channel", user->nick.c_str(), target->nick.c_str()));
145                                 target->WriteNotice("*** " + user->nick + " removed you from " + channel->name + " with the message: " + reasonparam);
146
147                                 channel->PartUser(target, reason);
148                         }
149                         else
150                         {
151                                 user->WriteNotice(InspIRCd::Format("*** You do not have access to /REMOVE %s from %s", target->nick.c_str(), channel->name.c_str()));
152                                 return CMD_FAILURE;
153                         }
154                 }
155                 else
156                 {
157                         /* m_nokicks.so was loaded and +Q was set, block! */
158                         user->WriteNumeric(ERR_RESTRICTED, channel->name, InspIRCd::Format("Can't remove user %s from channel (+Q is set)", target->nick.c_str()));
159                         return CMD_FAILURE;
160                 }
161
162                 return CMD_SUCCESS;
163         }
164 };
165
166 /** Handle /REMOVE
167  */
168 class CommandRemove : public RemoveBase
169 {
170  public:
171         CommandRemove(Module* Creator, bool& snk, ChanModeReference& nkm)
172                 : RemoveBase(Creator, snk, nkm, "REMOVE")
173         {
174                 syntax = "<channel> <nick> [:<reason>]";
175                 TRANSLATE3(TR_NICK, TR_TEXT, TR_TEXT);
176         }
177
178         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
179         {
180                 return HandleRMB(user, parameters, false);
181         }
182 };
183
184 /** Handle /FPART
185  */
186 class CommandFpart : public RemoveBase
187 {
188  public:
189         CommandFpart(Module* Creator, bool& snk, ChanModeReference& nkm)
190                 : RemoveBase(Creator, snk, nkm, "FPART")
191         {
192                 syntax = "<channel> <nick> [:<reason>]";
193                 TRANSLATE3(TR_TEXT, TR_NICK, TR_TEXT);
194         }
195
196         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE
197         {
198                 return HandleRMB(user, parameters, true);
199         }
200 };
201
202 class ModuleRemove : public Module
203 {
204         ChanModeReference nokicksmode;
205         CommandRemove cmd1;
206         CommandFpart cmd2;
207         bool supportnokicks;
208
209  public:
210         ModuleRemove()
211                 : nokicksmode(this, "nokick")
212                 , cmd1(this, supportnokicks, nokicksmode)
213                 , cmd2(this, supportnokicks, nokicksmode)
214         {
215         }
216
217         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
218         {
219                 tokens["REMOVE"];
220         }
221
222         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
223         {
224                 ConfigTag* tag = ServerInstance->Config->ConfValue("remove");
225                 supportnokicks = tag->getBool("supportnokicks");
226                 cmd1.protectedrank = cmd2.protectedrank = tag->getUInt("protectedrank", 50000);
227         }
228
229         Version GetVersion() CXX11_OVERRIDE
230         {
231                 return Version("Adds the /FPART and /REMOVE commands which allows channel operators to force part users from a channel.", VF_OPTCOMMON | VF_VENDOR);
232         }
233 };
234
235 MODULE_INIT(ModuleRemove)