From: Carl Worth Date: Sun, 6 Jan 2008 01:23:49 +0000 (-0800) Subject: Add support for a %quit command X-Git-Url: https://git.cworth.org/git?p=loudgame;a=commitdiff_plain;h=8c5c387f532da09722b53e5f3853b545066ba394 Add support for a %quit command --- diff --git a/lm-echo.c b/lm-echo.c index e723599..d92188b 100644 --- a/lm-echo.c +++ b/lm-echo.c @@ -26,9 +26,13 @@ #endif typedef struct { - gchar *name; - gchar *passwd; + gchar *name; + gchar *passwd; } UserInfo; + +typedef struct { + GMainLoop *main_loop; +} msg_data_t; static void free_user_info (UserInfo *info) @@ -39,10 +43,11 @@ free_user_info (UserInfo *info) g_free (info); } - static void authentication_cb (LmConnection *connection, gboolean result, gpointer ud) { + gchar *string; + g_print ("Auth: %d\n", result); free_user_info ((UserInfo *) ud); @@ -52,7 +57,9 @@ authentication_cb (LmConnection *connection, gboolean result, gpointer ud) m = lm_message_new_with_sub_type (NULL, LM_MESSAGE_TYPE_PRESENCE, LM_MESSAGE_SUB_TYPE_AVAILABLE); - g_print (":: %s\n", lm_message_node_to_string (m->node)); + string = lm_message_node_to_string (m->node); + g_print (":: %s\n", string); + free (string); lm_connection_send (connection, m, NULL); lm_message_unref (m); @@ -69,26 +76,19 @@ connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info) authentication_cb, info, FALSE, NULL); g_print ("Sent auth message\n"); } - -static LmHandlerResult -handle_messages (LmMessageHandler *handler, - LmConnection *connection, - LmMessage *m, - gpointer user_data) + +static void +send_reply (LmConnection *connection, + const char *peer, + const char *message) { LmMessage *reply; gboolean result; GError *error = NULL; - LmMessageNode *body; - g_print ("Incoming message from: %s\n", - lm_message_node_get_attribute (m->node, "from")); + reply = lm_message_new (peer, LM_MESSAGE_TYPE_MESSAGE); - reply = lm_message_new (lm_message_node_get_attribute (m->node, "from"), - LM_MESSAGE_TYPE_MESSAGE); - body = lm_message_node_get_child (m->node, "body"); - lm_message_node_add_child (reply->node, "body", - lm_message_node_get_value (body)); + lm_message_node_add_child (reply->node, "body", message); result = lm_connection_send (connection, reply, &error); lm_message_unref (reply); @@ -96,19 +96,65 @@ handle_messages (LmMessageHandler *handler, if (! result) { g_error ("lm_connection_send failed"); } +} + +static void +handle_command (LmConnection *connection, + const char *peer, + const char *command, + gpointer closure) +{ + msg_data_t *data = closure; + char *error; + + if (strcmp (command, "quit") == 0) { + g_main_loop_quit (data->main_loop); + return; + } + + error = g_strdup_printf ("Unknown command: '%s'", command); + send_reply (connection, peer, error); + free (error); +} +static LmHandlerResult +handle_messages (LmMessageHandler *handler, + LmConnection *connection, + LmMessage *m, + gpointer closure) +{ + LmMessageNode *body; + const char *peer; + const char *body_str; + + peer = lm_message_node_get_attribute (m->node, "from"); + + + g_print ("Incoming message from: %s\n", peer); + + body = lm_message_node_get_child (m->node, "body"); + if (body) { + body_str = lm_message_node_get_value (body); + + if (body_str && body_str[0] == '%') + handle_command (connection, peer, body_str + 1, closure); + else + send_reply (connection, peer, body_str); + } + return LM_HANDLER_RESULT_REMOVE_MESSAGE; } int main (int argc, char **argv) { - GMainLoop *main_loop; LmConnection *connection; 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; @@ -122,7 +168,9 @@ main (int argc, char **argv) lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10)); - handler = lm_message_handler_new (handle_messages, NULL, NULL); + 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, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_NORMAL); @@ -143,8 +191,12 @@ main (int argc, char **argv) g_print ("Returned from the connection_open\n"); } - main_loop = g_main_loop_new (NULL, FALSE); - g_main_loop_run (main_loop); + g_main_loop_run (data.main_loop); + + g_main_loop_unref (data.main_loop); + + lm_connection_close (connection, &error); + lm_connection_unref (connection); return 0; }