Some further achitecture thoguhts
This commit is contained in:
parent
ac8db32552
commit
b997972ec1
21 changed files with 103 additions and 15 deletions
13
component/CMakeLists.txt
Normal file
13
component/CMakeLists.txt
Normal file
|
@ -0,0 +1,13 @@
|
|||
set(SOURCES
|
||||
config.cpp
|
||||
logger.cpp
|
||||
actor.cpp
|
||||
)
|
||||
|
||||
set(HEADERS
|
||||
config.h
|
||||
logger.h
|
||||
actor.h
|
||||
)
|
||||
|
||||
target_sources(${EXEC_NAME} PRIVATE ${SOURCES})
|
4
component/actor.cpp
Normal file
4
component/actor.cpp
Normal file
|
@ -0,0 +1,4 @@
|
|||
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "actor.h"
|
7
component/actor.h
Normal file
7
component/actor.h
Normal file
|
@ -0,0 +1,7 @@
|
|||
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
class Actor {
|
||||
};
|
63
component/config.cpp
Normal file
63
component/config.cpp
Normal file
|
@ -0,0 +1,63 @@
|
|||
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "config.h"
|
||||
|
||||
Config::Config(const std::string& path):
|
||||
root(YAML::LoadFile(path))
|
||||
{}
|
||||
|
||||
std::string Config::getBareJID() const {
|
||||
return root["jid"].as<std::string>();
|
||||
}
|
||||
|
||||
std::string Config::getPassword() const {
|
||||
return root["password"].as<std::string>();
|
||||
}
|
||||
|
||||
std::string Config::getResource() const {
|
||||
return root["resource"].as<std::string>("bot");
|
||||
}
|
||||
|
||||
std::string Config::getFullJID() const {
|
||||
return getBareJID() + "/" + getResource();
|
||||
}
|
||||
|
||||
gloox::LogLevel Config::getLogLevel() const {
|
||||
std::string level = root["logLevel"].as<std::string>("warning");
|
||||
|
||||
if (level == "debug")
|
||||
return gloox::LogLevelDebug;
|
||||
else if (level == "error")
|
||||
return gloox::LogLevelError;
|
||||
else
|
||||
return gloox::LogLevelWarning;
|
||||
}
|
||||
|
||||
gloox::TLSPolicy Config::getTLSPolicy() const {
|
||||
std::string level = root["tls"].as<std::string>("optional");
|
||||
|
||||
if (level == "disabled")
|
||||
return gloox::TLSDisabled;
|
||||
else if (level == "required")
|
||||
return gloox::TLSRequired;
|
||||
else
|
||||
return gloox::TLSOptional;
|
||||
}
|
||||
|
||||
|
||||
std::set<std::string> Config::getOwners() const {
|
||||
std::set<std::string> result;
|
||||
YAML::Node owners = root["resource"];
|
||||
if (!owners.IsSequence())
|
||||
return result;
|
||||
|
||||
for (const YAML::Node& node : owners)
|
||||
result.insert(node.as<std::string>());
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
bool Config::isValid() const {
|
||||
return !getBareJID().empty() && !getPassword().empty();
|
||||
}
|
29
component/config.h
Normal file
29
component/config.h
Normal file
|
@ -0,0 +1,29 @@
|
|||
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <set>
|
||||
|
||||
#include "gloox/gloox.h"
|
||||
|
||||
#include "yaml-cpp/yaml.h"
|
||||
|
||||
class Config {
|
||||
public:
|
||||
Config(const std::string& path);
|
||||
|
||||
bool isValid() const;
|
||||
|
||||
std::string getBareJID() const;
|
||||
std::string getFullJID() const;
|
||||
std::string getPassword() const;
|
||||
std::string getResource() const;
|
||||
std::set<std::string> getOwners() const;
|
||||
gloox::LogLevel getLogLevel() const;
|
||||
gloox::TLSPolicy getTLSPolicy() const;
|
||||
|
||||
private:
|
||||
YAML::Node root;
|
||||
};
|
96
component/logger.cpp
Normal file
96
component/logger.cpp
Normal file
|
@ -0,0 +1,96 @@
|
|||
// SPDX-FileCopyrightText: 2024 Yury Gubich <blue@macaw.me>
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
#include "logger.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <chrono>
|
||||
|
||||
static constexpr std::string_view logLevelMap[] = {
|
||||
"DEBUG", // 0
|
||||
"WARNING", // 1
|
||||
"ERROR", // 2
|
||||
};
|
||||
|
||||
static constexpr std::string_view colorMap[] = {
|
||||
"\033[32m", // DEBUG (Green)
|
||||
"\033[33m", // WARNING (Yellow)
|
||||
"\033[31m", // ERROR (Red)
|
||||
};
|
||||
|
||||
static constexpr std::string_view clear = "\033[0m";
|
||||
static constexpr std::string_view bold = "\033[1m";
|
||||
|
||||
void writeTimestamp() {
|
||||
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
|
||||
std::time_t time_now = std::chrono::system_clock::to_time_t(now);
|
||||
std::tm local_time = *std::localtime(&time_now);
|
||||
std::chrono::milliseconds millis = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
|
||||
|
||||
std::cout << "\033[90m" << std::put_time(&local_time, "%Y-%m-%d %H:%M:%S");
|
||||
std::cout << "." << std::setfill('0') << std::setw(3) << millis.count();
|
||||
std::cout << clear << ' ';
|
||||
}
|
||||
|
||||
constexpr std::string_view getLogLevel(gloox::LogLevel level) {
|
||||
return (level >= 0 && level < 3) ? logLevelMap[level] : "UNKNOWN";
|
||||
}
|
||||
|
||||
constexpr std::string_view getColor(gloox::LogLevel level) {
|
||||
return (level >= 0 && level < 3) ? colorMap[level] : "";
|
||||
}
|
||||
|
||||
void writeTags(gloox::LogArea area) {
|
||||
if (area & gloox::LogAreaClassParser)
|
||||
std::cout << " [Parser]";
|
||||
if (area & gloox::LogAreaClassConnectionTCPBase)
|
||||
std::cout << " [Connection]";
|
||||
if (area & gloox::LogAreaClassClient)
|
||||
std::cout << " [Client]";
|
||||
if (area & gloox::LogAreaClassClientbase)
|
||||
std::cout << " [Client Base]";
|
||||
if (area & gloox::LogAreaClassComponent)
|
||||
std::cout << " [Component]";
|
||||
if (area & gloox::LogAreaClassDns)
|
||||
std::cout << " [DNS]";
|
||||
if (area & gloox::LogAreaClassConnectionHTTPProxy)
|
||||
std::cout << " [HTTP Proxy]";
|
||||
if (area & gloox::LogAreaClassConnectionSOCKS5Proxy)
|
||||
std::cout << " [SOCKS5 Proxy]";
|
||||
if (area & gloox::LogAreaClassConnectionTCPClient)
|
||||
std::cout << " [TCP Client]";
|
||||
if (area & gloox::LogAreaClassConnectionTCPServer)
|
||||
std::cout << " [TCP Server]";
|
||||
if (area & gloox::LogAreaClassS5BManager)
|
||||
std::cout << " [SOCKS5 Bytestream Manager]";
|
||||
if (area & gloox::LogAreaClassSOCKS5Bytestream)
|
||||
std::cout << " [SOCKS5 Bytestream]";
|
||||
if (area & gloox::LogAreaLinkLocalManager)
|
||||
std::cout << " [Link Local Manager]";
|
||||
if (area & gloox::LogAreaXmlIncoming)
|
||||
std::cout << " \033[1;36m[XML IN]";
|
||||
if (area & gloox::LogAreaXmlOutgoing)
|
||||
std::cout << " \033[1;35m[XML OUT]";
|
||||
if (area & gloox::LogAreaUser)
|
||||
std::cout << " [USER]";
|
||||
|
||||
if (area == gloox::LogAreaClassDns)
|
||||
std::cout << '\t';
|
||||
}
|
||||
|
||||
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) {
|
||||
writeTimestamp();
|
||||
std::cout << getColor(level) << bold << "[" << getLogLevel(level) << "]" << clear << bold;
|
||||
writeTags(area);
|
||||
std::cout << clear << '\t' << message << clear << std::endl;
|
||||
}
|
20
component/logger.h
Normal file
20
component/logger.h
Normal file
|
@ -0,0 +1,20 @@
|
|||
// 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;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue