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