dtranslatebot/src/settings.cpp

384 lines
14 KiB
C++
Raw Normal View History

2024-01-02 03:45:06 +01:00
/*****************************************************************************
* dtranslatebot Discord Translate Bot
* Copyright (C) 2023-2024 Syping
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* This software is provided as-is, no warranties are given to you, we are not
* responsible for anything with use of the software, you are self responsible.
*****************************************************************************/
#include <dpp/json.h>
#include <mutex>
#include <filesystem>
#include <fstream>
2024-01-02 03:45:06 +01:00
#include <iostream>
#include "settings.h"
2024-02-04 07:10:02 +01:00
#include "translator_libretranslate.h"
using namespace bot::settings;
2024-02-04 08:05:55 +01:00
void process_guild_settings(const dpp::json &json, std::vector<guild> *guilds, std::vector<dpp::snowflake> *webhookIds)
{
for (auto json_guild = json.begin(); json_guild != json.end(); json_guild++) {
if (json_guild->is_object()) {
guild guild;
auto json_guild_id = json_guild->find("id");
if (json_guild_id != json_guild->end()) {
if (json_guild_id->is_number())
guild.id = *json_guild_id;
else if (json_guild_id->is_string())
guild.id = std::stoull(std::string(*json_guild_id));
else
2024-02-04 09:24:32 +01:00
throw std::invalid_argument("Guild id is not a number or a string");
2024-02-04 08:05:55 +01:00
}
else
guild.id = std::stoull(json_guild.key());
for (auto json_channel = json_guild->begin(); json_channel != json_guild->end(); json_channel++) {
if (json_channel->is_object()) {
channel channel;
auto json_channel_id = json_channel->find("id");
if (json_channel_id != json_channel->end()) {
if (json_channel_id->is_number())
channel.id = *json_channel_id;
else if (json_channel_id->is_string())
channel.id = std::stoull(std::string(*json_channel_id));
else
throw std::invalid_argument("Channel id is not a number or a string");
}
else
channel.id = std::stoull(json_channel.key());
auto json_channel_source = json_channel->find("source");
if (json_channel_source != json_channel->end())
channel.source = *json_channel_source;
auto json_channel_target = json_channel->find("target");
if (json_channel_target != json_channel->end()) {
if (json_channel_target->is_string()) {
target target;
target.target = *json_channel_target;
target.webhook = dpp::webhook(json_channel->at("webhook"));
webhookIds->push_back(target.webhook.id);
channel.targets.push_back(std::move(target));
}
else if (json_channel_target->is_object()) {
for (auto json_target = json_channel_target->begin(); json_target != json_channel_target->end(); json_target++) {
target target;
target.target = json_target.key();
target.webhook = dpp::webhook(*json_target);
webhookIds->push_back(target.webhook.id);
channel.targets.push_back(std::move(target));
}
}
}
if (!channel.source.empty() && !channel.targets.empty())
guild.channel.push_back(std::move(channel));
}
}
guilds->push_back(std::move(guild));
}
}
}
2024-02-04 09:24:32 +01:00
void process_preflang_settings(const dpp::json &json, std::vector<std::string> *preferred_langs)
2024-02-04 08:05:55 +01:00
{
2024-02-04 09:24:32 +01:00
for (auto json_preferred_lang = json.begin(); json_preferred_lang != json.end(); json_preferred_lang++) {
if (std::distance(json.begin(), json_preferred_lang) >= 25) {
2024-02-04 10:03:18 +01:00
std::cerr << "[dtranslatebot] [ERROR] preferred_lang is limited to 25 languages" << std::endl;
2024-02-04 08:05:55 +01:00
break;
}
2024-02-04 09:24:32 +01:00
preferred_langs->push_back(*json_preferred_lang);
2024-02-04 08:05:55 +01:00
}
}
2024-02-04 10:03:18 +01:00
void process_user_settings(const dpp::json &json, uint16_t *avatar_size)
{
auto json_avatar_size = json.find("avatar_size");
if (json_avatar_size != json.end()) {
*avatar_size = *json_avatar_size;
if (*avatar_size < 16)
*avatar_size = 16;
else if (*avatar_size > 4096)
*avatar_size = 4096;
}
else
*avatar_size = 256;
}
2024-02-04 08:05:55 +01:00
bool process_translator_settings(const dpp::json &json, translator *translator)
{
if (!json.is_object()) {
2024-02-04 10:03:18 +01:00
std::cerr << "[dtranslatebot] [ERROR] Translator settings needs to be in a object" << std::endl;
2024-02-04 08:05:55 +01:00
return false;
}
auto json_translate_hostname = json.find("hostname");
if (json_translate_hostname == json.end()) {
2024-02-04 10:03:18 +01:00
std::cerr << "[dtranslatebot] [ERROR] hostname can not be found in Translator settings" << std::endl;
2024-02-04 08:05:55 +01:00
return false;
}
translator->hostname = *json_translate_hostname;
auto json_translate_port = json.find("port");
if (json_translate_port == json.end()) {
2024-02-04 10:03:18 +01:00
std::cerr << "[dtranslatebot] [ERROR] port can not be found in Translator settings" << std::endl;
2024-02-04 08:05:55 +01:00
return false;
}
translator->port = *json_translate_port;
auto json_translate_url = json.find("url");
if (json_translate_url == json.end()) {
2024-02-04 10:03:18 +01:00
std::cerr << "[dtranslatebot] [ERROR] url can not be found in Translate settings" << std::endl;
2024-02-04 08:05:55 +01:00
return false;
}
translator->url = *json_translate_url;
auto json_translate_tls = json.find("tls");
if (json_translate_tls != json.end())
translator->tls = *json_translate_tls;
else
translator->tls = false;
auto json_translate_apiKey = json.find("apiKey");
if (json_translate_apiKey != json.end())
translator->apiKey = *json_translate_apiKey;
else
translator->apiKey.clear();
return true;
}
2024-02-04 07:10:02 +01:00
void settings::add_channel(const channel &channel, dpp::snowflake guild_id)
{
2024-02-04 09:24:32 +01:00
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id) {
guild->channel.push_back(channel);
return;
}
}
// We will create the guild structure when it is not in memory
2024-02-04 07:10:02 +01:00
guild guild;
guild.id = guild_id;
guild.channel.push_back(std::move(channel));
m_guilds.push_back(std::move(guild));
}
2024-02-04 07:10:02 +01:00
bool settings::add_target(const target &target, dpp::snowflake guild_id, dpp::snowflake channel_id)
2024-02-03 08:10:28 +01:00
{
2024-02-04 09:24:32 +01:00
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
2024-02-03 08:10:28 +01:00
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id) {
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
if (channel->id == channel_id) {
channel->targets.push_back(target);
return true;
}
}
return false;
}
}
return false;
}
2024-02-04 07:10:02 +01:00
void settings::add_translatebot_webhook(dpp::snowflake webhook_id)
{
2024-02-04 09:24:32 +01:00
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
m_webhookIds.push_back(webhook_id);
}
2024-01-02 03:45:06 +01:00
2024-02-04 09:24:32 +01:00
uint16_t settings::avatar_size()
2024-01-17 12:38:16 +01:00
{
2024-02-04 09:24:32 +01:00
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
2024-01-17 12:38:16 +01:00
return m_avatarSize;
}
2024-02-04 09:24:32 +01:00
const channel* settings::get_channel(const guild *guild, dpp::snowflake channel_id) const
2024-01-02 03:45:06 +01:00
{
2024-02-04 09:24:32 +01:00
if (!m_externallyLockedCount) {
#ifndef NDEBUG
std::cerr << "[DEBUG] settings::get_channel(const guild*, dpp::snowflake) have being called without settings being locked." << std::endl;
#endif
return nullptr;
}
2024-01-02 03:45:06 +01:00
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
if (channel->id == channel_id)
return &(*channel);
}
return nullptr;
}
2024-02-04 09:24:32 +01:00
const channel* settings::get_channel(dpp::snowflake guild_id, dpp::snowflake channel_id) const
2024-01-12 11:18:15 +01:00
{
2024-02-04 09:24:32 +01:00
if (!m_externallyLockedCount) {
#ifndef NDEBUG
std::cerr << "[DEBUG] settings::get_channel(dpp::snowflake, dpp::snowflake) have being called without settings being locked." << std::endl;
#endif
return nullptr;
}
2024-01-12 11:18:15 +01:00
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id) {
for (auto channel = guild->channel.begin(); channel != guild->channel.end(); channel++) {
if (channel->id == channel_id)
return &(*channel);
}
return nullptr;
2024-01-12 11:18:15 +01:00
}
}
return nullptr;
}
2024-02-04 09:24:32 +01:00
const guild* settings::get_guild(dpp::snowflake guild_id) const
2024-01-02 03:45:06 +01:00
{
2024-02-04 09:24:32 +01:00
if (!m_externallyLockedCount) {
#ifndef NDEBUG
std::cerr << "[DEBUG] settings::get_guild(dpp::snowflake) have being called without settings being locked." << std::endl;
#endif
return nullptr;
}
2024-01-02 03:45:06 +01:00
for (auto guild = m_guilds.begin(); guild != m_guilds.end(); guild++) {
if (guild->id == guild_id)
return &(*guild);
}
return nullptr;
}
2024-02-04 09:24:32 +01:00
const std::vector<std::string> settings::preferred_languages() const
{
2024-02-04 09:24:32 +01:00
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
return m_prefLangs;
}
2024-02-04 09:24:32 +01:00
const std::filesystem::path settings::storage_path() const
2024-01-02 03:45:06 +01:00
{
2024-02-04 09:24:32 +01:00
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
return m_storagePath;
2024-01-02 03:45:06 +01:00
}
2024-02-04 09:24:32 +01:00
std::unique_ptr<bot::translator::translator> settings::get_translator() const
{
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
2024-02-04 07:10:02 +01:00
std::unique_ptr<bot::translator::libretranslate> libretranslate(
2024-02-04 10:03:18 +01:00
new bot::translator::libretranslate(m_translator.hostname, m_translator.port, m_translator.url, m_translator.tls, m_translator.apiKey));
return libretranslate;
}
2024-02-04 09:24:32 +01:00
const std::string settings::token() const
2024-01-02 03:45:06 +01:00
{
2024-02-04 09:24:32 +01:00
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
2024-01-02 03:45:06 +01:00
return m_token;
}
2024-02-04 09:24:32 +01:00
bool settings::is_translatebot(dpp::snowflake webhook_id) const
{
2024-02-04 09:24:32 +01:00
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
for (auto id = m_webhookIds.begin(); id != m_webhookIds.end(); id++) {
if (*id == webhook_id)
return true;
}
return false;
}
2024-02-04 07:10:02 +01:00
void settings::lock()
2024-01-02 03:45:06 +01:00
{
m_mutex.lock();
2024-02-04 09:24:32 +01:00
m_externallyLockedCount++;
2024-01-02 03:45:06 +01:00
}
2024-02-04 07:10:02 +01:00
bool settings::parse(const std::string &data)
2024-01-02 03:45:06 +01:00
{
2024-01-12 11:18:15 +01:00
try {
2024-02-04 10:03:18 +01:00
dpp::json json;
try {
json = dpp::json::parse(data);
}
catch (const std::exception &exception) {
std::cerr << "[dtranslatebot] [EXCEPTION] " << exception.what() << std::endl;
std::cerr << "[dtranslatebot] [ERROR] Exception while parsing JSON" << std::endl;
2024-01-12 11:18:15 +01:00
return false;
}
2024-01-02 03:45:06 +01:00
2024-01-12 11:18:15 +01:00
auto json_token = json.find("token");
if (json_token == json.end()) {
2024-02-04 10:03:18 +01:00
std::cerr << "[dtranslatebot] [ERROR] Bot token can not be found" << std::endl;
2024-01-12 11:18:15 +01:00
return false;
}
2024-01-02 03:45:06 +01:00
2024-01-12 11:18:15 +01:00
const std::lock_guard<std::recursive_mutex> guard(m_mutex);
2024-02-04 08:05:55 +01:00
m_token = *json_token;
2024-01-02 03:45:06 +01:00
auto json_storage = json.find("storage");
if (json_storage != json.end())
2024-02-04 09:24:32 +01:00
m_storagePath = std::string(*json_storage);
else if (char *storagepath = getenv("DTRANSLATEBOT_STORAGE"))
2024-02-04 09:24:32 +01:00
m_storagePath = storagepath;
2024-02-04 09:24:32 +01:00
if (m_storagePath.empty())
m_storagePath = std::filesystem::current_path();
2024-02-04 08:05:55 +01:00
auto json_translator = json.find("translator");
if (json_translator == json.end()) {
2024-02-04 10:03:18 +01:00
std::cerr << "[dtranslatebot] [ERROR] Translator settings can not be found" << std::endl;
return false;
}
2024-02-04 08:05:55 +01:00
if (!process_translator_settings(*json_translator, &m_translator))
return false;
2024-02-04 10:03:18 +01:00
m_avatarSize = 256;
m_guilds.clear();
2024-02-04 09:24:32 +01:00
m_prefLangs.clear();
m_webhookIds.clear();
auto json_guilds = json.find("guilds");
2024-02-04 08:05:55 +01:00
if (json_guilds != json.end() && json_guilds->is_object())
process_guild_settings(*json_guilds, &m_guilds, &m_webhookIds);
auto json_preflangs = json.find("preferred_lang");
2024-02-04 08:05:55 +01:00
if (json_preflangs != json.end() && json_preflangs->is_array())
2024-02-04 09:24:32 +01:00
process_preflang_settings(*json_preflangs, &m_prefLangs);
2024-02-04 10:03:18 +01:00
auto json_user = json.find("user");
if (json_user != json.end() && json_user->is_object())
process_user_settings(*json_user, &m_avatarSize);
return true;
2024-01-02 03:45:06 +01:00
}
catch (const std::exception &exception) {
2024-02-04 10:03:18 +01:00
std::cerr << "[dtranslatebot] [EXCEPTION] " << exception.what() << std::endl;
}
return false;
2024-01-02 03:45:06 +01:00
}
2024-02-04 07:10:02 +01:00
bool settings::parse_file(const std::string &filename)
{
std::ifstream ifs(filename, std::ios::in | std::ios::binary);
if (!ifs.is_open()) {
2024-02-04 10:03:18 +01:00
std::cerr << "[dtranslatebot] [ERROR] Failed to open JSON configuration file located at " << filename << std::endl;
2024-02-04 07:10:02 +01:00
return false;
}
std::string sdata(std::istreambuf_iterator<char>{ifs}, {});
ifs.close();
return parse(std::move(sdata));
}
void settings::unlock()
2024-01-02 03:45:06 +01:00
{
m_mutex.unlock();
2024-02-04 09:24:32 +01:00
m_externallyLockedCount--;
2024-01-02 03:45:06 +01:00
}