Code improvements
This commit is contained in:
parent
7e85d842f4
commit
e3b399e9ba
8 changed files with 116 additions and 64 deletions
2
.github/workflows/ubuntu.yml
vendored
2
.github/workflows/ubuntu.yml
vendored
|
@ -19,6 +19,6 @@ jobs:
|
|||
- name: Install packages
|
||||
run: sudo .ci/ubuntu_install.sh
|
||||
- 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
|
||||
run: cmake --build ${{github.workspace}}/build --config ${{env.BUILD_TYPE}}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
cmake_minimum_required(VERSION 3.7)
|
||||
|
||||
project(xmppbot LANGUAGES CXX)
|
||||
project(xmppbot VERSION 0.3 LANGUAGES CXX)
|
||||
|
||||
set(CMAKE_INCLUDE_CURRENT_DIR ON)
|
||||
|
||||
|
@ -12,17 +12,19 @@ set(CMAKE_CXX_STANDARD 17)
|
|||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
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
|
||||
src/xmppbot/main.cpp
|
||||
src/xmppbot/unixsocket.cpp
|
||||
src/xmppbot/unixsocket.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)
|
||||
|
|
7
cmake/LoadDefs.cmake
Normal file
7
cmake/LoadDefs.cmake
Normal 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
24
cmake/LoadQXmpp.cmake
Normal 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()
|
|
@ -23,21 +23,22 @@
|
|||
#include <QTextStream>
|
||||
#include <QSettings>
|
||||
#include <QProcess>
|
||||
#include <QTimer>
|
||||
#include <QFile>
|
||||
|
||||
#include "xmppbot.h"
|
||||
#include "unixsocket.h"
|
||||
#include "xmppsocket.h"
|
||||
#include "QXmppClient.h"
|
||||
#include "QXmppMessage.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
QCoreApplication app(argc, argv);
|
||||
app.setApplicationName("xmppbot");
|
||||
app.setApplicationVersion("0.2.1");
|
||||
app.setApplicationName(QLatin1String("xmppbot"));
|
||||
app.setApplicationVersion(QLatin1String("0.3"));
|
||||
|
||||
QCommandLineParser commandLineParser;
|
||||
commandLineParser.addPositionalArgument("config", QCoreApplication::translate("xmppbot", "Configuration file."));
|
||||
commandLineParser.addPositionalArgument(QLatin1String("config"), QCoreApplication::translate("xmppbot", "Configuration file."));
|
||||
commandLineParser.addHelpOption();
|
||||
commandLineParser.addVersionOption();
|
||||
commandLineParser.process(app);
|
||||
|
@ -50,7 +51,7 @@ int main(int argc, char *argv[])
|
|||
settingsPath = a_settingsPath;
|
||||
}
|
||||
else {
|
||||
QTextStream(stderr) << "xmppbot: " << a_settingsPath << " not found!" << xendl;
|
||||
QTextStream(stderr) << QLatin1String("xmppbot: ") << a_settingsPath << QLatin1String(" not found!") << xendl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
@ -66,10 +67,10 @@ int main(int argc, char *argv[])
|
|||
for (const QString &group : settings.childGroups()) {
|
||||
settings.beginGroup(group);
|
||||
for (const QString &key : settings.childKeys()) {
|
||||
if (key == "Password") {
|
||||
if (key == QLatin1String("Password")) {
|
||||
if (!loginSet) {
|
||||
jid = group;
|
||||
const QString instance = settings.value("Instance", QString()).toString();
|
||||
const QString instance = settings.value(QLatin1String("Instance"), QString()).toString();
|
||||
if (!instance.isEmpty()) {
|
||||
jid += QLatin1String("/") + instance;
|
||||
}
|
||||
|
@ -81,52 +82,54 @@ int main(int argc, char *argv[])
|
|||
return 1;
|
||||
}
|
||||
}
|
||||
if (key == "UnixSocket") {
|
||||
UnixSocket *unixSocket = new UnixSocket(&client, jid, group);
|
||||
const QString permission = settings.value("SocketPermission", QString()).toString();
|
||||
if (permission == "UG" || permission == "UserGroup") {
|
||||
unixSocket->setSocketOptions(QLocalServer::UserAccessOption | QLocalServer::GroupAccessOption);
|
||||
if (key == QLatin1String(XmppSocketType)) {
|
||||
XmppSocket *xmppSocket = new XmppSocket(&client, jid, group);
|
||||
#ifdef Q_OS_UNIX
|
||||
const QString permission = settings.value(QLatin1String("SocketPermission"), QString()).toString();
|
||||
if (permission == QLatin1String("UG") || permission == QLatin1String("UserGroup")) {
|
||||
xmppSocket->setSocketOptions(QLocalServer::UserAccessOption | QLocalServer::GroupAccessOption);
|
||||
}
|
||||
if (permission == "UO" || permission == "UserOther") {
|
||||
unixSocket->setSocketOptions(QLocalServer::UserAccessOption | QLocalServer::OtherAccessOption);
|
||||
if (permission == QLatin1String("UO") || permission == QLatin1String("UserOther")) {
|
||||
xmppSocket->setSocketOptions(QLocalServer::UserAccessOption | QLocalServer::OtherAccessOption);
|
||||
}
|
||||
if (permission == "U" || permission == "User") {
|
||||
unixSocket->setSocketOptions(QLocalServer::UserAccessOption);
|
||||
if (permission == QLatin1String("U") || permission == QLatin1String("User")) {
|
||||
xmppSocket->setSocketOptions(QLocalServer::UserAccessOption);
|
||||
}
|
||||
if (permission == "GO" || permission == "GroupOther") {
|
||||
unixSocket->setSocketOptions(QLocalServer::GroupAccessOption | QLocalServer::OtherAccessOption);
|
||||
if (permission == QLatin1String("GO") || permission == QLatin1String("GroupOther")) {
|
||||
xmppSocket->setSocketOptions(QLocalServer::GroupAccessOption | QLocalServer::OtherAccessOption);
|
||||
}
|
||||
if (permission == "G" || permission == "Group") {
|
||||
unixSocket->setSocketOptions(QLocalServer::GroupAccessOption);
|
||||
if (permission == QLatin1String("G") || permission == QLatin1String("Group")) {
|
||||
xmppSocket->setSocketOptions(QLocalServer::GroupAccessOption);
|
||||
}
|
||||
if (permission == "O" || permission == "Other") {
|
||||
unixSocket->setSocketOptions(QLocalServer::OtherAccessOption);
|
||||
if (permission == QLatin1String("O") || permission == QLatin1String("Other")) {
|
||||
xmppSocket->setSocketOptions(QLocalServer::OtherAccessOption);
|
||||
}
|
||||
if (permission == "A" || permission == "All" || permission == "UGO" || permission == "UserGroupOther") {
|
||||
unixSocket->setSocketOptions(QLocalServer::WorldAccessOption);
|
||||
if (permission == QLatin1String("A") || permission == QLatin1String("All") || permission == QLatin1String("UGO") || permission == QLatin1String("UserGroupOther")) {
|
||||
xmppSocket->setSocketOptions(QLocalServer::WorldAccessOption);
|
||||
}
|
||||
#endif
|
||||
const QString socketPath = settings.value(key, QString()).toString();
|
||||
bool listen = unixSocket->listen(socketPath);
|
||||
bool listen = xmppSocket->listen(socketPath);
|
||||
#ifdef Q_OS_UNIX
|
||||
if (!listen) {
|
||||
QLocalServer::removeServer(socketPath);
|
||||
listen = unixSocket->listen(socketPath);
|
||||
listen = xmppSocket->listen(socketPath);
|
||||
}
|
||||
#endif
|
||||
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();
|
||||
if (incoming.startsWith("message:")) {
|
||||
QTextStream(stderr) << "xmppbot: Account message incoming " << group << " initialised" << xendl;
|
||||
if (incoming.startsWith(QLatin1String("message:"))) {
|
||||
QTextStream(stderr) << QLatin1String("xmppbot: Account message incoming ") << group << QLatin1String(" initialised") << xendl;
|
||||
h_msg.insert(group, incoming.mid(8));
|
||||
}
|
||||
if (incoming.startsWith("run:")) {
|
||||
QTextStream(stderr) << "xmppbot: Account run incoming " << group << " initialised" << xendl;
|
||||
if (incoming.startsWith(QLatin1String("run:"))) {
|
||||
QTextStream(stderr) << QLatin1String("xmppbot: Account run incoming ") << group << QLatin1String(" initialised") << xendl;
|
||||
h_run.insert(group, incoming.mid(4));
|
||||
}
|
||||
}
|
||||
else {
|
||||
delete unixSocket;
|
||||
delete xmppSocket;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -134,25 +137,37 @@ int main(int argc, char *argv[])
|
|||
}
|
||||
}
|
||||
else {
|
||||
QTextStream(stderr) << "xmppbot: Can't initialise without settings.ini!" << xendl;
|
||||
QTextStream(stderr) << QLatin1String("xmppbot: Can't initialise without settings.ini!") << xendl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
QObject::connect(&client, &QXmppClient::connected, [&]() {
|
||||
QTextStream(stderr) << "xmppbot: Account " << jid << " connected" << xendl;
|
||||
QTextStream(stderr) << QLatin1String("xmppbot: Account login ") << jid << QLatin1String(" initialised") << 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);
|
||||
client.setClientPresence(xmppPresence);
|
||||
});
|
||||
|
||||
QObject::connect(&client, &QXmppClient::disconnected, [&]() {
|
||||
QTextStream(stderr) << "xmppbot: Account " << jid << " disconnected" << xendl;
|
||||
break;
|
||||
}
|
||||
case QXmppClient::ConnectingState:
|
||||
break;
|
||||
case QXmppClient::DisconnectedState:
|
||||
QTextStream(stderr) << QLatin1String("xmppbot: Account ") << jid << QLatin1String(" disconnected") << xendl;
|
||||
QTimer::singleShot(5000, &client, [&]() {
|
||||
client.connectToServer(jid, jpw);
|
||||
});
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
QObject::connect(&client, &QXmppClient::messageReceived, [&](const QXmppMessage &xmppMessage) {
|
||||
const QString body = xmppMessage.body();
|
||||
|
@ -175,14 +190,12 @@ int main(int argc, char *argv[])
|
|||
qint64 pid;
|
||||
bool isStarted = QProcess::startDetached(run, QStringList() << from << xmppMessage.to() << xmppMessage.body(), QString(), &pid);
|
||||
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);
|
||||
|
||||
QTextStream(stderr) << "xmppbot: Account login " << jid << " initialised" << xendl;
|
||||
|
||||
return app.exec();
|
||||
}
|
||||
|
|
|
@ -27,4 +27,10 @@
|
|||
#define xendl endl;
|
||||
#endif
|
||||
|
||||
#ifdef Q_OS_WIN
|
||||
#define XmppSocketType "NamedPipe"
|
||||
#else
|
||||
#define XmppSocketType "UnixSocket"
|
||||
#endif
|
||||
|
||||
#endif // XMPPBOT_H
|
||||
|
|
|
@ -18,14 +18,14 @@
|
|||
|
||||
#include <QLocalSocket>
|
||||
|
||||
#include "unixsocket.h"
|
||||
#include "xmppsocket.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());
|
||||
if (localSocket) {
|
||||
|
@ -37,10 +37,10 @@ void UnixSocket::readyRead()
|
|||
}
|
||||
}
|
||||
|
||||
void UnixSocket::incomingConnection(quintptr socketDescriptor)
|
||||
void XmppSocket::incomingConnection(quintptr socketDescriptor)
|
||||
{
|
||||
QLocalSocket *localSocket = new QLocalSocket(this);
|
||||
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);
|
||||
}
|
|
@ -16,19 +16,19 @@
|
|||
* responsible for anything with use of the software, you are self responsible.
|
||||
*****************************************************************************/
|
||||
|
||||
#ifndef UNIXSOCKET_H
|
||||
#define UNIXSOCKET_H
|
||||
#ifndef XMPPSOCKET_H
|
||||
#define XMPPSOCKET_H
|
||||
|
||||
#include <QLocalServer>
|
||||
#include <QObject>
|
||||
|
||||
#include "QXmppClient.h"
|
||||
|
||||
class UnixSocket : public QLocalServer
|
||||
class XmppSocket : public QLocalServer
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
UnixSocket(QXmppClient *client, const QString &from, const QString &to);
|
||||
XmppSocket(QXmppClient *client, const QString &from, const QString &to);
|
||||
|
||||
private slots:
|
||||
void readyRead();
|
||||
|
@ -42,4 +42,4 @@ protected:
|
|||
void incomingConnection(quintptr socketDescriptor);
|
||||
};
|
||||
|
||||
#endif // UNIXSOCKET_H
|
||||
#endif // XMPPSOCKET_H
|
Loading…
Reference in a new issue