2024-03-30 22:44:52 -03:00
|
|
|
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "jay.h"
|
|
|
|
|
2025-02-22 21:03:21 +02:00
|
|
|
Jay::Jay(const std::string& configPath):
|
|
|
|
runMutex(),
|
|
|
|
config(std::make_shared<Config>(configPath)),
|
|
|
|
client(),
|
|
|
|
messageHandler(),
|
|
|
|
connectionHandler(),
|
2025-03-02 21:33:22 +02:00
|
|
|
loggers(),
|
|
|
|
actors()
|
2025-02-22 21:03:21 +02:00
|
|
|
{}
|
2024-03-30 22:44:52 -03:00
|
|
|
|
2025-02-21 23:01:11 +02:00
|
|
|
Jay::~Jay() {}
|
2024-03-30 22:44:52 -03:00
|
|
|
|
2025-02-22 21:03:21 +02:00
|
|
|
bool Jay::isConfigValid() const {
|
|
|
|
return config->isValid();
|
2024-03-30 22:44:52 -03:00
|
|
|
}
|
|
|
|
|
2025-02-21 23:01:11 +02:00
|
|
|
|
2025-02-22 21:03:21 +02:00
|
|
|
void Jay::run() {
|
|
|
|
std::lock_guard lock(runMutex);
|
2025-02-21 23:01:11 +02:00
|
|
|
|
2025-02-22 21:03:21 +02:00
|
|
|
initialize();
|
|
|
|
client->connect(true);
|
2025-02-21 23:01:11 +02:00
|
|
|
}
|
|
|
|
|
2025-02-22 21:03:21 +02:00
|
|
|
void Jay::initialize() {
|
|
|
|
createClient();
|
|
|
|
|
|
|
|
if (!messageHandler)
|
|
|
|
messageHandler = std::make_unique<Message>(config, client);
|
|
|
|
|
|
|
|
if (!connectionHandler)
|
|
|
|
connectionHandler = std::make_unique<Connection>(config, client);
|
2024-03-30 22:44:52 -03:00
|
|
|
}
|
|
|
|
|
2025-02-22 21:03:21 +02:00
|
|
|
void Jay::createClient() {
|
|
|
|
if (client)
|
|
|
|
return;
|
|
|
|
|
|
|
|
client = std::make_shared<gloox::Client>(config->getFullJID(), config->getPassword());
|
|
|
|
addLogger(config->getLogLevel());
|
|
|
|
|
|
|
|
gloox::Disco* disco = client->disco();
|
2025-02-21 23:01:11 +02:00
|
|
|
|
2025-02-22 21:03:21 +02:00
|
|
|
disco->setVersion("Jay", "0.0.1");
|
|
|
|
disco->setIdentity("client", "bot");
|
|
|
|
|
|
|
|
client->setTls(config->getTLSPolicy());
|
|
|
|
client->setSASLMechanisms(gloox::SaslMechAll);
|
|
|
|
client->setStreamManagement(true, true);
|
2024-03-30 22:44:52 -03:00
|
|
|
}
|
2025-02-21 23:01:11 +02:00
|
|
|
|
2025-03-02 21:33:22 +02:00
|
|
|
void Jay::createActors() {
|
|
|
|
for (const std::string& jid : config->getOwners()) {
|
|
|
|
Actors::const_iterator act = actors.find(jid);
|
|
|
|
if (act == actors.end())
|
|
|
|
actors.emplace(jid, std::make_unique<Actor>());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-02-22 21:03:21 +02:00
|
|
|
|
|
|
|
void Jay::addLogger(gloox::LogLevel level) {
|
|
|
|
loggers.emplace_back(std::make_unique<Logger>(client->logInstance(), level));
|
2025-02-21 23:01:11 +02:00
|
|
|
}
|