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