]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
m_remove Recognize /REMOVE <chan> <nick>
[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         RemoveBase(Module* Creator, bool& snk, ChanModeReference& nkm, const char* cmdn)
42                 : Command(Creator, cmdn, 2, 3)
43                 , supportnokicks(snk)
44                 , nokicksmode(nkm)
45         {
46         }
47
48         CmdResult HandleRMB(const std::vector<std::string>& parameters, User *user, bool fpart)
49         {
50                 User* target;
51                 Channel* channel;
52                 std::string reason;
53
54                 // If the command is a /REMOVE then detect the parameter order
55                 bool neworder = ((fpart) || (parameters[0][0] == '#'));
56
57                 /* Set these to the parameters needed, the new version of this module switches it's parameters around
58                  * supplying a new command with the new order while keeping the old /remove with the older order.
59                  * /remove <nick> <channel> [reason ...]
60                  * /fpart <channel> <nick> [reason ...]
61                  */
62                 const std::string& channame = parameters[neworder ? 0 : 1];
63                 const std::string& username = parameters[neworder ? 1 : 0];
64
65                 /* Look up the user we're meant to be removing from the channel */
66                 if (IS_LOCAL(user))
67                         target = ServerInstance->FindNickOnly(username);
68                 else
69                         target = ServerInstance->FindNick(username);
70
71                 /* And the channel we're meant to be removing them from */
72                 channel = ServerInstance->FindChan(channame);
73
74                 /* Fix by brain - someone needs to learn to validate their input! */
75                 if ((!target) || (target->registered != REG_ALL) || (!channel))
76                 {
77                         user->WriteNumeric(ERR_NOSUCHNICK, "%s :No such nick/channel", !channel ? channame.c_str() : username.c_str());
78                         return CMD_FAILURE;
79                 }
80
81                 if (!channel->HasUser(target))
82                 {
83                         user->WriteServ( "NOTICE %s :*** The user %s is not on channel %s", user->nick.c_str(), target->nick.c_str(), channel->name.c_str());
84                         return CMD_FAILURE;
85                 }
86
87                 int ulevel = channel->GetPrefixValue(user);
88                 int tlevel = channel->GetPrefixValue(target);
89
90                 if (target->server->IsULine())
91                 {
92                         user->WriteNumeric(482, "%s :Only a u-line may remove a u-line from a channel.", channame.c_str());
93                         return CMD_FAILURE;
94                 }
95
96                 /* 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 */
97                 if ((!IS_LOCAL(user)) || (!supportnokicks) || (!channel->IsModeSet(nokicksmode)))
98                 {
99                         /* We'll let everyone remove their level and below, eg:
100                          * ops can remove ops, halfops, voices, and those with no mode (no moders actually are set to 1)
101                          * a ulined target will get a higher level than it's possible for a /remover to get..so they're safe.
102                          * Nobody may remove a founder.
103                          */
104                         if ((!IS_LOCAL(user)) || ((ulevel > VOICE_VALUE) && (ulevel >= tlevel) && (tlevel != 50000)))
105                         {
106                                 // 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)
107                                 if (!IS_LOCAL(target))
108                                 {
109                                         // Send an ENCAP REMOVE with parameters being in the old <user> <chan> order which is
110                                         // compatible with both 2.0 and 2.2. This also turns FPART into REMOVE.
111                                         std::vector<std::string> p;
112                                         p.push_back(target->uuid);
113                                         p.push_back(channel->name);
114                                         if (parameters.size() > 2)
115                                                 p.push_back(":" + parameters[2]);
116                                         ServerInstance->PI->SendEncapsulatedData(target->server->GetName(), "REMOVE", p, user);
117
118                                         return CMD_SUCCESS;
119                                 }
120
121                                 std::string reasonparam;
122
123                                 /* If a reason is given, use it */
124                                 if(parameters.size() > 2)
125                                         reasonparam = parameters[2];
126                                 else
127                                         reasonparam = "No reason given";
128
129                                 /* Build up the part reason string. */
130                                 reason = "Removed by " + user->nick + ": " + reasonparam;
131
132                                 channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s removed %s from the channel", channel->name.c_str(), user->nick.c_str(), target->nick.c_str());
133                                 target->WriteNotice("*** " + user->nick + " removed you from " + channel->name + " with the message: " + reasonparam);
134
135                                 channel->PartUser(target, reason);
136                         }
137                         else
138                         {
139                                 user->WriteServ( "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick.c_str(), target->nick.c_str(), channel->name.c_str());
140                                 return CMD_FAILURE;
141                         }
142                 }
143                 else
144                 {
145                         /* m_nokicks.so was loaded and +Q was set, block! */
146                         user->WriteNumeric(ERR_RESTRICTED, "%s :Can't remove user %s from channel (nokicks mode is set)", channel->name.c_str(), target->nick.c_str());
147                         return CMD_FAILURE;
148                 }
149
150                 return CMD_SUCCESS;
151         }
152 };
153
154 /** Handle /REMOVE
155  */
156 class CommandRemove : public RemoveBase
157 {
158  public:
159         CommandRemove(Module* Creator, bool& snk, ChanModeReference& nkm)
160                 : RemoveBase(Creator, snk, nkm, "REMOVE")
161         {
162                 syntax = "<channel> <nick> [<reason>]";
163                 TRANSLATE3(TR_NICK, TR_TEXT, TR_TEXT);
164         }
165
166         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
167         {
168                 return HandleRMB(parameters, user, false);
169         }
170 };
171
172 /** Handle /FPART
173  */
174 class CommandFpart : public RemoveBase
175 {
176  public:
177         CommandFpart(Module* Creator, bool& snk, ChanModeReference& nkm)
178                 : RemoveBase(Creator, snk, nkm, "FPART")
179         {
180                 syntax = "<channel> <nick> [<reason>]";
181                 TRANSLATE3(TR_TEXT, TR_NICK, TR_TEXT);
182         }
183
184         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
185         {
186                 return HandleRMB(parameters, user, true);
187         }
188 };
189
190 class ModuleRemove : public Module
191 {
192         ChanModeReference nokicksmode;
193         CommandRemove cmd1;
194         CommandFpart cmd2;
195         bool supportnokicks;
196
197  public:
198         ModuleRemove()
199                 : nokicksmode(this, "nokick")
200                 , cmd1(this, supportnokicks, nokicksmode)
201                 , cmd2(this, supportnokicks, nokicksmode)
202         {
203         }
204
205         void On005Numeric(std::map<std::string, std::string>& tokens) CXX11_OVERRIDE
206         {
207                 tokens["REMOVE"];
208         }
209
210         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
211         {
212                 supportnokicks = ServerInstance->Config->ConfValue("remove")->getBool("supportnokicks");
213         }
214
215         Version GetVersion() CXX11_OVERRIDE
216         {
217                 return Version("Provides a /remove command, this is mostly an alternative to /kick, except makes users appear to have parted the channel", VF_OPTCOMMON | VF_VENDOR);
218         }
219 };
220
221 MODULE_INIT(ModuleRemove)