jay/jay.cpp

72 lines
1.7 KiB
C++
Raw Normal View History

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)),
2025-03-03 21:36:02 +02:00
router(std::make_unique<Router>()),
2025-02-22 21:03:21 +02:00
client(),
messageHandler(),
connectionHandler(),
2025-03-03 21:36:02 +02:00
loggers()
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)
2025-03-03 21:36:02 +02:00
messageHandler = std::make_unique<Message>(
config,
client,
std::bind(&Router::routeMessage, router.get(), std::placeholders::_1, std::placeholders::_2)
);
2025-02-22 21:03:21 +02:00
if (!connectionHandler)
connectionHandler = std::make_unique<Connection>(config, client);
2025-03-03 21:36:02 +02:00
createActors();
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() {
2025-03-03 21:36:02 +02:00
for (const std::pair<const std::string, std::string>& pair : config->getOwners())
router->registerActor(pair.first, pair.second);
2025-03-02 21:33:22 +02:00
}
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
}