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)
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);
#include <errno.h>
#include <stdio.h>
#include "message.h"
+#include "menu.h"
enum {
STATE_OFFLINE,
}
}
+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;
};
int sock_init(void);
struct friend;
+
+void friend_init(void);
+
struct cache;
int create_cache(struct cache **cache);
int destroy_cache(struct cache *cache);
--- /dev/null
+/*
+ * 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;
+ }
+ }
+}
--- /dev/null
+/*
+ * 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
#include "message.h"
#include "friend.h"
+#include "menu.h"
#include <glib.h>
#include <gio/gio.h>
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)
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));