1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
#include "commands.h"
#include "music.h"
#include <string.h>
static void commands_list(GSocketConnection *connection, const gchar *cmd) {
GError *error = NULL;
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) {
g_warning("couldn't find directory %s", dirname);
gchar *buf = g_strdup_printf("error: couldn't find directory %s\n", dirname);
GSocket *socket = g_socket_connection_get_socket(connection);
g_socket_send(socket, buf, strlen(buf), NULL, NULL);
g_free(buf);
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) {
g_warning(error->message);
g_error_free(error);
}
g_string_free(string, TRUE);
}
static void commands_find(GSocketConnection *connection, const gchar *cmd) {
GError *error = NULL;
gchar **data = g_strsplit(cmd, " ", 3);
if(g_strv_length(data) != 3) {
const gchar *buf = "syntax: find (artist) search\n";
GSocket *socket = g_socket_connection_get_socket(connection);
g_socket_send(socket, buf, strlen(buf), NULL, NULL);
return;
}
GSList *list = NULL;
g_debug("strlen(%s) == %d", data[1], strlen(data[1]));
if(g_ascii_strcasecmp(data[1], "artist") == 0) {
g_debug("artist search");
list = music_find_artist(data[2]);
} else {
g_debug("unknown search");
}
GString *string = g_string_new(NULL);
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);
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) {
g_warning(error->message);
g_error_free(error);
}
g_string_free(string, TRUE);
}
static void commands_ping(GSocketConnection *connection, const gchar *cmd) {
GError *error = NULL;
GSocket *socket = g_socket_connection_get_socket(connection);
if(g_socket_send(socket, "pong\n", 5, NULL, NULL) == -1) {
g_warning(error->message);
g_error_free(error);
}
}
static void commands_exit(GSocketConnection *connection, const gchar *cmd) {
GSocket *socket = g_socket_connection_get_socket(connection);
g_socket_close(socket, NULL);
}
void commands_handle(GSocketConnection *connection, const gchar *cmd) {
g_debug(cmd);
if(g_strncasecmp(cmd, "ping", 4) == 0) {
commands_ping(connection, cmd);
} else if(g_strncasecmp(cmd, "list", 4) == 0) {
commands_list(connection, cmd);
} else if(g_strncasecmp(cmd, "find", 4) == 0) {
commands_find(connection, cmd);
} else if(g_strncasecmp(cmd, "exit", 4) == 0) {
commands_exit(connection, cmd);
} else {
g_debug("unknown command");
gchar *buf = g_strdup_printf("error: unknown command %s\n", cmd);
GSocket *socket = g_socket_connection_get_socket(connection);
g_socket_send(socket, buf, strlen(buf), NULL, NULL);
g_free(buf);
}
}
|