138 lines
5.1 KiB
C++
138 lines
5.1 KiB
C++
#include "config.h"
|
|
#include "nlohmann/json.hpp"
|
|
#include "spdlog/spdlog.h"
|
|
#include <fstream>
|
|
#include <string>
|
|
|
|
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_SOURCES = "vk_sources";
|
|
const char *JSON_KEY_TG_SOURCES = "tg_sources";
|
|
const char *JSON_KEY_TG_DESTINAION = "tg_destination_id";
|
|
const char *JSON_KEY_ID = "id";
|
|
const char *JSON_KEY_LINK = "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_SOURCES = "vk_sources must be an array of objects";
|
|
const char *ERR_INVALID_TYPE_VK_SOURCE_ID = "vk_sources[].id must be an integer or a string";
|
|
const char *ERR_INVALID_TYPE_VK_SOURCE_LINK = "vk_sources[].link must be a string";
|
|
const char *ERR_INVALID_TYPE_TG_SOURCES = "tg_sources must be an array of objects";
|
|
const char *ERR_INVALID_TYPE_TG_SOURCE_ID = "tg_sources[].id must be an integer";
|
|
const char *ERR_INVALID_TYPE_TG_SOURCE_LINK = "tg_sources[].link must be a string";
|
|
const char *ERR_INVALID_TYPE_TG_DESTINATION = "tg_destination_id must be an integer";
|
|
|
|
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_TG_DESTINAION)) {
|
|
json tgDestinationId_ = config[JSON_KEY_TG_DESTINAION];
|
|
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);
|
|
}
|
|
}
|
|
|
|
json vkSources_ = config[JSON_KEY_VK_SOURCES];
|
|
if (vkSources_.type() == json::value_t::array) {
|
|
for (auto &src_ : vkSources_) {
|
|
if (src_.type() != json::value_t::object) {
|
|
throw InvalidConfigException(ERR_INVALID_TYPE_VK_SOURCES);
|
|
}
|
|
VkSourceConfig &&src { };
|
|
if (src_.contains(JSON_KEY_LINK)) {
|
|
src.link = src_[JSON_KEY_LINK];
|
|
}
|
|
if (!src_.contains(JSON_KEY_ID) ||
|
|
src_[JSON_KEY_ID].type() != json::value_t::string &&
|
|
src_[JSON_KEY_ID].type() != json::value_t::number_integer &&
|
|
src_[JSON_KEY_ID].type() != json::value_t::number_unsigned) {
|
|
throw InvalidConfigException(ERR_INVALID_TYPE_VK_SOURCE_ID);
|
|
}
|
|
json id_ = src_[JSON_KEY_ID];
|
|
if (id_.type() == json::value_t::string) {
|
|
src.id = id_.get<std::string>();
|
|
} else {
|
|
src.id = id_.get<long>();
|
|
}
|
|
vkSources.emplace_back(src);
|
|
}
|
|
} else {
|
|
throw InvalidConfigException(ERR_INVALID_TYPE_VK_SOURCES);
|
|
}
|
|
|
|
json tgSources_ = config[JSON_KEY_TG_SOURCES];
|
|
if (tgSources_.type() == json::value_t::array) {
|
|
for (auto &src_ : tgSources_) {
|
|
if (src_.type() != json::value_t::object) {
|
|
throw InvalidConfigException(ERR_INVALID_TYPE_VK_SOURCES);
|
|
}
|
|
if (!src_.contains(JSON_KEY_ID) ||
|
|
src_[JSON_KEY_ID].type() != json::value_t::number_integer &&
|
|
src_[JSON_KEY_ID].type() != json::value_t::number_unsigned) {
|
|
throw InvalidConfigException(ERR_INVALID_TYPE_VK_SOURCE_ID);
|
|
}
|
|
TgSourceConfig &&src { .id = src_[JSON_KEY_ID]};
|
|
if (src_.contains(JSON_KEY_LINK)) {
|
|
src.link = src_[JSON_KEY_LINK];
|
|
}
|
|
tgSources.emplace_back(src);
|
|
}
|
|
} else {
|
|
throw InvalidConfigException(ERR_INVALID_TYPE_TG_SOURCES);
|
|
}
|
|
} |