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