]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/ctables.h
e8a3337fd2b6e23959ff7988931a7835bd2cd9ac
[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 userrec;
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 /** 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_t.
47  */
48 class CoreExport command_t : 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         int min_params;
64         /** used by /stats m
65          */
66         long use_count;
67         /** used by /stats m
68          */
69         float 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         /** Create a new command.
85          * @param Instance Pointer to creator class
86          * @param cmd Command name. This must be UPPER CASE.
87          * @param flags User modes required to execute the command.
88          * For oper only commands, set this to 'o', otherwise use 0.
89          * @param minpara Minimum parameters required for the command.
90          * @param before_reg If this is set to true, the command will
91          * be allowed before the user is 'registered' (has sent USER,
92          * NICK, optionally PASS, and been resolved).
93          */
94         command_t(InspIRCd* Instance, const std::string &cmd, char flags, int minpara, int before_reg = false) : ServerInstance(Instance), command(cmd), flags_needed(flags), min_params(minpara), disabled(false), works_before_reg(before_reg)
95         {
96                 use_count = 0;
97                 total_bytes = 0;
98                 source = "<core>";
99                 syntax = "";
100         }
101
102         /** Handle the command from a user.
103          * @param parameters The parameters for the command.
104          * @param pcnt The number of parameters available in 'parameters'
105          * @param user The user who issued the command.
106          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
107          * If the command succeeds but should remain local to this server,
108          * return CMD_LOCALONLY.
109          */
110         virtual CmdResult Handle(const char** parameters, int pcnt, userrec* user) = 0;
111
112         /** Handle an internal request from another command, the core, or a module
113          * @param Command ID
114          * @param Zero or more parameters, whos form is specified by the command ID.
115          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
116          * If the command succeeds but should remain local to this server,
117          * return CMD_LOCALONLY.
118          */
119         virtual CmdResult HandleInternal(const unsigned int id, const std::deque<classbase*> &params)
120         {
121                 return CMD_INVALID;
122         }
123
124         /** Handle the command from a server.
125          * Not currently used in this version of InspIRCd.
126          * @param parameters The parameters given
127          * @param pcnt The number of parameters available
128          * @param servername The server name which issued the command
129          * @return Return CMD_SUCCESS on success, or CMD_FAILURE on failure.
130          * If the command succeeds but should remain local to this server,
131          * return CMD_LOCALONLY.
132          */
133         virtual CmdResult HandleServer(const char** parameters, int pcnt, const std::string &servername)
134         {
135                 return CMD_INVALID;
136         }
137
138         /** Disable or enable this command.
139          * @param setting True to disable the command.
140          */
141         void Disable(bool setting)
142         {
143                 disabled = setting;
144         }
145
146         /** Obtain this command's disable state.
147          * @return true if the command is currently disabled
148          * (disabled commands can be used only by operators)
149          */
150         bool IsDisabled()
151         {
152                 return disabled;
153         }
154
155         /** @return true if the command works before registration.
156          */
157         bool WorksBeforeReg()
158         {
159                 return works_before_reg;
160         }
161
162         /** Standard constructor gubbins
163          */
164         virtual ~command_t() {}
165 };
166
167 /** A hash of commands used by the core
168  */
169 typedef nspace::hash_map<std::string,command_t*> command_table;
170
171 #endif
172