The InspIRCd Project
Home | Developers | Wiki | Forums | Bug Tracker | SVN | Download
Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

CommandParser Class Reference

This class handles command management and parsing. More...

#include <command_parse.h>

Inheritance diagram for CommandParser:

Inheritance graph
[legend]
Collaboration diagram for CommandParser:

Collaboration graph
[legend]
List of all members.

Public Member Functions

bool ReloadCommand (std::string cmd, User *user)
 Reload a core command.
 CommandParser (InspIRCd *Instance)
 Default constructor.
CmdResult CallHandler (const std::string &commandname, const std::vector< std::string > &parameters, User *user)
 Calls the handler for a given command.
CommandGetHandler (const std::string &commandname)
 Get the handler function for a command.
bool IsValidCommand (const std::string &commandname, unsigned int pcnt, User *user)
 This function returns true if a command is valid with the given number of parameters and user.
int LoopCall (User *user, Command *CommandObj, const std::vector< std::string > &parameters, unsigned int splithere, unsigned int extra)
 LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
int LoopCall (User *user, Command *CommandObj, const std::vector< std::string > &parameters, unsigned int splithere)
 LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.
bool ProcessBuffer (std::string &buffer, User *user)
 Take a raw input buffer from a recvq, and process it on behalf of a user.
void DoLines (User *current, bool one_only=false)
 Process lines in a users sendq.
void RemoveCommands (const char *source)
 Remove all commands relating to module 'source'.
bool CreateCommand (Command *f, void *so_handle=NULL)
 Add a new command to the commands hash.
void SetupCommandTable (User *user)
 Insert the default RFC1459 commands into the command hash.
int TranslateUIDs (TranslateType to, const std::string &source, std::string &dest)
 Translate nicknames in a string into UIDs, based on the TranslationType given.

Public Attributes

Commandtable cmdlist
 Command list, a hash_map of command names to Command*.

Private Member Functions

int ProcessParameters (std::vector< std::string > &command_p, char *parameters)
 Process a parameter string into a list of items.
bool ProcessCommand (User *user, std::string &cmd)
 Process a command from a user.
bool FindSym (void **v, void *h, const std::string &name)
 Finds the init_command symbol in a .so file.
const char * LoadCommand (const char *name)
 Load a command from a shared object on disk.
void RemoveCommand (nspace::hash_map< std::string, Command * >::iterator safei, const char *source)
 Removes a command if the sources match.

Private Attributes

InspIRCdServerInstance
 The creator of this class.
std::vector< std::stringpara
 Parameter buffer.
SharedObjectList RFCCommands
 A list of core-implemented modes and their shared object handles.

Detailed Description

This class handles command management and parsing.

It allows you to add and remove commands from the map, call command handlers by name, and chop up comma seperated parameters into multiple calls.

Definition at line 26 of file command_parse.h.


Constructor & Destructor Documentation

CommandParser::CommandParser InspIRCd Instance  ) 
 

Default constructor.

Parameters:
Instance The creator of this class

Definition at line 433 of file command_parse.cpp.

References para.


Member Function Documentation

CmdResult CommandParser::CallHandler const std::string commandname,
const std::vector< std::string > &  parameters,
User user
 

Calls the handler for a given command.

Parameters:
commandname The command to find. This should be in uppercase.
parameters Parameter list
user The user to call the handler on behalf of
Returns:
This method will return CMD_SUCCESS if the command handler was found and called, and the command completeld successfully. It will return CMD_FAILURE if the command handler was found and called, but the command did not complete successfully, and it will return CMD_INVALID if the command simply did not exist at all or the wrong number of parameters were given, or the user was not privilaged enough to execute the command.

Definition at line 184 of file command_parse.cpp.

References CMD_INVALID, cmdlist, User::HasPermission(), IS_LOCAL, and User::IsModeSet().

Referenced by InspIRCd::CallCommandHandler(), ModuleAlias::DoCommand(), User::ForceNickChange(), CommandSVSWatch::Handle(), CommandSVSSilence::Handle(), ModuleModesOnConnect::OnPostConnect(), ModuleAntiBottler::OnPreCommand(), ModeHandler::RemoveMode(), and User::UnOper().

bool CommandParser::CreateCommand Command f,
void *  so_handle = NULL
 

Add a new command to the commands hash.

Parameters:
f The new Command to add to the list
so_handle The handle to the shared object where the command can be found. Only core commands loaded via cmd_*.so files should set this parameter to anything meaningful. Module authors should leave this parameter at its default of NULL.
Returns:
True if the command was added

Definition at line 411 of file command_parse.cpp.

References cmdlist, Command::command, DEFAULT, LogManager::Log(), InspIRCd::Logs, RFCCommands, and ServerInstance.

Referenced by LoadCommand(), and SetupCommandTable().

void CommandParser::DoLines User current,
bool  one_only = false
 

Process lines in a users sendq.

Parameters:
current The user to process
one_only if one_only is set only one command is processed from the sendq.

Definition at line 220 of file command_parse.cpp.

References User::BufferIsReady(), connection::bytes_in, connection::cmds_in, InspIRCd::FloodQuitUser, User::GetBuffer(), ConnectClass::GetFlood(), ConnectClass::GetThreshold(), User::lines_in, MAXBUF, User::MyClass, InspIRCd::Parser, ProcessBuffer(), User::reset_due, ServerInstance, and InspIRCd::Time().

Referenced by InspIRCd::DoBackgroundUserStuff().

bool CommandParser::FindSym void **  v,
void *  h,
const std::string name
[private]
 

Finds the init_command symbol in a .so file.

Parameters:
v A function pointer to be initialized
h A valid shared object handle
name The filename being loaded, used for error reporting
Returns:
True if the symbol could be found

Definition at line 438 of file command_parse.cpp.

References LogManager::Log(), InspIRCd::Logs, ServerInstance, and SPARSE.

Command * CommandParser::GetHandler const std::string commandname  ) 
 

Get the handler function for a command.

Parameters:
commandname The command required. Always use uppercase for this parameter.
Returns:
a pointer to the command handler, or NULL

Definition at line 173 of file command_parse.cpp.

References cmdlist.

Referenced by User::AddToWhoWas(), User::ForceNickChange(), ModuleSQLOper::LoginFail(), ModuleSpanningTree::OnPostCommand(), ModuleOperLog::OnPreCommand(), FilterBase::OnPreCommand(), ModuleInvisible::OnUserQuit(), ModuleDelayJoin::OnUserQuit(), ModuleAuditorium::OnUserQuit(), and ValidateWhoWas().

bool CommandParser::IsValidCommand const std::string commandname,
unsigned int  pcnt,
User user
 

This function returns true if a command is valid with the given number of parameters and user.

Parameters:
commandname The command name to check
pcnt The parameter count
user The user to check against
Returns:
If the user given has permission to execute the command, and the parameter count is equal to or greater than the minimum number of parameters to the given command, then this function will return true, otherwise it will return false.

Definition at line 149 of file command_parse.cpp.

References cmdlist, User::HasPermission(), IS_LOCAL, and User::IsModeSet().

Referenced by InspIRCd::IsValidModuleCommand().

const char * CommandParser::LoadCommand const char *  name  )  [private]
 

Load a command from a shared object on disk.

Parameters:
name The shared object to load (without path)
Returns:
NULL on success, pointer to dlerrr() error message on failure

Definition at line 501 of file command_parse.cpp.

References CreateCommand(), DEBUG, LIBRARYDIR, LogManager::Log(), InspIRCd::Logs, MAXBUF, RFCCommands, ServerInstance, and SPARSE.

Referenced by ReloadCommand(), and SetupCommandTable().

int CommandParser::LoopCall User user,
Command CommandObj,
const std::vector< std::string > &  parameters,
unsigned int  splithere
 

LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.

There are two overriden versions of this method, one of which takes two potential lists and the other takes one. We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once, the channel names and their keys as follows:

JOIN chan1,chan2,chan3 key1,,key3

Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating two instances of irc::commasepstream and reading them both together until the first runs out of tokens. The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc. Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.

Parameters:
user The user who sent the command
CommandObj the command object to call for each parameter in the list
parameters Parameter list as an array of array of char (that's not a typo).
The number of items in the parameters list
splithere The first parameter index to split as a comma seperated list
extra The second parameter index to split as a comma seperated list
Returns:
This function will return 1 when there are no more parameters to process. When this occurs, its caller should return without doing anything, otherwise it should continue into its main section of code.

Definition at line 103 of file command_parse.cpp.

References InspIRCd::Config, irc::sepstream::GetToken(), Command::Handle(), MAXPARAMETERS, ServerConfig::MaxTargets, and ServerInstance.

int CommandParser::LoopCall User user,
Command CommandObj,
const std::vector< std::string > &  parameters,
unsigned int  splithere,
unsigned int  extra
 

LoopCall is used to call a command classes handler repeatedly based on the contents of a comma seperated list.

There are two overriden versions of this method, one of which takes two potential lists and the other takes one. We need a version which takes two potential lists for JOIN, because a JOIN may contain two lists of items at once, the channel names and their keys as follows:

JOIN chan1,chan2,chan3 key1,,key3

Therefore, we need to deal with both lists concurrently. The first instance of this method does that by creating two instances of irc::commasepstream and reading them both together until the first runs out of tokens. The second version is much simpler and just has the one stream to read, and is used in NAMES, WHOIS, PRIVMSG etc. Both will only parse until they reach ServerInstance->Config->MaxTargets number of targets, to stop abuse via spam.

Parameters:
user The user who sent the command
CommandObj the command object to call for each parameter in the list
parameters Parameter list as an array of array of char (that's not a typo).
The number of items in the parameters list
splithere The first parameter index to split as a comma seperated list
extra The second parameter index to split as a comma seperated list
Returns:
This function will return 1 when there are no more parameters to process. When this occurs, its caller should return without doing anything, otherwise it should continue into its main section of code.

Definition at line 51 of file command_parse.cpp.

References InspIRCd::Config, irc::sepstream::GetToken(), Command::Handle(), MAXPARAMETERS, ServerConfig::MaxTargets, and ServerInstance.

bool CommandParser::ProcessBuffer std::string buffer,
User user
 

Take a raw input buffer from a recvq, and process it on behalf of a user.

Parameters:
buffer The buffer line to process
user The user to whom this line belongs

Definition at line 390 of file command_parse.cpp.

References DEBUG, EventHandler::GetFd(), LogManager::Log(), InspIRCd::Logs, User::nick, ProcessCommand(), and ServerInstance.

Referenced by DoLines().

bool CommandParser::ProcessCommand User user,
std::string cmd
[private]
 

Process a command from a user.

Parameters:
user The user to parse the command for
cmd The command string to process

Definition at line 261 of file command_parse.cpp.

References cmdlist, InspIRCd::Config, User::ExemptFromPenalty, FOREACH_MOD, FOREACH_RESULT, ConnectClass::GetPingTime(), irc::tokenstream::GetToken(), User::HasPermission(), connection::host, I_OnPostCommand, I_OnPreCommand, User::ident, User::IncreasePenalty(), IS_OPER, User::IsModeSet(), MAXPARAMETERS, User::MyClass, User::nick, connection::nping, User::oper, User::OverPenalty, User::Penalty, REG_ALL, connection::registered, ServerInstance, InspIRCd::SNO, InspIRCd::stats, serverstats::statsUnknown, ServerConfig::SyntaxHints, InspIRCd::Time(), User::WriteNumeric(), and SnomaskManager::WriteToSnoMask().

Referenced by ProcessBuffer().

int CommandParser::ProcessParameters std::vector< std::string > &  command_p,
char *  parameters
[private]
 

Process a parameter string into a list of items.

Parameters:
command_p The output list of items
parameters The input string
Returns:
The number of parameters parsed into command_p

bool CommandParser::ReloadCommand std::string  cmd,
User user
 

Reload a core command.

This will only reload commands implemented by the core, to reload a modular command, you must reload that module.

Parameters:
cmd The command to reload. This will cause the shared object which implements this command to be closed, and then reloaded.
Returns:
True if the command was reloaded, false if it could not be found or another error occured

Definition at line 450 of file command_parse.cpp.

References cmdlist, LoadCommand(), MAXBUF, and RFCCommands.

Referenced by cmd_reload::Handle().

void CommandParser::RemoveCommand nspace::hash_map< std::string, Command * >::iterator  safei,
const char *  source
[private]
 

Removes a command if the sources match.

Used as a helper for safe hash_map delete while iter in RemoveCommands(const char* source).

Referenced by RemoveCommands().

void CommandParser::RemoveCommands const char *  source  ) 
 

Remove all commands relating to module 'source'.

Parameters:
source A module name which has introduced new commands

Definition at line 369 of file command_parse.cpp.

References cmdlist, and RemoveCommand().

Referenced by InspIRCd::Cleanup(), and ModuleManager::Unload().

void CommandParser::SetupCommandTable User user  ) 
 

Insert the default RFC1459 commands into the command hash.

Ignore any already loaded commands.

Parameters:
user User to spool errors to, or if NULL, when an error occurs spool the errors to stdout then exit with EXIT_STATUS_HANDLER.

Definition at line 532 of file command_parse.cpp.

References cmdlist, CreateCommand(), EXIT_STATUS_BADHANDLER, LIBRARYDIR, LoadCommand(), match(), RFCCommands, and ServerInstance.

Referenced by ServerConfig::Read().

int CommandParser::TranslateUIDs TranslateType  to,
const std::string source,
std::string dest
 

Translate nicknames in a string into UIDs, based on the TranslationType given.

Parameters:
to The translation type to use for the process.
source The input string
dest The output string, it is safe to pass source and dest as the same variable only for translation type TR_TEXT.
Returns:
returns the number of substitutions made. Will always be 0 or 1 for TR_TEXT and 0..n for other types.

Definition at line 579 of file command_parse.cpp.

References InspIRCd::FindNick(), irc::sepstream::GetToken(), ServerInstance, TR_END, TR_NICK, TR_NICKLIST, TR_SPACENICKLIST, TR_TEXT, and User::uuid.

Referenced by ModuleSpanningTree::OnMode(), ModuleSpanningTree::OnPostCommand(), ModuleSpanningTree::ProtoSendMode(), and SpanningTreeProtocolInterface::SendMode().


Member Data Documentation

Commandtable CommandParser::cmdlist
 

Command list, a hash_map of command names to Command*.

Definition at line 77 of file command_parse.h.

Referenced by CallHandler(), CreateCommand(), GetHandler(), InitializeDisabledCommands(), IsValidCommand(), ModuleAbbreviation::OnPreCommand(), ProcessCommand(), ReloadCommand(), RemoveCommands(), and SetupCommandTable().

std::vector<std::string> CommandParser::para [private]
 

Parameter buffer.

Definition at line 35 of file command_parse.h.

Referenced by CommandParser().

SharedObjectList CommandParser::RFCCommands [private]
 

A list of core-implemented modes and their shared object handles.

Definition at line 60 of file command_parse.h.

Referenced by CreateCommand(), LoadCommand(), ReloadCommand(), and SetupCommandTable().

InspIRCd* CommandParser::ServerInstance [private]
 

The creator of this class.

Definition at line 31 of file command_parse.h.

Referenced by CreateCommand(), DoLines(), FindSym(), LoadCommand(), LoopCall(), ProcessBuffer(), ProcessCommand(), SetupCommandTable(), and TranslateUIDs().


The documentation for this class was generated from the following files: