From 7d3c343f17fb1934742259e84b80cbb567539ade Mon Sep 17 00:00:00 2001 From: Gergely Polonkai Date: Sat, 5 Jan 2013 04:47:04 +0100 Subject: [PATCH] Migrated the old wmudPlayer struct to the WmudPlayer GObject --- wmud/Makefile.am | 2 +- wmud/db.c | 19 ++-- wmud/db.h | 3 +- wmud/game-networking.c | 37 +++++--- wmud/maintenance.c | 11 ++- wmud/players.c | 79 +-------------- wmud/players.h | 5 +- wmud/wmud-types.h | 21 ---- wmud/wmudclient.c | 12 +-- wmud/wmudclient.h | 9 +- wmud/wmudplayer.c | 211 +++++++++++++++++++++++++++++++++++++++++ wmud/wmudplayer.h | 70 ++++++++++++++ 12 files changed, 338 insertions(+), 141 deletions(-) create mode 100644 wmud/wmudplayer.c create mode 100644 wmud/wmudplayer.h diff --git a/wmud/Makefile.am b/wmud/Makefile.am index 8a7367e..76f3325 100644 --- a/wmud/Makefile.am +++ b/wmud/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) $(CURL_CFLAGS) -wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c game.c configuration.c world.c menu.c texts.c wmudclient.c +wmud_SOURCES = main.c game-networking.c interpreter.c db.c players.c maintenance.c game.c configuration.c world.c menu.c texts.c wmudclient.c wmudplayer.c wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS) $(CURL_LIBS) diff --git a/wmud/db.c b/wmud/db.c index fe86eea..a4b3eeb 100644 --- a/wmud/db.c +++ b/wmud/db.c @@ -26,6 +26,7 @@ #include "players.h" #include "configuration.h" #include "menu.h" +#include "wmudplayer.h" /** * SECTION:db @@ -97,13 +98,13 @@ wmud_db_load_players(GError **err) while (1) { sqlite_code = sqlite3_step(sth); if (sqlite_code == SQLITE_ROW) { - wmudPlayer *player = g_new0(wmudPlayer, 1); - player->id = sqlite3_column_int(sth, 0); - player->player_name = g_strdup((gchar *)sqlite3_column_text(sth, 1)); - player->cpassword = g_strdup((gchar *)sqlite3_column_text(sth, 2)); - player->email = g_strdup((gchar *)sqlite3_column_text(sth, 3)); + WmudPlayer *player = wmud_player_new(); + wmud_player_set_id(player, sqlite3_column_int(sth, 0)); + wmud_player_set_player_name(player, (const gchar *)sqlite3_column_text(sth, 1)); + wmud_player_set_cpassword(player, (const gchar *)sqlite3_column_text(sth, 2)); + wmud_player_set_email(player, (const gchar *)sqlite3_column_text(sth, 3)); - g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded player _%s_", player->player_name); + g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded player _%s_", wmud_player_get_player_name(player)); players = g_slist_prepend(players, player); } else if (sqlite_code == SQLITE_DONE) { @@ -131,7 +132,7 @@ wmud_db_load_players(GError **err) * set accordingly. */ gboolean -wmud_db_save_player(wmudPlayer *player, GError **err) +wmud_db_save_player(WmudPlayer *player, GError **err) { sqlite3_stmt *sth = NULL; int sqlite_code; @@ -149,13 +150,13 @@ wmud_db_save_player(wmudPlayer *player, GError **err) return FALSE; } - if ((sqlite_code = sqlite3_bind_text(sth, 1, player->player_name, -1, SQLITE_STATIC)) != SQLITE_OK) { + if ((sqlite_code = sqlite3_bind_text(sth, 1, wmud_player_get_player_name(player), -1, SQLITE_STATIC)) != SQLITE_OK) { g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad parameter in wmud_db_save_player(): %s", sqlite3_errmsg(dbh)); return FALSE; } - if ((sqlite_code = sqlite3_bind_text(sth, 2, player->email, -1, SQLITE_STATIC)) != SQLITE_OK) { + if ((sqlite_code = sqlite3_bind_text(sth, 2, wmud_player_get_email(player), -1, SQLITE_STATIC)) != SQLITE_OK) { g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad parameter in wmud_db_save_player(): %s", sqlite3_errmsg(dbh)); return FALSE; diff --git a/wmud/db.h b/wmud/db.h index 4eede14..e63fc60 100644 --- a/wmud/db.h +++ b/wmud/db.h @@ -21,6 +21,7 @@ #include +#include "wmudplayer.h" #include "wmud-types.h" #define WMUD_DB_ERROR wmud_db_error_quark() @@ -42,7 +43,7 @@ typedef enum { gboolean wmud_db_init(GError **err); gboolean wmud_db_load_players(GError **err); -gboolean wmud_db_save_player(wmudPlayer *player, GError **err); +gboolean wmud_db_save_player(WmudPlayer *player, GError **err); gboolean wmud_db_load_planes(GSList **planes, GError **err); gboolean wmud_db_load_planets(GSList **planets, GError **err); gboolean wmud_db_load_directions(GSList **directions, GError **err); diff --git a/wmud/game-networking.c b/wmud/game-networking.c index 8961621..be37737 100644 --- a/wmud/game-networking.c +++ b/wmud/game-networking.c @@ -34,6 +34,7 @@ #include "menu.h" #include "texts.h" #include "wmudclient.h" +#include "wmudplayer.h" /** * SECTION:game-networking @@ -399,26 +400,26 @@ static void state_fresh(WmudClient *client) { if (*(wmud_client_get_buffer(client)->str)) { - wmudPlayer *player; + WmudPlayer *player; if ((player = wmud_player_exists(wmud_client_get_buffer(client)->str)) != NULL) { g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Trying to" " login with playername '%s'", wmud_client_get_buffer(client)->str); - if (player->cpassword == NULL) { + if (wmud_player_get_cpassword(player) == NULL) { wmud_client_send(client, "Your registration is" " not finished yet.\r\n"); remove_client(client, TRUE); } else { wmud_client_set_state(client, WMUD_CLIENT_STATE_PASSWAIT); - player->registered = TRUE; + wmud_player_set_registered(player, TRUE); wmud_client_set_player(client, player); wmud_client_send(client, "Please provide us your password: %c%c%c", TELNET_IAC, TELNET_WILL, TELNET_ECHO); } } else { - wmudPlayer *player = g_new0(wmudPlayer, 1); - player->player_name = g_strdup(wmud_client_get_buffer(client)->str); + WmudPlayer *player = g_new0(WmudPlayer, 1); + wmud_player_set_player_name(player, wmud_client_get_buffer(client)->str); wmud_client_set_player(client, player); wmud_client_set_state(client, WMUD_CLIENT_STATE_YESNO); wmud_client_set_yesno_callback(client, wmud_client_newchar_answer); @@ -431,20 +432,27 @@ static void state_passwait(WmudClient *client) { if (*(wmud_client_get_buffer(client)->str)) { - if (wmud_player_auth(client)) { + WmudPlayer *player; + + player = wmud_client_get_player(client); + + if (wmud_player_password_valid(player, wmud_client_get_buffer(client)->str)) { + gint fail_count; + wmud_client_send(client, "%c%c%c\r\nLogin successful." "\r\n", TELNET_IAC, TELNET_WONT, TELNET_ECHO); wmud_client_set_authenticated(client, TRUE); - if (wmud_client_get_player(client)->fail_count > 0) + if ((fail_count = wmud_player_get_fail_count(player)) > 0) { wmud_client_send(client, "There %s %d failed" " login attempt%s with your" " account since your last" " visit\r\n", - (wmud_client_get_player(client)->fail_count == 1) ? "was" : "were", - wmud_client_get_player(client)->fail_count, - (wmud_client_get_player(client)->fail_count == 1) ? "" : "s"); + (fail_count == 1) ? "was" : "were", + fail_count, + (fail_count == 1) ? "" : "s"); + } wmud_text_send_to_client("motd", client); wmud_menu_present(client); @@ -455,7 +463,7 @@ state_passwait(WmudClient *client) " be called? ", TELNET_IAC, TELNET_WONT, TELNET_ECHO); wmud_client_set_state(client, WMUD_CLIENT_STATE_FRESH); - wmud_client_get_player(client)->fail_count++; + wmud_player_increase_fail_count(player); wmud_client_increase_login_fail_count(client); if (wmud_client_get_login_fail_count(client) == 3) { wmud_client_send(client, "You are trying " @@ -507,7 +515,7 @@ state_registering(WmudClient *client) remove_client(client, TRUE); if (g_regex_match(email_regex, wmud_client_get_buffer(client)->str, 0, NULL)) { - wmud_client_get_player(client)->email = g_strdup(wmud_client_get_buffer(client)->str); + wmud_player_set_email(wmud_client_get_player(client), wmud_client_get_buffer(client)->str); wmud_client_set_state(client, WMUD_CLIENT_STATE_REGEMAIL_CONFIRM); wmud_client_send(client, "It seems to be a valid" " address to me, but could you" @@ -535,7 +543,7 @@ state_regemail_confirm(WmudClient *client) { GError *err = NULL; - if (g_ascii_strcasecmp(wmud_client_get_player(client)->email, wmud_client_get_buffer(client)->str) == 0) { + if (g_ascii_strcasecmp(wmud_player_get_email(wmud_client_get_player(client)), wmud_client_get_buffer(client)->str) == 0) { if (wmud_db_save_player(wmud_client_get_player(client), &err)) { wmud_client_send(client, "Good. We will generate the password for this player name, and send it to you\r\nvia e-mail. Please come back to us, if you get that code, so you can log\r\nin.\r\n"); players = g_slist_prepend(players, wmud_player_dup(wmud_client_get_player(client))); @@ -546,8 +554,7 @@ state_regemail_confirm(WmudClient *client) remove_client(client, TRUE); } else { - g_free(wmud_client_get_player(client)->email); - wmud_client_get_player(client)->email = NULL; + wmud_player_set_email(wmud_client_get_player(client), NULL); wmud_client_send(client, "This is not the same as you entered before.\r\nLet's just try it again: "); wmud_client_set_state(client, WMUD_CLIENT_STATE_REGISTERING); diff --git a/wmud/maintenance.c b/wmud/maintenance.c index 63aa8cc..35899c7 100644 --- a/wmud/maintenance.c +++ b/wmud/maintenance.c @@ -32,6 +32,7 @@ #include "main.h" #include "players.h" #include "configuration.h" +#include "wmudplayer.h" /** * SECTION:maintenance-thread @@ -42,16 +43,16 @@ /** * wmud_maintenance_check_players: - * @player: #wmudPLayer structure of the player record to check + * @player: #WmudPLayer object of the player record to check * @user_data: not used * * Callback called from within the maintenance loop. Checks if the player has * an unset password, and generate one for them, if so. */ void -wmud_maintenance_check_players(wmudPlayer *player, gpointer user_data) +wmud_maintenance_check_players(WmudPlayer *player, gpointer user_data) { - if (player->cpassword == NULL) { + if (wmud_player_get_cpassword(player) == NULL) { gchar *pw, *salt, *cpw; @@ -64,9 +65,9 @@ wmud_maintenance_check_players(wmudPlayer *player, gpointer user_data) cpw = g_strdup(crypt(pw, full_salt->str)); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Player %s has no" - " password set", player->player_name); + " password set", wmud_player_get_player_name(player)); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "New password will be %s", pw); - player->cpassword = cpw; + wmud_player_set_cpassword(player, cpw); /* TODO: Send e-mail about the new password. Upon completion, set it in * the database */ diff --git a/wmud/players.c b/wmud/players.c index a505c8d..eb312c2 100644 --- a/wmud/players.c +++ b/wmud/players.c @@ -42,29 +42,10 @@ */ GSList *players = NULL; -/** - * wmud_player_auth: - * @client: The client to be authenticated. The authentication password comes - * from the client's buffer. - * - * Tries to authenticate a client based on the associated player structure, and - * the password stored in the client's buffer. - * - * Return value: %TRUE if the password is valid, %FALSE otherwise. - */ -gboolean -wmud_player_auth(WmudClient *client) -{ - if (g_strcmp0(crypt(wmud_client_get_buffer(client)->str, wmud_client_get_player(client)->cpassword), wmud_client_get_player(client)->cpassword) == 0) - return TRUE; - - return FALSE; -} - static gint -find_player_by_name(wmudPlayer *player, gchar *player_name) +find_player_by_name(WmudPlayer *player, gchar *player_name) { - return g_ascii_strcasecmp(player->player_name, player_name); + return g_ascii_strcasecmp(wmud_player_get_player_name(player), player_name); } /** @@ -73,10 +54,10 @@ find_player_by_name(wmudPlayer *player, gchar *player_name) * * Check if the player with the given name already exists. * - * Return value: the wmudPlayer structure for the given player name, or %NULL - * if it can not be found. + * Return value: the WmudPlayer object for the given player name, or %NULL if + * it can not be found. */ -wmudPlayer * +WmudPlayer * wmud_player_exists(gchar *player_name) { GSList *player_elem; @@ -88,53 +69,3 @@ wmud_player_exists(gchar *player_name) } -/** - * wmud_player_dup: - * @player: the player structure to duplicate - * - * Duplicates a #wmudPlayer structure. - * - * Return value: the new, duplicated player structure. It must be freed with wmud_player_free(). - */ -wmudPlayer * -wmud_player_dup(wmudPlayer *player) -{ - wmudPlayer *new_player; - - if (!player) - return NULL; - - new_player = g_new0(wmudPlayer, 1); - new_player->id = player->id; - new_player->player_name = g_strdup(player->player_name); - new_player->cpassword = g_strdup(player->cpassword); - new_player->email = g_strdup(player->email); - - return new_player; -} - -/** - * wmud_player_free: - * @player: A pointer to the player structure to be freed - * - * Frees a #wmudPlayer structure with all its fields, and sets the structure - * variable to %NULL. - */ -void -wmud_player_free(wmudPlayer **player) -{ - if (!*player) - return; - - if ((*player)->player_name) - g_free((*player)->player_name); - - if ((*player)->cpassword) - g_free((*player)->cpassword); - - if ((*player)->email) - g_free((*player)->email); - - g_free(*player); - *player = NULL; -} diff --git a/wmud/players.h b/wmud/players.h index 32a5a96..bcc324a 100644 --- a/wmud/players.h +++ b/wmud/players.h @@ -26,9 +26,6 @@ extern GSList *players; -gboolean wmud_player_auth(WmudClient *client); -wmudPlayer *wmud_player_exists(gchar *player_name); -void wmud_player_free(wmudPlayer **player); -wmudPlayer *wmud_player_dup(wmudPlayer *player); +WmudPlayer *wmud_player_exists(gchar *player_name); #endif /* __WMUD_PLAYERS_H__ */ diff --git a/wmud/wmud-types.h b/wmud/wmud-types.h index 46a1e01..909379c 100644 --- a/wmud/wmud-types.h +++ b/wmud/wmud-types.h @@ -62,26 +62,5 @@ typedef enum { WMUD_CLIENT_STATE_REGEMAIL_CONFIRM } wmudClientState; -/** - * wmudPlayer: - * @id: Player's database ID - * @player_name: Player's login name - * @cpassword: crypt()ed password of the player. This is NULL for newly - * registered players, who have no password generated for them by the - * maintenance loop - * @email: E-mail address of the player - * - * The wmudPlayer structure contains all information of - * a player. - */ -typedef struct _wmudPlayer { - guint32 id; - gchar *player_name; - gchar *cpassword; - gchar *email; - gint fail_count; - gboolean registered; -} wmudPlayer; - #endif /* __WMUD_TYPES_H__ */ diff --git a/wmud/wmudclient.c b/wmud/wmudclient.c index becd01a..63d5731 100644 --- a/wmud/wmudclient.c +++ b/wmud/wmudclient.c @@ -29,7 +29,7 @@ struct _WmudClientPrivate GString *buffer; wmudClientState state; gboolean authenticated; - wmudPlayer *player; + WmudPlayer *player; gboolean bademail; gint login_try_count; WmudClientYesnoCallback yesno_callback; @@ -149,10 +149,8 @@ wmud_client_close(WmudClient *self, gboolean send_goodbye) g_socket_shutdown(self->priv->socket, TRUE, TRUE, NULL); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Connection closed."); - /* TODO: player->registered should be a wmud_player_get_registered() - * call after wmudPlayer is migrated to be a GObject */ - if (self->priv->player && !self->priv->player->registered) - wmud_player_free(&(self->priv->player)); + if (self->priv->player && !wmud_player_get_registered(self->priv->player)) + g_object_unref(self->priv->player); g_object_unref(self); } @@ -182,12 +180,12 @@ wmud_client_set_state(WmudClient *self, wmudClientState state) } void -wmud_client_set_player(WmudClient *self, wmudPlayer *player) +wmud_client_set_player(WmudClient *self, WmudPlayer *player) { self->priv->player = player; } -wmudPlayer * +WmudPlayer * wmud_client_get_player(WmudClient *self) { return self->priv->player; diff --git a/wmud/wmudclient.h b/wmud/wmudclient.h index df6b937..a50fad6 100644 --- a/wmud/wmudclient.h +++ b/wmud/wmudclient.h @@ -22,12 +22,13 @@ #include #include #include +#include "wmudplayer.h" #include "wmud-types.h" #define WMUD_TYPE_CLIENT (wmud_client_get_type()) #define WMUD_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), WMUD_TYPE_CLIENT, WmudClient)) #define WMUD_IS_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), WMUD_TYPE_CLIENT)) -#define WMUD_TYPE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), WMUD_TYPE_CLIENT, WmudClientClass)) +#define WMUD_CLIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), WMUD_TYPE_CLIENT, WmudClientClass)) #define WMUD_IS_CLIENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), WMUD_TYPE_CLIENT)) #define WMUD_CLIENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), WMUT_TYPE_CLIENT, WmudClientClass)) @@ -60,8 +61,8 @@ GString *wmud_client_get_buffer(WmudClient *client); gsize wmud_client_get_buffer_length(WmudClient *client); void wmud_client_set_state(WmudClient *client, wmudClientState state); wmudClientState wmud_client_get_state(WmudClient *client); -void wmud_client_set_player(WmudClient *client, wmudPlayer *player); -wmudPlayer *wmud_client_get_player(WmudClient *client); +void wmud_client_set_player(WmudClient *client, WmudPlayer *player); +WmudPlayer *wmud_client_get_player(WmudClient *client); void wmud_client_set_yesno_callback(WmudClient *client, WmudClientYesnoCallback yesno_callback); WmudClientYesnoCallback wmud_client_get_yesno_callback(WmudClient *client); void wmud_client_set_authenticated(WmudClient *client, gboolean authenticated); @@ -70,5 +71,5 @@ gint wmud_client_get_login_fail_count(WmudClient *client); void wmud_client_set_bademail(WmudClient *client, gboolean bademail); gboolean wmud_client_get_bademail(WmudClient *client); -#endif /* __WMUD_WMUDMENU_H__ */ +#endif /* __WMUD_WMUDCLIENT_H__ */ diff --git a/wmud/wmudplayer.c b/wmud/wmudplayer.c new file mode 100644 index 0000000..6599fc8 --- /dev/null +++ b/wmud/wmudplayer.c @@ -0,0 +1,211 @@ +/* wMUD - Yet another MUD codebase by W00d5t0ck + * Copyright (C) 2012 - Gergely POLONKAI + * + * wmudplayer.c: the WmudPlayer GObject type + * + * 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 . + */ +#include "wmudplayer.h" + +#include + +#include "players.h" + +#define WMUD_PLAYER_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE((obj), WMUD_TYPE_PLAYER, WmudPlayerPrivate)) + +struct _WmudPlayerPrivate +{ + guint32 id; + gchar *player_name; + gchar *cpassword; + gchar *email; + gint fail_count; + gboolean registered; +}; + +G_DEFINE_TYPE(WmudPlayer, wmud_player, G_TYPE_OBJECT); + +static void +wmud_player_dispose(GObject *gobject) +{ + //WmudPlayer *self = WMUD_PLAYER(gobject); + + G_OBJECT_CLASS(wmud_player_parent_class)->dispose(gobject); +} + +static void +wmud_player_finalize(GObject *gobject) +{ + WmudPlayer *self = WMUD_PLAYER(gobject); + + if (self->priv->player_name) + g_free(self->priv->player_name); + + if (self->priv->cpassword) + g_free(self->priv->cpassword); + + if (self->priv->email) + g_free(self->priv->email); + + G_OBJECT_CLASS(wmud_player_parent_class)->finalize(gobject); +} + +static void +wmud_player_class_init(WmudPlayerClass *klass) +{ + GObjectClass *gobject_class = G_OBJECT_CLASS(klass); + + gobject_class->dispose = wmud_player_dispose; + gobject_class->finalize = wmud_player_finalize; + + g_type_class_add_private(klass, sizeof(WmudPlayerPrivate)); +} + +static void +wmud_player_init(WmudPlayer *self) +{ + self->priv = WMUD_PLAYER_GET_PRIVATE(self); +} + +WmudPlayer * +wmud_player_new(void) +{ + return g_object_new(WMUD_TYPE_PLAYER, NULL, NULL); +} + +void +wmud_player_set_cpassword(WmudPlayer *self, const gchar *cpassword) +{ + if (self->priv->cpassword) + g_free(self->priv->cpassword); + + self->priv->cpassword = g_strdup(cpassword); +} + +gchar * +wmud_player_get_cpassword(WmudPlayer *self) +{ + return self->priv->cpassword; +} + +void +wmud_player_set_registered(WmudPlayer *self, gboolean registered) +{ + self->priv->registered = registered; +} + +gboolean +wmud_player_get_registered(WmudPlayer *self) +{ + return self->priv->registered; +} + +void +wmud_player_set_player_name(WmudPlayer *self, const gchar *name) +{ + self->priv->player_name = g_strdup(name); +} + +gchar * +wmud_player_get_player_name(WmudPlayer *self) +{ + return self->priv->player_name; +} + +void +wmud_player_reset_fail_count(WmudPlayer *self) +{ + self->priv->fail_count = 0; +} + +void +wmud_player_increase_fail_count(WmudPlayer *self) +{ + self->priv->fail_count++; +} + +gint +wmud_player_get_fail_count(WmudPlayer *self) +{ + return self->priv->fail_count; +} + +void +wmud_player_set_email(WmudPlayer *self, const gchar *email) +{ + if (self->priv->email) + g_free(self->priv->email); + + self->priv->email = g_strdup(email); +} + +gchar * +wmud_player_get_email(WmudPlayer *self) +{ + return self->priv->email; +} + +void +wmud_player_set_id(WmudPlayer *self, guint32 id) +{ + self->priv->id = id; +} + +guint32 +wmud_player_get_id(WmudPlayer *self) +{ + return self->priv->id; +} + +/** + * wmud_player_dup: + * @player: the object structure to duplicate + * + * Duplicates a #WmudPlayer object. + * + * Return value: the new, duplicated player object. + */ +WmudPlayer * +wmud_player_dup(WmudPlayer *self) +{ + WmudPlayer *new_player; + + if (!self) + return NULL; + + new_player = wmud_player_new(); + new_player->priv->id = self->priv->id; + new_player->priv->player_name = g_strdup(self->priv->player_name); + new_player->priv->cpassword = g_strdup(self->priv->cpassword); + new_player->priv->email = g_strdup(self->priv->email); + + return new_player; +} + +/** + * wmud_player_password_valid: + * @client: The client to be authenticated + * @password: The password in clear text + * + * Tries to authenticate a client with the name stored in the player object, + * and the password provided. + * + * Return value: %TRUE if the password is valid, %FALSE otherwise. + */ +gboolean +wmud_player_password_valid(WmudPlayer *self, const gchar *password) +{ + return (g_strcmp0(crypt(password, self->priv->cpassword), self->priv->cpassword) == 0); +} + diff --git a/wmud/wmudplayer.h b/wmud/wmudplayer.h new file mode 100644 index 0000000..b580dcf --- /dev/null +++ b/wmud/wmudplayer.h @@ -0,0 +1,70 @@ +/* wMUD - Yet another MUD codebase by W00d5t0ck + * Copyright (C) 2012 - Gergely POLONKAI + * + * wmudplayer.h: the WmudPlayer GObject type + * + * 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 . + */ +#ifndef __WMUD_WMUDPLAYER_H__ +#define __WMUD_WMUDPLAYER_H__ + +#include +#include +#include +#include "wmud-types.h" + +#define WMUD_TYPE_PLAYER (wmud_player_get_type()) +#define WMUD_PLAYER(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), WMUD_TYPE_PLAYER, WmudPlayer)) +#define WMUD_IS_PLAYER(obj) (G_TYPE_CHECK_INSTANCE_TYPE((obj), WMUD_TYPE_PLAYER)) +#define WMUD_PLAYER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST((klass), WMUD_TYPE_PLAYER, WmudPlayerClass)) +#define WMUD_IS_PLAYER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE((klass), WMUD_TYPE_PLAYER)) +#define WMUD_PLAYER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS((obj), WMUT_TYPE_PLAYER, WmudPlayerClass)) + +typedef struct _WmudPlayer WmudPlayer; +typedef struct _WmudPlayerClass WmudPlayerClass; +typedef struct _WmudPlayerPrivate WmudPlayerPrivate; + +struct _WmudPlayer +{ + GObject parent_instance; + + /*< private >*/ + WmudPlayerPrivate *priv; +}; + +struct _WmudPlayerClass +{ + GObjectClass parent_class; +}; + +GType wmud_player_get_type(void); +WmudPlayer *wmud_player_new(void); +void wmud_player_set_cpassword(WmudPlayer *player, const gchar *cpassword); +gchar *wmud_player_get_cpassword(WmudPlayer *player); +void wmud_player_set_registered(WmudPlayer *player, gboolean registered); +gboolean wmud_player_get_registered(WmudPlayer *player); +void wmud_player_set_player_name(WmudPlayer *player, const gchar *name); +gchar *wmud_player_get_player_name(WmudPlayer *player); +gint wmud_player_get_fail_count(WmudPlayer *player); +void wmud_player_increase_fail_count(WmudPlayer *player); +void wmud_player_reset_fail_count(WmudPlayer *player); +void wmud_player_set_email(WmudPlayer *player, const gchar *email); +gchar *wmud_player_get_email(WmudPlayer *player); +void wmud_player_set_id(WmudPlayer *player, guint32 id); +guint32 wmud_player_get_id(WmudPlayer *player); +WmudPlayer *wmud_player_dup(WmudPlayer *player); +gboolean wmud_player_password_valid(WmudPlayer *self, const gchar *password); + +#endif /* __WMUD_WMUDPLAYER_H__ */ +