chiark / gitweb /
more memory hygeine
[disorder] / lib / client.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004-2009 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 3 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,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU 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, see <http://www.gnu.org/licenses/>.
17  */
18 /** @file lib/client.c
19  * @brief Simple C client
20  *
21  * See @ref lib/eclient.c for an asynchronous-capable client
22  * implementation.
23  */
24
25 #include "common.h"
26
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
30 #include <sys/un.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <netdb.h>
34 #include <pcre.h>
35
36 #include "log.h"
37 #include "mem.h"
38 #include "queue.h"
39 #include "client.h"
40 #include "charset.h"
41 #include "hex.h"
42 #include "split.h"
43 #include "vector.h"
44 #include "inputline.h"
45 #include "kvp.h"
46 #include "syscalls.h"
47 #include "printf.h"
48 #include "sink.h"
49 #include "addr.h"
50 #include "authhash.h"
51 #include "client-common.h"
52 #include "rights.h"
53 #include "trackdb.h"
54 #include "kvp.h"
55
56 /** @brief Client handle contents */
57 struct disorder_client {
58   /** @brief Stream to read from */
59   FILE *fpin;
60   /** @brief Stream to write to */
61   FILE *fpout;
62   /** @brief Peer description */
63   char *ident;
64   /** @brief Username */
65   char *user;
66   /** @brief Report errors to @c stderr */
67   int verbose;
68   /** @brief Last error string */
69   const char *last;
70 };
71
72 /** @brief Create a new client
73  * @param verbose If nonzero, write extra junk to stderr
74  * @return Pointer to new client
75  *
76  * You must call disorder_connect(), disorder_connect_user() or
77  * disorder_connect_cookie() to connect it.  Use disorder_close() to
78  * dispose of the client when finished with it.
79  */
80 disorder_client *disorder_new(int verbose) {
81   disorder_client *c = xmalloc(sizeof (struct disorder_client));
82
83   c->verbose = verbose;
84   return c;
85 }
86
87 /** @brief Read a response line
88  * @param c Client
89  * @param rp Where to store response, or NULL (UTF-8)
90  * @return Response code 0-999 or -1 on error
91  */
92 static int response(disorder_client *c, char **rp) {
93   char *r;
94
95   if(inputline(c->ident, c->fpin, &r, '\n')) {
96     byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
97     return -1;
98   }
99   D(("response: %s", r));
100   if(rp)
101     *rp = r;
102   if(r[0] >= '0' && r[0] <= '9'
103      && r[1] >= '0' && r[1] <= '9'
104      && r[2] >= '0' && r[2] <= '9'
105      && r[3] == ' ') {
106     c->last = r + 4;
107     return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0';
108   } else {
109     c->last = "invalid reply format";
110     disorder_error(0, "invalid reply format from %s", c->ident);
111     return -1;
112   }
113 }
114
115 /** @brief Return last response string
116  * @param c Client
117  * @return Last response string (UTF-8, English) or NULL
118  */
119 const char *disorder_last(disorder_client *c) {
120   return c->last;
121 }
122
123 /** @brief Read and partially parse a response
124  * @param c Client
125  * @param rp Where to store response text (or NULL) (UTF-8)
126  * @return 0 on success, non-0 on error
127  *
128  * 5xx responses count as errors.
129  *
130  * @p rp will NOT be filled in for xx9 responses (where it is just
131  * commentary for a command where it would normally be meaningful).
132  *
133  * NB that the response will NOT be converted to the local encoding.
134  */
135 static int check_response(disorder_client *c, char **rp) {
136   int rc;
137   char *r;
138
139   if((rc = response(c, &r)) == -1)
140     return -1;
141   else if(rc / 100 == 2) {
142     if(rp)
143       *rp = (rc % 10 == 9) ? 0 : xstrdup(r + 4);
144     xfree(r);
145     return 0;
146   } else {
147     if(c->verbose)
148       disorder_error(0, "from %s: %s", c->ident, utf82mb(r));
149     xfree(r);
150     return rc;
151   }
152 }
153
154 /** @brief Issue a command and parse a simple response
155  * @param c Client
156  * @param rp Where to store result, or NULL
157  * @param cmd Command
158  * @param body Body or NULL
159  * @param nbody Length of body or -1
160  * @param ap Arguments (UTF-8), terminated by (char *)0
161  * @return 0 on success, non-0 on error
162  *
163  * 5xx responses count as errors.
164  *
165  * @p rp will NOT be filled in for xx9 responses (where it is just
166  * commentary for a command where it would normally be meaningful).
167  *
168  * NB that the response will NOT be converted to the local encoding
169  * nor will quotes be stripped.  See dequote().
170  *
171  * If @p body is not NULL then the body is sent immediately after the
172  * command.  @p nbody should be the number of lines or @c -1 to count
173  * them if @p body is NULL-terminated.
174  *
175  * Usually you would call this via one of the following interfaces:
176  * - disorder_simple()
177  * - disorder_simple_body()
178  * - disorder_simple_list()
179  */
180 static int disorder_simple_v(disorder_client *c,
181                              char **rp,
182                              const char *cmd,
183                              char **body, int nbody,
184                              va_list ap) {
185   const char *arg;
186   struct dynstr d;
187
188   if(!c->fpout) {
189     c->last = "not connected";
190     disorder_error(0, "not connected to server");
191     return -1;
192   }
193   if(cmd) {
194     dynstr_init(&d);
195     dynstr_append_string(&d, cmd);
196     while((arg = va_arg(ap, const char *))) {
197       dynstr_append(&d, ' ');
198       dynstr_append_string(&d, quoteutf8(arg));
199     }
200     dynstr_append(&d, '\n');
201     dynstr_terminate(&d);
202     D(("command: %s", d.vec));
203     if(fputs(d.vec, c->fpout) < 0)
204       goto write_error;
205     xfree(d.vec);
206     if(body) {
207       if(nbody < 0)
208         for(nbody = 0; body[nbody]; ++nbody)
209           ;
210       for(int n = 0; n < nbody; ++n) {
211         if(body[n][0] == '.')
212           if(fputc('.', c->fpout) < 0)
213             goto write_error;
214         if(fputs(body[n], c->fpout) < 0)
215           goto write_error;
216         if(fputc('\n', c->fpout) < 0)
217           goto write_error;
218       }
219       if(fputs(".\n", c->fpout) < 0)
220         goto write_error;
221     }
222     if(fflush(c->fpout))
223       goto write_error;
224   }
225   return check_response(c, rp);
226 write_error:
227   byte_xasprintf((char **)&c->last, "write error: %s", strerror(errno));
228   disorder_error(errno, "error writing to %s", c->ident);
229   return -1;
230 }
231
232 /** @brief Issue a command and parse a simple response
233  * @param c Client
234  * @param rp Where to store result, or NULL (UTF-8)
235  * @param cmd Command
236  * @return 0 on success, non-0 on error
237  *
238  * The remaining arguments are command arguments, terminated by (char
239  * *)0.  They should be in UTF-8.
240  *
241  * 5xx responses count as errors.
242  *
243  * @p rp will NOT be filled in for xx9 responses (where it is just
244  * commentary for a command where it would normally be meaningful).
245  *
246  * NB that the response will NOT be converted to the local encoding
247  * nor will quotes be stripped.  See dequote().
248  */
249 static int disorder_simple(disorder_client *c,
250                            char **rp,
251                            const char *cmd, ...) {
252   va_list ap;
253   int ret;
254
255   va_start(ap, cmd);
256   ret = disorder_simple_v(c, rp, cmd, 0, 0, ap);
257   va_end(ap);
258   return ret;
259 }
260
261 /** @brief Issue a command with a body and parse a simple response
262  * @param c Client
263  * @param rp Where to store result, or NULL (UTF-8)
264  * @param body Pointer to body
265  * @param nbody Size of body
266  * @param cmd Command
267  * @return 0 on success, non-0 on error
268  *
269  * See disorder_simple().
270  */
271 static int disorder_simple_body(disorder_client *c,
272                                 char **rp,
273                                 char **body, int nbody,
274                                 const char *cmd, ...) {
275   va_list ap;
276   int ret;
277
278   va_start(ap, cmd);
279   ret = disorder_simple_v(c, rp, cmd, body, nbody, ap);
280   va_end(ap);
281   return ret;
282 }
283
284 /** @brief Dequote a result string
285  * @param rc 0 on success, non-0 on error
286  * @param rp Where result string is stored (UTF-8)
287  * @return @p rc
288  *
289  * This is used as a wrapper around disorder_simple() to dequote
290  * results in place.
291  */
292 static int dequote(int rc, char **rp) {
293   char **rr;
294
295   if(!rc) {
296     if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) {
297       xfree(*rp);
298       *rp = *rr;
299       xfree(rr);
300       return 0;
301     }
302     disorder_error(0, "invalid reply: %s", *rp);
303   }
304   return rc;
305 }
306
307 /** @brief Generic connection routine
308  * @param conf Configuration to follow
309  * @param c Client
310  * @param username Username to log in with or NULL
311  * @param password Password to log in with or NULL
312  * @param cookie Cookie to log in with or NULL
313  * @return 0 on success, non-0 on error
314  *
315  * @p cookie is tried first if not NULL.  If it is NULL then @p
316  * username must not be.  If @p username is not NULL then nor may @p
317  * password be.
318  */
319 int disorder_connect_generic(struct config *conf,
320                              disorder_client *c,
321                              const char *username,
322                              const char *password,
323                              const char *cookie) {
324   int fd = -1, fd2 = -1, nrvec = 0, rc;
325   unsigned char *nonce = NULL;
326   size_t nl;
327   char *res = NULL;
328   char *r = NULL, **rvec = NULL;
329   const char *protocol, *algorithm, *challenge;
330   struct sockaddr *sa = NULL;
331   socklen_t salen;
332
333   if((salen = find_server(conf, &sa, &c->ident)) == (socklen_t)-1)
334     return -1;
335   c->fpin = c->fpout = 0;
336   if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
337     byte_xasprintf((char **)&c->last, "socket: %s", strerror(errno));
338     disorder_error(errno, "error calling socket");
339     return -1;
340   }
341   if(connect(fd, sa, salen) < 0) {
342     byte_xasprintf((char **)&c->last, "connect: %s", strerror(errno));
343     disorder_error(errno, "error calling connect");
344     goto error;
345   }
346   if((fd2 = dup(fd)) < 0) {
347     byte_xasprintf((char **)&c->last, "dup: %s", strerror(errno));
348     disorder_error(errno, "error calling dup");
349     goto error;
350   }
351   if(!(c->fpin = fdopen(fd, "rb"))) {
352     byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno));
353     disorder_error(errno, "error calling fdopen");
354     goto error;
355   }
356   fd = -1;
357   if(!(c->fpout = fdopen(fd2, "wb"))) {
358     byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno));
359     disorder_error(errno, "error calling fdopen");
360     goto error;
361   }
362   fd2 = -1;
363   if((rc = disorder_simple(c, &r, 0, (const char *)0)))
364     goto error_rc;
365   if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
366     goto error;
367   if(nrvec != 3) {
368     c->last = "cannot parse server greeting";
369     disorder_error(0, "cannot parse server greeting %s", r);
370     goto error;
371   }
372   protocol = rvec[0];
373   if(strcmp(protocol, "2")) {
374     c->last = "unknown protocol version";
375     disorder_error(0, "unknown protocol version: %s", protocol);
376     goto error;
377   }
378   algorithm = rvec[1];
379   challenge = rvec[2];
380   if(!(nonce = unhex(challenge, &nl)))
381     goto error;
382   if(cookie) {
383     if(!dequote(disorder_simple(c, &c->user, "cookie", cookie, (char *)0),
384                 &c->user))
385       return 0;                         /* success */
386     if(!username) {
387       c->last = "cookie failed and no username";
388       disorder_error(0, "cookie did not work and no username available");
389       goto error;
390     }
391   }
392   if(!(res = authhash(nonce, nl, password, algorithm))) {
393     c->last = "error computing authorization hash";
394     goto error;
395   }
396   if((rc = disorder_simple(c, 0, "user", username, res, (char *)0)))
397     goto error_rc;
398   c->user = xstrdup(username);
399   xfree(res);
400   free_strings(nrvec, rvec);
401   xfree(nonce);
402   xfree(sa);
403   xfree(r);
404   return 0;
405 error:
406   rc = -1;
407 error_rc:
408   if(c->fpin) {
409     fclose(c->fpin);
410     c->fpin = 0;
411   }
412   if(c->fpout) {
413     fclose(c->fpout);
414     c->fpout = 0;
415   }
416   if(fd2 != -1) close(fd2);
417   if(fd != -1) close(fd);
418   return rc;
419 }
420
421 /** @brief Connect a client with a specified username and password
422  * @param c Client
423  * @param username Username to log in with
424  * @param password Password to log in with
425  * @return 0 on success, non-0 on error
426  */
427 int disorder_connect_user(disorder_client *c,
428                           const char *username,
429                           const char *password) {
430   return disorder_connect_generic(config,
431                                   c,
432                                   username,
433                                   password,
434                                   0);
435 }
436
437 /** @brief Connect a client
438  * @param c Client
439  * @return 0 on success, non-0 on error
440  *
441  * The connection will use the username and password found in @ref
442  * config, or directly from the database if no password is found and
443  * the database is readable (usually only for root).
444  */
445 int disorder_connect(disorder_client *c) {
446   const char *username, *password;
447
448   if(!(username = config->username)) {
449     c->last = "no username";
450     disorder_error(0, "no username configured");
451     return -1;
452   }
453   password = config->password;
454   /* Maybe we can read the database */
455   if(!password && trackdb_readable()) {
456     trackdb_init(TRACKDB_NO_RECOVER|TRACKDB_NO_UPGRADE);
457     trackdb_open(TRACKDB_READ_ONLY);
458     password = trackdb_get_password(username);
459     trackdb_close();
460   }
461   if(!password) {
462     /* Oh well */
463     c->last = "no password";
464     disorder_error(0, "no password configured for user '%s'", username);
465     return -1;
466   }
467   return disorder_connect_generic(config,
468                                   c,
469                                   username,
470                                   password,
471                                   0);
472 }
473
474 /** @brief Connect a client
475  * @param c Client
476  * @param cookie Cookie to log in with, or NULL
477  * @return 0 on success, non-0 on error
478  *
479  * If @p cookie is NULL or does not work then we attempt to log in as
480  * guest instead (so when the cookie expires only an extra round trip
481  * is needed rathre than a complete new login).
482  */
483 int disorder_connect_cookie(disorder_client *c,
484                             const char *cookie) {
485   return disorder_connect_generic(config,
486                                   c,
487                                   "guest",
488                                   "",
489                                   cookie);
490 }
491
492 /** @brief Close a client
493  * @param c Client
494  * @return 0 on succcess, non-0 on errior
495  *
496  * The client is still closed even on error.  It might well be
497  * appropriate to ignore the return value.
498  */
499 int disorder_close(disorder_client *c) {
500   int ret = 0;
501
502   if(c->fpin) {
503     if(fclose(c->fpin) < 0) {
504       byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno));
505       disorder_error(errno, "error calling fclose");
506       ret = -1;
507     }
508     c->fpin = 0;
509   }
510   if(c->fpout) {
511     if(fclose(c->fpout) < 0) {
512       byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno));
513       disorder_error(errno, "error calling fclose");
514       ret = -1;
515     }
516     c->fpout = 0;
517   }
518   xfree(c->ident);
519   c->ident = 0;
520   xfree(c->user);
521   c->user = 0;
522   return 0;
523 }
524
525 /** @brief Play a track
526  * @param c Client
527  * @param track Track to play (UTF-8)
528  * @return 0 on success, non-0 on error
529  */
530 int disorder_play(disorder_client *c, const char *track) {
531   return disorder_simple(c, 0, "play", track, (char *)0);
532 }
533
534 /** @brief Remove a track
535  * @param c Client
536  * @param track Track to remove (UTF-8)
537  * @return 0 on success, non-0 on error
538  */
539 int disorder_remove(disorder_client *c, const char *track) {
540   return disorder_simple(c, 0, "remove", track, (char *)0);
541 }
542
543 /** @brief Move a track
544  * @param c Client
545  * @param track Track to move (UTF-8)
546  * @param delta Distance to move by
547  * @return 0 on success, non-0 on error
548  */
549 int disorder_move(disorder_client *c, const char *track, int delta) {
550   char d[16];
551
552   byte_snprintf(d, sizeof d, "%d", delta);
553   return disorder_simple(c, 0, "move", track, d, (char *)0);
554 }
555
556 /** @brief Enable play
557  * @param c Client
558  * @return 0 on success, non-0 on error
559  */
560 int disorder_enable(disorder_client *c) {
561   return disorder_simple(c, 0, "enable", (char *)0);
562 }
563
564 /** @brief Disable play
565  * @param c Client
566  * @return 0 on success, non-0 on error
567  */
568 int disorder_disable(disorder_client *c) {
569   return disorder_simple(c, 0, "disable", (char *)0);
570 }
571
572 /** @brief Scratch the currently playing track
573  * @param id Playing track ID or NULL (UTF-8)
574  * @param c Client
575  * @return 0 on success, non-0 on error
576  */
577 int disorder_scratch(disorder_client *c, const char *id) {
578   return disorder_simple(c, 0, "scratch", id, (char *)0);
579 }
580
581 /** @brief Shut down the server
582  * @param c Client
583  * @return 0 on success, non-0 on error
584  */
585 int disorder_shutdown(disorder_client *c) {
586   return disorder_simple(c, 0, "shutdown", (char *)0);
587 }
588
589 /** @brief Make the server re-read its configuration
590  * @param c Client
591  * @return 0 on success, non-0 on error
592  */
593 int disorder_reconfigure(disorder_client *c) {
594   return disorder_simple(c, 0, "reconfigure", (char *)0);
595 }
596
597 /** @brief Rescan tracks
598  * @param c Client
599  * @return 0 on success, non-0 on error
600  */
601 int disorder_rescan(disorder_client *c) {
602   return disorder_simple(c, 0, "rescan", (char *)0);
603 }
604
605 /** @brief Get server version number
606  * @param c Client
607  * @param rp Where to store version string (UTF-8)
608  * @return 0 on success, non-0 on error
609  */
610 int disorder_version(disorder_client *c, char **rp) {
611   return dequote(disorder_simple(c, rp, "version", (char *)0), rp);
612 }
613
614 static void client_error(const char *msg,
615                          void attribute((unused)) *u) {
616   disorder_error(0, "error parsing reply: %s", msg);
617 }
618
619 /** @brief Get currently playing track
620  * @param c Client
621  * @param qp Where to store track information
622  * @return 0 on success, non-0 on error
623  *
624  * @p qp gets NULL if no track is playing.
625  */
626 int disorder_playing(disorder_client *c, struct queue_entry **qp) {
627   char *r;
628   struct queue_entry *q;
629   int rc;
630
631   if((rc = disorder_simple(c, &r, "playing", (char *)0)))
632     return rc;
633   if(r) {
634     q = xmalloc(sizeof *q);
635     if(queue_unmarshall(q, r, client_error, 0))
636       return -1;
637     *qp = q;
638   } else
639     *qp = 0;
640   return 0;
641 }
642
643 /** @brief Fetch the queue, recent list, etc */
644 static int disorder_somequeue(disorder_client *c,
645                               const char *cmd, struct queue_entry **qp) {
646   struct queue_entry *qh, **qt = &qh, *q;
647   char *l;
648   int rc;
649
650   if((rc = disorder_simple(c, 0, cmd, (char *)0)))
651     return rc;
652   while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
653     if(!strcmp(l, ".")) {
654       *qt = 0;
655       *qp = qh;
656       return 0;
657     }
658     q = xmalloc(sizeof *q);
659     if(!queue_unmarshall(q, l, client_error, 0)) {
660       *qt = q;
661       qt = &q->next;
662     }
663   }
664   if(ferror(c->fpin)) {
665     byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
666     disorder_error(errno, "error reading %s", c->ident);
667   } else {
668     c->last = "input error: unexpxected EOF";
669     disorder_error(0, "error reading %s: unexpected EOF", c->ident);
670   }
671   return -1;
672 }
673
674 /** @brief Get recently played tracks
675  * @param c Client
676  * @param qp Where to store track information
677  * @return 0 on success, non-0 on error
678  *
679  * The last entry in the list is the most recently played track.
680  */
681 int disorder_recent(disorder_client *c, struct queue_entry **qp) {
682   return disorder_somequeue(c, "recent", qp);
683 }
684
685 /** @brief Get queue
686  * @param c Client
687  * @param qp Where to store track information
688  * @return 0 on success, non-0 on error
689  *
690  * The first entry in the list will be played next.
691  */
692 int disorder_queue(disorder_client *c, struct queue_entry **qp) {
693   return disorder_somequeue(c, "queue", qp);
694 }
695
696 /** @brief Read a dot-stuffed list
697  * @param c Client
698  * @param vecp Where to store list (UTF-8)
699  * @param nvecp Where to store number of items, or NULL
700  * @return 0 on success, non-0 on error
701  *
702  * The list will have a final NULL not counted in @p nvecp.
703  */
704 static int readlist(disorder_client *c, char ***vecp, int *nvecp) {
705   char *l;
706   struct vector v;
707
708   vector_init(&v);
709   while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
710     if(!strcmp(l, ".")) {
711       vector_terminate(&v);
712       if(nvecp)
713         *nvecp = v.nvec;
714       *vecp = v.vec;
715       xfree(l);
716       return 0;
717     }
718     vector_append(&v, xstrdup(l + (*l == '.')));
719     xfree(l);
720   }
721   if(ferror(c->fpin)) {
722     byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
723     disorder_error(errno, "error reading %s", c->ident);
724   } else {
725     c->last = "input error: unexpxected EOF";
726     disorder_error(0, "error reading %s: unexpected EOF", c->ident);
727   }
728   return -1;
729 }
730
731 /** @brief Issue a comamnd and get a list response
732  * @param c Client
733  * @param vecp Where to store list (UTF-8)
734  * @param nvecp Where to store number of items, or NULL
735  * @param cmd Command
736  * @return 0 on success, non-0 on error
737  *
738  * The remaining arguments are command arguments, terminated by (char
739  * *)0.  They should be in UTF-8.
740  *
741  * 5xx responses count as errors.
742  *
743  * See disorder_simple().
744  */
745 static int disorder_simple_list(disorder_client *c,
746                                 char ***vecp, int *nvecp,
747                                 const char *cmd, ...) {
748   va_list ap;
749   int ret;
750
751   va_start(ap, cmd);
752   ret = disorder_simple_v(c, 0, cmd, 0, 0, ap);
753   va_end(ap);
754   if(ret) return ret;
755   return readlist(c, vecp, nvecp);
756 }
757
758 /** @brief List directories below @p dir
759  * @param c Client
760  * @param dir Directory to list, or NULL for root (UTF-8)
761  * @param re Regexp that results must match, or NULL (UTF-8)
762  * @param vecp Where to store list (UTF-8)
763  * @param nvecp Where to store number of items, or NULL
764  * @return 0 on success, non-0 on error
765  */
766 int disorder_directories(disorder_client *c, const char *dir, const char *re,
767                          char ***vecp, int *nvecp) {
768   return disorder_simple_list(c, vecp, nvecp, "dirs", dir, re, (char *)0);
769 }
770
771 /** @brief List files below @p dir
772  * @param c Client
773  * @param dir Directory to list, or NULL for root (UTF-8)
774  * @param re Regexp that results must match, or NULL (UTF-8)
775  * @param vecp Where to store list (UTF-8)
776  * @param nvecp Where to store number of items, or NULL
777  * @return 0 on success, non-0 on error
778  */
779 int disorder_files(disorder_client *c, const char *dir, const char *re,
780                    char ***vecp, int *nvecp) {
781   return disorder_simple_list(c, vecp, nvecp, "files", dir, re, (char *)0);
782 }
783
784 /** @brief List files and directories below @p dir
785  * @param c Client
786  * @param dir Directory to list, or NULL for root (UTF-8)
787  * @param re Regexp that results must match, or NULL (UTF-8)
788  * @param vecp Where to store list (UTF-8)
789  * @param nvecp Where to store number of items, or NULL
790  * @return 0 on success, non-0 on error
791  */
792 int disorder_allfiles(disorder_client *c, const char *dir, const char *re,
793                       char ***vecp, int *nvecp) {
794   return disorder_simple_list(c, vecp, nvecp, "allfiles", dir, re, (char *)0);
795 }
796
797 /** @brief Return the user we logged in with
798  * @param c Client
799  * @return User name (owned by @p c, don't modify)
800  */
801 char *disorder_user(disorder_client *c) {
802   return c->user;
803 }
804
805 /** @brief Set a track preference
806  * @param c Client
807  * @param track Track name (UTF-8)
808  * @param key Preference name (UTF-8)
809  * @param value Preference value (UTF-8)
810  * @return 0 on success, non-0 on error
811  */
812 int disorder_set(disorder_client *c, const char *track,
813                  const char *key, const char *value) {
814   return disorder_simple(c, 0, "set", track, key, value, (char *)0);
815 }
816
817 /** @brief Unset a track preference
818  * @param c Client
819  * @param track Track name (UTF-8)
820  * @param key Preference name (UTF-8)
821  * @return 0 on success, non-0 on error
822  */
823 int disorder_unset(disorder_client *c, const char *track,
824                    const char *key) {
825   return disorder_simple(c, 0, "unset", track, key, (char *)0);
826 }
827
828 /** @brief Get a track preference
829  * @param c Client
830  * @param track Track name (UTF-8)
831  * @param key Preference name (UTF-8)
832  * @param valuep Where to store preference value (UTF-8)
833  * @return 0 on success, non-0 on error
834  */
835 int disorder_get(disorder_client *c,
836                  const char *track, const char *key, char **valuep) {
837   return dequote(disorder_simple(c, valuep, "get", track, key, (char *)0),
838                  valuep);
839 }
840
841 static void pref_error_handler(const char *msg,
842                                void attribute((unused)) *u) {
843   disorder_error(0, "error handling 'prefs' reply: %s", msg);
844 }
845
846 /** @brief Get all preferences for a trcak
847  * @param c Client
848  * @param track Track name
849  * @param kp Where to store linked list of preferences
850  * @return 0 on success, non-0 on error
851  */
852 int disorder_prefs(disorder_client *c, const char *track, struct kvp **kp) {
853   char **vec, **pvec;
854   int nvec, npvec, n, rc;
855   struct kvp *k;
856
857   if((rc = disorder_simple_list(c, &vec, &nvec, "prefs", track, (char *)0)))
858     return rc;
859   for(n = 0; n < nvec; ++n) {
860     if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pref_error_handler, 0)))
861       return -1;
862     if(npvec != 2) {
863       pref_error_handler("malformed response", 0);
864       return -1;
865     }
866     *kp = k = xmalloc(sizeof *k);
867     k->name = pvec[0];
868     k->value = pvec[1];
869     kp = &k->next;
870   }
871   *kp = 0;
872   return 0;
873 }
874
875 /** @brief Parse a boolean response
876  * @param cmd Command for use in error messsage
877  * @param value Result from server
878  * @param flagp Where to store result
879  * @return 0 on success, non-0 on error
880  */
881 static int boolean(const char *cmd, const char *value,
882                    int *flagp) {
883   if(!strcmp(value, "yes")) *flagp = 1;
884   else if(!strcmp(value, "no")) *flagp = 0;
885   else {
886     disorder_error(0, "malformed response to '%s'", cmd);
887     return -1;
888   }
889   return 0;
890 }
891
892 /** @brief Test whether a track exists
893  * @param c Client
894  * @param track Track name (UTF-8)
895  * @param existsp Where to store result (non-0 iff does exist)
896  * @return 0 on success, non-0 on error
897  */
898 int disorder_exists(disorder_client *c, const char *track, int *existsp) {
899   char *v;
900   int rc;
901
902   if((rc = disorder_simple(c, &v, "exists", track, (char *)0)))
903     return rc;
904   return boolean("exists", v, existsp);
905 }
906
907 /** @brief Test whether playing is enabled
908  * @param c Client
909  * @param enabledp Where to store result (non-0 iff enabled)
910  * @return 0 on success, non-0 on error
911  */
912 int disorder_enabled(disorder_client *c, int *enabledp) {
913   char *v;
914   int rc;
915
916   if((rc = disorder_simple(c, &v, "enabled", (char *)0)))
917     return rc;
918   return boolean("enabled", v, enabledp);
919 }
920
921 /** @brief Get the length of a track
922  * @param c Client
923  * @param track Track name (UTF-8)
924  * @param valuep Where to store length in seconds
925  * @return 0 on success, non-0 on error
926  *
927  * If the length is unknown 0 is returned.
928  */
929 int disorder_length(disorder_client *c, const char *track,
930                     long *valuep) {
931   char *value;
932   int rc;
933
934   if((rc = disorder_simple(c, &value, "length", track, (char *)0)))
935     return rc;
936   *valuep = atol(value);
937   return 0;
938 }
939
940 /** @brief Search for tracks
941  * @param c Client
942  * @param terms Search terms (UTF-8)
943  * @param vecp Where to store list (UTF-8)
944  * @param nvecp Where to store number of items, or NULL
945  * @return 0 on success, non-0 on error
946  */
947 int disorder_search(disorder_client *c, const char *terms,
948                     char ***vecp, int *nvecp) {
949   return disorder_simple_list(c, vecp, nvecp, "search", terms, (char *)0);
950 }
951
952 /** @brief Enable random play
953  * @param c Client
954  * @return 0 on success, non-0 on error
955  */
956 int disorder_random_enable(disorder_client *c) {
957   return disorder_simple(c, 0, "random-enable", (char *)0);
958 }
959
960 /** @brief Disable random play
961  * @param c Client
962  * @return 0 on success, non-0 on error
963  */
964 int disorder_random_disable(disorder_client *c) {
965   return disorder_simple(c, 0, "random-disable", (char *)0);
966 }
967
968 /** @brief Test whether random play is enabled
969  * @param c Client
970  * @param enabledp Where to store result (non-0 iff enabled)
971  * @return 0 on success, non-0 on error
972  */
973 int disorder_random_enabled(disorder_client *c, int *enabledp) {
974   char *v;
975   int rc;
976
977   if((rc = disorder_simple(c, &v, "random-enabled", (char *)0)))
978     return rc;
979   return boolean("random-enabled", v, enabledp);
980 }
981
982 /** @brief Get server stats
983  * @param c Client
984  * @param vecp Where to store list (UTF-8)
985  * @param nvecp Where to store number of items, or NULL
986  * @return 0 on success, non-0 on error
987  */
988 int disorder_stats(disorder_client *c,
989                    char ***vecp, int *nvecp) {
990   return disorder_simple_list(c, vecp, nvecp, "stats", (char *)0);
991 }
992
993 /** @brief Set volume
994  * @param c Client
995  * @param left New left channel value
996  * @param right New right channel value
997  * @return 0 on success, non-0 on error
998  */
999 int disorder_set_volume(disorder_client *c, int left, int right) {
1000   char *ls, *rs;
1001
1002   if(byte_asprintf(&ls, "%d", left) < 0
1003      || byte_asprintf(&rs, "%d", right) < 0)
1004     return -1;
1005   return disorder_simple(c, 0, "volume", ls, rs, (char *)0);
1006 }
1007
1008 /** @brief Get volume
1009  * @param c Client
1010  * @param left Where to store left channel value
1011  * @param right Where to store right channel value
1012  * @return 0 on success, non-0 on error
1013  */
1014 int disorder_get_volume(disorder_client *c, int *left, int *right) {
1015   char *r;
1016   int rc;
1017
1018   if((rc = disorder_simple(c, &r, "volume", (char *)0)))
1019     return rc;
1020   if(sscanf(r, "%d %d", left, right) != 2) {
1021     c->last = "malformed volume response";
1022     disorder_error(0, "error parsing response to 'volume': '%s'", r);
1023     return -1;
1024   }
1025   return 0;
1026 }
1027
1028 /** @brief Log to a sink
1029  * @param c Client
1030  * @param s Sink to write log lines to
1031  * @return 0 on success, non-0 on error
1032  */
1033 int disorder_log(disorder_client *c, struct sink *s) {
1034   char *l;
1035   int rc;
1036     
1037   if((rc = disorder_simple(c, 0, "log", (char *)0)))
1038     return rc;
1039   while(inputline(c->ident, c->fpin, &l, '\n') >= 0 && strcmp(l, "."))
1040     if(sink_printf(s, "%s\n", l) < 0) return -1;
1041   if(ferror(c->fpin) || feof(c->fpin)) {
1042     byte_xasprintf((char **)&c->last, "input error: %s",
1043                    ferror(c->fpin) ? strerror(errno) : "unexpxected EOF");
1044     return -1;
1045   }
1046   return 0;
1047 }
1048
1049 /** @brief Look up a track name part
1050  * @param c Client
1051  * @param partp Where to store result (UTF-8)
1052  * @param track Track name (UTF-8)
1053  * @param context Context (usually "sort" or "display") (UTF-8)
1054  * @param part Track part (UTF-8)
1055  * @return 0 on success, non-0 on error
1056  */
1057 int disorder_part(disorder_client *c, char **partp,
1058                   const char *track, const char *context, const char *part) {
1059   return dequote(disorder_simple(c, partp, "part",
1060                                  track, context, part, (char *)0), partp);
1061 }
1062
1063 /** @brief Resolve aliases
1064  * @param c Client
1065  * @param trackp Where to store canonical name (UTF-8)
1066  * @param track Track name (UTF-8)
1067  * @return 0 on success, non-0 on error
1068  */
1069 int disorder_resolve(disorder_client *c, char **trackp, const char *track) {
1070   return dequote(disorder_simple(c, trackp, "resolve", track, (char *)0),
1071                  trackp);
1072 }
1073
1074 /** @brief Pause the current track
1075  * @param c Client
1076  * @return 0 on success, non-0 on error
1077  */
1078 int disorder_pause(disorder_client *c) {
1079   return disorder_simple(c, 0, "pause", (char *)0);
1080 }
1081
1082 /** @brief Resume the current track
1083  * @param c Client
1084  * @return 0 on success, non-0 on error
1085  */
1086 int disorder_resume(disorder_client *c) {
1087   return disorder_simple(c, 0, "resume", (char *)0);
1088 }
1089
1090 /** @brief List all known tags
1091  * @param c Client
1092  * @param vecp Where to store list (UTF-8)
1093  * @param nvecp Where to store number of items, or NULL
1094  * @return 0 on success, non-0 on error
1095  */
1096 int disorder_tags(disorder_client *c,
1097                    char ***vecp, int *nvecp) {
1098   return disorder_simple_list(c, vecp, nvecp, "tags", (char *)0);
1099 }
1100
1101 /** @brief List all known users
1102  * @param c Client
1103  * @param vecp Where to store list (UTF-8)
1104  * @param nvecp Where to store number of items, or NULL
1105  * @return 0 on success, non-0 on error
1106  */
1107 int disorder_users(disorder_client *c,
1108                    char ***vecp, int *nvecp) {
1109   return disorder_simple_list(c, vecp, nvecp, "users", (char *)0);
1110 }
1111
1112 /** @brief Get recently added tracks
1113  * @param c Client
1114  * @param vecp Where to store pointer to list (UTF-8)
1115  * @param nvecp Where to store count
1116  * @param max Maximum tracks to fetch, or 0 for all available
1117  * @return 0 on success, non-0 on error
1118  */
1119 int disorder_new_tracks(disorder_client *c,
1120                         char ***vecp, int *nvecp,
1121                         int max) {
1122   char limit[32];
1123
1124   sprintf(limit, "%d", max);
1125   return disorder_simple_list(c, vecp, nvecp, "new", limit, (char *)0);
1126 }
1127
1128 /** @brief Set a global preference
1129  * @param c Client
1130  * @param key Preference name (UTF-8)
1131  * @param value Preference value (UTF-8)
1132  * @return 0 on success, non-0 on error
1133  */
1134 int disorder_set_global(disorder_client *c,
1135                         const char *key, const char *value) {
1136   return disorder_simple(c, 0, "set-global", key, value, (char *)0);
1137 }
1138
1139 /** @brief Unset a global preference
1140  * @param c Client
1141  * @param key Preference name (UTF-8)
1142  * @return 0 on success, non-0 on error
1143  */
1144 int disorder_unset_global(disorder_client *c, const char *key) {
1145   return disorder_simple(c, 0, "unset-global", key, (char *)0);
1146 }
1147
1148 /** @brief Get a global preference
1149  * @param c Client
1150  * @param key Preference name (UTF-8)
1151  * @param valuep Where to store preference value (UTF-8)
1152  * @return 0 on success, non-0 on error
1153  */
1154 int disorder_get_global(disorder_client *c, const char *key, char **valuep) {
1155   return dequote(disorder_simple(c, valuep, "get-global", key, (char *)0),
1156                  valuep);
1157 }
1158
1159 /** @brief Get server's RTP address information
1160  * @param c Client
1161  * @param addressp Where to store address (UTF-8)
1162  * @param portp Where to store port (UTF-8)
1163  * @return 0 on success, non-0 on error
1164  */
1165 int disorder_rtp_address(disorder_client *c, char **addressp, char **portp) {
1166   char *r;
1167   int rc, n;
1168   char **vec;
1169
1170   if((rc = disorder_simple(c, &r, "rtp-address", (char *)0)))
1171     return rc;
1172   vec = split(r, &n, SPLIT_QUOTES, 0, 0);
1173   if(n != 2) {
1174     c->last = "malformed RTP address";
1175     disorder_error(0, "malformed rtp-address reply");
1176     return -1;
1177   }
1178   *addressp = vec[0];
1179   *portp = vec[1];
1180   return 0;
1181 }
1182
1183 /** @brief Create a user
1184  * @param c Client
1185  * @param user Username
1186  * @param password Password
1187  * @param rights Initial rights or NULL to use default
1188  * @return 0 on success, non-0 on error
1189  */
1190 int disorder_adduser(disorder_client *c,
1191                      const char *user, const char *password,
1192                      const char *rights) {
1193   return disorder_simple(c, 0, "adduser", user, password, rights, (char *)0);
1194 }
1195
1196 /** @brief Delete a user
1197  * @param c Client
1198  * @param user Username
1199  * @return 0 on success, non-0 on error
1200  */
1201 int disorder_deluser(disorder_client *c, const char *user) {
1202   return disorder_simple(c, 0, "deluser", user, (char *)0);
1203 }
1204
1205 /** @brief Get user information
1206  * @param c Client
1207  * @param user Username
1208  * @param key Property name (UTF-8)
1209  * @param valuep Where to store value (UTF-8)
1210  * @return 0 on success, non-0 on error
1211  */
1212 int disorder_userinfo(disorder_client *c, const char *user, const char *key,
1213                       char **valuep) {
1214   return dequote(disorder_simple(c, valuep, "userinfo", user, key, (char *)0),
1215                  valuep);
1216 }
1217
1218 /** @brief Set user information
1219  * @param c Client
1220  * @param user Username
1221  * @param key Property name (UTF-8)
1222  * @param value New property value (UTF-8)
1223  * @return 0 on success, non-0 on error
1224  */
1225 int disorder_edituser(disorder_client *c, const char *user,
1226                       const char *key, const char *value) {
1227   return disorder_simple(c, 0, "edituser", user, key, value, (char *)0);
1228 }
1229
1230 /** @brief Register a user
1231  * @param c Client
1232  * @param user Username
1233  * @param password Password
1234  * @param email Email address (UTF-8)
1235  * @param confirmp Where to store confirmation string
1236  * @return 0 on success, non-0 on error
1237  */
1238 int disorder_register(disorder_client *c, const char *user,
1239                       const char *password, const char *email,
1240                       char **confirmp) {
1241   return dequote(disorder_simple(c, confirmp, "register",
1242                                  user, password, email, (char *)0),
1243                  confirmp);
1244 }
1245
1246 /** @brief Confirm a user
1247  * @param c Client
1248  * @param confirm Confirmation string
1249  * @return 0 on success, non-0 on error
1250  */
1251 int disorder_confirm(disorder_client *c, const char *confirm) {
1252   char *u;
1253   int rc;
1254   
1255   if(!(rc = dequote(disorder_simple(c, &u, "confirm", confirm, (char *)0),
1256                     &u)))
1257     c->user = u;
1258   return rc;
1259 }
1260
1261 /** @brief Make a cookie for this login
1262  * @param c Client
1263  * @param cookiep Where to store cookie string
1264  * @return 0 on success, non-0 on error
1265  */
1266 int disorder_make_cookie(disorder_client *c, char **cookiep) {
1267   return dequote(disorder_simple(c, cookiep, "make-cookie", (char *)0),
1268                  cookiep);
1269 }
1270
1271 /** @brief Revoke the cookie used by this session
1272  * @param c Client
1273  * @return 0 on success, non-0 on error
1274  */
1275 int disorder_revoke(disorder_client *c) {
1276   return disorder_simple(c, 0, "revoke", (char *)0);
1277 }
1278
1279 /** @brief Request a password reminder email
1280  * @param c Client
1281  * @param user Username
1282  * @return 0 on success, non-0 on error
1283  */
1284 int disorder_reminder(disorder_client *c, const char *user) {
1285   return disorder_simple(c, 0, "reminder", user, (char *)0);
1286 }
1287
1288 /** @brief List scheduled events
1289  * @param c Client
1290  * @param idsp Where to put list of event IDs
1291  * @param nidsp Where to put count of event IDs, or NULL
1292  * @return 0 on success, non-0 on error
1293  */
1294 int disorder_schedule_list(disorder_client *c, char ***idsp, int *nidsp) {
1295   return disorder_simple_list(c, idsp, nidsp, "schedule-list", (char *)0);
1296 }
1297
1298 /** @brief Delete a scheduled event
1299  * @param c Client
1300  * @param id Event ID to delete
1301  * @return 0 on success, non-0 on error
1302  */
1303 int disorder_schedule_del(disorder_client *c, const char *id) {
1304   return disorder_simple(c, 0, "schedule-del", id, (char *)0);
1305 }
1306
1307 /** @brief Get details of a scheduled event
1308  * @param c Client
1309  * @param id Event ID
1310  * @param actiondatap Where to put details
1311  * @return 0 on success, non-0 on error
1312  */
1313 int disorder_schedule_get(disorder_client *c, const char *id,
1314                           struct kvp **actiondatap) {
1315   char **lines, **bits;
1316   int rc, nbits;
1317
1318   *actiondatap = 0;
1319   if((rc = disorder_simple_list(c, &lines, NULL,
1320                                 "schedule-get", id, (char *)0)))
1321     return rc;
1322   while(*lines) {
1323     if(!(bits = split(*lines++, &nbits, SPLIT_QUOTES, 0, 0))) {
1324       disorder_error(0, "invalid schedule-get reply: cannot split line");
1325       return -1;
1326     }
1327     if(nbits != 2) {
1328       disorder_error(0, "invalid schedule-get reply: wrong number of fields");
1329       return -1;
1330     }
1331     kvp_set(actiondatap, bits[0], bits[1]);
1332   }
1333   return 0;
1334 }
1335
1336 /** @brief Add a scheduled event
1337  * @param c Client
1338  * @param when When to trigger the event
1339  * @param priority Event priority ("normal" or "junk")
1340  * @param action What action to perform
1341  * @param ... Action-specific arguments
1342  * @return 0 on success, non-0 on error
1343  *
1344  * For action @c "play" the next argument is the track.
1345  *
1346  * For action @c "set-global" next argument is the global preference name
1347  * and the final argument the value to set it to, or (char *)0 to unset it.
1348  */
1349 int disorder_schedule_add(disorder_client *c,
1350                           time_t when,
1351                           const char *priority,
1352                           const char *action,
1353                           ...) {
1354   va_list ap;
1355   char when_str[64];
1356   int rc;
1357
1358   snprintf(when_str, sizeof when_str, "%lld", (long long)when);
1359   va_start(ap, action);
1360   if(!strcmp(action, "play"))
1361     rc = disorder_simple(c, 0, "schedule-add", when_str, priority,
1362                          action, va_arg(ap, char *),
1363                          (char *)0);
1364   else if(!strcmp(action, "set-global")) {
1365     const char *key = va_arg(ap, char *);
1366     const char *value = va_arg(ap, char *);
1367     rc = disorder_simple(c, 0,"schedule-add",  when_str, priority,
1368                          action, key, value,
1369                          (char *)0);
1370   } else
1371     disorder_fatal(0, "unknown action '%s'", action);
1372   va_end(ap);
1373   return rc;
1374 }
1375
1376 /** @brief Adopt a track
1377  * @param c Client
1378  * @param id Track ID to adopt
1379  * @return 0 on success, non-0 on error
1380  */
1381 int disorder_adopt(disorder_client *c, const char *id) {
1382   return disorder_simple(c, 0, "adopt", id, (char *)0);
1383 }
1384
1385 /** @brief Delete a playlist
1386  * @param c Client
1387  * @param playlist Playlist to delete
1388  * @return 0 on success, non-0 on error
1389  */
1390 int disorder_playlist_delete(disorder_client *c,
1391                              const char *playlist) {
1392   return disorder_simple(c, 0, "playlist-delete", playlist, (char *)0);
1393 }
1394
1395 /** @brief Get the contents of a playlist
1396  * @param c Client
1397  * @param playlist Playlist to get
1398  * @param tracksp Where to put list of tracks
1399  * @param ntracksp Where to put count of tracks
1400  * @return 0 on success, non-0 on error
1401  */
1402 int disorder_playlist_get(disorder_client *c, const char *playlist,
1403                           char ***tracksp, int *ntracksp) {
1404   return disorder_simple_list(c, tracksp, ntracksp,
1405                               "playlist-get", playlist, (char *)0);
1406 }
1407
1408 /** @brief List all readable playlists
1409  * @param c Client
1410  * @param playlistsp Where to put list of playlists
1411  * @param nplaylistsp Where to put count of playlists
1412  * @return 0 on success, non-0 on error
1413  */
1414 int disorder_playlists(disorder_client *c,
1415                        char ***playlistsp, int *nplaylistsp) {
1416   return disorder_simple_list(c, playlistsp, nplaylistsp,
1417                               "playlists", (char *)0);
1418 }
1419
1420 /** @brief Get the sharing status of a playlist
1421  * @param c Client
1422  * @param playlist Playlist to inspect
1423  * @param sharep Where to put sharing status
1424  * @return 0 on success, non-0 on error
1425  *
1426  * Possible @p sharep values are @c public, @c private and @c shared.
1427  */
1428 int disorder_playlist_get_share(disorder_client *c, const char *playlist,
1429                                 char **sharep) {
1430   return disorder_simple(c, sharep,
1431                          "playlist-get-share", playlist, (char *)0);
1432 }
1433
1434 /** @brief Get the sharing status of a playlist
1435  * @param c Client
1436  * @param playlist Playlist to modify
1437  * @param share New sharing status
1438  * @return 0 on success, non-0 on error
1439  *
1440  * Possible @p share values are @c public, @c private and @c shared.
1441  */
1442 int disorder_playlist_set_share(disorder_client *c, const char *playlist,
1443                                 const char *share) {
1444   return disorder_simple(c, 0,
1445                          "playlist-set-share", playlist, share, (char *)0);
1446 }
1447
1448 /** @brief Lock a playlist for modifications
1449  * @param c Client
1450  * @param playlist Playlist to lock
1451  * @return 0 on success, non-0 on error
1452  */
1453 int disorder_playlist_lock(disorder_client *c, const char *playlist) {
1454   return disorder_simple(c, 0,
1455                          "playlist-lock", playlist, (char *)0);
1456 }
1457
1458 /** @brief Unlock the locked playlist
1459  * @param c Client
1460  * @return 0 on success, non-0 on error
1461  */
1462 int disorder_playlist_unlock(disorder_client *c) {
1463   return disorder_simple(c, 0,
1464                          "playlist-unlock", (char *)0);
1465 }
1466
1467 /** @brief Set the contents of a playlst
1468  * @param c Client
1469  * @param playlist Playlist to modify
1470  * @param tracks List of tracks
1471  * @param ntracks Length of @p tracks (or -1 to count up to the first NULL)
1472  * @return 0 on success, non-0 on error
1473  */
1474 int disorder_playlist_set(disorder_client *c,
1475                           const char *playlist,
1476                           char **tracks,
1477                           int ntracks) {
1478   return disorder_simple_body(c, 0, tracks, ntracks,
1479                               "playlist-set", playlist, (char *)0);
1480 }
1481
1482 /*
1483 Local Variables:
1484 c-basic-offset:2
1485 comment-column:40
1486 End:
1487 */