]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
e7aa9f7a978b88b44e5317ca303a0daa0bef31a6
[user/henk/code/inspircd.git] / src / modules / m_remove.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include <sstream>
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18 #include "configreader.h"
19 #include "inspircd.h"
20
21 /* $ModDesc: Provides a /remove command, this is mostly an alternative to /kick, except makes users appear to have parted the channel */
22
23 /*      
24  * This module supports the use of the +q and +a usermodes, but should work without them too.
25  * Usage of the command is restricted to +hoaq, and you cannot remove a user with a "higher" level than yourself.
26  * eg: +h can remove +hv and users with no modes. +a can remove +aohv and users with no modes.
27 */
28
29 /** Base class for /FPART and /REMOVE
30  */
31 class RemoveBase
32 {
33  private: 
34         bool& supportnokicks;
35         InspIRCd* ServerInstance;
36  
37  protected:
38         RemoveBase(InspIRCd* Instance, bool& snk) : supportnokicks(snk), ServerInstance(Instance)
39         {
40         }               
41  
42         enum ModeLevel { PEON = 0, HALFOP = 1, OP = 2, ADMIN = 3, OWNER = 4, ULINE = 5 };        
43  
44         /* This little function just converts a chanmode character (U ~ & @ & +) into an integer (5 4 3 2 1 0) */
45         /* XXX - We should probably use the new mode prefix rank stuff
46          * for this instead now -- Brain */
47         ModeLevel chartolevel(const std::string &privs)
48         {
49                 if(privs.empty())
50                 {
51                         return PEON;
52                 }
53         
54                 switch (privs[0])
55                 {
56                         case 'U':
57                                 /* Ulined */
58                                 return ULINE;
59                         case '~':
60                                 /* Owner */
61                                 return OWNER;
62                         case '&':
63                                 /* Admin */
64                                 return ADMIN;
65                         case '@':
66                                 /* Operator */
67                                 return OP;
68                         case '%':
69                                 /* Halfop */
70                                 return HALFOP;
71                         default:
72                                 /* Peon */
73                                 return PEON;
74                 }
75         }
76         
77         CmdResult Handle (const char** parameters, int pcnt, userrec *user, bool neworder)
78         {
79                 const char* channame;
80                 const char* username;
81                 userrec* target;
82                 chanrec* channel;
83                 ModeLevel tlevel;
84                 ModeLevel ulevel;
85                 std::string reason;
86                 std::string protectkey;
87                 std::string founderkey;
88                 bool hasnokicks;
89                 
90                 /* Set these to the parameters needed, the new version of this module switches it's parameters around
91                  * supplying a new command with the new order while keeping the old /remove with the older order.
92                  * /remove <nick> <channel> [reason ...]
93                  * /fpart <channel> <nick> [reason ...]
94                  */
95                 channame = parameters[ neworder ? 0 : 1];
96                 username = parameters[ neworder ? 1 : 0];
97                 
98                 /* Look up the user we're meant to be removing from the channel */
99                 target = ServerInstance->FindNick(username);
100                 
101                 /* And the channel we're meant to be removing them from */
102                 channel = ServerInstance->FindChan(channame);
103
104                 /* Fix by brain - someone needs to learn to validate their input! */
105                 if (!target || !channel)
106                 {
107                         user->WriteServ("401 %s %s :No such nick/channel", user->nick, !target ? username : channame);
108                         return CMD_FAILURE;
109                 }
110
111                 if (!channel->HasUser(target))
112                 {
113                         user->WriteServ( "NOTICE %s :*** The user %s is not on channel %s", user->nick, target->nick, channel->name);
114                         return CMD_FAILURE;
115                 }       
116                 
117                 /* This is adding support for the +q and +a channel modes, basically if they are enabled, and the remover has them set.
118                  * Then we change the @|%|+ to & if they are +a, or ~ if they are +q */
119                 protectkey = "cm_protect_" + std::string(channel->name);
120                 founderkey = "cm_founder_" + std::string(channel->name);
121                 
122                 if (ServerInstance->ULine(user->server) || ServerInstance->ULine(user->nick))
123                 {
124                         ServerInstance->Log(DEBUG, "Setting ulevel to U");
125                         ulevel = chartolevel("U");
126                 }
127                 if (user->GetExt(founderkey))
128                 {
129                         ServerInstance->Log(DEBUG, "Setting ulevel to ~");
130                         ulevel = chartolevel("~");
131                 }
132                 else if (user->GetExt(protectkey))
133                 {
134                         ServerInstance->Log(DEBUG, "Setting ulevel to &");
135                         ulevel = chartolevel("&");
136                 }
137                 else
138                 {
139                         ServerInstance->Log(DEBUG, "Setting ulevel to %s", channel->GetPrefixChar(user));
140                         ulevel = chartolevel(channel->GetPrefixChar(user));
141                 }
142                         
143                 /* 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 */
144                 if (ServerInstance->ULine(target->server) || ServerInstance->ULine(target->nick))
145                 {
146                         ServerInstance->Log(DEBUG, "Setting tlevel to U");
147                         tlevel = chartolevel("U");
148                 }
149                 else if (target->GetExt(founderkey))
150                 {
151                         ServerInstance->Log(DEBUG, "Setting tlevel to ~");
152                         tlevel = chartolevel("~");
153                 }
154                 else if (target->GetExt(protectkey))
155                 {
156                         ServerInstance->Log(DEBUG, "Setting tlevel to &");
157                         tlevel = chartolevel("&");
158                 }
159                 else
160                 {
161                         ServerInstance->Log(DEBUG, "Setting tlevel to %s", channel->GetPrefixChar(target));
162                         tlevel = chartolevel(channel->GetPrefixChar(target));
163                 }
164                 
165                 hasnokicks = (ServerInstance->FindModule("m_nokicks.so") && channel->IsModeSet('Q'));
166                 
167                 /* 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 */
168                 if ((!IS_LOCAL(user)) || (!supportnokicks || !hasnokicks || (ulevel == ULINE)))
169                 {
170                         /* We'll let everyone remove their level and below, eg:
171                          * ops can remove ops, halfops, voices, and those with no mode (no moders actually are set to 1)
172                          * a ulined target will get a higher level than it's possible for a /remover to get..so they're safe.
173                          * Nobody may remove a founder.
174                          */
175                         if ((!IS_LOCAL(user)) || ((ulevel > PEON) && (ulevel >= tlevel) && (tlevel != OWNER)))
176                         {
177                                 // no you can't just go from a std::ostringstream to a std::string, Om. -nenolod
178                                 // but you can do this, nenolod -brain
179
180                                 std::string reasonparam("No reason given");
181                                 
182                                 /* If a reason is given, use it */
183                                 if(pcnt > 2)
184                                 {
185                                         /* Join params 2 ... pcnt - 1 (inclusive) into one */
186                                         irc::stringjoiner reason_join(" ", parameters, 2, pcnt - 1);
187                                         reasonparam = reason_join.GetJoined();
188                                 }
189
190                                 /* Build up the part reason string. */
191                                 reason = std::string("Removed by ") + user->nick + ": " + reasonparam;
192
193                                 channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s removed %s from the channel", channel->name, user->nick, target->nick);
194                                 target->WriteServ("NOTICE %s :*** %s removed you from %s with the message: %s", target->nick, user->nick, channel->name, reasonparam.c_str());
195
196                                 if (!channel->PartUser(target, reason.c_str()))
197                                         delete channel;
198                         }
199                         else
200                         {
201                                 user->WriteServ( "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick, target->nick, channel->name);
202                                 return CMD_FAILURE;
203                         }
204                 }
205                 else
206                 {
207                         /* m_nokicks.so was loaded and +Q was set, block! */
208                         user->WriteServ( "484 %s %s :Can't remove user %s from channel (+Q set)", user->nick, channel->name, target->nick);
209                         return CMD_FAILURE;
210                 }
211
212                 return CMD_SUCCESS;
213         }
214 };
215
216 /** Handle /REMOVE
217  */
218 class cmd_remove : public command_t, public RemoveBase
219 {
220  public:
221         cmd_remove(InspIRCd* Instance, bool& snk) : command_t(Instance, "REMOVE", 0, 2), RemoveBase(Instance, snk)
222         {
223                 this->source = "m_remove.so";
224                 syntax = "<nick> <channel> [<reason>]";
225         }
226         
227         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
228         {
229                 return RemoveBase::Handle(parameters, pcnt, user, false);
230         }
231 };
232
233 /** Handle /FPART
234  */
235 class cmd_fpart : public command_t, public RemoveBase
236 {
237  public:
238         cmd_fpart(InspIRCd* Instance, bool& snk) : command_t(Instance, "FPART", 0, 2), RemoveBase(Instance, snk)
239         {
240                 this->source = "m_remove.so";
241                 syntax = "<channel> <nick> [<reason>]";
242         }
243
244         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
245         {
246                 return RemoveBase::Handle(parameters, pcnt, user, true);
247         }
248 };
249
250 class ModuleRemove : public Module
251 {
252         cmd_remove* mycommand;
253         cmd_fpart* mycommand2;
254         bool supportnokicks;
255         
256         
257  public:
258         ModuleRemove(InspIRCd* Me)
259         : Module::Module(Me)
260         {
261                 mycommand = new cmd_remove(ServerInstance, supportnokicks);
262                 mycommand2 = new cmd_fpart(ServerInstance, supportnokicks);
263                 ServerInstance->AddCommand(mycommand);
264                 ServerInstance->AddCommand(mycommand2);
265                 OnRehash("");
266         }
267
268         void Implements(char* List)
269         {
270                 List[I_On005Numeric] = List[I_OnRehash] = 1;
271         }
272
273         virtual void On005Numeric(std::string &output)
274         {
275                 output.append(" REMOVE");
276         }
277         
278         virtual void OnRehash(const std::string&)
279         {
280                 ConfigReader conf(ServerInstance);
281                 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
282         }
283         
284         virtual ~ModuleRemove()
285         {
286                 delete mycommand;
287                 delete mycommand2;
288         }
289         
290         virtual Version GetVersion()
291         {
292                 return Version(1,1,1,0,VF_VENDOR,API_VERSION);
293         }
294         
295 };
296
297 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
298
299 class ModuleRemoveFactory : public ModuleFactory
300 {
301  public:
302         ModuleRemoveFactory()
303         {
304         }
305         
306         ~ModuleRemoveFactory()
307         {
308         }
309         
310         virtual Module * CreateModule(InspIRCd* Me)
311         {
312                 return new ModuleRemove(Me);
313         }
314         
315 };
316
317
318 extern "C" void * init_module( void )
319 {
320         return new ModuleRemoveFactory;
321 }