]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
Atheme wont work right, because this wasnt VF_COMMON. If its not VF_COMMON, its not...
[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                                          reason <<  ":";
188                                         
189                                         /* Use all the remaining parameters as the reason */
190                                         for(int i = 2; i < pcnt; i++)
191                                         {
192                                                 reason << " " << parameters[i];
193                                         }
194                                         
195                                         reasonparam = reason.str();
196                                         reason.clear();
197                                 }
198
199                                 /* Build up the part reason string. */
200                                 reason << "Removed by " << user->nick << reasonparam;
201
202                                 channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s removed %s from the channel", channel->name, user->nick, target->nick);
203                                 target->WriteServ("NOTICE %s :*** %s removed you from %s with the message: %s", target->nick, user->nick, channel->name, reasonparam.c_str());
204
205                                 if (!channel->PartUser(target, reason.str().c_str()))
206                                         delete channel;
207                         }
208                         else
209                         {
210                                 user->WriteServ( "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick, target->nick, channel->name);
211                                 return CMD_FAILURE;
212                         }
213                 }
214                 else
215                 {
216                         /* m_nokicks.so was loaded and +Q was set, block! */
217                         user->WriteServ( "484 %s %s :Can't remove user %s from channel (+Q set)", user->nick, channel->name, target->nick);
218                         return CMD_FAILURE;
219                 }
220
221                 return CMD_SUCCESS;
222         }
223 };
224
225 /** Handle /REMOVE
226  */
227 class cmd_remove : public command_t, public RemoveBase
228 {
229  public:
230         cmd_remove(InspIRCd* Instance, bool& snk) : command_t(Instance, "REMOVE", 0, 2), RemoveBase(Instance, snk)
231         {
232                 this->source = "m_remove.so";
233                 syntax = "<nick> <channel> [<reason>]";
234         }
235         
236         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
237         {
238                 return RemoveBase::Handle(parameters, pcnt, user, false);
239         }
240 };
241
242 /** Handle /FPART
243  */
244 class cmd_fpart : public command_t, public RemoveBase
245 {
246  public:
247         cmd_fpart(InspIRCd* Instance, bool& snk) : command_t(Instance, "FPART", 0, 2), RemoveBase(Instance, snk)
248         {
249                 this->source = "m_remove.so";
250                 syntax = "<channel> <nick> [<reason>]";
251         }
252
253         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
254         {
255                 return RemoveBase::Handle(parameters, pcnt, user, true);
256         }
257 };
258
259 class ModuleRemove : public Module
260 {
261         cmd_remove* mycommand;
262         cmd_fpart* mycommand2;
263         bool supportnokicks;
264         
265         
266  public:
267         ModuleRemove(InspIRCd* Me)
268         : Module::Module(Me)
269         {
270                 mycommand = new cmd_remove(ServerInstance, supportnokicks);
271                 mycommand2 = new cmd_fpart(ServerInstance, supportnokicks);
272                 ServerInstance->AddCommand(mycommand);
273                 ServerInstance->AddCommand(mycommand2);
274                 OnRehash("");
275         }
276
277         void Implements(char* List)
278         {
279                 List[I_On005Numeric] = List[I_OnRehash] = 1;
280         }
281
282         virtual void On005Numeric(std::string &output)
283         {
284                 output.append(" REMOVE");
285         }
286         
287         virtual void OnRehash(const std::string&)
288         {
289                 ConfigReader conf(ServerInstance);
290                 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
291         }
292         
293         virtual ~ModuleRemove()
294         {
295                 delete mycommand;
296                 delete mycommand2;
297         }
298         
299         virtual Version GetVersion()
300         {
301                 return Version(1,0,1,0,VF_VENDOR);
302         }
303         
304 };
305
306 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
307
308 class ModuleRemoveFactory : public ModuleFactory
309 {
310  public:
311         ModuleRemoveFactory()
312         {
313         }
314         
315         ~ModuleRemoveFactory()
316         {
317         }
318         
319         virtual Module * CreateModule(InspIRCd* Me)
320         {
321                 return new ModuleRemove(Me);
322         }
323         
324 };
325
326
327 extern "C" void * init_module( void )
328 {
329         return new ModuleRemoveFactory;
330 }