]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
Lots, lots more numerics.h conversion
[user/henk/code/inspircd.git] / src / modules / m_remove.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15
16 /* $ModDesc: Provides a /remove command, this is mostly an alternative to /kick, except makes users appear to have parted the channel */
17
18 /*
19  * This module supports the use of the +q and +a usermodes, but should work without them too.
20  * Usage of the command is restricted to +hoaq, and you cannot remove a user with a "higher" level than yourself.
21  * eg: +h can remove +hv and users with no modes. +a can remove +aohv and users with no modes.
22 */
23
24 /** Base class for /FPART and /REMOVE
25  */
26 class RemoveBase
27 {
28  private:
29         bool& supportnokicks;
30         InspIRCd* ServerInstance;
31
32  protected:
33         RemoveBase(InspIRCd* Instance, bool& snk) : supportnokicks(snk), ServerInstance(Instance)
34         {
35         }
36
37         enum ModeLevel { PEON = 0, HALFOP = 1, OP = 2, ADMIN = 3, OWNER = 4, ULINE = 5 };
38
39         /* This little function just converts a chanmode character (U ~ & @ & +) into an integer (5 4 3 2 1 0) */
40         /* XXX - We should probably use the new mode prefix rank stuff
41          * for this instead now -- Brain */
42         ModeLevel chartolevel(const std::string &privs)
43         {
44                 if(privs.empty())
45                 {
46                         return PEON;
47                 }
48
49                 switch (privs[0])
50                 {
51                         case 'U':
52                                 /* Ulined */
53                                 return ULINE;
54                         case '~':
55                                 /* Owner */
56                                 return OWNER;
57                         case '&':
58                                 /* Admin */
59                                 return ADMIN;
60                         case '@':
61                                 /* Operator */
62                                 return OP;
63                         case '%':
64                                 /* Halfop */
65                                 return HALFOP;
66                         default:
67                                 /* Peon */
68                                 return PEON;
69                 }
70         }
71
72         CmdResult Handle (const std::vector<std::string>& parameters, User *user, bool neworder)
73         {
74                 const char* channame;
75                 const char* username;
76                 User* target;
77                 Channel* channel;
78                 ModeLevel tlevel;
79                 ModeLevel ulevel;
80                 std::string reason;
81                 std::string protectkey;
82                 std::string founderkey;
83                 bool hasnokicks;
84
85                 /* Set these to the parameters needed, the new version of this module switches it's parameters around
86                  * supplying a new command with the new order while keeping the old /remove with the older order.
87                  * /remove <nick> <channel> [reason ...]
88                  * /fpart <channel> <nick> [reason ...]
89                  */
90                 channame = parameters[ neworder ? 0 : 1].c_str();
91                 username = parameters[ neworder ? 1 : 0].c_str();
92
93                 /* Look up the user we're meant to be removing from the channel */
94                 target = ServerInstance->FindNick(username);
95
96                 /* And the channel we're meant to be removing them from */
97                 channel = ServerInstance->FindChan(channame);
98
99                 /* Fix by brain - someone needs to learn to validate their input! */
100                 if (!target || !channel)
101                 {
102                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel", user->nick.c_str(), !target ? username : channame);
103                         return CMD_FAILURE;
104                 }
105
106                 if (!channel->HasUser(target))
107                 {
108                         user->WriteServ( "NOTICE %s :*** The user %s is not on channel %s", user->nick.c_str(), target->nick.c_str(), channel->name.c_str());
109                         return CMD_FAILURE;
110                 }
111
112                 /* This is adding support for the +q and +a channel modes, basically if they are enabled, and the remover has them set.
113                  * Then we change the @|%|+ to & if they are +a, or ~ if they are +q */
114                 protectkey = "cm_protect_" + std::string(channel->name);
115                 founderkey = "cm_founder_" + std::string(channel->name);
116
117                 if (ServerInstance->ULine(user->server) || ServerInstance->ULine(user->nick.c_str()))
118                 {
119                         ulevel = chartolevel("U");
120                 }
121                 if (user->GetExt(founderkey))
122                 {
123                         ulevel = chartolevel("~");
124                 }
125                 else if (user->GetExt(protectkey))
126                 {
127                         ulevel = chartolevel("&");
128                 }
129                 else
130                 {
131                         ulevel = chartolevel(channel->GetPrefixChar(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 (ServerInstance->ULine(target->server) || ServerInstance->ULine(target->nick.c_str()))
136                 {
137                         tlevel = chartolevel("U");
138                 }
139                 else if (target->GetExt(founderkey))
140                 {
141                         tlevel = chartolevel("~");
142                 }
143                 else if (target->GetExt(protectkey))
144                 {
145                         tlevel = chartolevel("&");
146                 }
147                 else
148                 {
149                         tlevel = chartolevel(channel->GetPrefixChar(target));
150                 }
151
152                 hasnokicks = (ServerInstance->Modules->Find("m_nokicks.so") && channel->IsModeSet('Q'));
153
154                 /* 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 */
155                 if ((!IS_LOCAL(user)) || (!supportnokicks || !hasnokicks || (ulevel == ULINE)))
156                 {
157                         /* We'll let everyone remove their level and below, eg:
158                          * ops can remove ops, halfops, voices, and those with no mode (no moders actually are set to 1)
159                          * a ulined target will get a higher level than it's possible for a /remover to get..so they're safe.
160                          * Nobody may remove a founder.
161                          */
162                         if ((!IS_LOCAL(user)) || ((ulevel > PEON) && (ulevel >= tlevel) && (tlevel != OWNER)))
163                         {
164                                 // no you can't just go from a std::ostringstream to a std::string, Om. -nenolod
165                                 // but you can do this, nenolod -brain
166
167                                 std::string reasonparam("No reason given");
168
169                                 /* If a reason is given, use it */
170                                 if(parameters.size() > 2)
171                                 {
172                                         /* Join params 2 ... pcnt - 1 (inclusive) into one */
173                                         irc::stringjoiner reason_join(" ", parameters, 2, parameters.size() - 1);
174                                         reasonparam = reason_join.GetJoined();
175                                 }
176
177                                 /* Build up the part reason string. */
178                                 reason = std::string("Removed by ") + user->nick + ": " + reasonparam;
179
180                                 channel->WriteChannelWithServ(ServerInstance->Config->ServerName, "NOTICE %s :%s removed %s from the channel", channel->name.c_str(), user->nick.c_str(), target->nick.c_str());
181                                 target->WriteServ("NOTICE %s :*** %s removed you from %s with the message: %s", target->nick.c_str(), user->nick.c_str(), channel->name.c_str(), reasonparam.c_str());
182
183                                 if (!channel->PartUser(target, reason))
184                                         delete channel;
185                         }
186                         else
187                         {
188                                 user->WriteServ( "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick.c_str(), target->nick.c_str(), channel->name.c_str());
189                                 return CMD_FAILURE;
190                         }
191                 }
192                 else
193                 {
194                         /* m_nokicks.so was loaded and +Q was set, block! */
195                         user->WriteServ( "484 %s %s :Can't remove user %s from channel (+Q set)", user->nick.c_str(), channel->name.c_str(), target->nick.c_str());
196                         return CMD_FAILURE;
197                 }
198
199                 /* route me */
200                 return CMD_SUCCESS;
201         }
202 };
203
204 /** Handle /REMOVE
205  */
206 class CommandRemove : public Command, public RemoveBase
207 {
208  public:
209         CommandRemove(InspIRCd* Instance, bool& snk) : Command(Instance, "REMOVE", 0, 2), RemoveBase(Instance, snk)
210         {
211                 this->source = "m_remove.so";
212                 syntax = "<nick> <channel> [<reason>]";
213                 TRANSLATE4(TR_NICK, TR_TEXT, TR_TEXT, TR_END);
214         }
215
216         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
217         {
218                 return RemoveBase::Handle(parameters, user, false);
219         }
220 };
221
222 /** Handle /FPART
223  */
224 class CommandFpart : public Command, public RemoveBase
225 {
226  public:
227         CommandFpart(InspIRCd* Instance, bool& snk) : Command(Instance, "FPART", 0, 2), RemoveBase(Instance, snk)
228         {
229                 this->source = "m_remove.so";
230                 syntax = "<channel> <nick> [<reason>]";
231         }
232
233         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
234         {
235                 return RemoveBase::Handle(parameters, user, true);
236         }
237 };
238
239 class ModuleRemove : public Module
240 {
241         CommandRemove* mycommand;
242         CommandFpart* mycommand2;
243         bool supportnokicks;
244
245
246  public:
247         ModuleRemove(InspIRCd* Me)
248         : Module(Me)
249         {
250                 mycommand = new CommandRemove(ServerInstance, supportnokicks);
251                 mycommand2 = new CommandFpart(ServerInstance, supportnokicks);
252                 ServerInstance->AddCommand(mycommand);
253                 ServerInstance->AddCommand(mycommand2);
254                 OnRehash(NULL,"");
255                 Implementation eventlist[] = { I_On005Numeric, I_OnRehash };
256                 ServerInstance->Modules->Attach(eventlist, this, 2);
257         }
258
259
260         virtual void On005Numeric(std::string &output)
261         {
262                 output.append(" REMOVE");
263         }
264
265         virtual void OnRehash(User* user, const std::string&)
266         {
267                 ConfigReader conf(ServerInstance);
268                 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
269         }
270
271         virtual ~ModuleRemove()
272         {
273         }
274
275         virtual Version GetVersion()
276         {
277                 return Version(1, 2, 1, 0, VF_COMMON | VF_VENDOR, API_VERSION);
278         }
279
280 };
281
282 MODULE_INIT(ModuleRemove)