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