Fix build for tarball
[cascardo/f2fchat.git] / message.c
index 8c6dafd..1d6c22e 100644 (file)
--- a/message.c
+++ b/message.c
@@ -18,6 +18,7 @@
 
 #include "message.h"
 #include "friend.h"
+#include "menu.h"
 #include <glib.h>
 #include <gio/gio.h>
 
@@ -33,9 +34,14 @@ gboolean ping_timeout(gpointer data)
 static GIOChannel *uchannel;
 static GSocket *gusock;
 
-static void command(char *buffer, size_t len)
+static void command(char *buffer, size_t len, GSocketAddress *address)
 {
-       printf("message from loopback: %d %.*s\n", len, len, buffer);
+       gchar **args;
+       args = g_strsplit(buffer, " ", -1);
+       if (args == NULL)
+               return;
+       menu_run(args, address);
+       g_strfreev(args);
 }
 
 gboolean message_incoming(GIOChannel *channel, GIOCondition cond, gpointer data)
@@ -48,11 +54,15 @@ gboolean message_incoming(GIOChannel *channel, GIOCondition cond, gpointer data)
                gusock = g_socket_new_from_fd(g_io_channel_unix_get_fd(channel), NULL);
        }
        len = g_socket_get_available_bytes(gusock);
-       buffer = g_malloc(len);
+       if (len <= 0) {
+               goto out;
+       }
+       buffer = g_malloc(len + 1);
        len = g_socket_receive_from(gusock, &address, buffer, len, NULL, NULL);
+       buffer[len] = 0;
        iaddress = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(address));
        if (g_inet_address_get_is_loopback(iaddress)) {
-               command(buffer, len);
+               command(buffer, len, address);
        } else {
                struct friend *friend;
                uint16_t port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(address));
@@ -66,6 +76,7 @@ gboolean message_incoming(GIOChannel *channel, GIOCondition cond, gpointer data)
        }
        g_object_unref(address);
        g_free(buffer);
+out:
        return TRUE;
 }