]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
Tweak this module to always allow remove from remote, no matter what the permissions
[user/henk/code/inspircd.git] / src / modules / m_remove.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                     E-mail:
7  *              <brain@chatspike.net>
8  *              <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  * the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
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 */
19
20 #include <sstream>
21 #include "users.h"
22 #include "channels.h"
23 #include "modules.h"
24 #include "configreader.h"
25 #include "inspircd.h"
26
27 /* $ModDesc: Provides a /remove command, this is mostly an alternative to /kick, except makes users appear to have parted the channel */
28
29 /*      
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.
33 */
34
35 /** Base class for /FPART and /REMOVE
36  */
37 class RemoveBase
38 {
39  private: 
40         bool& supportnokicks;
41         InspIRCd* ServerInstance;
42  
43  protected:
44         RemoveBase(InspIRCd* Instance, bool& snk) : supportnokicks(snk), ServerInstance(Instance)
45         {
46         }               
47  
48         enum ModeLevel { PEON = 0, HALFOP = 1, OP = 2, ADMIN = 3, OWNER = 4, ULINE = 5 };        
49  
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)
54         {
55                 if(privs.empty())
56                 {
57                         return PEON;
58                 }
59         
60                 switch (privs[0])
61                 {
62                         case 'U':
63                                 /* Ulined */
64                                 return ULINE;
65                         case '~':
66                                 /* Owner */
67                                 return OWNER;
68                         case '&':
69                                 /* Admin */
70                                 return ADMIN;
71                         case '@':
72                                 /* Operator */
73                                 return OP;
74                         case '%':
75                                 /* Halfop */
76                                 return HALFOP;
77                         default:
78                                 /* Peon */
79                                 return PEON;
80                 }
81         }
82         
83         CmdResult Handle (const char** parameters, int pcnt, userrec *user, bool neworder)
84         {
85                 const char* channame;
86                 const char* username;
87                 userrec* target;
88                 chanrec* channel;
89                 ModeLevel tlevel;
90                 ModeLevel ulevel;
91                 std::string reason;
92                 std::string protectkey;
93                 std::string founderkey;
94                 bool hasnokicks;
95                 
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 ...]
100                  */
101                 channame = parameters[ neworder ? 0 : 1];
102                 username = parameters[ neworder ? 1 : 0];
103                 
104                 /* Look up the user we're meant to be removing from the channel */
105                 target = ServerInstance->FindNick(username);
106                 
107                 /* And the channel we're meant to be removing them from */
108                 channel = ServerInstance->FindChan(channame);
109
110                 /* Fix by brain - someone needs to learn to validate their input! */
111                 if (!target || !channel)
112                 {
113                         user->WriteServ("401 %s %s :No such nick/channel", user->nick, !target ? username : channame);
114                         return CMD_FAILURE;
115                 }
116
117                 if (!channel->HasUser(target))
118                 {
119                         user->WriteServ( "NOTICE %s :*** The user %s is not on channel %s", user->nick, target->nick, channel->name);
120                         return CMD_FAILURE;
121                 }       
122                 
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);
127                 
128                 if (ServerInstance->ULine(user->server) || ServerInstance->ULine(user->nick))
129                 {
130                         ServerInstance->Log(DEBUG, "Setting ulevel to U");
131                         ulevel = chartolevel("U");
132                 }
133                 if (user->GetExt(founderkey))
134                 {
135                         ServerInstance->Log(DEBUG, "Setting ulevel to ~");
136                         ulevel = chartolevel("~");
137                 }
138                 else if (user->GetExt(protectkey))
139                 {
140                         ServerInstance->Log(DEBUG, "Setting ulevel to &");
141                         ulevel = chartolevel("&");
142                 }
143                 else
144                 {
145                         ServerInstance->Log(DEBUG, "Setting ulevel to %s", channel->GetPrefixChar(user));
146                         ulevel = chartolevel(channel->GetPrefixChar(user));
147                 }
148                         
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))
151                 {
152                         ServerInstance->Log(DEBUG, "Setting tlevel to U");
153                         tlevel = chartolevel("U");
154                 }
155                 else if (target->GetExt(founderkey))
156                 {
157                         ServerInstance->Log(DEBUG, "Setting tlevel to ~");
158                         tlevel = chartolevel("~");
159                 }
160                 else if (target->GetExt(protectkey))
161                 {
162                         ServerInstance->Log(DEBUG, "Setting tlevel to &");
163                         tlevel = chartolevel("&");
164                 }
165                 else
166                 {
167                         ServerInstance->Log(DEBUG, "Setting tlevel to %s", channel->GetPrefixChar(target));
168                         tlevel = chartolevel(channel->GetPrefixChar(target));
169                 }
170                 
171                 hasnokicks = (ServerInstance->FindModule("m_nokicks.so") && channel->IsModeSet('Q'));
172                 
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)))
175                 {
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.
180                          */
181                         if ((!IS_LOCAL(user)) || ((ulevel > PEON) && (ulevel >= tlevel) && (tlevel != OWNER)))
182                         {
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
185
186                                 std::string reasonparam("No reason given");
187                                 
188                                 /* If a reason is given, use it */
189                                 if(pcnt > 2)
190                                 {
191                                         /* Join params 2 ... pcnt - 1 (inclusive) into one */
192                                         irc::stringjoiner reason_join(" ", parameters, 2, pcnt - 1);
193                                         reasonparam = reason_join.GetJoined();
194                                 }
195
196                                 /* Build up the part reason string. */
197                                 reason = std::string("Removed by ") + user->nick + ": " + reasonparam;
198
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());
201
202                                 if (!channel->PartUser(target, reason.c_str()))
203                                         delete channel;
204                         }
205                         else
206                         {
207                                 user->WriteServ( "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick, target->nick, channel->name);
208                                 return CMD_FAILURE;
209                         }
210                 }
211                 else
212                 {
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);
215                         return CMD_FAILURE;
216                 }
217
218                 return CMD_SUCCESS;
219         }
220 };
221
222 /** Handle /REMOVE
223  */
224 class cmd_remove : public command_t, public RemoveBase
225 {
226  public:
227         cmd_remove(InspIRCd* Instance, bool& snk) : command_t(Instance, "REMOVE", 0, 2), RemoveBase(Instance, snk)
228         {
229                 this->source = "m_remove.so";
230                 syntax = "<nick> <channel> [<reason>]";
231         }
232         
233         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
234         {
235                 return RemoveBase::Handle(parameters, pcnt, user, false);
236         }
237 };
238
239 /** Handle /FPART
240  */
241 class cmd_fpart : public command_t, public RemoveBase
242 {
243  public:
244         cmd_fpart(InspIRCd* Instance, bool& snk) : command_t(Instance, "FPART", 0, 2), RemoveBase(Instance, snk)
245         {
246                 this->source = "m_remove.so";
247                 syntax = "<channel> <nick> [<reason>]";
248         }
249
250         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
251         {
252                 return RemoveBase::Handle(parameters, pcnt, user, true);
253         }
254 };
255
256 class ModuleRemove : public Module
257 {
258         cmd_remove* mycommand;
259         cmd_fpart* mycommand2;
260         bool supportnokicks;
261         
262         
263  public:
264         ModuleRemove(InspIRCd* Me)
265         : Module::Module(Me)
266         {
267                 mycommand = new cmd_remove(ServerInstance, supportnokicks);
268                 mycommand2 = new cmd_fpart(ServerInstance, supportnokicks);
269                 ServerInstance->AddCommand(mycommand);
270                 ServerInstance->AddCommand(mycommand2);
271                 OnRehash("");
272         }
273
274         void Implements(char* List)
275         {
276                 List[I_On005Numeric] = List[I_OnRehash] = 1;
277         }
278
279         virtual void On005Numeric(std::string &output)
280         {
281                 output.append(" REMOVE");
282         }
283         
284         virtual void OnRehash(const std::string&)
285         {
286                 ConfigReader conf(ServerInstance);
287                 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
288         }
289         
290         virtual ~ModuleRemove()
291         {
292                 delete mycommand;
293                 delete mycommand2;
294         }
295         
296         virtual Version GetVersion()
297         {
298                 return Version(1,1,1,0,VF_VENDOR,API_VERSION);
299         }
300         
301 };
302
303 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
304
305 class ModuleRemoveFactory : public ModuleFactory
306 {
307  public:
308         ModuleRemoveFactory()
309         {
310         }
311         
312         ~ModuleRemoveFactory()
313         {
314         }
315         
316         virtual Module * CreateModule(InspIRCd* Me)
317         {
318                 return new ModuleRemove(Me);
319         }
320         
321 };
322
323
324 extern "C" void * init_module( void )
325 {
326         return new ModuleRemoveFactory;
327 }