diff options
| author | 2016-04-16 00:02:19 +0200 | |
|---|---|---|
| committer | 2016-04-16 00:02:19 +0200 | |
| commit | 99b60d13b2b504e263c18870a8d55efc5f0a2ea7 (patch) | |
| tree | 0a9728dafdb33a601c6740716791eea1c095288e /irc | |
| parent | e25c9d650eb7c7633fd1a768bff9cde013c7b057 (diff) | |
| download | OneRoll-99b60d13b2b504e263c18870a8d55efc5f0a2ea7.tar.gz OneRoll-99b60d13b2b504e263c18870a8d55efc5f0a2ea7.zip | |
Implement connection to freenode.
remove GUI
Diffstat (limited to 'irc')
| -rw-r--r-- | irc/CMakeLists.txt | 2 | ||||
| -rw-r--r-- | irc/botircdiceparser.cpp | 207 | ||||
| -rw-r--r-- | irc/botircdiceparser.h | 39 | ||||
| -rw-r--r-- | irc/main.cpp | 135 | ||||
| -rw-r--r-- | irc/mainwindow.ui | 6 |
5 files changed, 216 insertions, 173 deletions
diff --git a/irc/CMakeLists.txt b/irc/CMakeLists.txt index c7373c6..d43ffed 100644 --- a/irc/CMakeLists.txt +++ b/irc/CMakeLists.txt @@ -69,7 +69,7 @@ add_executable( ../node/startingnode.cpp ../node/ifnode.cpp ${UI_HEADERS} - mainwindow.cpp + botircdiceparser.cpp main.cpp) diff --git a/irc/botircdiceparser.cpp b/irc/botircdiceparser.cpp index 39bac4e..87d47ff 100644 --- a/irc/botircdiceparser.cpp +++ b/irc/botircdiceparser.cpp @@ -1,14 +1,33 @@ -#include "mainwindow.h" -#include "ui_mainwindow.h" +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://www.rolisteam.org/contact * +* * +* This file is part of DiceParser * +* * +* DiceParser is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the * +* Free Software Foundation, Inc., * +* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * +***************************************************************************/ +#include "botircdiceparser.h" #include <math.h> #include <QDebug> +#include <QString> -MainWindow::MainWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::MainWindow) +BotIrcDiceParser::BotIrcDiceParser(QObject *parent) : + QObject(parent) { - ui->setupUi(this); // Create socket m_socket = new QTcpSocket(this); @@ -19,27 +38,24 @@ MainWindow::MainWindow(QWidget *parent) : connect(m_socket, SIGNAL(readyRead()), this, SLOT(readData())); connect(m_socket,SIGNAL(connected()),this,SLOT(authentificationProcess())); connect(m_socket,SIGNAL(error(QAbstractSocket::SocketError)),this,SLOT(errorOccurs(QAbstractSocket::SocketError))); - connect(ui->m_connectButton, SIGNAL(clicked()), this, SLOT(connectToServer())); - connect(ui->m_disconnectButton, SIGNAL(clicked()), this, SLOT(disconnectFromServer())); - connect(ui->m_joinButton, SIGNAL(clicked()), this, SLOT(joinChannel())); } -MainWindow::~MainWindow() +BotIrcDiceParser::~BotIrcDiceParser() { - delete ui; + // delete ui; } -void MainWindow::connectToServer() +void BotIrcDiceParser::connectToServer() { qDebug() << "start connection"; m_socket->connectToHost(QString("irc.freenode.net"), 8001); } -void MainWindow::errorOccurs(QAbstractSocket::SocketError) +void BotIrcDiceParser::errorOccurs(QAbstractSocket::SocketError) { qDebug() << "ERROR" << m_socket->errorString(); } -void MainWindow::readData() +void BotIrcDiceParser::readData() { qDebug() << "Reply"; @@ -64,19 +80,24 @@ void MainWindow::readData() if(list.size()==2) { QString cmd = list[1]; - if(m_parser->parseLine(cmd.simplified())) + if(!cmd.isEmpty()) { - m_parser->Start(); - QString result = m_parser->displayResult(); - QString msg("PRIVMSG #Rolisteam :%1 \r\n"); - m_socket->write(msg.arg(result).toLatin1()); + + 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 :")) @@ -92,32 +113,160 @@ void MainWindow::readData() m_socket->write(resp.toLatin1()); } } - if(readLine.contains("Found your hostname")) + 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()); + //ui->m_output->append(readLine.trimmed()); + QMessageLogger().debug() << readLine.trimmed(); // Next data?? if(m_socket->canReadLine()) readData(); } +void BotIrcDiceParser::setRegisterName() +{ + m_socket->write(QLatin1String("msg NickServ identify \r\n").data()); + joinChannel(); +} -void MainWindow::disconnectFromServer() +void BotIrcDiceParser::disconnectFromServer() { // Disconnect from IRC server m_socket->write("QUIT Good bye \r\n"); // Good bye is optional message m_socket->flush(); m_socket->disconnect(); // Now we can try it :-) + } - void MainWindow::authentificationProcess() + void BotIrcDiceParser::authentificationProcess() { qDebug() << "authentification"; - m_socket->write("NICK rolisteamDice \r\n"); - m_socket->write("USER rolisteamDice rolisteamDice rolisteamDice :rolisteamDice BOT \r\n"); - m_socket->write("MSG NickServ identify \r\n"); + m_socket->write(QLatin1String("NICK rolisteamDice \r\n").data()); + m_socket->write(QLatin1String("USER rolisteamDice rolisteamDice rolisteamDice :rolisteamDice BOT \r\n").data()); + } -void MainWindow::joinChannel() +void BotIrcDiceParser::joinChannel() +{ + m_socket->write(QLatin1String("JOIN #RolisteamOfficial \r\n").data()); +} +QString BotIrcDiceParser::diceToText(ExportedDiceResult& dice,bool highlight,bool homogeneous) { - m_socket->write("JOIN #Rolisteam \r\n"); + QStringList resultGlobal; + foreach(int face, dice.keys()) + { + QStringList result; + ListDiceResult diceResult = dice.value(face); + //patternColor = patternColorarg(); + foreach (HighLightDice tmp, diceResult) + { + QStringList diceListStr; + QStringList diceListChildren; + + + for(int i =0; i < tmp.getResult().size(); ++i) + { + qint64 dievalue = tmp.getResult()[i]; + QString prefix("%1"); + + if((tmp.isHighlighted())&&(highlight)) + { + if(tmp.getColor().isEmpty()|| tmp.getColor()=="black") + { + prefix = "%1"; + } + if(tmp.getColor()=="white") + { + prefix = "%1"; + } + if(tmp.getColor()=="blue") + { + prefix = "%1"; + } + if(tmp.getColor()=="red") + { + prefix = "%1"; + } + } + + if(i==0) + { + diceListStr << prefix.arg(QString::number(dievalue)); + } + else + { + diceListChildren << prefix.arg(QString::number(dievalue)); + } + } + if(!diceListChildren.isEmpty()) + { + diceListStr << QString("[%1]").arg(diceListChildren.join(' ')); + } + + result << diceListStr.join(' '); + // qDebug() << result << tmp.first << tmp.second; + } + + if(dice.keys().size()>1) + { + resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face); + } + else + { + resultGlobal << result; + } + } + return resultGlobal.join(' '); +} + +QString BotIrcDiceParser::startDiceParsing(QString& cmd,bool highlight) +{ + QString result; + QTextStream out(&result); + if(m_parser->parseLine(cmd)) + { + + m_parser->Start(); + if(!m_parser->getErrorMap().isEmpty()) + { + out << "Error" << m_parser->humanReadableError()<< "\n"; + return QString(); + } + + ExportedDiceResult list; + bool homogeneous = true; + m_parser->getLastDiceResult(list,homogeneous); + QString diceText = diceToText(list,highlight,homogeneous); + QString scalarText; + QString str; + + if(m_parser->hasIntegerResultNotInFirst()) + { + scalarText = QString("%1").arg(m_parser->getLastIntegerResult()); + } + else if(!list.isEmpty()) + { + scalarText = QString("%1").arg(m_parser->getSumOfDiceResult()); + } + if(highlight) + str = QString("Result: %1, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(m_parser->getDiceCommand()); + else + str = QString("Result: %1, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(m_parser->getDiceCommand()); + + if(m_parser->hasStringResult()) + { + str = m_parser->getStringResult(); + } + out << str << "\n"; + + } + else + { + out << m_parser->humanReadableError()<< "\n";; + } + + return result; } diff --git a/irc/botircdiceparser.h b/irc/botircdiceparser.h index d7139d6..5933b12 100644 --- a/irc/botircdiceparser.h +++ b/irc/botircdiceparser.h @@ -1,36 +1,55 @@ +/*************************************************************************** +* Copyright (C) 2014 by Renaud Guezennec * +* http://www.rolisteam.org/contact * +* * +* This file is part of DiceParser * +* * +* DiceParser is free software; you can redistribute it and/or modify * +* it under the terms of the GNU General Public License as published by * +* the Free Software Foundation; either version 2 of the License, or * +* (at your option) any later version. * +* * +* This program is distributed in the hope that it will be useful, * +* but WITHOUT ANY WARRANTY; without even the implied warranty of * +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * +* GNU General Public License for more details. * +* * +* You should have received a copy of the GNU General Public License * +* along with this program; if not, write to the * +* Free Software Foundation, Inc., * +* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * +***************************************************************************/ #ifndef MAINWINDOW_H #define MAINWINDOW_H -#include <QMainWindow> #include "diceparser.h" #include <QTcpSocket> -namespace Ui { -class MainWindow; -} - -class MainWindow : public QMainWindow +class BotIrcDiceParser : public QObject { Q_OBJECT public: - explicit MainWindow(QWidget *parent = 0); - virtual ~MainWindow(); + explicit BotIrcDiceParser(QObject *parent = 0); + virtual ~BotIrcDiceParser(); + QString diceToText(ExportedDiceResult &dice, bool highlight, bool homogeneous); + QString startDiceParsing(QString &cmd, bool highlight); public slots: void errorOccurs(QAbstractSocket::SocketError); + void connectToServer(); private: - Ui::MainWindow *ui; + //Ui::BotIrcDiceParser *ui; QTcpSocket * m_socket; DiceParser* m_parser; private slots: void readData(); - void connectToServer(); void disconnectFromServer(); void authentificationProcess(); void joinChannel(); + void setRegisterName(); }; #endif // MAINWINDOW_H diff --git a/irc/main.cpp b/irc/main.cpp index 4a58256..23c9dbf 100644 --- a/irc/main.cpp +++ b/irc/main.cpp @@ -20,17 +20,8 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -/*#include <QStringList> -#include "diceparser.h" -#include <QCommandLineParser> -#include <QCommandLineOption> -#include "highlightdice.h" -*/ - - - -#include <QApplication> -#include "mainwindow.h" +#include <QCoreApplication> +#include "botircdiceparser.h" /** * @page Dice * The cli for DiceParser the new dice system from rolisteam. @@ -45,132 +36,16 @@ */ int main(int argc, char *argv[]) { - QApplication app(argc,argv); - MainWindow window; - window.show(); + QCoreApplication app(argc,argv); + BotIrcDiceParser bot; + bot.connectToServer(); return app.exec(); } /*QTextStream out(stdout, QIODevice::WriteOnly); -QString diceToText(ExportedDiceResult& dice,bool highlight,bool homogeneous) -{ - QStringList resultGlobal; - foreach(int face, dice.keys()) - { - QStringList result; - ListDiceResult diceResult = dice.value(face); - //patternColor = patternColorarg(); - foreach (HighLightDice tmp, diceResult) - { - QStringList diceListStr; - QStringList diceListChildren; - - - for(int i =0; i < tmp.getResult().size(); ++i) - { - qint64 dievalue = tmp.getResult()[i]; - QString prefix("%1"); - if((tmp.isHighlighted())&&(highlight)) - { - if(tmp.getColor().isEmpty()|| tmp.getColor()=="black") - { - prefix = "\e[0;31m%1\e[0m"; - } - if(tmp.getColor()=="white") - { - prefix = "\e[97m%1\e[0m"; - } - if(tmp.getColor()=="blue") - { - prefix = "\e[34m%1\e[0m"; - } - } - - if(i==0) - { - diceListStr << prefix.arg(QString::number(dievalue)); - } - else - { - diceListChildren << prefix.arg(QString::number(dievalue)); - } - } - if(!diceListChildren.isEmpty()) - { - diceListStr << QString("[%1]").arg(diceListChildren.join(' ')); - } - - result << diceListStr.join(' '); - // qDebug() << result << tmp.first << tmp.second; - } - - if(dice.keys().size()>1) - { - resultGlobal << QString(" d%2:(%1)").arg(result.join(',')).arg(face); - } - else - { - resultGlobal << result; - } - } - return resultGlobal.join(' '); -} - -void startDiceParsing(QString& cmd,QString& treeFile,bool highlight) -{ - DiceParser* parser = new DiceParser(); - - if(parser->parseLine(cmd)) - { - // - if(treeFile.isEmpty()) - { - parser->Start(); - if(!parser->getErrorMap().isEmpty()) - { - out << "Error" << parser->humanReadableError()<< "\n"; - return; - } - - ExportedDiceResult list; - bool homogeneous = true; - parser->getLastDiceResult(list,homogeneous); - QString diceText = diceToText(list,highlight,homogeneous); - QString scalarText; - QString str; - - if(parser->hasIntegerResultNotInFirst()) - { - scalarText = QString("%1").arg(parser->getLastIntegerResult()); - } - else if(!list.isEmpty()) - { - scalarText = QString("%1").arg(parser->getSumOfDiceResult()); - } - if(highlight) - str = QString("Result: \e[0;31m%1\e[0m, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(parser->getDiceCommand()); - else - str = QString("Result: %1, details:[%3 (%2)]").arg(scalarText).arg(diceText).arg(parser->getDiceCommand()); - - if(parser->hasStringResult()) - { - str = parser->getStringResult(); - } - out << str << "\n"; - } - else - { - parser->writeDownDotTree(treeFile); - } - } - else - { - out << parser->humanReadableError()<< "\n";; - } -} void usage() { diff --git a/irc/mainwindow.ui b/irc/mainwindow.ui index d15d229..198a293 100644 --- a/irc/mainwindow.ui +++ b/irc/mainwindow.ui @@ -1,7 +1,7 @@ <?xml version="1.0" encoding="UTF-8"?> <ui version="4.0"> - <class>MainWindow</class> - <widget class="QMainWindow" name="MainWindow"> + <class>BotIrcDiceParser</class> + <widget class="QBotIrcDiceParser" name="BotIrcDiceParser"> <property name="geometry"> <rect> <x>0</x> @@ -11,7 +11,7 @@ </rect> </property> <property name="windowTitle"> - <string>MainWindow</string> + <string>BotIrcDiceParser</string> </property> <widget class="QWidget" name="centralwidget"> <layout class="QVBoxLayout" name="verticalLayout"> |