Initial commit

This commit is contained in:
faumaray committed 2026-02-01 23:04:49 +03:00
commit a583c17916
56 files changed
+11025

No files matched your search

+355
View File
@@ -0,0 +1,355 @@
#include "echoion/core/ConfigLoader.hpp"
#include "echoion/api/AudioTypes.hpp"
#include <glaze/glaze.hpp>
#include <fstream>
#include <format>
namespace Echoion::Core {
// JSON structures for configuration
struct BotConfigJson {
std::string botName = "Echoion";
std::string commandPrefix = "!";
float defaultVolume = 0.5f;
bool autoReconnect = true;
uint32_t reconnectDelay = 5000;
uint32_t maxReconnectAttempts = 5;
std::optional<std::string> proxyHost;
std::optional<uint16_t> proxyPort;
std::optional<std::string> proxyUsername;
std::optional<std::string> proxyPassword;
std::optional<std::string> proxyType;
std::optional<std::string> youtubeApiKey;
std::string preferredQuality = "High";
bool allowPlaylistImports = true;
uint32_t maxQueueSize = 100;
uint32_t trackTimeout = 30000;
std::optional<std::string> youtubeCookiesPath;
std::optional<std::string> cookiesFromBrowser;
};
struct ServerConfigJson {
std::string address;
uint16_t port = 9987;
std::string serverPassword = "";
std::string nickname = "EchoionBot";
std::optional<std::string> defaultChannelId;
std::optional<std::string> channelPassword;
std::vector<uint64_t> adminGroups;
std::vector<uint64_t> djGroups;
std::vector<uint64_t> bannedGroups;
};
struct SecurityConfigJson {
bool requireAuthForAdmin = true;
uint32_t maxLoginAttempts = 5;
uint32_t lockoutDuration = 300000;
uint32_t sessionTimeout = 3600000;
bool enableAuditLog = true;
};
struct AppConfigJson {
BotConfigJson bot;
ServerConfigJson server;
SecurityConfigJson security;
};
} // namespace Echoion::Core
// Glaze metadata for JSON serialization
template<> struct glz::meta<Echoion::Core::BotConfigJson> {
using T = Echoion::Core::BotConfigJson;
static constexpr auto value = object(
"botName", &T::botName,
"commandPrefix", &T::commandPrefix,
"defaultVolume", &T::defaultVolume,
"autoReconnect", &T::autoReconnect,
"reconnectDelay", &T::reconnectDelay,
"maxReconnectAttempts", &T::maxReconnectAttempts,
"proxyHost", &T::proxyHost,
"proxyPort", &T::proxyPort,
"proxyUsername", &T::proxyUsername,
"proxyPassword", &T::proxyPassword,
"proxyType", &T::proxyType,
"youtubeApiKey", &T::youtubeApiKey,
"preferredQuality", &T::preferredQuality,
"allowPlaylistImports", &T::allowPlaylistImports,
"maxQueueSize", &T::maxQueueSize,
"trackTimeout", &T::trackTimeout,
"youtubeCookiesPath", &T::youtubeCookiesPath,
"cookiesFromBrowser", &T::cookiesFromBrowser
);
};
template<> struct glz::meta<Echoion::Core::ServerConfigJson> {
using T = Echoion::Core::ServerConfigJson;
static constexpr auto value = object(
"address", &T::address,
"port", &T::port,
"serverPassword", &T::serverPassword,
"nickname", &T::nickname,
"defaultChannelId", &T::defaultChannelId,
"channelPassword", &T::channelPassword,
"adminGroups", &T::adminGroups,
"djGroups", &T::djGroups,
"bannedGroups", &T::bannedGroups
);
};
template<> struct glz::meta<Echoion::Core::SecurityConfigJson> {
using T = Echoion::Core::SecurityConfigJson;
static constexpr auto value = object(
"requireAuthForAdmin", &T::requireAuthForAdmin,
"maxLoginAttempts", &T::maxLoginAttempts,
"lockoutDuration", &T::lockoutDuration,
"sessionTimeout", &T::sessionTimeout,
"enableAuditLog", &T::enableAuditLog
);
};
template<> struct glz::meta<Echoion::Core::AppConfigJson> {
using T = Echoion::Core::AppConfigJson;
static constexpr auto value = object(
"bot", &T::bot,
"server", &T::server,
"security", &T::security
);
};
namespace Echoion::Core {
Api::Result<Api::AppConfig> ConfigLoader::loadFromFile(const std::filesystem::path& path) {
if (!std::filesystem::exists(path)) {
return std::unexpected(Api::Error(Api::ErrorCode::NotFound,
std::format("Configuration file not found: {}", path.string())));
}
std::ifstream file(path);
if (!file.is_open()) {
return std::unexpected(Api::Error(Api::ErrorCode::PermissionDenied,
std::format("Failed to open configuration file: {}", path.string())));
}
std::string content((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
file.close();
AppConfigJson jsonConfig;
auto ec = glz::read_json(jsonConfig, content);
if (ec) {
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
std::format("Failed to parse configuration: {}", glz::format_error(ec, content))));
}
// Convert to API config
Api::AppConfig config;
// Bot config
config.bot.botName = jsonConfig.bot.botName;
config.bot.commandPrefix = jsonConfig.bot.commandPrefix;
config.bot.defaultVolume = jsonConfig.bot.defaultVolume;
config.bot.autoReconnect = jsonConfig.bot.autoReconnect;
config.bot.reconnectDelay = std::chrono::milliseconds(jsonConfig.bot.reconnectDelay);
config.bot.maxReconnectAttempts = jsonConfig.bot.maxReconnectAttempts;
config.bot.preferredQuality = parseQuality(jsonConfig.bot.preferredQuality);
config.bot.allowPlaylistImports = jsonConfig.bot.allowPlaylistImports;
config.bot.maxQueueSize = jsonConfig.bot.maxQueueSize;
config.bot.trackTimeout = std::chrono::milliseconds(jsonConfig.bot.trackTimeout);
// Proxy config
if (jsonConfig.bot.proxyHost.has_value() && jsonConfig.bot.proxyPort.has_value()) {
Api::ProxyConfig proxy;
proxy.host = *jsonConfig.bot.proxyHost;
proxy.port = *jsonConfig.bot.proxyPort;
proxy.username = jsonConfig.bot.proxyUsername;
proxy.password = jsonConfig.bot.proxyPassword;
// Parse proxy type (default to HTTP)
if (jsonConfig.bot.proxyType.has_value()) {
const auto& typeStr = *jsonConfig.bot.proxyType;
if (typeStr == "SOCKS5" || typeStr == "socks5") {
proxy.type = Api::ProxyType::SOCKS5;
} else {
proxy.type = Api::ProxyType::HTTP;
}
} else {
proxy.type = Api::ProxyType::HTTP;
}
config.bot.proxy = proxy;
}
// YouTube API key
if (jsonConfig.bot.youtubeApiKey.has_value()) {
config.bot.youtubeApiKey = jsonConfig.bot.youtubeApiKey;
}
// YouTube cookies
if (jsonConfig.bot.youtubeCookiesPath.has_value()) {
config.bot.youtubeCookiesPath = jsonConfig.bot.youtubeCookiesPath;
}
if (jsonConfig.bot.cookiesFromBrowser.has_value()) {
config.bot.cookiesFromBrowser = jsonConfig.bot.cookiesFromBrowser;
}
// Server config
config.server.address = jsonConfig.server.address;
config.server.port = jsonConfig.server.port;
config.server.serverPassword = jsonConfig.server.serverPassword;
config.server.nickname = jsonConfig.server.nickname;
config.server.defaultChannelId = jsonConfig.server.defaultChannelId;
config.server.channelPassword = jsonConfig.server.channelPassword;
config.server.adminGroups = jsonConfig.server.adminGroups;
config.server.djGroups = jsonConfig.server.djGroups;
config.server.bannedGroups = jsonConfig.server.bannedGroups;
// Security config
config.security.requireAuthForAdmin = jsonConfig.security.requireAuthForAdmin;
config.security.maxLoginAttempts = jsonConfig.security.maxLoginAttempts;
config.security.lockoutDuration = std::chrono::milliseconds(jsonConfig.security.lockoutDuration);
config.security.sessionTimeout = std::chrono::milliseconds(jsonConfig.security.sessionTimeout);
config.security.enableAuditLog = jsonConfig.security.enableAuditLog;
ECHOION_LOG_INFO(std::format("Configuration loaded from {}", path.string()));
return config;
}
Api::Result<void> ConfigLoader::saveToFile(const std::filesystem::path& path, const Api::AppConfig& config) {
AppConfigJson jsonConfig;
// Bot config
jsonConfig.bot.botName = config.bot.botName;
jsonConfig.bot.commandPrefix = config.bot.commandPrefix;
jsonConfig.bot.defaultVolume = config.bot.defaultVolume;
jsonConfig.bot.autoReconnect = config.bot.autoReconnect;
jsonConfig.bot.reconnectDelay = static_cast<uint32_t>(config.bot.reconnectDelay.count());
jsonConfig.bot.maxReconnectAttempts = config.bot.maxReconnectAttempts;
jsonConfig.bot.preferredQuality = qualityToString(config.bot.preferredQuality);
jsonConfig.bot.allowPlaylistImports = config.bot.allowPlaylistImports;
jsonConfig.bot.maxQueueSize = config.bot.maxQueueSize;
jsonConfig.bot.trackTimeout = static_cast<uint32_t>(config.bot.trackTimeout.count());
// Proxy config
if (config.bot.proxy.has_value()) {
jsonConfig.bot.proxyHost = config.bot.proxy->host;
jsonConfig.bot.proxyPort = config.bot.proxy->port;
jsonConfig.bot.proxyUsername = config.bot.proxy->username;
jsonConfig.bot.proxyPassword = config.bot.proxy->password;
jsonConfig.bot.proxyType = config.bot.proxy->type == Api::ProxyType::SOCKS5 ? "SOCKS5" : "HTTP";
}
// YouTube API key
if (config.bot.youtubeApiKey.has_value()) {
jsonConfig.bot.youtubeApiKey = *config.bot.youtubeApiKey;
}
// YouTube cookies
if (config.bot.youtubeCookiesPath.has_value()) {
jsonConfig.bot.youtubeCookiesPath = *config.bot.youtubeCookiesPath;
}
if (config.bot.cookiesFromBrowser.has_value()) {
jsonConfig.bot.cookiesFromBrowser = *config.bot.cookiesFromBrowser;
}
// Server config
jsonConfig.server.address = config.server.address;
jsonConfig.server.port = config.server.port;
jsonConfig.server.serverPassword = config.server.serverPassword;
jsonConfig.server.nickname = config.server.nickname;
jsonConfig.server.defaultChannelId = config.server.defaultChannelId;
jsonConfig.server.channelPassword = config.server.channelPassword;
jsonConfig.server.adminGroups = config.server.adminGroups;
jsonConfig.server.djGroups = config.server.djGroups;
jsonConfig.server.bannedGroups = config.server.bannedGroups;
// Security config
jsonConfig.security.requireAuthForAdmin = config.security.requireAuthForAdmin;
jsonConfig.security.maxLoginAttempts = config.security.maxLoginAttempts;
jsonConfig.security.lockoutDuration = static_cast<uint32_t>(config.security.lockoutDuration.count());
jsonConfig.security.sessionTimeout = static_cast<uint32_t>(config.security.sessionTimeout.count());
jsonConfig.security.enableAuditLog = config.security.enableAuditLog;
auto json = glz::write_json(jsonConfig);
if (!json.has_value())
{
return std::unexpected(Api::Error(Api::ErrorCode::ParseError,
std::format("Failed to serialize: {}", json.error().custom_error_message)));
}
std::ofstream file(path);
if (!file.is_open()) {
return std::unexpected(Api::Error(Api::ErrorCode::PermissionDenied,
std::format("Failed to write configuration file: {}", path.string())));
}
file << json.value();
file.close();
ECHOION_LOG_INFO(std::format("Configuration saved to {}", path.string()));
return {};
}
std::filesystem::path ConfigLoader::getDefaultConfigPath() {
// Try to get config path from environment or use default
const char* configHome = std::getenv("XDG_CONFIG_HOME");
if (configHome) {
return std::filesystem::path(configHome) / "echoion" / "config.json";
}
const char* home = std::getenv("HOME");
if (home) {
return std::filesystem::path(home) / ".config" / "echoion" / "config.json";
}
// Fallback to current directory
return "echoion_config.json";
}
Api::AppConfig ConfigLoader::createDefaultConfig() {
Api::AppConfig config;
// Default bot config
config.bot.botName = "Echoion";
config.bot.commandPrefix = "!";
config.bot.defaultVolume = 0.5f;
config.bot.autoReconnect = true;
config.bot.reconnectDelay = std::chrono::milliseconds(5000);
config.bot.maxReconnectAttempts = 5;
config.bot.preferredQuality = Api::AudioQuality::High;
config.bot.allowPlaylistImports = true;
config.bot.maxQueueSize = 100;
config.bot.trackTimeout = std::chrono::milliseconds(30000);
// Default server config
config.server.address = "localhost";
config.server.port = 9987;
config.server.nickname = "EchoionBot";
// Default security config
config.security.requireAuthForAdmin = true;
config.security.maxLoginAttempts = 5;
config.security.lockoutDuration = std::chrono::milliseconds(300000);
config.security.sessionTimeout = std::chrono::milliseconds(3600000);
config.security.enableAuditLog = true;
return config;
}
Api::AudioQuality ConfigLoader::parseQuality(const std::string& str) {
if (str == "Low") return Api::AudioQuality::Low;
if (str == "Medium") return Api::AudioQuality::Medium;
if (str == "High") return Api::AudioQuality::High;
if (str == "VeryHigh") return Api::AudioQuality::VeryHigh;
if (str == "Lossless") return Api::AudioQuality::Lossless;
return Api::AudioQuality::High; // Default
}
std::string ConfigLoader::qualityToString(Api::AudioQuality quality) {
switch (quality) {
case Api::AudioQuality::Low: return "Low";
case Api::AudioQuality::Medium: return "Medium";
case Api::AudioQuality::High: return "High";
case Api::AudioQuality::VeryHigh: return "VeryHigh";
case Api::AudioQuality::Lossless: return "Lossless";
default: return "High";
}
}
} // namespace Echoion::Core