X-Git-Url: https://git.cworth.org/git?p=loudgame;a=blobdiff_plain;f=lm-echo.c;h=661949e2771ff425b1175e12ffa4e10581a9be5a;hp=a93fb545e0ef464eda579b566555875308822d3f;hb=e995240ce8748861317919d3ac98efece67076da;hpb=e0371a16123cad151bd4cf89db649388e0aee672 diff --git a/lm-echo.c b/lm-echo.c index a93fb54..661949e 100644 --- a/lm-echo.c +++ b/lm-echo.c @@ -25,55 +25,64 @@ #include #endif -typedef struct { - gchar *name; - gchar *passwd; -} UserInfo; - -typedef struct { - GMainLoop *main_loop; -} msg_data_t; - +typedef struct _loudgame { + gchar *server; + gchar *name; + gchar *passwd; + LmConnection *connection; + GMainLoop *main_loop; + int return_value; +} loudgame_t; + static void -free_user_info (UserInfo *info) +loudgame_quit (loudgame_t *lg, int return_value) { - g_free (info->name); - g_free (info->passwd); - - g_free (info); + GError *error; + + lg->return_value = return_value; + + if (! lm_connection_close (lg->connection, &error)) + g_print ("An error occurred during lm_connection_close: %s\n", + error->message); + + lm_connection_unref (lg->connection); + + g_main_loop_quit (lg->main_loop); } static void -authentication_cb (LmConnection *connection, gboolean result, gpointer ud) +authentication_cb (LmConnection *connection, gboolean result, gpointer closure) { - g_print ("Auth: %d\n", result); - free_user_info ((UserInfo *) ud); - - if (result == TRUE) { - LmMessage *m; - - m = lm_message_new_with_sub_type (NULL, - LM_MESSAGE_TYPE_PRESENCE, - LM_MESSAGE_SUB_TYPE_AVAILABLE); - - lm_connection_send (connection, m, NULL); - lm_message_unref (m); + LmMessage *m; + loudgame_t *lg = closure; + + if (! result) { + g_print ("Authentication for %s failed\n", lg->name); + loudgame_quit (lg, 1); + return; } + + m = lm_message_new_with_sub_type (NULL, + LM_MESSAGE_TYPE_PRESENCE, + LM_MESSAGE_SUB_TYPE_AVAILABLE); + lm_connection_send (connection, m, NULL); + lm_message_unref (m); } static void -connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info) +connection_open_cb (LmConnection *connection, gboolean result, loudgame_t *lg) { lm_connection_authenticate (connection, - info->name, info->passwd, "TestLM", - authentication_cb, info, FALSE, NULL); + lg->name, lg->passwd, "TestLM", + authentication_cb, lg, FALSE, NULL); } static void send_reply (LmConnection *connection, const char *peer, - const char *message) + const char *message, + loudgame_t *lg) { LmMessage *reply; gboolean result; @@ -87,7 +96,8 @@ send_reply (LmConnection *connection, lm_message_unref (reply); if (! result) { - g_error ("lm_connection_send failed"); + g_error ("lm_connection_send failed: error->message"); + loudgame_quit (lg, 1); } } @@ -95,18 +105,17 @@ static void handle_command (LmConnection *connection, const char *peer, const char *command, - gpointer closure) + loudgame_t *lg) { - msg_data_t *data = closure; char *error; if (strcmp (command, "quit") == 0) { - g_main_loop_quit (data->main_loop); + loudgame_quit (lg, 0); return; } error = g_strdup_printf ("Unknown command: '%s'", command); - send_reply (connection, peer, error); + send_reply (connection, peer, error, lg); free (error); } @@ -116,6 +125,7 @@ handle_messages (LmMessageHandler *handler, LmMessage *m, gpointer closure) { + loudgame_t *lg = closure; LmMessageNode *body; const char *peer; const char *body_str; @@ -127,64 +137,73 @@ handle_messages (LmMessageHandler *handler, body_str = lm_message_node_get_value (body); if (body_str && body_str[0] == '%') - handle_command (connection, peer, body_str + 1, closure); + handle_command (connection, peer, body_str + 1, lg); else - send_reply (connection, peer, body_str); + send_reply (connection, peer, body_str, lg); } return LM_HANDLER_RESULT_REMOVE_MESSAGE; } -int -main (int argc, char **argv) + +static gboolean +make_connection (gpointer closure) { - LmConnection *connection; + loudgame_t *lg; LmMessageHandler *handler; - gboolean result; - UserInfo *info; gchar *jid; - msg_data_t data; GError *error; - if (argc < 6) { - g_print ("Usage: %s \n", argv[0]); - return 1; - } + lg = closure; - connection = lm_connection_new (argv[4]); + lg->connection = lm_connection_new (lg->server); - jid = g_strdup_printf ("%s@%s", argv[2], argv[1]); - lm_connection_set_jid (connection, jid); + jid = g_strdup_printf ("%s@%s", lg->name, lg->server); + lm_connection_set_jid (lg->connection, jid); g_free (jid); - lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10)); - - data.main_loop = g_main_loop_new (NULL, FALSE); - - handler = lm_message_handler_new (handle_messages, &data, NULL); - lm_connection_register_message_handler (connection, handler, + handler = lm_message_handler_new (handle_messages, lg, NULL); + lm_connection_register_message_handler (lg->connection, + handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_NORMAL); lm_message_handler_unref (handler); - info = g_new0 (UserInfo, 1); - info->name = g_strdup (argv[2]); - info->passwd = g_strdup (argv[3]); - - result = lm_connection_open (connection, - (LmResultFunction) connection_open_cb, - info, NULL, NULL); - if (!result) { - g_print ("Opening connection failed: %d\n", result); - exit (1); + if (! lm_connection_open (lg->connection, + (LmResultFunction) connection_open_cb, + lg, NULL, &error)) + { + g_print ("Opening connection failed: %s\n", error->message); + loudgame_quit (lg, 1); } - - g_main_loop_run (data.main_loop); - g_main_loop_unref (data.main_loop); + /* Return false to not schedule another call. */ + return 0; +} + +int +main (int argc, char **argv) +{ + loudgame_t lg; - lm_connection_close (connection, &error); - lm_connection_unref (connection); + if (argc != 4) { + g_print ("Usage: %s \n", argv[0]); + return 1; + } + + lg.server = argv[1]; + lg.name = argv[2]; + lg.passwd = argv[3]; + + lg.connection = NULL; + + lg.main_loop = g_main_loop_new (NULL, FALSE); + + g_idle_add (make_connection, &lg); + + g_main_loop_run (lg.main_loop); + + g_main_loop_unref (lg.main_loop); - return 0; + return lg.return_value; }