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"
12 /* $ModDesc: Provides a /remove command, this is mostly an alternative to /kick, except makes users appear to have parted the channel */
15 * This module supports the use of the +q and +a usermodes, but should work without them too.
16 * Usage of the command is restricted to +hoaq, and you cannot remove a user with a "higher" level than yourself.
17 * eg: +h can remove +hv and users with no modes. +a can remove +aohv and users with no modes.
27 RemoveBase(Server* Me, bool& snk)
28 : Srv(Me), supportnokicks(snk)
32 enum ModeLevel { PEON = 0, HALFOP = 1, OP = 2, ADMIN = 3, OWNER = 4, ULINE = 5 };
34 /* This little function just converts a chanmode character (U ~ & @ & +) into an integer (5 4 3 2 1 0) */
35 /* XXX - this could be handy in the core, so it can be used elsewhere */
36 ModeLevel chartolevel(const std::string &privs)
66 void Handle (const char** parameters, int pcnt, userrec *user, bool neworder)
74 std::ostringstream reason;
75 std::string protectkey;
76 std::string founderkey;
79 /* Set these to the parameters needed, the new version of this module switches it's parameters around
80 * supplying a new command with the new order while keeping the old /remove with the older order.
81 * /remove <nick> <channel> [reason ...]
82 * /fpart <channel> <nick> [reason ...]
84 channame = parameters[ neworder ? 0 : 1];
85 username = parameters[ neworder ? 1 : 0];
87 /* Look up the user we're meant to be removing from the channel */
88 target = Srv->FindNick(username);
90 /* And the channel we're meant to be removing them from */
91 channel = Srv->FindChannel(channame);
93 /* Fix by brain - someone needs to learn to validate their input! */
94 if (!target || !channel)
96 user->WriteServ("401 %s %s :No such nick/channel", user->nick, !target ? username : channame);
100 if (!channel->HasUser(target))
102 user->WriteServ( "NOTICE %s :*** The user %s is not on channel %s", user->nick, target->nick, channel->name);
106 /* This is adding support for the +q and +a channel modes, basically if they are enabled, and the remover has them set.
107 * Then we change the @|%|+ to & if they are +a, or ~ if they are +q */
108 protectkey = "cm_protect_" + std::string(channel->name);
109 founderkey = "cm_founder_" + std::string(channel->name);
111 if (Srv->IsUlined(user->server) || Srv->IsUlined(user->nick))
113 log(DEBUG, "Setting ulevel to U");
114 ulevel = chartolevel("U");
116 if (user->GetExt(founderkey))
118 log(DEBUG, "Setting ulevel to ~");
119 ulevel = chartolevel("~");
121 else if (user->GetExt(protectkey))
123 log(DEBUG, "Setting ulevel to &");
124 ulevel = chartolevel("&");
128 log(DEBUG, "Setting ulevel to %s", Srv->ChanMode(user, channel).c_str());
129 ulevel = chartolevel(Srv->ChanMode(user, channel));
132 /* 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 */
133 if (Srv->IsUlined(target->server) || Srv->IsUlined(target->nick))
135 log(DEBUG, "Setting tlevel to U");
136 tlevel = chartolevel("U");
138 else if (target->GetExt(founderkey))
140 log(DEBUG, "Setting tlevel to ~");
141 tlevel = chartolevel("~");
143 else if (target->GetExt(protectkey))
145 log(DEBUG, "Setting tlevel to &");
146 tlevel = chartolevel("&");
150 log(DEBUG, "Setting tlevel to %s", Srv->ChanMode(target, channel).c_str());
151 tlevel = chartolevel(Srv->ChanMode(target, channel));
154 hasnokicks = (Srv->FindModule("m_nokicks.so") && channel->IsModeSet('Q'));
156 /* 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 */
157 if(!supportnokicks || !hasnokicks || (ulevel == ULINE))
159 /* We'll let everyone remove their level and below, eg:
160 * ops can remove ops, halfops, voices, and those with no mode (no moders actually are set to 1)
161 * a ulined target will get a higher level than it's possible for a /remover to get..so they're safe.
162 * Nobody may remove a founder.
164 if ((ulevel > PEON) && (ulevel >= tlevel) && (tlevel != OWNER))
166 std::string reasonparam;
168 /* If a reason is given, use it */
173 /* Use all the remaining parameters as the reason */
174 for(int i = 2; i < pcnt; i++)
176 reason << " " << parameters[i];
179 reasonparam = reason.str();
183 /* Build up the part reason string. */
184 reason << "Removed by " << user->nick << reasonparam;
186 channel->WriteChannelWithServ(Srv->GetServerName().c_str(), "NOTICE %s :%s removed %s from the channel", channel->name, user->nick, target->nick);
187 target->WriteServ("NOTICE %s :*** %s removed you from %s with the message: %s", target->nick, user->nick, channel->name, reasonparam.c_str());
189 if (!channel->PartUser(target, reason.str().c_str()))
194 user->WriteServ( "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick, target->nick, channel->name);
199 /* m_nokicks.so was loaded and +Q was set, block! */
200 user->WriteServ( "484 %s %s :Can't remove user %s from channel (+Q set)", user->nick, channel->name, target->nick);
205 class cmd_remove : public command_t, public RemoveBase
208 cmd_remove(Server* Srv, bool& snk) : command_t("REMOVE", 0, 2), RemoveBase(Srv, snk)
210 this->source = "m_remove.so";
211 syntax = "<nick> <channel> [<reason>]";
214 void Handle (const char** parameters, int pcnt, userrec *user)
216 RemoveBase::Handle(parameters, pcnt, user, false);
220 class cmd_fpart : public command_t, public RemoveBase
223 cmd_fpart(Server* Srv, bool snk) : command_t("FPART", 0, 2), RemoveBase(Srv, snk)
225 this->source = "m_remove.so";
226 syntax = "<channel> <nick> [<reason>]";
229 void Handle (const char** parameters, int pcnt, userrec *user)
231 RemoveBase::Handle(parameters, pcnt, user, true);
235 class ModuleRemove : public Module
237 cmd_remove* mycommand;
238 cmd_fpart* mycommand2;
242 ModuleRemove(Server* Me)
245 mycommand = new cmd_remove(Me, supportnokicks);
246 mycommand2 = new cmd_fpart(Me, supportnokicks);
247 Me->AddCommand(mycommand);
248 Me->AddCommand(mycommand2);
252 void Implements(char* List)
254 List[I_On005Numeric] = List[I_OnRehash] = 1;
257 virtual void On005Numeric(std::string &output)
259 output.append(" REMOVE");
262 virtual void OnRehash(const std::string&)
266 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
269 virtual ~ModuleRemove()
275 virtual Version GetVersion()
277 return Version(1,0,1,0,VF_VENDOR);
282 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
284 class ModuleRemoveFactory : public ModuleFactory
287 ModuleRemoveFactory()
291 ~ModuleRemoveFactory()
295 virtual Module * CreateModule(Server* Me)
297 return new ModuleRemove(Me);
303 extern "C" void * init_module( void )
305 return new ModuleRemoveFactory;