]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_remove.cpp
Remove unnecessary header traffic
[user/henk/code/inspircd.git] / src / modules / m_remove.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 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 char** parameters, int pcnt, userrec *user, bool neworder)
73         {
74                 const char* channame;
75                 const char* username;
76                 userrec* target;
77                 chanrec* 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];
91                 username = parameters[ neworder ? 1 : 0];
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->WriteServ("401 %s %s :No such nick/channel", user->nick, !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, target->nick, channel->name);
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))
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))
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->FindModule("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(pcnt > 2)
171                                 {
172                                         /* Join params 2 ... pcnt - 1 (inclusive) into one */
173                                         irc::stringjoiner reason_join(" ", parameters, 2, pcnt - 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, user->nick, target->nick);
181                                 target->WriteServ("NOTICE %s :*** %s removed you from %s with the message: %s", target->nick, user->nick, channel->name, reasonparam.c_str());
182
183                                 if (!channel->PartUser(target, reason.c_str()))
184                                         delete channel;
185                         }
186                         else
187                         {
188                                 user->WriteServ( "NOTICE %s :*** You do not have access to /remove %s from %s", user->nick, target->nick, channel->name);
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, channel->name, target->nick);
196                         return CMD_FAILURE;
197                 }
198
199                 /* route me */
200                 return CMD_SUCCESS;
201         }
202 };
203
204 /** Handle /REMOVE
205  */
206 class cmd_remove : public command_t, public RemoveBase
207 {
208  public:
209         cmd_remove(InspIRCd* Instance, bool& snk) : command_t(Instance, "REMOVE", 0, 2), RemoveBase(Instance, snk)
210         {
211                 this->source = "m_remove.so";
212                 syntax = "<nick> <channel> [<reason>]";
213         }
214         
215         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
216         {
217                 return RemoveBase::Handle(parameters, pcnt, user, false);
218         }
219 };
220
221 /** Handle /FPART
222  */
223 class cmd_fpart : public command_t, public RemoveBase
224 {
225  public:
226         cmd_fpart(InspIRCd* Instance, bool& snk) : command_t(Instance, "FPART", 0, 2), RemoveBase(Instance, snk)
227         {
228                 this->source = "m_remove.so";
229                 syntax = "<channel> <nick> [<reason>]";
230         }
231
232         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
233         {
234                 return RemoveBase::Handle(parameters, pcnt, user, true);
235         }
236 };
237
238 class ModuleRemove : public Module
239 {
240         cmd_remove* mycommand;
241         cmd_fpart* mycommand2;
242         bool supportnokicks;
243         
244         
245  public:
246         ModuleRemove(InspIRCd* Me)
247         : Module(Me)
248         {
249                 mycommand = new cmd_remove(ServerInstance, supportnokicks);
250                 mycommand2 = new cmd_fpart(ServerInstance, supportnokicks);
251                 ServerInstance->AddCommand(mycommand);
252                 ServerInstance->AddCommand(mycommand2);
253                 OnRehash(NULL,"");
254         }
255
256         void Implements(char* List)
257         {
258                 List[I_On005Numeric] = List[I_OnRehash] = 1;
259         }
260
261         virtual void On005Numeric(std::string &output)
262         {
263                 output.append(" REMOVE");
264         }
265         
266         virtual void OnRehash(userrec* user, const std::string&)
267         {
268                 ConfigReader conf(ServerInstance);
269                 supportnokicks = conf.ReadFlag("remove", "supportnokicks", 0);
270         }
271         
272         virtual ~ModuleRemove()
273         {
274         }
275         
276         virtual Version GetVersion()
277         {
278                 return Version(1, 1, 1, 0, VF_COMMON | VF_VENDOR, API_VERSION);
279         }
280         
281 };
282
283 MODULE_INIT(ModuleRemove)