]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/command_parse.h
Change to virtual
[user/henk/code/inspircd.git] / include / command_parse.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 __COMMAND_PARSE_H
15 #define __COMMAND_PARSE_H
16
17 #include <string>
18 #include "users.h"
19 #include "ctables.h"
20 #include "typedefs.h"
21
22 /** Required forward declaration
23  */
24 class InspIRCd;
25
26 /** A list of dll/so files containing the command handlers for the core
27  */
28 typedef std::map<std::string, void*> SharedObjectList;
29
30 /** This class handles command management and parsing.
31  * It allows you to add and remove commands from the map,
32  * call command handlers by name, and chop up comma seperated
33  * parameters into multiple calls.
34  */
35 class CoreExport CommandParser : public classbase
36 {
37  private:
38         /** The creator of this class
39          */
40         InspIRCd* ServerInstance;
41
42         /** Parameter buffer
43          */
44         std::vector<std::string> para;
45
46         /** Process a parameter string into a list of items
47          * @param command_p The output list of items
48          * @param parameters The input string
49          * @return The number of parameters parsed into command_p
50          */
51         int ProcessParameters(char **command_p,char *parameters);
52
53         /** Process a command from a user.
54          * @param user The user to parse the command for
55          * @param cmd The command string to process
56          */
57         void ProcessCommand(userrec *user, std::string &cmd);
58
59         /** Finds the init_command symbol in a .so file
60          * @param v A function pointer to be initialized
61          * @param h A valid shared object handle
62          * @return True if the symbol could be found
63          */
64         bool FindSym(void** v, void* h);
65
66         /** A list of core-implemented modes and their shared object handles
67          */
68         SharedObjectList RFCCommands;
69
70         /** Load a command from a shared object on disk.
71          * @param name The shared object to load (without path)
72          * @return NULL on success, pointer to dlerrr() error message on failure
73          */
74         const char* LoadCommand(const char* name);
75
76         /** Removes a command if the sources match. Used as a helper for
77          *  safe hash_map delete while iter in RemoveCommands(const char* source).
78          */
79         void RemoveCommand(nspace::hash_map<std::string,command_t*>::iterator safei, const char* source);
80
81
82  public:
83         /** Command list, a hash_map of command names to command_t*
84          */
85         command_table cmdlist;
86
87         /** Reload a core command.
88          * This will only reload commands implemented by the core,
89          * to reload a modular command, you must reload that module.
90          * @param cmd The command to reload. This will cause the shared
91          * object which implements this command to be closed, and then reloaded.
92          * @return True if the command was reloaded, false if it could not be found
93          * or another error occured
94          */
95         bool ReloadCommand(const char* cmd, userrec* user);
96
97         /** Default constructor.
98          * @param Instance The creator of this class
99          */
100         CommandParser(InspIRCd* Instance);
101
102         /** Calls the handler for a given command.
103          * @param commandname The command to find. This should be in uppercase.
104          * @param parameters Parameter list as an array of array of char (that's not a typo).
105          * @param pcnt The number of items in the parameters list
106          * @param user The user to call the handler on behalf of
107          * @return This method will return CMD_SUCCESS if the command handler was found and called,
108          * and the command completeld successfully. It will return CMD_FAILURE if the command handler was found
109          * and called, but the command did not complete successfully, and it will return CMD_INVALID if the
110          * command simply did not exist at all or the wrong number of parameters were given, or the user
111          * was not privilaged enough to execute the command.
112          */
113         CmdResult CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user);
114
115         /** Get the handler function for a command.
116          * @param commandname The command required. Always use uppercase for this parameter.
117          * @return a pointer to the command handler, or NULL
118          */
119         command_t* GetHandler(const std::string &commandname);
120
121         /** This function returns true if a command is valid with the given number of parameters and user.
122          * @param commandname The command name to check
123          * @param pcnt The parameter count
124          * @param user The user to check against
125          * @return If the user given has permission to execute the command, and the parameter count is
126          * equal to or greater than the minimum number of parameters to the given command, then this
127          * function will return true, otherwise it will return false.
128          */
129         bool IsValidCommand(const std::string &commandname, int pcnt, userrec * user);
130         
131         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
132          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
133          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
134          * the channel names and their keys as follows:
135          *
136          * JOIN #chan1,#chan2,#chan3 key1,,key3
137          *
138          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
139          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
140          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
141          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
142          *
143          * @param user The user who sent the command
144          * @param CommandObj the command object to call for each parameter in the list
145          * @param parameters Parameter list as an array of array of char (that's not a typo).
146          * @param The number of items in the parameters list
147          * @param splithere The first parameter index to split as a comma seperated list
148          * @param extra The second parameter index to split as a comma seperated list
149          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
150          * caller should return without doing anything, otherwise it should continue into its main section of code.
151          */
152         int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra);
153
154         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
155          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
156          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
157          * the channel names and their keys as follows:
158          *
159          * JOIN #chan1,#chan2,#chan3 key1,,key3
160          *
161          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
162          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
163          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
164          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
165          *
166          * @param user The user who sent the command
167          * @param CommandObj the command object to call for each parameter in the list
168          * @param parameters Parameter list as an array of array of char (that's not a typo).
169          * @param The number of items in the parameters list
170          * @param splithere The first parameter index to split as a comma seperated list
171          * @param extra The second parameter index to split as a comma seperated list
172          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
173          * caller should return without doing anything, otherwise it should continue into its main section of code.
174          */
175         int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere);
176
177         /** Take a raw input buffer from a recvq, and process it on behalf of a user.
178          * @param buffer The buffer line to process
179          * @param user The user to whom this line belongs
180          */
181         void ProcessBuffer(std::string &buffer,userrec *user);
182
183         /** Remove all commands relating to module 'source'.
184          * @param source A module name which has introduced new commands
185          * @return True This function returns true if commands were removed
186          */
187         bool RemoveCommands(const char* source);
188
189         /** Add a new command to the commands hash
190          * @param f The new command_t to add to the list
191          * @param so_handle The handle to the shared object where the command can be found.
192          * Only core commands loaded via cmd_*.so files should set this parameter to anything
193          * meaningful. Module authors should leave this parameter at its default of NULL.
194          * @return True if the command was added
195          */
196         bool CreateCommand(command_t *f, void* so_handle = NULL);
197
198         /** Insert the default RFC1459 commands into the command hash.
199          * Ignore any already loaded commands.
200          * @param user User to spool errors to, or if NULL, when an error occurs spool the errors to
201          * stdout then exit with EXIT_STATUS_HANDLER.
202          */
203         void SetupCommandTable(userrec* user);
204 };
205
206 /** Command handler class for the RELOAD command.
207  * A command cant really reload itself, so this has to be in here.
208  */
209 class cmd_reload : public command_t
210 {
211  public:
212         /** Standard constructor
213          */
214         cmd_reload (InspIRCd* Instance) : command_t(Instance,"RELOAD",'o',1) { syntax = "<core-command>"; }
215         /** Handle RELOAD
216          */
217         CmdResult Handle(const char** parameters, int pcnt, userrec *user);
218 };
219
220 /** A lookup table of values for multiplier characters used by
221  * InspIRCd::Duration(). In this lookup table, the indexes for
222  * the ascii values 'm' and 'M' have the value '60', the indexes
223  * for the ascii values 'D' and 'd' have a value of '86400', etc.
224  */
225 const int duration_multi[] =
226 {
227         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
228         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
229         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
230         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
231         1, 1, 1, 1, 1, 1, 1, 1, 86400, 1, 1, 1, 3600,
232         1, 1, 1, 1, 60, 1, 1, 1, 1, 1, 1, 1, 1, 1,
233         604800, 1, 31536000, 1, 1, 1, 1, 1, 1, 1, 1,
234         1, 1, 86400, 1, 1, 1, 3600, 1, 1, 1, 1, 60,
235         1, 1, 1, 1, 1, 1, 1, 1, 1, 604800, 1, 31536000,
236         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
237         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
238         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
239         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
240         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
241         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
242         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
243         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
244         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
245 };
246
247 #endif
248