]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/command_parse.h
a0d27be174e9d514ff488dcf0a96ed56e06bebd5
[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         /** Process a parameter string into a list of items
42          * @param command_p The output list of items
43          * @param parameters The input string
44          * @return The number of parameters parsed into command_p
45          */
46         int ProcessParameters(char **command_p,char *parameters);
47
48         /** Process a command from a user.
49          * @param user The user to parse the command for
50          * @param cmd The command string to process
51          */
52         void ProcessCommand(userrec *user, std::string &cmd);
53
54         /** Insert the default RFC1459 commands into the command hash.
55          */
56         void SetupCommandTable();
57
58         /** Finds the init_command symbol in a .so file
59          * @param v A function pointer to be initialized
60          * @param h A valid shared object handle
61          * @return True if the symbol could be found
62          */
63         bool FindSym(void** v, void* h);
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          */
72         void LoadCommand(const char* name);
73
74  public:
75         /** Command list, a hash_map of command names to command_t*
76          */
77         command_table cmdlist;
78
79         /** Reload a core command.
80          * This will only reload commands implemented by the core,
81          * to reload a modular command, you must reload that module.
82          * @param cmd The command to reload. This will cause the shared
83          * object which implements this command to be closed, and then reloaded.
84          * @return True if the command was reloaded, false if it could not be found
85          * or another error occured
86          */
87         bool ReloadCommand(const char* cmd);
88
89         /** Default constructor.
90          * @param Instance The creator of this class
91          */
92         CommandParser(InspIRCd* Instance);
93
94         /** Calls the handler for a given command.
95          * @param commandname The command to find. This should be in uppercase.
96          * @param parameters Parameter list as an array of array of char (that's not a typo).
97          * @param pcnt The number of items in the parameters list
98          * @param user The user to call the handler on behalf of
99          * @return This method will return true if the command handler was found and called
100          */
101         bool CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user);
102
103         /** This function returns true if a command is valid with the given number of parameters and user.
104          * @param commandname The command name to check
105          * @param pcnt The parameter count
106          * @param user The user to check against
107          * @return If the user given has permission to execute the command, and the parameter count is
108          * equal to or greater than the minimum number of parameters to the given command, then this
109          * function will return true, otherwise it will return false.
110          */
111         bool IsValidCommand(const std::string &commandname, int pcnt, userrec * user);
112         
113         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
114          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
115          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
116          * the channel names and their keys as follows:
117          *
118          * JOIN #chan1,#chan2,#chan3 key1,,key3
119          *
120          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
121          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
122          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
123          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
124          *
125          * @param user The user who sent the command
126          * @param CommandObj the command object to call for each parameter in the list
127          * @param parameters Parameter list as an array of array of char (that's not a typo).
128          * @param The number of items in the parameters list
129          * @param splithere The first parameter index to split as a comma seperated list
130          * @param extra The second parameter index to split as a comma seperated list
131          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
132          * caller should return without doing anything, otherwise it should continue into its main section of code.
133          */
134         int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra);
135
136         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
137          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
138          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
139          * the channel names and their keys as follows:
140          *
141          * JOIN #chan1,#chan2,#chan3 key1,,key3
142          *
143          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
144          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
145          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
146          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
147          *
148          * @param user The user who sent the command
149          * @param CommandObj the command object to call for each parameter in the list
150          * @param parameters Parameter list as an array of array of char (that's not a typo).
151          * @param The number of items in the parameters list
152          * @param splithere The first parameter index to split as a comma seperated list
153          * @param extra The second parameter index to split as a comma seperated list
154          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
155          * caller should return without doing anything, otherwise it should continue into its main section of code.
156          */
157         int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere);
158
159         /** Take a raw input buffer from a recvq, and process it on behalf of a user.
160          * @param buffer The buffer line to process
161          * @param user The user to whom this line belongs
162          */
163         void ProcessBuffer(std::string &buffer,userrec *user);
164
165         /** Remove all commands relating to module 'source'.
166          * @param source A module name which has introduced new commands
167          * @return True This function returns true if commands were removed
168          */
169         bool RemoveCommands(const char* source);
170
171         /** Add a new command to the commands hash
172          * @param f The new command_t to add to the list
173          * @param so_handle The handle to the shared object where the command can be found.
174          * Only core commands loaded via cmd_*.so files should set this parameter to anything
175          * meaningful. Module authors should leave this parameter at its default of NULL.
176          * @return True if the command was added
177          */
178         bool CreateCommand(command_t *f, void* so_handle = NULL);
179 };
180
181 /** Command handler class for the RELOAD command.
182  * A command cant really reload itself, so this has to be in here.
183  */
184 class cmd_reload : public command_t
185 {
186  public:
187         /** Standard constructor
188          */
189         cmd_reload (InspIRCd* Instance) : command_t(Instance,"RELOAD",'o',1) { syntax = "<core-command>"; }
190         /** Handle RELOAD
191          */
192         void Handle(const char** parameters, int pcnt, userrec *user);
193 };
194
195
196
197 #endif