Example #1
0
void IrcClient::onTextEntered()
{
    QString input = lineEdit->text();
    IrcCommand* command = parser->parse(input);
    if (command) {
        connection->sendCommand(command);

        // echo own messages (servers do not send our own messages back)
        if (command->type() == IrcCommand::Message || command->type() == IrcCommand::CtcpAction) {
            IrcMessage* msg = command->toMessage(connection->nickName(), connection);
            receiveMessage(msg);
            delete msg;
        }

        lineEdit->clear();
    } else if (input.length() > 1) {
        QString error;
        QString command = lineEdit->text().mid(1).split(" ", QString::SkipEmptyParts).value(0).toUpper();
        if (parser->commands().contains(command))
            error = tr("[ERROR] Syntax: %1").arg(parser->syntax(command).replace("<", "&lt;").replace(">", "&gt;"));
        else
            error = tr("[ERROR] Unknown command: %1").arg(command);
        textEdit->append(IrcMessageFormatter::formatMessage(error));
        lineEdit->setStyleSheet("background: salmon");
    }
}
Example #2
0
void tst_IrcCommand::testDebug()
{
    QString str;
    QDebug dbg(&str);

    dbg << static_cast<IrcCommand*>(0);
    QCOMPARE(str.trimmed(), QString::fromLatin1("IrcCommand(0x0)"));
    str.clear();

    IrcCommand command;
    QTest::ignoreMessage(QtWarningMsg, "Reimplement IrcCommand::toString() for IrcCommand::Custom");
    dbg << &command;
    QVERIFY(QRegExp("IrcCommand\\(0x[0-9A-Fa-f]+, type=Custom\\) ").exactMatch(str));
    str.clear();

    command.setType(IrcCommand::Quit);
    dbg << &command;
    QVERIFY(QRegExp("IrcCommand\\(0x[0-9A-Fa-f]+, type=Quit, \"QUIT :\"\\) ").exactMatch(str));
    str.clear();

    command.setObjectName("foo");
    dbg << &command;
    QVERIFY(QRegExp("IrcCommand\\(0x[0-9A-Fa-f]+, name=foo, type=Quit, \"QUIT :\"\\) ").exactMatch(str));
    str.clear();

    dbg << IrcCommand::Join;
    QCOMPARE(str.trimmed(), QString::fromLatin1("Join"));
    str.clear();
}
Example #3
0
//![receive]
void IrcBot::processMessage(IrcPrivateMessage* message)
{
    if (message->isPrivate()) {
        // private message: reply to the message sender
        // => triggers: "!<cmd> <params>" and "<cmd> <params>"
        parser.setTarget(message->nick());
        parser.setTriggers(QStringList() << "!" << "");
    } else {
        // channel message: reply to the target channel
        // => triggers: "!<cmd> <params>" and "bot: <cmd> <params>"
        parser.setTarget(message->target());
        parser.setTriggers(QStringList() << "!" << nickName().append(":"));
    }

    IrcCommand* cmd = parser.parse(message->content());
    if (cmd) {
        if (cmd->type() == IrcCommand::Custom && cmd->parameters().value(0) == "HELP") {
            help(cmd->parameters().mid(1));
        } else {
            sendCommand(cmd);

            if (cmd->type() == IrcCommand::Quit) {
                connect(this, SIGNAL(disconnected()), qApp, SLOT(quit()));
                QTimer::singleShot(1000, qApp, SLOT(quit()));
            }
        }
    }
}
Example #4
0
IrcCommand* IrcCommandPrivate::createCommand(IrcCommand::Type type, const QStringList& parameters)
{
    IrcCommand* command = new IrcCommand;
    command->setType(type);
    command->setParameters(parameters);
    return command;
}
Example #5
0
void tst_IrcCommand::testDefaults()
{
    IrcCommand cmd;
    QVERIFY(cmd.parameters().isEmpty());
    QCOMPARE(cmd.type(), IrcCommand::Custom);
    QCOMPARE(cmd.encoding(), QByteArray("UTF-8"));

    QTest::ignoreMessage(QtWarningMsg, "Reimplement IrcCommand::toString() for IrcCommand::Custom");
    QVERIFY(cmd.toString().isEmpty());
}
Example #6
0
void tst_IrcCommand::testEncoding()
{
    QFETCH(QByteArray, encoding);
    QFETCH(QByteArray, actual);
    QFETCH(bool, supported);

    if (!supported)
        QTest::ignoreMessage(QtWarningMsg, "IrcCommand::setEncoding(): unsupported encoding \"" + encoding + "\" ");

    IrcCommand cmd;
    cmd.setEncoding(encoding);
    QCOMPARE(cmd.encoding(), actual);
}
Example #7
0
IrcCommand* IrcCommandParserPrivate::parseCommand(const IrcCommandInfo& command, const QString& input) const
{
    IrcCommand* cmd = 0;
    QStringList params;
    if (processParameters(command, input, &params)) {
        const int count = params.count();
        if (count >= command.min && count <= command.max) {
            cmd = new IrcCommand;
            cmd->setType(command.type);
            if (command.type == IrcCommand::Custom)
                params.prepend(command.command);
            cmd->setParameters(params);
        }
    }
    return cmd;
}
Example #8
0
IrcCommand* CommandParser::parseCustomCommand(const QString& command, const QStringList& params, const QString& syntax)
{
	QStringList tokens = syntax.split(" ", QString::SkipEmptyParts);
    int min = 0;
	int max = tokens.count();
	while (!tokens.isEmpty())
	{
		QString p = tokens.takeFirst();
		if ( tokens.isEmpty() && (p.endsWith("...>") || p.endsWith("...>)")) )
            max = INT_MAX;
		if (!p.startsWith("(<"))
			++min;
	}
    if (params.count() >= min && params.count() <= max) {
        IrcCommand* cmd = new IrcCommand;
        cmd->setType(IrcCommand::Custom);
        cmd->setParameters(QStringList(command) + params);
        return cmd;
    }
    return 0;
}
 static IrcCommand* create(const QString& target, const QDateTime &timeStamp)
 {
     IrcCommand* command = new IrcMessageSeenCommand;
     command->setParameters(QStringList() << target << timeStamp.toUTC().toString(Qt::ISODate));
     return command;
 }
Example #10
0
void tst_IrcCommand::testDefaults()
{
    IrcCommand cmd;
    QVERIFY(cmd.type() == IrcCommand::Custom);
    QVERIFY(cmd.parameters().isEmpty());
}