xmppbot/src/xmppbot/xmppsocket.cpp

47 lines
1.8 KiB
C++
Raw Normal View History

2021-04-29 16:11:51 +02:00
/*****************************************************************************
2021-05-01 22:26:41 +02:00
* xmppbot Simple Unix Socket based XMPP bot
2021-04-29 16:11:51 +02:00
* Copyright (C) 2021 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* This software is provided as-is, no warranties are given to you, we are not
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#include <QLocalSocket>
2021-04-30 21:10:28 +02:00
#include "xmppsocket.h"
2021-04-29 16:11:51 +02:00
#include "QXmppMessage.h"
2021-04-30 21:10:28 +02:00
XmppSocket::XmppSocket(QXmppClient *client, const QString &from, const QString &to) : client(client), from(from), to(to)
2021-04-29 16:11:51 +02:00
{
}
2021-04-30 21:10:28 +02:00
void XmppSocket::readyRead()
2021-04-29 16:11:51 +02:00
{
QLocalSocket *localSocket = qobject_cast<QLocalSocket*>(sender());
if (localSocket) {
const QString message = QString::fromUtf8(localSocket->readAll());
if (!message.isEmpty()) {
QXmppMessage xmppMessage(from, to, message);
client->sendPacket(xmppMessage);
}
}
}
2021-04-30 21:10:28 +02:00
void XmppSocket::incomingConnection(quintptr socketDescriptor)
2021-04-29 16:11:51 +02:00
{
QLocalSocket *localSocket = new QLocalSocket(this);
localSocket->setSocketDescriptor(socketDescriptor);
2021-04-30 21:10:28 +02:00
QObject::connect(localSocket, &QLocalSocket::readyRead, this, &XmppSocket::readyRead);
2021-04-29 16:11:51 +02:00
QObject::connect(localSocket, &QLocalSocket::disconnected, localSocket, &QLocalSocket::deleteLater);
}