]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/command_parse.h
Someone forgot support for CIDR here
[user/henk/code/inspircd.git] / include / command_parse.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 #ifndef __COMMAND_PARSE_H
18 #define __COMMAND_PARSE_H
19
20 #include <string>
21 #include "users.h"
22 #include "ctables.h"
23 #include "typedefs.h"
24
25 class InspIRCd;
26
27 typedef std::map<std::string, void*> SharedObjectList;
28
29 /** This class handles command management and parsing.
30  * It allows you to add and remove commands from the map,
31  * call command handlers by name, and chop up comma seperated
32  * parameters into multiple calls.
33  */
34 class CommandParser : public classbase
35 {
36  private:
37         /** The creator of this class
38          */
39         InspIRCd* ServerInstance;
40
41         /** Parameter buffer
42          */
43         std::vector<std::string> para;
44
45         /** Process a parameter string into a list of items
46          * @param command_p The output list of items
47          * @param parameters The input string
48          * @return The number of parameters parsed into command_p
49          */
50         int ProcessParameters(char **command_p,char *parameters);
51
52         /** Process a command from a user.
53          * @param user The user to parse the command for
54          * @param cmd The command string to process
55          */
56         void ProcessCommand(userrec *user, std::string &cmd);
57
58         /** Insert the default RFC1459 commands into the command hash.
59          */
60         void SetupCommandTable();
61
62         /** Finds the init_command symbol in a .so file
63          * @param v A function pointer to be initialized
64          * @param h A valid shared object handle
65          * @return True if the symbol could be found
66          */
67         bool FindSym(void** v, void* h);
68
69         /** A list of core-implemented modes and their shared object handles
70          */
71         SharedObjectList RFCCommands;
72
73         /** Load a command from a shared object on disk.
74          * @param name The shared object to load (without path)
75          */
76         void LoadCommand(const char* name);
77
78  public:
79         /** Command list, a hash_map of command names to command_t*
80          */
81         command_table cmdlist;
82
83         /** Reload a core command.
84          * This will only reload commands implemented by the core,
85          * to reload a modular command, you must reload that module.
86          * @param cmd The command to reload. This will cause the shared
87          * object which implements this command to be closed, and then reloaded.
88          * @return True if the command was reloaded, false if it could not be found
89          * or another error occured
90          */
91         bool ReloadCommand(const char* cmd);
92
93         /** Default constructor.
94          * @param Instance The creator of this class
95          */
96         CommandParser(InspIRCd* Instance);
97
98         /** Calls the handler for a given command.
99          * @param commandname The command to find. This should be in uppercase.
100          * @param parameters Parameter list as an array of array of char (that's not a typo).
101          * @param pcnt The number of items in the parameters list
102          * @param user The user to call the handler on behalf of
103          * @return This method will return CMD_SUCCESS if the command handler was found and called,
104          * and the command completeld successfully. It will return CMD_FAILURE if the command handler was found
105          * and called, but the command did not complete successfully, and it will return CMD_INVALID if the
106          * command simply did not exist at all or the wrong number of parameters were given, or the user
107          * was not privilaged enough to execute the command.
108          */
109         CmdResult CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user);
110
111         /** This function returns true if a command is valid with the given number of parameters and user.
112          * @param commandname The command name to check
113          * @param pcnt The parameter count
114          * @param user The user to check against
115          * @return If the user given has permission to execute the command, and the parameter count is
116          * equal to or greater than the minimum number of parameters to the given command, then this
117          * function will return true, otherwise it will return false.
118          */
119         bool IsValidCommand(const std::string &commandname, int pcnt, userrec * user);
120         
121         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
122          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
123          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
124          * the channel names and their keys as follows:
125          *
126          * JOIN #chan1,#chan2,#chan3 key1,,key3
127          *
128          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
129          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
130          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
131          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
132          *
133          * @param user The user who sent the command
134          * @param CommandObj the command object to call for each parameter in the list
135          * @param parameters Parameter list as an array of array of char (that's not a typo).
136          * @param The number of items in the parameters list
137          * @param splithere The first parameter index to split as a comma seperated list
138          * @param extra The second parameter index to split as a comma seperated list
139          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
140          * caller should return without doing anything, otherwise it should continue into its main section of code.
141          */
142         int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra);
143
144         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
145          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
146          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
147          * the channel names and their keys as follows:
148          *
149          * JOIN #chan1,#chan2,#chan3 key1,,key3
150          *
151          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
152          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
153          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
154          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
155          *
156          * @param user The user who sent the command
157          * @param CommandObj the command object to call for each parameter in the list
158          * @param parameters Parameter list as an array of array of char (that's not a typo).
159          * @param The number of items in the parameters list
160          * @param splithere The first parameter index to split as a comma seperated list
161          * @param extra The second parameter index to split as a comma seperated list
162          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
163          * caller should return without doing anything, otherwise it should continue into its main section of code.
164          */
165         int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere);
166
167         /** Take a raw input buffer from a recvq, and process it on behalf of a user.
168          * @param buffer The buffer line to process
169          * @param user The user to whom this line belongs
170          */
171         void ProcessBuffer(std::string &buffer,userrec *user);
172
173         /** Remove all commands relating to module 'source'.
174          * @param source A module name which has introduced new commands
175          * @return True This function returns true if commands were removed
176          */
177         bool RemoveCommands(const char* source);
178
179         /** Add a new command to the commands hash
180          * @param f The new command_t to add to the list
181          * @param so_handle The handle to the shared object where the command can be found.
182          * Only core commands loaded via cmd_*.so files should set this parameter to anything
183          * meaningful. Module authors should leave this parameter at its default of NULL.
184          * @return True if the command was added
185          */
186         bool CreateCommand(command_t *f, void* so_handle = NULL);
187 };
188
189 /** Command handler class for the RELOAD command.
190  * A command cant really reload itself, so this has to be in here.
191  */
192 class cmd_reload : public command_t
193 {
194  public:
195         /** Standard constructor
196          */
197         cmd_reload (InspIRCd* Instance) : command_t(Instance,"RELOAD",'o',1) { syntax = "<core-command>"; }
198         /** Handle RELOAD
199          */
200         CmdResult Handle(const char** parameters, int pcnt, userrec *user);
201 };
202
203
204
205 #endif