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