Accept command to list friends
authorThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Thu, 17 Oct 2013 21:47:42 +0000 (18:47 -0300)
committerThadeu Lima de Souza Cascardo <cascardo@cascardo.info>
Thu, 17 Oct 2013 21:47:42 +0000 (18:47 -0300)
friend.c

index af5d5e3..5abd50c 100644 (file)
--- a/friend.c
+++ b/friend.c
@@ -89,9 +89,27 @@ void friend_got_message(struct friend *friend, char *buffer, size_t len)
        }
 }
 
+static void friend_list(gchar **args, GSocketAddress *address);
+
+static void friend_add(gchar **args, GSocketAddress *address)
+{
+}
+
+struct menu_item cmds[] = {
+       { "list", friend_list },
+       { "add", friend_add },
+};
+
 void friend_cmd(gchar **args, GSocketAddress *address)
 {
-       printf("%s\n", args[1]);
+       int i;
+       if (args[1] == NULL)
+               return;
+       for (i = 0; i < sizeof(cmds)/sizeof(cmds[0]); i++) {
+               if (!strcmp(args[1], cmds[i].cmd)) {
+                       cmds[i].func(args, address);
+               }
+       }
 }
 
 void friend_init(void)
@@ -109,6 +127,19 @@ struct cache {
 
 static struct cache *ucache;
 
+static void friend_list(gchar **args, GSocketAddress *address)
+{
+       char *buffer;
+       GList *l;
+       for (l = g_list_first(ucache->friends); l != NULL; l = g_list_next(l)) {
+               struct friend *friend = l->data;
+               buffer = g_strdup_printf("%s\n", friend->name);
+               g_socket_send_to(usock, address, buffer, strlen(buffer), NULL, NULL);
+               g_free(buffer);
+       }
+       g_socket_send_to(usock, address, buffer, 0, NULL, NULL);
+}
+
 struct friend *friend_get_by_address(GInetAddress *address, uint16_t port)
 {
        GList *l;