WeechatRelay/qweechatrelay/weechatrelay.cpp

68 lines
2.7 KiB
C++

#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);
}