summaryrefslogtreecommitdiff
path: root/commands.c
diff options
context:
space:
mode:
Diffstat (limited to 'commands.c')
-rw-r--r--commands.c90
1 files changed, 69 insertions, 21 deletions
diff --git a/commands.c b/commands.c
index 98d775c..4108cff 100644
--- a/commands.c
+++ b/commands.c
@@ -9,30 +9,13 @@ GQuark commands_quark() {
return g_quark_from_static_string("commands");
}
-static void commands_list(GSocketConnection *connection, const gchar *cmd, GError **error) {
- gchar **data = g_strsplit(cmd, " ", 2);
- for(gint i = 0; data[i]; i++) {
- g_debug("\tdata[%d] = %s", i, data[i]);
- }
- g_assert(data[0] != NULL);
-
- gchar *dirname;
- if(data[1] != NULL) {
- dirname = g_strdup(data[1]);
- } else {
- dirname = "/";
- }
-
- g_strfreev(data);
-
+static void list_local(GString *string, gchar *dirname, GError **error) {
struct directory *directory = music_find_dir(dirname);
if(directory == NULL) {
*error = g_error_new(commands_quark(), 0, "error: couldn't find directory %s\n", dirname);
return;
}
- GString *string = g_string_new(NULL);
-
for(GSList *node = directory->sub; node; node = g_slist_next(node)) {
struct directory *d = node->data;
gchar *name = g_path_get_basename(d->path);
@@ -44,13 +27,78 @@ static void commands_list(GSocketConnection *connection, const gchar *cmd, GErro
struct file *f = node->data;
g_string_append_printf(string, "%s\n", f->name);
}
+}
- GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
- if(g_output_stream_write_all(os, string->str, string->len, NULL, NULL,
- error) == FALSE) {
+static void list_remote(GString *string, gchar *remotehost, gchar *dirname, GError **error) {
+ struct server *server = NULL;
+
+ for(GSList *node = servers; node; node = g_slist_next(node)) {
+ struct server *temp = node->data;
+ if(g_strcasecmp(temp->host, remotehost) == 0) {
+ server = temp;
+ break;
+ }
+ }
+
+ if(server == NULL) {
+ *error = g_error_new(commands_quark(), 0, "couldn't find remote host %s", remotehost);
return;
}
+ gchar **data = server_list(server, dirname);
+ g_debug("got %d results from %s", g_strv_length(data), remotehost);
+
+ for(gint i = 0; i < g_strv_length(data); i++) {
+ if(strlen(data[i]) == 0) {
+ break;
+ }
+ g_string_append_printf(string, "%s:%s\n", server->host, data[i]);
+ }
+
+ g_strfreev(data);
+}
+
+static void commands_list(GSocketConnection *connection, const gchar *cmd, GError **error) {
+ gboolean remote = g_strncasecmp(cmd, "listr", 5) == 0 ? TRUE : FALSE;
+
+ gchar **data = g_strsplit(cmd, " ", remote == TRUE ? 3 : 2);
+
+ if(remote == TRUE && data[1] == NULL) {
+ *error = g_error_new(commands_quark(), 0, "syntax: list[r host] [directory]");
+ g_strfreev(data);
+ return;
+ }
+
+ gchar *dirname, *remotehost;
+ if(remote == TRUE) {
+ remotehost = g_strdup(data[1]);
+ dirname = g_strdup(data[2]);
+ } else {
+ dirname = g_strdup(data[1]);
+ }
+
+ g_strfreev(data);
+
+ if(dirname == NULL) {
+ dirname = "/";
+ }
+
+ GString *string = g_string_new(NULL);
+
+ if(remote == TRUE) {
+ list_remote(string, remotehost, dirname, error);
+ } else {
+ list_local(string, dirname, error);
+ }
+
+ if(*error != NULL) {
+ g_string_free(string, TRUE);
+ return;
+ }
+
+ GOutputStream *os = g_io_stream_get_output_stream((GIOStream*)connection);
+ g_output_stream_write_all(os, string->str, string->len, NULL, NULL, error);
+
g_string_free(string, TRUE);
}