Trying different stuff
This commit is contained in:
parent
b2692a43f3
commit
698e706daa
33
jay.cpp
33
jay.cpp
@ -5,16 +5,17 @@
|
||||
|
||||
Jay::Jay(const std::string& jid, const std::string& password) :
|
||||
client(jid, password),
|
||||
loggers()
|
||||
loggers(),
|
||||
owners()
|
||||
{
|
||||
client.registerMessageHandler(this);
|
||||
client.registerConnectionListener(this);
|
||||
|
||||
client.setTls(gloox::TLSPolicy::TLSOptional);
|
||||
client.setSASLMechanisms(gloox::SaslMechScramSha1);
|
||||
}
|
||||
|
||||
Jay::~Jay() {
|
||||
loggers.clear();
|
||||
}
|
||||
Jay::~Jay() {}
|
||||
|
||||
void Jay::run() {
|
||||
if (client.connect(false)) {
|
||||
@ -25,14 +26,36 @@ void Jay::run() {
|
||||
|
||||
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;
|
||||
// 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) {
|
||||
loggers.emplace_back(std::make_unique<Logger>(client.logInstance(), level));
|
||||
return loggers.back().get();
|
||||
}
|
||||
|
||||
void Jay::addOwner(const std::string& jid) {
|
||||
owners.insert(jid);
|
||||
}
|
||||
|
9
jay.h
9
jay.h
@ -4,28 +4,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <set>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include <gloox/client.h>
|
||||
#include <gloox/messagehandler.h>
|
||||
#include <gloox/connectionlistener.h>
|
||||
#include <gloox/message.h>
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
class Jay : public gloox::MessageHandler {
|
||||
class Jay : public gloox::MessageHandler, public gloox::ConnectionListener {
|
||||
public:
|
||||
Jay(const std::string& jid, const std::string& password);
|
||||
~Jay();
|
||||
|
||||
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();
|
||||
|
||||
Logger* addLogger(gloox::LogLevel level);
|
||||
void addOwner(const std::string& jid);
|
||||
|
||||
private:
|
||||
gloox::Client client;
|
||||
std::vector<std::unique_ptr<Logger>> loggers;
|
||||
std::set<std::string> owners;
|
||||
};
|
||||
|
29
main.cpp
29
main.cpp
@ -5,20 +5,35 @@
|
||||
#include "logger.h"
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
if (argc != 3) {
|
||||
std::cerr << "Usage: " << argv[0] << " <jid> <password>\n";
|
||||
return 1;
|
||||
}
|
||||
std::string readEnv(const std::string& key, const std::string& defaultValue = "") {
|
||||
const char* val = std::getenv(key.data());
|
||||
if (val)
|
||||
return std::string(val);
|
||||
|
||||
std::string jid = argv[1];
|
||||
std::string password = argv[2];
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
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);
|
||||
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();
|
||||
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user