]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/command_parse.h
Merge v2.0.23 and v2.0.24 into master.
[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 #pragma once
24
25 /** This class handles command management and parsing.
26  * It allows you to add and remove commands from the map,
27  * call command handlers by name, and chop up comma seperated
28  * parameters into multiple calls.
29  */
30 class CoreExport CommandParser
31 {
32  public:
33         typedef TR1NS::unordered_map<std::string, Command*> CommandMap;
34
35  private:
36         /** Process a command from a user.
37          * @param user The user to parse the command for
38          * @param cmd The command string to process
39          */
40         void ProcessCommand(LocalUser* user, std::string& cmd);
41
42         /** Command list, a hash_map of command names to Command*
43          */
44         CommandMap cmdlist;
45
46  public:
47         /** Default constructor.
48          */
49         CommandParser();
50
51         /** Get a command name -> Command* map containing all client to server commands
52          * @return A map of command handlers keyed by command names
53          */
54         const CommandMap& GetCommands() const { return cmdlist; }
55
56         /** Calls the handler for a given command.
57          * @param commandname The command to find. This should be in uppercase.
58          * @param parameters Parameter list
59          * @param user The user to call the handler on behalf of
60          * @param cmd If non-NULL and the command was executed it is set to the command handler,
61          * otherwise it isn't written to.
62          * @return This method will return CMD_SUCCESS if the command handler was found and called,
63          * and the command completeld successfully. It will return CMD_FAILURE if the command handler was found
64          * and called, but the command did not complete successfully, and it will return CMD_INVALID if the
65          * command simply did not exist at all or the wrong number of parameters were given, or the user
66          * was not privilaged enough to execute the command.
67          */
68         CmdResult CallHandler(const std::string& commandname, const std::vector<std::string>& parameters, User* user, Command** cmd = NULL);
69
70         /** Get the handler function for a command.
71          * @param commandname The command required. Always use uppercase for this parameter.
72          * @return a pointer to the command handler, or NULL
73          */
74         Command* GetHandler(const std::string &commandname);
75
76         /** LoopCall is used to call a command handler repeatedly based on the contents of a comma seperated list.
77          * There are two ways to call this method, either with one potential list or with two potential lists.
78          * We need to handle two potential lists for JOIN, because a JOIN may contain two lists of items at once:
79          * the channel names and their keys as follows:
80          *
81          * JOIN \#chan1,\#chan2,\#chan3 key1,,key3
82          *
83          * Therefore, we need to deal with both lists concurrently. If there are two lists then the method reads
84          * them both together until the first runs out of tokens.
85          * With one list it is much simpler, and is used in NAMES, WHOIS, PRIVMSG etc.
86          *
87          * If there is only one list and there are duplicates in it, then the command handler is only called for
88          * unique items. Entries are compared using "irc comparison".
89          * If the usemax parameter is true (the default) the function only parses until it reaches
90          * ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
91          *
92          * The OnPostCommand hook is executed for each item after it has been processed by the handler, with the
93          * original line parameter being empty (to indicate that the command in that form was created by this function).
94          * This only applies if the user executing the command is local.
95          *
96          * If there are two lists and the second list runs out of tokens before the first list then parameters[extra]
97          * will be an EMPTY string when Handle() is called for the remaining tokens in the first list, even if it is
98          * in the middle of parameters[]! Moreover, empty tokens in the second list are allowed, and those will also
99          * result in the appropiate entry being empty in parameters[].
100          * This is different than what command handlers usually expect; the command parser only allows an empty param
101          * as the last item in the vector.
102          *
103          * @param user The user who sent the command
104          * @param handler The command handler to call for each parameter in the list
105          * @param parameters Parameter list as a vector of strings
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, or -1 (the default) if there is only one list
108          * @param usemax True to limit the command to MaxTargets targets (default), or false to process all tokens
109          * @return This function returns true when it identified a list in the given parameter and finished calling the
110          * command handler for each entry on the list. When this occurs, the caller should return without doing anything,
111          * otherwise it should continue into its main section of code.
112          */
113         static bool LoopCall(User* user, Command* handler, const std::vector<std::string>& parameters, unsigned int splithere, int extra = -1, bool usemax = true);
114
115         /** Take a raw input buffer from a recvq, and process it on behalf of a user.
116          * @param buffer The buffer line to process
117          * @param user The user to whom this line belongs
118          */
119         void ProcessBuffer(std::string &buffer,LocalUser *user);
120
121         /** Add a new command to the commands hash
122          * @param f The new Command to add to the list
123          * @return True if the command was added
124          */
125         bool AddCommand(Command *f);
126
127         /** Removes a command.
128          */
129         void RemoveCommand(Command* x);
130
131         /** Translate a single item based on the TranslationType given.
132          * @param to The translation type to use for the process
133          * @param item The input string
134          * @param dest The output string. The translation result will be appended to this string
135          * @param custom_translator Used to translate the parameter if the translation type is TR_CUSTOM, if NULL, TR_CUSTOM will act like TR_TEXT
136          * @param paramnumber The index of the parameter we are translating.
137          */
138         static void TranslateSingleParam(TranslateType to, const std::string& item, std::string& dest, CommandBase* custom_translator = NULL, unsigned int paramnumber = 0);
139
140         /** Translate nicknames in a list of strings into UIDs, based on the TranslateTypes given.
141          * @param to The translation types to use for the process. If this list is too short, TR_TEXT is assumed for the rest.
142          * @param source The strings to translate
143          * @param prefix_final True if the final source argument should have a colon prepended (if it could contain a space)
144          * @param custom_translator Used to translate the parameter if the translation type is TR_CUSTOM, if NULL, TR_CUSTOM will act like TR_TEXT
145          * @return dest The output string
146          */
147         static std::string TranslateUIDs(const std::vector<TranslateType>& to, const std::vector<std::string>& source, bool prefix_final = false, CommandBase* custom_translator = NULL);
148 };
149
150 /** A lookup table of values for multiplier characters used by
151  * InspIRCd::Duration(). In this lookup table, the indexes for
152  * the ascii values 'm' and 'M' have the value '60', the indexes
153  * for the ascii values 'D' and 'd' have a value of '86400', etc.
154  */
155 const int duration_multi[] =
156 {
157         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
158         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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, 86400, 1, 1, 1, 3600,
162         1, 1, 1, 1, 60, 1, 1, 1, 1, 1, 1, 1, 1, 1,
163         604800, 1, 31557600, 1, 1, 1, 1, 1, 1, 1, 1,
164         1, 1, 86400, 1, 1, 1, 3600, 1, 1, 1, 1, 60,
165         1, 1, 1, 1, 1, 1, 1, 1, 1, 604800, 1, 31557600,
166         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
167         1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
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
175 };