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