changed structure of the application to be more easily exportable
This commit is contained in:
parent
a61b2ba603
commit
a0aca93799
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,4 +3,5 @@ debug/*
|
||||
release/*
|
||||
*.user
|
||||
*.Debug
|
||||
*.Release
|
||||
*.Release
|
||||
Makefile
|
||||
|
@ -10,19 +10,20 @@ TARGET = SparrowBot
|
||||
TEMPLATE = app
|
||||
CONFIG += c++11
|
||||
|
||||
SOURCES += main.cpp\
|
||||
mainwindow.cpp \
|
||||
sparrowbot.cpp \
|
||||
SOURCES += mainwindow.cpp \
|
||||
socketirc.cpp \
|
||||
message.cpp \
|
||||
user.cpp \
|
||||
prompt.cpp
|
||||
prompt.cpp \
|
||||
ircbot.cpp \
|
||||
botapp.cpp
|
||||
|
||||
HEADERS += mainwindow.h \
|
||||
sparrowbot.h \
|
||||
HEADERS += mainwindow.h \
|
||||
socketirc.h \
|
||||
message.h \
|
||||
user.h \
|
||||
prompt.h
|
||||
prompt.h \
|
||||
ircbot.h \
|
||||
botapp.h
|
||||
|
||||
FORMS += mainwindow.ui
|
||||
|
72
botapp.cpp
Normal file
72
botapp.cpp
Normal file
@ -0,0 +1,72 @@
|
||||
#include "botapp.h"
|
||||
|
||||
BotApp::BotApp(int argc, char** argv) :
|
||||
nogui(false),
|
||||
server("irc.freenode.net"),
|
||||
port(6667),
|
||||
nick("SparrowBotDebug"),
|
||||
chan("epicsparrow")
|
||||
{
|
||||
// parsing arguments
|
||||
for(int i=1; i<argc; ++i)
|
||||
{
|
||||
QString str = QString(argv[i]);
|
||||
if(str.compare("-nogui") == 0)
|
||||
nogui = true;
|
||||
else if(str.contains('='))
|
||||
{
|
||||
QStringList argList = str.split('=');
|
||||
if(argList.size() == 2)
|
||||
{
|
||||
QString key = argList.at(0);
|
||||
if(key.compare("server") == 0)
|
||||
server = argList.at(1);
|
||||
else if(key.compare("port") == 0)
|
||||
port = argList.at(1).toInt();
|
||||
else if(key.compare("nick") == 0)
|
||||
nick = argList.at(1);
|
||||
else if(key.compare("chan") == 0)
|
||||
chan = argList.at(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sock = new SocketIRC(server, port);
|
||||
bot = new IRCBot(nick, chan);
|
||||
if(nogui)
|
||||
coreApp = new QCoreApplication(argc, argv);
|
||||
else
|
||||
coreApp = new QApplication(argc, argv);
|
||||
|
||||
QObject::connect(sock, SIGNAL(receivedMsg(QString)), bot, SLOT(receiveMsg(QString)));
|
||||
QObject::connect(bot, SIGNAL(sendMsg(QString)), sock, SLOT(sendMsg(QString)));
|
||||
QObject::connect(coreApp, SIGNAL(aboutToQuit()), bot, SLOT(disconnect()));
|
||||
}
|
||||
|
||||
BotApp::~BotApp()
|
||||
{
|
||||
delete(sock);
|
||||
delete(bot);
|
||||
if(!nogui)
|
||||
delete(window);
|
||||
}
|
||||
|
||||
int BotApp::exec()
|
||||
{
|
||||
if(!nogui)
|
||||
{
|
||||
window = new MainWindow();
|
||||
QObject::connect(sock, SIGNAL(receivedMsg(QString)), window->getConsole(), SLOT(append(QString)));
|
||||
QObject::connect(bot, SIGNAL(sendMsg(QString)), window->getConsole(), SLOT(append(QString)));
|
||||
QObject::connect(window->getPrompt(), SIGNAL(sendMsg(QString)), sock, SLOT(sendMsg(QString)));
|
||||
QObject::connect(window->getPrompt(), SIGNAL(returnPressed()), window->getPrompt(), SLOT(confirmationPerformed()));
|
||||
window->show();
|
||||
}
|
||||
return sock->connectToServer(coreApp);
|
||||
}
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
BotApp app = BotApp(argc, argv);
|
||||
return app.exec();
|
||||
}
|
28
botapp.h
Normal file
28
botapp.h
Normal file
@ -0,0 +1,28 @@
|
||||
#ifndef BOTAPP_H
|
||||
#define BOTAPP_H
|
||||
|
||||
#include "mainwindow.h"
|
||||
#include "socketirc.h"
|
||||
#include "ircbot.h"
|
||||
#include <QApplication>
|
||||
|
||||
class BotApp
|
||||
{
|
||||
bool nogui = false;
|
||||
QString server = "irc.freenode.net";
|
||||
int port = 6667;
|
||||
QString nick = "SparrowBotDebug";
|
||||
QString chan = "epicsparrow";
|
||||
|
||||
SocketIRC* sock;
|
||||
IRCBot* bot;
|
||||
QCoreApplication* coreApp;
|
||||
MainWindow* window;
|
||||
|
||||
public:
|
||||
BotApp(int argc, char** argv);
|
||||
~BotApp();
|
||||
int exec();
|
||||
};
|
||||
|
||||
#endif // BOTAPP_H
|
@ -1,11 +1,11 @@
|
||||
#include "sparrowbot.h"
|
||||
#include "ircbot.h"
|
||||
#include <iostream>
|
||||
#include <stdio.h>
|
||||
#include <QStringList>
|
||||
|
||||
using namespace std;
|
||||
|
||||
void SparrowBot::receiveMsg(QString str)
|
||||
void IRCBot::receiveMsg(QString str)
|
||||
{
|
||||
Message msg = Message(str, &users);
|
||||
|
||||
@ -31,7 +31,7 @@ void SparrowBot::receiveMsg(QString str)
|
||||
}
|
||||
}
|
||||
|
||||
void SparrowBot::handleMessage(Message msg)
|
||||
void IRCBot::handleMessage(Message msg)
|
||||
{
|
||||
// si la ligne est un message
|
||||
if(msg.command.compare(QString("PRIVMSG"), Qt::CaseInsensitive) == 0)
|
||||
@ -105,42 +105,42 @@ void SparrowBot::handleMessage(Message msg)
|
||||
}
|
||||
}
|
||||
|
||||
QString SparrowBot::pong(QString target)
|
||||
QString IRCBot::pong(QString target)
|
||||
{
|
||||
return QString("PONG :%1").arg(target);
|
||||
}
|
||||
|
||||
QString SparrowBot::join(QString theChan)
|
||||
QString IRCBot::join(QString theChan)
|
||||
{
|
||||
return QString("JOIN #%1").arg(theChan);
|
||||
}
|
||||
|
||||
QString SparrowBot::whois(QString nick)
|
||||
QString IRCBot::whois(QString nick)
|
||||
{
|
||||
return QString("WHOIS %1").arg(nick);
|
||||
}
|
||||
|
||||
QString SparrowBot::say(QString str)
|
||||
QString IRCBot::say(QString str)
|
||||
{
|
||||
return QString("PRIVMSG #%1 :%2").arg(chan).arg(str);
|
||||
}
|
||||
|
||||
QString SparrowBot::privateSay(QString str, QString target)
|
||||
QString IRCBot::privateSay(QString str, QString target)
|
||||
{
|
||||
return QString("PRIVMSG %1 :%2").arg(target).arg(str);
|
||||
}
|
||||
|
||||
QString SparrowBot::action(QString str)
|
||||
QString IRCBot::action(QString str)
|
||||
{
|
||||
return QString("PRIVMSG #%1 :\001ACTION %2\001").arg(chan).arg(str);
|
||||
}
|
||||
|
||||
QString SparrowBot::quit(QString str)
|
||||
QString IRCBot::quit(QString str)
|
||||
{
|
||||
return QString("QUIT :%1").arg(str);
|
||||
}
|
||||
|
||||
void SparrowBot::disconnect()
|
||||
void IRCBot::disconnect()
|
||||
{
|
||||
emit sendMsg(quit("I'll be back"));
|
||||
}
|
@ -1,11 +1,11 @@
|
||||
#ifndef SPARROWBOT_H
|
||||
#define SPARROWBOT_H
|
||||
#ifndef IRCBOT_H
|
||||
#define IRCBOT_H
|
||||
|
||||
#include "user.h"
|
||||
#include "message.h"
|
||||
#include <QObject>
|
||||
|
||||
class SparrowBot : public QObject
|
||||
class IRCBot : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
@ -17,11 +17,12 @@ class SparrowBot : public QObject
|
||||
UserList users;
|
||||
|
||||
public:
|
||||
SparrowBot(QString nick_, QString chan_) : nick(nick_), chan(chan_), status(OFFLINE) {}
|
||||
IRCBot(QString nick_, QString chan_) : nick(nick_), chan(chan_), status(OFFLINE) {}
|
||||
|
||||
private:
|
||||
void handleMessage(Message msg);
|
||||
|
||||
protected:
|
||||
// IRC commands
|
||||
QString pong(QString target);
|
||||
QString join(QString theChan);
|
||||
@ -39,4 +40,4 @@ signals:
|
||||
void sendMsg(QString msg);
|
||||
};
|
||||
|
||||
#endif // SPARROWBOT_H
|
||||
#endif // IRCBOT_H
|
75
main.cpp
75
main.cpp
@ -1,75 +0,0 @@
|
||||
#include "mainwindow.h"
|
||||
#include "socketirc.h"
|
||||
#include "sparrowbot.h"
|
||||
#include <QApplication>
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
bool nogui = false;
|
||||
QString server = "irc.freenode.net";
|
||||
int port = 6667;
|
||||
QString nick = "SparrowBotDebug";
|
||||
QString chan = "epicsparrow";
|
||||
|
||||
// parsing arguments
|
||||
for(int i=1; i<argc; ++i)
|
||||
{
|
||||
QString str = QString(argv[i]);
|
||||
if(str.compare("-nogui") == 0)
|
||||
nogui = true;
|
||||
else if(str.contains('='))
|
||||
{
|
||||
QStringList argList = str.split('=');
|
||||
if(argList.size() == 2)
|
||||
{
|
||||
QString key = argList.at(0);
|
||||
if(key.compare("server") == 0)
|
||||
server = argList.at(1);
|
||||
else if(key.compare("port") == 0)
|
||||
port = argList.at(1).toInt();
|
||||
else if(key.compare("nick") == 0)
|
||||
nick = argList.at(1);
|
||||
else if(key.compare("chan") == 0)
|
||||
chan = argList.at(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// create the socket
|
||||
SocketIRC sock(server, port);
|
||||
|
||||
// create the bot
|
||||
SparrowBot* bot = new SparrowBot(nick, chan);
|
||||
|
||||
// connect the bot to the socket
|
||||
QObject::connect(&sock, SIGNAL(receivedMsg(QString)), bot, SLOT(receiveMsg(QString)));
|
||||
QObject::connect(bot, SIGNAL(sendMsg(QString)), &sock, SLOT(sendMsg(QString)));
|
||||
|
||||
// set up UI
|
||||
if(!nogui)
|
||||
{
|
||||
QApplication app(argc, argv);
|
||||
|
||||
// init window
|
||||
MainWindow w;
|
||||
QObject::connect(&sock, SIGNAL(receivedMsg(QString)), w.getConsole(), SLOT(append(QString)));
|
||||
QObject::connect(bot, SIGNAL(sendMsg(QString)), w.getConsole(), SLOT(append(QString)));
|
||||
QObject::connect(w.getPrompt(), SIGNAL(sendMsg(QString)), &sock, SLOT(sendMsg(QString)));
|
||||
QObject::connect(w.getPrompt(), SIGNAL(returnPressed()), w.getPrompt(), SLOT(confirmationPerformed()));
|
||||
w.show();
|
||||
|
||||
QObject::connect(&app, SIGNAL(aboutToQuit()), bot, SLOT(disconnect()));
|
||||
|
||||
sock.connectToServer(&app);
|
||||
}
|
||||
else
|
||||
{
|
||||
QCoreApplication coreApp(argc, argv);
|
||||
|
||||
QObject::connect(&coreApp, SIGNAL(aboutToQuit()), bot, SLOT(disconnect()));
|
||||
|
||||
sock.connectToServer(&coreApp);
|
||||
}
|
||||
delete(bot);
|
||||
return 0;
|
||||
}
|
@ -20,7 +20,7 @@ void SocketIRC::sendMsg(QString msg)
|
||||
sock.flush();
|
||||
}
|
||||
|
||||
void SocketIRC::connectToServer(QCoreApplication* app_)
|
||||
int SocketIRC::connectToServer(QCoreApplication* app_)
|
||||
{
|
||||
app = app_;
|
||||
|
||||
@ -34,9 +34,9 @@ void SocketIRC::connectToServer(QCoreApplication* app_)
|
||||
if(app != NULL)
|
||||
app->exit();
|
||||
else
|
||||
exit(0);
|
||||
return 0;
|
||||
}
|
||||
app->exec();
|
||||
return app->exec();
|
||||
}
|
||||
|
||||
void SocketIRC::readMsg()
|
||||
|
@ -16,7 +16,7 @@ public:
|
||||
SocketIRC(QString server_, int port_);
|
||||
void setServer(QString server_);
|
||||
void setPort(int port_);
|
||||
void connectToServer(QCoreApplication* app_);
|
||||
int connectToServer(QCoreApplication* app_);
|
||||
|
||||
private:
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user