]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
Change to using userrec::ip as a sockaddr to store port, ip and address family, rathe...
[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 using namespace std;
5
6 #include <stdio.h>
7 #include <string>
8 #include "users.h"
9 #include "channels.h"
10 #include "modules.h"
11 #include "helperfuncs.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 static Server *Srv;
22
23 /* This little function just converts a chanmode character (~ & @ & +) into an integer (5 4 3 2 1) */
24 /* XXX - this could be handy in the core, so it can be used elsewhere */
25 int chartolevel(std::string &privs)
26 {
27         const char* n = privs.c_str();
28
29         switch (*n)
30         {
31                 case '~':
32                         return 5;
33                 break;
34                 case '&':
35                         return 4;
36                 break;
37                 case '@':
38                         return 3;
39                 break;
40                 case '%':
41                         return 2;
42                 break;
43                 default:
44                         return 1;
45                 break;
46         }
47         return 1;
48 }
49
50 class cmd_remove : public command_t
51 {
52  public:
53         cmd_remove () : command_t("REMOVE", 0, 2)
54         {
55                 this->source = "m_remove.so";
56                 syntax = "<nick> <channel> [<reason>]";
57         }
58
59         void Handle (const char** parameters, int pcnt, userrec *user)
60         {
61                 userrec* target;
62                 chanrec* channel;
63                 int tlevel, ulevel;
64                 char* dummy;
65                 std::string tprivs, uprivs, reason;
66                 
67                 
68                 /* Look up the user we're meant to be removing from the channel */
69                 target = Srv->FindNick(parameters[0]);
70                 
71                 /* And the channel we're meant to be removing them from */
72                 channel = Srv->FindChannel(parameters[1]);
73
74                 /* Fix by brain - someone needs to learn to validate their input! */
75                 if (!target || !channel)
76                 {
77                         WriteServ(user->fd,"401 %s %s :No such nick/channel",user->nick, !target ? parameters[0] : parameters[1]);
78                         return;
79                 }
80
81                 if (!channel->HasUser(target))
82                 {
83                         Srv->SendTo(NULL,user,"NOTICE "+std::string(user->nick)+" :*** The user "+target->nick+" is not on channel "+channel->name);
84                         return;
85                 }       
86
87                 /* And see if the person calling the command has access to use it on the channel */
88                 uprivs = Srv->ChanMode(user, channel);
89                 
90                 /* Check what privs the person being removed has */
91                 tprivs = Srv->ChanMode(target, channel);
92
93                 if(pcnt > 2)
94                         reason = "Removed by " + std::string(user->nick) + ":";
95                 else
96                         reason = "Removed by " + std::string(user->nick);
97                 
98                 /* This turns all the parameters after the first two into a single string, so the part reason can be multi-word */
99                 for (int n = 2; n < pcnt; n++)
100                 {
101                         reason += " ";
102                         reason += parameters[n];
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
108                 std::string protect = "cm_protect_" + std::string(channel->name);
109                 std::string founder = "cm_founder_"+std::string(channel->name);
110                 
111                 if (user->GetExt(protect, dummy))
112                         uprivs = "&";
113                 if (user->GetExt(founder, dummy))
114                         uprivs = "~";
115                         
116                 /* Now it's the same idea, except for the target */
117                 if (target->GetExt(protect, dummy))
118                         tprivs = "&";
119                 if (target->GetExt(founder, dummy))
120                         tprivs = "~";
121                         
122                 tlevel = chartolevel(tprivs);
123                 ulevel = chartolevel(uprivs);
124                 
125                 /* If the user calling the command is either an admin, owner, operator or a half-operator on the channel */
126                 if (ulevel > 1)
127                 {
128                         /* For now, we'll let everyone remove their level and below, eg ops can remove ops, halfops, voices, and those with no mode (no moders actually are set to 1) */
129                         if ((ulevel >= tlevel && tlevel != 5) && (!Srv->IsUlined(target->server)))
130                         {
131                                 Srv->PartUserFromChannel(target, channel->name, reason);
132                                 WriteServ(user->fd, "NOTICE %s :%s removed %s from the channel", channel->name, user->nick, target->nick);
133                                 WriteServ(target->fd, "NOTICE %s :*** %s removed you from %s with the message:%s", target->nick, user->nick, channel->name, reason.c_str());
134                         }
135                         else
136                         {
137                                 WriteServ(user->fd, "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick, target->nick, channel->name);
138                         }
139                 }
140                 else
141                 {
142                         WriteServ(user->fd, "NOTICE %s :*** You do not have access to use /remove on %s", user->nick, channel->name);
143                 }
144         }
145 };
146
147 class ModuleRemove : public Module
148 {
149         cmd_remove* mycommand;
150  public:
151         ModuleRemove(Server* Me)
152                 : Module::Module(Me)
153         {
154                 Srv = Me;
155                 mycommand = new cmd_remove();
156                 Srv->AddCommand(mycommand);
157         }
158
159         void Implements(char* List)
160         {
161                 List[I_On005Numeric] = 1;
162         }
163
164         virtual void On005Numeric(std::string &output)
165         {
166                 output = output + std::string(" REMOVE");
167         }
168         
169         virtual ~ModuleRemove()
170         {
171         }
172         
173         virtual Version GetVersion()
174         {
175                 return Version(1,0,0,1,VF_VENDOR);
176         }
177         
178 };
179
180 // stuff down here is the module-factory stuff. For basic modules you can ignore this.
181
182 class ModuleRemoveFactory : public ModuleFactory
183 {
184  public:
185         ModuleRemoveFactory()
186         {
187         }
188         
189         ~ModuleRemoveFactory()
190         {
191         }
192         
193         virtual Module * CreateModule(Server* Me)
194         {
195                 return new ModuleRemove(Me);
196         }
197         
198 };
199
200
201 extern "C" void * init_module( void )
202 {
203         return new ModuleRemoveFactory;
204 }