2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
37 #include "configuration.h"
44 #include "inputline.h"
51 #include "client-common.h"
53 struct disorder_client {
60 disorder_client *disorder_new(int verbose) {
61 disorder_client *c = xmalloc(sizeof (struct disorder_client));
67 /* read a response line.
68 * If @rp@ is not a null pointer, returns the whole response through it.
69 * Return value is the response code, or -1 on error. */
70 static int response(disorder_client *c, char **rp) {
73 if(inputline(c->ident, c->fpin, &r, '\n'))
75 D(("response: %s", r));
78 if(r[0] >= '0' && r[0] <= '9'
79 && r[1] >= '0' && r[1] <= '9'
80 && r[2] >= '0' && r[2] <= '9'
82 return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0';
84 error(0, "invalid reply format from %s", c->ident);
90 * If @rp@ is not a null pointer then the response text (excluding
91 * the status code) is returned through it, UNLESS the response code
93 * Return value is 0 for 2xx responses and -1 otherwise.
95 static int check_response(disorder_client *c, char **rp) {
99 if((rc = response(c, &r)) == -1)
101 else if(rc / 100 == 2) {
103 *rp = (rc % 10 == 9) ? 0 : xstrdup(r + 4);
107 error(0, "from %s: %s", c->ident, utf82mb(r));
112 static int disorder_simple_v(disorder_client *c,
114 const char *cmd, va_list ap) {
120 dynstr_append_string(&d, cmd);
121 while((arg = va_arg(ap, const char *))) {
122 dynstr_append(&d, ' ');
123 dynstr_append_string(&d, quoteutf8(arg));
125 dynstr_append(&d, '\n');
126 dynstr_terminate(&d);
127 D(("command: %s", d.vec));
128 if(fputs(d.vec, c->fpout) < 0 || fflush(c->fpout)) {
129 error(errno, "error writing to %s", c->ident);
133 return check_response(c, rp);
136 /* Execute a simple command with any number of arguments.
137 * @rp@ and return value as for check_response().
139 static int disorder_simple(disorder_client *c,
141 const char *cmd, ...) {
146 ret = disorder_simple_v(c, rp, cmd, ap);
151 static int connect_sock(void *vc,
152 const struct sockaddr *sa,
155 const char *username, *password;
156 disorder_client *c = vc;
159 if(!(username = config->username)) {
160 error(0, "no username configured");
163 if(!(password = config->password)) {
164 for(n = 0; (n < config->allow.n
165 && strcmp(config->allow.s[n].s[0], username)); ++n)
167 if(n < config->allow.n)
168 password = config->allow.s[n].s[1];
170 error(0, "no password configured");
174 return disorder_connect_sock(c, sa, len, username, password, ident);
177 int disorder_connect(disorder_client *c) {
178 return with_sockaddr(c, connect_sock);
181 static int check_running(void attribute((unused)) *c,
182 const struct sockaddr *sa,
184 const char attribute((unused)) *ident) {
187 if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0)
188 fatal(errno, "error calling socket");
189 if(connect(fd, sa, len) < 0) {
190 if(errno == ECONNREFUSED || errno == ENOENT)
193 fatal(errno, "error calling connect");
200 int disorder_running(disorder_client *c) {
201 return with_sockaddr(c, check_running);
204 int disorder_connect_sock(disorder_client *c,
205 const struct sockaddr *sa,
207 const char *username,
208 const char *password,
210 int fd = -1, fd2 = -1, nrvec;
211 unsigned char *nonce;
215 const char *algo = "SHA1";
218 error(0, "no password found");
221 c->fpin = c->fpout = 0;
222 if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
223 error(errno, "error calling socket");
226 if(connect(fd, sa, len) < 0) {
227 error(errno, "error calling connect");
230 if((fd2 = dup(fd)) < 0) {
231 error(errno, "error calling dup");
234 if(!(c->fpin = fdopen(fd, "rb"))) {
235 error(errno, "error calling fdopen");
239 if(!(c->fpout = fdopen(fd2, "wb"))) {
240 error(errno, "error calling fdopen");
244 c->ident = xstrdup(ident);
245 if(disorder_simple(c, &r, 0, (const char *)0))
247 if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
255 if(!(nonce = unhex(*rvec, &nl)))
257 if(!(res = authhash(nonce, nl, password, algo))) goto error;
258 if(disorder_simple(c, 0, "user", username, res, (char *)0))
260 c->user = xstrdup(username);
263 if(c->fpin) fclose(c->fpin);
264 if(c->fpout) fclose(c->fpout);
265 if(fd2 != -1) close(fd2);
266 if(fd != -1) close(fd);
270 int disorder_close(disorder_client *c) {
274 if(fclose(c->fpin) < 0) {
275 error(errno, "error calling fclose");
281 if(fclose(c->fpout) < 0) {
282 error(errno, "error calling fclose");
290 int disorder_become(disorder_client *c, const char *user) {
291 if(disorder_simple(c, 0, "become", user, (char *)0)) return -1;
292 c->user = xstrdup(user);
296 int disorder_play(disorder_client *c, const char *track) {
297 return disorder_simple(c, 0, "play", track, (char *)0);
300 int disorder_remove(disorder_client *c, const char *track) {
301 return disorder_simple(c, 0, "remove", track, (char *)0);
304 int disorder_move(disorder_client *c, const char *track, int delta) {
307 byte_snprintf(d, sizeof d, "%d", delta);
308 return disorder_simple(c, 0, "move", track, d, (char *)0);
311 int disorder_enable(disorder_client *c) {
312 return disorder_simple(c, 0, "enable", (char *)0);
315 int disorder_disable(disorder_client *c) {
316 return disorder_simple(c, 0, "disable", (char *)0);
319 int disorder_scratch(disorder_client *c, const char *id) {
320 return disorder_simple(c, 0, "scratch", id, (char *)0);
323 int disorder_shutdown(disorder_client *c) {
324 return disorder_simple(c, 0, "shutdown", (char *)0);
327 int disorder_reconfigure(disorder_client *c) {
328 return disorder_simple(c, 0, "reconfigure", (char *)0);
331 int disorder_rescan(disorder_client *c) {
332 return disorder_simple(c, 0, "rescan", (char *)0);
335 int disorder_version(disorder_client *c, char **rp) {
336 return disorder_simple(c, rp, "version", (char *)0);
339 static void client_error(const char *msg,
340 void attribute((unused)) *u) {
341 error(0, "error parsing reply: %s", msg);
344 int disorder_playing(disorder_client *c, struct queue_entry **qp) {
346 struct queue_entry *q;
348 if(disorder_simple(c, &r, "playing", (char *)0))
351 q = xmalloc(sizeof *q);
352 if(queue_unmarshall(q, r, client_error, 0))
360 static int disorder_somequeue(disorder_client *c,
361 const char *cmd, struct queue_entry **qp) {
362 struct queue_entry *qh, **qt = &qh, *q;
365 if(disorder_simple(c, 0, cmd, (char *)0))
367 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
368 if(!strcmp(l, ".")) {
373 q = xmalloc(sizeof *q);
374 if(!queue_unmarshall(q, l, client_error, 0)) {
380 error(errno, "error reading %s", c->ident);
382 error(0, "error reading %s: unexpected EOF", c->ident);
386 int disorder_recent(disorder_client *c, struct queue_entry **qp) {
387 return disorder_somequeue(c, "recent", qp);
390 int disorder_queue(disorder_client *c, struct queue_entry **qp) {
391 return disorder_somequeue(c, "queue", qp);
394 static int readlist(disorder_client *c, char ***vecp, int *nvecp) {
399 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
400 if(!strcmp(l, ".")) {
401 vector_terminate(&v);
407 vector_append(&v, l + (*l == '.'));
410 error(errno, "error reading %s", c->ident);
412 error(0, "error reading %s: unexpected EOF", c->ident);
416 static int disorder_simple_list(disorder_client *c,
417 char ***vecp, int *nvecp,
418 const char *cmd, ...) {
423 ret = disorder_simple_v(c, 0, cmd, ap);
426 return readlist(c, vecp, nvecp);
429 int disorder_directories(disorder_client *c, const char *dir, const char *re,
430 char ***vecp, int *nvecp) {
431 return disorder_simple_list(c, vecp, nvecp, "dirs", dir, re, (char *)0);
434 int disorder_files(disorder_client *c, const char *dir, const char *re,
435 char ***vecp, int *nvecp) {
436 return disorder_simple_list(c, vecp, nvecp, "files", dir, re, (char *)0);
439 int disorder_allfiles(disorder_client *c, const char *dir, const char *re,
440 char ***vecp, int *nvecp) {
441 return disorder_simple_list(c, vecp, nvecp, "allfiles", dir, re, (char *)0);
444 char *disorder_user(disorder_client *c) {
448 int disorder_set(disorder_client *c, const char *track,
449 const char *key, const char *value) {
450 return disorder_simple(c, 0, "set", track, key, value, (char *)0);
453 int disorder_unset(disorder_client *c, const char *track,
455 return disorder_simple(c, 0, "unset", track, key, (char *)0);
458 int disorder_get(disorder_client *c,
459 const char *track, const char *key, char **valuep) {
460 return disorder_simple(c, valuep, "get", track, key, (char *)0);
463 static void pref_error_handler(const char *msg,
464 void attribute((unused)) *u) {
465 error(0, "error handling 'prefs' reply: %s", msg);
468 int disorder_prefs(disorder_client *c, const char *track, struct kvp **kp) {
473 if(disorder_simple_list(c, &vec, &nvec, "prefs", track, (char *)0))
475 for(n = 0; n < nvec; ++n) {
476 if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pref_error_handler, 0)))
479 pref_error_handler("malformed response", 0);
482 *kp = k = xmalloc(sizeof *k);
491 static int boolean(const char *cmd, const char *value,
493 if(!strcmp(value, "yes")) *flagp = 1;
494 else if(!strcmp(value, "no")) *flagp = 0;
496 error(0, "malformed response to '%s'", cmd);
502 int disorder_exists(disorder_client *c, const char *track, int *existsp) {
505 if(disorder_simple(c, &v, "exists", track, (char *)0)) return -1;
506 return boolean("exists", v, existsp);
509 int disorder_enabled(disorder_client *c, int *enabledp) {
512 if(disorder_simple(c, &v, "enabled", (char *)0)) return -1;
513 return boolean("enabled", v, enabledp);
516 int disorder_length(disorder_client *c, const char *track,
520 if(disorder_simple(c, &value, "length", track, (char *)0)) return -1;
521 *valuep = atol(value);
525 int disorder_search(disorder_client *c, const char *terms,
526 char ***vecp, int *nvecp) {
527 return disorder_simple_list(c, vecp, nvecp, "search", terms, (char *)0);
530 int disorder_random_enable(disorder_client *c) {
531 return disorder_simple(c, 0, "random-enable", (char *)0);
534 int disorder_random_disable(disorder_client *c) {
535 return disorder_simple(c, 0, "random-disable", (char *)0);
538 int disorder_random_enabled(disorder_client *c, int *enabledp) {
541 if(disorder_simple(c, &v, "random-enabled", (char *)0)) return -1;
542 return boolean("random-enabled", v, enabledp);
545 int disorder_stats(disorder_client *c,
546 char ***vecp, int *nvecp) {
547 return disorder_simple_list(c, vecp, nvecp, "stats", (char *)0);
550 int disorder_set_volume(disorder_client *c, int left, int right) {
553 if(byte_asprintf(&ls, "%d", left) < 0
554 || byte_asprintf(&rs, "%d", right) < 0)
556 if(disorder_simple(c, 0, "volume", ls, rs, (char *)0)) return -1;
560 int disorder_get_volume(disorder_client *c, int *left, int *right) {
563 if(disorder_simple(c, &r, "volume", (char *)0)) return -1;
564 if(sscanf(r, "%d %d", left, right) != 2) {
565 error(0, "error parsing response to 'volume': '%s'", r);
571 int disorder_log(disorder_client *c, struct sink *s) {
574 if(disorder_simple(c, 0, "log", (char *)0)) return -1;
575 while(inputline(c->ident, c->fpin, &l, '\n') >= 0 && strcmp(l, "."))
576 if(sink_printf(s, "%s\n", l) < 0) return -1;
577 if(ferror(c->fpin) || feof(c->fpin)) return -1;
581 int disorder_part(disorder_client *c, char **partp,
582 const char *track, const char *context, const char *part) {
583 return disorder_simple(c, partp, "part", track, context, part, (char *)0);
586 int disorder_resolve(disorder_client *c, char **trackp, const char *track) {
587 return disorder_simple(c, trackp, "resolve", track, (char *)0);
590 int disorder_pause(disorder_client *c) {
591 return disorder_simple(c, 0, "pause", (char *)0);
594 int disorder_resume(disorder_client *c) {
595 return disorder_simple(c, 0, "resume", (char *)0);
598 int disorder_tags(disorder_client *c,
599 char ***vecp, int *nvecp) {
600 return disorder_simple_list(c, vecp, nvecp, "tags", (char *)0);
603 int disorder_set_global(disorder_client *c,
604 const char *key, const char *value) {
605 return disorder_simple(c, 0, "set-global", key, value, (char *)0);
608 int disorder_unset_global(disorder_client *c, const char *key) {
609 return disorder_simple(c, 0, "unset-global", key, (char *)0);
612 int disorder_get_global(disorder_client *c, const char *key, char **valuep) {
613 return disorder_simple(c, valuep, "get-global", key, (char *)0);