chiark / gitweb /
Further ALSA underrun fiddling.
[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 */
c4e2cfdd
RK
20/** @file lib/client.c
21 * @brief Simple C client
22 *
158d0961 23 * See @ref lib/eclient.c for an asynchronous-capable client
c4e2cfdd
RK
24 * implementation.
25 */
460b9539 26
27#include <config.h>
28#include "types.h"
29
30#include <sys/types.h>
31#include <sys/socket.h>
32#include <netinet/in.h>
33#include <sys/un.h>
34#include <string.h>
35#include <stdio.h>
36#include <unistd.h>
37#include <errno.h>
38#include <netdb.h>
39#include <stdlib.h>
f0feb22e 40#include <pcre.h>
460b9539 41
42#include "log.h"
43#include "mem.h"
44#include "configuration.h"
45#include "queue.h"
46#include "client.h"
47#include "charset.h"
48#include "hex.h"
49#include "split.h"
50#include "vector.h"
51#include "inputline.h"
52#include "kvp.h"
53#include "syscalls.h"
54#include "printf.h"
55#include "sink.h"
56#include "addr.h"
57#include "authhash.h"
58#include "client-common.h"
5df73aeb 59#include "rights.h"
f0feb22e 60#include "trackdb.h"
460b9539 61
0590cedc 62/** @brief Client handle contents */
460b9539 63struct disorder_client {
0590cedc
RK
64 /** @brief Stream to read from */
65 FILE *fpin;
66 /** @brief Stream to write to */
67 FILE *fpout;
68 /** @brief Peer description */
460b9539 69 char *ident;
0590cedc 70 /** @brief Username */
460b9539 71 char *user;
0590cedc 72 /** @brief Report errors to @c stderr */
460b9539 73 int verbose;
0590cedc
RK
74 /** @brief Last error string */
75 char *last;
460b9539 76};
77
c4e2cfdd
RK
78/** @brief Create a new client
79 * @param verbose If nonzero, write extra junk to stderr
80 * @return Pointer to new client
81 *
fdf98378 82 * You must call disorder_connect(), disorder_connect_user() or
83 * disorder_connect_cookie() to connect it. Use disorder_close() to
84 * dispose of the client when finished with it.
c4e2cfdd 85 */
460b9539 86disorder_client *disorder_new(int verbose) {
87 disorder_client *c = xmalloc(sizeof (struct disorder_client));
88
89 c->verbose = verbose;
90 return c;
91}
92
c4e2cfdd
RK
93/** @brief Read a response line
94 * @param c Client
95 * @param rp Where to store response, or NULL (UTF-8)
96 * @return Response code 0-999 or -1 on error
97 */
460b9539 98static int response(disorder_client *c, char **rp) {
99 char *r;
100
101 if(inputline(c->ident, c->fpin, &r, '\n'))
102 return -1;
103 D(("response: %s", r));
104 if(rp)
105 *rp = r;
106 if(r[0] >= '0' && r[0] <= '9'
107 && r[1] >= '0' && r[1] <= '9'
108 && r[2] >= '0' && r[2] <= '9'
bb037be2 109 && r[3] == ' ') {
110 c->last = r + 4;
460b9539 111 return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0';
bb037be2 112 } else {
460b9539 113 error(0, "invalid reply format from %s", c->ident);
114 return -1;
115 }
116}
117
bb037be2 118/** @brief Return last response string
119 * @param c Client
120 * @return Last response string (UTF-8, English) or NULL
121 */
122const char *disorder_last(disorder_client *c) {
123 return c->last;
124}
125
c4e2cfdd
RK
126/** @brief Read and partially parse a response
127 * @param c Client
128 * @param rp Where to store response text (or NULL) (UTF-8)
129 * @return 0 on success, non-0 on error
130 *
131 * 5xx responses count as errors.
132 *
133 * @p rp will NOT be filled in for xx9 responses (where it is just
134 * commentary for a command where it would normally be meaningful).
135 *
136 * NB that the response will NOT be converted to the local encoding.
460b9539 137 */
138static int check_response(disorder_client *c, char **rp) {
139 int rc;
140 char *r;
141
142 if((rc = response(c, &r)) == -1)
143 return -1;
144 else if(rc / 100 == 2) {
145 if(rp)
146 *rp = (rc % 10 == 9) ? 0 : xstrdup(r + 4);
147 return 0;
148 } else {
149 if(c->verbose)
150 error(0, "from %s: %s", c->ident, utf82mb(r));
2bead829 151 return rc;
460b9539 152 }
153}
154
c4e2cfdd
RK
155/** @brief Issue a command and parse a simple response
156 * @param c Client
157 * @param rp Where to store result, or NULL
158 * @param cmd Command
159 * @param ap Arguments (UTF-8), terminated by (char *)0
160 * @return 0 on success, non-0 on error
161 *
162 * 5xx responses count as errors.
163 *
164 * @p rp will NOT be filled in for xx9 responses (where it is just
165 * commentary for a command where it would normally be meaningful).
166 *
167 * NB that the response will NOT be converted to the local encoding
168 * nor will quotes be stripped. See dequote().
169 */
460b9539 170static int disorder_simple_v(disorder_client *c,
171 char **rp,
172 const char *cmd, va_list ap) {
173 const char *arg;
174 struct dynstr d;
175
62ef2216 176 if(!c->fpout) {
177 error(0, "not connected to server");
178 return -1;
179 }
460b9539 180 if(cmd) {
181 dynstr_init(&d);
182 dynstr_append_string(&d, cmd);
183 while((arg = va_arg(ap, const char *))) {
184 dynstr_append(&d, ' ');
185 dynstr_append_string(&d, quoteutf8(arg));
186 }
187 dynstr_append(&d, '\n');
188 dynstr_terminate(&d);
189 D(("command: %s", d.vec));
190 if(fputs(d.vec, c->fpout) < 0 || fflush(c->fpout)) {
191 error(errno, "error writing to %s", c->ident);
192 return -1;
193 }
194 }
195 return check_response(c, rp);
196}
197
c4e2cfdd
RK
198/** @brief Issue a command and parse a simple response
199 * @param c Client
200 * @param rp Where to store result, or NULL (UTF-8)
201 * @param cmd Command
202 * @return 0 on success, non-0 on error
203 *
204 * The remaining arguments are command arguments, terminated by (char
205 * *)0. They should be in UTF-8.
206 *
207 * 5xx responses count as errors.
208 *
209 * @p rp will NOT be filled in for xx9 responses (where it is just
210 * commentary for a command where it would normally be meaningful).
211 *
212 * NB that the response will NOT be converted to the local encoding
213 * nor will quotes be stripped. See dequote().
460b9539 214 */
215static int disorder_simple(disorder_client *c,
216 char **rp,
217 const char *cmd, ...) {
218 va_list ap;
219 int ret;
220
221 va_start(ap, cmd);
222 ret = disorder_simple_v(c, rp, cmd, ap);
223 va_end(ap);
224 return ret;
225}
226
0227f67d
RK
227/** @brief Dequote a result string
228 * @param rc 0 on success, non-0 on error
229 * @param rp Where result string is stored (UTF-8)
230 * @return @p rc
231 *
232 * This is used as a wrapper around disorder_simple() to dequote
233 * results in place.
234 */
235static int dequote(int rc, char **rp) {
236 char **rr;
2bead829 237
0227f67d
RK
238 if(!rc) {
239 if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) {
240 *rp = *rr;
241 return 0;
242 }
243 error(0, "invalid reply: %s", *rp);
460b9539 244 }
2bead829 245 return rc;
460b9539 246}
247
0227f67d 248/** @brief Generic connection routine
c4e2cfdd 249 * @param c Client
0227f67d
RK
250 * @param username Username to log in with or NULL
251 * @param password Password to log in with or NULL
252 * @param cookie Cookie to log in with or NULL
c4e2cfdd
RK
253 * @return 0 on success, non-0 on error
254 *
0227f67d
RK
255 * @p cookie is tried first if not NULL. If it is NULL then @p
256 * username must not be. If @p username is not NULL then nor may @p
257 * password be.
c4e2cfdd 258 */
0227f67d
RK
259static int disorder_connect_generic(disorder_client *c,
260 const char *username,
261 const char *password,
262 const char *cookie) {
2bead829 263 int fd = -1, fd2 = -1, nrvec, rc;
460b9539 264 unsigned char *nonce;
265 size_t nl;
266 const char *res;
637fdea3 267 char *r, **rvec;
7b32e917 268 const char *protocol, *algorithm, *challenge;
0227f67d
RK
269 struct sockaddr *sa;
270 socklen_t salen;
460b9539 271
0227f67d 272 if((salen = find_server(&sa, &c->ident)) == (socklen_t)-1)
460b9539 273 return -1;
460b9539 274 c->fpin = c->fpout = 0;
275 if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
276 error(errno, "error calling socket");
277 return -1;
278 }
0227f67d 279 if(connect(fd, sa, salen) < 0) {
460b9539 280 error(errno, "error calling connect");
281 goto error;
282 }
283 if((fd2 = dup(fd)) < 0) {
284 error(errno, "error calling dup");
285 goto error;
286 }
287 if(!(c->fpin = fdopen(fd, "rb"))) {
288 error(errno, "error calling fdopen");
289 goto error;
290 }
291 fd = -1;
292 if(!(c->fpout = fdopen(fd2, "wb"))) {
293 error(errno, "error calling fdopen");
294 goto error;
295 }
296 fd2 = -1;
2bead829 297 if((rc = disorder_simple(c, &r, 0, (const char *)0)))
298 goto error_rc;
637fdea3 299 if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
2bead829 300 goto error;
7b32e917
RK
301 if(nrvec != 3) {
302 error(0, "cannot parse server greeting %s", r);
2bead829 303 goto error;
637fdea3 304 }
7b32e917
RK
305 protocol = *rvec++;
306 if(strcmp(protocol, "2")) {
307 error(0, "unknown protocol version: %s", protocol);
2bead829 308 goto error;
7b32e917
RK
309 }
310 algorithm = *rvec++;
311 challenge = *rvec++;
312 if(!(nonce = unhex(challenge, &nl)))
2bead829 313 goto error;
0227f67d
RK
314 if(cookie) {
315 if(!dequote(disorder_simple(c, &c->user, "cookie", cookie, (char *)0),
316 &c->user))
317 return 0; /* success */
318 if(!username) {
319 error(0, "cookie did not work and no username available");
2bead829 320 goto error;
0227f67d
RK
321 }
322 }
7b32e917 323 if(!(res = authhash(nonce, nl, password, algorithm))) goto error;
2bead829 324 if((rc = disorder_simple(c, 0, "user", username, res, (char *)0)))
325 goto error_rc;
460b9539 326 c->user = xstrdup(username);
327 return 0;
328error:
2bead829 329 rc = -1;
330error_rc:
331 if(c->fpin) {
332 fclose(c->fpin);
333 c->fpin = 0;
334 }
335 if(c->fpout) {
336 fclose(c->fpout);
337 c->fpout = 0;
338 }
460b9539 339 if(fd2 != -1) close(fd2);
340 if(fd != -1) close(fd);
2bead829 341 return rc;
460b9539 342}
343
fdf98378 344/** @brief Connect a client with a specified username and password
345 * @param c Client
346 * @param username Username to log in with
347 * @param password Password to log in with
348 * @return 0 on success, non-0 on error
349 */
350int disorder_connect_user(disorder_client *c,
351 const char *username,
352 const char *password) {
353 return disorder_connect_generic(c,
354 username,
355 password,
356 0);
357}
358
0227f67d
RK
359/** @brief Connect a client
360 * @param c Client
361 * @return 0 on success, non-0 on error
362 *
363 * The connection will use the username and password found in @ref
364 * config, or directly from the database if no password is found and
365 * the database is readable (usually only for root).
366 */
367int disorder_connect(disorder_client *c) {
368 const char *username, *password;
369
370 if(!(username = config->username)) {
371 error(0, "no username configured");
372 return -1;
373 }
374 password = config->password;
375 if(!password) {
376 /* Maybe we can read the database */
377 /* TODO failure to open the database should not be fatal */
378 trackdb_init(TRACKDB_NO_RECOVER|TRACKDB_NO_UPGRADE);
379 trackdb_open(TRACKDB_READ_ONLY);
380 password = trackdb_get_password(username);
381 trackdb_close();
382 }
383 if(!password) {
384 /* Oh well */
385 error(0, "no password configured");
386 return -1;
387 }
388 return disorder_connect_generic(c,
389 username,
390 password,
391 0);
392}
393
394/** @brief Connect a client
395 * @param c Client
396 * @param cookie Cookie to log in with, or NULL
397 * @return 0 on success, non-0 on error
398 *
399 * If @p cookie is NULL or does not work then we attempt to log in as
400 * guest instead (so when the cookie expires only an extra round trip
401 * is needed rathre than a complete new login).
402 */
403int disorder_connect_cookie(disorder_client *c,
404 const char *cookie) {
405 return disorder_connect_generic(c,
406 "guest",
407 "",
408 cookie);
409}
410
c4e2cfdd
RK
411/** @brief Close a client
412 * @param c Client
413 * @return 0 on succcess, non-0 on errior
414 *
415 * The client is still closed even on error. It might well be
416 * appropriate to ignore the return value.
417 */
460b9539 418int disorder_close(disorder_client *c) {
419 int ret = 0;
420
421 if(c->fpin) {
422 if(fclose(c->fpin) < 0) {
423 error(errno, "error calling fclose");
424 ret = -1;
425 }
426 c->fpin = 0;
427 }
428 if(c->fpout) {
429 if(fclose(c->fpout) < 0) {
430 error(errno, "error calling fclose");
431 ret = -1;
432 }
433 c->fpout = 0;
434 }
0227f67d
RK
435 c->ident = 0;
436 c->user = 0;
460b9539 437 return 0;
438}
439
c4e2cfdd
RK
440/** @brief Play a track
441 * @param c Client
442 * @param track Track to play (UTF-8)
443 * @return 0 on success, non-0 on error
444 */
460b9539 445int disorder_play(disorder_client *c, const char *track) {
446 return disorder_simple(c, 0, "play", track, (char *)0);
447}
448
c4e2cfdd
RK
449/** @brief Remove a track
450 * @param c Client
451 * @param track Track to remove (UTF-8)
452 * @return 0 on success, non-0 on error
453 */
460b9539 454int disorder_remove(disorder_client *c, const char *track) {
455 return disorder_simple(c, 0, "remove", track, (char *)0);
456}
457
c4e2cfdd
RK
458/** @brief Move a track
459 * @param c Client
460 * @param track Track to move (UTF-8)
461 * @param delta Distance to move by
462 * @return 0 on success, non-0 on error
463 */
460b9539 464int disorder_move(disorder_client *c, const char *track, int delta) {
465 char d[16];
466
467 byte_snprintf(d, sizeof d, "%d", delta);
468 return disorder_simple(c, 0, "move", track, d, (char *)0);
469}
470
c4e2cfdd
RK
471/** @brief Enable play
472 * @param c Client
473 * @return 0 on success, non-0 on error
474 */
460b9539 475int disorder_enable(disorder_client *c) {
476 return disorder_simple(c, 0, "enable", (char *)0);
477}
478
c4e2cfdd
RK
479/** @brief Disable play
480 * @param c Client
481 * @return 0 on success, non-0 on error
482 */
460b9539 483int disorder_disable(disorder_client *c) {
484 return disorder_simple(c, 0, "disable", (char *)0);
485}
486
c4e2cfdd
RK
487/** @brief Scratch the currently playing track
488 * @param id Playing track ID or NULL (UTF-8)
489 * @param c Client
490 * @return 0 on success, non-0 on error
491 */
460b9539 492int disorder_scratch(disorder_client *c, const char *id) {
493 return disorder_simple(c, 0, "scratch", id, (char *)0);
494}
495
c4e2cfdd
RK
496/** @brief Shut down the server
497 * @param c Client
498 * @return 0 on success, non-0 on error
499 */
460b9539 500int disorder_shutdown(disorder_client *c) {
501 return disorder_simple(c, 0, "shutdown", (char *)0);
502}
503
c4e2cfdd
RK
504/** @brief Make the server re-read its configuration
505 * @param c Client
506 * @return 0 on success, non-0 on error
507 */
460b9539 508int disorder_reconfigure(disorder_client *c) {
509 return disorder_simple(c, 0, "reconfigure", (char *)0);
510}
511
c4e2cfdd
RK
512/** @brief Rescan tracks
513 * @param c Client
514 * @return 0 on success, non-0 on error
515 */
460b9539 516int disorder_rescan(disorder_client *c) {
517 return disorder_simple(c, 0, "rescan", (char *)0);
518}
519
c4e2cfdd
RK
520/** @brief Get server version number
521 * @param c Client
522 * @param rp Where to store version string (UTF-8)
523 * @return 0 on success, non-0 on error
524 */
460b9539 525int disorder_version(disorder_client *c, char **rp) {
7b32e917 526 return dequote(disorder_simple(c, rp, "version", (char *)0), rp);
460b9539 527}
528
529static void client_error(const char *msg,
530 void attribute((unused)) *u) {
531 error(0, "error parsing reply: %s", msg);
532}
533
c4e2cfdd
RK
534/** @brief Get currently playing track
535 * @param c Client
536 * @param qp Where to store track information
537 * @return 0 on success, non-0 on error
538 *
539 * @p qp gets NULL if no track is playing.
540 */
460b9539 541int disorder_playing(disorder_client *c, struct queue_entry **qp) {
542 char *r;
543 struct queue_entry *q;
2bead829 544 int rc;
460b9539 545
2bead829 546 if((rc = disorder_simple(c, &r, "playing", (char *)0)))
547 return rc;
460b9539 548 if(r) {
549 q = xmalloc(sizeof *q);
550 if(queue_unmarshall(q, r, client_error, 0))
551 return -1;
552 *qp = q;
553 } else
554 *qp = 0;
555 return 0;
556}
557
0590cedc 558/** @brief Fetch the queue, recent list, etc */
460b9539 559static int disorder_somequeue(disorder_client *c,
560 const char *cmd, struct queue_entry **qp) {
561 struct queue_entry *qh, **qt = &qh, *q;
562 char *l;
2bead829 563 int rc;
460b9539 564
2bead829 565 if((rc = disorder_simple(c, 0, cmd, (char *)0)))
566 return rc;
460b9539 567 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
568 if(!strcmp(l, ".")) {
569 *qt = 0;
570 *qp = qh;
571 return 0;
572 }
573 q = xmalloc(sizeof *q);
574 if(!queue_unmarshall(q, l, client_error, 0)) {
575 *qt = q;
576 qt = &q->next;
577 }
578 }
579 if(ferror(c->fpin))
580 error(errno, "error reading %s", c->ident);
581 else
582 error(0, "error reading %s: unexpected EOF", c->ident);
583 return -1;
584}
585
c4e2cfdd
RK
586/** @brief Get recently played tracks
587 * @param c Client
588 * @param qp Where to store track information
589 * @return 0 on success, non-0 on error
590 *
591 * The last entry in the list is the most recently played track.
592 */
460b9539 593int disorder_recent(disorder_client *c, struct queue_entry **qp) {
594 return disorder_somequeue(c, "recent", qp);
595}
596
c4e2cfdd
RK
597/** @brief Get queue
598 * @param c Client
599 * @param qp Where to store track information
600 * @return 0 on success, non-0 on error
601 *
602 * The first entry in the list will be played next.
603 */
460b9539 604int disorder_queue(disorder_client *c, struct queue_entry **qp) {
605 return disorder_somequeue(c, "queue", qp);
606}
607
c4e2cfdd
RK
608/** @brief Read a dot-stuffed list
609 * @param c Client
610 * @param vecp Where to store list (UTF-8)
611 * @param nvecp Where to store number of items, or NULL
612 * @return 0 on success, non-0 on error
613 *
614 * The list will have a final NULL not counted in @p nvecp.
615 */
460b9539 616static int readlist(disorder_client *c, char ***vecp, int *nvecp) {
617 char *l;
618 struct vector v;
619
620 vector_init(&v);
621 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
622 if(!strcmp(l, ".")) {
623 vector_terminate(&v);
624 if(nvecp)
625 *nvecp = v.nvec;
626 *vecp = v.vec;
627 return 0;
628 }
629 vector_append(&v, l + (*l == '.'));
630 }
631 if(ferror(c->fpin))
632 error(errno, "error reading %s", c->ident);
633 else
634 error(0, "error reading %s: unexpected EOF", c->ident);
635 return -1;
636}
637
c4e2cfdd
RK
638/** @brief Issue a comamnd and get a list response
639 * @param c Client
640 * @param vecp Where to store list (UTF-8)
641 * @param nvecp Where to store number of items, or NULL
642 * @param cmd Command
643 * @return 0 on success, non-0 on error
644 *
645 * The remaining arguments are command arguments, terminated by (char
646 * *)0. They should be in UTF-8.
647 *
648 * 5xx responses count as errors.
649 */
460b9539 650static int disorder_simple_list(disorder_client *c,
651 char ***vecp, int *nvecp,
652 const char *cmd, ...) {
653 va_list ap;
654 int ret;
655
656 va_start(ap, cmd);
657 ret = disorder_simple_v(c, 0, cmd, ap);
658 va_end(ap);
659 if(ret) return ret;
660 return readlist(c, vecp, nvecp);
661}
662
c4e2cfdd
RK
663/** @brief List directories below @p dir
664 * @param c Client
0227f67d 665 * @param dir Directory to list, or NULL for root (UTF-8)
c4e2cfdd
RK
666 * @param re Regexp that results must match, or NULL (UTF-8)
667 * @param vecp Where to store list (UTF-8)
668 * @param nvecp Where to store number of items, or NULL
669 * @return 0 on success, non-0 on error
670 */
460b9539 671int disorder_directories(disorder_client *c, const char *dir, const char *re,
672 char ***vecp, int *nvecp) {
673 return disorder_simple_list(c, vecp, nvecp, "dirs", dir, re, (char *)0);
674}
675
c4e2cfdd
RK
676/** @brief List files below @p dir
677 * @param c Client
0227f67d 678 * @param dir Directory to list, or NULL for root (UTF-8)
c4e2cfdd
RK
679 * @param re Regexp that results must match, or NULL (UTF-8)
680 * @param vecp Where to store list (UTF-8)
681 * @param nvecp Where to store number of items, or NULL
682 * @return 0 on success, non-0 on error
683 */
460b9539 684int disorder_files(disorder_client *c, const char *dir, const char *re,
685 char ***vecp, int *nvecp) {
686 return disorder_simple_list(c, vecp, nvecp, "files", dir, re, (char *)0);
687}
688
c4e2cfdd
RK
689/** @brief List files and directories below @p dir
690 * @param c Client
0227f67d 691 * @param dir Directory to list, or NULL for root (UTF-8)
c4e2cfdd
RK
692 * @param re Regexp that results must match, or NULL (UTF-8)
693 * @param vecp Where to store list (UTF-8)
694 * @param nvecp Where to store number of items, or NULL
695 * @return 0 on success, non-0 on error
696 */
460b9539 697int disorder_allfiles(disorder_client *c, const char *dir, const char *re,
698 char ***vecp, int *nvecp) {
699 return disorder_simple_list(c, vecp, nvecp, "allfiles", dir, re, (char *)0);
700}
701
c4e2cfdd
RK
702/** @brief Return the user we logged in with
703 * @param c Client
704 * @return User name (owned by @p c, don't modify)
705 */
460b9539 706char *disorder_user(disorder_client *c) {
707 return c->user;
708}
709
c4e2cfdd
RK
710/** @brief Set a track preference
711 * @param c Client
712 * @param track Track name (UTF-8)
713 * @param key Preference name (UTF-8)
714 * @param value Preference value (UTF-8)
715 * @return 0 on success, non-0 on error
716 */
460b9539 717int disorder_set(disorder_client *c, const char *track,
718 const char *key, const char *value) {
719 return disorder_simple(c, 0, "set", track, key, value, (char *)0);
720}
721
c4e2cfdd
RK
722/** @brief Unset a track preference
723 * @param c Client
724 * @param track Track name (UTF-8)
725 * @param key Preference name (UTF-8)
726 * @return 0 on success, non-0 on error
727 */
460b9539 728int disorder_unset(disorder_client *c, const char *track,
729 const char *key) {
730 return disorder_simple(c, 0, "unset", track, key, (char *)0);
731}
732
c4e2cfdd
RK
733/** @brief Get a track preference
734 * @param c Client
735 * @param track Track name (UTF-8)
736 * @param key Preference name (UTF-8)
737 * @param valuep Where to store preference value (UTF-8)
738 * @return 0 on success, non-0 on error
739 */
460b9539 740int disorder_get(disorder_client *c,
741 const char *track, const char *key, char **valuep) {
7b32e917
RK
742 return dequote(disorder_simple(c, valuep, "get", track, key, (char *)0),
743 valuep);
460b9539 744}
745
746static void pref_error_handler(const char *msg,
747 void attribute((unused)) *u) {
748 error(0, "error handling 'prefs' reply: %s", msg);
749}
750
158d0961 751/** @brief Get all preferences for a trcak
c4e2cfdd
RK
752 * @param c Client
753 * @param track Track name
754 * @param kp Where to store linked list of preferences
755 * @return 0 on success, non-0 on error
756 */
460b9539 757int disorder_prefs(disorder_client *c, const char *track, struct kvp **kp) {
758 char **vec, **pvec;
2bead829 759 int nvec, npvec, n, rc;
460b9539 760 struct kvp *k;
761
2bead829 762 if((rc = disorder_simple_list(c, &vec, &nvec, "prefs", track, (char *)0)))
763 return rc;
460b9539 764 for(n = 0; n < nvec; ++n) {
765 if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pref_error_handler, 0)))
766 return -1;
767 if(npvec != 2) {
768 pref_error_handler("malformed response", 0);
769 return -1;
770 }
771 *kp = k = xmalloc(sizeof *k);
772 k->name = pvec[0];
773 k->value = pvec[1];
774 kp = &k->next;
775 }
776 *kp = 0;
777 return 0;
778}
779
c4e2cfdd
RK
780/** @brief Parse a boolean response
781 * @param cmd Command for use in error messsage
782 * @param value Result from server
783 * @param flagp Where to store result
784 * @return 0 on success, non-0 on error
785 */
460b9539 786static int boolean(const char *cmd, const char *value,
787 int *flagp) {
788 if(!strcmp(value, "yes")) *flagp = 1;
789 else if(!strcmp(value, "no")) *flagp = 0;
790 else {
791 error(0, "malformed response to '%s'", cmd);
792 return -1;
793 }
794 return 0;
795}
796
c4e2cfdd
RK
797/** @brief Test whether a track exists
798 * @param c Client
799 * @param track Track name (UTF-8)
800 * @param existsp Where to store result (non-0 iff does exist)
801 * @return 0 on success, non-0 on error
802 */
460b9539 803int disorder_exists(disorder_client *c, const char *track, int *existsp) {
804 char *v;
2bead829 805 int rc;
460b9539 806
2bead829 807 if((rc = disorder_simple(c, &v, "exists", track, (char *)0)))
808 return rc;
460b9539 809 return boolean("exists", v, existsp);
810}
811
c4e2cfdd
RK
812/** @brief Test whether playing is enabled
813 * @param c Client
814 * @param enabledp Where to store result (non-0 iff enabled)
815 * @return 0 on success, non-0 on error
816 */
460b9539 817int disorder_enabled(disorder_client *c, int *enabledp) {
818 char *v;
2bead829 819 int rc;
460b9539 820
2bead829 821 if((rc = disorder_simple(c, &v, "enabled", (char *)0)))
822 return rc;
460b9539 823 return boolean("enabled", v, enabledp);
824}
825
c4e2cfdd
RK
826/** @brief Get the length of a track
827 * @param c Client
828 * @param track Track name (UTF-8)
829 * @param valuep Where to store length in seconds
830 * @return 0 on success, non-0 on error
831 *
832 * If the length is unknown 0 is returned.
833 */
460b9539 834int disorder_length(disorder_client *c, const char *track,
835 long *valuep) {
836 char *value;
2bead829 837 int rc;
460b9539 838
2bead829 839 if((rc = disorder_simple(c, &value, "length", track, (char *)0)))
840 return rc;
460b9539 841 *valuep = atol(value);
842 return 0;
843}
844
c4e2cfdd
RK
845/** @brief Search for tracks
846 * @param c Client
847 * @param terms Search terms (UTF-8)
848 * @param vecp Where to store list (UTF-8)
849 * @param nvecp Where to store number of items, or NULL
850 * @return 0 on success, non-0 on error
851 */
460b9539 852int disorder_search(disorder_client *c, const char *terms,
853 char ***vecp, int *nvecp) {
854 return disorder_simple_list(c, vecp, nvecp, "search", terms, (char *)0);
855}
856
c4e2cfdd
RK
857/** @brief Enable random play
858 * @param c Client
859 * @return 0 on success, non-0 on error
860 */
460b9539 861int disorder_random_enable(disorder_client *c) {
862 return disorder_simple(c, 0, "random-enable", (char *)0);
863}
864
c4e2cfdd
RK
865/** @brief Disable random play
866 * @param c Client
867 * @return 0 on success, non-0 on error
868 */
460b9539 869int disorder_random_disable(disorder_client *c) {
870 return disorder_simple(c, 0, "random-disable", (char *)0);
871}
872
c4e2cfdd
RK
873/** @brief Test whether random play is enabled
874 * @param c Client
158d0961 875 * @param enabledp Where to store result (non-0 iff enabled)
c4e2cfdd
RK
876 * @return 0 on success, non-0 on error
877 */
460b9539 878int disorder_random_enabled(disorder_client *c, int *enabledp) {
879 char *v;
2bead829 880 int rc;
460b9539 881
2bead829 882 if((rc = disorder_simple(c, &v, "random-enabled", (char *)0)))
883 return rc;
460b9539 884 return boolean("random-enabled", v, enabledp);
885}
886
c4e2cfdd
RK
887/** @brief Get server stats
888 * @param c Client
889 * @param vecp Where to store list (UTF-8)
890 * @param nvecp Where to store number of items, or NULL
891 * @return 0 on success, non-0 on error
892 */
460b9539 893int disorder_stats(disorder_client *c,
894 char ***vecp, int *nvecp) {
895 return disorder_simple_list(c, vecp, nvecp, "stats", (char *)0);
896}
897
c4e2cfdd
RK
898/** @brief Set volume
899 * @param c Client
900 * @param left New left channel value
901 * @param right New right channel value
902 * @return 0 on success, non-0 on error
903 */
460b9539 904int disorder_set_volume(disorder_client *c, int left, int right) {
905 char *ls, *rs;
906
907 if(byte_asprintf(&ls, "%d", left) < 0
908 || byte_asprintf(&rs, "%d", right) < 0)
909 return -1;
2bead829 910 return disorder_simple(c, 0, "volume", ls, rs, (char *)0);
460b9539 911}
912
c4e2cfdd
RK
913/** @brief Get volume
914 * @param c Client
915 * @param left Where to store left channel value
916 * @param right Where to store right channel value
917 * @return 0 on success, non-0 on error
918 */
460b9539 919int disorder_get_volume(disorder_client *c, int *left, int *right) {
920 char *r;
2bead829 921 int rc;
460b9539 922
2bead829 923 if((rc = disorder_simple(c, &r, "volume", (char *)0)))
924 return rc;
460b9539 925 if(sscanf(r, "%d %d", left, right) != 2) {
926 error(0, "error parsing response to 'volume': '%s'", r);
927 return -1;
928 }
929 return 0;
930}
931
c4e2cfdd
RK
932/** @brief Log to a sink
933 * @param c Client
934 * @param s Sink to write log lines to
935 * @return 0 on success, non-0 on error
936 */
460b9539 937int disorder_log(disorder_client *c, struct sink *s) {
938 char *l;
2bead829 939 int rc;
460b9539 940
2bead829 941 if((rc = disorder_simple(c, 0, "log", (char *)0)))
942 return rc;
460b9539 943 while(inputline(c->ident, c->fpin, &l, '\n') >= 0 && strcmp(l, "."))
944 if(sink_printf(s, "%s\n", l) < 0) return -1;
945 if(ferror(c->fpin) || feof(c->fpin)) return -1;
946 return 0;
947}
948
c4e2cfdd
RK
949/** @brief Look up a track name part
950 * @param c Client
951 * @param partp Where to store result (UTF-8)
952 * @param track Track name (UTF-8)
953 * @param context Context (usually "sort" or "display") (UTF-8)
954 * @param part Track part (UTF-8)
955 * @return 0 on success, non-0 on error
956 */
460b9539 957int disorder_part(disorder_client *c, char **partp,
958 const char *track, const char *context, const char *part) {
7b32e917
RK
959 return dequote(disorder_simple(c, partp, "part",
960 track, context, part, (char *)0), partp);
460b9539 961}
962
c4e2cfdd
RK
963/** @brief Resolve aliases
964 * @param c Client
158d0961 965 * @param trackp Where to store canonical name (UTF-8)
c4e2cfdd
RK
966 * @param track Track name (UTF-8)
967 * @return 0 on success, non-0 on error
968 */
460b9539 969int disorder_resolve(disorder_client *c, char **trackp, const char *track) {
7b32e917
RK
970 return dequote(disorder_simple(c, trackp, "resolve", track, (char *)0),
971 trackp);
460b9539 972}
973
c4e2cfdd
RK
974/** @brief Pause the current track
975 * @param c Client
976 * @return 0 on success, non-0 on error
977 */
460b9539 978int disorder_pause(disorder_client *c) {
979 return disorder_simple(c, 0, "pause", (char *)0);
980}
981
c4e2cfdd
RK
982/** @brief Resume the current track
983 * @param c Client
984 * @return 0 on success, non-0 on error
985 */
460b9539 986int disorder_resume(disorder_client *c) {
987 return disorder_simple(c, 0, "resume", (char *)0);
988}
989
c4e2cfdd
RK
990/** @brief List all known tags
991 * @param c Client
992 * @param vecp Where to store list (UTF-8)
993 * @param nvecp Where to store number of items, or NULL
994 * @return 0 on success, non-0 on error
995 */
460b9539 996int disorder_tags(disorder_client *c,
997 char ***vecp, int *nvecp) {
998 return disorder_simple_list(c, vecp, nvecp, "tags", (char *)0);
999}
1000
c4e2cfdd
RK
1001/** @brief List all known users
1002 * @param c Client
1003 * @param vecp Where to store list (UTF-8)
1004 * @param nvecp Where to store number of items, or NULL
1005 * @return 0 on success, non-0 on error
1006 */
c3be4f19
RK
1007int disorder_users(disorder_client *c,
1008 char ***vecp, int *nvecp) {
1009 return disorder_simple_list(c, vecp, nvecp, "users", (char *)0);
1010}
1011
c4e2cfdd 1012/** @brief Get recently added tracks
2a10b70b 1013 * @param c Client
c4e2cfdd 1014 * @param vecp Where to store pointer to list (UTF-8)
2a10b70b
RK
1015 * @param nvecp Where to store count
1016 * @param max Maximum tracks to fetch, or 0 for all available
1017 * @return 0 on success, non-0 on error
1018 */
1019int disorder_new_tracks(disorder_client *c,
1020 char ***vecp, int *nvecp,
1021 int max) {
1022 char limit[32];
1023
1024 sprintf(limit, "%d", max);
1025 return disorder_simple_list(c, vecp, nvecp, "new", limit, (char *)0);
1026}
1027
c4e2cfdd
RK
1028/** @brief Set a global preference
1029 * @param c Client
1030 * @param key Preference name (UTF-8)
1031 * @param value Preference value (UTF-8)
1032 * @return 0 on success, non-0 on error
1033 */
460b9539 1034int disorder_set_global(disorder_client *c,
1035 const char *key, const char *value) {
1036 return disorder_simple(c, 0, "set-global", key, value, (char *)0);
1037}
1038
c4e2cfdd
RK
1039/** @brief Unset a global preference
1040 * @param c Client
1041 * @param key Preference name (UTF-8)
1042 * @return 0 on success, non-0 on error
1043 */
460b9539 1044int disorder_unset_global(disorder_client *c, const char *key) {
1045 return disorder_simple(c, 0, "unset-global", key, (char *)0);
1046}
1047
c4e2cfdd
RK
1048/** @brief Get a global preference
1049 * @param c Client
1050 * @param key Preference name (UTF-8)
1051 * @param valuep Where to store preference value (UTF-8)
1052 * @return 0 on success, non-0 on error
1053 */
460b9539 1054int disorder_get_global(disorder_client *c, const char *key, char **valuep) {
7b32e917
RK
1055 return dequote(disorder_simple(c, valuep, "get-global", key, (char *)0),
1056 valuep);
460b9539 1057}
1058
c4e2cfdd
RK
1059/** @brief Get server's RTP address information
1060 * @param c Client
1061 * @param addressp Where to store address (UTF-8)
1062 * @param portp Where to store port (UTF-8)
1063 * @return 0 on success, non-0 on error
1064 */
ca831831
RK
1065int disorder_rtp_address(disorder_client *c, char **addressp, char **portp) {
1066 char *r;
1067 int rc, n;
1068 char **vec;
1069
1070 if((rc = disorder_simple(c, &r, "rtp-address", (char *)0)))
1071 return rc;
1072 vec = split(r, &n, SPLIT_QUOTES, 0, 0);
1073 if(n != 2) {
1074 error(0, "malformed rtp-address reply");
1075 return -1;
1076 }
1077 *addressp = vec[0];
1078 *portp = vec[1];
1079 return 0;
1080}
1081
c4e2cfdd
RK
1082/** @brief Create a user
1083 * @param c Client
1084 * @param user Username
1085 * @param password Password
1086 * @param rights Initial rights or NULL to use default
1087 * @return 0 on success, non-0 on error
1088 */
f0feb22e 1089int disorder_adduser(disorder_client *c,
0f55e905
RK
1090 const char *user, const char *password,
1091 const char *rights) {
1092 return disorder_simple(c, 0, "adduser", user, password, rights, (char *)0);
f0feb22e
RK
1093}
1094
c4e2cfdd
RK
1095/** @brief Delete a user
1096 * @param c Client
1097 * @param user Username
1098 * @return 0 on success, non-0 on error
1099 */
f0feb22e
RK
1100int disorder_deluser(disorder_client *c, const char *user) {
1101 return disorder_simple(c, 0, "deluser", user, (char *)0);
1102}
1103
c4e2cfdd
RK
1104/** @brief Get user information
1105 * @param c Client
1106 * @param user Username
1107 * @param key Property name (UTF-8)
1108 * @param valuep Where to store value (UTF-8)
1109 * @return 0 on success, non-0 on error
1110 */
a55c70c7
RK
1111int disorder_userinfo(disorder_client *c, const char *user, const char *key,
1112 char **valuep) {
7b32e917
RK
1113 return dequote(disorder_simple(c, valuep, "userinfo", user, key, (char *)0),
1114 valuep);
5df73aeb
RK
1115}
1116
c4e2cfdd
RK
1117/** @brief Set user information
1118 * @param c Client
1119 * @param user Username
1120 * @param key Property name (UTF-8)
1121 * @param value New property value (UTF-8)
1122 * @return 0 on success, non-0 on error
1123 */
5df73aeb
RK
1124int disorder_edituser(disorder_client *c, const char *user,
1125 const char *key, const char *value) {
1126 return disorder_simple(c, 0, "edituser", user, key, value, (char *)0);
1127}
1128
c4e2cfdd
RK
1129/** @brief Register a user
1130 * @param c Client
1131 * @param user Username
1132 * @param password Password
1133 * @param email Email address (UTF-8)
c4e2cfdd
RK
1134 * @param confirmp Where to store confirmation string
1135 * @return 0 on success, non-0 on error
1136 */
ba39faf6
RK
1137int disorder_register(disorder_client *c, const char *user,
1138 const char *password, const char *email,
1139 char **confirmp) {
1140 return dequote(disorder_simple(c, confirmp, "register",
1141 user, password, email, (char *)0),
1142 confirmp);
1143}
1144
c4e2cfdd
RK
1145/** @brief Confirm a user
1146 * @param c Client
1147 * @param confirm Confirmation string
1148 * @return 0 on success, non-0 on error
1149 */
ba39faf6 1150int disorder_confirm(disorder_client *c, const char *confirm) {
30365519 1151 char *u;
1152 int rc;
1153
1154 if(!(rc = dequote(disorder_simple(c, &u, "confirm", confirm, (char *)0),
1155 &u)))
1156 c->user = u;
1157 return rc;
ba39faf6
RK
1158}
1159
c4e2cfdd
RK
1160/** @brief Make a cookie for this login
1161 * @param c Client
1162 * @param cookiep Where to store cookie string
1163 * @return 0 on success, non-0 on error
1164 */
07969f9b
RK
1165int disorder_make_cookie(disorder_client *c, char **cookiep) {
1166 return dequote(disorder_simple(c, cookiep, "make-cookie", (char *)0),
1167 cookiep);
1168}
1169
b42ffad6 1170/** @brief Revoke the cookie used by this session
1171 * @param c Client
1172 * @return 0 on success, non-0 on error
1173 */
1174int disorder_revoke(disorder_client *c) {
1175 return disorder_simple(c, 0, "revoke", (char *)0);
1176}
1177
6207d2f3 1178/** @brief Request a password reminder email
1179 * @param c Client
1180 * @param user Username
1181 * @return 0 on success, non-0 on error
1182 */
1183int disorder_reminder(disorder_client *c, const char *user) {
1184 return disorder_simple(c, 0, "reminder", user, (char *)0);
1185}
1186
460b9539 1187/*
1188Local Variables:
1189c-basic-offset:2
1190comment-column:40
1191End:
1192*/