1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
7 * <brain@chatspike.net>
8 * <Craig@chatspike.net>
10 * Written by Craig Edwards, Craig McLure, and others.
11 * This program is free but copyrighted software; see
12 * the file COPYING for details.
14 * ---------------------------------------------------
17 #ifndef __COMMAND_PARSE_H
18 #define __COMMAND_PARSE_H
27 typedef std::map<std::string, void*> SharedObjectList;
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.
34 class CommandParser : public classbase
37 /** The creator of this class
39 InspIRCd* ServerInstance;
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
46 int ProcessParameters(char **command_p,char *parameters);
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
52 void ProcessCommand(userrec *user, std::string &cmd);
54 /** Insert the default RFC1459 commands into the command hash.
56 void SetupCommandTable();
58 void FindSym(void** v, void* h);
60 SharedObjectList RFCCommands;
62 void LoadCommand(const char* name);
65 /** Command list, a hash_map of command names to command_t*
67 command_table cmdlist;
69 bool ReloadCommand(const char* cmd);
71 /** Default constructor.
72 * @param Instance The creator of this class
74 CommandParser(InspIRCd* Instance);
76 /** Calls the handler for a given command.
77 * @param commandname The command to find. This should be in uppercase.
78 * @param parameters Parameter list as an array of array of char (that's not a typo).
79 * @param pcnt The number of items in the parameters list
80 * @param user The user to call the handler on behalf of
81 * @return This method will return true if the command handler was found and called
83 bool CallHandler(const std::string &commandname,const char** parameters, int pcnt, userrec *user);
85 /** This function returns true if a command is valid with the given number of parameters and user.
86 * @param commandname The command name to check
87 * @param pcnt The parameter count
88 * @param user The user to check against
89 * @return If the user given has permission to execute the command, and the parameter count is
90 * equal to or greater than the minimum number of parameters to the given command, then this
91 * function will return true, otherwise it will return false.
93 bool IsValidCommand(const std::string &commandname, int pcnt, userrec * user);
95 /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
96 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
97 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
98 * the channel names and their keys as follows:
100 * JOIN #chan1,#chan2,#chan3 key1,,key3
102 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
103 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
104 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
105 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
107 * @param user The user who sent the command
108 * @param CommandObj the command object to call for each parameter in the list
109 * @param parameters Parameter list as an array of array of char (that's not a typo).
110 * @param The number of items in the parameters list
111 * @param splithere The first parameter index to split as a comma seperated list
112 * @param extra The second parameter index to split as a comma seperated list
113 * @return This function will return 1 when there are no more parameters to process. When this occurs, its
114 * caller should return without doing anything, otherwise it should continue into its main section of code.
116 int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere, unsigned int extra);
118 /** LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
119 * There are two overriden versions of this method, one of which takes two potential lists and the other takes one.
120 * We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once,
121 * the channel names and their keys as follows:
123 * JOIN #chan1,#chan2,#chan3 key1,,key3
125 * Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating
126 * two instances of irc::commasepstream and reading them both together until the first runs out of tokens.
127 * The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc.
128 * Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.
130 * @param user The user who sent the command
131 * @param CommandObj the command object to call for each parameter in the list
132 * @param parameters Parameter list as an array of array of char (that's not a typo).
133 * @param The number of items in the parameters list
134 * @param splithere The first parameter index to split as a comma seperated list
135 * @param extra The second parameter index to split as a comma seperated list
136 * @return This function will return 1 when there are no more parameters to process. When this occurs, its
137 * caller should return without doing anything, otherwise it should continue into its main section of code.
139 int LoopCall(userrec* user, command_t* CommandObj, const char** parameters, int pcnt, unsigned int splithere);
141 /** Take a raw input buffer from a recvq, and process it on behalf of a user.
142 * @param buffer The buffer line to process
143 * @param user The user to whom this line belongs
145 void ProcessBuffer(std::string &buffer,userrec *user);
147 /** Remove all commands relating to module 'source'.
148 * @param source A module name which has introduced new commands
149 * @return True This function returns true if commands were removed
151 bool RemoveCommands(const char* source);
153 /** Add a new command to the commands hash
154 * @param f The new command_t to add to the list
155 * @param so_handle The handle to the shared object where the command can be found.
156 * Only core commands loaded via cmd_*.so files should set this parameter to anything
157 * meaningful. Module authors should leave this parameter at its default of NULL.
158 * @return True if the command was added
160 bool CreateCommand(command_t *f, void* so_handle = NULL);
163 /** Command handler class for the RELOAD command.
164 * A command cant really reload itself, so this has to be in here.
166 class cmd_reload : public command_t
169 /** Standard constructor
171 cmd_reload (InspIRCd* Instance) : command_t(Instance,"RELOAD",'o',1) { syntax = "<core-command>"; }
174 void Handle(const char** parameters, int pcnt, userrec *user);