jay/jay.cpp

62 lines
1.6 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"
Jay::Jay(const std::string& jid, const std::string& password) :
client(jid, password),
2025-02-21 23:01:11 +02:00
loggers(),
owners()
2024-03-30 22:44:52 -03:00
{
client.registerMessageHandler(this);
2025-02-21 23:01:11 +02:00
client.registerConnectionListener(this);
2024-03-30 22:44:52 -03:00
client.setTls(gloox::TLSPolicy::TLSOptional);
client.setSASLMechanisms(gloox::SaslMechScramSha1);
}
2025-02-21 23:01:11 +02:00
Jay::~Jay() {}
2024-03-30 22:44:52 -03:00
void Jay::run() {
if (client.connect(false)) {
// Run the event loop
gloox::ConnectionError ce = gloox::ConnNoError;
while (ce == gloox::ConnNoError)
ce = client.recv();
std::cout << "Connection terminated with error: " << ce << std::endl;
}
2025-02-21 23:01:11 +02:00
std::cout << "Out of loop" << std::endl;
2024-03-30 22:44:52 -03:00
}
void Jay::handleMessage(const gloox::Message& message, gloox::MessageSession* session) {
2025-02-21 23:01:11 +02:00
if (owners.count(message.from().bare())) {
std::cout << "Received message from owner: " << message.body() << std::endl;
} else {
std::cout << "Received message: " << message.body() << std::endl;
}
}
void Jay::onConnect() {
for (const std::string& owner : owners)
client.send(gloox::Message(gloox::Message::Chat, owner, "I'm online!"));
}
void Jay::onDisconnect(gloox::ConnectionError e) {
std::cout << "Disconnected: " << e << std::endl;
}
bool Jay::onTLSConnect(const gloox::CertInfo&) {
return true;
2024-03-30 22:44:52 -03:00
}
2025-02-21 23:01:11 +02:00
2024-03-30 22:44:52 -03:00
Logger* Jay::addLogger(gloox::LogLevel level) {
loggers.emplace_back(std::make_unique<Logger>(client.logInstance(), level));
return loggers.back().get();
}
2025-02-21 23:01:11 +02:00
void Jay::addOwner(const std::string& jid) {
owners.insert(jid);
}