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