Moved configuration code to configuration.c

Signed-off-by: Gergely Polonkai (W00d5t0ck) <polesz@w00d5t0ck.info>
This commit is contained in:
Gergely Polonkai (W00d5t0ck) 2012-03-26 16:55:36 +02:00
parent e56e4a2f8f
commit f9994aa3ae
11 changed files with 248 additions and 152 deletions

View File

@ -18,6 +18,7 @@
<title>wMUD hackers' guide</title>
<xi:include href="xml/types.xml"/>
<xi:include href="xml/utils.xml"/>
<xi:include href="xml/configuration.xml"/>
<xi:include href="xml/maintenance-thread.xml"/>
<xi:include href="xml/game-thread.xml"/>
<xi:include href="xml/game-networking.xml"/>

View File

@ -10,6 +10,16 @@ random_number
wmud_random_string
</SECTION>
<SECTION>
<TITLE></TITLE>
<FILE>configuration</FILE>
WMUD_CONFIG_ERROR
wmudConfigError
ConfigData
active_config
wmud_config_init
</SECTION>
<SECTION>
<TITLE></TITLE>
<FILE>maintenance-thread</FILE>
@ -27,6 +37,8 @@ game_context
wmud_game_init
</SECTION>
<SECTION>
<TITLE></TITLE>
<FILE>game-networking</FILE>
TELNET_IAC
TELNET_WILL

View File

@ -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)

169
src/configuration.c Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <glib.h>
#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;
}

48
src/configuration.h Normal file
View File

@ -0,0 +1,48 @@
#ifndef __WMUD_CONFIGURATION_H__
#define __WMUD_CONFIGURATION_H__
#include <glib.h>
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__ */

View File

@ -16,12 +16,14 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <sqlite3.h>
#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)
{

View File

@ -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);

View File

@ -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;
}

View File

@ -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)
{

View File

@ -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:

View File

@ -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