Extensible IRC Bot Question - Pointer to QTcpSocket



  • Hello, this is my first post here.
    Anyway, I have written an IRC Bot, which i want to be extensible (I have created the Plugin loading code, but its not implemented btw)
    But my only problem now, is that I cant create a pointer to a QTcpSocket, because I want to use the QTcpSocket inside the API of my IRC Bot. I just need to know how to create a pointer to a QTcpSocket object that can be accessed by other classes.

    And I know its badly coded ( I'm just coding it for fun ;) )

    MainWindow.h:
    @#ifndef MAINWINDOW_H
    #define MAINWINDOW_H

    #include <QMainWindow>
    #include <QtNetwork/QTcpSocket>
    #include <QSqlDatabase>
    #include <QSqlQuery>
    #include <QInputDialog>
    #include "api/permissions.h"
    #include "api/commandparser.h"
    #include <iostream>
    #include <QMessageBox>

    using namespace std;

    namespace Ui {
    class MainWindow;
    }

    class MainWindow : public QMainWindow
    {
    Q_OBJECT

    public:
    explicit MainWindow(QWidget *parent = 0,const QStringList arguments = QStringList("THIS TEXT IS DERP YEA"));
    ~MainWindow();
    static void SendToSocket(QString msg);
    private:
    Ui::MainWindow *ui;
    QTcpSocket * socket;
    QSqlDatabase sql;
    QString sqlServer,sqlPort,sqlUser,sqlPass,sqlType,server,port,nick,pass;
    private slots:
    void readData();
    void connectToServer();
    void disconnectFromServer();
    void sqlStart();
    };

    #endif // MAINWINDOW_H
    @
    MainWindow.cpp
    @#include "mainwindow.h"
    #include "ui_mainwindow.h"

    MainWindow::MainWindow(QWidget *parent, const QStringList arguments) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
    {
    ui->setupUi(this);
    // Setup required variables
    server = arguments.at(0);
    port = arguments.at(1);
    nick = arguments.at(2);
    pass = arguments.at(3);
    sqlServer = arguments.at(4);
    sqlPort = arguments.at(5);
    sqlUser = arguments.at(6);
    sqlPass = arguments.at(7);
    sqlType = arguments.at(8);
    // Create socket and database instance
    socket = new QTcpSocket(this);
    if(sqlType=="MySQL") sql = QSqlDatabase::addDatabase("QMYSQL");
    else if(sqlType=="PostgreSQL") sql = QSqlDatabase::addDatabase("QPSQL");
    // Connect signals and slots!
    connect(socket, SIGNAL(readyRead()), this, SLOT(readData()));
    connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(connectToServer()));
    connect(ui->connectButton, SIGNAL(clicked()), this, SLOT(sqlStart()));
    connect(ui->disconnectButton, SIGNAL(clicked()), this, SLOT(disconnectFromServer()));
    connect(ui->sendButton, SIGNAL(clicked()), this, SLOT(sendRawCommand()));
    //UI Stuff
    ui->lineEdit->setEnabled(false);
    ui->disconnectButton->setEnabled(false);
    }
    void MainWindow::sqlStart()
    {
    //QString password = QInputDialog::getText(this, tr("SQL Database"),tr("Password: "), QLineEdit::Normal,"");
    sql.setPort(sqlPort.toInt());
    sql.setHostName(sqlServer);
    sql.setDatabaseName("biobot");
    sql.setUserName(sqlUser);
    sql.setPassword(sqlPass);
    if (!sql.open()) ui->textEdit->append("Couldn't connect to SQL database");
    else ui->textEdit->append("Connected to SQL database!");
    permissions::registerPermission("hello", 1);
    }

    void MainWindow::connectToServer() {
    ui->textEdit->append("Connecting...");
    ui->lineEdit->setEnabled(true);
    ui->disconnectButton->setEnabled(true);
    ui->connectButton->setEnabled(false);
    socket->connectToHost(QString(server), port.toInt());
    QString nickCmd = "NICK " + nick + "\r\n";
    socket->write((nickCmd.toStdString().c_str()));
    socket->write("USER Biobot 8 * : Biobot C++ \r\n");
    }

    void MainWindow::readData() {
    QString readLine = socket->readLine();
    //Biohazard!Biohazard@ie.freebnc.net PRIVMSG #Biobot hi
    if(readLine.startsWith("PING"))
    {
    ui->textEdit->append(("PONG " + readLine.split(" ")[1]));
    socket->write(("PONG " + readLine.split(" ")[1]).toStdString().c_str());
    }
    else if(readLine.startsWith(":NickServ!NickServ@")&&
    readLine.contains("You have 30 seconds to identify to your nickname before it is changed."))
    {
    QString passCmd = "NICKSERV identify " + pass + "\r\n";
    socket->write(passCmd.toStdString().c_str());
    }
    else if(readLine.split(" ")[1]=="PRIVMSG")
    {
    QString command = CommandParser::getMessage(readLine);
    if(command.split(" ")[0]=="@hello"&&permissions::getPermission("hello", CommandParser::getName(readLine)))
    {
    const QString text = "PRIVMSG " + CommandParser::getChannel(readLine) + ":hello world!\r\n";
    socket->write(text.toStdString().c_str());
    }
    }
    ui->textEdit->append(readLine);
    if(socket->canReadLine()) readData();
    }

    void MainWindow::disconnectFromServer() {
    socket->write("QUIT GTFO\r\n");
    socket->flush();
    socket->disconnect();
    sql.close();
    ui->lineEdit->setEnabled(false);
    ui->disconnectButton->setEnabled(false);
    ui->sendButton->setEnabled(false);
    QMessageBox::information(this, "Notice", "To reconnect, please restart the bot.");
    QMessageBox::aboutQt(this, "About QT");
    ui->textEdit->append("Disconnected.");
    }

    MainWindow::~MainWindow()
    {
    delete ui;
    }
    @



  • If you want to make that pointer accessible by other classes of your project you need to either create a function in MainWindow that returns that pointer or to pass the pointer to the classes (by constructor using a setter function) that shall use it.

    This seems to be a reoccurring problem for beginners I guess. Take a look a this "thread":http://qt-project.org/forums/viewthread/16852/#84756 as the original poster struggles with the same problem only for a GLWindow instead of a QTcpSocket.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.