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