jay/jay.cpp
2025-03-03 21:36:02 +02:00

72 lines
1.7 KiB
C++

// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
// SPDX-License-Identifier: GPL-3.0-or-later
#include "jay.h"
Jay::Jay(const std::string& configPath):
runMutex(),
config(std::make_shared<Config>(configPath)),
router(std::make_unique<Router>()),
client(),
messageHandler(),
connectionHandler(),
loggers()
{}
Jay::~Jay() {}
bool Jay::isConfigValid() const {
return config->isValid();
}
void Jay::run() {
std::lock_guard lock(runMutex);
initialize();
client->connect(true);
}
void Jay::initialize() {
createClient();
if (!messageHandler)
messageHandler = std::make_unique<Message>(
config,
client,
std::bind(&Router::routeMessage, router.get(), std::placeholders::_1, std::placeholders::_2)
);
if (!connectionHandler)
connectionHandler = std::make_unique<Connection>(config, client);
createActors();
}
void Jay::createClient() {
if (client)
return;
client = std::make_shared<gloox::Client>(config->getFullJID(), config->getPassword());
addLogger(config->getLogLevel());
gloox::Disco* disco = client->disco();
disco->setVersion("Jay", "0.0.1");
disco->setIdentity("client", "bot");
client->setTls(config->getTLSPolicy());
client->setSASLMechanisms(gloox::SaslMechAll);
client->setStreamManagement(true, true);
}
void Jay::createActors() {
for (const std::pair<const std::string, std::string>& pair : config->getOwners())
router->registerActor(pair.first, pair.second);
}
void Jay::addLogger(gloox::LogLevel level) {
loggers.emplace_back(std::make_unique<Logger>(client->logInstance(), level));
}