Add command menu for messages received from loopback.
[cascardo/f2fchat.git] / friend.c
1 /*
2  *  Copyright (C) 2013  Thadeu Lima de Souza Cascardo <cascardo@cascardo.info>
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; either version 3 of the License, or
7  *  (at your option) any later version.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License along
15  *  with this program; if not, write to the Free Software Foundation, Inc.,
16  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18
19 #include "friend.h"
20 #include <string.h>
21 #include <glib.h>
22 #include <sys/socket.h>
23 #include <netinet/in.h>
24 #include <arpa/inet.h>
25 #include <netdb.h>
26 #include <errno.h>
27 #include <stdio.h>
28 #include "message.h"
29 #include "menu.h"
30
31 enum {
32         STATE_OFFLINE,
33         STATE_PINGED,
34         STATE_ONLINE,
35 };
36
37 struct friend {
38         char *name;
39         char *address;
40         uint16_t port;
41         GInetSocketAddress *saddr;
42         int state;
43 };
44
45 static GSocket *usock;
46
47 int sock_init(void)
48 {
49         GSocketAddress *address;
50         GInetAddress *any_addr;
51         GError *error;
52         int err = 0;
53         any_addr = g_inet_address_new_any(G_SOCKET_FAMILY_IPV6);
54         usock = g_socket_new(G_SOCKET_FAMILY_IPV6, G_SOCKET_TYPE_DATAGRAM, G_SOCKET_PROTOCOL_UDP, NULL);
55         address = g_inet_socket_address_new(any_addr, 17078);
56         if (!g_socket_bind(usock, address, TRUE, &error)) {
57                 err = error->code;
58                 g_error_free(error);
59         }
60         g_object_unref(address);
61         g_object_unref(any_addr);
62         if (!err)
63                 message_init(usock);
64         else
65                 g_object_unref(usock);
66         return err;
67 }
68
69 int friend_send_message(struct friend *friend, char *buffer, size_t len)
70 {
71         g_socket_send_to(usock, G_SOCKET_ADDRESS(friend->saddr), buffer, len, NULL, NULL);
72         return 0;
73 }
74
75 void friend_timeout(struct friend *friend)
76 {
77         if (friend->state == STATE_PINGED) {
78                 friend->state = STATE_OFFLINE;
79         }
80 }
81
82 void friend_got_message(struct friend *friend, char *buffer, size_t len)
83 {
84         if (len >= 4 && !strncmp(buffer, "PING", 4)) {
85                 friend->state = STATE_ONLINE;
86                 pong(friend);
87         } else if (len >= 4 && !strncmp(buffer, "PONG", 4)) {
88                 friend->state = STATE_ONLINE;
89         }
90 }
91
92 void friend_cmd(gchar **args, GSocketAddress *address)
93 {
94         printf("%s\n", args[1]);
95 }
96
97 void friend_init(void)
98 {
99         struct menu_item *mi;
100         mi = g_malloc(sizeof(*mi));
101         mi->cmd = "friend";
102         mi->func = friend_cmd;
103         menu_add(mi);
104 }
105
106 struct cache {
107         GList *friends;
108 };
109
110 static struct cache *ucache;
111
112 struct friend *friend_get_by_address(GInetAddress *address, uint16_t port)
113 {
114         GList *l;
115         for (l = g_list_first(ucache->friends); l != NULL; l = g_list_next(l)) {
116                 struct friend *friend = l->data;
117                 if (g_inet_address_equal(g_inet_socket_address_get_address(friend->saddr), address) &&
118                     friend->port == port)
119                         return friend;
120         }
121         return NULL;
122 }
123
124 int create_cache(struct cache **cache)
125 {
126         ucache = *cache = g_slice_new0(struct cache);
127         (*cache)->friends = NULL;
128         return 0;
129 }
130
131 static void destroy_friend(gpointer data)
132 {
133         struct friend *friend = data;
134         g_free(friend->name);
135         g_free(friend->address);
136         g_object_unref(friend->saddr);
137         g_slice_free(struct friend, friend);
138 }
139
140 int destroy_cache(struct cache *cache)
141 {
142         if (cache->friends)
143                 g_list_free_full(cache->friends, destroy_friend);
144         g_slice_free(struct cache, cache);
145 }
146
147 char * friend_get_name(struct friend *friend)
148 {
149         return friend->name;
150 }
151
152 int cache_add_friend(struct cache *cache, char *name, char *address, uint16_t port)
153 {
154         struct friend *friend;
155         GInetAddress *addr;
156         friend = g_slice_new0(struct friend);
157         friend->name = g_strdup(name);
158         friend->address = g_strdup(address);
159         friend->port = port;
160         addr = g_inet_address_new_from_string(address);
161         friend->saddr = G_INET_SOCKET_ADDRESS(g_inet_socket_address_new(addr, friend->port));
162         g_object_unref(addr);
163         cache->friends = g_list_append(cache->friends, friend);
164         ping(friend);
165         friend->state = STATE_PINGED;
166         return 0;
167 }
168
169 int load_cache(struct cache *cache, char *fname)
170 {
171         GKeyFile *file;
172         gchar **groups;
173         gchar **group;
174         file = g_key_file_new();
175         g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
176         groups = g_key_file_get_groups(file, NULL);
177         for (group = groups; *group != NULL; group++) {
178                 gchar *name;
179                 gchar *address;
180                 uint16_t port;
181                 name = g_key_file_get_value(file, *group, "name", NULL);
182                 address = g_key_file_get_value(file, *group, "address", NULL);
183                 port = g_key_file_get_integer(file, *group, "port", NULL);
184                 cache_add_friend(cache, name, address, port);
185                 g_free(name);
186                 g_free(address);
187         }
188         g_strfreev(groups);
189         g_key_file_free(file);
190         return 0;
191 }
192
193 int store_cache(struct cache *cache, char *fname)
194 {
195         GKeyFile *file;
196         GList *f;
197         gchar *contents;
198         gssize len;
199         file = g_key_file_new();
200         g_key_file_load_from_file(file, fname, G_KEY_FILE_KEEP_COMMENTS | G_KEY_FILE_KEEP_TRANSLATIONS, NULL);
201         for (f = g_list_first(cache->friends); f != NULL; f = g_list_next(f)) {
202                 struct friend *friend = f->data;
203                 g_key_file_set_value(file, friend->name, "name", friend->name);
204                 g_key_file_set_value(file, friend->name, "address", friend->address);
205                 g_key_file_set_integer(file, friend->name, "port", friend->port);
206         }
207         contents = g_key_file_to_data(file, &len, NULL);
208         g_file_set_contents(fname, contents, len, NULL);
209         g_free(contents);
210         g_key_file_free(file);
211         return 0;
212 }