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