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