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

Provides an interface for filtering messages. More...

Inherited by IrcQmlFilter, and IrcQmlFilter.

Public Member Functions

virtual ~IrcMessageFilter ()
 
virtual bool messageFilter (IrcMessage *message)=0
 

Detailed Description

IrcMessageFilter may be used to intercept messages before IrcConnection::messageReceived() is emitted and the messages get delivered further. In order to use IrcMessageFilter, it must be installed via IrcConnection::installMessageFilter().

Message filtering is mostly useful for handling specific replies before the rest of the application receives it. This way there is no need to for example ignore and hide such replies later in the application code.

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;
};
See also
IrcConnection::installMessageFilter(), IrcCommandFilter

Constructor & Destructor Documentation

IrcMessageFilter::~IrcMessageFilter ( )
inlinevirtual

Destructs the message filter.

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

See also
IrcConnection::removeMessageFilter()

Member Function Documentation

bool IrcMessageFilter::messageFilter ( IrcMessage message)
pure virtual

Reimplement this function to filter messages from installed connections.

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

See also
IrcConnection::installMessageFilter()