master
Rachel Fae Fox (foxiepaws) 2020-02-21 03:08:41 -05:00
commit 836c1b17f0
12 changed files with 348 additions and 0 deletions

5
WeechatRelay.pro Normal file
View File

@ -0,0 +1,5 @@
TEMPLATE = subdirs
SUBDIRS += \
qweechatrelay \
weechatrelaytest

View File

@ -0,0 +1,6 @@
#include "netstring.h"
NetString::NetString()
{
}

View File

@ -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
}

View File

@ -0,0 +1,12 @@
#ifndef QWEECHATRELAY_GLOBAL_H
#define QWEECHATRELAY_GLOBAL_H
#include <QtCore/qglobal.h>
#if defined(QWEECHATRELAY_LIBRARY)
# define QWEECHATRELAYSHARED_EXPORT Q_DECL_EXPORT
#else
# define QWEECHATRELAYSHARED_EXPORT Q_DECL_IMPORT
#endif
#endif // QWEECHATRELAY_GLOBAL_H

View File

@ -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<WeeObject> objects;
}

View File

@ -0,0 +1,21 @@
#ifndef WEECHATMESSAGE_H
#define WEECHATMESSAGE_H
#include <qweechatrelay_global.h>
#include <QByteArray>
#include <QString>
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

View File

@ -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<char> i;
// std::basic_stringstream<char> o;
// boost::iostreams::filtering_streambuf<boost::iostreams::input> 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<void (QAbstractSocket::*)(QAbstractSocket::SocketError)>(&QAbstractSocket::error), this, [](QAbstractSocket::SocketError e) {qDebug() << "error:" << e;});
sock.connectToHost(hostname,port,QIODevice::ReadWrite,QAbstractSocket::IPv4Protocol);
}

View File

@ -0,0 +1,44 @@
#ifndef WEECHATRELAY_H
#define WEECHATRELAY_H
#include "qweechatrelay_global.h"
#include <QTcpSocket>
#include <zlib.h>
#include <QSslSocket>
#include <QString>
#include <QObject>
#include <list>
#include <weechatmessage.h>
//#include <iostream>
//#include <sstream>
//#include <boost/iostreams/filtering_streambuf.hpp>
//#include <boost/iostreams/copy.hpp>
//#include <boost/iostreams/filter/zlib.hpp>
#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<WeechatMessage*> 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

View File

@ -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();
}
}

36
qweechatrelay/weeobject.h Normal file
View File

@ -0,0 +1,36 @@
#ifndef WEEOBJECT_H
#define WEEOBJECT_H
#include <QByteArray>
#include <QString>
#include <unordered_map>
#include <list>
#include <qweechatrelay_global.h>
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<QString> pointers;
std::unordered_map<QString,WeeObject> objects;
};
class WeeHdata {
QString hpath;
};
#endif // WEEOBJECT_H

11
weechatrelaytest/main.cpp Normal file
View File

@ -0,0 +1,11 @@
#include <QCoreApplication>
#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();
}

View File

@ -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