]> git.cworth.org Git - loudgame/blobdiff - lm-echo.c
Make *everything* including connection open, happen from inside the glib main loop
[loudgame] / lm-echo.c
index 312e6fee7bf2728f2abcd5b2c452b68528b7114a..22291d3e2643da3b83d8ac966357eb3f2d62cb75 100644 (file)
--- a/lm-echo.c
+++ b/lm-echo.c
@@ -1,4 +1,3 @@
-/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 /*
  * Copyright (C) 2003-2004 Imendio AB
  *
 #endif
  
 typedef struct {
-        gchar *name;
-        gchar *passwd;
+    gchar              *server;
+    gchar              *name;
+    gchar              *passwd;
+    LmConnection       *connection;
+    GMainLoop          *main_loop;
+    int                         return_value;
 } UserInfo;
+
 static void
-free_user_info (UserInfo *info)
+loudgame_quit (UserInfo *info, int return_value)
 {
-        g_free (info->name);
-        g_free (info->passwd);
-        g_free (info);
+    GError *error;
+
+    info->return_value = return_value;
+
+    if (! lm_connection_close (info->connection, &error))
+       g_print ("An error occurred during lm_connection_close: %s\n",
+                error->message);
+
+    lm_connection_unref (info->connection);
+
+    g_main_loop_quit (info->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);
-                g_print (":: %s\n", lm_message_node_to_string (m->node));
+    LmMessage *m;
+    UserInfo *info = closure;
+
+    if (! result) {
+       g_print ("Authentication for %s failed\n", info->name);
+       loudgame_quit (info, 1);
+       return;
+    }
                  
-                lm_connection_send (connection, m, NULL);
-                lm_message_unref (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);
 }
  
 static void
 connection_open_cb (LmConnection *connection, gboolean result, UserInfo *info)
 {
-        g_print ("Connected callback\n");
-        lm_connection_authenticate (connection,
-                                    info->name, info->passwd, "TestLM",
-                                    authentication_cb, info, FALSE,  NULL);
-        g_print ("Sent auth message\n");
+    lm_connection_authenticate (connection,
+                               info->name, info->passwd, "TestLM",
+                               authentication_cb, info, FALSE,  NULL);
+}
+
+static void
+send_reply (LmConnection       *connection,
+           const char          *peer,
+           const char          *message,
+           UserInfo            *info)
+{
+    LmMessage *reply;
+    gboolean result;
+    GError *error = NULL;
+
+    reply = lm_message_new (peer, LM_MESSAGE_TYPE_MESSAGE);
+
+    lm_message_node_add_child (reply->node, "body", message);
+
+    result = lm_connection_send (connection, reply, &error);
+    lm_message_unref (reply);
+
+    if (! result) {
+       g_error ("lm_connection_send failed: error->message");
+       loudgame_quit (info, 1);
+    }
+}
+
+static void
+handle_command (LmConnection   *connection,
+               const char      *peer,
+               const char      *command,
+               UserInfo        *info)
+{
+    char *error;
+
+    if (strcmp (command, "quit") == 0) {
+       loudgame_quit (info, 0);
+       return;
+    }
+
+    error = g_strdup_printf ("Unknown command: '%s'", command);
+    send_reply (connection, peer, error, info);
+    free (error);
 }
  
 static LmHandlerResult
 handle_messages (LmMessageHandler *handler,
                  LmConnection     *connection,
                  LmMessage        *m,
-                 gpointer          user_data)
+                 gpointer          closure)
 {
-       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 (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));
-
-       result = lm_connection_send (connection, reply, &error);
-       lm_message_unref (reply);
-
-       if (! result) {
-               g_error ("lm_connection_send failed");
-       }
-        return LM_HANDLER_RESULT_REMOVE_MESSAGE;
+    UserInfo *info = closure;
+    LmMessageNode *body;
+    const char *peer;
+    const char *body_str;
+
+    peer = lm_message_node_get_attribute (m->node, "from");
+
+    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, info);
+       else
+           send_reply (connection, peer, body_str, info);
+    }
+       
+    return LM_HANDLER_RESULT_REMOVE_MESSAGE;
 }
-int
-main (int argc, char **argv)
+
+static gboolean
+make_connection (gpointer closure)
 {
-        GMainLoop        *main_loop;
-        LmConnection     *connection;
-        LmMessageHandler *handler;
-        gboolean          result;
-        UserInfo         *info;
-       gchar            *jid;
-                                                                                
-        if (argc < 6) {
-                g_print ("Usage: %s <server> <username> <password> <connectserver> <connectport>\n", argv[0]);
-                return 1;
-        }
-                                                                                
-        connection = lm_connection_new (argv[4]);
+    UserInfo        *info;
+    LmMessageHandler *handler;
+    gchar            *jid;
+    GError          *error;
 
-       jid = g_strdup_printf ("%s@%s", argv[2], argv[1]);
-       lm_connection_set_jid (connection, jid);
-       g_free (jid);
+    info = closure;
+                                                                                
+    info->connection = lm_connection_new (info->server);
 
-       lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10));
+    jid = g_strdup_printf ("%s@%s", info->name, info->server);
+    lm_connection_set_jid (info->connection, jid);
+    g_free (jid);
 
-        handler = lm_message_handler_new (handle_messages, NULL, NULL);
-        lm_connection_register_message_handler (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);
-        } else {
-                g_print ("Returned from the connection_open\n");
-        }
+    handler = lm_message_handler_new (handle_messages, info, NULL);
+    lm_connection_register_message_handler (info->connection,
+                                           handler,
+                                           LM_MESSAGE_TYPE_MESSAGE,
+                                           LM_HANDLER_PRIORITY_NORMAL);
                                                                                 
-        main_loop = g_main_loop_new (NULL, FALSE);
-        g_main_loop_run (main_loop);
+    lm_message_handler_unref (handler);
                                                                                 
-        return 0;
+    if (! lm_connection_open (info->connection,
+                             (LmResultFunction) connection_open_cb,
+                             info, NULL, &error))
+    {
+       g_print ("Opening connection failed: %s\n", error->message);
+       loudgame_quit (info, 1);
+    }
+
+    /* Return false to not schedule another call. */
+    return 0;
 }
 
+int
+main (int argc, char **argv)
+{
+    UserInfo          info;
+
+    if (argc != 4) {
+       g_print ("Usage: %s <server> <username> <password>\n", argv[0]);
+       return 1;
+    }
+
+    info.server = argv[1];
+    info.name = argv[2];
+    info.passwd = argv[3];
+
+    info.connection = NULL;
+
+    info.main_loop = g_main_loop_new (NULL, FALSE);
+
+    g_idle_add (make_connection, &info);
+
+    g_main_loop_run (info.main_loop);
+
+    g_main_loop_unref (info.main_loop);
+                                                                                
+    return info.return_value;
+}