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