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