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