Uncrustify sources

This commit is contained in:
Gergely Polonkai 2016-06-22 17:59:53 +02:00
parent 1c88c88b63
commit e820f2308a
7 changed files with 1289 additions and 900 deletions

140
uncrustify.cfg Normal file
View File

@ -0,0 +1,140 @@
newlines lf
input_tab_size 8
output_tab_size 8
string_escape_char 92
string_escape_char2 0
# indenting
indent_columns 4
indent_with_tabs 0
indent_align_string True
indent_brace 0
indent_braces False
indent_braces_no_func True
indent_func_call_param False
indent_func_def_param False
indent_func_proto_param False
indent_switch_case indent_columns
indent_case_brace indent_columns
indent_paren_close 1
indent_var_def_cont False
# spacing
sp_arith Add
sp_assign Add
sp_enum_assign Add
sp_bool Add
sp_compare Add
sp_inside_paren Remove
sp_inside_fparens Remove
sp_func_def_paren Remove
sp_func_proto_paren Remove
sp_paren_paren Remove
sp_balance_nested_parens False
sp_paren_brace Remove
sp_before_square Remove
sp_before_squares Remove
sp_inside_square Remove
sp_after_comma Add
sp_before_comma Remove
sp_after_cast Remove
sp_sizeof_paren Remove
sp_not Remove
sp_inv Remove
sp_addr Remove
sp_member Remove
sp_deref Remove
sp_sign Remove
sp_incdec Remove
sp_attribute_paren Remove
sp_macro Force
sp_func_call_paren Remove
sp_brace_typedef add
sp_cond_colon add
sp_cond_question add
sp_defined_paren Remove
sp_inside_paren_cast Ignore
sp_else_brace Add
sp_between_ptr_star Remove
sp_before_ptr_star Add
# alignment
align_keep_tabs False
align_with_tabs False
align_on_tabstop False
align_number_left True
align_func_params True
align_var_def_span 2
align_var_def_amp_style 1
align_var_def_colon True
align_enum_equ_span 0
align_var_struct_span 2
align_var_def_star_style 1
align_var_def_amp_style 1
align_typedef_span 2
align_typedef_func 0
align_typedef_star_style 1
align_typedef_amp_style 2
align_var_def_inline True
align_assign_span 1
align_pp_define_together True
# newlines
nl_assign_leave_one_liners True
nl_enum_leave_one_liners False
nl_func_leave_one_liners False
nl_if_leave_one_liners False
nl_end_of_file Add
nl_end_of_file_min 2
nl_assign_brace Remove
nl_func_var_def_blk 1
nl_fcall_brace Add
nl_enum_brace Remove
nl_struct_brace Remove
nl_union_brace Remove
nl_if_brace Remove
nl_brace_else Remove
nl_elseif_brace Remove
nl_else_brace Remove
nl_for_brace Remove
nl_while_brace Remove
nl_do_brace Remove
nl_brace_while Remove
nl_switch_brace Remove
nl_before_case True
nl_after_case True
nl_func_type_name Force
nl_func_proto_type_name Remove
nl_func_paren Remove
nl_func_decl_start Remove
nl_func_decl_args Force
nl_func_decl_end Remove
nl_fdef_brace Force
nl_after_return False
nl_define_macro False
nl_create_if_one_liner False
nl_create_for_one_liner False
nl_create_while_one_liner False
nl_after_semicolon True
nl_multi_line_cond False
nl_var_def_blk_in 2
nl_multi_line_define True
# mod
mod_full_brace_for Force
mod_full_brace_if Force
mod_full_brace_while Force
mod_full_brace_do Remove
mod_full_brace_nl 3
mod_paren_on_return Remove
# line splitting
#code_width = 78
ls_for_split_full True
ls_func_split_full True
# positioning
pos_bool Trail
pos_conditional Lead

View File

@ -42,7 +42,7 @@ ConfigData *active_config = NULL;
GQuark GQuark
wmud_config_error_quark() wmud_config_error_quark()
{ {
return g_quark_from_static_string("wmud-config-error"); return g_quark_from_static_string("wmud-config-error");
} }
/** /**
@ -54,26 +54,32 @@ wmud_config_error_quark()
void void
wmud_configdata_free(ConfigData **config_data) wmud_configdata_free(ConfigData **config_data)
{ {
if ((*config_data)->admin_email) if ((*config_data)->admin_email) {
g_free((*config_data)->admin_email); g_free((*config_data)->admin_email);
}
if ((*config_data)->database_dsn) if ((*config_data)->database_dsn) {
g_free((*config_data)->database_dsn); g_free((*config_data)->database_dsn);
}
if ((*config_data)->smtp_server) if ((*config_data)->smtp_server) {
g_free((*config_data)->smtp_server); g_free((*config_data)->smtp_server);
}
if ((*config_data)->smtp_username) if ((*config_data)->smtp_username) {
g_free((*config_data)->smtp_username); g_free((*config_data)->smtp_username);
}
if ((*config_data)->smtp_password) if ((*config_data)->smtp_password) {
g_free((*config_data)->smtp_password); g_free((*config_data)->smtp_password);
}
if ((*config_data)->smtp_sender) if ((*config_data)->smtp_sender) {
g_free((*config_data)->smtp_sender); g_free((*config_data)->smtp_sender);
}
g_free(*config_data); g_free(*config_data);
*config_data = NULL; *config_data = NULL;
} }
/** /**
@ -90,127 +96,176 @@ wmud_configdata_free(ConfigData **config_data)
gboolean gboolean
wmud_config_init(ConfigData **config_data, GError **err) wmud_config_init(ConfigData **config_data, GError **err)
{ {
GString *config_file = g_string_new(WMUD_CONFDIR); GString *config_file = g_string_new(WMUD_CONFDIR);
GKeyFile *config; GKeyFile *config;
GError *in_err = NULL; GError *in_err = NULL;
gchar *pos; gchar *pos;
if (!config_data) if (!config_data)
return FALSE; return FALSE;
if (*config_data) { if (*config_data) {
g_clear_error(err); g_clear_error(err);
g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_REUSE, "Configuration pointer reuse. Please file a bug report!"); g_set_error(err,
return FALSE; WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_REUSE,
} "Configuration pointer reuse. Please file a bug report!");
return FALSE;
}
*config_data = g_new0(ConfigData, 1); *config_data = g_new0(ConfigData, 1);
g_string_append(config_file, "/wmud.conf"); g_string_append(config_file, "/wmud.conf");
config = g_key_file_new(); config = g_key_file_new();
/* TODO: Error checking */ /* TODO: Error checking */
g_key_file_load_from_file(config, config_file->str, 0, &in_err); g_key_file_load_from_file(config, config_file->str, 0, &in_err);
if (!g_key_file_has_group(config, "global")) { 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_set_error(err,
g_key_file_free(config); WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOGLOBAL,
g_string_free(config_file, TRUE); "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; return FALSE;
} }
if (!g_key_file_has_group(config, "smtp")) { if (!g_key_file_has_group(config, "smtp")) {
g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOSMTP, "Config file (%s) does not contain an [smtp] group", config_file->str); g_set_error(err,
g_key_file_free(config); WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NOSMTP,
g_string_free(config_file, TRUE); "Config file (%s) does not contain an [smtp] group",
config_file->str);
g_key_file_free(config);
g_string_free(config_file, TRUE);
return FALSE; return FALSE;
} }
if (!g_key_file_has_group(config, "database")) { if (!g_key_file_has_group(config, "database")) {
g_set_error(err, WMUD_CONFIG_ERROR, WMUD_CONFIG_ERROR_NODATABASE, "Config file (%s) does not contain a [database] group", config_file->str); g_set_error(err,
g_key_file_free(config); WMUD_CONFIG_ERROR,
g_string_free(config_file, TRUE); WMUD_CONFIG_ERROR_NODATABASE,
"Config file (%s) does not contain a [database] group",
config_file->str);
g_key_file_free(config);
g_string_free(config_file, TRUE);
return FALSE; return FALSE;
} }
g_clear_error(&in_err); g_clear_error(&in_err);
(*config_data)->port = g_key_file_get_integer(config, "global", "port", &in_err); (*config_data)->port = g_key_file_get_integer(config,
if (in_err) "global", "port",
{ &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; 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); return FALSE;
(*config_data)->admin_email = g_key_file_get_string(config, "global", "admin email", &in_err); }
if (in_err && 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_clear_error(&in_err);
} (*config_data)->admin_email = g_key_file_get_string(config,
"global", "admin email",
&in_err);
if (in_err && 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);
g_clear_error(&in_err); return FALSE;
(*config_data)->smtp_server = g_key_file_get_string(config, "smtp", "smtp server", &in_err); }
if (in_err && 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_NOSMTPSERVER, "Config file (%s) does not contain an smtp server address", 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)->smtp_server = g_key_file_get_string(config,
"smtp", "smtp server",
&in_err);
if (in_err && 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_NOSMTPSERVER,
"Config file (%s) does not contain an smtp server address",
config_file->str);
g_key_file_free(config);
g_string_free(config_file, TRUE);
wmud_configdata_free(config_data);
g_clear_error(&in_err); return FALSE;
(*config_data)->smtp_sender = g_key_file_get_string(config, "smtp", "smtp sender", &in_err); }
if (in_err && 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_NOSMTPSENDER, "Config file (%s) does not contain an smtp sender name", 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)->smtp_sender = g_key_file_get_string(config,
"smtp", "smtp sender",
&in_err);
if (in_err && 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_NOSMTPSENDER,
"Config file (%s) does not contain an smtp sender name",
config_file->str);
g_key_file_free(config);
g_string_free(config_file, TRUE);
wmud_configdata_free(config_data);
g_clear_error(&in_err); return FALSE;
(*config_data)->database_dsn = g_key_file_get_string(config, "database", "dsn", &in_err); }
if (in_err && 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 database dsn", 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)->database_dsn = g_key_file_get_string(config,
"database", "dsn",
&in_err);
if (in_err && 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 database dsn",
config_file->str);
g_key_file_free(config);
g_string_free(config_file, TRUE);
wmud_configdata_free(config_data);
if ((pos = g_strstr_len((*config_data)->database_dsn, -1, "{statedir}")) != NULL) { return FALSE;
guint real_pos = pos - (*config_data)->database_dsn; }
GString *tmp = g_string_new((*config_data)->database_dsn);
g_string_erase(tmp, real_pos, 10); if ((pos = g_strstr_len((*config_data)->database_dsn,
g_string_insert(tmp, real_pos, WMUD_STATEDIR); -1, "{statedir}")) != NULL) {
(*config_data)->database_dsn = tmp->str; guint real_pos = pos - (*config_data)->database_dsn;
g_string_free(tmp, FALSE); GString *tmp = g_string_new((*config_data)->database_dsn);
}
g_key_file_free(config); g_string_erase(tmp, real_pos, 10);
g_string_free(config_file, TRUE); g_string_insert(tmp, real_pos, WMUD_STATEDIR);
(*config_data)->database_dsn = tmp->str;
g_string_free(tmp, FALSE);
}
return TRUE; g_key_file_free(config);
g_string_free(config_file, TRUE);
return TRUE;
} }

767
wmud/db.c
View File

@ -45,7 +45,7 @@ static GdaSqlParser *parser = NULL;
GQuark GQuark
wmud_db_error_quark() wmud_db_error_quark()
{ {
return g_quark_from_static_string("wmud-db-error"); return g_quark_from_static_string("wmud-db-error");
} }
/** /**
@ -57,26 +57,36 @@ wmud_db_error_quark()
gboolean gboolean
wmud_db_init(GError **err) wmud_db_init(GError **err)
{ {
GError *local_err = NULL; GError *local_err = NULL;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Initializing database"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Initializing database");
gda_init(); gda_init();
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Database DSN is \"%s\"", active_config->database_dsn); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
dbh = gda_connection_open_from_string(NULL, active_config->database_dsn, NULL, GDA_CONNECTION_OPTIONS_THREAD_SAFE, &local_err); "Database DSN is \"%s\"",
active_config->database_dsn);
dbh = gda_connection_open_from_string(NULL,
active_config->database_dsn,
NULL,
GDA_CONNECTION_OPTIONS_THREAD_SAFE,
&local_err);
if (dbh == NULL) { if (dbh == NULL) {
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_CANTOPEN, "Can not open databsae (%s): %s", active_config->database_dsn, local_err->message); g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_CANTOPEN,
"Can not open databsae (%s): %s",
active_config->database_dsn,
local_err->message);
return FALSE; return FALSE;
} }
parser = gda_sql_parser_new(); parser = gda_sql_parser_new();
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Database initialization finished."); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Database initialization finished.");
return TRUE; return TRUE;
} }
/** /**
@ -88,62 +98,79 @@ wmud_db_init(GError **err)
gboolean gboolean
wmud_db_load_players(GError **err) wmud_db_load_players(GError **err)
{ {
GdaStatement *sth = NULL; GdaStatement *sth = NULL;
GdaDataModel *res; GdaDataModel *res;
GdaDataModelIter *iter; GdaDataModelIter *iter;
GError *local_err = NULL; GError *local_err = NULL;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading players"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading players");
if (dbh == NULL) { if (dbh == NULL) {
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
"Database backend not initialized");
return FALSE; return FALSE;
} }
sth = gda_sql_parser_parse_string(parser, "SELECT id, login, password, email FROM players", NULL, NULL); sth = gda_sql_parser_parse_string(
parser,
"SELECT id, login, password, email FROM players",
NULL,
NULL);
/* TODO: error checking! */ /* TODO: error checking! */
if ((res = gda_connection_statement_execute_select(dbh, sth, NULL, &local_err)) == NULL) { if ((res = gda_connection_statement_execute_select(
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "Unable to load players: %s", local_err->message); dbh,
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_SELECT_ERROR, "SELECT error: %s", local_err->message); sth,
NULL,
&local_err)) == NULL) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
"Unable to load players: %s",
local_err->message);
g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_SELECT_ERROR,
"SELECT error: %s",
local_err->message);
return FALSE; return FALSE;
} }
iter = gda_data_model_create_iter(res); iter = gda_data_model_create_iter(res);
gda_data_model_iter_move_next(iter); gda_data_model_iter_move_next(iter);
while (gda_data_model_iter_is_valid(iter)) { while (gda_data_model_iter_is_valid(iter)) {
const GValue *val; const GValue *val;
WmudPlayer *player; WmudPlayer *player;
player = wmud_player_new(); player = wmud_player_new();
val = gda_data_model_iter_get_value_at(iter, 0); val = gda_data_model_iter_get_value_at(iter, 0);
wmud_player_set_id(player, g_value_get_int(val)); wmud_player_set_id(player, g_value_get_int(val));
val = gda_data_model_iter_get_value_at(iter, 1); val = gda_data_model_iter_get_value_at(iter, 1);
wmud_player_set_player_name(player, g_value_get_string(val)); wmud_player_set_player_name(player, g_value_get_string(val));
val = gda_data_model_iter_get_value_at(iter, 2); val = gda_data_model_iter_get_value_at(iter, 2);
wmud_player_set_cpassword(player, g_value_get_string(val)); wmud_player_set_cpassword(player, g_value_get_string(val));
val = gda_data_model_iter_get_value_at(iter, 3); val = gda_data_model_iter_get_value_at(iter, 3);
wmud_player_set_email(player, g_value_get_string(val)); wmud_player_set_email(player, g_value_get_string(val));
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded player _%s_", wmud_player_get_player_name(player)); 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);
gda_data_model_iter_move_next(iter); gda_data_model_iter_move_next(iter);
} }
g_object_unref(iter); g_object_unref(iter);
g_object_unref(sth); g_object_unref(sth);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Finished loading players"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Finished loading players");
return TRUE; return TRUE;
} }
/** /**
@ -159,41 +186,63 @@ wmud_db_load_players(GError **err)
gboolean gboolean
wmud_db_save_player(WmudPlayer *player, GError **err) wmud_db_save_player(WmudPlayer *player, GError **err)
{ {
GError *local_err = NULL; GError *local_err = NULL;
GValue *login_value, GValue *login_value,
*email_value; *email_value;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Saving player"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Saving player");
if (dbh == NULL) { if (dbh == NULL) {
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
"Database backend not initialized");
return FALSE; return FALSE;
} }
login_value = gda_value_new_from_string(wmud_player_get_player_name(player), G_TYPE_STRING); login_value = gda_value_new_from_string(
email_value = gda_value_new_from_string(wmud_player_get_email(player), G_TYPE_STRING); wmud_player_get_player_name(player),
G_TYPE_STRING);
email_value = gda_value_new_from_string(
wmud_player_get_email(player),
G_TYPE_STRING);
if (!gda_connection_insert_row_into_table(dbh, "players", &local_err, "id", NULL, "login", login_value, "password", NULL, "email", email_value, NULL)) { if (!gda_connection_insert_row_into_table(dbh,
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Error saving player: %s", local_err->message); "players",
&local_err,
"id", NULL,
"login", login_value,
"password", NULL,
"email", email_value,
NULL)) {
g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY,
"Error saving player: %s",
local_err->message);
return FALSE; return FALSE;
} }
return TRUE; return TRUE;
} }
gboolean gboolean
wmud_db_update_player_password(WmudPlayer *player, gchar *crypted_password, GError **err) wmud_db_update_player_password(WmudPlayer *player,
gchar *crypted_password,
GError **err)
{ {
GValue *cpw, GValue *cpw,
*player_id; *player_id;
GError *local_err = NULL; GError *local_err = NULL;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Saving player password for %s", wmud_player_get_player_name(player)); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"Saving player password for %s",
wmud_player_get_player_name(player));
if (dbh == NULL) { if (dbh == NULL) {
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
"Database backend not initialized");
return FALSE; return FALSE;
} }
@ -202,8 +251,16 @@ wmud_db_update_player_password(WmudPlayer *player, gchar *crypted_password, GErr
g_value_set_uint(player_id, wmud_player_get_id(player)); g_value_set_uint(player_id, wmud_player_get_id(player));
cpw = gda_value_new_from_string(crypted_password, G_TYPE_STRING); cpw = gda_value_new_from_string(crypted_password, G_TYPE_STRING);
if (!gda_connection_update_row_in_table(dbh, "players", "id", player_id, &local_err, "password", cpw, NULL)) { if (!gda_connection_update_row_in_table(dbh,
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY, "Error saving player password: %s", local_err->message); "players",
"id", player_id,
&local_err,
"password", cpw,
NULL)) {
g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_BADQUERY,
"Error saving player password: %s",
local_err->message);
return FALSE; return FALSE;
} }
@ -214,395 +271,465 @@ wmud_db_update_player_password(WmudPlayer *player, gchar *crypted_password, GErr
gboolean gboolean
wmud_db_load_planes(GSList **planes, GError **err) wmud_db_load_planes(GSList **planes, GError **err)
{ {
GdaStatement *sth = NULL; GdaStatement *sth = NULL;
GdaDataModel *res = NULL; GdaDataModel *res = NULL;
GdaDataModelIter *iter; GdaDataModelIter *iter;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading planes"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading planes");
if (dbh == NULL) { if (dbh == NULL) {
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
"Database backend not initialized");
return FALSE; return FALSE;
} }
sth = gda_sql_parser_parse_string(parser, "SELECT id, name FROM planes", NULL, NULL); sth = gda_sql_parser_parse_string(parser,
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL); "SELECT id, name FROM planes",
iter = gda_data_model_create_iter(res); NULL,
gda_data_model_iter_move_next(iter); NULL);
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL);
iter = gda_data_model_create_iter(res);
gda_data_model_iter_move_next(iter);
while (gda_data_model_iter_is_valid(iter)) { while (gda_data_model_iter_is_valid(iter)) {
const GValue *val; const GValue *val;
wmudPlane *plane; wmudPlane *plane;
plane = g_new0(wmudPlane, 1); plane = g_new0(wmudPlane, 1);
val = gda_data_model_iter_get_value_at(iter, 0); val = gda_data_model_iter_get_value_at(iter, 0);
plane->id = g_value_get_int(val); plane->id = g_value_get_int(val);
val = gda_data_model_iter_get_value_at(iter, 1); val = gda_data_model_iter_get_value_at(iter, 1);
plane->name = g_strdup(g_value_get_string(val)); plane->name = g_strdup(g_value_get_string(val));
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded plane _%s_", plane->name); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"Loaded plane _%s_",
plane->name);
*planes = g_slist_prepend(*planes, plane); *planes = g_slist_prepend(*planes, plane);
gda_data_model_iter_move_next(iter); gda_data_model_iter_move_next(iter);
} }
g_object_unref(iter); g_object_unref(iter);
g_object_unref(sth); g_object_unref(sth);
return TRUE; return TRUE;
} }
gboolean gboolean
wmud_db_load_planets(GSList **planets, GError **err) wmud_db_load_planets(GSList **planets, GError **err)
{ {
GdaStatement *sth = NULL; GdaStatement *sth = NULL;
GdaDataModel *res = NULL; GdaDataModel *res = NULL;
GdaDataModelIter *iter; GdaDataModelIter *iter;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading planets"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading planets");
if (dbh == NULL) { if (dbh == NULL) {
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
"Database backend not initialized");
return FALSE; return FALSE;
} }
sth = gda_sql_parser_parse_string(parser, "SELECT id, name FROM planets", NULL, NULL); sth = gda_sql_parser_parse_string(parser,
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL); "SELECT id, name FROM planets",
iter = gda_data_model_create_iter(res); NULL,
gda_data_model_iter_move_next(iter); NULL);
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL);
iter = gda_data_model_create_iter(res);
gda_data_model_iter_move_next(iter);
while (gda_data_model_iter_is_valid(iter)) { while (gda_data_model_iter_is_valid(iter)) {
const GValue *val; const GValue *val;
wmudPlanet *planet; wmudPlanet *planet;
planet = g_new0(wmudPlanet, 1); planet = g_new0(wmudPlanet, 1);
val = gda_data_model_iter_get_value_at(iter, 0); val = gda_data_model_iter_get_value_at(iter, 0);
planet->id = g_value_get_int(val); planet->id = g_value_get_int(val);
val = gda_data_model_iter_get_value_at(iter, 1); val = gda_data_model_iter_get_value_at(iter, 1);
planet->name = g_strdup(g_value_get_string(val)); planet->name = g_strdup(g_value_get_string(val));
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded planet _%s_", planet->name); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"Loaded planet _%s_",
planet->name);
*planets = g_slist_prepend(*planets, planet); *planets = g_slist_prepend(*planets, planet);
gda_data_model_iter_move_next(iter); gda_data_model_iter_move_next(iter);
} }
g_object_unref(iter); g_object_unref(iter);
g_object_unref(sth); g_object_unref(sth);
return TRUE; return TRUE;
} }
gboolean gboolean
wmud_db_load_directions(GSList **directions, GError **err) wmud_db_load_directions(GSList **directions, GError **err)
{ {
GdaStatement *sth = NULL; GdaStatement *sth = NULL;
GdaDataModel *res = NULL; GdaDataModel *res = NULL;
GdaDataModelIter *iter; GdaDataModelIter *iter;
GError *local_err = NULL; GError *local_err = NULL;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading directions"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading directions");
if (dbh == NULL) { if (dbh == NULL) {
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
"Database backend not initialized");
return FALSE; return FALSE;
} }
sth = gda_sql_parser_parse_string(parser, "SELECT id, short_name, name FROM directions", NULL, NULL); sth = gda_sql_parser_parse_string(
parser,
"SELECT id, short_name, name FROM directions",
NULL,
NULL);
if ((res = gda_connection_statement_execute_select(dbh, sth, NULL, &local_err)) == NULL) { if ((res = gda_connection_statement_execute_select(
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_SELECT_ERROR, "Unable to load directions: %s", local_err->message); dbh, sth,
NULL, &local_err)) == NULL) {
g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_SELECT_ERROR,
"Unable to load directions: %s",
local_err->message);
return FALSE; return FALSE;
} }
iter = gda_data_model_create_iter(res); iter = gda_data_model_create_iter(res);
gda_data_model_iter_move_next(iter); gda_data_model_iter_move_next(iter);
while (gda_data_model_iter_is_valid(iter)) { while (gda_data_model_iter_is_valid(iter)) {
const GValue *val; const GValue *val;
wmudDirection *dir; wmudDirection *dir;
dir = g_new0(wmudDirection, 1); dir = g_new0(wmudDirection, 1);
val = gda_data_model_iter_get_value_at(iter, 0); val = gda_data_model_iter_get_value_at(iter, 0);
dir->id = g_value_get_int(val); dir->id = g_value_get_int(val);
val = gda_data_model_iter_get_value_at(iter, 1); val = gda_data_model_iter_get_value_at(iter, 1);
dir->short_name = g_strdup(g_value_get_string(val)); dir->short_name = g_strdup(g_value_get_string(val));
val = gda_data_model_iter_get_value_at(iter, 2); val = gda_data_model_iter_get_value_at(iter, 2);
dir->name = g_strdup(g_value_get_string(val)); dir->name = g_strdup(g_value_get_string(val));
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded direction _%s_", dir->name); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"Loaded direction _%s_",
dir->name);
*directions = g_slist_prepend(*directions, dir); *directions = g_slist_prepend(*directions, dir);
gda_data_model_iter_move_next(iter); gda_data_model_iter_move_next(iter);
} }
g_object_unref(iter); g_object_unref(iter);
g_object_unref(sth); g_object_unref(sth);
return TRUE; return TRUE;
} }
gboolean gboolean
wmud_db_load_areas(GSList **areas, GError **err) wmud_db_load_areas(GSList **areas, GError **err)
{ {
GdaStatement *sth = NULL; GdaStatement *sth = NULL;
GdaDataModel *res = NULL; GdaDataModel *res = NULL;
GdaDataModelIter *iter; GdaDataModelIter *iter;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading areas"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading areas");
if (dbh == NULL) { if (dbh == NULL) {
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
"Database backend not initialized");
return FALSE; return FALSE;
} }
sth = gda_sql_parser_parse_string(parser, "SELECT id, name FROM areas", NULL, NULL); sth = gda_sql_parser_parse_string(
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL); parser,
iter = gda_data_model_create_iter(res); "SELECT id, name FROM areas",
gda_data_model_iter_move_next(iter); NULL,
NULL);
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL);
iter = gda_data_model_create_iter(res);
gda_data_model_iter_move_next(iter);
while (gda_data_model_iter_is_valid(iter)) { while (gda_data_model_iter_is_valid(iter)) {
const GValue *val; const GValue *val;
wmudArea *area; wmudArea *area;
area = g_new0(wmudArea, 1); area = g_new0(wmudArea, 1);
val = gda_data_model_iter_get_value_at(iter, 0); val = gda_data_model_iter_get_value_at(iter, 0);
area->id = g_value_get_int(val); area->id = g_value_get_int(val);
val = gda_data_model_iter_get_value_at(iter, 1); val = gda_data_model_iter_get_value_at(iter, 1);
area->name = g_strdup(g_value_get_string(val)); area->name = g_strdup(g_value_get_string(val));
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded area _%s_", area->name); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded area _%s_", area->name);
*areas = g_slist_prepend(*areas, area); *areas = g_slist_prepend(*areas, area);
gda_data_model_iter_move_next(iter); gda_data_model_iter_move_next(iter);
} }
g_object_unref(iter); g_object_unref(iter);
g_object_unref(sth); g_object_unref(sth);
return TRUE; return TRUE;
} }
gboolean gboolean
wmud_db_load_rooms(GSList **rooms, GError **err) wmud_db_load_rooms(GSList **rooms, GError **err)
{ {
GdaStatement *sth = NULL; GdaStatement *sth = NULL;
GdaDataModel *res = NULL; GdaDataModel *res = NULL;
GdaDataModelIter *iter; GdaDataModelIter *iter;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading rooms"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading rooms");
if (dbh == NULL) { if (dbh == NULL) {
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
"Database backend not initialized");
return FALSE; return FALSE;
} }
sth = gda_sql_parser_parse_string(parser, "SELECT id, area, name, distant_description, close_description FROM rooms", NULL, NULL); sth = gda_sql_parser_parse_string(
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL); parser,
iter = gda_data_model_create_iter(res); "SELECT id, area, name, distant_description, close_description FROM rooms",
gda_data_model_iter_move_next(iter); NULL,
NULL);
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL);
iter = gda_data_model_create_iter(res);
gda_data_model_iter_move_next(iter);
while (gda_data_model_iter_is_valid(iter)) { while (gda_data_model_iter_is_valid(iter)) {
const GValue *val; const GValue *val;
wmudRoom *room; wmudRoom *room;
room = g_new0(wmudRoom, 1); room = g_new0(wmudRoom, 1);
val = gda_data_model_iter_get_value_at(iter, 0); val = gda_data_model_iter_get_value_at(iter, 0);
room->id = g_value_get_int(val); room->id = g_value_get_int(val);
val = gda_data_model_iter_get_value_at(iter, 1); val = gda_data_model_iter_get_value_at(iter, 1);
room->area_id = g_value_get_int(val); room->area_id = g_value_get_int(val);
val = gda_data_model_iter_get_value_at(iter, 2); val = gda_data_model_iter_get_value_at(iter, 2);
room->name = g_strdup(g_value_get_string(val)); room->name = g_strdup(g_value_get_string(val));
val = gda_data_model_iter_get_value_at(iter, 3); val = gda_data_model_iter_get_value_at(iter, 3);
room->distant_description = g_strdup(g_value_get_string(val)); room->distant_description = g_strdup(g_value_get_string(val));
val = gda_data_model_iter_get_value_at(iter, 4); val = gda_data_model_iter_get_value_at(iter, 4);
room->close_description = g_strdup(g_value_get_string(val)); room->close_description = g_strdup(g_value_get_string(val));
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded room %d/_%s_", room->area_id, room->name); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"Loaded room %d/_%s_",
room->area_id,
room->name);
*rooms = g_slist_prepend(*rooms, room); *rooms = g_slist_prepend(*rooms, room);
gda_data_model_iter_move_next(iter); gda_data_model_iter_move_next(iter);
} }
g_object_unref(iter); g_object_unref(iter);
g_object_unref(sth); g_object_unref(sth);
return TRUE; return TRUE;
} }
gboolean gboolean
wmud_db_load_exits(GSList **exits, GError **err) wmud_db_load_exits(GSList **exits, GError **err)
{ {
GdaStatement *sth = NULL; GdaStatement *sth = NULL;
GdaDataModel *res = NULL; GdaDataModel *res = NULL;
GdaDataModelIter *iter; GdaDataModelIter *iter;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading exits"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading exits");
if (dbh == NULL) { if (dbh == NULL) {
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
"Database backend not initialized");
return FALSE; return FALSE;
} }
sth = gda_sql_parser_parse_string(parser, "SELECT room_id, direction, other_side FROM room_exits", NULL, NULL); sth = gda_sql_parser_parse_string(
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL); parser,
iter = gda_data_model_create_iter(res); "SELECT room_id, direction, other_side FROM room_exits",
gda_data_model_iter_move_next(iter); NULL,
NULL);
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL);
iter = gda_data_model_create_iter(res);
gda_data_model_iter_move_next(iter);
while (gda_data_model_iter_is_valid(iter)) { while (gda_data_model_iter_is_valid(iter)) {
const GValue *val; const GValue *val;
wmudExit *room_exit; wmudExit *room_exit;
room_exit = g_new0(wmudExit, 1); room_exit = g_new0(wmudExit, 1);
val = gda_data_model_iter_get_value_at(iter, 0); val = gda_data_model_iter_get_value_at(iter, 0);
room_exit->source_room_id = g_value_get_int(val); room_exit->source_room_id = g_value_get_int(val);
val = gda_data_model_iter_get_value_at(iter, 1); val = gda_data_model_iter_get_value_at(iter, 1);
room_exit->direction_id = g_value_get_int(val); room_exit->direction_id = g_value_get_int(val);
val = gda_data_model_iter_get_value_at(iter, 2); val = gda_data_model_iter_get_value_at(iter, 2);
room_exit->destination_room_id = g_value_get_int(val); room_exit->destination_room_id = g_value_get_int(val);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded exit %d =%d=> %d", room_exit->source_room_id, room_exit->direction_id, room_exit->destination_room_id); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"Loaded exit %d =%d=> %d",
room_exit->source_room_id,
room_exit->direction_id,
room_exit->destination_room_id);
*exits = g_slist_prepend(*exits, room_exit); *exits = g_slist_prepend(*exits, room_exit);
gda_data_model_iter_move_next(iter); gda_data_model_iter_move_next(iter);
} }
g_object_unref(iter); g_object_unref(iter);
g_object_unref(sth); g_object_unref(sth);
return TRUE; return TRUE;
} }
gboolean gboolean
wmud_db_load_planet_planes(GSList **planet_planes, GError **err) wmud_db_load_planet_planes(GSList **planet_planes, GError **err)
{ {
GdaStatement *sth = NULL; GdaStatement *sth = NULL;
GdaDataModel *res = NULL; GdaDataModel *res = NULL;
GdaDataModelIter *iter; GdaDataModelIter *iter;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading planet<->plane associations"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
if (dbh == NULL) { "Loading planet<->plane associations");
if (err) if (dbh == NULL) {
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); if (err)
g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
"Database backend not initialized");
return FALSE; return FALSE;
} }
sth = gda_sql_parser_parse_string(parser, "SELECT planet_id, plane_id FROM planet_planes", NULL, NULL); sth = gda_sql_parser_parse_string(
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL); parser,
iter = gda_data_model_create_iter(res); "SELECT planet_id, plane_id FROM planet_planes",
gda_data_model_iter_move_next(iter); NULL,
NULL);
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL);
iter = gda_data_model_create_iter(res);
gda_data_model_iter_move_next(iter);
while (gda_data_model_iter_is_valid(iter)) { while (gda_data_model_iter_is_valid(iter)) {
const GValue *val; const GValue *val;
wmudPlanetPlaneAssoc *planet_plane; wmudPlanetPlaneAssoc *planet_plane;
planet_plane = g_new0(wmudPlanetPlaneAssoc, 1); planet_plane = g_new0(wmudPlanetPlaneAssoc, 1);
val = gda_data_model_iter_get_value_at(iter, 0); val = gda_data_model_iter_get_value_at(iter, 0);
planet_plane->planet_id = g_value_get_int(val); planet_plane->planet_id = g_value_get_int(val);
val = gda_data_model_iter_get_value_at(iter, 1); val = gda_data_model_iter_get_value_at(iter, 1);
planet_plane->plane_id = g_value_get_int(val); planet_plane->plane_id = g_value_get_int(val);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded planet-plane association %d <> %d", planet_plane->planet_id, planet_plane->plane_id); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"Loaded planet-plane association %d <> %d",
planet_plane->planet_id,
planet_plane->plane_id);
*planet_planes = g_slist_prepend(*planet_planes, planet_plane); *planet_planes = g_slist_prepend(*planet_planes, planet_plane);
gda_data_model_iter_move_next(iter); gda_data_model_iter_move_next(iter);
} }
g_object_unref(iter); g_object_unref(iter);
g_object_unref(sth); g_object_unref(sth);
return TRUE; return TRUE;
} }
gboolean gboolean
wmud_db_load_menu(GSList **menu_items, GError **err) wmud_db_load_menu(GSList **menu_items, GError **err)
{ {
GdaStatement *sth = NULL; GdaStatement *sth = NULL;
GdaDataModel *res = NULL; GdaDataModel *res = NULL;
GdaDataModelIter *iter; GdaDataModelIter *iter;
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading menu items"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loading menu items");
if (dbh == NULL) { if (dbh == NULL) {
if (err) if (err)
g_set_error(err, WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT, "Database backend not initialized"); g_set_error(err,
WMUD_DB_ERROR, WMUD_DB_ERROR_NOINIT,
"Database backend not initialized");
return FALSE; return FALSE;
} }
sth = gda_sql_parser_parse_string(parser, "SELECT id, menuchar, need_active_char, placement, display_text, fnctn FROM menu ORDER BY placement", NULL, NULL); sth = gda_sql_parser_parse_string(
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL); parser,
iter = gda_data_model_create_iter(res); "SELECT id, menuchar, need_active_char, placement, display_text, fnctn FROM menu ORDER BY placement",
gda_data_model_iter_move_next(iter); NULL,
NULL);
res = gda_connection_statement_execute_select(dbh, sth, NULL, NULL);
iter = gda_data_model_create_iter(res);
gda_data_model_iter_move_next(iter);
while (gda_data_model_iter_is_valid(iter)) { while (gda_data_model_iter_is_valid(iter)) {
const GValue *val; const GValue *val;
wmudMenu *menu_item; wmudMenu *menu_item;
menu_item = g_new0(wmudMenu, 1); menu_item = g_new0(wmudMenu, 1);
val = gda_data_model_iter_get_value_at(iter, 0); val = gda_data_model_iter_get_value_at(iter, 0);
menu_item->id = g_value_get_int(val); menu_item->id = g_value_get_int(val);
val = gda_data_model_iter_get_value_at(iter, 1); val = gda_data_model_iter_get_value_at(iter, 1);
menu_item->menuchar = *(g_value_get_string(val)); menu_item->menuchar = *(g_value_get_string(val));
val = gda_data_model_iter_get_value_at(iter, 2); val = gda_data_model_iter_get_value_at(iter, 2);
menu_item->need_active_char = g_value_get_boolean(val); menu_item->need_active_char = g_value_get_boolean(val);
val = gda_data_model_iter_get_value_at(iter, 3); val = gda_data_model_iter_get_value_at(iter, 3);
menu_item->placement = g_value_get_int(val); menu_item->placement = g_value_get_int(val);
val = gda_data_model_iter_get_value_at(iter, 4); val = gda_data_model_iter_get_value_at(iter, 4);
menu_item->text = g_strdup(g_value_get_string(val)); menu_item->text = g_strdup(g_value_get_string(val));
val = gda_data_model_iter_get_value_at(iter, 5); val = gda_data_model_iter_get_value_at(iter, 5);
menu_item->func = g_strdup(g_value_get_string(val)); menu_item->func = g_strdup(g_value_get_string(val));
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Loaded menu item %d: %s(%c)", menu_item->id, menu_item->text, menu_item->menuchar); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG,
"Loaded menu item %d: %s(%c)",
menu_item->id, menu_item->text,
menu_item->menuchar);
*menu_items = g_slist_prepend(*menu_items, menu_item); *menu_items = g_slist_prepend(*menu_items, menu_item);
gda_data_model_iter_move_next(iter); gda_data_model_iter_move_next(iter);
} }
g_object_unref(iter); g_object_unref(iter);
g_object_unref(sth); g_object_unref(sth);
return TRUE; return TRUE;
} }

View File

@ -12,27 +12,28 @@
GType GType
@enum_name@_get_type (void) @enum_name@_get_type (void)
{ {
static volatile gsize g_define_type_id__volatile = 0; static volatile gsize g_define_type_id__volatile = 0;
if (g_once_init_enter(&g_define_type_id__volatile)) { if (g_once_init_enter(&g_define_type_id__volatile)) {
static const G@Type@Value values[] = { static const G@Type@Value values[] = {
/*** END value-header ***/ /*** END value-header ***/
/*** BEGIN value-production ***/ /*** BEGIN value-production ***/
{ @VALUENAME@, "@VALUENAME@", "@valuenick@" }, { @VALUENAME@, "@VALUENAME@", "@valuenick@" },
/*** END value-production ***/ /*** END value-production ***/
/*** BEGIN value-tail ***/ /*** BEGIN value-tail ***/
{ 0, NULL, NULL } { 0, NULL, NULL }
}; };
GType g_define_type_id = g_@type@_register_static(g_intern_static_string("@EnumName@"), values); GType g_define_type_id = g_@type@_register_static(
g_intern_static_string("@EnumName@"),
values);
g_once_init_leave (&g_define_type_id__volatile, g_define_type_id); g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
} }
return g_define_type_id__volatile; return g_define_type_id__volatile;
} }
/*** END value-tail ***/ /*** END value-tail ***/

View File

@ -45,8 +45,8 @@
*/ */
struct AcceptData { struct AcceptData {
GMainContext *context; GMainContext *context;
GSocketListener *listener; GSocketListener *listener;
}; };
/** /**
@ -70,14 +70,14 @@ static void state_regemail_confirm(WmudClient *client);
static void static void
remove_client(WmudClient *client, gboolean send_quitmessage) remove_client(WmudClient *client, gboolean send_quitmessage)
{ {
clients = g_slist_remove(clients, client); clients = g_slist_remove(clients, client);
wmud_client_close(client, send_quitmessage); wmud_client_close(client, send_quitmessage);
} }
static void static void
hup_client(WmudClient *client) hup_client(WmudClient *client)
{ {
remove_client(client, FALSE); remove_client(client, FALSE);
} }
/** /**
@ -91,103 +91,123 @@ hup_client(WmudClient *client)
static void static void
recv_client(WmudClient *client) recv_client(WmudClient *client)
{ {
GError *err = NULL; GError *err = NULL;
GSocket *client_socket; GSocket *client_socket;
gssize len; gssize len;
gchar *buf2; gchar *buf2;
gchar *buf = g_malloc0(sizeof(gchar) * (MAX_RECV_LEN + 1)); gchar *buf = g_malloc0(sizeof(gchar) * (MAX_RECV_LEN + 1));
client_socket = wmud_client_get_socket(client); client_socket = wmud_client_get_socket(client);
if ((len = g_socket_receive(client_socket, buf, MAX_RECV_LEN, NULL, &err)) == 0) { if ((len = g_socket_receive(client_socket,
g_free(buf); buf,
remove_client(client, FALSE); MAX_RECV_LEN,
NULL,
&err)) == 0) {
g_free(buf);
remove_client(client, FALSE);
return; return;
} }
buf2 = buf; buf2 = buf;
while (TRUE) { while (TRUE) {
char *r = strchr((char *)buf2, '\r'), char *r = strchr((char *)buf2, '\r'),
*n = strchr((char *)buf2, '\n'); *n = strchr((char *)buf2, '\n');
if (r || n) { if (r || n) {
gint i, gint i,
sloc = -1; sloc = -1;
if ((r < n) && r) { if ((r < n) && r) {
if (wmud_client_get_buffer_length(client) > 0) if (wmud_client_get_buffer_length(client) > 0)
g_string_append_len(wmud_client_get_buffer(client), buf2, (r - buf2)); g_string_append_len(wmud_client_get_buffer(client),
else buf2,
g_string_overwrite_len(wmud_client_get_buffer(client), 0, buf2, (r - buf2)); (r - buf2));
buf2 = r; else
} else if (n) { g_string_overwrite_len(
if (wmud_client_get_buffer_length(client) > 0) wmud_client_get_buffer(client),
g_string_append_len(wmud_client_get_buffer(client), buf2, (n - buf2)); 0,
else buf2,
g_string_overwrite_len(wmud_client_get_buffer(client), 0, buf2, (n - buf2)); (r - buf2));
buf2 = n; buf2 = r;
} } else if (n) {
if (wmud_client_get_buffer_length(client) > 0)
g_string_append_len(
wmud_client_get_buffer(client),
buf2,
(n - buf2));
else
g_string_overwrite_len(
wmud_client_get_buffer(client),
0,
buf2,
(n - buf2));
buf2 = n;
}
/* Remove telnet codes from the string */ /* Remove telnet codes from the string */
for (i = 0; i < wmud_client_get_buffer_length(client); i++) { for (i = 0; i < wmud_client_get_buffer_length(client); i++) {
guchar c = (wmud_client_get_buffer(client)->str)[i]; guchar c = (wmud_client_get_buffer(client)->str)[i];
if ((c >= 240) || (c == 1)) { if ((c >= 240) || (c == 1)) {
if (sloc == -1) if (sloc == -1)
sloc = i; sloc = i;
} else { } else {
if (sloc != -1) { if (sloc != -1) {
g_string_erase(wmud_client_get_buffer(client), sloc, i - sloc); g_string_erase(
sloc = -1; wmud_client_get_buffer(client),
} sloc,
} i - sloc);
} sloc = -1;
}
}
}
if (sloc != -1) if (sloc != -1)
g_string_erase(wmud_client_get_buffer(client), sloc, -1); g_string_erase(wmud_client_get_buffer(client), sloc, -1);
switch (wmud_client_get_state(client)) switch (wmud_client_get_state(client))
{ {
case WMUD_CLIENT_STATE_FRESH: case WMUD_CLIENT_STATE_FRESH:
state_fresh(client); state_fresh(client);
break; break;
case WMUD_CLIENT_STATE_PASSWAIT: case WMUD_CLIENT_STATE_PASSWAIT:
state_passwait(client); state_passwait(client);
break; break;
case WMUD_CLIENT_STATE_MENU: case WMUD_CLIENT_STATE_MENU:
state_menu(client); state_menu(client);
break; break;
case WMUD_CLIENT_STATE_INGAME: case WMUD_CLIENT_STATE_INGAME:
wmud_interpret_game_command(client); wmud_interpret_game_command(client);
break; break;
case WMUD_CLIENT_STATE_YESNO: case WMUD_CLIENT_STATE_YESNO:
state_yesno(client); state_yesno(client);
break; break;
case WMUD_CLIENT_STATE_REGISTERING: case WMUD_CLIENT_STATE_REGISTERING:
state_registering(client); state_registering(client);
break; break;
case WMUD_CLIENT_STATE_REGEMAIL_CONFIRM: case WMUD_CLIENT_STATE_REGEMAIL_CONFIRM:
state_regemail_confirm(client); state_regemail_confirm(client);
break; break;
} }
g_string_erase(wmud_client_get_buffer(client), 0, -1); g_string_erase(wmud_client_get_buffer(client), 0, -1);
for (; ((*buf2 == '\r') || (*buf2 == '\n')) && *buf2; buf2++); for (; ((*buf2 == '\r') || (*buf2 == '\n')) && *buf2; buf2++);
if (!*buf2) if (!*buf2)
break; break;
} else { } else {
if (wmud_client_get_buffer_length(client) > 0) if (wmud_client_get_buffer_length(client) > 0)
g_string_append(wmud_client_get_buffer(client), buf2); g_string_append(wmud_client_get_buffer(client), buf2);
else else
g_string_overwrite(wmud_client_get_buffer(client), 0, buf2); g_string_overwrite(wmud_client_get_buffer(client), 0, buf2);
break; break;
} }
} }
g_free(buf); g_free(buf);
} }
/** /**
@ -202,48 +222,50 @@ recv_client(WmudClient *client)
* Return value: this function always returns %TRUE * Return value: this function always returns %TRUE
*/ */
gboolean gboolean
game_source_callback(GSocket *socket, GIOCondition condition, struct AcceptData *accept_data) game_source_callback(GSocket *socket,
GIOCondition condition,
struct AcceptData *accept_data)
{ {
GSocket *client_socket; GSocket *client_socket;
GError *err = NULL; GError *err = NULL;
GSocketAddress *remote_addr; GSocketAddress *remote_addr;
WmudClient *client; WmudClient *client;
/* This function should never return an error. If so, it is a huge bug, /* This function should never return an error. If so, it is a huge bug,
* and will trigger a higher level error. */ * and will trigger a higher level error. */
client_socket = g_socket_listener_accept_socket(accept_data->listener, NULL, NULL, &err); client_socket = g_socket_listener_accept_socket(accept_data->listener, NULL, NULL, &err);
client = wmud_client_new(); client = wmud_client_new();
wmud_client_set_socket(WMUD_CLIENT(client), client_socket); wmud_client_set_socket(WMUD_CLIENT(client), client_socket);
wmud_client_set_context(client, accept_data->context); wmud_client_set_context(client, accept_data->context);
g_signal_connect(client, "net-hup", G_CALLBACK(hup_client), NULL); g_signal_connect(client, "net-hup", G_CALLBACK(hup_client), NULL);
g_signal_connect(client, "net-recv", G_CALLBACK(recv_client), NULL); g_signal_connect(client, "net-recv", G_CALLBACK(recv_client), NULL);
clients = g_slist_prepend(clients, client); clients = g_slist_prepend(clients, client);
g_clear_error(&err); g_clear_error(&err);
if ((remote_addr = g_socket_get_remote_address(client_socket, &err)) != NULL) { if ((remote_addr = g_socket_get_remote_address(client_socket, &err)) != NULL) {
GInetAddress *addr; GInetAddress *addr;
gchar *ip_addr; gchar *ip_addr;
addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(remote_addr)); addr = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(remote_addr));
ip_addr = g_inet_address_to_string(addr); ip_addr = g_inet_address_to_string(addr);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "New game connection from %s", ip_addr); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_INFO, "New game connection from %s", ip_addr);
g_free(ip_addr); g_free(ip_addr);
g_object_unref(addr); g_object_unref(addr);
g_object_unref(remote_addr); g_object_unref(remote_addr);
} else { } else {
if (err) if (err)
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "New game connection. The remote address is unknown. This is a bug. Message from upper level: %s", err->message); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "New game connection. The remote address is unknown. This is a bug. Message from upper level: %s", err->message);
else else
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "New game connection. The remote address is unknown. This is a bug."); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "New game connection. The remote address is unknown. This is a bug.");
} }
g_clear_error(&err); g_clear_error(&err);
wmud_client_send(WMUD_CLIENT(client), "By what name shall we call you? "); wmud_client_send(WMUD_CLIENT(client), "By what name shall we call you? ");
return TRUE; return TRUE;
} }
/** /**
@ -261,190 +283,190 @@ game_source_callback(GSocket *socket, GIOCondition condition, struct AcceptData
gboolean gboolean
wmud_networking_init(guint port_number, GMainContext *game_context, GSList *menu_items, GError **err) wmud_networking_init(guint port_number, GMainContext *game_context, GSList *menu_items, GError **err)
{ {
struct AcceptData *accept_data; struct AcceptData *accept_data;
GSocketListener *game_listener; GSocketListener *game_listener;
gboolean need_ipv4_socket = TRUE; gboolean need_ipv4_socket = TRUE;
GSocket *game_socket6, GSocket *game_socket6,
*game_socket4; *game_socket4;
GError *in_err = NULL; GError *in_err = NULL;
GSource *game_net_source4 = NULL, GSource *game_net_source4 = NULL,
*game_net_source6 = NULL; *game_net_source6 = NULL;
clients = NULL; clients = NULL;
game_listener = g_socket_listener_new(); game_listener = g_socket_listener_new();
/* The following snippet is borrowed from GLib 2.30's gsocketlistener.c /* The following snippet is borrowed from GLib 2.30's gsocketlistener.c
* code, to create the necessary sockets to listen on both IPv4 and * code, to create the necessary sockets to listen on both IPv4 and
* IPv6 address */ * IPv6 address */
if ((game_socket6 = g_socket_new(G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL)) != NULL) { if ((game_socket6 = g_socket_new(G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL)) != NULL) {
GInetAddress *inet_address; GInetAddress *inet_address;
GSocketAddress *address; GSocketAddress *address;
gboolean result; gboolean result;
inet_address = g_inet_address_new_any(G_SOCKET_FAMILY_IPV6); inet_address = g_inet_address_new_any(G_SOCKET_FAMILY_IPV6);
address = g_inet_socket_address_new(inet_address, port_number); address = g_inet_socket_address_new(inet_address, port_number);
g_object_unref(inet_address); g_object_unref(inet_address);
g_socket_set_listen_backlog(game_socket6, 10); g_socket_set_listen_backlog(game_socket6, 10);
result = result =
g_socket_bind(game_socket6, address, TRUE, NULL) g_socket_bind(game_socket6, address, TRUE, NULL)
&& g_socket_listen(game_socket6, NULL); && g_socket_listen(game_socket6, NULL);
g_object_unref(address); g_object_unref(address);
if (!result) { if (!result) {
g_object_unref(game_socket6); g_object_unref(game_socket6);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Unable to create listener IPv6 socket"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Unable to create listener IPv6 socket");
return FALSE; return FALSE;
} }
if (g_socket_speaks_ipv4(game_socket6)) if (g_socket_speaks_ipv4(game_socket6))
need_ipv4_socket = FALSE; need_ipv4_socket = FALSE;
game_net_source6 = g_socket_create_source(game_socket6, G_IO_IN, NULL); game_net_source6 = g_socket_create_source(game_socket6, G_IO_IN, NULL);
/* This function should never return error. If so, that would be a /* This function should never return error. If so, that would be a
* really big bug which will trigger a higher level problem for sure */ * really big bug which will trigger a higher level problem for sure */
g_socket_listener_add_socket(game_listener, game_socket6, NULL, NULL); g_socket_listener_add_socket(game_listener, game_socket6, NULL, NULL);
} }
if (need_ipv4_socket) { if (need_ipv4_socket) {
if ((game_socket4 = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL)) != NULL) { if ((game_socket4 = g_socket_new(G_SOCKET_FAMILY_IPV4, G_SOCKET_TYPE_STREAM, G_SOCKET_PROTOCOL_DEFAULT, NULL)) != NULL) {
GInetAddress *inet_address; GInetAddress *inet_address;
GSocketAddress *address; GSocketAddress *address;
gboolean result; gboolean result;
inet_address = g_inet_address_new_any(G_SOCKET_FAMILY_IPV4); inet_address = g_inet_address_new_any(G_SOCKET_FAMILY_IPV4);
address = g_inet_socket_address_new(inet_address, port_number); address = g_inet_socket_address_new(inet_address, port_number);
g_object_unref(inet_address); g_object_unref(inet_address);
g_socket_set_listen_backlog(game_socket4, 10); g_socket_set_listen_backlog(game_socket4, 10);
result = g_socket_bind(game_socket4, address, TRUE, NULL) result = g_socket_bind(game_socket4, address, TRUE, NULL)
&& g_socket_listen(game_socket4, NULL); && g_socket_listen(game_socket4, NULL);
g_object_unref(address); g_object_unref(address);
if (!result) { if (!result) {
g_object_unref(game_socket4); g_object_unref(game_socket4);
if (!game_socket6) if (!game_socket6)
g_object_unref(game_socket6); g_object_unref(game_socket6);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Unable to create listener IPv4 socket!\n"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_WARNING, "Unable to create listener IPv4 socket!\n");
return FALSE; return FALSE;
} }
game_net_source4 = g_socket_create_source(game_socket4, G_IO_IN, NULL); game_net_source4 = g_socket_create_source(game_socket4, G_IO_IN, NULL);
g_socket_listener_add_socket(game_listener, game_socket4, NULL, NULL); g_socket_listener_add_socket(game_listener, game_socket4, NULL, NULL);
} }
} else { } else {
if (game_socket6 != NULL) if (game_socket6 != NULL)
g_clear_error(&in_err); g_clear_error(&in_err);
else else
return FALSE; return FALSE;
} }
accept_data = g_new(struct AcceptData, 1); accept_data = g_new(struct AcceptData, 1);
accept_data->listener = game_listener; accept_data->listener = game_listener;
accept_data->context = game_context; accept_data->context = game_context;
if (game_net_source6) { if (game_net_source6) {
g_source_set_callback(game_net_source6, (GSourceFunc)game_source_callback, (gpointer)accept_data, NULL); g_source_set_callback(game_net_source6, (GSourceFunc)game_source_callback, (gpointer)accept_data, NULL);
g_source_attach(game_net_source6, game_context); g_source_attach(game_net_source6, game_context);
} }
if (game_net_source4) { if (game_net_source4) {
g_source_set_callback(game_net_source4, (GSourceFunc)game_source_callback, (gpointer)accept_data, NULL); g_source_set_callback(game_net_source4, (GSourceFunc)game_source_callback, (gpointer)accept_data, NULL);
g_source_attach(game_net_source4, game_context); g_source_attach(game_net_source4, game_context);
} }
game_menu = menu_items; game_menu = menu_items;
email_regex = g_regex_new("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$", G_REGEX_CASELESS, 0, NULL); email_regex = g_regex_new("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,4}$", G_REGEX_CASELESS, 0, NULL);
return TRUE; return TRUE;
} }
void void
wmud_client_quitanswer(WmudClient *client, gboolean answer) wmud_client_quitanswer(WmudClient *client, gboolean answer)
{ {
if (answer) { if (answer) {
remove_client(client, TRUE); remove_client(client, TRUE);
} else { } else {
wmud_client_send(client, "Good boy!\r\n"); wmud_client_send(client, "Good boy!\r\n");
wmud_client_set_state(client, WMUD_CLIENT_STATE_MENU); wmud_client_set_state(client, WMUD_CLIENT_STATE_MENU);
} }
} }
void void
wmud_client_newchar_answer(WmudClient *client, gboolean answer) wmud_client_newchar_answer(WmudClient *client, gboolean answer)
{ {
if (answer) { if (answer) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Creating new player\n"); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "Creating new player\n");
wmud_client_send(client, "Welcome to this MUD!\r\nPlease enter your e-mail address: "); wmud_client_send(client, "Welcome to this MUD!\r\nPlease enter your e-mail address: ");
wmud_client_set_state(client, WMUD_CLIENT_STATE_REGISTERING); wmud_client_set_state(client, WMUD_CLIENT_STATE_REGISTERING);
} else { } else {
wmud_client_send(client, "What is your player-name, then? "); wmud_client_send(client, "What is your player-name, then? ");
wmud_client_set_state(client, WMUD_CLIENT_STATE_FRESH); wmud_client_set_state(client, WMUD_CLIENT_STATE_FRESH);
} }
} }
static void 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 (wmud_player_get_cpassword(player) == 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);
wmud_player_set_registered(player, 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 = wmud_player_new(); WmudPlayer *player = wmud_player_new();
wmud_player_set_player_name(player, 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);
wmud_client_send(client, "Is %s new to this game? [Y/N] ", wmud_client_get_buffer(client)->str); wmud_client_send(client, "Is %s new to this game? [Y/N] ", wmud_client_get_buffer(client)->str);
} }
} }
} }
static void static void
state_passwait(WmudClient *client) state_passwait(WmudClient *client)
{ {
if (*(wmud_client_get_buffer(client)->str)) { if (*(wmud_client_get_buffer(client)->str)) {
WmudPlayer *player; WmudPlayer *player;
player = wmud_client_get_player(client); player = wmud_client_get_player(client);
if (wmud_player_password_valid(player, wmud_client_get_buffer(client)->str)) { if (wmud_player_password_valid(player, wmud_client_get_buffer(client)->str)) {
gint fail_count; gint fail_count;
GSocketAddress *socket_address; GSocketAddress *socket_address;
GInetAddress *inet_address; GInetAddress *inet_address;
gchar *ip_addr; gchar *ip_addr;
GError *err = NULL; GError *err = NULL;
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);
socket_address = g_socket_get_remote_address(wmud_client_get_socket(client), &err); socket_address = g_socket_get_remote_address(wmud_client_get_socket(client), &err);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "socket family: %d", g_socket_address_get_family(socket_address)); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_DEBUG, "socket family: %d", g_socket_address_get_family(socket_address));
@ -456,120 +478,120 @@ state_passwait(WmudClient *client)
g_free(ip_addr); g_free(ip_addr);
g_object_unref(socket_address); g_object_unref(socket_address);
if ((fail_count = wmud_player_get_fail_count(player)) > 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",
(fail_count == 1) ? "was" : "were", (fail_count == 1) ? "was" : "were",
fail_count, fail_count,
(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);
} else { } else {
wmud_client_send(client, "%c%c%cThis password doesn't" wmud_client_send(client, "%c%c%cThis password doesn't"
" seem to be valid. Let's try it again..." " seem to be valid. Let's try it again..."
" \r\nBy what name would you like to be" " \r\nBy what name would you like to be"
" 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_player_increase_fail_count(player); 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 "
" these bad passwords for" " these bad passwords for"
" too many times. Please" " too many times. Please"
" stop that!\r\n"); " stop that!\r\n");
remove_client(client, TRUE); remove_client(client, TRUE);
/* TODO: Increase IP fail count, and ban IP if it's too high */ /* TODO: Increase IP fail count, and ban IP if it's too high */
} }
/* TODO: Increase and save login fail count */ /* TODO: Increase and save login fail count */
wmud_client_set_player(client, NULL); wmud_client_set_player(client, NULL);
} }
} else { } else {
wmud_client_send(client, "\r\nEmpty passwords are" wmud_client_send(client, "\r\nEmpty passwords are"
" not valid.\r\nTry again: "); " not valid.\r\nTry again: ");
} }
} }
static void static void
state_menu(WmudClient *client) state_menu(WmudClient *client)
{ {
gchar *menu_command; gchar *menu_command;
if ((menu_command = wmud_menu_get_command_by_menuchar(*(wmud_client_get_buffer(client)->str), game_menu)) != NULL) if ((menu_command = wmud_menu_get_command_by_menuchar(*(wmud_client_get_buffer(client)->str), game_menu)) != NULL)
wmud_menu_execute_command(client, menu_command); wmud_menu_execute_command(client, menu_command);
else else
wmud_client_send(client, "Unknown menu command.\r\n"); wmud_client_send(client, "Unknown menu command.\r\n");
} }
static void static void
state_yesno(WmudClient *client) state_yesno(WmudClient *client)
{ {
if (g_ascii_strcasecmp(wmud_client_get_buffer(client)->str, "y") == 0) if (g_ascii_strcasecmp(wmud_client_get_buffer(client)->str, "y") == 0)
(wmud_client_get_yesno_callback(client))(client, TRUE); (wmud_client_get_yesno_callback(client))(client, TRUE);
else if (g_ascii_strcasecmp(wmud_client_get_buffer(client)->str, "n") == 0) else if (g_ascii_strcasecmp(wmud_client_get_buffer(client)->str, "n") == 0)
(wmud_client_get_yesno_callback(client))(client, FALSE); (wmud_client_get_yesno_callback(client))(client, FALSE);
else else
wmud_client_send(client, "Please enter a 'Y' or 'N'" wmud_client_send(client, "Please enter a 'Y' or 'N'"
" character: "); " character: ");
} }
static void static void
state_registering(WmudClient *client) state_registering(WmudClient *client)
{ {
if (!*(wmud_client_get_buffer(client)->str) && (wmud_client_get_bademail(client) == TRUE)) if (!*(wmud_client_get_buffer(client)->str) && (wmud_client_get_bademail(client) == TRUE))
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_player_set_email(wmud_client_get_player(client), 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"
" write it again? "); " write it again? ");
} else { } else {
wmud_client_send(client, "\r\nSorry, but this" wmud_client_send(client, "\r\nSorry, but this"
"e-mail address doesn't seem to be" "e-mail address doesn't seem to be"
" valid to me.\r\n\r\nIf you think" " valid to me.\r\n\r\nIf you think"
" this is a valid address, simply" " this is a valid address, simply"
" press enter to quit, and send an" " press enter to quit, and send an"
" e-mail to %s from that address," " e-mail to %s from that address,"
" so we can fix our e-mail" " so we can fix our e-mail"
" validation code.\r\n\r\nIf you" " validation code.\r\n\r\nIf you"
" just mistyped your address, type" " just mistyped your address, type"
" it now: ", " it now: ",
active_config->admin_email); active_config->admin_email);
if (*(wmud_client_get_buffer(client)->str)) if (*(wmud_client_get_buffer(client)->str))
wmud_client_set_bademail(client, TRUE); wmud_client_set_bademail(client, TRUE);
} }
} }
static void static void
state_regemail_confirm(WmudClient *client) state_regemail_confirm(WmudClient *client)
{ {
GError *err = NULL; GError *err = NULL;
if (g_ascii_strcasecmp(wmud_player_get_email(wmud_client_get_player(client)), 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, "\r\nGood. 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, "\r\nGood. 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)));
} else { } else {
g_critical("wmud_db_save_player() error: %s", err->message); g_critical("wmud_db_save_player() error: %s", err->message);
wmud_client_send(client, "\r\nThere was an error during the database update. Please try again later!\r\n"); wmud_client_send(client, "\r\nThere was an error during the database update. Please try again later!\r\n");
} }
remove_client(client, TRUE); remove_client(client, TRUE);
} else { } else {
wmud_player_set_email(wmud_client_get_player(client), 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_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

@ -70,17 +70,19 @@ guint32 elapsed_cycle = 0;
gboolean gboolean
rl_sec_elapsed(gpointer user_data) rl_sec_elapsed(gpointer user_data)
{ {
elapsed_ticks++; elapsed_ticks++;
if (elapsed_ticks == G_MAXUINT32) { if (elapsed_ticks == G_MAXUINT32) {
elapsed_ticks = 0; elapsed_ticks = 0;
elapsed_cycle++; elapsed_cycle++;
} }
if (elapsed_ticks % WMUD_HEARTBEAT_LENGTH == 0) { if (elapsed_ticks % WMUD_HEARTBEAT_LENGTH == 0) {
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE, "Heartbeat (%d ticks)", elapsed_ticks); g_log(G_LOG_DOMAIN, G_LOG_LEVEL_MESSAGE,
} "Heartbeat (%d ticks)",
elapsed_ticks);
}
return TRUE; return TRUE;
} }
/** /**
@ -94,36 +96,41 @@ rl_sec_elapsed(gpointer user_data)
gpointer gpointer
game_thread_func(GMainLoop *game_loop) game_thread_func(GMainLoop *game_loop)
{ {
/* Run the game loop */ /* Run the game loop */
g_main_loop_run(game_loop); g_main_loop_run(game_loop);
return NULL; return NULL;
} }
gboolean gboolean
wmud_game_init(GThread **game_thread, GMainContext **game_context) wmud_game_init(GThread **game_thread, GMainContext **game_context)
{ {
GMainLoop *game_loop; GMainLoop *game_loop;
GSource *timeout_source; GSource *timeout_source;
GError *err = NULL; GError *err = NULL;
/* Create the game context and main loop */ /* Create the game context and main loop */
*game_context = g_main_context_new(); *game_context = g_main_context_new();
game_loop = g_main_loop_new(*game_context, FALSE); game_loop = g_main_loop_new(*game_context, FALSE);
/* Create the timeout source which keeps track of elapsed real-world /* Create the timeout source which keeps track of elapsed real-world
* time */ * time */
timeout_source = g_timeout_source_new(WMUD_TICK_LENGTH); timeout_source = g_timeout_source_new(WMUD_TICK_LENGTH);
g_source_set_callback(timeout_source, rl_sec_elapsed, NULL, NULL); g_source_set_callback(timeout_source, rl_sec_elapsed, NULL, NULL);
g_source_attach(timeout_source, *game_context); g_source_attach(timeout_source, *game_context);
g_source_unref(timeout_source); g_source_unref(timeout_source);
g_clear_error(&err); g_clear_error(&err);
#if GLIB_CHECK_VERSION(2, 32, 0) #if GLIB_CHECK_VERSION(2, 32, 0)
*game_thread = g_thread_new("game", (GThreadFunc)game_thread_func, game_loop); *game_thread = g_thread_new("game",
(GThreadFunc)game_thread_func,
game_loop);
#else #else
*game_thread = g_thread_create((GThreadFunc)game_thread_func, game_loop, TRUE, &err); *game_thread = g_thread_create((GThreadFunc)game_thread_func,
game_loop,
TRUE,
&err);
#endif #endif
return TRUE; return TRUE;
} }

View File

@ -52,8 +52,8 @@
* This variable holds the location of the last context marker * This variable holds the location of the last context marker
*/ */
struct { struct {
char *file; char *file;
int line; int line;
} debug_context_loc = {NULL, 0}; } debug_context_loc = {NULL, 0};
/** /**
@ -65,21 +65,21 @@ struct {
gchar * gchar *
wmud_random_string(gint len) wmud_random_string(gint len)
{ {
gchar *ret = g_malloc0(len + 1); gchar *ret = g_malloc0(len + 1);
gint i; gint i;
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
gchar c = 0; gchar c = 0;
/* Include only printable characters, but exclude $ because of /* Include only printable characters, but exclude $ because of
* salt generation, and space to avoid misunderstanding in the * salt generation, and space to avoid misunderstanding in the
* random generated passwords */ * random generated passwords */
while (!g_ascii_isprint(c) || (c == '$') || (c == ' ') || (c == '\t')) while (!g_ascii_isprint(c) || (c == '$') || (c == ' ') || (c == '\t'))
c = random_number(1, 127); c = random_number(1, 127);
ret[i] = c; ret[i] = c;
} }
return ret; return ret;
} }
#ifdef DEBUG #ifdef DEBUG
@ -97,11 +97,11 @@ void
*/ */
debug_context(char *file, int line) debug_context(char *file, int line)
{ {
if (debug_context_loc.file != NULL) if (debug_context_loc.file != NULL)
g_free(debug_context_loc.file); g_free(debug_context_loc.file);
debug_context_loc.file = g_strdup(file); debug_context_loc.file = g_strdup(file);
debug_context_loc.line = line; debug_context_loc.line = line;
} }
/** /**
@ -122,45 +122,72 @@ debug_context(char *file, int line)
*/ */
void void
wmud_type_init(void) wmud_type_init(void)
{ {}
}
void void
wmud_logger(const gchar *log_domain, GLogLevelFlags log_level, const gchar *message, gpointer user_data) wmud_logger(const gchar *log_domain,
GLogLevelFlags log_level,
const gchar *message,
gpointer user_data)
{ {
static char timestamp[20]; static char timestamp[20];
struct tm *tm; struct tm *tm;
time_t ts = time(NULL); time_t ts = time(NULL);
size_t last_char; size_t last_char;
tm = localtime(&ts); tm = localtime(&ts);
last_char = strftime((char *)&timestamp, 20, "%F %T", tm); last_char = strftime((char *)&timestamp, 20, "%F %T", tm);
timestamp[last_char] = '\0'; timestamp[last_char] = '\0';
switch (log_level) { switch (log_level) {
case G_LOG_LEVEL_DEBUG: case G_LOG_LEVEL_DEBUG:
g_print("[%s] [%s] DEBUG: %s\n", log_domain, timestamp, message); g_print("[%s] [%s] DEBUG: %s\n",
log_domain,
timestamp,
message);
#ifndef DEBUG #ifndef DEBUG
g_warning("Logging a debug-level message without debugging support!"); g_warning("Logging a debug-level message without debugging support!");
#endif #endif
break;
case G_LOG_LEVEL_MESSAGE: break;
g_print("[%s] [%s] MESSAGE: %s\n", log_domain, timestamp, message); case G_LOG_LEVEL_MESSAGE:
break; g_print("[%s] [%s] MESSAGE: %s\n",
case G_LOG_LEVEL_INFO: log_domain,
g_print("[%s] [%s] INFO: %s\n", log_domain, timestamp, message); timestamp,
break; message);
case G_LOG_LEVEL_WARNING:
g_print("[%s] [%s] WARNING: %s\n", log_domain, timestamp, message); break;
break; case G_LOG_LEVEL_INFO:
case G_LOG_LEVEL_CRITICAL: g_print("[%s] [%s] INFO: %s\n",
g_print("[%s] [%s] CRITICAL: %s\n", log_domain, timestamp, message); log_domain,
break; timestamp,
default: message);
g_print("[%s] [%s] UNKNOWN LEVEL %03d: %s\n", log_domain, timestamp, log_level, message);
break; break;
} case G_LOG_LEVEL_WARNING:
g_print("[%s] [%s] WARNING: %s\n",
log_domain,
timestamp,
message);
break;
case G_LOG_LEVEL_CRITICAL:
g_print("[%s] [%s] CRITICAL: %s\n",
log_domain,
timestamp,
message);
break;
default:
g_print("[%s] [%s] UNKNOWN LEVEL %03d: %s\n",
log_domain,
timestamp,
log_level,
message);
break;
}
} }
/** /**
@ -173,70 +200,80 @@ wmud_logger(const gchar *log_domain, GLogLevelFlags log_level, const gchar *mess
int int
main(int argc, char **argv) main(int argc, char **argv)
{ {
GError *err = NULL; GError *err = NULL;
GThread *game_thread; GThread *game_thread;
GMainContext *game_context; GMainContext *game_context;
GSList *game_menu = NULL; GSList *game_menu = NULL;
g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK , wmud_logger, NULL); g_log_set_handler(G_LOG_DOMAIN, G_LOG_LEVEL_MASK , wmud_logger, NULL);
/* Initialize the thread and type system */ /* Initialize the thread and type system */
wmud_type_init(); wmud_type_init();
/* TODO: Command line parsing */ /* TODO: Command line parsing */
/* TODO: Create signal handlers! */ /* TODO: Create signal handlers! */
if (!wmud_config_init(&active_config, &err)) { if (!wmud_config_init(&active_config, &err)) {
if (err) if (err) {
g_critical("Config file parsing error: %s", err->message); g_critical("Config file parsing error: %s", err->message);
else } else {
g_critical("Config file parsing error!"); g_critical("Config file parsing error!");
}
return 1; return 1;
} }
g_clear_error(&err); g_clear_error(&err);
if (!wmud_db_init(&err)) {
if (err)
g_critical("Database initialization error: %s", err->message);
else
g_critical("Database initialization error!");
return 1; if (!wmud_db_init(&err)) {
} if (err) {
g_critical("Database initialization error: %s", err->message);
} else {
g_critical("Database initialization error!");
}
g_clear_error(&err); return 1;
wmud_db_load_players(&err); }
if (!wmud_world_load(&err))
return 1;
if (!wmud_menu_init(&game_menu)) { g_clear_error(&err);
g_log(G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL, "An error occured during menu loading."); wmud_db_load_players(&err);
return 1; if (!wmud_world_load(&err)) {
} return 1;
}
wmud_texts_init(); if (!wmud_menu_init(&game_menu)) {
g_log(G_LOG_DOMAIN,
G_LOG_LEVEL_CRITICAL,
"An error occured during menu loading.");
/* Non-thread initialization ends here */ return 1;
}
wmud_game_init(&game_thread, &game_context); wmud_texts_init();
g_clear_error(&err); /* Non-thread initialization ends here */
if (!wmud_networking_init(active_config->port, game_context, game_menu, &err)) {
if (err)
g_critical("Database initialization error: %s", err->message);
else
g_critical("Database initialization error: unknown error!");
return 1; wmud_game_init(&game_thread, &game_context);
}
wmud_maintenance_init(); g_clear_error(&err);
if (!wmud_networking_init(active_config->port,
game_context,
game_menu,
&err)) {
if (err) {
g_critical("Database initialization error: %s", err->message);
} else {
g_critical("Database initialization error: unknown error!");
}
/* Initialize other threads here */ return 1;
}
g_thread_join(game_thread); wmud_maintenance_init();
return 0; /* Initialize other threads here */
g_thread_join(game_thread);
return 0;
} }