]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/command_parse.h
5750416db1806208e408af97170db3935dd713ed
[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 class InspIRCd;
23
24 typedef std::map<std::string, void*> SharedObjectList;
25
26 /** This class handles command management and parsing.
27  * It allows you to add and remove commands from the map,
28  * call command handlers by name, and chop up comma seperated
29  * parameters into multiple calls.
30  */
31 class CommandParser : public classbase
32 {
33  private:
34         /** The creator of this class
35          */
36         InspIRCd* ServerInstance;
37
38         /** Parameter buffer
39          */
40         std::vector<std::string> para;
41
42         /** Process a parameter string into a list of items
43          * @param command_p The output list of items
44          * @param parameters The input string
45          * @return The number of parameters parsed into command_p
46          */
47         int ProcessParameters(char **command_p,char *parameters);
48
49         /** Process a command from a user.
50          * @param user The user to parse the command for
51          * @param cmd The command string to process
52          */
53         void ProcessCommand(userrec *user, std::string &cmd);
54
55         /** Insert the default RFC1459 commands into the command hash.
56          */
57         void SetupCommandTable();
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          */
73         void 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_t*>::iterator safei, const char* source);
79
80
81  public:
82         /** Command list, a hash_map of command names to command_t*
83          */
84         command_table 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);
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, userrec *user);
113
114         command_t* GetHandler(const std::string &commandname);
115
116         /** This function returns true if a command is valid with the given number of parameters and user.
117          * @param commandname The command name to check
118          * @param pcnt The parameter count
119          * @param user The user to check against
120          * @return If the user given has permission to execute the command, and the parameter count is
121          * equal to or greater than the minimum number of parameters to the given command, then this
122          * function will return true, otherwise it will return false.
123          */
124         bool IsValidCommand(const std::string &commandname, int pcnt, userrec * user);
125         
126         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
127          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
128          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
129          * the channel names and their keys as follows:
130          *
131          * JOIN #chan1,#chan2,#chan3 key1,,key3
132          *
133          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
134          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
135          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
136          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
137          *
138          * @param user The user who sent the command
139          * @param CommandObj the command object to call for each parameter in the list
140          * @param parameters Parameter list as an array of array of char (that's not a typo).
141          * @param The number of items in the parameters list
142          * @param splithere The first parameter index to split as a comma seperated list
143          * @param extra The second parameter index to split as a comma seperated list
144          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
145          * caller should return without doing anything, otherwise it should continue into its main section of code.
146          */
147         int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra);
148
149         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
150          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
151          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
152          * the channel names and their keys as follows:
153          *
154          * JOIN #chan1,#chan2,#chan3 key1,,key3
155          *
156          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
157          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
158          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
159          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
160          *
161          * @param user The user who sent the command
162          * @param CommandObj the command object to call for each parameter in the list
163          * @param parameters Parameter list as an array of array of char (that's not a typo).
164          * @param The number of items in the parameters list
165          * @param splithere The first parameter index to split as a comma seperated list
166          * @param extra The second parameter index to split as a comma seperated list
167          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
168          * caller should return without doing anything, otherwise it should continue into its main section of code.
169          */
170         int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere);
171
172         /** Take a raw input buffer from a recvq, and process it on behalf of a user.
173          * @param buffer The buffer line to process
174          * @param user The user to whom this line belongs
175          */
176         void ProcessBuffer(std::string &buffer,userrec *user);
177
178         /** Remove all commands relating to module 'source'.
179          * @param source A module name which has introduced new commands
180          * @return True This function returns true if commands were removed
181          */
182         bool RemoveCommands(const char* source);
183
184         /** Add a new command to the commands hash
185          * @param f The new command_t to add to the list
186          * @param so_handle The handle to the shared object where the command can be found.
187          * Only core commands loaded via cmd_*.so files should set this parameter to anything
188          * meaningful. Module authors should leave this parameter at its default of NULL.
189          * @return True if the command was added
190          */
191         bool CreateCommand(command_t *f, void* so_handle = NULL);
192 };
193
194 /** Command handler class for the RELOAD command.
195  * A command cant really reload itself, so this has to be in here.
196  */
197 class cmd_reload : public command_t
198 {
199  public:
200         /** Standard constructor
201          */
202         cmd_reload (InspIRCd* Instance) : command_t(Instance,"RELOAD",'o',1) { syntax = "<core-command>"; }
203         /** Handle RELOAD
204          */
205         CmdResult Handle(const char** parameters, int pcnt, userrec *user);
206 };
207
208
209
210 #endif