first commit
This commit is contained in:
commit
b2692a43f3
19
CMakeLists.txt
Normal file
19
CMakeLists.txt
Normal file
@ -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)
|
38
jay.cpp
Normal file
38
jay.cpp
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
// 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()
|
||||||
|
{
|
||||||
|
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<Logger>(client.logInstance(), level));
|
||||||
|
return loggers.back().get();
|
||||||
|
}
|
31
jay.h
Normal file
31
jay.h
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
#include <gloox/client.h>
|
||||||
|
#include <gloox/messagehandler.h>
|
||||||
|
#include <gloox/message.h>
|
||||||
|
|
||||||
|
#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<std::unique_ptr<Logger>> loggers;
|
||||||
|
};
|
20
logger.cpp
Normal file
20
logger.cpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
19
logger.h
Normal file
19
logger.h
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <gloox/loghandler.h>
|
||||||
|
#include <gloox/logsink.h>
|
||||||
|
|
||||||
|
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;
|
||||||
|
};
|
25
main.cpp
Normal file
25
main.cpp
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||||
|
|
||||||
|
#include "jay.h"
|
||||||
|
#include "logger.h"
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main(int argc, char** argv) {
|
||||||
|
if (argc != 3) {
|
||||||
|
std::cerr << "Usage: " << argv[0] << " <jid> <password>\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;
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user