Code improvements

This commit is contained in:
Syping 2021-04-30 21:10:28 +02:00
parent 7e85d842f4
commit e3b399e9ba
8 changed files with 116 additions and 64 deletions

View file

@ -19,6 +19,6 @@ jobs:
- name: Install packages - name: Install packages
run: sudo .ci/ubuntu_install.sh run: sudo .ci/ubuntu_install.sh
- name: Configure CMake - name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=${{env.BUILD_TYPE}} -DBUILD_SHARED=OFF
- name: Build - name: Build
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}} run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}

View file

@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.7) cmake_minimum_required(VERSION 3.7)
project(xmppbot LANGUAGES CXX) project(xmppbot VERSION 0.3 LANGUAGES CXX)
set(CMAKE_INCLUDE_CURRENT_DIR ON) set(CMAKE_INCLUDE_CURRENT_DIR ON)
@ -12,17 +12,19 @@ set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED) find_package(QT NAMES Qt6 Qt5 COMPONENTS Core REQUIRED)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Network REQUIRED) find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Network REQUIRED)
include(cmake/LoadDefs.cmake)
include(cmake/LoadQXmpp.cmake)
add_subdirectory(src/3rdparty/qxmpp)
add_executable(xmppbot add_executable(xmppbot
src/xmppbot/main.cpp src/xmppbot/main.cpp
src/xmppbot/unixsocket.cpp
src/xmppbot/unixsocket.h
src/xmppbot/xmppbot.h src/xmppbot/xmppbot.h
src/xmppbot/xmppsocket.cpp
src/xmppbot/xmppsocket.h
) )
add_dependencies(xmppbot qxmpp) add_dependencies(xmppbot ${QXmppLibrary})
target_link_libraries(xmppbot Qt${QT_VERSION_MAJOR}::Network qxmpp) target_link_libraries(xmppbot Qt${QT_VERSION_MAJOR}::Network ${QXmppLibrary})
install(TARGETS xmppbot DESTINATION bin) install(TARGETS xmppbot DESTINATION bin)

7
cmake/LoadDefs.cmake Normal file
View file

@ -0,0 +1,7 @@
cmake_minimum_required(VERSION 3.7)
add_definitions(
-DQT_DISABLE_DEPRECATED_BEFORE=0x050F00
-DQURL_NO_CAST_FROM_STRING
-DQT_NO_CAST_TO_ASCII
-DQT_NO_FOREACH
)

24
cmake/LoadQXmpp.cmake Normal file
View file

@ -0,0 +1,24 @@
cmake_minimum_required(VERSION 3.7)
find_package(Qt${QT_VERSION_MAJOR} COMPONENTS Core Network Xml REQUIRED)
option(BUILD_SYSTEM_QXMPP "Build with system QXmpp Library" OFF)
if(BUILD_SYSTEM_QXMPP)
find_package(QXmpp)
if (QXmpp_FOUND)
get_target_property(QXmppLinkLibraries QXmpp::QXmpp INTERFACE_LINK_LIBRARIES)
if("${QXmppLinkLibraries}" MATCHES "Qt${QT_VERSION_MAJOR}")
if(QXmpp_VERSION VERSION_LESS "1.4.0")
set(CMAKE_CXX_STANDARD 11)
endif()
set(QXmppLibrary QXmpp::QXmpp)
else()
message(FATAL_ERROR "QXmpp Library incorrect Qt Version")
endif()
else()
message(FATAL_ERROR "QXmpp Library not found")
endif()
else()
project(qxmpp VERSION 1.4.0)
set(SO_VERSION 3)
set(QXmppLibrary qxmpp)
add_subdirectory(src/3rdparty/qxmpp/src)
endif()

View file

@ -23,21 +23,22 @@
#include <QTextStream> #include <QTextStream>
#include <QSettings> #include <QSettings>
#include <QProcess> #include <QProcess>
#include <QTimer>
#include <QFile> #include <QFile>
#include "xmppbot.h" #include "xmppbot.h"
#include "unixsocket.h" #include "xmppsocket.h"
#include "QXmppClient.h" #include "QXmppClient.h"
#include "QXmppMessage.h" #include "QXmppMessage.h"
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
QCoreApplication app(argc, argv); QCoreApplication app(argc, argv);
app.setApplicationName("xmppbot"); app.setApplicationName(QLatin1String("xmppbot"));
app.setApplicationVersion("0.2.1"); app.setApplicationVersion(QLatin1String("0.3"));
QCommandLineParser commandLineParser; QCommandLineParser commandLineParser;
commandLineParser.addPositionalArgument("config", QCoreApplication::translate("xmppbot", "Configuration file.")); commandLineParser.addPositionalArgument(QLatin1String("config"), QCoreApplication::translate("xmppbot", "Configuration file."));
commandLineParser.addHelpOption(); commandLineParser.addHelpOption();
commandLineParser.addVersionOption(); commandLineParser.addVersionOption();
commandLineParser.process(app); commandLineParser.process(app);
@ -50,7 +51,7 @@ int main(int argc, char *argv[])
settingsPath = a_settingsPath; settingsPath = a_settingsPath;
} }
else { else {
QTextStream(stderr) << "xmppbot: " << a_settingsPath << " not found!" << xendl; QTextStream(stderr) << QLatin1String("xmppbot: ") << a_settingsPath << QLatin1String(" not found!") << xendl;
return 1; return 1;
} }
} }
@ -66,10 +67,10 @@ int main(int argc, char *argv[])
for (const QString &group : settings.childGroups()) { for (const QString &group : settings.childGroups()) {
settings.beginGroup(group); settings.beginGroup(group);
for (const QString &key : settings.childKeys()) { for (const QString &key : settings.childKeys()) {
if (key == "Password") { if (key == QLatin1String("Password")) {
if (!loginSet) { if (!loginSet) {
jid = group; jid = group;
const QString instance = settings.value("Instance", QString()).toString(); const QString instance = settings.value(QLatin1String("Instance"), QString()).toString();
if (!instance.isEmpty()) { if (!instance.isEmpty()) {
jid += QLatin1String("/") + instance; jid += QLatin1String("/") + instance;
} }
@ -81,52 +82,54 @@ int main(int argc, char *argv[])
return 1; return 1;
} }
} }
if (key == "UnixSocket") { if (key == QLatin1String(XmppSocketType)) {
UnixSocket *unixSocket = new UnixSocket(&client, jid, group); XmppSocket *xmppSocket = new XmppSocket(&client, jid, group);
const QString permission = settings.value("SocketPermission", QString()).toString(); #ifdef Q_OS_UNIX
if (permission == "UG" || permission == "UserGroup") { const QString permission = settings.value(QLatin1String("SocketPermission"), QString()).toString();
unixSocket->setSocketOptions(QLocalServer::UserAccessOption | QLocalServer::GroupAccessOption); if (permission == QLatin1String("UG") || permission == QLatin1String("UserGroup")) {
xmppSocket->setSocketOptions(QLocalServer::UserAccessOption | QLocalServer::GroupAccessOption);
} }
if (permission == "UO" || permission == "UserOther") { if (permission == QLatin1String("UO") || permission == QLatin1String("UserOther")) {
unixSocket->setSocketOptions(QLocalServer::UserAccessOption | QLocalServer::OtherAccessOption); xmppSocket->setSocketOptions(QLocalServer::UserAccessOption | QLocalServer::OtherAccessOption);
} }
if (permission == "U" || permission == "User") { if (permission == QLatin1String("U") || permission == QLatin1String("User")) {
unixSocket->setSocketOptions(QLocalServer::UserAccessOption); xmppSocket->setSocketOptions(QLocalServer::UserAccessOption);
} }
if (permission == "GO" || permission == "GroupOther") { if (permission == QLatin1String("GO") || permission == QLatin1String("GroupOther")) {
unixSocket->setSocketOptions(QLocalServer::GroupAccessOption | QLocalServer::OtherAccessOption); xmppSocket->setSocketOptions(QLocalServer::GroupAccessOption | QLocalServer::OtherAccessOption);
} }
if (permission == "G" || permission == "Group") { if (permission == QLatin1String("G") || permission == QLatin1String("Group")) {
unixSocket->setSocketOptions(QLocalServer::GroupAccessOption); xmppSocket->setSocketOptions(QLocalServer::GroupAccessOption);
} }
if (permission == "O" || permission == "Other") { if (permission == QLatin1String("O") || permission == QLatin1String("Other")) {
unixSocket->setSocketOptions(QLocalServer::OtherAccessOption); xmppSocket->setSocketOptions(QLocalServer::OtherAccessOption);
} }
if (permission == "A" || permission == "All" || permission == "UGO" || permission == "UserGroupOther") { if (permission == QLatin1String("A") || permission == QLatin1String("All") || permission == QLatin1String("UGO") || permission == QLatin1String("UserGroupOther")) {
unixSocket->setSocketOptions(QLocalServer::WorldAccessOption); xmppSocket->setSocketOptions(QLocalServer::WorldAccessOption);
} }
#endif
const QString socketPath = settings.value(key, QString()).toString(); const QString socketPath = settings.value(key, QString()).toString();
bool listen = unixSocket->listen(socketPath); bool listen = xmppSocket->listen(socketPath);
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
if (!listen) { if (!listen) {
QLocalServer::removeServer(socketPath); QLocalServer::removeServer(socketPath);
listen = unixSocket->listen(socketPath); listen = xmppSocket->listen(socketPath);
} }
#endif #endif
if (listen) { if (listen) {
QTextStream(stderr) << "xmppbot: Account socket " << group << " initialised" << xendl; QTextStream(stderr) << QLatin1String("xmppbot: Account socket ") << group << QLatin1String(" initialised") << xendl;
const QString incoming = settings.value("Incoming", QString()).toString(); const QString incoming = settings.value("Incoming", QString()).toString();
if (incoming.startsWith("message:")) { if (incoming.startsWith(QLatin1String("message:"))) {
QTextStream(stderr) << "xmppbot: Account message incoming " << group << " initialised" << xendl; QTextStream(stderr) << QLatin1String("xmppbot: Account message incoming ") << group << QLatin1String(" initialised") << xendl;
h_msg.insert(group, incoming.mid(8)); h_msg.insert(group, incoming.mid(8));
} }
if (incoming.startsWith("run:")) { if (incoming.startsWith(QLatin1String("run:"))) {
QTextStream(stderr) << "xmppbot: Account run incoming " << group << " initialised" << xendl; QTextStream(stderr) << QLatin1String("xmppbot: Account run incoming ") << group << QLatin1String(" initialised") << xendl;
h_run.insert(group, incoming.mid(4)); h_run.insert(group, incoming.mid(4));
} }
} }
else { else {
delete unixSocket; delete xmppSocket;
} }
} }
} }
@ -134,25 +137,37 @@ int main(int argc, char *argv[])
} }
} }
else { else {
QTextStream(stderr) << "xmppbot: Can't initialise without settings.ini!" << xendl; QTextStream(stderr) << QLatin1String("xmppbot: Can't initialise without settings.ini!") << xendl;
return 1; return 1;
} }
if (jid.isEmpty() || jpw.isEmpty()) { if (jid.isEmpty() || jpw.isEmpty()) {
QTextStream(stderr) << "xmppbot: Can't initialise without XMPP account!" << xendl; QTextStream(stderr) << QLatin1String("xmppbot: Can't initialise without XMPP account!") << xendl;
return 1; return 1;
} }
QObject::connect(&client, &QXmppClient::connected, [&]() { QTextStream(stderr) << QLatin1String("xmppbot: Account login ") << jid << QLatin1String(" initialised") << xendl;
QTextStream(stderr) << "xmppbot: Account " << jid << " connected" << xendl;
QObject::connect(&client, &QXmppClient::stateChanged, [&](QXmppClient::State state) {
switch (state) {
case QXmppClient::ConnectedState: {
QTextStream(stderr) << QLatin1String("xmppbot: Account ") << jid << QLatin1String(" connected") << xendl;
QXmppPresence xmppPresence(QXmppPresence::Available); QXmppPresence xmppPresence(QXmppPresence::Available);
client.setClientPresence(xmppPresence); client.setClientPresence(xmppPresence);
}); break;
}
QObject::connect(&client, &QXmppClient::disconnected, [&]() { case QXmppClient::ConnectingState:
QTextStream(stderr) << "xmppbot: Account " << jid << " disconnected" << xendl; break;
case QXmppClient::DisconnectedState:
QTextStream(stderr) << QLatin1String("xmppbot: Account ") << jid << QLatin1String(" disconnected") << xendl;
QTimer::singleShot(5000, &client, [&]() {
client.connectToServer(jid, jpw); client.connectToServer(jid, jpw);
}); });
break;
default:
break;
}
});
QObject::connect(&client, &QXmppClient::messageReceived, [&](const QXmppMessage &xmppMessage) { QObject::connect(&client, &QXmppClient::messageReceived, [&](const QXmppMessage &xmppMessage) {
const QString body = xmppMessage.body(); const QString body = xmppMessage.body();
@ -175,14 +190,12 @@ int main(int argc, char *argv[])
qint64 pid; qint64 pid;
bool isStarted = QProcess::startDetached(run, QStringList() << from << xmppMessage.to() << xmppMessage.body(), QString(), &pid); bool isStarted = QProcess::startDetached(run, QStringList() << from << xmppMessage.to() << xmppMessage.body(), QString(), &pid);
if (isStarted) { if (isStarted) {
QTextStream(stderr) << "xmppbot: Account " << from_jid << " executed pid " << pid << xendl; QTextStream(stderr) << QLatin1String("xmppbot: Account ") << from_jid << QLatin1String(" executed pid ") << pid << xendl;
} }
} }
}); });
client.connectToServer(jid, jpw); client.connectToServer(jid, jpw);
QTextStream(stderr) << "xmppbot: Account login " << jid << " initialised" << xendl;
return app.exec(); return app.exec();
} }

View file

@ -27,4 +27,10 @@
#define xendl endl; #define xendl endl;
#endif #endif
#ifdef Q_OS_WIN
#define XmppSocketType "NamedPipe"
#else
#define XmppSocketType "UnixSocket"
#endif
#endif // XMPPBOT_H #endif // XMPPBOT_H

View file

@ -18,14 +18,14 @@
#include <QLocalSocket> #include <QLocalSocket>
#include "unixsocket.h" #include "xmppsocket.h"
#include "QXmppMessage.h" #include "QXmppMessage.h"
UnixSocket::UnixSocket(QXmppClient *client, const QString &from, const QString &to) : client(client), from(from), to(to) XmppSocket::XmppSocket(QXmppClient *client, const QString &from, const QString &to) : client(client), from(from), to(to)
{ {
} }
void UnixSocket::readyRead() void XmppSocket::readyRead()
{ {
QLocalSocket *localSocket = qobject_cast<QLocalSocket*>(sender()); QLocalSocket *localSocket = qobject_cast<QLocalSocket*>(sender());
if (localSocket) { if (localSocket) {
@ -37,10 +37,10 @@ void UnixSocket::readyRead()
} }
} }
void UnixSocket::incomingConnection(quintptr socketDescriptor) void XmppSocket::incomingConnection(quintptr socketDescriptor)
{ {
QLocalSocket *localSocket = new QLocalSocket(this); QLocalSocket *localSocket = new QLocalSocket(this);
localSocket->setSocketDescriptor(socketDescriptor); localSocket->setSocketDescriptor(socketDescriptor);
QObject::connect(localSocket, &QLocalSocket::readyRead, this, &UnixSocket::readyRead); QObject::connect(localSocket, &QLocalSocket::readyRead, this, &XmppSocket::readyRead);
QObject::connect(localSocket, &QLocalSocket::disconnected, localSocket, &QLocalSocket::deleteLater); QObject::connect(localSocket, &QLocalSocket::disconnected, localSocket, &QLocalSocket::deleteLater);
} }

View file

@ -16,19 +16,19 @@
* responsible for anything with use of the software, you are self responsible. * responsible for anything with use of the software, you are self responsible.
*****************************************************************************/ *****************************************************************************/
#ifndef UNIXSOCKET_H #ifndef XMPPSOCKET_H
#define UNIXSOCKET_H #define XMPPSOCKET_H
#include <QLocalServer> #include <QLocalServer>
#include <QObject> #include <QObject>
#include "QXmppClient.h" #include "QXmppClient.h"
class UnixSocket : public QLocalServer class XmppSocket : public QLocalServer
{ {
Q_OBJECT Q_OBJECT
public: public:
UnixSocket(QXmppClient *client, const QString &from, const QString &to); XmppSocket(QXmppClient *client, const QString &from, const QString &to);
private slots: private slots:
void readyRead(); void readyRead();
@ -42,4 +42,4 @@ protected:
void incomingConnection(quintptr socketDescriptor); void incomingConnection(quintptr socketDescriptor);
}; };
#endif // UNIXSOCKET_H #endif // XMPPSOCKET_H