]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
Describe module purpose in /MODULES output
[user/henk/code/inspircd.git] / src / modules / m_remove.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 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                 /* 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 */
81                 if ((!IS_LOCAL(user)) || (!supportnokicks || !hasnokicks))
82                 {
83                         /* We'll let everyone remove their level and below, eg:
84                          * ops can remove ops, halfops, voices, and those with no mode (no moders actually are set to 1)
85                          * a ulined target will get a higher level than it's possible for a /remover to get..so they're safe.
86                          * Nobody may remove a founder.
87                          */
88                         if ((!IS_LOCAL(user)) || ((ulevel > VOICE_VALUE) && (ulevel >= tlevel) && (tlevel != 50000)))
89                         {
90                                 // no you can't just go from a std::ostringstream to a std::string, Om. -nenolod
91                                 // but you can do this, nenolod -brain
92
93                                 std::string reasonparam("No reason given");
94
95                                 /* If a reason is given, use it */
96                                 if(parameters.size() > 2)
97                                 {
98                                         /* Join params 2 ... pcnt - 1 (inclusive) into one */
99                                         irc::stringjoiner reason_join(" ", parameters, 2, parameters.size() - 1);
100                                         reasonparam = reason_join.GetJoined();
101                                 }
102
103                                 /* Build up the part reason string. */
104                                 reason = std::string("Removed by ") + user->nick + ": " + reasonparam;
105
106                                 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());
107                                 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());
108
109                                 if (!channel->PartUser(target, reason))
110                                         delete channel;
111                         }
112                         else
113                         {
114                                 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());
115                                 return CMD_FAILURE;
116                         }
117                 }
118                 else
119                 {
120                         /* m_nokicks.so was loaded and +Q was set, block! */
121                         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());
122                         return CMD_FAILURE;
123                 }
124
125                 return CMD_SUCCESS;
126         }
127         virtual RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters) = 0;
128 };
129
130 /** Handle /REMOVE
131  */
132 class CommandRemove : public RemoveBase
133 {
134  public:
135         CommandRemove(Module* Creator, bool& snk)
136                 : RemoveBase(Creator, snk, "REMOVE")
137         {
138                 syntax = "<nick> <channel> [<reason>]";
139                 TRANSLATE4(TR_NICK, TR_TEXT, TR_TEXT, TR_END);
140         }
141
142         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
143         {
144                 return HandleRMB(parameters, user, false);
145         }
146
147         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
148         {
149                 User* dest = ServerInstance->FindNick(parameters[0]);
150                 if (dest)
151                         return ROUTE_OPT_UCAST(dest->server);
152                 return ROUTE_LOCALONLY;
153         }
154 };
155
156 /** Handle /FPART
157  */
158 class CommandFpart : public RemoveBase
159 {
160  public:
161         CommandFpart(Module* Creator, bool& snk)
162                 : RemoveBase(Creator, snk, "FPART")
163         {
164                 syntax = "<channel> <nick> [<reason>]";
165                 TRANSLATE4(TR_TEXT, TR_NICK, TR_TEXT, TR_END);
166         }
167
168         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
169         {
170                 return HandleRMB(parameters, user, true);
171         }
172
173         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
174         {
175                 User* dest = ServerInstance->FindNick(parameters[1]);
176                 if (dest)
177                         return ROUTE_OPT_UCAST(dest->server);
178                 return ROUTE_LOCALONLY;
179         }
180 };
181
182 class ModuleRemove : public Module
183 {
184         CommandRemove cmd1;
185         CommandFpart cmd2;
186         bool supportnokicks;
187
188
189  public:
190         ModuleRemove(InspIRCd*) : cmd1(this, supportnokicks), cmd2(this, supportnokicks)
191         {
192                 ServerInstance->AddCommand(&cmd1);
193                 ServerInstance->AddCommand(&cmd2);
194                 OnRehash(NULL);
195                 Implementation eventlist[] = { I_On005Numeric, I_OnRehash };
196                 ServerInstance->Modules->Attach(eventlist, this, 2);
197         }
198
199
200         virtual void On005Numeric(std::string &output)
201         {
202                 output.append(" REMOVE");
203         }
204
205         virtual void OnRehash(User* user)
206         {
207                 ConfigReader conf(ServerInstance);
208                 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
209         }
210
211         virtual ~ModuleRemove()
212         {
213         }
214
215         virtual Version GetVersion()
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, API_VERSION);
218         }
219
220 };
221
222 MODULE_INIT(ModuleRemove)