Esempio n. 1
0
void MyIrcSession::on_messageReceived(IrcMessage *message) {
	IrcPrivateMessage *m = (IrcPrivateMessage *) message;
	if (m->isRequest()) {
		QString request = m->message().split(" ", QString::SkipEmptyParts).value(0).toUpper();
		if (request == "PING" || request == "TIME" || request == "VERSION") {
			LOG4CXX_INFO(logger, user << ": " << TO_UTF8(request) << " received and has been answered");
			return;
		}
	}

	QString msg = m->message();
	if (m->isAction()) {
		msg = QString("/me ") + msg;
	}

	std::string target = TO_UTF8(m->target());
	LOG4CXX_INFO(logger, user << ": Message from " << target);
	if (target.find("#") == 0) {
		std::string nickname = TO_UTF8(m->sender().name());
		correctNickname(nickname);
		np->handleMessage(user, target + suffix, TO_UTF8(msg), nickname);
	}
	else {
		std::string nickname = TO_UTF8(m->sender().name());
		correctNickname(nickname);
		LOG4CXX_INFO(logger, nickname + suffix);
		np->handleMessage(user, nickname + suffix, TO_UTF8(msg));
	}
}
Esempio n. 2
0
void MyIrcSession::on_messageReceived(IrcMessage *message) {
	IrcPrivateMessage *m = (IrcPrivateMessage *) message;
	if (m->isRequest()) {
		QString request = m->message().split(" ", QString::SkipEmptyParts).value(0).toUpper();
		if (request == "PING" || request == "TIME" || request == "VERSION") {
			LOG4CXX_INFO(logger, user << ": " << TO_UTF8(request) << " received and has been answered");
			return;
		}
	}

	QString msg = m->message();
	if (m->isAction()) {
		msg = QString("/me ") + msg;
	}
	QString html = "";//msg;
	CommuniBackport::toPlainText(msg);

	// TODO: Communi produces invalid html now...
// 	if (html == msg) {
// 		html = "";
// 	}
// 	else {
// 		html = IrcUtil::messageToHtml(html);
// 	}

	std::string target = TO_UTF8(m->target().toLower());
	LOG4CXX_INFO(logger, user << ": Message from " << target);
	if (target.find("#") == 0) {
		std::string nickname = TO_UTF8(m->sender().name());
		correctNickname(nickname);
		np->handleMessage(user, target + suffix, TO_UTF8(msg), nickname, TO_UTF8(html));
	}
	else {
		std::string nickname = TO_UTF8(m->sender().name());
		correctNickname(nickname);
		if (m_pms.find(nickname) != m_pms.end()) {
			if (hasIRCBuddy(m_pms[nickname], nickname)) {
				LOG4CXX_INFO(logger, nickname);
				np->handleMessage(user, m_pms[nickname] + suffix, TO_UTF8(msg), nickname, TO_UTF8(html), "", false, true);
				return;
			}
			else {
				nickname = nickname + suffix;
			}
		}
		else {
			nickname = nickname + suffix;
		}

		LOG4CXX_INFO(logger, nickname);
		np->handleMessage(user, nickname, TO_UTF8(msg), "", TO_UTF8(html));
	}
}
Esempio n. 3
0
void IrcSessionPrivate::receiveMessage(IrcMessage* msg)
{
    Q_Q(IrcSession);
    switch (msg->type()) {
        case IrcMessage::Numeric: {
            IrcNumericMessage* numeric = static_cast<IrcNumericMessage*>(msg);
            if (numeric->code() == Irc::RPL_WELCOME) {
                setNick(msg->parameters().value(0));
                setConnected(true);
            } else if (numeric->code() == Irc::RPL_ISUPPORT) {
                foreach (const QString& param, msg->parameters().mid(1)) {
                    QStringList keyValue = param.split("=", QString::SkipEmptyParts);
                    info.insert(keyValue.value(0), keyValue.value(1));
                }
                emit q->sessionInfoReceived(IrcSessionInfo(q));
            }
            break;
        }
        case IrcMessage::Ping:
            q->sendRaw("PONG " + static_cast<IrcPingMessage*>(msg)->argument());
            break;
        case IrcMessage::Private: {
            IrcPrivateMessage* privMsg = static_cast<IrcPrivateMessage*>(msg);
            if (privMsg->isRequest()) {
                IrcCommand* reply = q->createCtcpReply(privMsg);
                if (reply)
                    q->sendCommand(reply);
            }
            break;
        }
        case IrcMessage::Nick:
            if (msg->flags() & IrcMessage::Own)
                setNick(static_cast<IrcNickMessage*>(msg)->nick());
            break;
        case IrcMessage::Capability: {
            IrcCapabilityMessage* capMsg = static_cast<IrcCapabilityMessage*>(msg);
            QString subCommand = capMsg->subCommand();
            if (subCommand == "LS") {
                foreach (const QString& cap, capMsg->capabilities())
                    handleCapability(&availableCaps, cap);

                if (!connected) {
                    QStringList params = capMsg->parameters();
                    if (params.value(params.count() - 1) != QLatin1String("*")) {
                        QStringList request;
                        emit q->capabilities(availableCaps.toList(), &request);
                        if (!request.isEmpty())
                            q->sendCommand(IrcCommand::createCapability("REQ", request));
                        else
                            q->sendData("CAP END");
                    }
                }
            } else if (subCommand == "ACK" || subCommand == "NAK") {
Esempio n. 4
0
void tst_IrcMessage::testPrivateMessage()
{
    QFETCH(bool, valid);
    QFETCH(QString, cap);
    QFETCH(QByteArray, data);
    QFETCH(QString, target);
    QFETCH(QString, content);
    QFETCH(bool, priv);
    QFETCH(bool, action);
    QFETCH(bool, request);
    QFETCH(uint, flags);

    IrcConnection connection;
    connection.setNickName("communi");
    TestProtocol protocol(cap, &connection);
    static_cast<FriendConnection*>(&connection)->setProtocol(&protocol);

    IrcMessage* message = IrcMessage::fromData(data, &connection);
    QCOMPARE(message->type(), IrcMessage::Private);
    QCOMPARE(message->command(), QString("PRIVMSG"));
    QCOMPARE(message->property("valid").toBool(), valid);
    QCOMPARE(message->property("target").toString(), target);
    QCOMPARE(message->property("content").toString(), content);
    QCOMPARE(message->property("private").toBool(), priv);
    QCOMPARE(message->property("action").toBool(), action);
    QCOMPARE(message->property("request").toBool(), request);
    QCOMPARE(message->property("flags").toUInt(), flags);

    IrcPrivateMessage* privateMessage = qobject_cast<IrcPrivateMessage*>(message);
    QVERIFY(privateMessage);
    QCOMPARE(privateMessage->isValid(), valid);
    QCOMPARE(privateMessage->target(), target);
    QCOMPARE(privateMessage->content(), content);
    QCOMPARE(privateMessage->isPrivate(), priv);
    QCOMPARE(privateMessage->isAction(), action);
    QCOMPARE(privateMessage->isRequest(), request);
    QCOMPARE(static_cast<uint>(privateMessage->flags()), flags);
}
Esempio n. 5
0
void IrcSessionPrivate::processLine(const QByteArray& line)
{
    Q_Q(IrcSession);

    QString encoded = encoder.encode(line);
    static bool dbg = qgetenv("COMMUNI_DEBUG").toInt();
    if (dbg) qDebug() << encoded;

    IrcMessage* msg = IrcMessage::fromString(encoded);
    if (msg)
    {
        switch (msg->type())
        {
        case IrcMessage::Numeric:
            if (static_cast<IrcNumericMessage*>(msg)->code() == Irc::RPL_WELCOME)
            {
                setNick(msg->parameters().value(0));
                connected = true;
                emit q->connected();
                emit q->connectedChanged(true);
            }
            else if (static_cast<IrcNumericMessage*>(msg)->code() == Irc::RPL_ISUPPORT)
            {
                foreach (const QString& param, msg->parameters().mid(1))
                {
                    const QStringList keyValue = param.split("=", QString::SkipEmptyParts);
                    info.insert(keyValue.value(0), keyValue.value(1));
                }
            }
            break;
        case IrcMessage::Ping:
            q->sendRaw("PONG " + static_cast<IrcPingMessage*>(msg)->argument());
            break;
        case IrcMessage::Private: {
            IrcPrivateMessage* privMsg = static_cast<IrcPrivateMessage*>(msg);
            if (privMsg->isRequest())
            {
                QString reply;
                QString request = privMsg->message().split(" ", QString::SkipEmptyParts).value(0).toUpper();
                if (request == "PING")
                    reply = privMsg->message();
                else if (request == "TIME")
                    reply = "TIME " + QLocale().toString(QDateTime::currentDateTime(), QLocale::ShortFormat);
                else if (request == "VERSION")
                    reply = QString("VERSION Communi ") + Irc::version();
                if (!reply.isNull())
                    q->sendCommand(IrcCommand::createCtcpReply(msg->sender().name(), reply));
            }
            break;
            }
        case IrcMessage::Nick:
            if (msg->sender().name() == nickName)
                setNick(static_cast<IrcNickMessage*>(msg)->nick());
            break;
        default:
            break;
        }

        emit q->messageReceived(msg);
        msg->deleteLater();
    }
Esempio n. 6
0
void IrcSessionPrivate::processLine(const QByteArray& line)
{
    Q_Q(IrcSession);

    static bool dbg = qgetenv("COMMUNI_DEBUG").toInt();
    if (dbg) qDebug() << line;

    IrcMessage* msg = IrcMessage::fromData(line, encoding, q);
    if (msg)
    {
        switch (msg->type())
        {
        case IrcMessage::Numeric:
            if (static_cast<IrcNumericMessage*>(msg)->code() == Irc::RPL_WELCOME)
            {
                setNick(msg->parameters().value(0));
                setConnected(true);
            }
            break;
        case IrcMessage::Ping:
            q->sendRaw("PONG " + static_cast<IrcPingMessage*>(msg)->argument());
            break;
        case IrcMessage::Private: {
            IrcPrivateMessage* privMsg = static_cast<IrcPrivateMessage*>(msg);
            if (privMsg->isRequest())
            {
                IrcCommand* reply = q->createCtcpReply(privMsg);
                if (reply)
                    q->sendCommand(reply);
            }
            break;
            }
        case IrcMessage::Nick:
            if (msg->isOwn())
                setNick(static_cast<IrcNickMessage*>(msg)->nick());
            break;
        case IrcMessage::Capability:
            if (!connected)
            {
                IrcCapabilityMessage* capMsg = static_cast<IrcCapabilityMessage*>(msg);
                QString subCommand = capMsg->subCommand();
                if (subCommand == "LS")
                {
                    foreach (const QString& cap, capMsg->capabilities())
                        capabilities.insert(cap);

                    QStringList params = capMsg->parameters();
                    if (params.value(params.count() - 1) != QLatin1String("*"))
                    {
                        QStringList request;
                        emit q->capabilities(capabilities.toList(), &request);
                        if (!request.isEmpty())
                            q->sendCommand(IrcCommand::createCapability("REQ", request));
                        else
                            q->sendData("CAP END");
                    }
                }
                else if (subCommand == "ACK" || subCommand == "NAK")
                {
                    q->sendData("CAP END");
                }
            }