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