From f9994aa3ae106e33033bb3efa75267513e1e8cae Mon Sep 17 00:00:00 2001 From: "Gergely Polonkai (W00d5t0ck)" Date: Mon, 26 Mar 2012 16:55:36 +0200 Subject: [PATCH] Moved configuration code to configuration.c Signed-off-by: Gergely Polonkai (W00d5t0ck) --- docs/reference/wmud/wmud-docs.sgml | 1 + docs/reference/wmud/wmud-sections.txt | 12 ++ src/Makefile.am | 2 +- src/configuration.c | 169 ++++++++++++++++++++++++++ src/configuration.h | 48 ++++++++ src/db.c | 12 +- src/db.h | 2 + src/game-networking.c | 3 +- src/main.c | 128 +------------------ src/main.h | 3 - src/wmud-types.h | 20 --- 11 files changed, 248 insertions(+), 152 deletions(-) create mode 100644 src/configuration.c create mode 100644 src/configuration.h diff --git a/docs/reference/wmud/wmud-docs.sgml b/docs/reference/wmud/wmud-docs.sgml index a8ff767..a9e57c2 100644 --- a/docs/reference/wmud/wmud-docs.sgml +++ b/docs/reference/wmud/wmud-docs.sgml @@ -18,6 +18,7 @@ wMUD hackers' guide + diff --git a/docs/reference/wmud/wmud-sections.txt b/docs/reference/wmud/wmud-sections.txt index 00a9ebb..e3c06c5 100644 --- a/docs/reference/wmud/wmud-sections.txt +++ b/docs/reference/wmud/wmud-sections.txt @@ -10,6 +10,16 @@ random_number wmud_random_string +
+ +configuration +WMUD_CONFIG_ERROR +wmudConfigError +ConfigData +active_config +wmud_config_init +
+
maintenance-thread @@ -27,6 +37,8 @@ game_context wmud_game_init
+
+ game-networking TELNET_IAC TELNET_WILL diff --git a/src/Makefile.am b/src/Makefile.am index f8553e0..b44e361 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,5 +1,5 @@ bin_PROGRAMS = wmud AM_CPPFLAGS = -DWMUD_STATEDIR=\""$(localstatedir)"\" -DWMUD_CONFDIR=\""$(sysconfdir)"\" $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS) -wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c game.c +wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c game.c configuration.c wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS) diff --git a/src/configuration.c b/src/configuration.c new file mode 100644 index 0000000..56ec439 --- /dev/null +++ b/src/configuration.c @@ -0,0 +1,169 @@ +/* wMUD - Yet another MUD codebase by W00d5t0ck + * Copyright (C) 2012 - Gergely POLONKAI + * + * configuration.c: configuration file related functions + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include + +#include "wmud-types.h" +#include "configuration.h" + +/** + * SECTION:configuration + * @short_description: Configuration file handling functions + * @title: Configuration file handling functions + * + */ + +/** + * WMUD_CONFIG_ERROR: + * + * the GQuark for the config error GError + */ +GQuark WMUD_CONFIG_ERROR = 0; +/** + * active_config: + * + * the currently active configuration directives + */ +ConfigData *active_config = NULL; + +/** + * wmud_configdata_free: + * @config_data: pointer to the ConfigData struct to free + * + * Correctly free a ConfigData struct with all its members + */ +void +wmud_configdata_free(ConfigData **config_data) +{ + if ((*config_data)->admin_email) + g_free((*config_data)->admin_email); + + if ((*config_data)->database_file) + g_free((*config_data)->database_file); + + g_free(*config_data); + *config_data = NULL; +} + +/** + * wmud_config_init: + * @config_data: a pointer to a ConfigData struct. This will be filled with the + * configuration file's data + * @err: The GError in which the config handling status should be returned + * + * Parses the default configuration file, and sets different variables + * according to it. + * + * Return value: %TRUE if parsing was successful. %FALSE otherwise. + */ +gboolean +wmud_config_init(ConfigData **config_data, GError **err) +{ + GString *config_file = g_string_new(WMUD_CONFDIR); + GKeyFile *config; + GError *in_err = NULL; + + if (!config_data) + return FALSE; + + if (!*config_data) + { + g_clear_error(err); + g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_REUSE, "Configuration pointer reuse. Please file a bug report!"); + return FALSE; + } + + *config_data = g_new0(ConfigData, 1); + + g_string_append(config_file, "/wmud.conf"); + + config = g_key_file_new(); + /* TODO: Error checking */ + g_key_file_load_from_file(config, config_file->str, 0, &in_err); + + if (!g_key_file_has_group(config, "global")) + { + g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOGLOBAL, "Config file (%s) does not contain a [global] group", config_file->str); + g_key_file_free(config); + g_string_free(config_file, TRUE); + + return FALSE; + } + + g_clear_error(&in_err); + (*config_data)->port = g_key_file_get_integer(config, "global", "port", &in_err); + if (in_err) + { + if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) + { + (*config_data)->port = DEFAULT_PORT; + } + else if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE)) + { + g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_BADPORT, "Config file (%s) contains an invalid port number", config_file->str); + g_key_file_free(config); + g_string_free(config_file, TRUE); + (*config_data)->port = 0; + + return FALSE; + } + + return FALSE; + } + + g_clear_error(&in_err); + (*config_data)->database_file = g_key_file_get_string(config, "global", "world file", &in_err); + if (in_err) + { + if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) + { + g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOWORLD, "Config file (%s) does not contain a world file path", config_file->str); + g_key_file_free(config); + g_string_free(config_file, TRUE); + wmud_configdata_free(config_data); + + return FALSE; + } + } + + g_clear_error(&in_err); + (*config_data)->admin_email = g_key_file_get_string(config, "global", "admin email", &in_err); + if (in_err) + { + if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) + { + g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOEMAIL, "Config file (%s) does not contain an admin e-mail address", config_file->str); + g_key_file_free(config); + g_string_free(config_file, TRUE); + wmud_configdata_free(config_data); + + return FALSE; + } + } + + g_key_file_free(config); + g_string_free(config_file, TRUE); + + return TRUE; +} + diff --git a/src/configuration.h b/src/configuration.h new file mode 100644 index 0000000..e0329f9 --- /dev/null +++ b/src/configuration.h @@ -0,0 +1,48 @@ +#ifndef __WMUD_CONFIGURATION_H__ +#define __WMUD_CONFIGURATION_H__ + +#include + +extern GQuark WMUD_CONFIG_ERROR; + +/** + * wmudConfigError: + * @WMUD_CONFIG_ERROR_NOGLOBAL: Indicates that the config file read doesn't + * contain a [global] section + * @WMUD_CONFIG_ERROR_BADPORT: Indicates that the config file contains and + * invalid port number + * @WMUD_CONFIG_ERROR_NOWORLD: Indicates that the config file doesn't contain a + * world database file + * @WMUD_CONFIG_ERROR_NOEMAIL: Indicates that the config file doesn't contain + * an administrator e-mail address + * @WMUD_CONFIG_ERROR_REUSE: configuration data is reused (non-NULL) + * + * Error codes returned by configuration file parsing functions. + */ +typedef enum { + WMUD_CONFIG_ERROR_NOGLOBAL, + WMUD_CONFIG_ERROR_BADPORT, + WMUD_CONFIG_ERROR_NOWORLD, + WMUD_CONFIG_ERROR_NOEMAIL, + WMUD_CONFIG_ERROR_REUSE +} wmudConfigError; + +/** + * ConfigData: + * @port: the port number of the game interface to listen on + * @database_file: the database file of the world associated with this + * configuration + * @admin_email: the world administrator's e-mail address + */ +typedef struct _ConfigData { + guint port; + gchar *database_file; + gchar *admin_email; +} ConfigData; + +extern ConfigData *active_config; + +gboolean wmud_config_init(ConfigData **config_data, GError **err); + +#endif /* __WMUD_CONFIGURATION_H__ */ + diff --git a/src/db.c b/src/db.c index 0fd3614..50eca92 100644 --- a/src/db.c +++ b/src/db.c @@ -16,12 +16,14 @@ * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ + #include #include #include "main.h" #include "db.h" #include "players.h" +#include "configuration.h" /** * SECTION:db @@ -30,7 +32,13 @@ * */ -sqlite3 *dbh = NULL; +/** + * WMUD_DB_ERROR: + * + * the GQuark for the database error GError + */ +GQuark WMUD_DB_ERROR = 0; +static sqlite3 *dbh = NULL; /** * wmud_db_init: @@ -44,7 +52,7 @@ wmud_db_init(GError **err) GString *db_file = g_string_new(WMUD_STATEDIR); int sqlite_code; - g_string_append_printf(db_file, "/%s", database_file); + g_string_append_printf(db_file, "/%s", active_config->database_file); if ((sqlite_code = sqlite3_open(db_file->str, &dbh)) != SQLITE_OK) { diff --git a/src/db.h b/src/db.h index d6c5ad4..fb98002 100644 --- a/src/db.h +++ b/src/db.h @@ -23,6 +23,8 @@ #include "wmud-types.h" +extern GQuark WMUD_DB_ERROR; + gboolean wmud_db_init(GError **err); gboolean wmud_db_players_load(GError **err); gboolean wmud_db_save_player(wmudPlayer *player, GError **err); diff --git a/src/game-networking.c b/src/game-networking.c index c2cc900..6d6c4f3 100644 --- a/src/game-networking.c +++ b/src/game-networking.c @@ -30,6 +30,7 @@ #include "interpreter.h" #include "players.h" #include "db.h" +#include "configuration.h" /** * SECTION:game-networking @@ -247,7 +248,7 @@ wmud_client_callback(GSocket *client_socket, GIOCondition condition, wmudClient } else { - wmud_client_send(client, "\r\nSorry, but this e-mail address doesn't seem to be valid to me.\r\n\r\nIf you think this is a valid address, simply press enter to quit, and send an e-mail to %s from that address, so we can fix our e-mail validation code.\r\n\r\nIf you just mistyped your address, type it now: ", admin_email); + wmud_client_send(client, "\r\nSorry, but this e-mail address doesn't seem to be valid to me.\r\n\r\nIf you think this is a valid address, simply press enter to quit, and send an e-mail to %s from that address, so we can fix our e-mail validation code.\r\n\r\nIf you just mistyped your address, type it now: ", active_config->admin_email); if (*(client->buffer->str)) client->bademail = TRUE; } diff --git a/src/main.c b/src/main.c index a66542a..5875320 100644 --- a/src/main.c +++ b/src/main.c @@ -34,6 +34,7 @@ #include "players.h" #include "maintenance.h" #include "game.h" +#include "configuration.h" /** * SECTION:utils @@ -58,38 +59,6 @@ struct { * the main random generator */ GRand *main_rand = NULL; -/** - * WMUD_CONFIG_ERROR: - * - * the GQuark for the config error GError - */ -GQuark WMUD_CONFIG_ERROR = 0; -/** - * WMUD_DB_ERROR: - * - * the GQuark for the database error GError - */ -GQuark WMUD_DB_ERROR = 0; -/** - * port: - * - * the port number to listen on - */ -guint port = 0; -/** - * database_file: - * - * the filename of the world database - */ -gchar *database_file = NULL; -/** - * admin_email: - * - * e-mail address of the MUD's administrator - */ -gchar *admin_email = NULL; - -/** * wmud_random_string: * @len: the desired length of the generated random string * @@ -159,94 +128,6 @@ wmud_type_init(void) WMUD_DB_ERROR = g_quark_from_string("wmud_db_error"); } -/** - * wmud_config_init: - * @err: The GError in which the config handling status should be returned - * - * Parses the default configuration file, and sets different variables - * according to it. - * - * Return value: %TRUE if parsing was successful. %FALSE otherwise. - */ -gboolean -wmud_config_init(GError **err) -{ - GString *config_file = g_string_new(WMUD_CONFDIR); - GKeyFile *config; - GError *in_err = NULL; - - g_string_append(config_file, "/wmud.conf"); - - config = g_key_file_new(); - /* TODO: Error checking */ - g_key_file_load_from_file(config, config_file->str, 0, &in_err); - - if (!g_key_file_has_group(config, "global")) - { - g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOGLOBAL, "Config file (%s) does not contain a [global] group", config_file->str); - g_key_file_free(config); - g_string_free(config_file, TRUE); - - return FALSE; - } - - g_clear_error(&in_err); - port = g_key_file_get_integer(config, "global", "port", &in_err); - if (in_err) - { - if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) - { - port = DEFAULT_PORT; - } - else if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_INVALID_VALUE)) - { - g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_BADPORT, "Config file (%s) contains an invalid port number", config_file->str); - g_key_file_free(config); - g_string_free(config_file, TRUE); - port = 0; - - return FALSE; - } - - return FALSE; - } - - g_clear_error(&in_err); - database_file = g_key_file_get_string(config, "global", "world file", &in_err); - if (in_err) - { - if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) - { - g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOWORLD, "Config file (%s) does not contain a world file path", config_file->str); - g_key_file_free(config); - g_string_free(config_file, TRUE); - database_file = NULL; - - return FALSE; - } - } - - g_clear_error(&in_err); - admin_email = g_key_file_get_string(config, "global", "admin email", &in_err); - if (in_err) - { - if (g_error_matches(in_err, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_KEY_NOT_FOUND)) - { - g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOEMAIL, "Config file (%s) does not contain an admin e-mail address", config_file->str); - g_key_file_free(config); - g_string_free(config_file, TRUE); - admin_email = NULL; - - return FALSE; - } - } - - g_key_file_free(config); - g_string_free(config_file, TRUE); - - return TRUE; -} - /** * main: * @argc: The number of arguments on the command line @@ -273,7 +154,7 @@ main(int argc, char **argv) /* TODO: Create signal handlers! */ - if (!wmud_config_init(&err)) + if (!wmud_config_init(&active_config, &err)) { if (err) { @@ -287,9 +168,6 @@ main(int argc, char **argv) return 1; } - g_assert(port != 0); - g_assert(database_file != NULL); - g_clear_error(&err); if (!wmud_db_init(&err)) { @@ -313,7 +191,7 @@ main(int argc, char **argv) wmud_game_init(&game_thread, &game_context); g_clear_error(&err); - if (!wmud_networking_init(port, game_context, &err)) + if (!wmud_networking_init(active_config->port, game_context, &err)) { if (err) { diff --git a/src/main.h b/src/main.h index 2a25ed5..08ddf8b 100644 --- a/src/main.h +++ b/src/main.h @@ -6,9 +6,6 @@ extern GMainContext *game_context; extern guint32 elapsed_seconds; extern GRand *main_rand; -extern gchar *database_file; -extern GQuark WMUD_DB_ERROR; -extern gchar *admin_email; /** * random_number: diff --git a/src/wmud-types.h b/src/wmud-types.h index 9b9460f..ed6a93b 100644 --- a/src/wmud-types.h +++ b/src/wmud-types.h @@ -109,26 +109,6 @@ typedef struct _wmudClient { gint login_try_count; } wmudClient; -/** - * wmudConfigError: - * @WMUD_CONFIG_ERROR_NOGLOBAL: Indicates that the config file read doesn't - * contain a [global] section - * @WMUD_CONFIG_ERROR_BADPORT: Indicates that the config file contains and - * invalid port number - * @WMUD_CONFIG_ERROR_NOWORLD: Indicates that the config file doesn't contain a - * world database file - * @WMUD_CONFIG_ERROR_NOEMAIL: Indicates that the config file doesn't contain - * an administrator e-mail address - * - * Error codes returned by configuration file parsing functions. - */ -typedef enum { - WMUD_CONFIG_ERROR_NOGLOBAL, - WMUD_CONFIG_ERROR_BADPORT, - WMUD_CONFIG_ERROR_NOWORLD, - WMUD_CONFIG_ERROR_NOEMAIL -} wmudConfigError; - /** * wmudDbError: * @WMUD_DB_ERROR_CANTOPEN: Database file cannot be opened