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