summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--config_load.c150
-rw-r--r--config_load.h40
2 files changed, 190 insertions, 0 deletions
diff --git a/config_load.c b/config_load.c
new file mode 100644
index 0000000..df29cb7
--- /dev/null
+++ b/config_load.c
@@ -0,0 +1,150 @@
+#include "config_load.h"
+
+int config_load_ini(char *ini_fname, config_ini **config)
+{
+ int i;
+ config_ini *res_conf=NULL;
+
+ if (!ini_fname)
+ {
+ PERM();
+ return -1;
+ }
+
+ if (!config)
+ {
+ PERM();
+ return -1;
+ }
+
+ res_conf = calloc(1,sizeof(config_ini));
+
+ res_conf->ini = iniparser_load(ini_fname);
+ if (!res_conf->ini)
+ {
+ ERROR("Cant parse ini file %s\n",ini_fname);
+ free(res_conf);
+ return -1;
+ }
+
+ *config = res_conf;
+
+ return 0;
+}
+
+
+int config_server_num(config_ini *config)
+{
+ int ret=-1;
+ if (!config)
+ {
+ PERM();
+ return -1;
+ }
+
+ ret = iniparser_getnsec(config->ini);
+
+ return ret;
+}
+
+
+int config_get_irc_config(config_ini *config, int n, irc_server_conf *server_conf)
+{
+
+ int i;
+ size_t buf_sz=128;
+ char buf[buf_sz];
+ char *section_name=NULL;
+ dictionary *ini;
+ char *ini_str;
+ int ini_int;
+ int count;
+ sds params;
+ sds *tokens=NULL;
+
+ if (!config)
+ {
+ PERM();
+ return -1;
+ }
+
+ if (!server_conf)
+ {
+ PERM();
+ return -1;
+ }
+
+ if (n<0)
+ {
+ PERM();
+ return -1;
+ }
+
+ ini = config->ini;
+ section_name = (char *)iniparser_getsecname(ini, n);
+ if (!section_name)
+ {
+ PERM();
+ return -1;
+ }
+
+ //section name
+ server_conf->name = (char *)alloc_new_str(section_name);
+
+ //get server name
+ snprintf(buf, buf_sz, "%s:server", section_name);
+ ini_str = (char *)iniparser_getstring(ini, buf, NULL);
+ server_conf->server = alloc_new_str(ini_str);
+
+ //get user name
+ snprintf(buf, buf_sz, "%s:user", section_name);
+ ini_str = (char *)iniparser_getstring(ini, buf, NULL);
+ server_conf->user = alloc_new_str(ini_str);
+
+ //get port number
+ snprintf(buf, buf_sz, "%s:port", section_name);
+ ini_str = (char *)iniparser_getstring(ini, buf, NULL);
+ server_conf->port = (char *)alloc_new_str(ini_str);
+
+ //get enabled
+ snprintf(buf, buf_sz, "%s:enabled", section_name);
+ ini_int = (int)iniparser_getint(ini, buf, -1);
+ server_conf->enabled = ini_int;
+
+ //get ssl
+ snprintf(buf, buf_sz, "%s:ssl", section_name);
+ ini_int = (int)iniparser_getint(ini, buf, -1);
+ server_conf->ssl = ini_int;
+
+ //get channel list
+ snprintf(buf, buf_sz, "%s:channels", section_name);
+ ini_str = (char *)iniparser_getstring(ini, buf, NULL);
+ params = sdsnew(ini_str);
+ tokens = sdssplitlen(params,sdslen(params),",",1,&count);
+ for (i=0;(i<count)&&(i<15);i++)//woop woop dirty hack, max chan nums is 16 not constanteniuated
+ {
+ server_conf->channels[i] = alloc_new_str(tokens[i]);
+ }
+ server_conf->channels[i]=NULL;
+ sdsfree(params);
+ sdsfreesplitres(tokens,count);
+
+ return 0;
+}
+
+
+void config_free(config_ini *config)
+{
+ if (!config)
+ {
+ PERM();
+ return;
+ }
+
+ iniparser_freedict(config->ini);
+ config->ini = NULL;
+
+ free(config);
+ config = NULL;
+
+} \ No newline at end of file
diff --git a/config_load.h b/config_load.h
new file mode 100644
index 0000000..8d32ed7
--- /dev/null
+++ b/config_load.h
@@ -0,0 +1,40 @@
+#ifndef __CONFIG_LOAD_H
+#define __CONFIG_LOAD_H
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+
+#include "arg.h"
+#include "iniparser.h"
+#include "debug.h"
+
+#include "config_servers.h"
+#include "sds.h"
+#include "util.h"
+
+typedef struct config_ini {
+ dictionary *ini; //dict with configs
+} config_ini;
+
+/*
+Load config file to dict
+config should be NULL
+*/
+int config_load_ini(char *ini_fname, config_ini **config);
+/*
+How many servers there are to be configured,count
+*/
+int config_server_num(config_ini *config);
+/*
+server_conf should be preallocated
+*/
+int config_get_irc_config(config_ini *config, int n, irc_server_conf *server_conf);
+/*
+free internal structures
+*/
+void config_free(config_ini *config);
+
+
+#endif \ No newline at end of file