Communi  3.7.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;
QMap<quint64, IrcCommand*> commands;
};
Provides an interface for filtering commands.
Definition: ircfilter.h:48
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
virtual bool messageFilter(IrcMessage *message)=0
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
See also
IrcConnection::installMessageFilter(), IrcCommandFilter

Constructor & Destructor Documentation

◆ ~IrcMessageFilter()

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

◆ messageFilter()

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