#include "config.h" #include "nlohmann/json.hpp" #include "spdlog/spdlog.h" #include #include using namespace config; using json = nlohmann::json; const char *JSON_KEY_VK_SERVICE_KEY = "vk_service_key"; const char *JSON_KEY_TG_API_ID = "tg_api_id"; const char *JSON_KEY_TG_API_HASH = "tg_api_hash"; const char *JSON_KEY_TG_PHONE_NUMBER = "tg_phone_number"; const char *JSON_KEY_VK_SOURCE = "vk_source"; const char *JSON_KEY_TG_SOURCE_ID = "tg_source_id"; const char *JSON_KEY_TG_DESTINATION_ID = "tg_destination_id"; const char *JSON_KEY_VK_SOURCE_LINK = "vk_source_link"; const char *JSON_KEY_TG_SOURCE_LINK = "tg_source_link"; const char *ERR_INVALID_TYPE_VK_SERVICE_KEY = "vk_service_key must be a string"; const char *ERR_INVALID_TYPE_TG_API_ID = "tg_api_id must be an integer or a string"; const char *ERR_INVALID_TYPE_TG_API_HASH = "tg_api_hash must be a string"; const char *ERR_INVALID_TYPE_TG_PHONE_NUMBER = "tg_phone_number must be a string"; const char *ERR_INVALID_TYPE_VK_SOURCE = "vk_source must be an integer or a string"; const char *ERR_INVALID_TYPE_TG_SOURCE_ID = "tg_source_id must be an integer"; const char *ERR_INVALID_TYPE_TG_DESTINATION_ID = "tg_destination_id must be an integer"; const char *ERR_INVALID_TYPE_VK_SOURCE_LINK = "vk_source_link must be a string"; const char *ERR_INVALID_TYPE_TG_SOURCE_LINK = "tg_source_link must be a string"; AppConfig::AppConfig(const std::string &filename) { std::ifstream f(filename); if (f.fail()) { return; } json config = json::parse(f); if (config.contains(JSON_KEY_VK_SERVICE_KEY)) { json vkServiceKey_ = config[JSON_KEY_VK_SERVICE_KEY]; if (vkServiceKey_.type() == json::value_t::string) { vkServiceKey = vkServiceKey_; } else { throw InvalidConfigException(ERR_INVALID_TYPE_VK_SERVICE_KEY); } } if (config.contains(JSON_KEY_TG_API_ID)) { json tgApiId_ = config[JSON_KEY_TG_API_ID]; json::value_t t = tgApiId_.type(); if (t == json::value_t::string) { std::string tgApiIdStr = tgApiId_; tgApiId = std::stol(tgApiIdStr.c_str()); } else if (t == json::value_t::number_integer || t == json::value_t::number_unsigned) { tgApiId = tgApiId_; } else { throw InvalidConfigException(ERR_INVALID_TYPE_TG_API_ID); } } if (config.contains(JSON_KEY_TG_API_HASH)) { json tgApiHash_ = config[JSON_KEY_TG_API_HASH]; if (tgApiHash_.type() == json::value_t::string) { tgApiHash = tgApiHash_; } else { throw InvalidConfigException(ERR_INVALID_TYPE_TG_API_HASH); } } if (config.contains(JSON_KEY_TG_PHONE_NUMBER)) { json tgPhoneNumber_ = config[JSON_KEY_TG_PHONE_NUMBER]; if (tgPhoneNumber_.type() == json::value_t::string) { tgPhoneNumber = tgPhoneNumber_; } else { throw InvalidConfigException(ERR_INVALID_TYPE_TG_PHONE_NUMBER); } } if (config.contains(JSON_KEY_VK_SOURCE)) { json vkSource_ = config[JSON_KEY_VK_SOURCE]; json::value_t t = vkSource_.type(); if (t == json::value_t::string) { vkSource = vkSource_.get(); } else if (t == json::value_t::number_integer || t == json::value_t::number_unsigned) { vkSource = vkSource_.get(); } else { throw InvalidConfigException(ERR_INVALID_TYPE_VK_SOURCE); } } if (config.contains(JSON_KEY_TG_SOURCE_ID)) { json tgSourceId_ = config[JSON_KEY_TG_SOURCE_ID]; if (tgSourceId_.type() == json::value_t::number_integer || tgSourceId_.type() == json::value_t::number_unsigned) { tgSourceId = tgSourceId_; } else { throw InvalidConfigException(ERR_INVALID_TYPE_TG_SOURCE_ID); } } if (config.contains(JSON_KEY_TG_DESTINATION_ID)) { json tgDestinationId_ = config[JSON_KEY_TG_DESTINATION_ID]; if (tgDestinationId_.type() == json::value_t::number_integer || tgDestinationId_.type() == json::value_t::number_unsigned) { tgDestinationId = tgDestinationId_; } else { throw InvalidConfigException(ERR_INVALID_TYPE_TG_DESTINATION_ID); } } if (config.contains(JSON_KEY_VK_SOURCE_LINK)) { json vkSourceLink_ = config[JSON_KEY_VK_SOURCE_LINK]; if (vkSourceLink_.type() == json::value_t::string) { vkSourceLink = vkSourceLink_; } else { spdlog::warn("config: {}", ERR_INVALID_TYPE_VK_SOURCE_LINK); } } if (config.contains(JSON_KEY_TG_SOURCE_LINK)) { json tgSourceLink_ = config[JSON_KEY_TG_SOURCE_LINK]; if (tgSourceLink_.type() == json::value_t::string) { tgSourceLink = tgSourceLink_; } else { spdlog::warn("config: {}", ERR_INVALID_TYPE_TG_SOURCE_LINK); } } }