62 lines
1.6 KiB
C++
62 lines
1.6 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& jid, const std::string& password) :
|
|
client(jid, password),
|
|
loggers(),
|
|
owners()
|
|
{
|
|
client.registerMessageHandler(this);
|
|
client.registerConnectionListener(this);
|
|
|
|
client.setTls(gloox::TLSPolicy::TLSOptional);
|
|
client.setSASLMechanisms(gloox::SaslMechScramSha1);
|
|
}
|
|
|
|
Jay::~Jay() {}
|
|
|
|
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;
|
|
}
|
|
std::cout << "Out of loop" << std::endl;
|
|
}
|
|
|
|
void Jay::handleMessage(const gloox::Message& message, gloox::MessageSession* session) {
|
|
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;
|
|
}
|
|
|
|
|
|
Logger* Jay::addLogger(gloox::LogLevel level) {
|
|
loggers.emplace_back(std::make_unique<Logger>(client.logInstance(), level));
|
|
return loggers.back().get();
|
|
}
|
|
|
|
void Jay::addOwner(const std::string& jid) {
|
|
owners.insert(jid);
|
|
}
|