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