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