WIP: more config stuff

This commit is contained in:
Alex D. 2020-12-09 02:29:24 +00:00
parent 008afed09f
commit 85eaddd2ab
Signed by: caskd
GPG Key ID: F92BA85F61F4C173
3 changed files with 69 additions and 24 deletions

View File

@ -1,2 +1,7 @@
address = "41.157.98.109";
service = "9006";
user = {
nickname = "caskd-test";
username = "caskd-test";
}
channels = [ "#general", "#wow", "#test" ]

View File

@ -24,31 +24,52 @@ parse_configfile(char* config_path, Connection* conn)
{
config_t conf;
int res = -1;
config_setting_t* setting;
config_setting_t* root;
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[] = {
if ((root = config_lookup(&conf, ".")) != NULL) {
Mapping rootmaps[] = {
{ "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);
mapconf(root, rootmaps, sizeof(rootmaps) / sizeof(*rootmaps));
// Timeout is saved as a time_t (long)
int* tmp = NULL;
config_setting_lookup_int(root, "timeout", tmp);
if (tmp != NULL) 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 (int i = 0; i < config_setting_length(chans); i++) {
const char* chan = config_setting_get_string_elem(chans, i);
LOG(LOG_DEBUG, "Got channel #%i: %s", i, chan);
/* TODO: Allocate dynamic-sized channel array
if (chan != NULL) {
if (allocate_copy(&conn->data.channels[i]->name, chan)) {
LOG(LOG_DEBUG, "Saved %s to %p", chan, (const void*) conn->data.channels[i]);
} else {
LOG(LOG_WARN, "Failed to allocate memory to save channel #%i = %s", i, chan);
}
}
*/
}
}
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));
}
res = 1;
}
} else
@ -65,18 +86,31 @@ parse_configfile(char* config_path, Connection* conn)
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 },
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].var, defsalloc[i].def)) return 0;
if (!allocate_copy(defsalloc[i].save, defsalloc[i].str)) return 0;
}
conn->data.timeout = 20;
return 1;
}
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);
}
}
}
}

View File

@ -35,11 +35,17 @@
#define UIRCD_DEFAULT_REAL "uIRC user"
#define UIRCD_DEFAULT_QUITMSG "uIRCd " UIRCD_VERSION
typedef struct {
const char* str;
char** save;
} Mapping;
#ifdef UIRCD_FEATURE_LIBCONFIG
int parse_configfile(char* config_path, Connection* conn);
#endif /* UIRCD_FEATURE_LIBCONFIG */
int set_config_defaults(Connection* conn);
void mapconf(config_setting_t* setting, Mapping* mapping, unsigned long len);
int set_config_defaults(Connection* conn);
#endif /* UIRCD_GUARD_CONFIGURATION */