]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/ctables.h
Allow commands to optionally route themselves using ENCAP
[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 enum RouteType
41 {
42         ROUTE_TYPE_LOCALONLY,
43         ROUTE_TYPE_BROADCAST,
44         ROUTE_TYPE_UNICAST,
45         ROUTE_TYPE_OPT_BCAST,
46         ROUTE_TYPE_OPT_UCAST
47 };
48
49 struct RouteDescriptor
50 {
51         const RouteType type;
52         /** For unicast, the destination server's name
53          */
54         const std::string serverdest;
55         RouteDescriptor(RouteType t, const std::string d)
56                 : type(t), serverdest(d) { }
57 };
58
59 /** Do not route this command */
60 #define ROUTE_LOCALONLY (RouteDescriptor(ROUTE_TYPE_LOCALONLY, ""))
61 /** Route this command to all servers, fail if not understood */
62 #define ROUTE_BROADCAST (RouteDescriptor(ROUTE_TYPE_BROADCAST, ""))
63 /** Route this command to a single server (do nothing if own server name specified) */
64 #define ROUTE_UNICAST(x) (RouteDescriptor(ROUTE_TYPE_UNICAST, x))
65 /** Route this command to all servers, ignore if not understood */
66 #define ROUTE_OPT_BCAST (RouteDescriptor(ROUTE_TYPE_OPT_BCAST, ""))
67 /** Route this command to a single server, ignore if not understood */
68 #define ROUTE_OPT_UCAST(x) (RouteDescriptor(ROUTE_TYPE_OPT_UCAST, x))
69
70 /** A structure that defines a command. Every command available
71  * in InspIRCd must be defined as derived from Command.
72  */
73 class CoreExport Command : public Extensible
74 {
75  protected:
76         /** Owner/Creator object
77          */
78         InspIRCd* ServerInstance;
79  public:
80         /** Command name
81         */
82          std::string command;
83
84         /** User flags needed to execute the command or 0
85          */
86         char flags_needed;
87
88         /** Minimum number of parameters command takes
89         */
90         const unsigned int min_params;
91
92         /** Maximum number of parameters command takes.
93          * This is used by the command parser to join extra parameters into one last param.
94          * If not set, no munging is done to this command.
95          */
96         const unsigned int max_params;
97
98         /** used by /stats m
99          */
100         long double use_count;
101
102         /** used by /stats m
103          */
104         long double total_bytes;
105
106         /** used for resource tracking between modules
107          */
108         std::string source;
109
110         /** True if the command is disabled to non-opers
111          */
112         bool disabled;
113
114         /** True if the command can be issued before registering
115          */
116         bool works_before_reg;
117
118         /** Syntax string for the command, displayed if non-empty string.
119          * This takes place of the text in the 'not enough parameters' numeric.
120          */
121         std::string syntax;
122
123         /** Translation type list for possible parameters, used to tokenize
124          * parameters into UIDs and SIDs etc.
125          */
126         std::vector<TranslateType> translation;
127
128         /** How many seconds worth of penalty does this command have?
129          */
130         const int Penalty;
131
132         /** Create a new command.
133          * @param Instance Pointer to creator class
134          * @param cmd Command name. This must be UPPER CASE.
135          * @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.
136          * For oper only commands, set this to 'o', otherwise use 0.
137          * @param minpara Minimum parameters required for the command.
138          * @param maxpara Maximum number of parameters this command may have - extra parameters will be tossed into one last space-seperated param.
139          * @param before_reg If this is set to true, the command will
140          * be allowed before the user is 'registered' (has sent USER,
141          * NICK, optionally PASS, and been resolved).
142          */
143         Command(InspIRCd* Instance, const std::string &cmd, const char *flags, int minpara, bool before_reg = false, int penalty = 1) :         ServerInstance(Instance), command(cmd), flags_needed(flags ? *flags : 0), min_params(minpara), max_params(0), disabled(false), works_before_reg(before_reg), Penalty(penalty)
144         {
145                 use_count = 0;
146                 total_bytes = 0;
147                 source = "<core>";
148                 syntax = "";
149                 translation.clear();
150         }
151
152         Command(InspIRCd* Instance, const std::string &cmd, const char *flags, int minpara, int maxpara, bool before_reg = false, int penalty = 1) :    ServerInstance(Instance), command(cmd), flags_needed(flags ? *flags : 0), min_params(minpara), max_params(maxpara), disabled(false), works_before_reg(before_reg), Penalty(penalty)
153         {
154                 use_count = 0;
155                 total_bytes = 0;
156                 source = "<core>";
157                 syntax = "";
158                 translation.clear();
159         }
160
161         /** Handle the command from a user.
162          * @param parameters The parameters for the command.
163          * @param user The user who issued the command.
164          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
165          * If the command succeeds but should remain local to this server,
166          * return CMD_LOCALONLY.
167          */
168         virtual CmdResult Handle(const std::vector<std::string>& parameters, User* user) = 0;
169
170         virtual RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
171         {
172                 return ROUTE_BROADCAST;
173         }
174
175         /** Handle an internal request from another command, the core, or a module
176          * @param Command ID
177          * @param Zero or more parameters, whos form is specified by the command ID.
178          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
179          * If the command succeeds but should remain local to this server,
180          * return CMD_LOCALONLY.
181          */
182         virtual CmdResult HandleInternal(const unsigned int /* id */, const std::deque<classbase*>& /* params */)
183         {
184                 return CMD_INVALID;
185         }
186
187         /** Handle the command from a server.
188          * Not currently used in this version of InspIRCd.
189          * @param parameters The parameters given
190          * @param servername The server name which issued the command
191          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
192          * If the command succeeds but should remain local to this server,
193          * return CMD_LOCALONLY.
194          */
195         virtual CmdResult HandleServer(const std::vector<std::string>& /* parameters */, const std::string& /* servername */)
196         {
197                 return CMD_INVALID;
198         }
199
200         /** Encode a parameter for server->server transmission.
201          * Used for parameters for which the translation type is TR_CUSTOM.
202          * @param parameter The parameter to encode. Can be modified in place.
203          * @param index The parameter index (0 == first parameter).
204          */
205         virtual void EncodeParameter(std::string& parameter, int index)
206         {
207         }
208
209         /** Decode a parameter from server->server transmission.
210          * Not currently used in this version of InspIRCd.
211          * Used for parameters for which the translation type is TR_CUSTOM.
212          * @param parameter The parameter to decode. Can be modified in place.
213          * @param index The parameter index (0 == first parameter).
214          */
215         virtual void DecodeParameter(std::string& parameter, int index)
216         {
217         }
218
219         /** Disable or enable this command.
220          * @param setting True to disable the command.
221          */
222         void Disable(bool setting)
223         {
224                 disabled = setting;
225         }
226
227         /** Obtain this command's disable state.
228          * @return true if the command is currently disabled
229          * (disabled commands can be used only by operators)
230          */
231         bool IsDisabled()
232         {
233                 return disabled;
234         }
235
236         /** @return true if the command works before registration.
237          */
238         bool WorksBeforeReg()
239         {
240                 return works_before_reg;
241         }
242
243         /** Standard constructor gubbins
244          */
245         virtual ~Command()
246         {
247                 syntax.clear();
248         }
249 };
250
251 /** A hash of commands used by the core
252  */
253 typedef nspace::hash_map<std::string,Command*> Commandtable;
254
255 /** Shortcut macros for defining translation lists
256  */
257 #define TRANSLATE1(x1)  translation.push_back(x1);
258 #define TRANSLATE2(x1,x2)  translation.push_back(x1);translation.push_back(x2);
259 #define TRANSLATE3(x1,x2,x3)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);
260 #define TRANSLATE4(x1,x2,x3,x4)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);
261 #define TRANSLATE5(x1,x2,x3,x4,x5)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
262         translation.push_back(x5);
263 #define TRANSLATE6(x1,x2,x3,x4,x5,x6)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
264         translation.push_back(x5);translation.push_back(x6);
265 #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);\
266         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);
267 #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);\
268         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);translation.push_back(x8);
269
270 #endif