1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 /* Support for a dancer-style /remove command, an alternative to /kick to try and avoid auto-rejoin-on-kick scripts */
18 /* Written by Om, 25-03-05 */
24 #include "configreader.h"
27 /* $ModDesc: Provides a /remove command, this is mostly an alternative to /kick, except makes users appear to have parted the channel */
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.
35 /** Base class for /FPART and /REMOVE
41 InspIRCd* ServerInstance;
44 RemoveBase(InspIRCd* Instance, bool& snk) : supportnokicks(snk), ServerInstance(Instance)
48 enum ModeLevel { PEON = 0, HALFOP = 1, OP = 2, ADMIN = 3, OWNER = 4, ULINE = 5 };
50 /* This little function just converts a chanmode character (U ~ & @ & +) into an integer (5 4 3 2 1 0) */
51 /* XXX - We should probably use the new mode prefix rank stuff
52 * for this instead now -- Brain */
53 ModeLevel chartolevel(const std::string &privs)
83 CmdResult Handle (const char** parameters, int pcnt, userrec *user, bool neworder)
92 std::string protectkey;
93 std::string founderkey;
96 /* Set these to the parameters needed, the new version of this module switches it's parameters around
97 * supplying a new command with the new order while keeping the old /remove with the older order.
98 * /remove <nick> <channel> [reason ...]
99 * /fpart <channel> <nick> [reason ...]
101 channame = parameters[ neworder ? 0 : 1];
102 username = parameters[ neworder ? 1 : 0];
104 /* Look up the user we're meant to be removing from the channel */
105 target = ServerInstance->FindNick(username);
107 /* And the channel we're meant to be removing them from */
108 channel = ServerInstance->FindChan(channame);
110 /* Fix by brain - someone needs to learn to validate their input! */
111 if (!target || !channel)
113 user->WriteServ("401 %s %s :No such nick/channel", user->nick, !target ? username : channame);
117 if (!channel->HasUser(target))
119 user->WriteServ( "NOTICE %s :*** The user %s is not on channel %s", user->nick, target->nick, channel->name);
123 /* This is adding support for the +q and +a channel modes, basically if they are enabled, and the remover has them set.
124 * Then we change the @|%|+ to & if they are +a, or ~ if they are +q */
125 protectkey = "cm_protect_" + std::string(channel->name);
126 founderkey = "cm_founder_" + std::string(channel->name);
128 if (ServerInstance->ULine(user->server) || ServerInstance->ULine(user->nick))
130 ServerInstance->Log(DEBUG, "Setting ulevel to U");
131 ulevel = chartolevel("U");
133 if (user->GetExt(founderkey))
135 ServerInstance->Log(DEBUG, "Setting ulevel to ~");
136 ulevel = chartolevel("~");
138 else if (user->GetExt(protectkey))
140 ServerInstance->Log(DEBUG, "Setting ulevel to &");
141 ulevel = chartolevel("&");
145 ServerInstance->Log(DEBUG, "Setting ulevel to %s", channel->GetPrefixChar(user));
146 ulevel = chartolevel(channel->GetPrefixChar(user));
149 /* Now it's the same idea, except for the target. If they're ulined make sure they get a higher level than the sender can */
150 if (ServerInstance->ULine(target->server) || ServerInstance->ULine(target->nick))
152 ServerInstance->Log(DEBUG, "Setting tlevel to U");
153 tlevel = chartolevel("U");
155 else if (target->GetExt(founderkey))
157 ServerInstance->Log(DEBUG, "Setting tlevel to ~");
158 tlevel = chartolevel("~");
160 else if (target->GetExt(protectkey))
162 ServerInstance->Log(DEBUG, "Setting tlevel to &");
163 tlevel = chartolevel("&");
167 ServerInstance->Log(DEBUG, "Setting tlevel to %s", channel->GetPrefixChar(target));
168 tlevel = chartolevel(channel->GetPrefixChar(target));
171 hasnokicks = (ServerInstance->FindModule("m_nokicks.so") && channel->IsModeSet('Q'));
173 /* 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 */
174 if ((!IS_LOCAL(user)) || (!supportnokicks || !hasnokicks || (ulevel == ULINE)))
176 /* We'll let everyone remove their level and below, eg:
177 * ops can remove ops, halfops, voices, and those with no mode (no moders actually are set to 1)
178 * a ulined target will get a higher level than it's possible for a /remover to get..so they're safe.
179 * Nobody may remove a founder.
181 if ((!IS_LOCAL(user)) || ((ulevel > PEON) && (ulevel >= tlevel) && (tlevel != OWNER)))
183 // no you can't just go from a std::ostringstream to a std::string, Om. -nenolod
184 // but you can do this, nenolod -brain
186 std::string reasonparam("No reason given");
188 /* If a reason is given, use it */
191 /* Join params 2 ... pcnt - 1 (inclusive) into one */
192 irc::stringjoiner reason_join(" ", parameters, 2, pcnt - 1);
193 reasonparam = reason_join.GetJoined();
196 /* Build up the part reason string. */
197 reason = std::string("Removed by ") + user->nick + ": " + reasonparam;
199 channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s removed %s from the channel", channel->name, user->nick, target->nick);
200 target->WriteServ("NOTICE %s :*** %s removed you from %s with the message: %s", target->nick, user->nick, channel->name, reasonparam.c_str());
202 if (!channel->PartUser(target, reason.c_str()))
207 user->WriteServ( "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick, target->nick, channel->name);
213 /* m_nokicks.so was loaded and +Q was set, block! */
214 user->WriteServ( "484 %s %s :Can't remove user %s from channel (+Q set)", user->nick, channel->name, target->nick);
224 class cmd_remove : public command_t, public RemoveBase
227 cmd_remove(InspIRCd* Instance, bool& snk) : command_t(Instance, "REMOVE", 0, 2), RemoveBase(Instance, snk)
229 this->source = "m_remove.so";
230 syntax = "<nick> <channel> [<reason>]";
233 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
235 return RemoveBase::Handle(parameters, pcnt, user, false);
241 class cmd_fpart : public command_t, public RemoveBase
244 cmd_fpart(InspIRCd* Instance, bool& snk) : command_t(Instance, "FPART", 0, 2), RemoveBase(Instance, snk)
246 this->source = "m_remove.so";
247 syntax = "<channel> <nick> [<reason>]";
250 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
252 return RemoveBase::Handle(parameters, pcnt, user, true);
256 class ModuleRemove : public Module
258 cmd_remove* mycommand;
259 cmd_fpart* mycommand2;
264 ModuleRemove(InspIRCd* Me)
267 mycommand = new cmd_remove(ServerInstance, supportnokicks);
268 mycommand2 = new cmd_fpart(ServerInstance, supportnokicks);
269 ServerInstance->AddCommand(mycommand);
270 ServerInstance->AddCommand(mycommand2);
274 void Implements(char* List)
276 List[I_On005Numeric] = List[I_OnRehash] = 1;
279 virtual void On005Numeric(std::string &output)
281 output.append(" REMOVE");
284 virtual void OnRehash(const std::string&)
286 ConfigReader conf(ServerInstance);
287 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
290 virtual ~ModuleRemove()
296 virtual Version GetVersion()
298 return Version(1,1,1,0,VF_VENDOR,API_VERSION);
303 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
305 class ModuleRemoveFactory : public ModuleFactory
308 ModuleRemoveFactory()
312 ~ModuleRemoveFactory()
316 virtual Module * CreateModule(InspIRCd* Me)
318 return new ModuleRemove(Me);
324 extern "C" void * init_module( void )
326 return new ModuleRemoveFactory;