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