chiark / gitweb /
A bit more doxygenization.
[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 /** @brief Client handle contents */
63 struct disorder_client {
64   /** @brief Stream to read from */
65   FILE *fpin;
66   /** @brief Stream to write to */
67   FILE *fpout;
68   /** @brief Peer description */
69   char *ident;
70   /** @brief Username */
71   char *user;
72   /** @brief Report errors to @c stderr */
73   int verbose;
74   /** @brief Last error string */
75   char *last;
76 };
77
78 /** @brief Create a new client
79  * @param verbose If nonzero, write extra junk to stderr
80  * @return Pointer to new client
81  *
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.
85  */
86 disorder_client *disorder_new(int verbose) {
87   disorder_client *c = xmalloc(sizeof (struct disorder_client));
88
89   c->verbose = verbose;
90   return c;
91 }
92
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  */
98 static 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'
109      && r[3] == ' ') {
110     c->last = r + 4;
111     return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0';
112   } else {
113     error(0, "invalid reply format from %s", c->ident);
114     return -1;
115   }
116 }
117
118 /** @brief Return last response string
119  * @param c Client
120  * @return Last response string (UTF-8, English) or NULL
121  */
122 const char *disorder_last(disorder_client *c) {
123   return c->last;
124 }
125
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.
137  */
138 static 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));
151     return rc;
152   }
153 }
154
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  */
170 static 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
176   if(!c->fpout) {
177     error(0, "not connected to server");
178     return -1;
179   }
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
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().
214  */
215 static 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
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  */
235 static int dequote(int rc, char **rp) {
236   char **rr;
237
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);
244   }
245   return rc;
246 }
247
248 /** @brief Generic connection routine
249  * @param c Client
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
253  * @return 0 on success, non-0 on error
254  *
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.
258  */
259 static int disorder_connect_generic(disorder_client *c,
260                                     const char *username,
261                                     const char *password,
262                                     const char *cookie) {
263   int fd = -1, fd2 = -1, nrvec, rc;
264   unsigned char *nonce;
265   size_t nl;
266   const char *res;
267   char *r, **rvec;
268   const char *protocol, *algorithm, *challenge;
269   struct sockaddr *sa;
270   socklen_t salen;
271
272   if((salen = find_server(&sa, &c->ident)) == (socklen_t)-1)
273     return -1;
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   }
279   if(connect(fd, sa, salen) < 0) {
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;
297   if((rc = disorder_simple(c, &r, 0, (const char *)0)))
298     goto error_rc;
299   if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
300     goto error;
301   if(nrvec != 3) {
302     error(0, "cannot parse server greeting %s", r);
303     goto error;
304   }
305   protocol = *rvec++;
306   if(strcmp(protocol, "2")) {
307     error(0, "unknown protocol version: %s", protocol);
308     goto error;
309   }
310   algorithm = *rvec++;
311   challenge = *rvec++;
312   if(!(nonce = unhex(challenge, &nl)))
313     goto error;
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");
320       goto error;
321     }
322   }
323   if(!(res = authhash(nonce, nl, password, algorithm))) goto error;
324   if((rc = disorder_simple(c, 0, "user", username, res, (char *)0)))
325     goto error_rc;
326   c->user = xstrdup(username);
327   return 0;
328 error:
329   rc = -1;
330 error_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   }
339   if(fd2 != -1) close(fd2);
340   if(fd != -1) close(fd);
341   return rc;
342 }
343
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  */
350 int 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
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  */
367 int 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  */
403 int disorder_connect_cookie(disorder_client *c,
404                             const char *cookie) {
405   return disorder_connect_generic(c,
406                                   "guest",
407                                   "",
408                                   cookie);
409 }
410
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  */
418 int 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   }
435   c->ident = 0;
436   c->user = 0;
437   return 0;
438 }
439
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  */
445 int disorder_play(disorder_client *c, const char *track) {
446   return disorder_simple(c, 0, "play", track, (char *)0);
447 }
448
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  */
454 int disorder_remove(disorder_client *c, const char *track) {
455   return disorder_simple(c, 0, "remove", track, (char *)0);
456 }
457
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  */
464 int 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
471 /** @brief Enable play
472  * @param c Client
473  * @return 0 on success, non-0 on error
474  */
475 int disorder_enable(disorder_client *c) {
476   return disorder_simple(c, 0, "enable", (char *)0);
477 }
478
479 /** @brief Disable play
480  * @param c Client
481  * @return 0 on success, non-0 on error
482  */
483 int disorder_disable(disorder_client *c) {
484   return disorder_simple(c, 0, "disable", (char *)0);
485 }
486
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  */
492 int disorder_scratch(disorder_client *c, const char *id) {
493   return disorder_simple(c, 0, "scratch", id, (char *)0);
494 }
495
496 /** @brief Shut down the server
497  * @param c Client
498  * @return 0 on success, non-0 on error
499  */
500 int disorder_shutdown(disorder_client *c) {
501   return disorder_simple(c, 0, "shutdown", (char *)0);
502 }
503
504 /** @brief Make the server re-read its configuration
505  * @param c Client
506  * @return 0 on success, non-0 on error
507  */
508 int disorder_reconfigure(disorder_client *c) {
509   return disorder_simple(c, 0, "reconfigure", (char *)0);
510 }
511
512 /** @brief Rescan tracks
513  * @param c Client
514  * @return 0 on success, non-0 on error
515  */
516 int disorder_rescan(disorder_client *c) {
517   return disorder_simple(c, 0, "rescan", (char *)0);
518 }
519
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  */
525 int disorder_version(disorder_client *c, char **rp) {
526   return dequote(disorder_simple(c, rp, "version", (char *)0), rp);
527 }
528
529 static void client_error(const char *msg,
530                          void attribute((unused)) *u) {
531   error(0, "error parsing reply: %s", msg);
532 }
533
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  */
541 int disorder_playing(disorder_client *c, struct queue_entry **qp) {
542   char *r;
543   struct queue_entry *q;
544   int rc;
545
546   if((rc = disorder_simple(c, &r, "playing", (char *)0)))
547     return rc;
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
558 /** @brief Fetch the queue, recent list, etc */
559 static 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;
563   int rc;
564
565   if((rc = disorder_simple(c, 0, cmd, (char *)0)))
566     return rc;
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
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  */
593 int disorder_recent(disorder_client *c, struct queue_entry **qp) {
594   return disorder_somequeue(c, "recent", qp);
595 }
596
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  */
604 int disorder_queue(disorder_client *c, struct queue_entry **qp) {
605   return disorder_somequeue(c, "queue", qp);
606 }
607
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  */
616 static 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
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  */
650 static 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
663 /** @brief List directories 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_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
676 /** @brief List files 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_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
689 /** @brief List files and directories below @p dir
690  * @param c Client
691  * @param dir Directory to list, or NULL for root (UTF-8)
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  */
697 int 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
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  */
706 char *disorder_user(disorder_client *c) {
707   return c->user;
708 }
709
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  */
717 int 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
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  */
728 int 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
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  */
740 int disorder_get(disorder_client *c,
741                  const char *track, const char *key, char **valuep) {
742   return dequote(disorder_simple(c, valuep, "get", track, key, (char *)0),
743                  valuep);
744 }
745
746 static void pref_error_handler(const char *msg,
747                                void attribute((unused)) *u) {
748   error(0, "error handling 'prefs' reply: %s", msg);
749 }
750
751 /** @brief Get all preferences for a trcak
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  */
757 int disorder_prefs(disorder_client *c, const char *track, struct kvp **kp) {
758   char **vec, **pvec;
759   int nvec, npvec, n, rc;
760   struct kvp *k;
761
762   if((rc = disorder_simple_list(c, &vec, &nvec, "prefs", track, (char *)0)))
763     return rc;
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
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  */
786 static 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
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  */
803 int disorder_exists(disorder_client *c, const char *track, int *existsp) {
804   char *v;
805   int rc;
806
807   if((rc = disorder_simple(c, &v, "exists", track, (char *)0)))
808     return rc;
809   return boolean("exists", v, existsp);
810 }
811
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  */
817 int disorder_enabled(disorder_client *c, int *enabledp) {
818   char *v;
819   int rc;
820
821   if((rc = disorder_simple(c, &v, "enabled", (char *)0)))
822     return rc;
823   return boolean("enabled", v, enabledp);
824 }
825
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  */
834 int disorder_length(disorder_client *c, const char *track,
835                     long *valuep) {
836   char *value;
837   int rc;
838
839   if((rc = disorder_simple(c, &value, "length", track, (char *)0)))
840     return rc;
841   *valuep = atol(value);
842   return 0;
843 }
844
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  */
852 int 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
857 /** @brief Enable random play
858  * @param c Client
859  * @return 0 on success, non-0 on error
860  */
861 int disorder_random_enable(disorder_client *c) {
862   return disorder_simple(c, 0, "random-enable", (char *)0);
863 }
864
865 /** @brief Disable random play
866  * @param c Client
867  * @return 0 on success, non-0 on error
868  */
869 int disorder_random_disable(disorder_client *c) {
870   return disorder_simple(c, 0, "random-disable", (char *)0);
871 }
872
873 /** @brief Test whether random play is enabled
874  * @param c Client
875  * @param enabledp Where to store result (non-0 iff enabled)
876  * @return 0 on success, non-0 on error
877  */
878 int disorder_random_enabled(disorder_client *c, int *enabledp) {
879   char *v;
880   int rc;
881
882   if((rc = disorder_simple(c, &v, "random-enabled", (char *)0)))
883     return rc;
884   return boolean("random-enabled", v, enabledp);
885 }
886
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  */
893 int disorder_stats(disorder_client *c,
894                    char ***vecp, int *nvecp) {
895   return disorder_simple_list(c, vecp, nvecp, "stats", (char *)0);
896 }
897
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  */
904 int 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;
910   return disorder_simple(c, 0, "volume", ls, rs, (char *)0);
911 }
912
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  */
919 int disorder_get_volume(disorder_client *c, int *left, int *right) {
920   char *r;
921   int rc;
922
923   if((rc = disorder_simple(c, &r, "volume", (char *)0)))
924     return rc;
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
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  */
937 int disorder_log(disorder_client *c, struct sink *s) {
938   char *l;
939   int rc;
940     
941   if((rc = disorder_simple(c, 0, "log", (char *)0)))
942     return rc;
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
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  */
957 int disorder_part(disorder_client *c, char **partp,
958                   const char *track, const char *context, const char *part) {
959   return dequote(disorder_simple(c, partp, "part",
960                                  track, context, part, (char *)0), partp);
961 }
962
963 /** @brief Resolve aliases
964  * @param c Client
965  * @param trackp Where to store canonical name (UTF-8)
966  * @param track Track name (UTF-8)
967  * @return 0 on success, non-0 on error
968  */
969 int disorder_resolve(disorder_client *c, char **trackp, const char *track) {
970   return dequote(disorder_simple(c, trackp, "resolve", track, (char *)0),
971                  trackp);
972 }
973
974 /** @brief Pause the current track
975  * @param c Client
976  * @return 0 on success, non-0 on error
977  */
978 int disorder_pause(disorder_client *c) {
979   return disorder_simple(c, 0, "pause", (char *)0);
980 }
981
982 /** @brief Resume the current track
983  * @param c Client
984  * @return 0 on success, non-0 on error
985  */
986 int disorder_resume(disorder_client *c) {
987   return disorder_simple(c, 0, "resume", (char *)0);
988 }
989
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  */
996 int disorder_tags(disorder_client *c,
997                    char ***vecp, int *nvecp) {
998   return disorder_simple_list(c, vecp, nvecp, "tags", (char *)0);
999 }
1000
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  */
1007 int disorder_users(disorder_client *c,
1008                    char ***vecp, int *nvecp) {
1009   return disorder_simple_list(c, vecp, nvecp, "users", (char *)0);
1010 }
1011
1012 /** @brief Get recently added tracks
1013  * @param c Client
1014  * @param vecp Where to store pointer to list (UTF-8)
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  */
1019 int 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
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  */
1034 int 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
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  */
1044 int disorder_unset_global(disorder_client *c, const char *key) {
1045   return disorder_simple(c, 0, "unset-global", key, (char *)0);
1046 }
1047
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  */
1054 int disorder_get_global(disorder_client *c, const char *key, char **valuep) {
1055   return dequote(disorder_simple(c, valuep, "get-global", key, (char *)0),
1056                  valuep);
1057 }
1058
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  */
1065 int 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
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  */
1089 int disorder_adduser(disorder_client *c,
1090                      const char *user, const char *password,
1091                      const char *rights) {
1092   return disorder_simple(c, 0, "adduser", user, password, rights, (char *)0);
1093 }
1094
1095 /** @brief Delete a user
1096  * @param c Client
1097  * @param user Username
1098  * @return 0 on success, non-0 on error
1099  */
1100 int disorder_deluser(disorder_client *c, const char *user) {
1101   return disorder_simple(c, 0, "deluser", user, (char *)0);
1102 }
1103
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  */
1111 int disorder_userinfo(disorder_client *c, const char *user, const char *key,
1112                       char **valuep) {
1113   return dequote(disorder_simple(c, valuep, "userinfo", user, key, (char *)0),
1114                  valuep);
1115 }
1116
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  */
1124 int 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
1129 /** @brief Register a user
1130  * @param c Client
1131  * @param user Username
1132  * @param password Password
1133  * @param email Email address (UTF-8)
1134  * @param confirmp Where to store confirmation string
1135  * @return 0 on success, non-0 on error
1136  */
1137 int 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
1145 /** @brief Confirm a user
1146  * @param c Client
1147  * @param confirm Confirmation string
1148  * @return 0 on success, non-0 on error
1149  */
1150 int disorder_confirm(disorder_client *c, const char *confirm) {
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;
1158 }
1159
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  */
1165 int disorder_make_cookie(disorder_client *c, char **cookiep) {
1166   return dequote(disorder_simple(c, cookiep, "make-cookie", (char *)0),
1167                  cookiep);
1168 }
1169
1170 /** @brief Revoke the cookie used by this session
1171  * @param c Client
1172  * @return 0 on success, non-0 on error
1173  */
1174 int disorder_revoke(disorder_client *c) {
1175   return disorder_simple(c, 0, "revoke", (char *)0);
1176 }
1177
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  */
1183 int disorder_reminder(disorder_client *c, const char *user) {
1184   return disorder_simple(c, 0, "reminder", user, (char *)0);
1185 }
1186
1187 /*
1188 Local Variables:
1189 c-basic-offset:2
1190 comment-column:40
1191 End:
1192 */