Parses commands from user input. More...
Inherits QObject.
Public Types | |
enum | Detail { Full = 0x0, NoTarget = 0x1, NoPrefix = 0x2, NoEllipsis = 0x4, NoParentheses = 0x8, NoBrackets = 0x10, NoAngles = 0x20, Visual = NoTarget | NoPrefix | NoEllipsis } |
Public Slots | |
void | clear () |
void | reset () |
Public Member Functions | |
IrcCommandParser (QObject *parent=0) | |
virtual | ~IrcCommandParser () |
Q_INVOKABLE void | addCommand (IrcCommand::Type type, const QString &syntax) |
Q_INVOKABLE IrcCommand * | parse (const QString &input) const |
Q_INVOKABLE void | removeCommand (IrcCommand::Type type, const QString &syntax=QString()) |
Q_INVOKABLE QString | syntax (const QString &command, Details details=Visual) const |
Properties | |
QStringList | channels |
QStringList | commands |
QString | target |
bool | tolerant |
QStringList | triggers |
Since the list of supported commands and the exact syntax for each command is application specific, IrcCommandParser does not provide any built-in command syntaxes. It is left up to the applications to introduce the supported commands and syntaxes. IrcCommandParser supports the following command syntax markup:
Syntax | Example | Description |
---|---|---|
<param> | <target> | A required parameter. |
(<param>) | (<key>) | An optional parameter. |
<param...> | <message...> | A required parameter, multiple words accepted. (1) |
(<param...>) | (<message...>) | An optional parameter, multiple words accepted. (1) |
(<#param>) | (<#channel>) | An optional channel parameter. (2) |
[param] | [target] | Inject the current target. |
The following example presents introducing some typical commands.
Notice that commands are often context sensitive. While some command may accept an optional parameter that is filled up with the current target (channel/query) name when absent, another command may always inject the current target name as a certain parameter. Therefore IrcCommandParser must be kept up-to-date with the current target and the list of channels.
IrcCommandParser serves as a generic parser for typical IRC commands. It can be utilized for parsing commands from user input in GUI clients, and from messages from other clients when implementing IRC bots.
The command parsing behavior is controlled by setting up command triggers. Whilst a typical GUI client might use "/"
as a command trigger, an IRC bot might use "!"
and the nick name of the bot. The following snippet illustrates a typical GUI client usage.
Input | Result | Description |
---|---|---|
"hello" | IrcCommand::Message | No matching command trigger => a message "hello" to #communi |
"/join #channel" | IrcCommand::Join | Matching command trigger => a command to join "#channel" |
See the bot example to see how the parser can be effectively utilized for IRC bots.
The parser also supports such custom client specific commands that are not sent to the server. Since IrcCommand does not know how to handle custom commands, the parser treats them as a special case injecting the command as a first parameter.
This enum describes the available syntax details.
|
explicit |
Constructs a command parser with parent.
|
virtual |
Destructs the command parser.
void IrcCommandParser::addCommand | ( | IrcCommand::Type | type, |
const QString & | syntax | ||
) |
Adds a command with type and syntax.
|
slot |
Clears the list of commands.
IrcCommand * IrcCommandParser::parse | ( | const QString & | input | ) | const |
Parses and returns the command for input, or 0
if the input is not valid.
void IrcCommandParser::removeCommand | ( | IrcCommand::Type | type, |
const QString & | syntax = QString() |
||
) |
Removes the command with type and syntax.
|
slot |
Resets the channels and the current target.
Returns syntax for the given command in given details level.
|
readwrite |
This property holds the available channels.
|
read |
This property holds the known commands.
The commands are uppercased and in alphabetical order.
|
readwrite |
|
readwrite |
This property holds whether the parser is tolerant.
A tolerant parser creates message commands out of input that does not start with a command trigger, and raw server commands when the input starts with a command trigger but the command is unrecognized. Known commands with invalid arguments are still considered invalid.
The default value is false
.
|
readwrite |
This property holds the command triggers.