Add command menu for messages received from loopback.
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Thu, 17 Oct 2013 11:39:23 +0000 (08:39 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Thu, 17 Oct 2013 11:39:23 +0000 (08:39 -0300)
Makefile.am
f2fchat.c
friend.c
friend.h
menu.c [new file with mode: 0644]
menu.h [new file with mode: 0644]
message.c

index 6e77a5e..223a404 100644 (file)
@@ -1,6 +1,6 @@
 bin_PROGRAMS = f2fchat f2fcmd
 
 bin_PROGRAMS = f2fchat f2fcmd
 
-f2fchat_SOURCES = f2fchat.c friend.c message.c
+f2fchat_SOURCES = f2fchat.c friend.c message.c menu.c
 f2fchat_CFLAGS = $(GLIB_CFLAGS) $(GIO_CFLAGS)
 f2fchat_LDFLAGS = $(GLIB_LIBS) $(GIO_LIBS)
 
 f2fchat_CFLAGS = $(GLIB_CFLAGS) $(GIO_CFLAGS)
 f2fchat_LDFLAGS = $(GLIB_LIBS) $(GIO_LIBS)
 
index 5462e6a..eb9423c 100644 (file)
--- a/f2fchat.c
+++ b/f2fchat.c
@@ -36,6 +36,7 @@ int main(int argc, char **argv)
                fprintf(stderr, "Error creating socket.\n");
                return 1;
        }
                fprintf(stderr, "Error creating socket.\n");
                return 1;
        }
+       friend_init();
        create_cache(&cache);
        load_cache(cache, "friends.cache");
        loop = g_main_loop_new(g_main_context_default(), TRUE);
        create_cache(&cache);
        load_cache(cache, "friends.cache");
        loop = g_main_loop_new(g_main_context_default(), TRUE);
index 2e50f36..af5d5e3 100644 (file)
--- a/friend.c
+++ b/friend.c
@@ -26,6 +26,7 @@
 #include <errno.h>
 #include <stdio.h>
 #include "message.h"
 #include <errno.h>
 #include <stdio.h>
 #include "message.h"
+#include "menu.h"
 
 enum {
        STATE_OFFLINE,
 
 enum {
        STATE_OFFLINE,
@@ -88,6 +89,20 @@ void friend_got_message(struct friend *friend, char *buffer, size_t len)
        }
 }
 
        }
 }
 
+void friend_cmd(gchar **args, GSocketAddress *address)
+{
+       printf("%s\n", args[1]);
+}
+
+void friend_init(void)
+{
+       struct menu_item *mi;
+       mi = g_malloc(sizeof(*mi));
+       mi->cmd = "friend";
+       mi->func = friend_cmd;
+       menu_add(mi);
+}
+
 struct cache {
        GList *friends;
 };
 struct cache {
        GList *friends;
 };
index efe5f65..74af824 100644 (file)
--- a/friend.h
+++ b/friend.h
@@ -26,6 +26,9 @@
 int sock_init(void);
 
 struct friend;
 int sock_init(void);
 
 struct friend;
+
+void friend_init(void);
+
 struct cache;
 int create_cache(struct cache **cache);
 int destroy_cache(struct cache *cache);
 struct cache;
 int create_cache(struct cache **cache);
 int destroy_cache(struct cache *cache);
diff --git a/menu.c b/menu.c
new file mode 100644 (file)
index 0000000..6796be3
--- /dev/null
+++ b/menu.c
@@ -0,0 +1,40 @@
+/*
+ *  Copyright (C) 2013  Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#include "menu.h"
+
+#include <gio/gio.h>
+
+static GList *menu = NULL;
+
+void menu_add(struct menu_item *mi)
+{
+       menu = g_list_append(menu, mi);
+}
+
+void menu_run(gchar **args, GSocketAddress *address)
+{
+       GList *l;
+       for (l = g_list_first(menu); l != NULL; l = g_list_next(l)) {
+               struct menu_item *mi = l->data;
+               if (!strcmp(args[0], mi->cmd)) {
+                       mi->func(args, address);
+                       break;
+               }
+       }
+}
diff --git a/menu.h b/menu.h
new file mode 100644 (file)
index 0000000..c6b6f07
--- /dev/null
+++ b/menu.h
@@ -0,0 +1,32 @@
+/*
+ *  Copyright (C) 2013  Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+
+#ifndef _MENU_H
+#define _MENU_H
+
+#include <gio/gio.h>
+
+struct menu_item {
+       char *cmd;
+       void (*func)(gchar **, GSocketAddress *);
+};
+
+void menu_add(struct menu_item *mi);
+void menu_run(gchar **args, GSocketAddress *address);
+
+#endif
index 4d3150a..2074549 100644 (file)
--- a/message.c
+++ b/message.c
@@ -18,6 +18,7 @@
 
 #include "message.h"
 #include "friend.h"
 
 #include "message.h"
 #include "friend.h"
+#include "menu.h"
 #include <glib.h>
 #include <gio/gio.h>
 
 #include <glib.h>
 #include <gio/gio.h>
 
@@ -33,9 +34,12 @@ gboolean ping_timeout(gpointer data)
 static GIOChannel *uchannel;
 static GSocket *gusock;
 
 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);
+       menu_run(args, address);
+       g_strfreev(args);
 }
 
 gboolean message_incoming(GIOChannel *channel, GIOCondition cond, gpointer data)
 }
 
 gboolean message_incoming(GIOChannel *channel, GIOCondition cond, gpointer data)
@@ -55,7 +59,7 @@ gboolean message_incoming(GIOChannel *channel, GIOCondition cond, gpointer data)
        len = g_socket_receive_from(gusock, &address, buffer, len, NULL, NULL);
        iaddress = g_inet_socket_address_get_address(G_INET_SOCKET_ADDRESS(address));
        if (g_inet_address_get_is_loopback(iaddress)) {
        len = g_socket_receive_from(gusock, &address, buffer, len, NULL, NULL);
        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));
        } else {
                struct friend *friend;
                uint16_t port = g_inet_socket_address_get_port(G_INET_SOCKET_ADDRESS(address));