2 * InspIRCd -- Internet Relay Chat Daemon
4 * Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5 * Copyright (C) 2008 Robin Burchell <robin+git@viroteck.net>
6 * Copyright (C) 2008 Thomas Stagner <aquanight@inspircd.org>
7 * Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
8 * Copyright (C) 2003, 2007 Craig Edwards <craigedwards@brainbox.cc>
10 * This file is part of InspIRCd. InspIRCd is free software: you can
11 * redistribute it and/or modify it under the terms of the GNU General Public
12 * License as published by the Free Software Foundation, version 2.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
26 /** Used to indicate command success codes
30 CMD_FAILURE = 0, /* Command exists, but failed */
31 CMD_SUCCESS = 1, /* Command exists, and succeeded */
32 CMD_INVALID = 2, /* Command doesnt exist at all! */
33 CMD_EPERM = 3 /* Command failed because of a permission check */
36 /** Flag for commands that are only allowed from servers */
37 const char FLAG_SERVERONLY = 7; // technically anything nonzero below 'A' works
39 /** Translation types for translation of parameters to UIDs.
40 * This allows the core commands to not have to be aware of how UIDs
41 * work (making it still possible to write other linking modules which
42 * do not use UID (but why would you want to?)
46 TR_TEXT, /* Raw text, leave as-is */
47 TR_NICK, /* Nickname, translate to UUID for server->server */
48 TR_CUSTOM /* Custom translation handled by EncodeParameter/DecodeParameter */
51 /** Routing types for a command. Any command which is created defaults
52 * to having its command broadcasted on success. This behaviour may be
53 * overridden to one of the route types shown below (see the #defines
54 * below for more information on each one's behaviour)
66 /** Defines routing information for a command, containing a destination
67 * server id (if applicable) and a routing type from the enum above.
69 struct RouteDescriptor
71 /** Routing type from the enum above
74 /** For unicast, the destination server's name
76 std::string serverdest;
78 /** For unicast, the destination Server
82 /** Create a RouteDescriptor
84 RouteDescriptor(RouteType t, const std::string &d)
85 : type(t), serverdest(d), server(NULL) { }
87 RouteDescriptor(RouteType t, Server* srv)
88 : type(t), server(srv) { }
91 /** Do not route this command */
92 #define ROUTE_LOCALONLY (RouteDescriptor(ROUTE_TYPE_LOCALONLY, ""))
93 /** Route this command to all servers, fail if not understood */
94 #define ROUTE_BROADCAST (RouteDescriptor(ROUTE_TYPE_BROADCAST, ""))
95 /** Route this command to a single server (do nothing if own server name specified) */
96 #define ROUTE_UNICAST(x) (RouteDescriptor(ROUTE_TYPE_UNICAST, x))
97 /** Route this command as a message with the given target (any of user, #channel, @#channel, $servermask) */
98 #define ROUTE_MESSAGE(x) (RouteDescriptor(ROUTE_TYPE_MESSAGE, x))
99 /** Route this command to all servers wrapped via ENCAP, so ignored if not understood */
100 #define ROUTE_OPT_BCAST (RouteDescriptor(ROUTE_TYPE_OPT_BCAST, ""))
101 /** Route this command to a single server wrapped via ENCAP, so ignored if not understood */
102 #define ROUTE_OPT_UCAST(x) (RouteDescriptor(ROUTE_TYPE_OPT_UCAST, x))
104 /** A structure that defines a command. Every command available
105 * in InspIRCd must be defined as derived from Command.
107 class CoreExport CommandBase : public ServiceProvider
110 /** User flags needed to execute the command or 0
114 /** Minimum number of parameters command takes
116 const unsigned int min_params;
118 /** Maximum number of parameters command takes.
119 * This is used by the command parser to join extra parameters into one last param.
120 * If not set, no munging is done to this command.
122 const unsigned int max_params;
126 unsigned long use_count;
128 /** True if the command is disabled to non-opers
132 /** True if the command can be issued before registering
134 bool works_before_reg;
136 /** True if the command allows an empty last parameter.
137 * When false and the last parameter is empty, it's popped BEFORE
138 * checking there are enough params, etc. (i.e. the handler won't
139 * be called if there aren't enough params after popping the empty
143 bool allow_empty_last_param;
145 /** Syntax string for the command, displayed if non-empty string.
146 * This takes place of the text in the 'not enough parameters' numeric.
150 /** Translation type list for possible parameters, used to tokenize
151 * parameters into UIDs and SIDs etc.
153 std::vector<TranslateType> translation;
155 /** How many seconds worth of penalty does this command have?
159 /** Create a new command.
160 * @param me The module which created this command.
161 * @param cmd Command name. This must be UPPER CASE.
162 * @param minpara Minimum parameters required for the command.
163 * @param maxpara Maximum number of parameters this command may have - extra parameters
164 * will be tossed into one last space-seperated param.
166 CommandBase(Module* me, const std::string &cmd, int minpara = 0, int maxpara = 0) :
167 ServiceProvider(me, cmd, SERVICE_COMMAND), flags_needed(0), min_params(minpara), max_params(maxpara),
168 use_count(0), disabled(false), works_before_reg(false), allow_empty_last_param(true),
173 virtual RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
175 return ROUTE_LOCALONLY;
178 /** Encode a parameter for server->server transmission.
179 * Used for parameters for which the translation type is TR_CUSTOM.
180 * @param parameter The parameter to encode. Can be modified in place.
181 * @param index The parameter index (0 == first parameter).
183 virtual void EncodeParameter(std::string& parameter, int index)
187 /** Decode a parameter from server->server transmission.
188 * Not currently used in this version of InspIRCd.
189 * Used for parameters for which the translation type is TR_CUSTOM.
190 * @param parameter The parameter to decode. Can be modified in place.
191 * @param index The parameter index (0 == first parameter).
193 virtual void DecodeParameter(std::string& parameter, int index)
197 /** Disable or enable this command.
198 * @param setting True to disable the command.
200 void Disable(bool setting)
205 /** Obtain this command's disable state.
206 * @return true if the command is currently disabled
207 * (disabled commands can be used only by operators)
214 /** @return true if the command works before registration.
216 bool WorksBeforeReg()
218 return works_before_reg;
221 virtual ~CommandBase();
224 class CoreExport Command : public CommandBase
227 /** If true, the command will not be forwarded by the linking module even if it comes via ENCAP.
228 * Can be used to forward commands before their effects.
230 bool force_manual_route;
232 Command(Module* me, const std::string& cmd, unsigned int minpara = 0, unsigned int maxpara = 0)
233 : CommandBase(me, cmd, minpara, maxpara)
234 , force_manual_route(false)
238 /** Handle the command from a user.
239 * @param parameters The parameters for the command.
240 * @param user The user who issued the command.
241 * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
243 virtual CmdResult Handle(const std::vector<std::string>& parameters, User* user) = 0;
246 * Removes this command from the command parser
251 class CoreExport SplitCommand : public Command
254 SplitCommand(Module* me, const std::string &cmd, int minpara = 0, int maxpara = 0)
255 : Command(me, cmd, minpara, maxpara) {}
256 virtual CmdResult Handle(const std::vector<std::string>& parameters, User* user);
257 virtual CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user);
258 virtual CmdResult HandleRemote(const std::vector<std::string>& parameters, RemoteUser* user);
259 virtual CmdResult HandleServer(const std::vector<std::string>& parameters, FakeUser* user);
262 /** Shortcut macros for defining translation lists
264 #define TRANSLATE1(x1) translation.push_back(x1);
265 #define TRANSLATE2(x1,x2) translation.push_back(x1);translation.push_back(x2);
266 #define TRANSLATE3(x1,x2,x3) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);
267 #define TRANSLATE4(x1,x2,x3,x4) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);
268 #define TRANSLATE5(x1,x2,x3,x4,x5) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
269 translation.push_back(x5);
270 #define TRANSLATE6(x1,x2,x3,x4,x5,x6) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
271 translation.push_back(x5);translation.push_back(x6);
272 #define TRANSLATE7(x1,x2,x3,x4,x5,x6,x7) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
273 translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);
274 #define TRANSLATE8(x1,x2,x3,x4,x5,x6,x7,x8) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
275 translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);translation.push_back(x8);