Refactored in a way modules have access to bot infrastructure

This commit is contained in:
Blue 2025-03-09 21:01:41 +02:00
parent c605b3ba93
commit 89c04254b8
Signed by: blue
GPG key ID: 9B203B252A63EE38
25 changed files with 245 additions and 216 deletions

View file

@ -0,0 +1,9 @@
set(SOURCES
connection.cpp
)
set(HEADERS
connection.h
)
target_sources(${EXEC_NAME} PRIVATE ${SOURCES})

71
connection/connection.cpp Normal file
View file

@ -0,0 +1,71 @@
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "connection.h"
Connection::Connection(const std::shared_ptr<Core>& core):
state(initial),
core(core),
gloox()
{}
Connection::~Connection() noexcept {
deinitialize();
}
void Connection::initiialize() {
if (state != initial)
return;
gloox = std::make_unique<gloox::Client>(core->config.getFullJID(), core->config.getPassword());
gloox->registerConnectionListener(this);
gloox->registerMessageHandler(this);
gloox->logInstance().registerLogHandler(core->config.getLogLevel(), gloox::LogAreaAll, &core->logger);
gloox::Disco* disco = gloox->disco();
disco->setVersion("Jay", "0.0.1");
disco->setIdentity("client", "bot");
gloox->setTls(core->config.getTLSPolicy());
gloox->setSASLMechanisms(gloox::SaslMechAll);
gloox->setStreamManagement(true, true);
state = disconnected;
}
void Connection::deinitialize() {
if (state == initial)
return;
gloox->logInstance().removeLogHandler(&core->logger);
gloox->removeMessageHandler(this);
gloox->removeConnectionListener(this);
gloox = nullptr;
state = initial;
}
void Connection::connect() {
if (state != disconnected)
return;
state = connected;
gloox->connect(true);
state = disconnected;
}
void Connection::send(const std::string& jid, const std::string& body) {
gloox->send(gloox::Message(gloox::Message::Chat, jid, body));
}
void Connection::handleMessage(const gloox::Message& message, gloox::MessageSession* session) {
core->router.routeMessage(message.from().bare(), message.body());
}
void Connection::onConnect() {}
void Connection::onDisconnect(gloox::ConnectionError e) {}
bool Connection::onTLSConnect(const gloox::CertInfo&) { return true; }

47
connection/connection.h Normal file
View file

@ -0,0 +1,47 @@
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <memory>
#include <gloox/client.h>
#include <gloox/message.h>
#include <gloox/disco.h>
#include <gloox/connectionlistener.h>
#include <gloox/messagehandler.h>
#include "component/core.h"
class Connection:
public gloox::ConnectionListener,
public gloox::MessageHandler
{
public:
enum State {
initial,
disconnected,
connected
};
public:
Connection(const std::shared_ptr<Core>& core);
~Connection() noexcept;
void initiialize();
void deinitialize();
void connect();
void send(const std::string& jid, const std::string& body);
public:
void onConnect() override;
void onDisconnect(gloox::ConnectionError e) override;
bool onTLSConnect(const gloox::CertInfo&) override;
void handleMessage(const gloox::Message& message, gloox::MessageSession* session = 0) override;
private:
State state;
std::shared_ptr<Core> core;
std::unique_ptr<gloox::Client> gloox;
};