void BotIrcDiceParser::readData()
{
    qDebug() << "Reply";
    QString readLine= m_socket->readLine();

    if(readLine.startsWith("!"))
        readLine= readLine.remove(0, 1);

    if(readLine.contains("!"))
    {
        // qDebug()<< "in /dice";
        QString dice= ".*PRIVMSG.*!(.*)";
        QRegExp exp(dice);
        exp.indexIn(readLine);

        QStringList list= exp.capturedTexts();
        qDebug() << list;
        if(list.size() == 2)
        {
            QString cmd= list[1];
            if(!cmd.isEmpty())
            {
                cmd= cmd.simplified();
                QString result= startDiceParsing(cmd, true);
                if(!result.isEmpty())
                {
                    QString msg("PRIVMSG #RolisteamOfficial :%1 \r\n");
                    m_socket->write(msg.arg(result).toLatin1());
                }
            }
        }
        else
        {
            return;
        }
        //
    }
    else if(readLine.contains("PING :"))
    {
        QString dice= "PING :(.*)";
        QRegExp exp(dice);
        exp.indexIn(readLine);
        QStringList list= exp.capturedTexts();
        if(list.size() == 2)
        {
            QString resp= "PONG :" + list[1];

            m_socket->write(resp.toLatin1());
        }
    }
    if(readLine.contains(QLatin1String("Found your hostname")))
    {
        authentificationProcess();
    }
    if(readLine.contains(QLatin1String("msg NickServ identify")))
    {
        setRegisterName();
    }
    // Add to ouput
    // ui->m_output->append(readLine.trimmed());
    QMessageLogger().debug() << readLine.trimmed();
    // Next data??
    if(m_socket->canReadLine())
        readData();
}
Example #2
0
DiceServer::DiceServer(int port)
    : QObject(),m_diceParser(new DiceParser())
{

    m_diceParser->setPathToHelp("<span><a href=\"https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md\">Documentation</a>");
   // using namespace ;
    m_server = new qhttp::server::QHttpServer(this);
    m_server->listen( // listening on 0.0.0.0:8080
            QHostAddress::Any, port,
            [=](qhttp::server::QHttpRequest* req, qhttp::server::QHttpResponse* res)
            {
                req->collectData(1024);

               // qhttp::THeaderHash hash = req->headers();
               // qDebug() << hash << res->headers() << qhttp::Stringify::toString(req->method()) << qPrintable(req->url().toString()) << req->collectedData().constData();
                QString getArg = req->url().toString();
                getArg=getArg.replace("/?","");
                QStringList args = getArg.split('&');
                QHash<QString,QString> m_hashArgs ;
                for( auto argument : args)
                {
                    QStringList keyValue = argument.split('=');
                    if(keyValue.size()==2)
                    {
                        m_hashArgs.insert(keyValue[0],keyValue[1]);
                    }
                }

                if(m_hashArgs.contains("cmd"))
                {
                    qDebug() << QUrl::fromPercentEncoding(m_hashArgs["cmd"].toLocal8Bit());
                    QString result = startDiceParsing(QUrl::fromPercentEncoding(m_hashArgs["cmd"].toLocal8Bit()));
                    qDebug() << result;

                    res->setStatusCode(qhttp::ESTATUS_OK);
                    res->addHeader("Access-Control-Allow-Origin", "*");
                    res->addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS");
                    res->addHeader("Access-Control-Allow-Headers", "x-requested-with");


                    QString html("<!doctype html>\n"
                             "<html>\n"
                             "<head>\n"
                             "  <meta charset=\"utf-8\">\n"
                             "  <title>Rolisteam Dice System Webservice</title>\n"
                             "  <style>.dice {color:#FF0000;font-weight: bold;}</style>"
                             "</head>\n"
                             "<body>\n"
                             "%1\n"
                             "</body>\n"
                             "</html>\n");

                    res->end(html.arg(result).toLocal8Bit());
                }
                else
                {
                    res->setStatusCode(qhttp::ESTATUS_OK);
                    res->end("No Command found!\n");
                }

        });
    if ( !m_server->isListening() ) {
            qDebug() << "failed to listen";

        }
    else
    {
        qDebug()<< "Server is On!!";
    }
}