chiark / gitweb /
comments
[disorder] / lib / client.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
1020001c 3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
460b9539 4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20
21#include <config.h>
22#include "types.h"
23
24#include <sys/types.h>
25#include <sys/socket.h>
26#include <netinet/in.h>
27#include <sys/un.h>
28#include <string.h>
29#include <stdio.h>
30#include <unistd.h>
31#include <errno.h>
32#include <netdb.h>
33#include <stdlib.h>
f0feb22e 34#include <pcre.h>
460b9539 35
36#include "log.h"
37#include "mem.h"
38#include "configuration.h"
39#include "queue.h"
40#include "client.h"
41#include "charset.h"
42#include "hex.h"
43#include "split.h"
44#include "vector.h"
45#include "inputline.h"
46#include "kvp.h"
47#include "syscalls.h"
48#include "printf.h"
49#include "sink.h"
50#include "addr.h"
51#include "authhash.h"
52#include "client-common.h"
5df73aeb 53#include "rights.h"
f0feb22e 54#include "trackdb.h"
460b9539 55
56struct disorder_client {
57 FILE *fpin, *fpout;
58 char *ident;
59 char *user;
60 int verbose;
61};
62
63disorder_client *disorder_new(int verbose) {
64 disorder_client *c = xmalloc(sizeof (struct disorder_client));
65
66 c->verbose = verbose;
67 return c;
68}
69
70/* read a response line.
71 * If @rp@ is not a null pointer, returns the whole response through it.
72 * Return value is the response code, or -1 on error. */
73static int response(disorder_client *c, char **rp) {
74 char *r;
75
76 if(inputline(c->ident, c->fpin, &r, '\n'))
77 return -1;
78 D(("response: %s", r));
79 if(rp)
80 *rp = r;
81 if(r[0] >= '0' && r[0] <= '9'
82 && r[1] >= '0' && r[1] <= '9'
83 && r[2] >= '0' && r[2] <= '9'
84 && r[3] == ' ')
85 return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0';
86 else {
87 error(0, "invalid reply format from %s", c->ident);
88 return -1;
89 }
90}
91
92/* Read a response.
93 * If @rp@ is not a null pointer then the response text (excluding
94 * the status code) is returned through it, UNLESS the response code
95 * is xx9.
96 * Return value is 0 for 2xx responses and -1 otherwise.
97 */
98static int check_response(disorder_client *c, char **rp) {
99 int rc;
100 char *r;
101
102 if((rc = response(c, &r)) == -1)
103 return -1;
104 else if(rc / 100 == 2) {
105 if(rp)
106 *rp = (rc % 10 == 9) ? 0 : xstrdup(r + 4);
107 return 0;
108 } else {
109 if(c->verbose)
110 error(0, "from %s: %s", c->ident, utf82mb(r));
111 return -1;
112 }
113}
114
115static int disorder_simple_v(disorder_client *c,
116 char **rp,
117 const char *cmd, va_list ap) {
118 const char *arg;
119 struct dynstr d;
120
121 if(cmd) {
122 dynstr_init(&d);
123 dynstr_append_string(&d, cmd);
124 while((arg = va_arg(ap, const char *))) {
125 dynstr_append(&d, ' ');
126 dynstr_append_string(&d, quoteutf8(arg));
127 }
128 dynstr_append(&d, '\n');
129 dynstr_terminate(&d);
130 D(("command: %s", d.vec));
131 if(fputs(d.vec, c->fpout) < 0 || fflush(c->fpout)) {
132 error(errno, "error writing to %s", c->ident);
133 return -1;
134 }
135 }
136 return check_response(c, rp);
137}
138
139/* Execute a simple command with any number of arguments.
140 * @rp@ and return value as for check_response().
141 */
142static int disorder_simple(disorder_client *c,
143 char **rp,
144 const char *cmd, ...) {
145 va_list ap;
146 int ret;
147
148 va_start(ap, cmd);
149 ret = disorder_simple_v(c, rp, cmd, ap);
150 va_end(ap);
151 return ret;
152}
153
154static int connect_sock(void *vc,
155 const struct sockaddr *sa,
156 socklen_t len,
157 const char *ident) {
158 const char *username, *password;
159 disorder_client *c = vc;
460b9539 160
161 if(!(username = config->username)) {
162 error(0, "no username configured");
163 return -1;
164 }
f0feb22e
RK
165 password = config->password;
166 if(!password) {
167 /* Maybe we can read the database */
168 /* TODO failure to open the database should not be fatal */
169 trackdb_init(TRACKDB_NO_RECOVER|TRACKDB_NO_UPGRADE);
170 trackdb_open(TRACKDB_READ_ONLY);
171 password = trackdb_get_password(username);
172 trackdb_close();
173 }
174 if(!password) {
175 /* Oh well */
176 error(0, "no password configured");
177 return -1;
460b9539 178 }
179 return disorder_connect_sock(c, sa, len, username, password, ident);
180}
181
182int disorder_connect(disorder_client *c) {
183 return with_sockaddr(c, connect_sock);
184}
185
186static int check_running(void attribute((unused)) *c,
187 const struct sockaddr *sa,
188 socklen_t len,
189 const char attribute((unused)) *ident) {
190 int fd, ret;
191
192 if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0)
193 fatal(errno, "error calling socket");
194 if(connect(fd, sa, len) < 0) {
195 if(errno == ECONNREFUSED || errno == ENOENT)
196 ret = 0;
197 else
198 fatal(errno, "error calling connect");
199 } else
200 ret = 1;
201 xclose(fd);
202 return ret;
203}
204
205int disorder_running(disorder_client *c) {
206 return with_sockaddr(c, check_running);
207}
208
209int disorder_connect_sock(disorder_client *c,
210 const struct sockaddr *sa,
211 socklen_t len,
212 const char *username,
213 const char *password,
214 const char *ident) {
637fdea3 215 int fd = -1, fd2 = -1, nrvec;
460b9539 216 unsigned char *nonce;
217 size_t nl;
218 const char *res;
637fdea3 219 char *r, **rvec;
7b32e917 220 const char *protocol, *algorithm, *challenge;
460b9539 221
222 if(!password) {
223 error(0, "no password found");
224 return -1;
225 }
226 c->fpin = c->fpout = 0;
227 if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
228 error(errno, "error calling socket");
229 return -1;
230 }
231 if(connect(fd, sa, len) < 0) {
232 error(errno, "error calling connect");
233 goto error;
234 }
235 if((fd2 = dup(fd)) < 0) {
236 error(errno, "error calling dup");
237 goto error;
238 }
239 if(!(c->fpin = fdopen(fd, "rb"))) {
240 error(errno, "error calling fdopen");
241 goto error;
242 }
243 fd = -1;
244 if(!(c->fpout = fdopen(fd2, "wb"))) {
245 error(errno, "error calling fdopen");
246 goto error;
247 }
248 fd2 = -1;
249 c->ident = xstrdup(ident);
250 if(disorder_simple(c, &r, 0, (const char *)0))
251 return -1;
637fdea3
RK
252 if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
253 return -1;
7b32e917
RK
254 if(nrvec != 3) {
255 error(0, "cannot parse server greeting %s", r);
256 return -1;
637fdea3 257 }
7b32e917
RK
258 protocol = *rvec++;
259 if(strcmp(protocol, "2")) {
260 error(0, "unknown protocol version: %s", protocol);
637fdea3 261 return -1;
7b32e917
RK
262 }
263 algorithm = *rvec++;
264 challenge = *rvec++;
265 if(!(nonce = unhex(challenge, &nl)))
460b9539 266 return -1;
7b32e917 267 if(!(res = authhash(nonce, nl, password, algorithm))) goto error;
460b9539 268 if(disorder_simple(c, 0, "user", username, res, (char *)0))
269 return -1;
270 c->user = xstrdup(username);
271 return 0;
272error:
273 if(c->fpin) fclose(c->fpin);
274 if(c->fpout) fclose(c->fpout);
275 if(fd2 != -1) close(fd2);
276 if(fd != -1) close(fd);
277 return -1;
278}
279
280int disorder_close(disorder_client *c) {
281 int ret = 0;
282
283 if(c->fpin) {
284 if(fclose(c->fpin) < 0) {
285 error(errno, "error calling fclose");
286 ret = -1;
287 }
288 c->fpin = 0;
289 }
290 if(c->fpout) {
291 if(fclose(c->fpout) < 0) {
292 error(errno, "error calling fclose");
293 ret = -1;
294 }
295 c->fpout = 0;
296 }
297 return 0;
298}
299
300int disorder_become(disorder_client *c, const char *user) {
301 if(disorder_simple(c, 0, "become", user, (char *)0)) return -1;
302 c->user = xstrdup(user);
303 return 0;
304}
305
306int disorder_play(disorder_client *c, const char *track) {
307 return disorder_simple(c, 0, "play", track, (char *)0);
308}
309
310int disorder_remove(disorder_client *c, const char *track) {
311 return disorder_simple(c, 0, "remove", track, (char *)0);
312}
313
314int disorder_move(disorder_client *c, const char *track, int delta) {
315 char d[16];
316
317 byte_snprintf(d, sizeof d, "%d", delta);
318 return disorder_simple(c, 0, "move", track, d, (char *)0);
319}
320
321int disorder_enable(disorder_client *c) {
322 return disorder_simple(c, 0, "enable", (char *)0);
323}
324
325int disorder_disable(disorder_client *c) {
326 return disorder_simple(c, 0, "disable", (char *)0);
327}
328
329int disorder_scratch(disorder_client *c, const char *id) {
330 return disorder_simple(c, 0, "scratch", id, (char *)0);
331}
332
333int disorder_shutdown(disorder_client *c) {
334 return disorder_simple(c, 0, "shutdown", (char *)0);
335}
336
337int disorder_reconfigure(disorder_client *c) {
338 return disorder_simple(c, 0, "reconfigure", (char *)0);
339}
340
341int disorder_rescan(disorder_client *c) {
342 return disorder_simple(c, 0, "rescan", (char *)0);
343}
344
7b32e917
RK
345static int dequote(int rc, char **rp) {
346 char **rr;
347 if(!rc){
348 if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) {
349 *rp = *rr;
350 return 0;
351 }
352 error(0, "invalid reply: %s", *rp);
353 }
354 return -1;
355}
356
460b9539 357int disorder_version(disorder_client *c, char **rp) {
7b32e917 358 return dequote(disorder_simple(c, rp, "version", (char *)0), rp);
460b9539 359}
360
361static void client_error(const char *msg,
362 void attribute((unused)) *u) {
363 error(0, "error parsing reply: %s", msg);
364}
365
366int disorder_playing(disorder_client *c, struct queue_entry **qp) {
367 char *r;
368 struct queue_entry *q;
369
370 if(disorder_simple(c, &r, "playing", (char *)0))
371 return -1;
372 if(r) {
373 q = xmalloc(sizeof *q);
374 if(queue_unmarshall(q, r, client_error, 0))
375 return -1;
376 *qp = q;
377 } else
378 *qp = 0;
379 return 0;
380}
381
382static int disorder_somequeue(disorder_client *c,
383 const char *cmd, struct queue_entry **qp) {
384 struct queue_entry *qh, **qt = &qh, *q;
385 char *l;
386
387 if(disorder_simple(c, 0, cmd, (char *)0))
388 return -1;
389 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
390 if(!strcmp(l, ".")) {
391 *qt = 0;
392 *qp = qh;
393 return 0;
394 }
395 q = xmalloc(sizeof *q);
396 if(!queue_unmarshall(q, l, client_error, 0)) {
397 *qt = q;
398 qt = &q->next;
399 }
400 }
401 if(ferror(c->fpin))
402 error(errno, "error reading %s", c->ident);
403 else
404 error(0, "error reading %s: unexpected EOF", c->ident);
405 return -1;
406}
407
408int disorder_recent(disorder_client *c, struct queue_entry **qp) {
409 return disorder_somequeue(c, "recent", qp);
410}
411
412int disorder_queue(disorder_client *c, struct queue_entry **qp) {
413 return disorder_somequeue(c, "queue", qp);
414}
415
416static int readlist(disorder_client *c, char ***vecp, int *nvecp) {
417 char *l;
418 struct vector v;
419
420 vector_init(&v);
421 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
422 if(!strcmp(l, ".")) {
423 vector_terminate(&v);
424 if(nvecp)
425 *nvecp = v.nvec;
426 *vecp = v.vec;
427 return 0;
428 }
429 vector_append(&v, l + (*l == '.'));
430 }
431 if(ferror(c->fpin))
432 error(errno, "error reading %s", c->ident);
433 else
434 error(0, "error reading %s: unexpected EOF", c->ident);
435 return -1;
436}
437
438static int disorder_simple_list(disorder_client *c,
439 char ***vecp, int *nvecp,
440 const char *cmd, ...) {
441 va_list ap;
442 int ret;
443
444 va_start(ap, cmd);
445 ret = disorder_simple_v(c, 0, cmd, ap);
446 va_end(ap);
447 if(ret) return ret;
448 return readlist(c, vecp, nvecp);
449}
450
451int disorder_directories(disorder_client *c, const char *dir, const char *re,
452 char ***vecp, int *nvecp) {
453 return disorder_simple_list(c, vecp, nvecp, "dirs", dir, re, (char *)0);
454}
455
456int disorder_files(disorder_client *c, const char *dir, const char *re,
457 char ***vecp, int *nvecp) {
458 return disorder_simple_list(c, vecp, nvecp, "files", dir, re, (char *)0);
459}
460
461int disorder_allfiles(disorder_client *c, const char *dir, const char *re,
462 char ***vecp, int *nvecp) {
463 return disorder_simple_list(c, vecp, nvecp, "allfiles", dir, re, (char *)0);
464}
465
466char *disorder_user(disorder_client *c) {
467 return c->user;
468}
469
470int disorder_set(disorder_client *c, const char *track,
471 const char *key, const char *value) {
472 return disorder_simple(c, 0, "set", track, key, value, (char *)0);
473}
474
475int disorder_unset(disorder_client *c, const char *track,
476 const char *key) {
477 return disorder_simple(c, 0, "unset", track, key, (char *)0);
478}
479
480int disorder_get(disorder_client *c,
481 const char *track, const char *key, char **valuep) {
7b32e917
RK
482 return dequote(disorder_simple(c, valuep, "get", track, key, (char *)0),
483 valuep);
460b9539 484}
485
486static void pref_error_handler(const char *msg,
487 void attribute((unused)) *u) {
488 error(0, "error handling 'prefs' reply: %s", msg);
489}
490
491int disorder_prefs(disorder_client *c, const char *track, struct kvp **kp) {
492 char **vec, **pvec;
493 int nvec, npvec, n;
494 struct kvp *k;
495
496 if(disorder_simple_list(c, &vec, &nvec, "prefs", track, (char *)0))
497 return -1;
498 for(n = 0; n < nvec; ++n) {
499 if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pref_error_handler, 0)))
500 return -1;
501 if(npvec != 2) {
502 pref_error_handler("malformed response", 0);
503 return -1;
504 }
505 *kp = k = xmalloc(sizeof *k);
506 k->name = pvec[0];
507 k->value = pvec[1];
508 kp = &k->next;
509 }
510 *kp = 0;
511 return 0;
512}
513
514static int boolean(const char *cmd, const char *value,
515 int *flagp) {
516 if(!strcmp(value, "yes")) *flagp = 1;
517 else if(!strcmp(value, "no")) *flagp = 0;
518 else {
519 error(0, "malformed response to '%s'", cmd);
520 return -1;
521 }
522 return 0;
523}
524
525int disorder_exists(disorder_client *c, const char *track, int *existsp) {
526 char *v;
527
528 if(disorder_simple(c, &v, "exists", track, (char *)0)) return -1;
529 return boolean("exists", v, existsp);
530}
531
532int disorder_enabled(disorder_client *c, int *enabledp) {
533 char *v;
534
535 if(disorder_simple(c, &v, "enabled", (char *)0)) return -1;
536 return boolean("enabled", v, enabledp);
537}
538
539int disorder_length(disorder_client *c, const char *track,
540 long *valuep) {
541 char *value;
542
543 if(disorder_simple(c, &value, "length", track, (char *)0)) return -1;
544 *valuep = atol(value);
545 return 0;
546}
547
548int disorder_search(disorder_client *c, const char *terms,
549 char ***vecp, int *nvecp) {
550 return disorder_simple_list(c, vecp, nvecp, "search", terms, (char *)0);
551}
552
553int disorder_random_enable(disorder_client *c) {
554 return disorder_simple(c, 0, "random-enable", (char *)0);
555}
556
557int disorder_random_disable(disorder_client *c) {
558 return disorder_simple(c, 0, "random-disable", (char *)0);
559}
560
561int disorder_random_enabled(disorder_client *c, int *enabledp) {
562 char *v;
563
564 if(disorder_simple(c, &v, "random-enabled", (char *)0)) return -1;
565 return boolean("random-enabled", v, enabledp);
566}
567
568int disorder_stats(disorder_client *c,
569 char ***vecp, int *nvecp) {
570 return disorder_simple_list(c, vecp, nvecp, "stats", (char *)0);
571}
572
573int disorder_set_volume(disorder_client *c, int left, int right) {
574 char *ls, *rs;
575
576 if(byte_asprintf(&ls, "%d", left) < 0
577 || byte_asprintf(&rs, "%d", right) < 0)
578 return -1;
579 if(disorder_simple(c, 0, "volume", ls, rs, (char *)0)) return -1;
580 return 0;
581}
582
583int disorder_get_volume(disorder_client *c, int *left, int *right) {
584 char *r;
585
586 if(disorder_simple(c, &r, "volume", (char *)0)) return -1;
587 if(sscanf(r, "%d %d", left, right) != 2) {
588 error(0, "error parsing response to 'volume': '%s'", r);
589 return -1;
590 }
591 return 0;
592}
593
594int disorder_log(disorder_client *c, struct sink *s) {
595 char *l;
596
597 if(disorder_simple(c, 0, "log", (char *)0)) return -1;
598 while(inputline(c->ident, c->fpin, &l, '\n') >= 0 && strcmp(l, "."))
599 if(sink_printf(s, "%s\n", l) < 0) return -1;
600 if(ferror(c->fpin) || feof(c->fpin)) return -1;
601 return 0;
602}
603
604int disorder_part(disorder_client *c, char **partp,
605 const char *track, const char *context, const char *part) {
7b32e917
RK
606 return dequote(disorder_simple(c, partp, "part",
607 track, context, part, (char *)0), partp);
460b9539 608}
609
610int disorder_resolve(disorder_client *c, char **trackp, const char *track) {
7b32e917
RK
611 return dequote(disorder_simple(c, trackp, "resolve", track, (char *)0),
612 trackp);
460b9539 613}
614
615int disorder_pause(disorder_client *c) {
616 return disorder_simple(c, 0, "pause", (char *)0);
617}
618
619int disorder_resume(disorder_client *c) {
620 return disorder_simple(c, 0, "resume", (char *)0);
621}
622
623int disorder_tags(disorder_client *c,
624 char ***vecp, int *nvecp) {
625 return disorder_simple_list(c, vecp, nvecp, "tags", (char *)0);
626}
627
c3be4f19
RK
628int disorder_users(disorder_client *c,
629 char ***vecp, int *nvecp) {
630 return disorder_simple_list(c, vecp, nvecp, "users", (char *)0);
631}
632
2a10b70b
RK
633/** @brief Get recentl added tracks
634 * @param c Client
635 * @param vecp Where to store pointer to list
636 * @param nvecp Where to store count
637 * @param max Maximum tracks to fetch, or 0 for all available
638 * @return 0 on success, non-0 on error
639 */
640int disorder_new_tracks(disorder_client *c,
641 char ***vecp, int *nvecp,
642 int max) {
643 char limit[32];
644
645 sprintf(limit, "%d", max);
646 return disorder_simple_list(c, vecp, nvecp, "new", limit, (char *)0);
647}
648
460b9539 649int disorder_set_global(disorder_client *c,
650 const char *key, const char *value) {
651 return disorder_simple(c, 0, "set-global", key, value, (char *)0);
652}
653
654int disorder_unset_global(disorder_client *c, const char *key) {
655 return disorder_simple(c, 0, "unset-global", key, (char *)0);
656}
657
658int disorder_get_global(disorder_client *c, const char *key, char **valuep) {
7b32e917
RK
659 return dequote(disorder_simple(c, valuep, "get-global", key, (char *)0),
660 valuep);
460b9539 661}
662
ca831831
RK
663int disorder_rtp_address(disorder_client *c, char **addressp, char **portp) {
664 char *r;
665 int rc, n;
666 char **vec;
667
668 if((rc = disorder_simple(c, &r, "rtp-address", (char *)0)))
669 return rc;
670 vec = split(r, &n, SPLIT_QUOTES, 0, 0);
671 if(n != 2) {
672 error(0, "malformed rtp-address reply");
673 return -1;
674 }
675 *addressp = vec[0];
676 *portp = vec[1];
677 return 0;
678}
679
f0feb22e 680int disorder_adduser(disorder_client *c,
0f55e905
RK
681 const char *user, const char *password,
682 const char *rights) {
683 return disorder_simple(c, 0, "adduser", user, password, rights, (char *)0);
f0feb22e
RK
684}
685
686int disorder_deluser(disorder_client *c, const char *user) {
687 return disorder_simple(c, 0, "deluser", user, (char *)0);
688}
689
a55c70c7
RK
690int disorder_userinfo(disorder_client *c, const char *user, const char *key,
691 char **valuep) {
7b32e917
RK
692 return dequote(disorder_simple(c, valuep, "userinfo", user, key, (char *)0),
693 valuep);
5df73aeb
RK
694}
695
696int disorder_edituser(disorder_client *c, const char *user,
697 const char *key, const char *value) {
698 return disorder_simple(c, 0, "edituser", user, key, value, (char *)0);
699}
700
ba39faf6
RK
701int disorder_register(disorder_client *c, const char *user,
702 const char *password, const char *email,
703 char **confirmp) {
704 return dequote(disorder_simple(c, confirmp, "register",
705 user, password, email, (char *)0),
706 confirmp);
707}
708
709int disorder_confirm(disorder_client *c, const char *confirm) {
710 return disorder_simple(c, 0, "confirm", confirm, (char *)0);
711}
712
07969f9b
RK
713int disorder_make_cookie(disorder_client *c, char **cookiep) {
714 return dequote(disorder_simple(c, cookiep, "make-cookie", (char *)0),
715 cookiep);
716}
717
460b9539 718/*
719Local Variables:
720c-basic-offset:2
721comment-column:40
722End:
723*/