2023-11-22 19:59:34 -08:00
|
|
|
#include "settings.hpp"
|
|
|
|
|
2025-02-02 16:20:52 -08:00
|
|
|
#include <myirc/openssl_utils.hpp>
|
|
|
|
|
2023-11-22 19:59:34 -08:00
|
|
|
#define TOML_ENABLE_FORMATTERS 0
|
|
|
|
#include <toml++/toml.hpp>
|
|
|
|
|
2025-01-25 15:45:31 -08:00
|
|
|
auto Settings::from_stream(std::istream &in) -> Settings
|
2023-11-22 19:59:34 -08:00
|
|
|
{
|
2025-01-25 15:45:31 -08:00
|
|
|
const auto config = toml::parse(in);
|
2023-11-22 19:59:34 -08:00
|
|
|
return Settings{
|
2025-01-25 15:45:31 -08:00
|
|
|
.host = config["host"].value_or(std::string{}),
|
2025-01-26 14:38:13 -08:00
|
|
|
.service = config["port"].value_or(std::uint16_t{6667}),
|
2025-01-25 12:25:38 -08:00
|
|
|
.password = config["password"].value_or(std::string{}),
|
|
|
|
.username = config["username"].value_or(std::string{}),
|
|
|
|
.realname = config["realname"].value_or(std::string{}),
|
|
|
|
.nickname = config["nickname"].value_or(std::string{}),
|
|
|
|
.sasl_mechanism = config["sasl_mechanism"].value_or(std::string{}),
|
|
|
|
.sasl_authcid = config["sasl_authcid"].value_or(std::string{}),
|
|
|
|
.sasl_authzid = config["sasl_authzid"].value_or(std::string{}),
|
2025-01-26 14:38:13 -08:00
|
|
|
.sasl_password = config["sasl_password"].value_or(std::string{}),
|
2025-01-30 16:39:23 -08:00
|
|
|
.sasl_key_file = config["sasl_key_file"].value_or(std::string{}),
|
|
|
|
.sasl_key_password = config["sasl_key_password"].value_or(std::string{}),
|
2025-01-26 14:38:13 -08:00
|
|
|
.tls_hostname = config["tls_hostname"].value_or(std::string{}),
|
2025-01-31 08:38:14 -08:00
|
|
|
.tls_cert_file = config["tls_cert_file"].value_or(std::string{}),
|
|
|
|
.tls_key_file = config["tls_key_file"].value_or(std::string{}),
|
|
|
|
.tls_key_password = config["tls_key_password"].value_or(std::string{}),
|
|
|
|
.challenge_username = config["challenge_username"].value_or(std::string{}),
|
|
|
|
.challenge_key_file = config["challenge_key_file"].value_or(std::string{}),
|
|
|
|
.challenge_key_password = config["challenge_key_password"].value_or(std::string{}),
|
2025-01-26 14:38:13 -08:00
|
|
|
.use_tls = config["use_tls"].value_or(false),
|
2023-11-22 19:59:34 -08:00
|
|
|
};
|
|
|
|
}
|
2025-02-02 16:20:52 -08:00
|
|
|
|
|
|
|
auto configure_sasl(const Settings &settings) -> std::unique_ptr<myirc::SaslMechanism>
|
|
|
|
{
|
|
|
|
if (settings.sasl_mechanism == "PLAIN" &&
|
|
|
|
not settings.sasl_authcid.empty()
|
|
|
|
) {
|
|
|
|
return std::make_unique<myirc::SaslPlain>(
|
|
|
|
settings.sasl_authcid,
|
|
|
|
settings.sasl_authzid,
|
|
|
|
settings.sasl_password);
|
|
|
|
|
|
|
|
} else if (settings.sasl_mechanism == "EXTERNAL") {
|
|
|
|
return std::make_unique<myirc::SaslExternal>(settings.sasl_authzid);
|
|
|
|
|
|
|
|
} else if (
|
|
|
|
settings.sasl_mechanism == "ECDSA" &&
|
|
|
|
not settings.sasl_authcid.empty() &&
|
|
|
|
not settings.sasl_key_file.empty()
|
|
|
|
) {
|
|
|
|
if (auto sasl_key = myirc::key_from_file(settings.sasl_key_file, settings.sasl_key_password))
|
|
|
|
return std::make_unique<myirc::SaslEcdsa>(
|
|
|
|
settings.sasl_authcid,
|
|
|
|
settings.sasl_authzid,
|
|
|
|
std::move(sasl_key));
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|