Communi  3.7.0
A cross-platform IRC framework written with Qt
Public Member Functions | List of all members
IrcCommandFilter Class Referenceabstract

Provides an interface for filtering commands. More...

Inherited by IrcQmlFilter, and IrcQmlFilter.

Public Member Functions

virtual ~IrcCommandFilter ()
 
virtual bool commandFilter (IrcCommand *command)=0
 

Detailed Description

IrcCommandFilter may be used to intercept commands before they get sent further. In order to use IrcCommandFilter, it must be installed via IrcConnection::installCommandFilter().

Command filtering can be useful doing extra tasks for specific type of commands. The following example sends a PING command after each PRIVMSG command. A consequent PONG reply from the server verifies that the PRIVMSG has been also processed.

class CommandVerifier : public QObject,
{
Q_OBJECT
public:
CommandVerifier(IrcConnection* parent) :
QObject(parent), identifier(0), connection(parent)
{
connection->installMessageFilter(this);
connection->installCommandFilter(this);
}
virtual bool commandFilter(IrcCommand* cmd)
{
if (cmd->type() == IrcCommand::Message) {
cmd->setParent(this); // take ownership
connection->sendCommand(cmd);
commands.insert(++identifier, cmd);
connection->sendData("PING communi/" + QByteArray::number(identifier));
return true;
}
return false;
}
virtual bool messageFilter(IrcMessage* msg)
{
if (msg->type() == IrcMessage::Pong) {
QString arg = static_cast<IrcPongMessage*>(msg)->argument();
if (arg.startsWith("communi/")) {
bool ok = false;
quint64 id = arg.mid(8).toULongLong(&ok);
if (ok) {
IrcCommand* command = commands.take(id);
if (command) {
emit verified(command);
command->deleteLater();
return true;
}
}
}
}
return false;
}
signals:
void verified(IrcCommand* cmd);
private:
quint64 identifier;
IrcConnection* connection;
QMap<quint64, IrcCommand*> commands;
};
Provides an interface for filtering commands.
Definition: ircfilter.h:48
virtual bool commandFilter(IrcCommand *command)=0
Provides the most common commands.
Definition: irccommand.h:45
@ Message
A message command (PRIVMSG) is used to send private messages to channels and users.
Definition: irccommand.h:69
Provides means to establish a connection to an IRC server.
Definition: ircconnection.h:49
Provides an interface for filtering messages.
Definition: ircfilter.h:41
The base class of all messages.
Definition: ircmessage.h:48
@ Pong
A pong message (IrcPongMessage).
Definition: ircmessage.h:85
Represents a pong message.
Definition: ircmessage.h:496
Note
Notice that it is safe to call IrcConnection::sendCommand() from IrcCommandFilter::commandFilter(). Such commands won't get delivered back to the same filter to avoid recursion.
See also
IrcConnection::installCommandFilter(), IrcMessageFilter

Constructor & Destructor Documentation

◆ ~IrcCommandFilter()

IrcCommandFilter::~IrcCommandFilter ( )
inlinevirtual

Destructs the command filter.

The command filter is automatically removed from any connection(s) it is installed on.

See also
IrcConnection::removeCommandFilter()

Member Function Documentation

◆ commandFilter()

bool IrcCommandFilter::commandFilter ( IrcCommand command)
pure virtual

Reimplement this function to filter commands to installed connections.

Return true to filter the command out, i.e. stop it being handled further; otherwise return false.

See also
IrcConnection::installCommandFilter()