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