]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/command_parse.h
Remove InspIRCd* parameters and fields
[user/henk/code/inspircd.git] / include / command_parse.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 /** A list of dll/so files containing the command handlers for the core
18  */
19 typedef std::map<std::string, void*> SharedObjectList;
20
21 /** This class handles command management and parsing.
22  * It allows you to add and remove commands from the map,
23  * call command handlers by name, and chop up comma seperated
24  * parameters into multiple calls.
25  */
26 class CoreExport CommandParser : public classbase
27 {
28  private:
29         /** Parameter buffer
30          */
31         std::vector<std::string> para;
32
33         /** Process a parameter string into a list of items
34          * @param command_p The output list of items
35          * @param parameters The input string
36          * @return The number of parameters parsed into command_p
37          */
38         int ProcessParameters(std::vector<std::string>& command_p, char* parameters);
39
40         /** Process a command from a user.
41          * @param user The user to parse the command for
42          * @param cmd The command string to process
43          */
44         bool ProcessCommand(User *user, std::string &cmd);
45
46         /** Removes a command if the sources match. Used as a helper for
47          *  safe hash_map delete while iter in RemoveCommands(const char* source).
48          */
49         void RemoveCommand(nspace::hash_map<std::string,Command*>::iterator safei, Module* source);
50
51
52  public:
53         /** Command list, a hash_map of command names to Command*
54          */
55         Commandtable cmdlist;
56
57         /** Default constructor.
58          * @param Instance The creator of this class
59          */
60         CommandParser();
61
62         /** Calls the handler for a given command.
63          * @param commandname The command to find. This should be in uppercase.
64          * @param parameters Parameter list
65          * @param user The user to call the handler on behalf of
66          * @return This method will return CMD_SUCCESS if the command handler was found and called,
67          * and the command completeld successfully. It will return CMD_FAILURE if the command handler was found
68          * and called, but the command did not complete successfully, and it will return CMD_INVALID if the
69          * command simply did not exist at all or the wrong number of parameters were given, or the user
70          * was not privilaged enough to execute the command.
71          */
72         CmdResult CallHandler(const std::string &commandname, const std::vector<std::string>& parameters, User *user);
73
74         /** Get the handler function for a command.
75          * @param commandname The command required. Always use uppercase for this parameter.
76          * @return a pointer to the command handler, or NULL
77          */
78         Command* GetHandler(const std::string &commandname);
79
80         /** This function returns true if a command is valid with the given number of parameters and user.
81          * @param commandname The command name to check
82          * @param pcnt The parameter count
83          * @param user The user to check against
84          * @return If the user given has permission to execute the command, and the parameter count is
85          * equal to or greater than the minimum number of parameters to the given command, then this
86          * function will return true, otherwise it will return false.
87          */
88         bool IsValidCommand(const std::string &commandname, unsigned int pcnt, User * user);
89
90         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
91          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
92          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
93          * the channel names and their keys as follows:
94          *
95          * JOIN #chan1,#chan2,#chan3 key1,,key3
96          *
97          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
98          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
99          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
100          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
101          *
102          * @param user The user who sent the command
103          * @param CommandObj the command object to call for each parameter in the list
104          * @param parameters Parameter list as an array of array of char (that's not a typo).
105          * @param The number of items in the parameters list
106          * @param splithere The first parameter index to split as a comma seperated list
107          * @param extra The second parameter index to split as a comma seperated list
108          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
109          * caller should return without doing anything, otherwise it should continue into its main section of code.
110          */
111         int LoopCall(User* user, Command* CommandObj, const std::vector<std::string>& parameters, unsigned int splithere, unsigned int extra);
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(User* user, Command* CommandObj, const std::vector<std::string>& parameters, unsigned int splithere);
135
136         /** Take a raw input buffer from a recvq, and process it on behalf of a user.
137          * @param buffer The buffer line to process
138          * @param user The user to whom this line belongs
139          */
140         bool ProcessBuffer(std::string &buffer,User *user);
141
142         /** Process lines in a users sendq.
143          * @param current The user to process
144          * @param one_only if one_only is set only one command is processed from the sendq.
145          */
146         void DoLines(User* current, bool one_only = false);
147
148         /** Remove all commands relating to module 'source'.
149          * @param source A module which has introduced new commands
150          */
151         void RemoveCommands(Module* source);
152
153         /** Add a new command to the commands hash
154          * @param f The new Command to add to the list
155          * @return True if the command was added
156          */
157         bool CreateCommand(Command *f);
158
159         /** Translate nicknames in a string into UIDs, based on the TranslationType given.
160          * @param to The translation type to use for the process.
161          * @param source The input string
162          * @param dest The output string, it is safe to pass source and dest as the same variable only for translation type TR_TEXT.
163          * @return returns the number of substitutions made. Will always be 0 or 1
164          */
165         int TranslateUIDs(TranslateType to, const std::string &source, std::string &dest);
166
167         /** Translate nicknames in a list of strings into UIDs, based on the TranslateTypes given.
168          * @param to The translation types to use for the process. If this list is too short, TR_TEXT is assumed for the rest.
169          * @param source The strings to translate
170          * @param dest The output string
171          * @param prefix_final True if the final source argument should have a colon prepended (if it could contain a space)
172          * @param custom_translator Used to translate the parameter if the TR_CUSTOM type is found in to
173          * @return returns the number of substitutions made.
174          */
175         int TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest, bool prefix_final = false, Command* custom_translator = NULL);
176 };
177
178 /** A lookup table of values for multiplier characters used by
179  * InspIRCd::Duration(). In this lookup table, the indexes for
180  * the ascii values 'm' and 'M' have the value '60', the indexes
181  * for the ascii values 'D' and 'd' have a value of '86400', etc.
182  */
183 const int duration_multi[] =
184 {
185         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
186         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
187         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
188         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
189         1, 1, 1, 1, 1, 1, 1, 1, 86400, 1, 1, 1, 3600,
190         1, 1, 1, 1, 60, 1, 1, 1, 1, 1, 1, 1, 1, 1,
191         604800, 1, 31557600, 1, 1, 1, 1, 1, 1, 1, 1,
192         1, 1, 86400, 1, 1, 1, 3600, 1, 1, 1, 1, 60,
193         1, 1, 1, 1, 1, 1, 1, 1, 1, 604800, 1, 31557600,
194         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
195         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
196         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
197         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
198         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
199         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
200         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
201         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
202         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
203 };
204
205 #endif