]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/command_parse.h
Auto loading of commands as shared objects via dlsym (very lightweight interface...
[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 /** This class handles command management and parsing.
28  * It allows you to add and remove commands from the map,
29  * call command handlers by name, and chop up comma seperated
30  * parameters into multiple calls.
31  */
32 class CommandParser : public classbase
33 {
34  private:
35         /** The creator of this class
36          */
37         InspIRCd* ServerInstance;
38
39         /** Process a parameter string into a list of items
40          * @param command_p The output list of items
41          * @param parameters The input string
42          * @return The number of parameters parsed into command_p
43          */
44         int ProcessParameters(char **command_p,char *parameters);
45
46         /** Process a command from a user.
47          * @param user The user to parse the command for
48          * @param cmd The command string to process
49          */
50         void ProcessCommand(userrec *user, std::string &cmd);
51
52         /** Insert the default RFC1459 commands into the command hash.
53          */
54         void SetupCommandTable();
55
56         void FindSym(void** v, void* h);
57
58  public:
59         /** Command list, a hash_map of command names to command_t*
60          */
61         command_table cmdlist;
62
63         void LoadCommand(const char* name);
64
65         /** Default constructor.
66          * @param Instance The creator of this class
67          */
68         CommandParser(InspIRCd* Instance);
69
70         /** Calls the handler for a given command.
71          * @param commandname The command to find. This should be in uppercase.
72          * @param parameters Parameter list as an array of array of char (that's not a typo).
73          * @param pcnt The number of items in the parameters list
74          * @param user The user to call the handler on behalf of
75          * @return This method will return true if the command handler was found and called
76          */
77         bool CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user);
78
79         /** This function returns true if a command is valid with the given number of parameters and user.
80          * @param commandname The command name to check
81          * @param pcnt The parameter count
82          * @param user The user to check against
83          * @return If the user given has permission to execute the command, and the parameter count is
84          * equal to or greater than the minimum number of parameters to the given command, then this
85          * function will return true, otherwise it will return false.
86          */
87         bool IsValidCommand(const std::string &commandname, int pcnt, userrec * user);
88         
89         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
90          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
91          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
92          * the channel names and their keys as follows:
93          *
94          * JOIN #chan1,#chan2,#chan3 key1,,key3
95          *
96          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
97          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
98          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
99          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
100          *
101          * @param user The user who sent the command
102          * @param CommandObj the command object to call for each parameter in the list
103          * @param parameters Parameter list as an array of array of char (that's not a typo).
104          * @param The number of items in the parameters list
105          * @param splithere The first parameter index to split as a comma seperated list
106          * @param extra The second parameter index to split as a comma seperated list
107          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
108          * caller should return without doing anything, otherwise it should continue into its main section of code.
109          */
110         int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra);
111
112         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
113          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
114          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
115          * the channel names and their keys as follows:
116          *
117          * JOIN #chan1,#chan2,#chan3 key1,,key3
118          *
119          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
120          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
121          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
122          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
123          *
124          * @param user The user who sent the command
125          * @param CommandObj the command object to call for each parameter in the list
126          * @param parameters Parameter list as an array of array of char (that's not a typo).
127          * @param The number of items in the parameters list
128          * @param splithere The first parameter index to split as a comma seperated list
129          * @param extra The second parameter index to split as a comma seperated list
130          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
131          * caller should return without doing anything, otherwise it should continue into its main section of code.
132          */
133         int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere);
134
135         /** Take a raw input buffer from a recvq, and process it on behalf of a user.
136          * @param buffer The buffer line to process
137          * @param user The user to whom this line belongs
138          */
139         void ProcessBuffer(std::string &buffer,userrec *user);
140
141         /** Remove all commands relating to module 'source'.
142          * @param source A module name which has introduced new commands
143          * @return True This function returns true if commands were removed
144          */
145         bool RemoveCommands(const char* source);
146
147         /** Add a new command to the commands hash
148          * @param f The new command_t to add to the list
149          * @return True if the command was added
150          */
151         bool CreateCommand(command_t *f);
152 };
153
154 #endif