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