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/>.
27 /** Used to indicate command success codes
31 CMD_FAILURE = 0, /* Command exists, but failed */
32 CMD_SUCCESS = 1, /* Command exists, and succeeded */
33 CMD_INVALID = 2, /* Command doesnt exist at all! */
34 CMD_EPERM = 3 /* Command failed because of a permission check */
37 /** Flag for commands that are only allowed from servers */
38 const char FLAG_SERVERONLY = 7; // technically anything nonzero below 'A' works
40 /** Translation types for translation of parameters to UIDs.
41 * This allows the core commands to not have to be aware of how UIDs
42 * work (making it still possible to write other linking modules which
43 * do not use UID (but why would you want to?)
47 TR_END, /* End of known parameters, everything after this is TR_TEXT */
48 TR_TEXT, /* Raw text, leave as-is */
49 TR_NICK, /* Nickname, translate to UUID for server->server */
50 TR_CUSTOM /* Custom translation handled by EncodeParameter/DecodeParameter */
53 /** Routing types for a command. Any command which is created defaults
54 * to having its command broadcasted on success. This behaviour may be
55 * overridden to one of the route types shown below (see the #defines
56 * below for more information on each one's behaviour)
68 /** Defines routing information for a command, containing a destination
69 * server id (if applicable) and a routing type from the enum above.
71 struct RouteDescriptor
73 /** Routing type from the enum above
76 /** For unicast, the destination server's name
78 std::string serverdest;
80 /** Create a RouteDescriptor
82 RouteDescriptor(RouteType t, const std::string &d)
83 : type(t), serverdest(d) { }
86 /** Do not route this command */
87 #define ROUTE_LOCALONLY (RouteDescriptor(ROUTE_TYPE_LOCALONLY, ""))
88 /** Route this command to all servers, fail if not understood */
89 #define ROUTE_BROADCAST (RouteDescriptor(ROUTE_TYPE_BROADCAST, ""))
90 /** Route this command to a single server (do nothing if own server name specified) */
91 #define ROUTE_UNICAST(x) (RouteDescriptor(ROUTE_TYPE_UNICAST, x))
92 /** Route this command as a message with the given target (any of user, #channel, @#channel, $servermask) */
93 #define ROUTE_MESSAGE(x) (RouteDescriptor(ROUTE_TYPE_MESSAGE, x))
94 /** Route this command to all servers wrapped via ENCAP, so ignored if not understood */
95 #define ROUTE_OPT_BCAST (RouteDescriptor(ROUTE_TYPE_OPT_BCAST, ""))
96 /** Route this command to a single server wrapped via ENCAP, so ignored if not understood */
97 #define ROUTE_OPT_UCAST(x) (RouteDescriptor(ROUTE_TYPE_OPT_UCAST, x))
99 /** A structure that defines a command. Every command available
100 * in InspIRCd must be defined as derived from Command.
102 class CoreExport Command : public ServiceProvider
105 /** User flags needed to execute the command or 0
109 /** Minimum number of parameters command takes
111 const unsigned int min_params;
113 /** Maximum number of parameters command takes.
114 * This is used by the command parser to join extra parameters into one last param.
115 * If not set, no munging is done to this command.
117 const unsigned int max_params;
121 unsigned long use_count;
125 unsigned long total_bytes;
127 /** True if the command is disabled to non-opers
131 /** True if the command can be issued before registering
133 bool works_before_reg;
135 /** True if the command allows an empty last parameter.
136 * When false and the last parameter is empty, it's popped BEFORE
137 * checking there are enough params, etc. (i.e. the handler won't
138 * be called if there aren't enough params after popping the empty
142 bool allow_empty_last_param;
144 /** Syntax string for the command, displayed if non-empty string.
145 * This takes place of the text in the 'not enough parameters' numeric.
149 /** Translation type list for possible parameters, used to tokenize
150 * parameters into UIDs and SIDs etc.
152 std::vector<TranslateType> translation;
154 /** How many seconds worth of penalty does this command have?
158 /** Create a new command.
159 * @param me The module which created this command.
160 * @param cmd Command name. This must be UPPER CASE.
161 * @param minpara Minimum parameters required for the command.
162 * @param maxpara Maximum number of parameters this command may have - extra parameters
163 * will be tossed into one last space-seperated param.
165 Command(Module* me, const std::string &cmd, int minpara = 0, int maxpara = 0) :
166 ServiceProvider(me, cmd, SERVICE_COMMAND), flags_needed(0), min_params(minpara), max_params(maxpara),
167 use_count(0), total_bytes(0), disabled(false), works_before_reg(false), allow_empty_last_param(true),
172 /** Handle the command from a user.
173 * @param parameters The parameters for the command.
174 * @param user The user who issued the command.
175 * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
177 virtual CmdResult Handle(const std::vector<std::string>& parameters, User* user) = 0;
179 virtual RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
181 return ROUTE_LOCALONLY;
184 /** Encode a parameter for server->server transmission.
185 * Used for parameters for which the translation type is TR_CUSTOM.
186 * @param parameter The parameter to encode. Can be modified in place.
187 * @param index The parameter index (0 == first parameter).
189 virtual void EncodeParameter(std::string& parameter, int index)
193 /** Decode a parameter from server->server transmission.
194 * Not currently used in this version of InspIRCd.
195 * Used for parameters for which the translation type is TR_CUSTOM.
196 * @param parameter The parameter to decode. Can be modified in place.
197 * @param index The parameter index (0 == first parameter).
199 virtual void DecodeParameter(std::string& parameter, int index)
203 /** Disable or enable this command.
204 * @param setting True to disable the command.
206 void Disable(bool setting)
211 /** Obtain this command's disable state.
212 * @return true if the command is currently disabled
213 * (disabled commands can be used only by operators)
220 /** @return true if the command works before registration.
222 bool WorksBeforeReg()
224 return works_before_reg;
230 class CoreExport SplitCommand : public Command
233 SplitCommand(Module* me, const std::string &cmd, int minpara = 0, int maxpara = 0)
234 : Command(me, cmd, minpara, maxpara) {}
235 virtual CmdResult Handle(const std::vector<std::string>& parameters, User* user);
236 virtual CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user);
237 virtual CmdResult HandleRemote(const std::vector<std::string>& parameters, RemoteUser* user);
238 virtual CmdResult HandleServer(const std::vector<std::string>& parameters, FakeUser* user);
241 /** Shortcut macros for defining translation lists
243 #define TRANSLATE1(x1) translation.push_back(x1);
244 #define TRANSLATE2(x1,x2) translation.push_back(x1);translation.push_back(x2);
245 #define TRANSLATE3(x1,x2,x3) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);
246 #define TRANSLATE4(x1,x2,x3,x4) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);
247 #define TRANSLATE5(x1,x2,x3,x4,x5) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
248 translation.push_back(x5);
249 #define TRANSLATE6(x1,x2,x3,x4,x5,x6) translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
250 translation.push_back(x5);translation.push_back(x6);
251 #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);\
252 translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);
253 #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);\
254 translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);translation.push_back(x8);