diff options
Diffstat (limited to 'src/daemon/settings.c')
-rw-r--r-- | src/daemon/settings.c | 168 |
1 files changed, 163 insertions, 5 deletions
diff --git a/src/daemon/settings.c b/src/daemon/settings.c index bac1c3b..5314fef 100644 --- a/src/daemon/settings.c +++ b/src/daemon/settings.c @@ -19,6 +19,16 @@ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ +#define _GNU_SOURCE + +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> +#include <fcntl.h> +#include <stdlib.h> +#include <string.h> +#include <stdio.h> +#include <errno.h> #include "settings.h" #include "settings_private.h" @@ -30,7 +40,16 @@ void settings_init(void) { unset_flag(flag_daemonize); settings.port = 6996; - settings.verbosity = USURP_VERBOSITY; + settings.verboselevel = USURP_VERBOSITY; + settings.progname = strdup(program_invocation_short_name); + settings.im_user = strdup("user"); + settings.im_password = strdup("password"); + settings.im_proto = strdup("prpl-irc"); +#define macro2str(a) _macro2str(a) +#define _macro2str(a) #a + setting_readconf("/" macro2str(SYSCONFDIR) "/usurpation.conf"); +#undef macro2str +#undef _macro2str } int setting_detach(void) @@ -40,7 +59,7 @@ int setting_detach(void) int setting_verbose(void) { - return settings.verbosity; + return settings.verboselevel; } unsigned short int setting_port(void) @@ -48,8 +67,28 @@ unsigned short int setting_port(void) return settings.port; } +char *setting_progname(void) +{ + return strdup(settings.progname); +} + +char *setting_im_user(void) +{ + return strdup(settings.im_user); +} + +char *setting_im_password(void) +{ + return strdup(settings.im_password); +} + +char *setting_im_proto(void) +{ + return strdup(settings.im_proto); +} + /* could be a one-liner, but let's make the logic more obvious */ -int test_flag(unsigned int flag) +static int test_flag(unsigned int flag) { int ret; @@ -59,12 +98,131 @@ int test_flag(unsigned int flag) return ret; } -void set_flag(unsigned int flag) +static void set_flag(unsigned int flag) { settings.flags |= flag; } -void unset_flag(unsigned int flag) +static void unset_flag(unsigned int flag) { settings.flags &= ~flag; } + +/* TODO: semi-stub! */ +static int setting_readconf(const char * const path) +{ + struct stat filestat; + char *buf; + char *line; + char *entry; + char *value; + char *line_state; + char *token_state; + int fd; + + + if ((fd = open(path, O_RDONLY)) == -1) { + if (USURP_VERBOSITY >= 2) { + perror("error while opening the configuration file"); + return 1; + } + } + + fstat(fd, &filestat); + buf = malloc(filestat.st_size+1); + buf[filestat.st_size] = 0; + if (read(fd, buf, filestat.st_size) != filestat.st_size) { + if (USURP_VERBOSITY >= 2) { + perror("partial read of configuration file detected"); + free(buf); + return 1; + } + } + + line = strtok_r(buf, "\n", &line_state); + + while (line) { + if (line[0] != '#') { + entry = strtok_r(line, "=", &token_state); + value = strtok_r(NULL, "=", &token_state); + setting_handle_config_entry(entry, value); + } + + line = strtok_r(NULL, "\n", &line_state); + } + + free(buf); + + return 0; +} + +static int setting_handle_config_entry(const char * const entry, const char * const value) +{ + size_t i; + + for (i = 0; i < sizeof(ent_table)/sizeof(*ent_table); ++i) { + if (!(strcmp(entry, ent_table[i].name))) { + ent_table[i].set(value); + return 0; + } + } + + return 1; +} + +static void set_daemonize(const void * const arg) +{ + const char * const a = arg; + + atoi(a) ? set_flag(flag_daemonize) : unset_flag(flag_daemonize); +} + +static void set_port(const void * const arg) +{ + const char * const a = arg; + + settings.port = strtoul(a, NULL, 0); +} + +static void set_verbosity(const void * const arg) +{ + const char * const a = arg; + + settings.verboselevel = strtol(a, NULL, 0); +} + +static void set_progname(const void * const arg) +{ + const char * const a = arg; + free(settings.progname); + settings.progname = strdup(a); +} + +static void set_im_user(const void * const arg) +{ + const char * const a = arg; + free(settings.im_user); + settings.im_user = strdup(a); +} + +static void set_im_password(const void * const arg) +{ + const char * const a = arg; + free(settings.im_password); + settings.im_password = strdup(a); +} + +static void set_im_proto(const void * const arg) +{ + const char * const a = arg; + free(settings.im_proto); + settings.im_proto = strdup(a); +} + +void settings_cleanup(void) +{ + free(settings.progname); + free(settings.im_user); + free(settings.im_password); + free(settings.im_proto); +} |