]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
2b237c08e3a1a4d13c67254ed951820f18759b6a
[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 - this could be handy in the core, so it can be used elsewhere */
52         ModeLevel chartolevel(const std::string &privs)
53         {
54                 if(privs.empty())
55                 {
56                         return PEON;
57                 }
58         
59                 switch (privs[0])
60                 {
61                         case 'U':
62                                 /* Ulined */
63                                 return ULINE;
64                         case '~':
65                                 /* Owner */
66                                 return OWNER;
67                         case '&':
68                                 /* Admin */
69                                 return ADMIN;
70                         case '@':
71                                 /* Operator */
72                                 return OP;
73                         case '%':
74                                 /* Halfop */
75                                 return HALFOP;
76                         default:
77                                 /* Peon */
78                                 return PEON;
79                 }
80         }
81         
82         CmdResult Handle (const char** parameters, int pcnt, userrec *user, bool neworder)
83         {
84                 const char* channame;
85                 const char* username;
86                 userrec* target;
87                 chanrec* channel;
88                 ModeLevel tlevel;
89                 ModeLevel ulevel;
90                 std::ostringstream reason;
91                 std::string protectkey;
92                 std::string founderkey;
93                 bool hasnokicks;
94                 
95                 /* Set these to the parameters needed, the new version of this module switches it's parameters around
96                  * supplying a new command with the new order while keeping the old /remove with the older order.
97                  * /remove <nick> <channel> [reason ...]
98                  * /fpart <channel> <nick> [reason ...]
99                  */
100                 channame = parameters[ neworder ? 0 : 1];
101                 username = parameters[ neworder ? 1 : 0];
102                 
103                 /* Look up the user we're meant to be removing from the channel */
104                 target = ServerInstance->FindNick(username);
105                 
106                 /* And the channel we're meant to be removing them from */
107                 channel = ServerInstance->FindChan(channame);
108
109                 /* Fix by brain - someone needs to learn to validate their input! */
110                 if (!target || !channel)
111                 {
112                         user->WriteServ("401 %s %s :No such nick/channel", user->nick, !target ? username : channame);
113                         return CMD_FAILURE;
114                 }
115
116                 if (!channel->HasUser(target))
117                 {
118                         user->WriteServ( "NOTICE %s :*** The user %s is not on channel %s", user->nick, target->nick, channel->name);
119                         return CMD_FAILURE;
120                 }       
121                 
122                 /* This is adding support for the +q and +a channel modes, basically if they are enabled, and the remover has them set.
123                  * Then we change the @|%|+ to & if they are +a, or ~ if they are +q */
124                 protectkey = "cm_protect_" + std::string(channel->name);
125                 founderkey = "cm_founder_" + std::string(channel->name);
126                 
127                 if (ServerInstance->ULine(user->server) || ServerInstance->ULine(user->nick))
128                 {
129                         ServerInstance->Log(DEBUG, "Setting ulevel to U");
130                         ulevel = chartolevel("U");
131                 }
132                 if (user->GetExt(founderkey))
133                 {
134                         ServerInstance->Log(DEBUG, "Setting ulevel to ~");
135                         ulevel = chartolevel("~");
136                 }
137                 else if (user->GetExt(protectkey))
138                 {
139                         ServerInstance->Log(DEBUG, "Setting ulevel to &");
140                         ulevel = chartolevel("&");
141                 }
142                 else
143                 {
144                         ServerInstance->Log(DEBUG, "Setting ulevel to %s", channel->GetPrefixChar(user));
145                         ulevel = chartolevel(channel->GetPrefixChar(user));
146                 }
147                         
148                 /* 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 */
149                 if (ServerInstance->ULine(target->server) || ServerInstance->ULine(target->nick))
150                 {
151                         ServerInstance->Log(DEBUG, "Setting tlevel to U");
152                         tlevel = chartolevel("U");
153                 }
154                 else if (target->GetExt(founderkey))
155                 {
156                         ServerInstance->Log(DEBUG, "Setting tlevel to ~");
157                         tlevel = chartolevel("~");
158                 }
159                 else if (target->GetExt(protectkey))
160                 {
161                         ServerInstance->Log(DEBUG, "Setting tlevel to &");
162                         tlevel = chartolevel("&");
163                 }
164                 else
165                 {
166                         ServerInstance->Log(DEBUG, "Setting tlevel to %s", channel->GetPrefixChar(target));
167                         tlevel = chartolevel(channel->GetPrefixChar(target));
168                 }
169                 
170                 hasnokicks = (ServerInstance->FindModule("m_nokicks.so") && channel->IsModeSet('Q'));
171                 
172                 /* 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 */
173                 if(!supportnokicks || !hasnokicks || (ulevel == ULINE))
174                 {
175                         /* We'll let everyone remove their level and below, eg:
176                          * ops can remove ops, halfops, voices, and those with no mode (no moders actually are set to 1)
177                          * a ulined target will get a higher level than it's possible for a /remover to get..so they're safe.
178                          * Nobody may remove a founder.
179                          */
180                         if ((ulevel > PEON) && (ulevel >= tlevel) && (tlevel != OWNER))
181                         {
182                                 std::string reasonparam;
183                                 
184                                 /* If a reason is given, use it */
185                                 if(pcnt > 2)
186                                 {
187                                         /* Use all the remaining parameters as the reason */
188                                         for(int i = 2; i < pcnt; i++)
189                                         {
190                                                 reason << " " << parameters[i];
191                                         }
192                                         
193                                         reasonparam = reason.str();
194                                         reason.clear();
195                                 }
196
197                                 /* Build up the part reason string. */
198                                 reason << "Removed by " << user->nick << reasonparam;
199
200                                 channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s removed %s from the channel", channel->name, user->nick, target->nick);
201                                 target->WriteServ("NOTICE %s :*** %s removed you from %s with the message: %s", target->nick, user->nick, channel->name, reasonparam.c_str());
202
203                                 if (!channel->PartUser(target, reason.str().c_str()))
204                                         delete channel;
205                         }
206                         else
207                         {
208                                 user->WriteServ( "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick, target->nick, channel->name);
209                                 return CMD_FAILURE;
210                         }
211                 }
212                 else
213                 {
214                         /* m_nokicks.so was loaded and +Q was set, block! */
215                         user->WriteServ( "484 %s %s :Can't remove user %s from channel (+Q set)", user->nick, channel->name, target->nick);
216                         return CMD_FAILURE;
217                 }
218
219                 return CMD_SUCCESS;
220         }
221 };
222
223 /** Handle /REMOVE
224  */
225 class cmd_remove : public command_t, public RemoveBase
226 {
227  public:
228         cmd_remove(InspIRCd* Instance, bool& snk) : command_t(Instance, "REMOVE", 0, 2), RemoveBase(Instance, snk)
229         {
230                 this->source = "m_remove.so";
231                 syntax = "<nick> <channel> [<reason>]";
232         }
233         
234         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
235         {
236                 return RemoveBase::Handle(parameters, pcnt, user, false);
237         }
238 };
239
240 /** Handle /FPART
241  */
242 class cmd_fpart : public command_t, public RemoveBase
243 {
244  public:
245         cmd_fpart(InspIRCd* Instance, bool& snk) : command_t(Instance, "FPART", 0, 2), RemoveBase(Instance, snk)
246         {
247                 this->source = "m_remove.so";
248                 syntax = "<channel> <nick> [<reason>]";
249         }
250
251         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
252         {
253                 return RemoveBase::Handle(parameters, pcnt, user, true);
254         }
255 };
256
257 class ModuleRemove : public Module
258 {
259         cmd_remove* mycommand;
260         cmd_fpart* mycommand2;
261         bool supportnokicks;
262         
263         
264  public:
265         ModuleRemove(InspIRCd* Me)
266         : Module::Module(Me)
267         {
268                 mycommand = new cmd_remove(ServerInstance, supportnokicks);
269                 mycommand2 = new cmd_fpart(ServerInstance, supportnokicks);
270                 ServerInstance->AddCommand(mycommand);
271                 ServerInstance->AddCommand(mycommand2);
272                 OnRehash("");
273         }
274
275         void Implements(char* List)
276         {
277                 List[I_On005Numeric] = List[I_OnRehash] = 1;
278         }
279
280         virtual void On005Numeric(std::string &output)
281         {
282                 output.append(" REMOVE");
283         }
284         
285         virtual void OnRehash(const std::string&)
286         {
287                 ConfigReader conf(ServerInstance);
288                 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
289         }
290         
291         virtual ~ModuleRemove()
292         {
293                 delete mycommand;
294                 delete mycommand2;
295         }
296         
297         virtual Version GetVersion()
298         {
299                 return Version(1,0,1,0,VF_VENDOR,API_VERSION);
300         }
301         
302 };
303
304 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
305
306 class ModuleRemoveFactory : public ModuleFactory
307 {
308  public:
309         ModuleRemoveFactory()
310         {
311         }
312         
313         ~ModuleRemoveFactory()
314         {
315         }
316         
317         virtual Module * CreateModule(InspIRCd* Me)
318         {
319                 return new ModuleRemove(Me);
320         }
321         
322 };
323
324
325 extern "C" void * init_module( void )
326 {
327         return new ModuleRemoveFactory;
328 }