]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/ctables.h
8be40cc54d597350b21a2b51ff845cc8b641c973
[user/henk/code/inspircd.git] / include / ctables.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
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>
9  *
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.
13  *
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
17  * details.
18  *
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/>.
21  */
22
23
24 #pragma once
25
26 /** Used to indicate the result of trying to execute a command. */
27 enum CmdResult
28 {
29         /** The command exists but its execution failed. */
30         CMD_FAILURE = 0,
31
32         /** The command exists and its execution succeeded. */
33         CMD_SUCCESS = 1,
34
35         /* The command does not exist. */
36         CMD_INVALID = 2
37 };
38
39 /** Flag for commands that are only allowed from servers */
40 const char FLAG_SERVERONLY = 7; // technically anything nonzero below 'A' works
41
42 /** Translation types for translation of parameters to UIDs.
43  * This allows the core commands to not have to be aware of how UIDs
44  * work (making it still possible to write other linking modules which
45  * do not use UID (but why would you want to?)
46  */
47 enum TranslateType
48 {
49         TR_TEXT,                /* Raw text, leave as-is */
50         TR_NICK,                /* Nickname, translate to UUID for server->server */
51         TR_CUSTOM               /* Custom translation handled by EncodeParameter/DecodeParameter */
52 };
53
54 /** Routing types for a command. Any command which is created defaults
55  * to having its command broadcasted on success. This behaviour may be
56  * overridden to one of the route types shown below (see the #defines
57  * below for more information on each one's behaviour)
58  */
59 enum RouteType
60 {
61         ROUTE_TYPE_LOCALONLY,
62         ROUTE_TYPE_BROADCAST,
63         ROUTE_TYPE_UNICAST,
64         ROUTE_TYPE_MESSAGE,
65         ROUTE_TYPE_OPT_BCAST,
66         ROUTE_TYPE_OPT_UCAST
67 };
68
69 /** Defines routing information for a command, containing a destination
70  * server id (if applicable) and a routing type from the enum above.
71  */
72 struct RouteDescriptor
73 {
74         /** Routing type from the enum above
75          */
76         RouteType type;
77         /** For unicast, the destination server's name
78          */
79         std::string serverdest;
80
81         /** For unicast, the destination Server
82          */
83         Server* server;
84
85         /** Create a RouteDescriptor
86          */
87         RouteDescriptor(RouteType t, const std::string &d)
88                 : type(t), serverdest(d), server(NULL) { }
89
90         RouteDescriptor(RouteType t, Server* srv)
91                 : type(t), server(srv) { }
92 };
93
94 /** Do not route this command */
95 #define ROUTE_LOCALONLY (RouteDescriptor(ROUTE_TYPE_LOCALONLY, ""))
96 /** Route this command to all servers, fail if not understood */
97 #define ROUTE_BROADCAST (RouteDescriptor(ROUTE_TYPE_BROADCAST, ""))
98 /** Route this command to a single server (do nothing if own server name specified) */
99 #define ROUTE_UNICAST(x) (RouteDescriptor(ROUTE_TYPE_UNICAST, x))
100 /** Route this command as a message with the given target (any of user, #channel, @#channel, $servermask) */
101 #define ROUTE_MESSAGE(x) (RouteDescriptor(ROUTE_TYPE_MESSAGE, x))
102 /** Route this command to all servers wrapped via ENCAP, so ignored if not understood */
103 #define ROUTE_OPT_BCAST (RouteDescriptor(ROUTE_TYPE_OPT_BCAST, ""))
104 /** Route this command to a single server wrapped via ENCAP, so ignored if not understood */
105 #define ROUTE_OPT_UCAST(x) (RouteDescriptor(ROUTE_TYPE_OPT_UCAST, x))
106
107 /** A structure that defines a command. Every command available
108  * in InspIRCd must be defined as derived from Command.
109  */
110 class CoreExport CommandBase : public ServiceProvider
111 {
112  public:
113         /** Encapsulates parameters to a command. */
114         class Params : public std::vector<std::string>
115         {
116          private:
117                 /* IRCv3 message tags. */
118                 ClientProtocol::TagMap tags;
119
120          public:
121                 /** Initializes a new instance from parameter and tag references.
122                  * @param paramsref Message parameters.
123                  * @param tagsref IRCv3 message tags.
124                  */
125                 Params(const std::vector<std::string>& paramsref, const ClientProtocol::TagMap& tagsref)
126                         : std::vector<std::string>(paramsref)
127                         , tags(tagsref)
128                 {
129                 }
130
131                 /** Initializes a new instance from parameter iterators.
132                  * @param first The first element in the parameter array.
133                  * @param last The last element in the parameter array.
134                  */
135                 template<typename Iterator>
136                 Params(Iterator first, Iterator last)
137                         : std::vector<std::string>(first, last)
138                 {
139                 }
140
141                 /** Initializes a new empty instance. */
142                 Params() { }
143
144                 /** Retrieves the IRCv3 message tags. */
145                 const ClientProtocol::TagMap& GetTags() const { return tags; }
146         };
147
148         /** User flags needed to execute the command or 0
149          */
150         unsigned char flags_needed;
151
152         /** Minimum number of parameters command takes
153         */
154         const unsigned int min_params;
155
156         /** Maximum number of parameters command takes.
157          * This is used by the command parser to join extra parameters into one last param.
158          * If not set, no munging is done to this command.
159          */
160         const unsigned int max_params;
161
162         /** used by /stats m
163          */
164         unsigned long use_count;
165
166         /** True if the command is disabled to non-opers
167          */
168         bool disabled;
169
170         /** True if the command can be issued before registering
171          */
172         bool works_before_reg;
173
174         /** True if the command allows an empty last parameter.
175          * When false and the last parameter is empty, it's popped BEFORE
176          * checking there are enough params, etc. (i.e. the handler won't
177          * be called if there aren't enough params after popping the empty
178          * param).
179          * True by default
180          */
181         bool allow_empty_last_param;
182
183         /** Syntax string for the command, displayed if non-empty string.
184          * This takes place of the text in the 'not enough parameters' numeric.
185          */
186         std::string syntax;
187
188         /** Translation type list for possible parameters, used to tokenize
189          * parameters into UIDs and SIDs etc.
190          */
191         std::vector<TranslateType> translation;
192
193         /** How many seconds worth of penalty does this command have?
194          */
195         unsigned int Penalty;
196
197         /** Create a new command.
198          * @param me The module which created this command.
199          * @param cmd Command name. This must be UPPER CASE.
200          * @param minpara Minimum parameters required for the command.
201          * @param maxpara Maximum number of parameters this command may have - extra parameters
202          * will be tossed into one last space-seperated param.
203          */
204         CommandBase(Module* me, const std::string& cmd, unsigned int minpara = 0, unsigned int maxpara = 0);
205
206         virtual RouteDescriptor GetRouting(User* user, const CommandBase::Params& parameters);
207
208         /** Encode a parameter for server->server transmission.
209          * Used for parameters for which the translation type is TR_CUSTOM.
210          * @param parameter The parameter to encode. Can be modified in place.
211          * @param index The parameter index (0 == first parameter).
212          */
213         virtual void EncodeParameter(std::string& parameter, unsigned int index);
214
215         /** Disable or enable this command.
216          * @param setting True to disable the command.
217          */
218         void Disable(bool setting)
219         {
220                 disabled = setting;
221         }
222
223         /** Obtain this command's disable state.
224          * @return true if the command is currently disabled
225          * (disabled commands can be used only by operators)
226          */
227         bool IsDisabled()
228         {
229                 return disabled;
230         }
231
232         /** @return true if the command works before registration.
233          */
234         bool WorksBeforeReg()
235         {
236                 return works_before_reg;
237         }
238
239         virtual ~CommandBase();
240 };
241
242 class CoreExport Command : public CommandBase
243 {
244  public:
245         /** If true, the command will not be forwarded by the linking module even if it comes via ENCAP.
246          * Can be used to forward commands before their effects.
247          */
248         bool force_manual_route;
249
250         Command(Module* me, const std::string& cmd, unsigned int minpara = 0, unsigned int maxpara = 0);
251
252         /** Handle the command from a user.
253          * @param parameters The parameters for the command.
254          * @param user The user who issued the command.
255          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
256          */
257         virtual CmdResult Handle(User* user, const Params& parameters) = 0;
258
259         /** Register this object in the CommandParser
260          */
261         void RegisterService() CXX11_OVERRIDE;
262
263         /** Destructor
264          * Removes this command from the command parser
265          */
266         ~Command();
267 };
268
269 class CoreExport SplitCommand : public Command
270 {
271  public:
272         SplitCommand(Module* me, const std::string &cmd, unsigned int minpara = 0, unsigned int maxpara = 0)
273                 : Command(me, cmd, minpara, maxpara) {}
274         CmdResult Handle(User* user, const Params& parameters) CXX11_OVERRIDE;
275         virtual CmdResult HandleLocal(LocalUser* user, const Params& parameters);
276         virtual CmdResult HandleRemote(RemoteUser* user, const Params& parameters);
277         virtual CmdResult HandleServer(FakeUser* user, const Params& parameters);
278 };
279
280 /** Shortcut macros for defining translation lists
281  */
282 #define TRANSLATE1(x1)  translation.push_back(x1);
283 #define TRANSLATE2(x1,x2)  translation.push_back(x1);translation.push_back(x2);
284 #define TRANSLATE3(x1,x2,x3)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);
285 #define TRANSLATE4(x1,x2,x3,x4)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);
286 #define TRANSLATE5(x1,x2,x3,x4,x5)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
287         translation.push_back(x5);
288 #define TRANSLATE6(x1,x2,x3,x4,x5,x6)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
289         translation.push_back(x5);translation.push_back(x6);
290 #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);\
291         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);
292 #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);\
293         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);translation.push_back(x8);