Communi  3.5.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;
};
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 ( )
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

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()