]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
cmode(), cflags(), cstatus() -> chanrec::GetStatusChar(), chanrec::GetStatusFlags...
[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 "inspircd.h"
11
12 /* $ModDesc: Provides a /remove command, this is mostly an alternative to /kick, except makes users appear to have parted the channel */
13
14 /*      
15  * This module supports the use of the +q and +a usermodes, but should work without them too.
16  * Usage of the command is restricted to +hoaq, and you cannot remove a user with a "higher" level than yourself.
17  * eg: +h can remove +hv and users with no modes. +a can remove +aohv and users with no modes.
18 */
19
20 extern InspIRCd* ServerInstance;
21
22 class RemoveBase
23 {
24  private: 
25         Server* Srv;
26         bool& supportnokicks;
27  
28  protected:
29         RemoveBase(Server* Me, bool& snk)
30         : Srv(Me), 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 (Srv->IsUlined(user->server) || Srv->IsUlined(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 (Srv->IsUlined(target->server) || Srv->IsUlined(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 = (Srv->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(Srv->GetServerName().c_str(), "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(Server* Srv, bool& snk) : command_t("REMOVE", 0, 2), RemoveBase(Srv, 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(Server* Srv, bool snk) : command_t("FPART", 0, 2), RemoveBase(Srv, 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         
243  public:
244         ModuleRemove(Server* Me)
245         : Module::Module(Me)
246         {
247                 mycommand = new cmd_remove(Me, supportnokicks);
248                 mycommand2 = new cmd_fpart(Me, supportnokicks);
249                 Me->AddCommand(mycommand);
250                 Me->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;
267                 
268                 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
269         }
270         
271         virtual ~ModuleRemove()
272         {
273                 delete mycommand;
274                 delete mycommand2;
275         }
276         
277         virtual Version GetVersion()
278         {
279                 return Version(1,0,1,0,VF_VENDOR);
280         }
281         
282 };
283
284 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
285
286 class ModuleRemoveFactory : public ModuleFactory
287 {
288  public:
289         ModuleRemoveFactory()
290         {
291         }
292         
293         ~ModuleRemoveFactory()
294         {
295         }
296         
297         virtual Module * CreateModule(Server* Me)
298         {
299                 return new ModuleRemove(Me);
300         }
301         
302 };
303
304
305 extern "C" void * init_module( void )
306 {
307         return new ModuleRemoveFactory;
308 }