]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/command_parse.h
Fix Doxygen syntax errors.
[user/henk/code/inspircd.git] / include / command_parse.h
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *   Copyright (C) 2005-2007 Craig Edwards <craigedwards@brainbox.cc>
8  *
9  * This file is part of InspIRCd.  InspIRCd is free software: you can
10  * redistribute it and/or modify it under the terms of the GNU General Public
11  * License as published by the Free Software Foundation, version 2.
12  *
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
20  */
21
22
23 #ifndef COMMAND_PARSE_H
24 #define COMMAND_PARSE_H
25
26 /** A list of dll/so files containing the command handlers for the core
27  */
28 typedef std::map<std::string, void*> SharedObjectList;
29
30 /** This class handles command management and parsing.
31  * It allows you to add and remove commands from the map,
32  * call command handlers by name, and chop up comma seperated
33  * parameters into multiple calls.
34  */
35 class CoreExport CommandParser
36 {
37  private:
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(std::vector<std::string>& 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         bool ProcessCommand(LocalUser *user, std::string &cmd);
54
55
56
57  public:
58         /** Command list, a hash_map of command names to Command*
59          */
60         Commandtable cmdlist;
61
62         /** Default constructor.
63          */
64         CommandParser();
65
66         /** Calls the handler for a given command.
67          * @param commandname The command to find. This should be in uppercase.
68          * @param parameters Parameter list
69          * @param user The user to call the handler on behalf of
70          * @return This method will return CMD_SUCCESS if the command handler was found and called,
71          * and the command completeld successfully. It will return CMD_FAILURE if the command handler was found
72          * and called, but the command did not complete successfully, and it will return CMD_INVALID if the
73          * command simply did not exist at all or the wrong number of parameters were given, or the user
74          * was not privilaged enough to execute the command.
75          */
76         CmdResult CallHandler(const std::string &commandname, const std::vector<std::string>& parameters, User *user);
77
78         /** Get the handler function for a command.
79          * @param commandname The command required. Always use uppercase for this parameter.
80          * @return a pointer to the command handler, or NULL
81          */
82         Command* GetHandler(const std::string &commandname);
83
84         /** This function returns true if a command is valid with the given number of parameters and user.
85          * @param commandname The command name to check
86          * @param pcnt The parameter count
87          * @param user The user to check against
88          * @return If the user given has permission to execute the command, and the parameter count is
89          * equal to or greater than the minimum number of parameters to the given command, then this
90          * function will return true, otherwise it will return false.
91          */
92         bool IsValidCommand(const std::string &commandname, unsigned int pcnt, User * user);
93
94         /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
95          * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
96          * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
97          * the channel names and their keys as follows:
98          *
99          * JOIN \#chan1,\#chan2,\#chan3 key1,,key3
100          *
101          * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
102          * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
103          * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
104          * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
105          *
106          * @param user The user who sent the command
107          * @param CommandObj the command object to call for each parameter in the list
108          * @param parameters Parameter list as an array of array of char (that's not a typo).
109          * @param splithere The first parameter index to split as a comma seperated list
110          * @param extra The second parameter index to split as a comma seperated list
111          * @param usemax Limit the command to MaxTargets targets
112          * @return This function will return 1 when there are no more parameters to process. When this occurs, its
113          * caller should return without doing anything, otherwise it should continue into its main section of code.
114          */
115         int LoopCall(User* user, Command* CommandObj, const std::vector<std::string>& parameters, unsigned int splithere, int extra = -1, bool usemax = true);
116
117         /** Take a raw input buffer from a recvq, and process it on behalf of a user.
118          * @param buffer The buffer line to process
119          * @param user The user to whom this line belongs
120          */
121         bool ProcessBuffer(std::string &buffer,LocalUser *user);
122
123         /** Add a new command to the commands hash
124          * @param f The new Command to add to the list
125          * @return True if the command was added
126          */
127         bool AddCommand(Command *f);
128
129         /** Removes a command.
130          */
131         void RemoveCommand(Command* x);
132
133         /** Translate nicknames in a string into UIDs, based on the TranslationType given.
134          * @param to The translation type to use for the process.
135          * @param source The input string
136          * @param dest The output string, it is safe to pass source and dest as the same variable only for translation type TR_TEXT.
137          * @return returns the number of substitutions made. Will always be 0 or 1
138          */
139         int TranslateUIDs(TranslateType to, const std::string &source, std::string &dest);
140
141         /** Translate nicknames in a list of strings into UIDs, based on the TranslateTypes given.
142          * @param to The translation types to use for the process. If this list is too short, TR_TEXT is assumed for the rest.
143          * @param source The strings to translate
144          * @param dest The output string
145          * @param prefix_final True if the final source argument should have a colon prepended (if it could contain a space)
146          * @param custom_translator Used to translate the parameter if the TR_CUSTOM type is found in to
147          * @return returns the number of substitutions made.
148          */
149         int TranslateUIDs(const std::vector<TranslateType> to, const std::vector<std::string> &source, std::string &dest, bool prefix_final = false, Command* custom_translator = NULL);
150 };
151
152 /** A lookup table of values for multiplier characters used by
153  * InspIRCd::Duration(). In this lookup table, the indexes for
154  * the ascii values 'm' and 'M' have the value '60', the indexes
155  * for the ascii values 'D' and 'd' have a value of '86400', etc.
156  */
157 const int duration_multi[] =
158 {
159         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
160         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
161         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
162         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
163         1, 1, 1, 1, 1, 1, 1, 1, 86400, 1, 1, 1, 3600,
164         1, 1, 1, 1, 60, 1, 1, 1, 1, 1, 1, 1, 1, 1,
165         604800, 1, 31557600, 1, 1, 1, 1, 1, 1, 1, 1,
166         1, 1, 86400, 1, 1, 1, 3600, 1, 1, 1, 1, 60,
167         1, 1, 1, 1, 1, 1, 1, 1, 1, 604800, 1, 31557600,
168         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
169         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
170         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
171         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
172         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
173         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
174         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
175         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
176         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
177 };
178
179 #endif