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