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