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