// SPDX-FileCopyrightText: 2024 Yury Gubich // SPDX-License-Identifier: GPL-3.0-or-later #include "jay.h" Jay::Jay(const std::string& configPath): runMutex(), config(std::make_shared(configPath)), router(std::make_unique()), 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( config, client, std::bind(&Router::routeMessage, router.get(), std::placeholders::_1, std::placeholders::_2) ); if (!connectionHandler) connectionHandler = std::make_unique(config, client); createActors(); } void Jay::createClient() { if (client) return; client = std::make_shared(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& pair : config->getOwners()) router->registerActor(pair.first, pair.second); } void Jay::addLogger(gloox::LogLevel level) { loggers.emplace_back(std::make_unique(client->logInstance(), level)); }