]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/ctables.h
Tidy up source files:
[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 command success codes
27  */
28 enum CmdResult
29 {
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 */
34 };
35
36 /** Flag for commands that are only allowed from servers */
37 const char FLAG_SERVERONLY = 7; // technically anything nonzero below 'A' works
38
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?)
43  */
44 enum TranslateType
45 {
46         TR_END,                 /* End of known parameters, everything after this is TR_TEXT */
47         TR_TEXT,                /* Raw text, leave as-is */
48         TR_NICK,                /* Nickname, translate to UUID for server->server */
49         TR_CUSTOM               /* Custom translation handled by EncodeParameter/DecodeParameter */
50 };
51
52 /** Routing types for a command. Any command which is created defaults
53  * to having its command broadcasted on success. This behaviour may be
54  * overridden to one of the route types shown below (see the #defines
55  * below for more information on each one's behaviour)
56  */
57 enum RouteType
58 {
59         ROUTE_TYPE_LOCALONLY,
60         ROUTE_TYPE_BROADCAST,
61         ROUTE_TYPE_UNICAST,
62         ROUTE_TYPE_MESSAGE,
63         ROUTE_TYPE_OPT_BCAST,
64         ROUTE_TYPE_OPT_UCAST
65 };
66
67 /** Defines routing information for a command, containing a destination
68  * server id (if applicable) and a routing type from the enum above.
69  */
70 struct RouteDescriptor
71 {
72         /** Routing type from the enum above
73          */
74         RouteType type;
75         /** For unicast, the destination server's name
76          */
77         std::string serverdest;
78
79         /** Create a RouteDescriptor
80          */
81         RouteDescriptor(RouteType t, const std::string &d)
82                 : type(t), serverdest(d) { }
83 };
84
85 /** Do not route this command */
86 #define ROUTE_LOCALONLY (RouteDescriptor(ROUTE_TYPE_LOCALONLY, ""))
87 /** Route this command to all servers, fail if not understood */
88 #define ROUTE_BROADCAST (RouteDescriptor(ROUTE_TYPE_BROADCAST, ""))
89 /** Route this command to a single server (do nothing if own server name specified) */
90 #define ROUTE_UNICAST(x) (RouteDescriptor(ROUTE_TYPE_UNICAST, x))
91 /** Route this command as a message with the given target (any of user, #channel, @#channel, $servermask) */
92 #define ROUTE_MESSAGE(x) (RouteDescriptor(ROUTE_TYPE_MESSAGE, x))
93 /** Route this command to all servers wrapped via ENCAP, so ignored if not understood */
94 #define ROUTE_OPT_BCAST (RouteDescriptor(ROUTE_TYPE_OPT_BCAST, ""))
95 /** Route this command to a single server wrapped via ENCAP, so ignored if not understood */
96 #define ROUTE_OPT_UCAST(x) (RouteDescriptor(ROUTE_TYPE_OPT_UCAST, x))
97
98 /** A structure that defines a command. Every command available
99  * in InspIRCd must be defined as derived from Command.
100  */
101 class CoreExport Command : public ServiceProvider
102 {
103  public:
104         /** User flags needed to execute the command or 0
105          */
106         char flags_needed;
107
108         /** Minimum number of parameters command takes
109         */
110         const unsigned int min_params;
111
112         /** Maximum number of parameters command takes.
113          * This is used by the command parser to join extra parameters into one last param.
114          * If not set, no munging is done to this command.
115          */
116         const unsigned int max_params;
117
118         /** used by /stats m
119          */
120         unsigned long use_count;
121
122         /** used by /stats m
123          */
124         unsigned long total_bytes;
125
126         /** True if the command is disabled to non-opers
127          */
128         bool disabled;
129
130         /** True if the command can be issued before registering
131          */
132         bool works_before_reg;
133
134         /** True if the command allows an empty last parameter.
135          * When false and the last parameter is empty, it's popped BEFORE
136          * checking there are enough params, etc. (i.e. the handler won't
137          * be called if there aren't enough params after popping the empty
138          * param).
139          * True by default
140          */
141         bool allow_empty_last_param;
142
143         /** Syntax string for the command, displayed if non-empty string.
144          * This takes place of the text in the 'not enough parameters' numeric.
145          */
146         std::string syntax;
147
148         /** Translation type list for possible parameters, used to tokenize
149          * parameters into UIDs and SIDs etc.
150          */
151         std::vector<TranslateType> translation;
152
153         /** How many seconds worth of penalty does this command have?
154          */
155         int Penalty;
156
157         /** Create a new command.
158          * @param me The module which created this command.
159          * @param cmd Command name. This must be UPPER CASE.
160          * @param minpara Minimum parameters required for the command.
161          * @param maxpara Maximum number of parameters this command may have - extra parameters
162          * will be tossed into one last space-seperated param.
163          */
164         Command(Module* me, const std::string &cmd, int minpara = 0, int maxpara = 0) :
165                 ServiceProvider(me, cmd, SERVICE_COMMAND), flags_needed(0), min_params(minpara), max_params(maxpara),
166                 use_count(0), total_bytes(0), disabled(false), works_before_reg(false), allow_empty_last_param(true),
167                 Penalty(1)
168         {
169         }
170
171         /** Handle the command from a user.
172          * @param parameters The parameters for the command.
173          * @param user The user who issued the command.
174          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
175          */
176         virtual CmdResult Handle(const std::vector<std::string>& parameters, User* user) = 0;
177
178         virtual RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
179         {
180                 return ROUTE_LOCALONLY;
181         }
182
183         /** Encode a parameter for server->server transmission.
184          * Used for parameters for which the translation type is TR_CUSTOM.
185          * @param parameter The parameter to encode. Can be modified in place.
186          * @param index The parameter index (0 == first parameter).
187          */
188         virtual void EncodeParameter(std::string& parameter, int index)
189         {
190         }
191
192         /** Decode a parameter from server->server transmission.
193          * Not currently used in this version of InspIRCd.
194          * Used for parameters for which the translation type is TR_CUSTOM.
195          * @param parameter The parameter to decode. Can be modified in place.
196          * @param index The parameter index (0 == first parameter).
197          */
198         virtual void DecodeParameter(std::string& parameter, int index)
199         {
200         }
201
202         /** Disable or enable this command.
203          * @param setting True to disable the command.
204          */
205         void Disable(bool setting)
206         {
207                 disabled = setting;
208         }
209
210         /** Obtain this command's disable state.
211          * @return true if the command is currently disabled
212          * (disabled commands can be used only by operators)
213          */
214         bool IsDisabled()
215         {
216                 return disabled;
217         }
218
219         /** @return true if the command works before registration.
220          */
221         bool WorksBeforeReg()
222         {
223                 return works_before_reg;
224         }
225
226         virtual ~Command();
227 };
228
229 class CoreExport SplitCommand : public Command
230 {
231  public:
232         SplitCommand(Module* me, const std::string &cmd, int minpara = 0, int maxpara = 0)
233                 : Command(me, cmd, minpara, maxpara) {}
234         virtual CmdResult Handle(const std::vector<std::string>& parameters, User* user);
235         virtual CmdResult HandleLocal(const std::vector<std::string>& parameters, LocalUser* user);
236         virtual CmdResult HandleRemote(const std::vector<std::string>& parameters, RemoteUser* user);
237         virtual CmdResult HandleServer(const std::vector<std::string>& parameters, FakeUser* user);
238 };
239
240 /** Shortcut macros for defining translation lists
241  */
242 #define TRANSLATE1(x1)  translation.push_back(x1);
243 #define TRANSLATE2(x1,x2)  translation.push_back(x1);translation.push_back(x2);
244 #define TRANSLATE3(x1,x2,x3)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);
245 #define TRANSLATE4(x1,x2,x3,x4)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);
246 #define TRANSLATE5(x1,x2,x3,x4,x5)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
247         translation.push_back(x5);
248 #define TRANSLATE6(x1,x2,x3,x4,x5,x6)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
249         translation.push_back(x5);translation.push_back(x6);
250 #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);\
251         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);
252 #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);\
253         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);translation.push_back(x8);