This repository has been archived on 2021-02-08. You can view files and clone it, but cannot push or open issues or pull requests.
uIRCd/src/configuration.c

117 lines
4.0 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"
#include "channels.h"
#include "connection.h"
#include "logging.h"
#include "memory.h"
#ifdef UIRCD_FEATURE_LIBCONFIG
#include <libconfig.h> // config_t config_setting_t config_init() ...
int
parse_configfile(char* config_path, Connection* conn)
{
config_t conf;
int res = -1;
config_setting_t* root;
config_init(&conf);
if (config_read_file(&conf, config_path)) {
if ((root = config_lookup(&conf, ".")) != NULL) {
Mapping rootmaps[] = {
{ "address", &conn->data.address },
{ "service", &conn->data.service },
{ "quitmsg", &conn->data.quitmsg },
{ "logdir", &conn->data.path },
};
mapconf(root, rootmaps, sizeof(rootmaps) / sizeof(*rootmaps));
// Timeout is saved as a time_t (long) and may not be negative, 0 is default (unset)
signed int tmp = -1;
config_setting_lookup_int(root, "timeout", &tmp);
if (tmp > 0) conn->data.timeout = tmp;
// Channels are a array
config_setting_t* chans;
if ((chans = config_lookup(&conf, ".channels")) != NULL) {
// TODO: Check if config_setting_length can return negative values
for (unsigned int i = 0; i < (unsigned int) config_setting_length(chans); i++) {
config_setting_t* chanelem = config_setting_get_elem(chans, i);
const char * name = NULL, *key = NULL;
config_setting_lookup_string(chanelem, "name", &name);
config_setting_lookup_string(chanelem, "key", &key);
LOG(LOG_DEBUG, "Got channel #%i: %s", i, name);
resize_chanarray(&conn->info.channels);
if (name != NULL) set_channel(&conn->info.channels[get_channelindex(name, conn->info.channels)], name, key, true);
}
}
config_setting_t* user;
if ((user = config_lookup(&conf, ".user")) != NULL) {
Mapping usermaps[] = {
{ "nickname", &conn->user.nickname },
{ "username", &conn->user.username },
{ "realname", &conn->user.realname },
{ "password", &conn->user.password },
};
mapconf(user, usermaps, sizeof(usermaps) / sizeof(*usermaps));
}
LOG(LOG_DEBUG, "%s.", "Parsed configuration file successfully");
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;
}
void
mapconf(config_setting_t* setting, Mapping* mapping, unsigned long len)
{
for (unsigned long i = 0; i < len; i++) {
const char* var = NULL;
config_setting_lookup_string(setting, mapping[i].str, &var);
if (var != NULL) {
if (allocate_copy(mapping[i].save, var)) {
LOG(LOG_DEBUG, "Saved %s to %p", var, (const void*) mapping[i].save);
} else {
LOG(LOG_WARN, "Failed to allocate memory to save config variable %s = %s", mapping[i].str, var);
}
}
}
}
#endif /* UIRCD_FEATURE_LIBCONFIG */
int
set_config_defaults(Connection* conn)
{
Mapping defsalloc[] = {
{ UIRCD_DEFAULT_ADDR, &conn->data.address }, { UIRCD_DEFAULT_PORT, &conn->data.service }, { UIRCD_DEFAULT_QUITMSG, &conn->data.quitmsg },
{ UIRCD_DEFAULT_NICK, &conn->user.nickname }, { UIRCD_DEFAULT_USER, &conn->user.username }, { UIRCD_DEFAULT_REAL, &conn->user.realname },
};
for (unsigned int i = 0; i < sizeof(defsalloc) / sizeof(*defsalloc); i++) {
if (!allocate_copy(defsalloc[i].save, defsalloc[i].str)) return 0;
}
conn->data.timeout = 0;
return 1;
}