Migrated the old wmudPlayer struct to the WmudPlayer GObject

This commit is contained in:
Gergely Polonkai 2013-01-05 04:47:04 +01:00
parent 1489be34bf
commit 7d3c343f17
12 changed files with 338 additions and 141 deletions

View File

@ -1,5 +1,5 @@
bin_PROGRAMS = wmud bin_PROGRAMS = wmud
AM_CPPFLAGS = -DWMUD_STATEDIR=\""$(localstatedir)"\" -DWMUD_CONFDIR=\""$(sysconfdir)"\" $(MEMCACHED_CFLAGS) $(GLIB_CFLAGS) $(GIO_CFLAGS) $(GTHREAD_CFLAGS) $(SQLITE3_CFLAGS) $(CURL_CFLAGS) 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) wmud_LDADD = $(MEMCACHED_LIBS) $(GLIB_LIBS) $(GIO_LIBS) $(GTHREAD_LIBS) $(SQLITE3_LIBS) $(CURL_LIBS)

View File

@ -26,6 +26,7 @@
#include "players.h" #include "players.h"
#include "configuration.h" #include "configuration.h"
#include "menu.h" #include "menu.h"
#include "wmudplayer.h"
/** /**
* SECTION:db * SECTION:db
@ -97,13 +98,13 @@ wmud_db_load_players(GError **err)
while (1) { while (1) {
sqlite_code = sqlite3_step(sth); sqlite_code = sqlite3_step(sth);
if (sqlite_code == SQLITE_ROW) { if (sqlite_code == SQLITE_ROW) {
wmudPlayer *player = g_new0(wmudPlayer, 1); WmudPlayer *player = wmud_player_new();
player->id = sqlite3_column_int(sth, 0); wmud_player_set_id(player, sqlite3_column_int(sth, 0));
player->player_name = g_strdup((gchar *)sqlite3_column_text(sth, 1)); wmud_player_set_player_name(player, (const gchar *)sqlite3_column_text(sth, 1));
player->cpassword = g_strdup((gchar *)sqlite3_column_text(sth, 2)); wmud_player_set_cpassword(player, (const gchar *)sqlite3_column_text(sth, 2));
player->email = g_strdup((gchar *)sqlite3_column_text(sth, 3)); 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); players = g_slist_prepend(players, player);
} else if (sqlite_code == SQLITE_DONE) { } else if (sqlite_code == SQLITE_DONE) {
@ -131,7 +132,7 @@ wmud_db_load_players(GError **err)
* set accordingly. * set accordingly.
*/ */
gboolean gboolean
wmud_db_save_player(wmudPlayer *player, GError **err) wmud_db_save_player(WmudPlayer *player, GError **err)
{ {
sqlite3_stmt *sth = NULL; sqlite3_stmt *sth = NULL;
int sqlite_code; int sqlite_code;
@ -149,13 +150,13 @@ wmud_db_save_player(wmudPlayer *player, GError **err)
return FALSE; 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)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad parameter in wmud_db_save_player(): %s", sqlite3_errmsg(dbh));
return FALSE; 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)); g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Bad parameter in wmud_db_save_player(): %s", sqlite3_errmsg(dbh));
return FALSE; return FALSE;

View File

@ -21,6 +21,7 @@
#include <glib.h> #include <glib.h>
#include "wmudplayer.h"
#include "wmud-types.h" #include "wmud-types.h"
#define WMUD_DB_ERROR wmud_db_error_quark() #define WMUD_DB_ERROR wmud_db_error_quark()
@ -42,7 +43,7 @@ typedef enum {
gboolean wmud_db_init(GError **err); gboolean wmud_db_init(GError **err);
gboolean wmud_db_load_players(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_planes(GSList **planes, GError **err);
gboolean wmud_db_load_planets(GSList **planets, GError **err); gboolean wmud_db_load_planets(GSList **planets, GError **err);
gboolean wmud_db_load_directions(GSList **directions, GError **err); gboolean wmud_db_load_directions(GSList **directions, GError **err);

View File

@ -34,6 +34,7 @@
#include "menu.h" #include "menu.h"
#include "texts.h" #include "texts.h"
#include "wmudclient.h" #include "wmudclient.h"
#include "wmudplayer.h"
/** /**
* SECTION:game-networking * SECTION:game-networking
@ -399,26 +400,26 @@ static void
state_fresh(WmudClient *client) state_fresh(WmudClient *client)
{ {
if (*(wmud_client_get_buffer(client)->str)) { if (*(wmud_client_get_buffer(client)->str)) {
wmudPlayer *player; WmudPlayer *player;
if ((player = wmud_player_exists(wmud_client_get_buffer(client)->str)) != NULL) { if ((player = wmud_player_exists(wmud_client_get_buffer(client)->str)) != NULL) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Trying to" g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Trying to"
" login with playername '%s'", " login with playername '%s'",
wmud_client_get_buffer(client)->str); wmud_client_get_buffer(client)->str);
if (player->cpassword == NULL) { if (wmud_player_get_cpassword(player) == NULL) {
wmud_client_send(client, "Your registration is" wmud_client_send(client, "Your registration is"
" not finished yet.\r\n"); " not finished yet.\r\n");
remove_client(client, TRUE); remove_client(client, TRUE);
} else { } else {
wmud_client_set_state(client, WMUD_CLIENT_STATE_PASSWAIT); 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_set_player(client, player);
wmud_client_send(client, "Please provide us your password: %c%c%c", TELNET_IAC, TELNET_WILL, TELNET_ECHO); wmud_client_send(client, "Please provide us your password: %c%c%c", TELNET_IAC, TELNET_WILL, TELNET_ECHO);
} }
} else { } else {
wmudPlayer *player = g_new0(wmudPlayer, 1); WmudPlayer *player = g_new0(WmudPlayer, 1);
player->player_name = g_strdup(wmud_client_get_buffer(client)->str); wmud_player_set_player_name(player, wmud_client_get_buffer(client)->str);
wmud_client_set_player(client, player); wmud_client_set_player(client, player);
wmud_client_set_state(client, WMUD_CLIENT_STATE_YESNO); wmud_client_set_state(client, WMUD_CLIENT_STATE_YESNO);
wmud_client_set_yesno_callback(client, wmud_client_newchar_answer); wmud_client_set_yesno_callback(client, wmud_client_newchar_answer);
@ -431,20 +432,27 @@ static void
state_passwait(WmudClient *client) state_passwait(WmudClient *client)
{ {
if (*(wmud_client_get_buffer(client)->str)) { 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." wmud_client_send(client, "%c%c%c\r\nLogin successful."
"\r\n", TELNET_IAC, TELNET_WONT, "\r\n", TELNET_IAC, TELNET_WONT,
TELNET_ECHO); TELNET_ECHO);
wmud_client_set_authenticated(client, TRUE); 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" wmud_client_send(client, "There %s %d failed"
" login attempt%s with your" " login attempt%s with your"
" account since your last" " account since your last"
" visit\r\n", " visit\r\n",
(wmud_client_get_player(client)->fail_count == 1) ? "was" : "were", (fail_count == 1) ? "was" : "were",
wmud_client_get_player(client)->fail_count, fail_count,
(wmud_client_get_player(client)->fail_count == 1) ? "" : "s"); (fail_count == 1) ? "" : "s");
}
wmud_text_send_to_client("motd", client); wmud_text_send_to_client("motd", client);
wmud_menu_present(client); wmud_menu_present(client);
@ -455,7 +463,7 @@ state_passwait(WmudClient *client)
" be called? ", TELNET_IAC, " be called? ", TELNET_IAC,
TELNET_WONT, TELNET_ECHO); TELNET_WONT, TELNET_ECHO);
wmud_client_set_state(client, WMUD_CLIENT_STATE_FRESH); 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); wmud_client_increase_login_fail_count(client);
if (wmud_client_get_login_fail_count(client) == 3) { if (wmud_client_get_login_fail_count(client) == 3) {
wmud_client_send(client, "You are trying " wmud_client_send(client, "You are trying "
@ -507,7 +515,7 @@ state_registering(WmudClient *client)
remove_client(client, TRUE); remove_client(client, TRUE);
if (g_regex_match(email_regex, wmud_client_get_buffer(client)->str, 0, NULL)) { 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_set_state(client, WMUD_CLIENT_STATE_REGEMAIL_CONFIRM);
wmud_client_send(client, "It seems to be a valid" wmud_client_send(client, "It seems to be a valid"
" address to me, but could you" " address to me, but could you"
@ -535,7 +543,7 @@ state_regemail_confirm(WmudClient *client)
{ {
GError *err = NULL; 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)) { 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"); 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))); 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); remove_client(client, TRUE);
} else { } else {
g_free(wmud_client_get_player(client)->email); wmud_player_set_email(wmud_client_get_player(client), NULL);
wmud_client_get_player(client)->email = NULL;
wmud_client_send(client, "This is not the same as you entered before.\r\nLet's just try it again: "); 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); wmud_client_set_state(client, WMUD_CLIENT_STATE_REGISTERING);

View File

@ -32,6 +32,7 @@
#include "main.h" #include "main.h"
#include "players.h" #include "players.h"
#include "configuration.h" #include "configuration.h"
#include "wmudplayer.h"
/** /**
* SECTION:maintenance-thread * SECTION:maintenance-thread
@ -42,16 +43,16 @@
/** /**
* wmud_maintenance_check_players: * 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 * @user_data: not used
* *
* Callback called from within the maintenance loop. Checks if the player has * Callback called from within the maintenance loop. Checks if the player has
* an unset password, and generate one for them, if so. * an unset password, and generate one for them, if so.
*/ */
void 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, gchar *pw,
*salt, *salt,
*cpw; *cpw;
@ -64,9 +65,9 @@ wmud_maintenance_check_players(wmudPlayer *player, gpointer user_data)
cpw = g_strdup(crypt(pw, full_salt->str)); cpw = g_strdup(crypt(pw, full_salt->str));
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Player %s has no" 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); 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 /* TODO: Send e-mail about the new password. Upon completion, set it in
* the database */ * the database */

View File

@ -42,29 +42,10 @@
*/ */
GSList *players = NULL; 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 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. * Check if the player with the given name already exists.
* *
* Return value: the wmudPlayer structure for the given player name, or %NULL * Return value: the WmudPlayer object for the given player name, or %NULL if
* if it can not be found. * it can not be found.
*/ */
wmudPlayer * WmudPlayer *
wmud_player_exists(gchar *player_name) wmud_player_exists(gchar *player_name)
{ {
GSList *player_elem; 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;
}

View File

@ -26,9 +26,6 @@
extern GSList *players; extern GSList *players;
gboolean wmud_player_auth(WmudClient *client); WmudPlayer *wmud_player_exists(gchar *player_name);
wmudPlayer *wmud_player_exists(gchar *player_name);
void wmud_player_free(wmudPlayer **player);
wmudPlayer *wmud_player_dup(wmudPlayer *player);
#endif /* __WMUD_PLAYERS_H__ */ #endif /* __WMUD_PLAYERS_H__ */

View File

@ -62,26 +62,5 @@ typedef enum {
WMUD_CLIENT_STATE_REGEMAIL_CONFIRM WMUD_CLIENT_STATE_REGEMAIL_CONFIRM
} wmudClientState; } 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 <structname>wmudPlayer</structname> 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__ */ #endif /* __WMUD_TYPES_H__ */

View File

@ -29,7 +29,7 @@ struct _WmudClientPrivate
GString *buffer; GString *buffer;
wmudClientState state; wmudClientState state;
gboolean authenticated; gboolean authenticated;
wmudPlayer *player; WmudPlayer *player;
gboolean bademail; gboolean bademail;
gint login_try_count; gint login_try_count;
WmudClientYesnoCallback yesno_callback; 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_socket_shutdown(self->priv->socket, TRUE, TRUE, NULL);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Connection closed."); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "Connection closed.");
/* TODO: player->registered should be a wmud_player_get_registered() if (self->priv->player && !wmud_player_get_registered(self->priv->player))
* call after wmudPlayer is migrated to be a GObject */ g_object_unref(self->priv->player);
if (self->priv->player && !self->priv->player->registered)
wmud_player_free(&(self->priv->player));
g_object_unref(self); g_object_unref(self);
} }
@ -182,12 +180,12 @@ wmud_client_set_state(WmudClient *self, wmudClientState state)
} }
void void
wmud_client_set_player(WmudClient *self, wmudPlayer *player) wmud_client_set_player(WmudClient *self, WmudPlayer *player)
{ {
self->priv->player = player; self->priv->player = player;
} }
wmudPlayer * WmudPlayer *
wmud_client_get_player(WmudClient *self) wmud_client_get_player(WmudClient *self)
{ {
return self->priv->player; return self->priv->player;

View File

@ -22,12 +22,13 @@
#include <glib-object.h> #include <glib-object.h>
#include <glib.h> #include <glib.h>
#include <gio/gio.h> #include <gio/gio.h>
#include "wmudplayer.h"
#include "wmud-types.h" #include "wmud-types.h"
#define WMUD_TYPE_CLIENT (wmud_client_get_type()) #define WMUD_TYPE_CLIENT (wmud_client_get_type())
#define WMUD_CLIENT(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), WMUD_TYPE_CLIENT, WmudClient)) #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_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_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)) #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); gsize wmud_client_get_buffer_length(WmudClient *client);
void wmud_client_set_state(WmudClient *client, wmudClientState state); void wmud_client_set_state(WmudClient *client, wmudClientState state);
wmudClientState wmud_client_get_state(WmudClient *client); wmudClientState wmud_client_get_state(WmudClient *client);
void wmud_client_set_player(WmudClient *client, wmudPlayer *player); void wmud_client_set_player(WmudClient *client, WmudPlayer *player);
wmudPlayer *wmud_client_get_player(WmudClient *client); WmudPlayer *wmud_client_get_player(WmudClient *client);
void wmud_client_set_yesno_callback(WmudClient *client, WmudClientYesnoCallback yesno_callback); void wmud_client_set_yesno_callback(WmudClient *client, WmudClientYesnoCallback yesno_callback);
WmudClientYesnoCallback wmud_client_get_yesno_callback(WmudClient *client); WmudClientYesnoCallback wmud_client_get_yesno_callback(WmudClient *client);
void wmud_client_set_authenticated(WmudClient *client, gboolean authenticated); 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); void wmud_client_set_bademail(WmudClient *client, gboolean bademail);
gboolean wmud_client_get_bademail(WmudClient *client); gboolean wmud_client_get_bademail(WmudClient *client);
#endif /* __WMUD_WMUDMENU_H__ */ #endif /* __WMUD_WMUDCLIENT_H__ */

211
wmud/wmudplayer.c Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#include "wmudplayer.h"
#include <crypt.h>
#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);
}

70
wmud/wmudplayer.h Normal file
View File

@ -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 <http://www.gnu.org/licenses/>.
*/
#ifndef __WMUD_WMUDPLAYER_H__
#define __WMUD_WMUDPLAYER_H__
#include <glib-object.h>
#include <glib.h>
#include <gio/gio.h>
#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__ */