83 lines
2.5 KiB
C
83 lines
2.5 KiB
C
/*
|
|
* This file is part of uIRCd. (https://git.redxen.eu/caskd/uIRCd)
|
|
* Copyright (c) 2019, 2020 Alex-David Denes
|
|
*
|
|
* uIRCd is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* any later version.
|
|
*
|
|
* uIRCd is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with uIRCd. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#include "configuration.h"
|
|
|
|
#ifdef UIRCD_FEATURE_LIBCONFIG
|
|
int
|
|
parse_configfile(char* config_path, Connection* conn)
|
|
{
|
|
config_t conf;
|
|
int res = -1;
|
|
config_setting_t* setting;
|
|
config_init(&conf);
|
|
if (config_read_file(&conf, config_path)) {
|
|
if ((setting = config_lookup(&conf, ".")) != NULL) {
|
|
// TODO: Add all options here
|
|
struct maps {
|
|
const char* str;
|
|
char** save;
|
|
} maps[] = {
|
|
{ "address", &conn->data.address },
|
|
{ "service", &conn->data.service },
|
|
{ "quitmsg", &conn->data.quitmsg },
|
|
{ "logdir", &conn->data.path },
|
|
};
|
|
for (unsigned long i = 0; i < sizeof(maps) / sizeof(*maps); i++) {
|
|
const char* var = NULL;
|
|
config_setting_lookup_string(setting, maps[i].str, &var);
|
|
if (var != NULL) {
|
|
if (allocate_copy(maps[i].save, var)) {
|
|
LOG(LOG_DEBUG, "Saved %s to %p", var, (const void*) maps[i].save);
|
|
} else {
|
|
LOG(LOG_WARN, "Failed to allocate memory to save config variable %s = %s", maps[i].str, var);
|
|
}
|
|
}
|
|
}
|
|
res = 1;
|
|
}
|
|
} else
|
|
LOG(LOG_WARN,
|
|
"Encountered a error while reading the config file. %s in %s line %d",
|
|
config_error_text(&conf),
|
|
config_error_file(&conf),
|
|
config_error_line(&conf));
|
|
config_destroy(&conf);
|
|
return res;
|
|
}
|
|
#endif
|
|
|
|
int
|
|
set_config_defaults(Connection* conn)
|
|
{
|
|
struct {
|
|
char** var;
|
|
char* def;
|
|
} defsalloc[] = {
|
|
{ &conn->data.address, UIRCD_DEFAULT_ADDR }, { &conn->data.service, UIRCD_DEFAULT_PORT },
|
|
{ &conn->data.quitmsg, UIRCD_DEFAULT_QUITMSG }, { &conn->user.nickname, UIRCD_DEFAULT_NICK },
|
|
{ &conn->user.username, UIRCD_DEFAULT_USER }, { &conn->user.realname, UIRCD_DEFAULT_REAL },
|
|
};
|
|
for (unsigned int i = 0; i < sizeof(defsalloc) / sizeof(*defsalloc); i++) {
|
|
if (!allocate_copy(defsalloc[i].var, defsalloc[i].def)) return 0;
|
|
}
|
|
conn->data.timeout = 20;
|
|
return 1;
|
|
}
|
|
|