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