1 /* Support for a dancer-style /remove command, an alternative to /kick to try and avoid auto-rejoin-on-kick scripts */
2 /* Written by Om, 25-03-05 */
9 #include "helperfuncs.h"
11 /* $ModDesc: Provides a /remove command, this is mostly an alternative to /kick, except makes users appear to have parted the channel */
14 * This module supports the use of the +q and +a usermodes, but should work without them too.
15 * Usage of the command is restricted to +hoaq, and you cannot remove a user with a "higher" level than yourself.
16 * eg: +h can remove +hv and users with no modes. +a can remove +aohv and users with no modes.
26 RemoveBase(Server* Me, bool& snk)
27 : Srv(Me), supportnokicks(snk)
31 enum ModeLevel { PEON = 0, HALFOP = 1, OP = 2, ADMIN = 3, OWNER = 4, ULINE = 5 };
33 /* This little function just converts a chanmode character (U ~ & @ & +) into an integer (5 4 3 2 1 0) */
34 /* XXX - this could be handy in the core, so it can be used elsewhere */
35 ModeLevel chartolevel(const std::string &privs)
65 void Handle (const char** parameters, int pcnt, userrec *user, bool neworder)
73 std::ostringstream reason;
74 std::string protectkey;
75 std::string founderkey;
78 /* Set these to the parameters needed, the new version of this module switches it's parameters around
79 * supplying a new command with the new order while keeping the old /remove with the older order.
80 * /remove <nick> <channel> [reason ...]
81 * /fpart <channel> <nick> [reason ...]
83 channame = parameters[ neworder ? 0 : 1];
84 username = parameters[ neworder ? 1 : 0];
86 /* Look up the user we're meant to be removing from the channel */
87 target = Srv->FindNick(username);
89 /* And the channel we're meant to be removing them from */
90 channel = Srv->FindChannel(channame);
92 /* Fix by brain - someone needs to learn to validate their input! */
93 if (!target || !channel)
95 WriteServ(user->fd,"401 %s %s :No such nick/channel", user->nick, !target ? username : channame);
99 if (!channel->HasUser(target))
101 WriteServ(user->fd, "NOTICE %s :*** The user %s is not on channel %s", user->nick, target->nick, channel->name);
105 /* This is adding support for the +q and +a channel modes, basically if they are enabled, and the remover has them set.
106 * Then we change the @|%|+ to & if they are +a, or ~ if they are +q */
107 protectkey = "cm_protect_" + std::string(channel->name);
108 founderkey = "cm_founder_" + std::string(channel->name);
110 if (Srv->IsUlined(user->server) || Srv->IsUlined(user->nick))
112 log(DEBUG, "Setting ulevel to U");
113 ulevel = chartolevel("U");
115 if (user->GetExt(founderkey))
117 log(DEBUG, "Setting ulevel to ~");
118 ulevel = chartolevel("~");
120 else if (user->GetExt(protectkey))
122 log(DEBUG, "Setting ulevel to &");
123 ulevel = chartolevel("&");
127 log(DEBUG, "Setting ulevel to %s", Srv->ChanMode(user, channel).c_str());
128 ulevel = chartolevel(Srv->ChanMode(user, channel));
131 /* 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 */
132 if (Srv->IsUlined(target->server) || Srv->IsUlined(target->nick))
134 log(DEBUG, "Setting tlevel to U");
135 tlevel = chartolevel("U");
137 else if (target->GetExt(founderkey))
139 log(DEBUG, "Setting tlevel to ~");
140 tlevel = chartolevel("~");
142 else if (target->GetExt(protectkey))
144 log(DEBUG, "Setting tlevel to &");
145 tlevel = chartolevel("&");
149 log(DEBUG, "Setting tlevel to %s", Srv->ChanMode(target, channel).c_str());
150 tlevel = chartolevel(Srv->ChanMode(target, channel));
153 hasnokicks = (Srv->FindModule("m_nokicks.so") && channel->IsModeSet('Q'));
155 /* 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 */
156 if(!supportnokicks || !hasnokicks || (ulevel == ULINE))
158 /* We'll let everyone remove their level and below, eg:
159 * ops can remove ops, halfops, voices, and those with no mode (no moders actually are set to 1)
160 * a ulined target will get a higher level than it's possible for a /remover to get..so they're safe.
161 * Nobody may remove a founder.
163 if ((ulevel > PEON) && (ulevel >= tlevel) && (tlevel != OWNER))
165 std::string reasonparam;
167 /* If a reason is given, use it */
172 /* Use all the remaining parameters as the reason */
173 for(int i = 2; i < pcnt; i++)
175 reason << " " << parameters[i];
178 reasonparam = reason.str();
182 /* Build up the part reason string. */
183 reason << "Removed by " << user->nick << reasonparam;
185 channel->WriteChannelWithServ(Srv->GetServerName().c_str(), "NOTICE %s :%s removed %s from the channel", channel->name, user->nick, target->nick);
186 WriteServ(target->fd, "NOTICE %s :*** %s removed you from %s with the message: %s", target->nick, user->nick, channel->name, reasonparam.c_str());
188 if (!channel->PartUser(target, reason.str().c_str()))
193 WriteServ(user->fd, "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick, target->nick, channel->name);
198 /* m_nokicks.so was loaded and +Q was set, block! */
199 WriteServ(user->fd, "484 %s %s :Can't remove user %s from channel (+Q set)", user->nick, channel->name, target->nick);
204 class cmd_remove : public command_t, public RemoveBase
207 cmd_remove(Server* Srv, bool& snk) : command_t("REMOVE", 0, 2), RemoveBase(Srv, snk)
209 this->source = "m_remove.so";
210 syntax = "<nick> <channel> [<reason>]";
213 void Handle (const char** parameters, int pcnt, userrec *user)
215 RemoveBase::Handle(parameters, pcnt, user, false);
219 class cmd_fpart : public command_t, public RemoveBase
222 cmd_fpart(Server* Srv, bool snk) : command_t("FPART", 0, 2), RemoveBase(Srv, snk)
224 this->source = "m_remove.so";
225 syntax = "<channel> <nick> [<reason>]";
228 void Handle (const char** parameters, int pcnt, userrec *user)
230 RemoveBase::Handle(parameters, pcnt, user, true);
234 class ModuleRemove : public Module
236 cmd_remove* mycommand;
237 cmd_fpart* mycommand2;
241 ModuleRemove(Server* Me)
244 mycommand = new cmd_remove(Me, supportnokicks);
245 mycommand2 = new cmd_fpart(Me, supportnokicks);
246 Me->AddCommand(mycommand);
247 Me->AddCommand(mycommand2);
251 void Implements(char* List)
253 List[I_On005Numeric] = List[I_OnRehash] = 1;
256 virtual void On005Numeric(std::string &output)
258 output.append(" REMOVE");
261 virtual void OnRehash(const std::string&)
265 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
268 virtual ~ModuleRemove()
274 virtual Version GetVersion()
276 return Version(1,0,1,0,VF_VENDOR);
281 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
283 class ModuleRemoveFactory : public ModuleFactory
286 ModuleRemoveFactory()
290 ~ModuleRemoveFactory()
294 virtual Module * CreateModule(Server* Me)
296 return new ModuleRemove(Me);
302 extern "C" void * init_module( void )
304 return new ModuleRemoveFactory;