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