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