Add command menu for messages received from loopback.
[cascardo/f2fchat.git] / friend.c
index 68deccd..af5d5e3 100644 (file)
--- a/friend.c
+++ b/friend.c
 #include <errno.h>
 #include <stdio.h>
 #include "message.h"
+#include "menu.h"
+
+enum {
+       STATE_OFFLINE,
+       STATE_PINGED,
+       STATE_ONLINE,
+};
 
 struct friend {
        char *name;
        char *address;
        uint16_t port;
        GInetSocketAddress *saddr;
+       int state;
 };
 
 static GSocket *usock;
@@ -64,6 +72,37 @@ int friend_send_message(struct friend *friend, char *buffer, size_t len)
        return 0;
 }
 
+void friend_timeout(struct friend *friend)
+{
+       if (friend->state == STATE_PINGED) {
+               friend->state = STATE_OFFLINE;
+       }
+}
+
+void friend_got_message(struct friend *friend, char *buffer, size_t len)
+{
+       if (len >= 4 && !strncmp(buffer, "PING", 4)) {
+               friend->state = STATE_ONLINE;
+               pong(friend);
+       } else if (len >= 4 && !strncmp(buffer, "PONG", 4)) {
+               friend->state = STATE_ONLINE;
+       }
+}
+
+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;
 };
@@ -123,6 +162,7 @@ int cache_add_friend(struct cache *cache, char *name, char *address, uint16_t po
        g_object_unref(addr);
        cache->friends = g_list_append(cache->friends, friend);
        ping(friend);
+       friend->state = STATE_PINGED;
        return 0;
 }