#include "commands.h" #include "music.h" #include "servers.h" #include "server_communication.h" #include 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); 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); g_string_append_printf(string, "%s\n", name); g_free(name); } for(GSList *node = directory->files; node; node = g_slist_next(node)) { 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) { return; } g_string_free(string, TRUE); } static void commands_find(GSocketConnection *connection, const gchar *cmd, GError **error) { gchar **data = g_strsplit(cmd, " ", 3); if(g_strv_length(data) != 3) { *error = g_error_new(commands_quark(), 0, "syntax: find[r|l] artist|title|album search"); return; } gboolean remote = g_strncasecmp(data[0], "findl", 5) == 0 ? FALSE : TRUE; gboolean local = g_strncasecmp(data[0], "findr", 5) == 0 ? FALSE : TRUE; GString *string = g_string_new(NULL); if(local == TRUE) { GSList *list = NULL; if(g_ascii_strcasecmp(data[1], "artist") == 0) { list = music_find_artist(data[2]); } else if(g_ascii_strcasecmp(data[1] , "title") == 0) { list = music_find_title(data[2]); } else if(g_ascii_strcasecmp(data[1], "album") == 0) { list = music_find_album(data[2]); } else { *error = g_error_new(commands_quark(), 0, "unknown search method %s", data[1]); return; } for(GSList *node = list; node; node = g_slist_next(node)) { struct file *f = node->data; gchar *relpath = g_build_filename(f->parent->path + strlen(music_root->path), f->name, NULL); g_string_append_printf(string, "%s\n", relpath); g_free(relpath); } g_slist_free(list); } if(remote == TRUE) { for(GSList *node = servers; node; node = g_slist_next(node)) { struct server *server = node->data; g_debug("fetching data from server %s", server->host); gchar **temp = server_find(server, data[1], data[2]); if(temp == NULL) { continue; } for(gint i = 0; i < g_strv_length(temp); i++) { if(strlen(temp[i]) == 0) { break; } g_string_append_printf(string, "%s:%s\n", server->host, temp[i]); } g_strfreev(temp); } } 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) { return; } g_string_free(string, TRUE); } static void commands_ping(GSocketConnection *connection, const gchar *cmd, GError **error) { GSocket *socket = g_socket_connection_get_socket(connection); g_socket_send(socket, "pong\n", 5, NULL, error); } static void commands_exit(GSocketConnection *connection, const gchar *cmd, GError **error) { GSocket *socket = g_socket_connection_get_socket(connection); g_socket_close(socket, NULL); } void commands_handle(GSocketConnection *connection, const gchar *cmd, GError **error) { g_debug(cmd); if(g_strncasecmp(cmd, "ping", 4) == 0) { commands_ping(connection, cmd, error); } else if(g_strncasecmp(cmd, "list", 4) == 0) { commands_list(connection, cmd, error); } else if(g_strncasecmp(cmd, "find", 4) == 0) { commands_find(connection, cmd, error); } else if(g_strncasecmp(cmd, "exit", 4) == 0) { commands_exit(connection, cmd, error); } else { g_debug("unknown command"); *error = g_error_new(commands_quark(), 0, "unknown command %s", cmd); } }