]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/ctables.h
More comments, and remove some unused craq. Someone (that being me) once wrote a...
[user/henk/code/inspircd.git] / include / ctables.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/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_NICKLIST,            /* Comma seperated nickname list, translate to UUIDs */
37         TR_SPACENICKLIST,       /* Space seperated nickname list, translate to UUIDs */
38         TR_CUSTOM               /* Custom translation handled by EncodeParameter/DecodeParameter */
39 };
40
41 /** For commands which should not be replicated to other
42  * servers, we usually return CMD_FAILURE. this isnt readable,
43  * so we define this alias for CMD_FAILURE called
44  * CMD_LOCALONLY, which of course does the same thing but is
45  * much more readable.
46  */
47 #define CMD_LOCALONLY CMD_FAILURE
48
49
50 /** A structure that defines a command. Every command available
51  * in InspIRCd must be defined as derived from Command.
52  */
53 class CoreExport Command : public Extensible
54 {
55  protected:
56         /** Owner/Creator object
57          */
58         InspIRCd* ServerInstance;
59  public:
60         /** Command name
61         */
62          std::string command;
63         /** User flags needed to execute the command or 0
64          */
65         char flags_needed;
66         /** Minimum number of parameters command takes
67         */
68         unsigned int min_params;
69         /** used by /stats m
70          */
71         long double use_count;
72         /** used by /stats m
73          */
74         long double total_bytes;
75         /** used for resource tracking between modules
76          */
77         std::string source;
78         /** True if the command is disabled to non-opers
79          */
80         bool disabled;
81         /** True if the command can be issued before registering
82          */
83         bool works_before_reg;
84         /** Syntax string for the command, displayed if non-empty string.
85          * This takes place of the text in the 'not enough parameters' numeric.
86          */
87         std::string syntax;
88
89         /** Translation type list for possible parameters, used to tokenize
90          * parameters into UIDs and SIDs etc.
91          */
92         std::vector<TranslateType> translation;
93
94         /** How many seconds worth of penalty does this command have?
95          */
96         const int Penalty;
97
98         /** Create a new command.
99          * @param Instance Pointer to creator class
100          * @param cmd Command name. This must be UPPER CASE.
101          * @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.
102          * For oper only commands, set this to 'o', otherwise use 0.
103          * @param minpara Minimum parameters required for the command.
104          * @param before_reg If this is set to true, the command will
105          * be allowed before the user is 'registered' (has sent USER,
106          * NICK, optionally PASS, and been resolved).
107          */
108         Command(InspIRCd* Instance, const std::string &cmd, const char *flags, int minpara, int before_reg = false, int penalty = 1) :  ServerInstance(Instance), command(cmd), flags_needed(flags ? *flags : 0), min_params(minpara), disabled(false), works_before_reg(before_reg),   Penalty(penalty)
109         {
110                 use_count = 0;
111                 total_bytes = 0;
112                 source = "<core>";
113                 syntax = "";
114                 translation.clear();
115         }
116
117         /** Handle the command from a user.
118          * @param parameters The parameters for the command.
119          * @param user The user who issued the command.
120          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
121          * If the command succeeds but should remain local to this server,
122          * return CMD_LOCALONLY.
123          */
124         virtual CmdResult Handle(const std::vector<std::string>& parameters, User* user) = 0;
125
126         /** Handle an internal request from another command, the core, or a module
127          * @param Command ID
128          * @param Zero or more parameters, whos form is specified by the command ID.
129          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
130          * If the command succeeds but should remain local to this server,
131          * return CMD_LOCALONLY.
132          */
133         virtual CmdResult HandleInternal(const unsigned int /* id */, const std::deque<classbase*>& /* params */)
134         {
135                 return CMD_INVALID;
136         }
137
138         /** Handle the command from a server.
139          * Not currently used in this version of InspIRCd.
140          * @param parameters The parameters given
141          * @param servername The server name which 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 HandleServer(const std::vector<std::string>& /* parameters */, const std::string& /* servername */)
147         {
148                 return CMD_INVALID;
149         }
150
151         /** Encode a parameter for server->server transmission.
152          * Used for parameters for which the translation type is TR_CUSTOM.
153          * @param parameter The parameter to encode. Can be modified in place.
154          * @param index The parameter index (0 == first parameter).
155          */
156         virtual void EncodeParameter(std::string& parameter, int index)
157         {
158         }
159
160         /** Decode a parameter from server->server transmission.
161          * Not currently used in this version of InspIRCd.
162          * Used for parameters for which the translation type is TR_CUSTOM.
163          * @param parameter The parameter to decode. Can be modified in place.
164          * @param index The parameter index (0 == first parameter).
165          */
166         virtual void DecodeParameter(std::string& parameter, int index)
167         {
168         }
169
170         /** Disable or enable this command.
171          * @param setting True to disable the command.
172          */
173         void Disable(bool setting)
174         {
175                 disabled = setting;
176         }
177
178         /** Obtain this command's disable state.
179          * @return true if the command is currently disabled
180          * (disabled commands can be used only by operators)
181          */
182         bool IsDisabled()
183         {
184                 return disabled;
185         }
186
187         /** @return true if the command works before registration.
188          */
189         bool WorksBeforeReg()
190         {
191                 return works_before_reg;
192         }
193
194         /** Standard constructor gubbins
195          */
196         virtual ~Command()
197         {
198                 syntax.clear();
199         }
200 };
201
202 /** A hash of commands used by the core
203  */
204 typedef nspace::hash_map<std::string,Command*> Commandtable;
205
206 /** Shortcut macros for defining translation lists
207  */
208 #define TRANSLATE1(x1)  translation.push_back(x1);
209 #define TRANSLATE2(x1,x2)  translation.push_back(x1);translation.push_back(x2);
210 #define TRANSLATE3(x1,x2,x3)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);
211 #define TRANSLATE4(x1,x2,x3,x4)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);
212 #define TRANSLATE5(x1,x2,x3,x4,x5)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
213         translation.push_back(x5);
214 #define TRANSLATE6(x1,x2,x3,x4,x5,x6)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
215         translation.push_back(x5);translation.push_back(x6);
216 #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);\
217         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);
218 #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);\
219         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);translation.push_back(x8);
220
221 #endif
222