Trying different stuff

This commit is contained in:
Blue 2025-02-21 23:01:11 +02:00
parent b2692a43f3
commit 698e706daa
Signed by: blue
GPG Key ID: 9B203B252A63EE38
3 changed files with 59 additions and 14 deletions

33
jay.cpp
View File

@ -5,16 +5,17 @@
Jay::Jay(const std::string& jid, const std::string& password) : Jay::Jay(const std::string& jid, const std::string& password) :
client(jid, password), client(jid, password),
loggers() loggers(),
owners()
{ {
client.registerMessageHandler(this); client.registerMessageHandler(this);
client.registerConnectionListener(this);
client.setTls(gloox::TLSPolicy::TLSOptional); client.setTls(gloox::TLSPolicy::TLSOptional);
client.setSASLMechanisms(gloox::SaslMechScramSha1); client.setSASLMechanisms(gloox::SaslMechScramSha1);
} }
Jay::~Jay() { Jay::~Jay() {}
loggers.clear();
}
void Jay::run() { void Jay::run() {
if (client.connect(false)) { if (client.connect(false)) {
@ -25,14 +26,36 @@ void Jay::run() {
std::cout << "Connection terminated with error: " << ce << std::endl; 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) { 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; std::cout << "Received message: " << message.body() << std::endl;
// Implement your response logic here
} }
}
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) { Logger* Jay::addLogger(gloox::LogLevel level) {
loggers.emplace_back(std::make_unique<Logger>(client.logInstance(), level)); loggers.emplace_back(std::make_unique<Logger>(client.logInstance(), level));
return loggers.back().get(); return loggers.back().get();
} }
void Jay::addOwner(const std::string& jid) {
owners.insert(jid);
}

9
jay.h
View File

@ -4,28 +4,35 @@
#pragma once #pragma once
#include <string> #include <string>
#include <set>
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include <memory> #include <memory>
#include <gloox/client.h> #include <gloox/client.h>
#include <gloox/messagehandler.h> #include <gloox/messagehandler.h>
#include <gloox/connectionlistener.h>
#include <gloox/message.h> #include <gloox/message.h>
#include "logger.h" #include "logger.h"
class Jay : public gloox::MessageHandler { class Jay : public gloox::MessageHandler, public gloox::ConnectionListener {
public: public:
Jay(const std::string& jid, const std::string& password); Jay(const std::string& jid, const std::string& password);
~Jay(); ~Jay();
void handleMessage(const gloox::Message& message, gloox::MessageSession* session = 0) override; void handleMessage(const gloox::Message& message, gloox::MessageSession* session = 0) override;
void onConnect() override;
void onDisconnect(gloox::ConnectionError e) override;
bool onTLSConnect(const gloox::CertInfo&) override;
void run(); void run();
Logger* addLogger(gloox::LogLevel level); Logger* addLogger(gloox::LogLevel level);
void addOwner(const std::string& jid);
private: private:
gloox::Client client; gloox::Client client;
std::vector<std::unique_ptr<Logger>> loggers; std::vector<std::unique_ptr<Logger>> loggers;
std::set<std::string> owners;
}; };

View File

@ -5,20 +5,35 @@
#include "logger.h" #include "logger.h"
#include <string> #include <string>
#include <sstream>
#include <iostream> #include <iostream>
int main(int argc, char** argv) { std::string readEnv(const std::string& key, const std::string& defaultValue = "") {
if (argc != 3) { const char* val = std::getenv(key.data());
std::cerr << "Usage: " << argv[0] << " <jid> <password>\n"; if (val)
return 1; return std::string(val);
return defaultValue;
} }
std::string jid = argv[1]; int main(int argc, char** argv) {
std::string password = argv[2]; std::string jid = readEnv("JID");
std::string password = readEnv("PASSWORD");
if (jid.empty() || password.empty()) {
std::cout << "You need to provide JID and PASSWORD environment variables" << std::endl;
return - 1;
}
Jay bot(jid, password); Jay bot(jid, password);
bot.addLogger(gloox::LogLevelDebug); bot.addLogger(gloox::LogLevelDebug);
std::string owners = readEnv("OWNERS");
std::stringstream ss(owners);
std::string owner;
while (std::getline(ss, owner, ','))
bot.addOwner(owner);
bot.run(); bot.run();
return 0; return 0;