]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
WHEEEEE!!!!!
[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                         user->WriteServ("401 %s %s :No such nick/channel", user->nick, !target ? username : channame);
96                         return;
97                 }
98
99                 if (!channel->HasUser(target))
100                 {
101                         user->WriteServ( "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                                 channel->WriteChannelWithServ(Srv->GetServerName().c_str(), "NOTICE %s :%s removed %s from the channel", channel->name, user->nick, target->nick);
186                                 target->WriteServ("NOTICE %s :*** %s removed you from %s with the message: %s", target->nick, user->nick, channel->name, reasonparam.c_str());
187
188                                 if (!channel->PartUser(target, reason.str().c_str()))
189                                         delete channel;
190                         }
191                         else
192                         {
193                                 user->WriteServ( "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick, target->nick, channel->name);
194                         }
195                 }
196                 else
197                 {
198                         /* m_nokicks.so was loaded and +Q was set, block! */
199                         user->WriteServ( "484 %s %s :Can't remove user %s from channel (+Q set)", user->nick, channel->name, target->nick);
200                 }
201         }
202 };
203
204 class cmd_remove : public command_t, public RemoveBase
205 {
206  public:
207         cmd_remove(Server* Srv, bool& snk) : command_t("REMOVE", 0, 2), RemoveBase(Srv, snk)
208         {
209                 this->source = "m_remove.so";
210                 syntax = "<nick> <channel> [<reason>]";
211         }
212         
213         void Handle (const char** parameters, int pcnt, userrec *user)
214         {
215                 RemoveBase::Handle(parameters, pcnt, user, false);
216         }
217 };
218
219 class cmd_fpart : public command_t, public RemoveBase
220 {
221  public:
222         cmd_fpart(Server* Srv, bool snk) : command_t("FPART", 0, 2), RemoveBase(Srv, snk)
223         {
224                 this->source = "m_remove.so";
225                 syntax = "<channel> <nick> [<reason>]";
226         }
227
228         void Handle (const char** parameters, int pcnt, userrec *user)
229         {
230                 RemoveBase::Handle(parameters, pcnt, user, true);
231         }       
232 };
233
234 class ModuleRemove : public Module
235 {
236         cmd_remove* mycommand;
237         cmd_fpart* mycommand2;
238         bool supportnokicks;
239         
240  public:
241         ModuleRemove(Server* Me)
242         : Module::Module(Me)
243         {
244                 mycommand = new cmd_remove(Me, supportnokicks);
245                 mycommand2 = new cmd_fpart(Me, supportnokicks);
246                 Me->AddCommand(mycommand);
247                 Me->AddCommand(mycommand2);
248                 OnRehash("");
249         }
250
251         void Implements(char* List)
252         {
253                 List[I_On005Numeric] = List[I_OnRehash] = 1;
254         }
255
256         virtual void On005Numeric(std::string &output)
257         {
258                 output.append(" REMOVE");
259         }
260         
261         virtual void OnRehash(const std::string&)
262         {
263                 ConfigReader conf;
264                 
265                 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
266         }
267         
268         virtual ~ModuleRemove()
269         {
270                 delete mycommand;
271                 delete mycommand2;
272         }
273         
274         virtual Version GetVersion()
275         {
276                 return Version(1,0,1,0,VF_VENDOR);
277         }
278         
279 };
280
281 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
282
283 class ModuleRemoveFactory : public ModuleFactory
284 {
285  public:
286         ModuleRemoveFactory()
287         {
288         }
289         
290         ~ModuleRemoveFactory()
291         {
292         }
293         
294         virtual Module * CreateModule(Server* Me)
295         {
296                 return new ModuleRemove(Me);
297         }
298         
299 };
300
301
302 extern "C" void * init_module( void )
303 {
304         return new ModuleRemoveFactory;
305 }