From 836c1b17f04701364c3649959602087884d2788d Mon Sep 17 00:00:00 2001 From: "Rachel Fae Fox (foxiepaws)" Date: Fri, 21 Feb 2020 03:08:41 -0500 Subject: [PATCH] initial --- WeechatRelay.pro | 5 ++ qweechatrelay/netstring.cpp | 6 +++ qweechatrelay/qweechatrelay.pro | 46 ++++++++++++++++++ qweechatrelay/qweechatrelay_global.h | 12 +++++ qweechatrelay/weechatmessage.cpp | 29 ++++++++++++ qweechatrelay/weechatmessage.h | 21 +++++++++ qweechatrelay/weechatrelay.cpp | 67 +++++++++++++++++++++++++++ qweechatrelay/weechatrelay.h | 44 ++++++++++++++++++ qweechatrelay/weeobject.cpp | 37 +++++++++++++++ qweechatrelay/weeobject.h | 36 ++++++++++++++ weechatrelaytest/main.cpp | 11 +++++ weechatrelaytest/weechatrelaytest.pro | 34 ++++++++++++++ 12 files changed, 348 insertions(+) create mode 100644 WeechatRelay.pro create mode 100644 qweechatrelay/netstring.cpp create mode 100644 qweechatrelay/qweechatrelay.pro create mode 100644 qweechatrelay/qweechatrelay_global.h create mode 100644 qweechatrelay/weechatmessage.cpp create mode 100644 qweechatrelay/weechatmessage.h create mode 100644 qweechatrelay/weechatrelay.cpp create mode 100644 qweechatrelay/weechatrelay.h create mode 100644 qweechatrelay/weeobject.cpp create mode 100644 qweechatrelay/weeobject.h create mode 100644 weechatrelaytest/main.cpp create mode 100644 weechatrelaytest/weechatrelaytest.pro diff --git a/WeechatRelay.pro b/WeechatRelay.pro new file mode 100644 index 0000000..1b02413 --- /dev/null +++ b/WeechatRelay.pro @@ -0,0 +1,5 @@ +TEMPLATE = subdirs + +SUBDIRS += \ + qweechatrelay \ + weechatrelaytest diff --git a/qweechatrelay/netstring.cpp b/qweechatrelay/netstring.cpp new file mode 100644 index 0000000..2368231 --- /dev/null +++ b/qweechatrelay/netstring.cpp @@ -0,0 +1,6 @@ +#include "netstring.h" + +NetString::NetString() +{ + +} diff --git a/qweechatrelay/qweechatrelay.pro b/qweechatrelay/qweechatrelay.pro new file mode 100644 index 0000000..9504bd2 --- /dev/null +++ b/qweechatrelay/qweechatrelay.pro @@ -0,0 +1,46 @@ +#------------------------------------------------- +# +# Project created by QtCreator 2020-02-20T18:25:58 +# +#------------------------------------------------- + +QT += network testlib + +QT -= gui + +TARGET = qweechatrelay +TEMPLATE = lib + +DEFINES += QWEECHATRELAY_LIBRARY + +# The following define makes your compiler emit warnings if you use +# any feature of Qt which has been marked as deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if you use deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +#DEFINES += WITH_BOOST +#LIBS += -L/usr/local/Cellar/boost/1.72.0/lib/ -lboost_iostreams +#QMAKE_CXXFLAGS += -g -I/usr/local/Cellar/boost/1.72.0/include/ + +SOURCES += \ + netstring.cpp \ + weechatmessage.cpp \ + weechatrelay.cpp \ + weeobject.cpp + +HEADERS += \ + qweechatrelay_global.h \ + weechatmessage.h \ + weechatrelay.h \ + weeobject.h + +unix { + target.path = /usr/lib + INSTALLS += target +} diff --git a/qweechatrelay/qweechatrelay_global.h b/qweechatrelay/qweechatrelay_global.h new file mode 100644 index 0000000..1ff701e --- /dev/null +++ b/qweechatrelay/qweechatrelay_global.h @@ -0,0 +1,12 @@ +#ifndef QWEECHATRELAY_GLOBAL_H +#define QWEECHATRELAY_GLOBAL_H + +#include + +#if defined(QWEECHATRELAY_LIBRARY) +# define QWEECHATRELAYSHARED_EXPORT Q_DECL_EXPORT +#else +# define QWEECHATRELAYSHARED_EXPORT Q_DECL_IMPORT +#endif + +#endif // QWEECHATRELAY_GLOBAL_H diff --git a/qweechatrelay/weechatmessage.cpp b/qweechatrelay/weechatmessage.cpp new file mode 100644 index 0000000..0f875b3 --- /dev/null +++ b/qweechatrelay/weechatmessage.cpp @@ -0,0 +1,29 @@ +#include "weechatmessage.h" + +qint32 get_string_length(QByteArray ba) { + const char * lenbytes = ba.constData(); + quint32 len = (quint32) lenbytes[0] << 24; + len |= (quint32) lenbytes[1] << 16; + len |= (quint32) lenbytes[2] << 8; + len |= (quint8) lenbytes[3]; + return len; +} +QString netstring_helper(QByteArray ba) { + auto len = get_string_length(ba.mid(0,4)); + return ba.mid(3,len); +} + +WeechatMessage::WeechatMessage() +{ + + +} + +WeechatMessage::WeechatMessage(quint32 len, bool compression, QByteArray arr, QByteArray con) { + length = len; + this->compression = compression; + this->raw = arr; + this->message = con; + std::list objects; + +} diff --git a/qweechatrelay/weechatmessage.h b/qweechatrelay/weechatmessage.h new file mode 100644 index 0000000..f6fed92 --- /dev/null +++ b/qweechatrelay/weechatmessage.h @@ -0,0 +1,21 @@ +#ifndef WEECHATMESSAGE_H +#define WEECHATMESSAGE_H + +#include +#include +#include + +class WeechatMessage +{ + quint32 length; + bool compression; + QByteArray raw; + QByteArray message; + QString netstring_helper(QByteArray ba); + +public: + WeechatMessage(); + WeechatMessage(quint32 len, bool compression, QByteArray arr, QByteArray con); +}; + +#endif // WEECHATMESSAGE_H diff --git a/qweechatrelay/weechatrelay.cpp b/qweechatrelay/weechatrelay.cpp new file mode 100644 index 0000000..7b33bbf --- /dev/null +++ b/qweechatrelay/weechatrelay.cpp @@ -0,0 +1,67 @@ +#include "weechatrelay.h" + +void WeechatRelay::on_connected() { + qDebug() << "connecting!"; + QStringList args; + args << QStringLiteral("password=%1").arg(password.replace(",","\\,")) << QStringLiteral("compression=%1").arg((compression)?"on":"off"); + sock.write(QStringLiteral("init %1\n").arg(args.join(",")).toUtf8()); + sock.write("sync\n"); + qDebug() << "??? synced!"; +} + +void WeechatRelay::on_readyread() { + qDebug() << "gotData!"; + QByteArray ba; + ba.append(sock.read(5)); + // pull out our 32bit length from start of message + const char * lenbytes = ba.mid(0,4).constData(); + quint32 len = (quint32) lenbytes[0] << 24; + len |= (quint32) lenbytes[1] << 16; + len |= (quint32) lenbytes[2] << 8; + len |= (quint8) lenbytes[3]; + const char * iscompressed = ba.mid(4,1).constData(); + bool compression = (bool) iscompressed[0]; + qDebug() << QStringLiteral("got %1 bytes %2").arg((quint32)len).arg((compression) ? "compressed" : "uncompressed"); + ba.append(sock.read(len-HEADER_LENGTH)); + QByteArray contents; + + //if (compression) { + // std::basic_stringstream i; + // std::basic_stringstream o; + // boost::iostreams::filtering_streambuf zlib; + // zlib.push(boost::iostreams::zlib_decompressor()); + // i << (const uchar*) ba.mid((quint32)HEADER_LENGTH,(quint32)len-(quint32)HEADER_LENGTH).data(); + // zlib.push(i); + // boost::iostreams::copy(zlib,o); + // std::string s = o.str(); + // contents = QByteArray::fromStdString(s); + //} else { + contents = ba.mid(5,len-HEADER_LENGTH); + //} + mbuff.push_back(new WeechatMessage(len, compression, ba, contents)); + emit messageReady(); +} + + +WeechatRelay::WeechatRelay() {} + +void WeechatRelay::connectToHost(QString password, bool compression, bool ssl, QString hostname, quint16 port) { + this->ssl=ssl; + this->hostname = hostname; + this->port = port; + this->password = password; + // compression won't be supported yet. + this->compression = false; + //if (ssl) { + // this->sock = new QSslSocket(this); + //} else { + //} + QObject::connect(&sock, &QAbstractSocket::readyRead, this, &WeechatRelay::on_readyread); + QObject::connect(&sock, &QAbstractSocket::connected, this, &WeechatRelay::on_connected); + QObject::connect(&sock, &QAbstractSocket::disconnected, this, []{qDebug() << "socket disconnected";}); + QObject::connect(&sock, static_cast(&QAbstractSocket::error), this, [](QAbstractSocket::SocketError e) {qDebug() << "error:" << e;}); + sock.connectToHost(hostname,port,QIODevice::ReadWrite,QAbstractSocket::IPv4Protocol); +} + + + diff --git a/qweechatrelay/weechatrelay.h b/qweechatrelay/weechatrelay.h new file mode 100644 index 0000000..8f447eb --- /dev/null +++ b/qweechatrelay/weechatrelay.h @@ -0,0 +1,44 @@ +#ifndef WEECHATRELAY_H +#define WEECHATRELAY_H + +#include "qweechatrelay_global.h" +#include +#include +#include +#include +#include +#include +#include +//#include +//#include +//#include +//#include +//#include + +#define HEADER_LENGTH 5 +class QWEECHATRELAYSHARED_EXPORT WeechatRelay : public QObject +{ + Q_OBJECT + + QTcpSocket sock; + QString hostname; + QString password; + bool compression; + quint16 port; + bool ssl; + std::list mbuff; + +public slots: + void on_connected(); + void on_readyread(); + +signals: + void messageReady(); + +public: + void connectToHost(QString password, bool compression, bool ssl, QString hostname, quint16 port); + WeechatRelay(); +}; + + +#endif // WEECHATRELAY_H diff --git a/qweechatrelay/weeobject.cpp b/qweechatrelay/weeobject.cpp new file mode 100644 index 0000000..0345e0a --- /dev/null +++ b/qweechatrelay/weeobject.cpp @@ -0,0 +1,37 @@ +#include "weeobject.h" + +WeeObject::WeeObject(QString type, QByteArray d) +{ + if (type=="chr") {} + else if (type == "int") {} + else if (type == "lon") {} + else if (type == "str") {} + else if (type == "ptr") {} + else if (type == "buf") {} + else if (type == "tim") {} + else if (type == "htb") {} + else if (type == "hda") {} + else if (type == "inf") {} + else if (type == "inl") {} + else if (type == "arr") {} + else {} + length = d.length(); + raw = d; +} + +void* WeeObject::get() { + switch(type) { + case Types::Chr : return raw.data(); + case Types::Int : return raw.data(); + case Types::Lon : return raw.data(); + case Types::Str : return raw.data(); + case Types::Ptr : return raw.data(); + case Types::Buf : return raw.data(); + case Types::Tim : return raw.data(); + case Types::Htb : return raw.data(); + case Types::Hda : return raw.data(); + case Types::Inf : return raw.data(); + case Types::Inl : return raw.data(); + case Types::Arr : return raw.data(); + } +} diff --git a/qweechatrelay/weeobject.h b/qweechatrelay/weeobject.h new file mode 100644 index 0000000..88e70de --- /dev/null +++ b/qweechatrelay/weeobject.h @@ -0,0 +1,36 @@ +#ifndef WEEOBJECT_H +#define WEEOBJECT_H + +#include +#include +#include +#include +#include + +class WeeObject { + enum class Types : int { Chr, Int, Lon, Str, Buf, Ptr, Tim, Htb, Hda, Inf, Inl, Arr }; +protected: + Types type; + quint32 length; + QByteArray raw; + +public: + void *get(); + WeeObject(QString,QByteArray); +}; + +class WeeHash { + +}; +class WeeHdataPV { + std::list pointers; + std::unordered_map objects; + +}; + +class WeeHdata { + QString hpath; + +}; + +#endif // WEEOBJECT_H diff --git a/weechatrelaytest/main.cpp b/weechatrelaytest/main.cpp new file mode 100644 index 0000000..0bb4da7 --- /dev/null +++ b/weechatrelaytest/main.cpp @@ -0,0 +1,11 @@ +#include +#include "weechatrelay.h" +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + WeechatRelay r; + r.connectToHost("", true, false,"127.0.0.1",9000); + QObject::connect(&r,&WeechatRelay::messageReady,&a,[](){qDebug() << "message ready!";}); + return a.exec(); +} diff --git a/weechatrelaytest/weechatrelaytest.pro b/weechatrelaytest/weechatrelaytest.pro new file mode 100644 index 0000000..e244742 --- /dev/null +++ b/weechatrelaytest/weechatrelaytest.pro @@ -0,0 +1,34 @@ +QT -= gui +QT += network +CONFIG += c++11 console +CONFIG -= app_bundle + +# The following define makes your compiler emit warnings if you use +# any Qt feature that has been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +DEFINES += WITH_BOOST +LIBS += -L/usr/local/Cellar/boost/1.72.0/lib/ -lboost_iostreams +QMAKE_CXXFLAGS += -g -I/usr/local/Cellar/boost/1.72.0/include/ + +# You can also make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target + +win32:CONFIG(release, debug|release): LIBS += -L$$OUT_PWD/../qweechatrelay/release/ -lqweechatrelay +else:win32:CONFIG(debug, debug|release): LIBS += -L$$OUT_PWD/../qweechatrelay/debug/ -lqweechatrelay +else:unix: LIBS += -L$$OUT_PWD/../qweechatrelay/ -lqweechatrelay + +INCLUDEPATH += $$PWD/../qweechatrelay +DEPENDPATH += $$PWD/../qweechatrelay