diff --git a/src/configfiles.c b/src/configfiles.c index 20215f2..94a7c78 100644 --- a/src/configfiles.c +++ b/src/configfiles.c @@ -10,13 +10,18 @@ #include "modules.h" int -wxmppd_processConfigfile(const char *file, int startup) +wxmppd_processConfigfile(const char *file, int startup, wxmppd_config_t **config) { xmlDocPtr doc; xmlXPathContextPtr xpathCtx; xmlXPathObjectPtr xpathObject; int i; + if (config) + { + *config = g_new0(wxmppd_config_t, 1); + } + if ((doc = xmlParseFile(file)) == NULL) { printf("Config file error!\n"); @@ -31,24 +36,60 @@ wxmppd_processConfigfile(const char *file, int startup) return WXMPPD_CONFIG_BADFILE; } - printf("%d nodes found.\n", xpathObject->nodesetval->nodeNr); - for (i = 0; i < xpathObject->nodesetval->nodeNr; i++) + if (xpathObject->nodesetval) { - xmlNodePtr textNode; - - if (xmlChildElementCount(xpathObject->nodesetval->nodeTab[i]) > 0) + for (i = 0; i < xpathObject->nodesetval->nodeNr; i++) { - printf("Config file error! modules/load elements cannot have children!\n"); - xmlXPathFreeContext(xpathCtx); + xmlNodePtr textNode; + + if (xmlChildElementCount(xpathObject->nodesetval->nodeTab[i]) > 0) + { + printf("Config file error! modules/load elements cannot have children!\n"); + xmlXPathFreeContext(xpathCtx); + xmlXPathFreeObject(xpathObject); + return WXMPPD_CONFIG_BADFILE; + } + + wxmppd_loadModule(xpathObject->nodesetval->nodeTab[i]->children->content, (config == NULL)); + } + } + + xmlXPathFreeObject(xpathObject); + + if ((xpathObject = xmlXPathEvalExpression("/wxmppd/modules/module-dir", xpathCtx)) == NULL) + { + printf("Config file error during xpath!\n"); + xmlXPathFreeContext(xpathCtx); + return WXMPPD_CONFIG_BADFILE; + } + + if (xpathObject->nodesetval) + { + if (xpathObject->nodesetval->nodeNr > 1) + { + printf("Config file cannot contain more than one module-dir definition!\n"); xmlXPathFreeObject(xpathObject); + xmlXPathFreeContext(xpathCtx); return WXMPPD_CONFIG_BADFILE; } + else if (xpathObject->nodesetval->nodeNr == 1) + { + if (xmlChildElementCount(xpathObject->nodesetval->nodeTab[0]) > 0) + { + printf("Config file error! modules/load elements cannot have children!\n"); + xmlXPathFreeContext(xpathCtx); + xmlXPathFreeObject(xpathObject); + return WXMPPD_CONFIG_BADFILE; + } - wxmppd_loadModule(xpathObject->nodesetval->nodeTab[i]->children->content); + if (config) + { + (*config)->modules_dir = g_strdup(xpathObject->nodesetval->nodeTab[0]->children->content); + } + } } xmlXPathFreeContext(xpathCtx); - xmlXPathFreeObject(xpathObject); return WXMPPD_CONFIG_SUCCESS; } diff --git a/src/configfiles.h b/src/configfiles.h index 28005f7..819faa8 100644 --- a/src/configfiles.h +++ b/src/configfiles.h @@ -6,7 +6,11 @@ enum { WXMPPD_CONFIG_BADFILE, }; -int wxmppd_processConfigfile(const char *file, int startup); +typedef struct _wxmppd_config_t { + char *modules_dir; +} wxmppd_config_t; + +int wxmppd_processConfigfile(const char *file, int startup, wxmppd_config_t **config); #endif /* __WXMPPD_CONFIGFILES_H */ diff --git a/src/modules.c b/src/modules.c index db1b35a..31c3876 100644 --- a/src/modules.c +++ b/src/modules.c @@ -1,9 +1,10 @@ #include +#include #include "modules.h" int -wxmppd_loadModule(char *moduleName) +wxmppd_loadModule(char *moduleName, gboolean dryRun) { printf("Will load module %s\n", moduleName); } diff --git a/src/modules.h b/src/modules.h index 464c894..5acb1d7 100644 --- a/src/modules.h +++ b/src/modules.h @@ -1 +1,8 @@ -int wxmppd_loadModule(char *moduleName); +#ifndef __WXMPPD_MODULES_H +# define __WXMPPD_MODULES_H + +# include + +int wxmppd_loadModule(char *moduleName, gboolean dryRun); + +#endif /* __WXMPPD_MODULES_H */ diff --git a/src/wxmppd.c b/src/wxmppd.c index 5db7a07..c3dd446 100644 --- a/src/wxmppd.c +++ b/src/wxmppd.c @@ -1,24 +1,67 @@ -#define CONFIG_DIR "/home/polesz/Projektek/wxmppd/data" - #include -#include -#include -#include +#include #include #include "configfiles.h" -char *config_dir = NULL; +gchar *config_dir = NULL; +gchar *cl_modules_dir = NULL; +gchar *cl_keys_file = NULL; + +static GOptionEntry optionEntries[] = { + { "config-dir", 'c', 0, G_OPTION_ARG_FILENAME, &config_dir, "Directory where configuration files can be found", "DIRECTORY" }, + { "module-dir", 'm', 0, G_OPTION_ARG_FILENAME, &cl_modules_dir, "Directory of the module files", "DIRECTORY" }, + { "keys-file", 'k', 0, G_OPTION_ARG_FILENAME, &cl_keys_file, "Path to key store file", "FILE" }, + { NULL }, +}; int main(int argc, char **argv) { + GError *err; + GOptionContext *optContext; + gchar *configFile; + wxmppd_config_t *configObject; + + optContext = g_option_context_new("- XMPP daemon"); + g_option_context_add_main_entries(optContext, optionEntries, NULL); + + if (!g_option_context_parse(optContext, &argc, &argv, &err)) + { + g_print("Option parsing failed (%s)!\n", err->message); + return 1; + } + + if (config_dir == NULL) + { + config_dir = g_strdup_printf("%s/wxmppd", SYSCONFDIR); + } + + configFile = g_strdup_printf("%s/wxmppd.xml", config_dir); + xmlInitParser(); LIBXML_TEST_VERSION; - printf("%s\n", SYSCONFDIR); + if (wxmppd_processConfigfile(configFile, 1, &configObject) != WXMPPD_CONFIG_SUCCESS) + { + return 1; + } - wxmppd_processConfigfile(CONFIG_DIR "/wxmppd.xml", 1); + if (cl_modules_dir != NULL) + { + if (configObject->modules_dir != NULL) + { + g_free(configObject->modules_dir); + } + configObject->modules_dir = g_strdup(cl_modules_dir); + } + + if (configObject->modules_dir == NULL) + { + configObject->modules_dir = g_strdup_printf("%s/wxmppd", LIBDIR); + } + + printf("%s\n", configObject->modules_dir); xmlCleanupParser();