]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/ctables.h
95db50b1a14317f41cacf10fe1a647a8d8a7d5ff
[user/henk/code/inspircd.git] / include / ctables.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __CTABLES_H__
15 #define __CTABLES_H__
16
17 /** Used to indicate command success codes
18  */
19 enum CmdResult
20 {
21         CMD_FAILURE = 0,        /* Command exists, but failed */
22         CMD_SUCCESS = 1,        /* Command exists, and succeeded */
23         CMD_INVALID = 2         /* Command doesnt exist at all! */
24 #define CMD_LOCALONLY CMD_FAILURE
25 };
26
27 /** Translation types for translation of parameters to UIDs.
28  * This allows the core commands to not have to be aware of how UIDs
29  * work (making it still possible to write other linking modules which
30  * do not use UID (but why would you want to?)
31  */
32 enum TranslateType
33 {
34         TR_END,                 /* End of known parameters, everything after this is TR_TEXT */
35         TR_TEXT,                /* Raw text, leave as-is */
36         TR_NICK,                /* Nickname, translate to UUID for server->server */
37         TR_CUSTOM               /* Custom translation handled by EncodeParameter/DecodeParameter */
38 };
39
40 /** Routing types for a command. Any command which is created defaults
41  * to having its command broadcasted on success. This behaviour may be
42  * overridden to one of the route types shown below (see the #defines
43  * below for more information on each one's behaviour)
44  */
45 enum RouteType
46 {
47         ROUTE_TYPE_LOCALONLY,
48         ROUTE_TYPE_BROADCAST,
49         ROUTE_TYPE_UNICAST,
50         ROUTE_TYPE_OPT_BCAST,
51         ROUTE_TYPE_OPT_UCAST
52 };
53
54 /** Defines routing information for a command, containing a destination
55  * server id (if applicable) and a routing type from the enum above.
56  */
57 struct RouteDescriptor
58 {
59         /** Routing type from the enum above
60          */
61         const RouteType type;
62         /** For unicast, the destination server's name
63          */
64         const std::string serverdest;
65
66         /** Create a RouteDescriptor
67          */
68         RouteDescriptor(RouteType t, const std::string &d)
69                 : type(t), serverdest(d) { }
70 };
71
72 /** Do not route this command */
73 #define ROUTE_LOCALONLY (RouteDescriptor(ROUTE_TYPE_LOCALONLY, ""))
74 /** Route this command to all servers, fail if not understood */
75 #define ROUTE_BROADCAST (RouteDescriptor(ROUTE_TYPE_BROADCAST, ""))
76 /** Route this command to a single server (do nothing if own server name specified) */
77 #define ROUTE_UNICAST(x) (RouteDescriptor(ROUTE_TYPE_UNICAST, x))
78 /** Route this command to all servers wrapped via ENCAP, so ignored if not understood */
79 #define ROUTE_OPT_BCAST (RouteDescriptor(ROUTE_TYPE_OPT_BCAST, ""))
80 /** Route this command to a single server wrapped via ENCAP, so ignored if not understood */
81 #define ROUTE_OPT_UCAST(x) (RouteDescriptor(ROUTE_TYPE_OPT_UCAST, x))
82
83 /** A structure that defines a command. Every command available
84  * in InspIRCd must be defined as derived from Command.
85  */
86 class CoreExport Command : public Extensible
87 {
88  protected:
89         /** Owner/Creator object
90          */
91         InspIRCd* ServerInstance;
92  public:
93         /** Command name
94         */
95         std::string command;
96
97         /** Creator module, NULL for core commands */
98         Module* creator;
99
100         /** User flags needed to execute the command or 0
101          */
102         char flags_needed;
103
104         /** Minimum number of parameters command takes
105         */
106         const unsigned int min_params;
107
108         /** Maximum number of parameters command takes.
109          * This is used by the command parser to join extra parameters into one last param.
110          * If not set, no munging is done to this command.
111          */
112         const unsigned int max_params;
113
114         /** used by /stats m
115          */
116         long double use_count;
117
118         /** used by /stats m
119          */
120         long double total_bytes;
121
122         /** True if the command is disabled to non-opers
123          */
124         bool disabled;
125
126         /** True if the command can be issued before registering
127          */
128         bool works_before_reg;
129
130         /** Syntax string for the command, displayed if non-empty string.
131          * This takes place of the text in the 'not enough parameters' numeric.
132          */
133         std::string syntax;
134
135         /** Translation type list for possible parameters, used to tokenize
136          * parameters into UIDs and SIDs etc.
137          */
138         std::vector<TranslateType> translation;
139
140         /** How many seconds worth of penalty does this command have?
141          */
142         const int Penalty;
143
144         /** Create a new command.
145          * @param Instance Pointer to creator class
146          * @param cmd Command name. This must be UPPER CASE.
147          * @param flags User mode required to execute the command. May ONLY be one mode - it's a string to give warnings if people mix params up.
148          * For oper only commands, set this to 'o', otherwise use 0.
149          * @param minpara Minimum parameters required for the command.
150          * @param maxpara Maximum number of parameters this command may have - extra parameters will be tossed into one last space-seperated param.
151          * @param before_reg If this is set to true, the command will
152          * be allowed before the user is 'registered' (has sent USER,
153          * NICK, optionally PASS, and been resolved).
154          */
155         Command(InspIRCd* Instance, Module* me, const std::string &cmd, const char *flags, int minpara, bool before_reg = false, int penalty = 1) :
156                 ServerInstance(Instance), command(cmd), creator(me), flags_needed(flags ? *flags : 0),
157                 min_params(minpara), max_params(0), disabled(false), works_before_reg(before_reg), Penalty(penalty)
158         {
159                 use_count = 0;
160                 total_bytes = 0;
161         }
162
163         Command(InspIRCd* Instance, Module* me, const std::string &cmd, const char *flags, int minpara, int maxpara, bool before_reg = false, int penalty = 1) :
164                 ServerInstance(Instance), command(cmd), creator(me), flags_needed(flags ? *flags : 0),
165                 min_params(minpara), max_params(maxpara), disabled(false), works_before_reg(before_reg), Penalty(penalty)
166         {
167                 use_count = 0;
168                 total_bytes = 0;
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          * If the command succeeds but should remain local to this server,
176          * return CMD_LOCALONLY.
177          */
178         virtual CmdResult Handle(const std::vector<std::string>& parameters, User* user) = 0;
179
180         virtual RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
181         {
182                 return ROUTE_BROADCAST;
183         }
184
185         /** Handle an internal request from another command, the core, or a module
186          * @param Command ID
187          * @param Zero or more parameters, whos form is specified by the command ID.
188          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
189          * If the command succeeds but should remain local to this server,
190          * return CMD_LOCALONLY.
191          */
192         virtual CmdResult HandleInternal(const unsigned int /* id */, const std::deque<classbase*>& /* params */)
193         {
194                 return CMD_INVALID;
195         }
196
197         /** Handle the command from a server.
198          * Not currently used in this version of InspIRCd.
199          * @param parameters The parameters given
200          * @param servername The server name which issued the command
201          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
202          * If the command succeeds but should remain local to this server,
203          * return CMD_LOCALONLY.
204          */
205         virtual CmdResult HandleServer(const std::vector<std::string>& /* parameters */, const std::string& /* servername */)
206         {
207                 return CMD_INVALID;
208         }
209
210         /** Encode a parameter for server->server transmission.
211          * Used for parameters for which the translation type is TR_CUSTOM.
212          * @param parameter The parameter to encode. Can be modified in place.
213          * @param index The parameter index (0 == first parameter).
214          */
215         virtual void EncodeParameter(std::string& parameter, int index)
216         {
217         }
218
219         /** Decode a parameter from server->server transmission.
220          * Not currently used in this version of InspIRCd.
221          * Used for parameters for which the translation type is TR_CUSTOM.
222          * @param parameter The parameter to decode. Can be modified in place.
223          * @param index The parameter index (0 == first parameter).
224          */
225         virtual void DecodeParameter(std::string& parameter, int index)
226         {
227         }
228
229         /** Disable or enable this command.
230          * @param setting True to disable the command.
231          */
232         void Disable(bool setting)
233         {
234                 disabled = setting;
235         }
236
237         /** Obtain this command's disable state.
238          * @return true if the command is currently disabled
239          * (disabled commands can be used only by operators)
240          */
241         bool IsDisabled()
242         {
243                 return disabled;
244         }
245
246         /** @return true if the command works before registration.
247          */
248         bool WorksBeforeReg()
249         {
250                 return works_before_reg;
251         }
252
253         /** Standard constructor gubbins
254          */
255         virtual ~Command()
256         {
257                 syntax.clear();
258         }
259 };
260
261 /** A hash of commands used by the core
262  */
263 typedef nspace::hash_map<std::string,Command*> Commandtable;
264
265 /** Shortcut macros for defining translation lists
266  */
267 #define TRANSLATE1(x1)  translation.push_back(x1);
268 #define TRANSLATE2(x1,x2)  translation.push_back(x1);translation.push_back(x2);
269 #define TRANSLATE3(x1,x2,x3)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);
270 #define TRANSLATE4(x1,x2,x3,x4)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);
271 #define TRANSLATE5(x1,x2,x3,x4,x5)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
272         translation.push_back(x5);
273 #define TRANSLATE6(x1,x2,x3,x4,x5,x6)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
274         translation.push_back(x5);translation.push_back(x6);
275 #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);\
276         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);
277 #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);\
278         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);translation.push_back(x8);
279
280 #endif