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