From b2692a43f3f779b1b7a07b61a696111803be1375 Mon Sep 17 00:00:00 2001 From: blue Date: Sat, 30 Mar 2024 22:44:52 -0300 Subject: [PATCH] first commit --- CMakeLists.txt | 19 +++++++++++++++++++ README.md | 0 jay.cpp | 38 ++++++++++++++++++++++++++++++++++++++ jay.h | 31 +++++++++++++++++++++++++++++++ logger.cpp | 20 ++++++++++++++++++++ logger.h | 19 +++++++++++++++++++ main.cpp | 25 +++++++++++++++++++++++++ 7 files changed, 152 insertions(+) create mode 100644 CMakeLists.txt create mode 100644 README.md create mode 100644 jay.cpp create mode 100644 jay.h create mode 100644 logger.cpp create mode 100644 logger.h create mode 100644 main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..afd8311 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.10) +project(jay + VERSION 0.0.1 + LANGUAGES CXX +) + +set(CMAKE_CXX_STANDARD 20) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +# Find gloox library +find_package(PkgConfig) +pkg_search_module(GLOOX REQUIRED gloox) + +add_executable(jay main.cpp jay.cpp logger.cpp) + +target_include_directories(jay PRIVATE ${GLOOX_INCLUDE_DIRS}) +target_link_libraries(jay ${GLOOX_LIBRARIES}) + +install(TARGETS jay RUNTIME DESTINATION bin) diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/jay.cpp b/jay.cpp new file mode 100644 index 0000000..4aa59db --- /dev/null +++ b/jay.cpp @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2024 Yury Gubich +// 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() +{ + client.registerMessageHandler(this); + client.setTls(gloox::TLSPolicy::TLSOptional); + client.setSASLMechanisms(gloox::SaslMechScramSha1); +} + +Jay::~Jay() { + loggers.clear(); +} + +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; + } +} + +void Jay::handleMessage(const gloox::Message& message, gloox::MessageSession* session) { + std::cout << "Received message: " << message.body() << std::endl; + // Implement your response logic here +} + +Logger* Jay::addLogger(gloox::LogLevel level) { + loggers.emplace_back(std::make_unique(client.logInstance(), level)); + return loggers.back().get(); +} diff --git a/jay.h b/jay.h new file mode 100644 index 0000000..d097ba2 --- /dev/null +++ b/jay.h @@ -0,0 +1,31 @@ +// SPDX-FileCopyrightText: 2024 Yury Gubich +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include +#include +#include +#include + +#include +#include +#include + +#include "logger.h" + +class Jay : public gloox::MessageHandler { +public: + Jay(const std::string& jid, const std::string& password); + ~Jay(); + + void handleMessage(const gloox::Message& message, gloox::MessageSession* session = 0) override; + + void run(); + + Logger* addLogger(gloox::LogLevel level); + +private: + gloox::Client client; + std::vector> loggers; +}; diff --git a/logger.cpp b/logger.cpp new file mode 100644 index 0000000..d53d303 --- /dev/null +++ b/logger.cpp @@ -0,0 +1,20 @@ +// SPDX-FileCopyrightText: 2024 Yury Gubich +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "logger.h" + +#include + +Logger::Logger(gloox::LogSink& sink, gloox::LogLevel level): + sink(sink) +{ + sink.registerLogHandler(level, gloox::LogAreaAll, this); +} + +Logger::~Logger() { + sink.removeLogHandler(this); +} + +void Logger::handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message) { + std::cout << "Log message: " << message << std::endl; +} diff --git a/logger.h b/logger.h new file mode 100644 index 0000000..cd02b3d --- /dev/null +++ b/logger.h @@ -0,0 +1,19 @@ +// SPDX-FileCopyrightText: 2024 Yury Gubich +// SPDX-License-Identifier: GPL-3.0-or-later + +#pragma once + +#include +#include + +class Logger : public gloox::LogHandler { +public: + Logger(gloox::LogSink& sink, gloox::LogLevel level); + ~Logger(); + + void handleLog(gloox::LogLevel level, gloox::LogArea area, const std::string& message) override; + +private: + gloox::LogSink& sink; + gloox::LogLevel level; +}; diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..b0da7dc --- /dev/null +++ b/main.cpp @@ -0,0 +1,25 @@ +// SPDX-FileCopyrightText: 2024 Yury Gubich +// SPDX-License-Identifier: GPL-3.0-or-later + +#include "jay.h" +#include "logger.h" + +#include +#include + +int main(int argc, char** argv) { + if (argc != 3) { + std::cerr << "Usage: " << argv[0] << " \n"; + return 1; + } + + std::string jid = argv[1]; + std::string password = argv[2]; + + Jay bot(jid, password); + bot.addLogger(gloox::LogLevelDebug); + + bot.run(); + + return 0; +}