From a0d74522f6c2dba5eaeb1a441de03a6f77800aaa Mon Sep 17 00:00:00 2001 From: Alexander Sulfrian Date: Wed, 7 Dec 2011 18:34:41 +0100 Subject: read config from ini file --- Makefile | 2 +- ts3db.c | 82 +++++++++++++++++++++++++++++++--------------------------------- 2 files changed, 41 insertions(+), 43 deletions(-) diff --git a/Makefile b/Makefile index 5309c21..8272d2c 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ objects:=$(sources:.c=.o) #32BIT=-m32 CFLAGS=-fPIC $(32BIT) -g -std=c99 CFLAGS+=-I$(shell pg_config --includedir) -LDFLAGS=-shared -Wl,-soname,$(TARGET) $(32BIT) -lpq +LDFLAGS=-shared -Wl,-soname,$(TARGET) $(32BIT) -lpq -liniparser ############################################################################## diff --git a/ts3db.c b/ts3db.c index 30799c4..70eb152 100644 --- a/ts3db.c +++ b/ts3db.c @@ -2,6 +2,7 @@ #include #include #include +#include #define MAX_CONNECTIONS 99 @@ -14,27 +15,13 @@ static log_callback log = 0; static pg_connection connections[MAX_CONNECTIONS]; static unsigned int connection_count = 0; -static const char *keywords[] = { "host", "hostaddr", "port", "dbname", - "user", "password", "sslmode", "sslcert", - "sslkey", "sslrootcert", "sslcrl", - "application_name", 0}; - -typedef struct { - char *host; - char *hostaddr; - char *port; - char *dbname; - char *user; - char *password; - char *sslmode; - char *sslcert; - char *sslkey; - char *sslrootcert; - char *sslcrl; - char *application_name; -} pg_settings; - -static pg_settings settings; +static dictionary *config = NULL; +static const char *config_keywords[13] = { "host", "hostaddr", "port", + "dbname", "user", "password", + "sslmode", "sslcert", "sslkey", + "sslrootcert", "sslcrl", + "application_name", NULL}; +static const char *config_values[13] = { NULL }; char *ts3dbplugin_version() { @@ -48,14 +35,6 @@ char *ts3dbplugin_name() int ts3dbplugin_disconnect() { - log("disconnect", LOG_DEVELOP); - ts3dbplugin_shutdown(); - return 0; -} - -void ts3dbplugin_shutdown() -{ - log("shutdown", LOG_DEVELOP); unsigned int i; for (i = 0; i < connection_count; i++) { PQclear(connections[i].result); @@ -63,24 +42,43 @@ void ts3dbplugin_shutdown() } connection_count = 0; + return 0; +} + +void ts3dbplugin_shutdown() +{ + ts3dbplugin_disconnect(); + + if (config != NULL) { + iniparser_freedict(config); + config = NULL; + } } int ts3dbplugin_init(log_callback logging, char *parameter) { log = logging; - settings.host = "localhost"; - settings.hostaddr = ""; - settings.port = ""; - settings.dbname = "ts3"; - settings.user = "ts3"; - settings.password = ""; - settings.sslmode = NULL; - settings.sslcert = NULL; - settings.sslkey = NULL; - settings.sslrootcert = NULL; - settings.sslcrl = NULL; - settings.application_name = "teamspeak3"; + if (parameter != NULL && strlen(parameter) > 0) { + config = iniparser_load(parameter); + } + else { + config = iniparser_load("ts3db_postgres.ini"); + } + + config_values[0] = iniparser_getstring(config, "config:host", "localhost"); + config_values[1] = iniparser_getstring(config, "config:hostaddr", "");; + config_values[2] = iniparser_getstring(config, "config:port", ""); + config_values[3] = iniparser_getstring(config, "config:dbname", "ts3"); + config_values[4] = iniparser_getstring(config, "config:user", "ts3"); + config_values[5] = iniparser_getstring(config, "config:password", ""); + config_values[6] = iniparser_getstring(config, "config:sslmode", NULL); + config_values[7] = iniparser_getstring(config, "config:sslcert", NULL); + config_values[8] = iniparser_getstring(config, "config:sslkey", NULL); + config_values[9] = iniparser_getstring(config, "config:sslrootcert", NULL); + config_values[10] = iniparser_getstring(config, "config:sslcrt", NULL); + config_values[11] = "teamspeak3"; + config_values[12] = NULL; return 0; } @@ -92,7 +90,7 @@ int ts3dbplugin_connect(unsigned int *connection_nr) return 0; } - conn = PQconnectdbParams(keywords, (const char **)&settings, true); + conn = PQconnectdbParams(config_keywords, config_values, true); if (PQstatus(conn) != CONNECTION_OK) { log(PQerrorMessage(conn), LOG_CRITICAL); return 1280; -- cgit v1.2.3