]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/ctables.h
Wheee, mass commit! this adds const stafety, throwing a compile error if anyone does...
[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 };
34
35 /** For commands which should not be replicated to other
36  * servers, we usually return CMD_FAILURE. this isnt readable,
37  * so we define this alias for CMD_FAILURE called
38  * CMD_LOCALONLY, which of course does the same thing but is
39  * much more readable.
40  */
41 #define CMD_LOCALONLY CMD_FAILURE
42
43
44 /** A structure that defines a command. Every command available
45  * in InspIRCd must be defined as derived from Command.
46  */
47 class CoreExport Command : public Extensible
48 {
49  protected:
50         /** Owner/Creator object
51          */
52         InspIRCd* ServerInstance;
53  public:
54         /** Command name
55         */
56          std::string command;
57         /** User flags needed to execute the command or 0
58          */
59         char flags_needed;
60         /** Minimum number of parameters command takes
61         */
62         int min_params;
63         /** used by /stats m
64          */
65         long double use_count;
66         /** used by /stats m
67          */
68         long double total_bytes;
69         /** used for resource tracking between modules
70          */
71         std::string source;
72         /** True if the command is disabled to non-opers
73          */
74         bool disabled;
75         /** True if the command can be issued before registering
76          */
77         bool works_before_reg;
78         /** Syntax string for the command, displayed if non-empty string.
79          * This takes place of the text in the 'not enough parameters' numeric.
80          */
81         std::string syntax;
82
83         std::vector<TranslateType> translation;
84
85         /** How many seconds worth of penalty does this command have?
86          */
87         const int Penalty;
88
89         /** Create a new command.
90          * @param Instance Pointer to creator class
91          * @param cmd Command name. This must be UPPER CASE.
92          * @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.
93          * For oper only commands, set this to 'o', otherwise use 0.
94          * @param minpara Minimum parameters required for the command.
95          * @param before_reg If this is set to true, the command will
96          * be allowed before the user is 'registered' (has sent USER,
97          * NICK, optionally PASS, and been resolved).
98          */
99         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)
100         {
101                 use_count = 0;
102                 total_bytes = 0;
103                 source = "<core>";
104                 syntax = "";
105                 translation.clear();
106         }
107
108         /** Handle the command from a user.
109          * @param parameters The parameters for the command.
110          * @param pcnt The number of parameters available in 'parameters'
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 char* const* parameters, int pcnt, 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 pcnt The number of parameters available
134          * @param servername The server name which issued the command
135          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
136          * If the command succeeds but should remain local to this server,
137          * return CMD_LOCALONLY.
138          */
139         virtual CmdResult HandleServer(const char* const* /* parameters */, int /* pcnt */, const std::string& /* servername */)
140         {
141                 return CMD_INVALID;
142         }
143
144         /** Disable or enable this command.
145          * @param setting True to disable the command.
146          */
147         void Disable(bool setting)
148         {
149                 disabled = setting;
150         }
151
152         /** Obtain this command's disable state.
153          * @return true if the command is currently disabled
154          * (disabled commands can be used only by operators)
155          */
156         bool IsDisabled()
157         {
158                 return disabled;
159         }
160
161         /** @return true if the command works before registration.
162          */
163         bool WorksBeforeReg()
164         {
165                 return works_before_reg;
166         }
167
168         /** Standard constructor gubbins
169          */
170         virtual ~Command() {}
171 };
172
173 /** A hash of commands used by the core
174  */
175 typedef nspace::hash_map<std::string,Command*> Commandable;
176
177 #define TRANSLATE1(x1)  translation.push_back(x1);
178 #define TRANSLATE2(x1,x2)  translation.push_back(x1);translation.push_back(x2);
179 #define TRANSLATE3(x1,x2,x3)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);
180 #define TRANSLATE4(x1,x2,x3,x4)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);
181 #define TRANSLATE5(x1,x2,x3,x4,x5)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
182         translation.push_back(x5);
183 #define TRANSLATE6(x1,x2,x3,x4,x5,x6)  translation.push_back(x1);translation.push_back(x2);translation.push_back(x3);translation.push_back(x4);\
184         translation.push_back(x5);translation.push_back(x6);
185 #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);\
186         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);
187 #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);\
188         translation.push_back(x5);translation.push_back(x6);translation.push_back(x7);translation.push_back(x8);
189
190 #endif
191