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