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