72 lines
1.8 KiB
C++
72 lines
1.8 KiB
C++
// 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; }
|
|
|
|
|