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