From 2ce1e48602de964b8a12747b4a329fc082da7523 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Thu, 17 Oct 2013 08:39:23 -0300 Subject: [PATCH] Add command menu for messages received from loopback. --- Makefile.am | 2 +- f2fchat.c | 1 + friend.c | 15 +++++++++++++++ friend.h | 3 +++ menu.c | 40 ++++++++++++++++++++++++++++++++++++++++ menu.h | 32 ++++++++++++++++++++++++++++++++ message.c | 10 +++++++--- 7 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 menu.c create mode 100644 menu.h diff --git a/Makefile.am b/Makefile.am index 6e77a5e..223a404 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ 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) diff --git a/f2fchat.c b/f2fchat.c index 5462e6a..eb9423c 100644 --- a/f2fchat.c +++ b/f2fchat.c @@ -36,6 +36,7 @@ int main(int argc, char **argv) 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); diff --git a/friend.c b/friend.c index 2e50f36..af5d5e3 100644 --- a/friend.c +++ b/friend.c @@ -26,6 +26,7 @@ #include #include #include "message.h" +#include "menu.h" 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; }; diff --git a/friend.h b/friend.h index efe5f65..74af824 100644 --- a/friend.h +++ b/friend.h @@ -26,6 +26,9 @@ int sock_init(void); struct friend; + +void friend_init(void); + 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 index 0000000..6796be3 --- /dev/null +++ b/menu.c @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * 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 + +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 index 0000000..c6b6f07 --- /dev/null +++ b/menu.h @@ -0,0 +1,32 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * 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 + +struct menu_item { + char *cmd; + void (*func)(gchar **, GSocketAddress *); +}; + +void menu_add(struct menu_item *mi); +void menu_run(gchar **args, GSocketAddress *address); + +#endif diff --git a/message.c b/message.c index 4d3150a..2074549 100644 --- a/message.c +++ b/message.c @@ -18,6 +18,7 @@ #include "message.h" #include "friend.h" +#include "menu.h" #include #include @@ -33,9 +34,12 @@ 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); + menu_run(args, address); + g_strfreev(args); } 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)) { - 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)); -- 2.20.1