cleaner code, nogui mode more stable
This commit is contained in:
parent
56f1082907
commit
d86feee482
6
.gitignore
vendored
6
.gitignore
vendored
@ -1,2 +1,6 @@
|
|||||||
build/*
|
build/*
|
||||||
*.user
|
debug/*
|
||||||
|
release/*
|
||||||
|
*.user
|
||||||
|
*.Debug
|
||||||
|
*.Release
|
35
main.cpp
35
main.cpp
@ -5,45 +5,46 @@
|
|||||||
|
|
||||||
int main(int argc, char *argv[])
|
int main(int argc, char *argv[])
|
||||||
{
|
{
|
||||||
// set up the socket
|
// create the socket
|
||||||
SocketIRC sock;
|
SocketIRC sock;
|
||||||
sock.setServer("irc.freenode.net");
|
sock.setServer("irc.freenode.net");
|
||||||
sock.setPort(6667);
|
sock.setPort(6667);
|
||||||
|
|
||||||
// set up the bot
|
// create the bot
|
||||||
SparrowBot* bot = new SparrowBot("SparrowBot", "epicsparrow");
|
SparrowBot* bot = new SparrowBot("SparrowBot", "epicsparrow");
|
||||||
|
|
||||||
|
// connect the bot to the socket
|
||||||
QObject::connect(&sock, SIGNAL(receivedMsg(QString)), bot, SLOT(receiveMsg(QString)));
|
QObject::connect(&sock, SIGNAL(receivedMsg(QString)), bot, SLOT(receiveMsg(QString)));
|
||||||
QObject::connect(bot, SIGNAL(sendMsg(QString)), &sock, SLOT(sendMsg(QString)));
|
QObject::connect(bot, SIGNAL(sendMsg(QString)), &sock, SLOT(sendMsg(QString)));
|
||||||
QObject::connect(bot, SIGNAL(changeSocketStatus(int)), &sock, SLOT(setConnected(int)));
|
|
||||||
|
|
||||||
// set up Qt
|
|
||||||
|
|
||||||
|
// parsing arguments
|
||||||
bool nogui = argc > 1 && QString(argv[1]).compare("-nogui") == 0;
|
bool nogui = argc > 1 && QString(argv[1]).compare("-nogui") == 0;
|
||||||
|
|
||||||
// set up UI
|
// set up UI
|
||||||
if(!nogui)
|
if(!nogui)
|
||||||
{
|
{
|
||||||
QApplication app(argc, argv);
|
QApplication app(argc, argv);
|
||||||
|
|
||||||
|
// init window
|
||||||
MainWindow w;
|
MainWindow w;
|
||||||
|
|
||||||
// bind signals to slots
|
|
||||||
QObject::connect(&sock, SIGNAL(receivedMsg(QString)), w.getConsole(), SLOT(append(QString)));
|
QObject::connect(&sock, SIGNAL(receivedMsg(QString)), w.getConsole(), SLOT(append(QString)));
|
||||||
QObject::connect(&sock, SIGNAL(stateChanged(int)), w.getLed(), SLOT(setValue(int)));
|
QObject::connect(bot, SIGNAL(sendMsg(QString)), w.getConsole(), SLOT(append(QString)));
|
||||||
QObject::connect(&sock, SIGNAL(stateChanged(int)), w.getSwitch(), SLOT(setValue(int)));
|
|
||||||
|
|
||||||
QObject::connect(w.getPrompt(), SIGNAL(sendMsg(QString)), &sock, SLOT(sendMsg(QString)));
|
QObject::connect(w.getPrompt(), SIGNAL(sendMsg(QString)), &sock, SLOT(sendMsg(QString)));
|
||||||
QObject::connect(w.getPrompt(), SIGNAL(returnPressed()), w.getPrompt(), SLOT(confirmationPerformed()));
|
QObject::connect(w.getPrompt(), SIGNAL(returnPressed()), w.getPrompt(), SLOT(confirmationPerformed()));
|
||||||
|
|
||||||
QObject::connect(w.getSwitch(), SIGNAL(valueChanged(int)), bot, SLOT(forceStatus(int)));
|
|
||||||
|
|
||||||
// show window
|
|
||||||
w.show();
|
w.show();
|
||||||
return app.exec();
|
|
||||||
|
QObject::connect(&app, SIGNAL(aboutToQuit()), bot, SLOT(disconnect()));
|
||||||
|
|
||||||
|
sock.connectToServer(&app);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
QCoreApplication coreApp(argc, argv);
|
QCoreApplication coreApp(argc, argv);
|
||||||
bot->forceStatus(1);
|
|
||||||
return coreApp.exec();
|
QObject::connect(&coreApp, SIGNAL(aboutToQuit()), bot, SLOT(disconnect()));
|
||||||
|
|
||||||
|
sock.connectToServer(&coreApp);
|
||||||
}
|
}
|
||||||
|
delete(bot);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -13,16 +13,6 @@ QTextBrowser* MainWindow::getConsole()
|
|||||||
return this->ui->monitoringConsole;
|
return this->ui->monitoringConsole;
|
||||||
}
|
}
|
||||||
|
|
||||||
QSlider* MainWindow::getSwitch()
|
|
||||||
{
|
|
||||||
return this->ui->connectSwitch;
|
|
||||||
}
|
|
||||||
|
|
||||||
QProgressBar* MainWindow::getLed()
|
|
||||||
{
|
|
||||||
return this->ui->statusLed;
|
|
||||||
}
|
|
||||||
|
|
||||||
Prompt* MainWindow::getPrompt()
|
Prompt* MainWindow::getPrompt()
|
||||||
{
|
{
|
||||||
return this->ui->prompt;
|
return this->ui->prompt;
|
||||||
|
@ -15,35 +15,6 @@
|
|||||||
</property>
|
</property>
|
||||||
<widget class="QWidget" name="centralWidget">
|
<widget class="QWidget" name="centralWidget">
|
||||||
<layout class="QGridLayout" name="gridLayout">
|
<layout class="QGridLayout" name="gridLayout">
|
||||||
<item row="2" column="0">
|
|
||||||
<widget class="QSlider" name="connectSwitch">
|
|
||||||
<property name="maximum">
|
|
||||||
<number>1</number>
|
|
||||||
</property>
|
|
||||||
<property name="pageStep">
|
|
||||||
<number>1</number>
|
|
||||||
</property>
|
|
||||||
<property name="orientation">
|
|
||||||
<enum>Qt::Horizontal</enum>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="2" column="1">
|
|
||||||
<widget class="QProgressBar" name="statusLed">
|
|
||||||
<property name="maximum">
|
|
||||||
<number>1</number>
|
|
||||||
</property>
|
|
||||||
<property name="value">
|
|
||||||
<number>0</number>
|
|
||||||
</property>
|
|
||||||
<property name="textVisible">
|
|
||||||
<bool>false</bool>
|
|
||||||
</property>
|
|
||||||
<property name="invertedAppearance">
|
|
||||||
<bool>false</bool>
|
|
||||||
</property>
|
|
||||||
</widget>
|
|
||||||
</item>
|
|
||||||
<item row="1" column="0" colspan="2">
|
<item row="1" column="0" colspan="2">
|
||||||
<widget class="QTextBrowser" name="monitoringConsole"/>
|
<widget class="QTextBrowser" name="monitoringConsole"/>
|
||||||
</item>
|
</item>
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
SocketIRC::SocketIRC() : server("irc.freenode.net"), port(6667), isConnected(false)
|
SocketIRC::SocketIRC() : server("irc.freenode.net"), port(6667), isConnected(false)
|
||||||
{
|
{
|
||||||
connect(&sock, SIGNAL(readyRead()), this, SLOT(readMsg()));
|
connect(&sock, SIGNAL(readyRead()), this, SLOT(readMsg()));
|
||||||
|
connect(&sock, SIGNAL(disconnected()), this, SLOT(onDisconnect()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketIRC::setServer(std::string server_)
|
void SocketIRC::setServer(std::string server_)
|
||||||
@ -18,34 +19,25 @@ void SocketIRC::setPort(int port_)
|
|||||||
|
|
||||||
void SocketIRC::sendMsg(QString msg)
|
void SocketIRC::sendMsg(QString msg)
|
||||||
{
|
{
|
||||||
if(isConnected)
|
msg.append("\r\n");
|
||||||
{
|
sock.write(msg.toStdString().c_str());
|
||||||
sock.write(msg.toStdString().c_str());
|
sock.flush();
|
||||||
sock.flush();
|
|
||||||
std::cout << msg.toStdString();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketIRC::setConnected(int c)
|
void SocketIRC::connectToServer(QCoreApplication* app_)
|
||||||
{
|
{
|
||||||
if(c != isConnected)
|
app = app_;
|
||||||
|
|
||||||
|
sock.connectToHost(QString(server.c_str()), quint16(port));
|
||||||
|
if(!sock.waitForConnected(3000))
|
||||||
{
|
{
|
||||||
if(isConnected)
|
std::cerr << "failed to connect : " << sock.errorString().toStdString() << std::endl;
|
||||||
{
|
if(app != NULL)
|
||||||
sock.close();
|
app->exit();
|
||||||
isConnected = false;
|
|
||||||
emit stateChanged(isConnected);
|
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
exit(0);
|
||||||
sock.connectToHost(QString(server.c_str()), quint16(port));
|
|
||||||
if(sock.waitForConnected(5000))
|
|
||||||
isConnected = true;
|
|
||||||
else
|
|
||||||
std::cerr << "failed to connect : " << sock.errorString().toStdString() << std::endl;
|
|
||||||
emit stateChanged(isConnected);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
app->exec();
|
||||||
}
|
}
|
||||||
|
|
||||||
void SocketIRC::readMsg()
|
void SocketIRC::readMsg()
|
||||||
@ -59,3 +51,8 @@ void SocketIRC::readMsg()
|
|||||||
emit receivedMsg(QString(buffer));
|
emit receivedMsg(QString(buffer));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SocketIRC::onDisconnect()
|
||||||
|
{
|
||||||
|
app->exit();
|
||||||
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <QTcpSocket>
|
#include <QTcpSocket>
|
||||||
#include <QPushButton>
|
#include <QPushButton>
|
||||||
#include <QThread>
|
#include <QThread>
|
||||||
|
#include <QCoreApplication>
|
||||||
|
|
||||||
class SocketIRC : public QObject
|
class SocketIRC : public QObject
|
||||||
{
|
{
|
||||||
@ -14,9 +15,11 @@ public:
|
|||||||
SocketIRC();
|
SocketIRC();
|
||||||
void setServer(std::string server_);
|
void setServer(std::string server_);
|
||||||
void setPort(int port_);
|
void setPort(int port_);
|
||||||
|
void connectToServer(QCoreApplication* app_);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
|
QCoreApplication* app;
|
||||||
std::string server;
|
std::string server;
|
||||||
int port;
|
int port;
|
||||||
QTcpSocket sock;
|
QTcpSocket sock;
|
||||||
@ -24,14 +27,13 @@ private:
|
|||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void readMsg();
|
void readMsg();
|
||||||
|
void onDisconnect();
|
||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void sendMsg(QString msg);
|
void sendMsg(QString msg);
|
||||||
void setConnected(int c);
|
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void receivedMsg(QString msg);
|
void receivedMsg(QString msg);
|
||||||
void stateChanged(int newState);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SOCKETIRC_H
|
#endif // SOCKETIRC_H
|
||||||
|
@ -31,21 +31,6 @@ void SparrowBot::receiveMsg(QString str)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void SparrowBot::forceStatus(int newStatus)
|
|
||||||
{
|
|
||||||
emit changeSocketStatus(newStatus);
|
|
||||||
if(newStatus)
|
|
||||||
{
|
|
||||||
//status = ONLINE;
|
|
||||||
//sendRawMessage("NICK " + nick + "\r\nUSER " + nick + " 0 * " + nick + "\r\n");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
status = OFFLINE;
|
|
||||||
users.clear();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void SparrowBot::handleMessage(Message msg)
|
void SparrowBot::handleMessage(Message msg)
|
||||||
{
|
{
|
||||||
// si la ligne est un message
|
// si la ligne est un message
|
||||||
@ -53,10 +38,8 @@ void SparrowBot::handleMessage(Message msg)
|
|||||||
{
|
{
|
||||||
// mise à jour du bot
|
// mise à jour du bot
|
||||||
if(msg.args.compare("!update") == 0 && msg.src != NULL && msg.src->isOp())
|
if(msg.args.compare("!update") == 0 && msg.src != NULL && msg.src->isOp())
|
||||||
{
|
disconnect("Rebooting for update...");
|
||||||
emit sendMsg("QUIT :i'll be back\r\n");
|
|
||||||
exit(0);
|
|
||||||
}
|
|
||||||
// affiche les gens connectés
|
// affiche les gens connectés
|
||||||
if(msg.args.compare("!list") == 0)
|
if(msg.args.compare("!list") == 0)
|
||||||
{
|
{
|
||||||
@ -95,7 +78,7 @@ void SparrowBot::handleMessage(Message msg)
|
|||||||
for(QString s : names)
|
for(QString s : names)
|
||||||
{
|
{
|
||||||
users.getOrAdd(s);
|
users.getOrAdd(s);
|
||||||
emit sendMsg(QString("WHOIS %1\r\n").arg(s));
|
emit sendMsg(QString("WHOIS %1").arg(s));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -117,11 +100,21 @@ void SparrowBot::handleMessage(Message msg)
|
|||||||
|
|
||||||
void SparrowBot::say(QString str)
|
void SparrowBot::say(QString str)
|
||||||
{
|
{
|
||||||
emit sendMsg(QString("PRIVMSG #%1 :%2\r\n").arg(chan).arg(str));
|
emit sendMsg(QString("PRIVMSG #%1 :%2").arg(chan).arg(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
void SparrowBot::action(QString str)
|
void SparrowBot::action(QString str)
|
||||||
{
|
{
|
||||||
emit sendMsg(QString("PRIVMSG #%1 :ACTION %2\r\n").arg(chan).arg(str));
|
emit sendMsg(QString("PRIVMSG #%1 :ACTION %2 ").arg(chan).arg(str));
|
||||||
|
}
|
||||||
|
|
||||||
|
void SparrowBot::disconnect()
|
||||||
|
{
|
||||||
|
disconnect("I'll be back");
|
||||||
|
}
|
||||||
|
|
||||||
|
void SparrowBot::disconnect(QString str)
|
||||||
|
{
|
||||||
|
emit sendMsg(QString("QUIT :%1").arg(str));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,8 @@ private:
|
|||||||
|
|
||||||
public slots:
|
public slots:
|
||||||
void receiveMsg(QString msg);
|
void receiveMsg(QString msg);
|
||||||
void forceStatus(int newStatus);
|
void disconnect();
|
||||||
|
void disconnect(QString str);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void sendMsg(QString msg);
|
void sendMsg(QString msg);
|
||||||
|
74
ui_mainwindow.h
Normal file
74
ui_mainwindow.h
Normal file
@ -0,0 +1,74 @@
|
|||||||
|
/********************************************************************************
|
||||||
|
** Form generated from reading UI file 'mainwindow.ui'
|
||||||
|
**
|
||||||
|
** Created by: Qt User Interface Compiler version 5.4.1
|
||||||
|
**
|
||||||
|
** WARNING! All changes made in this file will be lost when recompiling UI file!
|
||||||
|
********************************************************************************/
|
||||||
|
|
||||||
|
#ifndef UI_MAINWINDOW_H
|
||||||
|
#define UI_MAINWINDOW_H
|
||||||
|
|
||||||
|
#include <QtCore/QVariant>
|
||||||
|
#include <QtWidgets/QAction>
|
||||||
|
#include <QtWidgets/QApplication>
|
||||||
|
#include <QtWidgets/QButtonGroup>
|
||||||
|
#include <QtWidgets/QGridLayout>
|
||||||
|
#include <QtWidgets/QHeaderView>
|
||||||
|
#include <QtWidgets/QMainWindow>
|
||||||
|
#include <QtWidgets/QTextBrowser>
|
||||||
|
#include <QtWidgets/QWidget>
|
||||||
|
#include "prompt.h"
|
||||||
|
|
||||||
|
QT_BEGIN_NAMESPACE
|
||||||
|
|
||||||
|
class Ui_MainWindow
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
QWidget *centralWidget;
|
||||||
|
QGridLayout *gridLayout;
|
||||||
|
QTextBrowser *monitoringConsole;
|
||||||
|
Prompt *prompt;
|
||||||
|
|
||||||
|
void setupUi(QMainWindow *MainWindow)
|
||||||
|
{
|
||||||
|
if (MainWindow->objectName().isEmpty())
|
||||||
|
MainWindow->setObjectName(QStringLiteral("MainWindow"));
|
||||||
|
MainWindow->resize(374, 404);
|
||||||
|
centralWidget = new QWidget(MainWindow);
|
||||||
|
centralWidget->setObjectName(QStringLiteral("centralWidget"));
|
||||||
|
gridLayout = new QGridLayout(centralWidget);
|
||||||
|
gridLayout->setSpacing(6);
|
||||||
|
gridLayout->setContentsMargins(11, 11, 11, 11);
|
||||||
|
gridLayout->setObjectName(QStringLiteral("gridLayout"));
|
||||||
|
monitoringConsole = new QTextBrowser(centralWidget);
|
||||||
|
monitoringConsole->setObjectName(QStringLiteral("monitoringConsole"));
|
||||||
|
|
||||||
|
gridLayout->addWidget(monitoringConsole, 1, 0, 1, 2);
|
||||||
|
|
||||||
|
prompt = new Prompt(centralWidget);
|
||||||
|
prompt->setObjectName(QStringLiteral("prompt"));
|
||||||
|
|
||||||
|
gridLayout->addWidget(prompt, 0, 0, 1, 2);
|
||||||
|
|
||||||
|
MainWindow->setCentralWidget(centralWidget);
|
||||||
|
|
||||||
|
retranslateUi(MainWindow);
|
||||||
|
|
||||||
|
QMetaObject::connectSlotsByName(MainWindow);
|
||||||
|
} // setupUi
|
||||||
|
|
||||||
|
void retranslateUi(QMainWindow *MainWindow)
|
||||||
|
{
|
||||||
|
MainWindow->setWindowTitle(QApplication::translate("MainWindow", "MainWindow", 0));
|
||||||
|
} // retranslateUi
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace Ui {
|
||||||
|
class MainWindow: public Ui_MainWindow {};
|
||||||
|
} // namespace Ui
|
||||||
|
|
||||||
|
QT_END_NAMESPACE
|
||||||
|
|
||||||
|
#endif // UI_MAINWINDOW_H
|
Loading…
x
Reference in New Issue
Block a user