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