chiark / gitweb /
Mention scripts/protocol in README.developers, and bring other bits a
[disorder] / lib / client.c
... / ...
CommitLineData
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 */
57struct 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 */
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
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 */
92static 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 */
119const 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 */
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);
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 Marker for a command body */
155static const char disorder_body[1];
156
157/** @brief Marker for a list of args */
158static const char disorder_list[1];
159
160/** @brief Issue a command and parse a simple response
161 * @param c Client
162 * @param rp Where to store result, or NULL
163 * @param cmd Command
164 * @param ap Arguments (UTF-8), terminated by (char *)0
165 * @return 0 on success, non-0 on error
166 *
167 * 5xx responses count as errors.
168 *
169 * @p rp will NOT be filled in for xx9 responses (where it is just
170 * commentary for a command where it would normally be meaningful).
171 *
172 * NB that the response will NOT be converted to the local encoding
173 * nor will quotes be stripped. See dequote().
174 *
175 * Put @ref disorder_body in the argument list followed by a char **
176 * and int giving the body to follow the command. If the int is @c -1
177 * then the list is assumed to be NULL-terminated. This may be used
178 * only once.
179 *
180 * Put @ref disorder_list in the argument list followed by a char **
181 * and int giving a list of arguments to include. If the int is @c -1
182 * then the list is assumed to be NULL-terminated. This may be used
183 * any number of times.
184 *
185 * Usually you would call this via one of the following interfaces:
186 * - disorder_simple()
187 * - disorder_simple_list()
188 */
189static int disorder_simple_v(disorder_client *c,
190 char **rp,
191 const char *cmd,
192 va_list ap) {
193 const char *arg;
194 struct dynstr d;
195 char **body = NULL;
196 int nbody = 0;
197 int has_body = 0;
198
199 if(!c->fpout) {
200 c->last = "not connected";
201 disorder_error(0, "not connected to server");
202 return -1;
203 }
204 if(cmd) {
205 dynstr_init(&d);
206 dynstr_append_string(&d, cmd);
207 while((arg = va_arg(ap, const char *))) {
208 if(arg == disorder_body) {
209 body = va_arg(ap, char **);
210 nbody = va_arg(ap, int);
211 has_body = 1;
212 } else if(arg == disorder_list) {
213 char **list = va_arg(ap, char **);
214 int nlist = va_arg(ap, int);
215 if(nlist < 0) {
216 for(nlist = 0; list[nlist]; ++nlist)
217 ;
218 }
219 for(int n = 0; n < nlist; ++n) {
220 dynstr_append(&d, ' ');
221 dynstr_append_string(&d, quoteutf8(arg));
222 }
223 } else {
224 dynstr_append(&d, ' ');
225 dynstr_append_string(&d, quoteutf8(arg));
226 }
227 }
228 dynstr_append(&d, '\n');
229 dynstr_terminate(&d);
230 D(("command: %s", d.vec));
231 if(fputs(d.vec, c->fpout) < 0)
232 goto write_error;
233 xfree(d.vec);
234 if(has_body) {
235 if(nbody < 0)
236 for(nbody = 0; body[nbody]; ++nbody)
237 ;
238 for(int n = 0; n < nbody; ++n) {
239 if(body[n][0] == '.')
240 if(fputc('.', c->fpout) < 0)
241 goto write_error;
242 if(fputs(body[n], c->fpout) < 0)
243 goto write_error;
244 if(fputc('\n', c->fpout) < 0)
245 goto write_error;
246 }
247 if(fputs(".\n", c->fpout) < 0)
248 goto write_error;
249 }
250 if(fflush(c->fpout))
251 goto write_error;
252 }
253 return check_response(c, rp);
254write_error:
255 byte_xasprintf((char **)&c->last, "write error: %s", strerror(errno));
256 disorder_error(errno, "error writing to %s", c->ident);
257 return -1;
258}
259
260/** @brief Issue a command and parse a simple response
261 * @param c Client
262 * @param rp Where to store result, or NULL (UTF-8)
263 * @param cmd Command
264 * @return 0 on success, non-0 on error
265 *
266 * The remaining arguments are command arguments, terminated by (char
267 * *)0. They should be in UTF-8.
268 *
269 * 5xx responses count as errors.
270 *
271 * @p rp will NOT be filled in for xx9 responses (where it is just
272 * commentary for a command where it would normally be meaningful).
273 *
274 * NB that the response will NOT be converted to the local encoding
275 * nor will quotes be stripped. See dequote().
276 */
277static int disorder_simple(disorder_client *c,
278 char **rp,
279 const char *cmd, ...) {
280 va_list ap;
281 int ret;
282
283 va_start(ap, cmd);
284 ret = disorder_simple_v(c, rp, cmd, ap);
285 va_end(ap);
286 return ret;
287}
288
289/** @brief Dequote a result string
290 * @param rc 0 on success, non-0 on error
291 * @param rp Where result string is stored (UTF-8)
292 * @return @p rc
293 *
294 * This is used as a wrapper around disorder_simple() to dequote
295 * results in place.
296 */
297static int dequote(int rc, char **rp) {
298 char **rr;
299
300 if(!rc) {
301 if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) {
302 xfree(*rp);
303 *rp = *rr;
304 xfree(rr);
305 return 0;
306 }
307 disorder_error(0, "invalid reply: %s", *rp);
308 }
309 return rc;
310}
311
312/** @brief Generic connection routine
313 * @param conf Configuration to follow
314 * @param c Client
315 * @param username Username to log in with or NULL
316 * @param password Password to log in with or NULL
317 * @param cookie Cookie to log in with or NULL
318 * @return 0 on success, non-0 on error
319 *
320 * @p cookie is tried first if not NULL. If it is NULL then @p
321 * username must not be. If @p username is not NULL then nor may @p
322 * password be.
323 */
324int disorder_connect_generic(struct config *conf,
325 disorder_client *c,
326 const char *username,
327 const char *password,
328 const char *cookie) {
329 int fd = -1, fd2 = -1, nrvec = 0, rc;
330 unsigned char *nonce = NULL;
331 size_t nl;
332 char *res = NULL;
333 char *r = NULL, **rvec = NULL;
334 const char *protocol, *algorithm, *challenge;
335 struct sockaddr *sa = NULL;
336 socklen_t salen;
337
338 if((salen = find_server(conf, &sa, &c->ident)) == (socklen_t)-1)
339 return -1;
340 c->fpin = c->fpout = 0;
341 if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
342 byte_xasprintf((char **)&c->last, "socket: %s", strerror(errno));
343 disorder_error(errno, "error calling socket");
344 return -1;
345 }
346 if(connect(fd, sa, salen) < 0) {
347 byte_xasprintf((char **)&c->last, "connect: %s", strerror(errno));
348 disorder_error(errno, "error calling connect");
349 goto error;
350 }
351 if((fd2 = dup(fd)) < 0) {
352 byte_xasprintf((char **)&c->last, "dup: %s", strerror(errno));
353 disorder_error(errno, "error calling dup");
354 goto error;
355 }
356 if(!(c->fpin = fdopen(fd, "rb"))) {
357 byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno));
358 disorder_error(errno, "error calling fdopen");
359 goto error;
360 }
361 fd = -1;
362 if(!(c->fpout = fdopen(fd2, "wb"))) {
363 byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno));
364 disorder_error(errno, "error calling fdopen");
365 goto error;
366 }
367 fd2 = -1;
368 if((rc = disorder_simple(c, &r, 0, (const char *)0)))
369 goto error_rc;
370 if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
371 goto error;
372 if(nrvec != 3) {
373 c->last = "cannot parse server greeting";
374 disorder_error(0, "cannot parse server greeting %s", r);
375 goto error;
376 }
377 protocol = rvec[0];
378 if(strcmp(protocol, "2")) {
379 c->last = "unknown protocol version";
380 disorder_error(0, "unknown protocol version: %s", protocol);
381 goto error;
382 }
383 algorithm = rvec[1];
384 challenge = rvec[2];
385 if(!(nonce = unhex(challenge, &nl)))
386 goto error;
387 if(cookie) {
388 if(!dequote(disorder_simple(c, &c->user, "cookie", cookie, (char *)0),
389 &c->user))
390 return 0; /* success */
391 if(!username) {
392 c->last = "cookie failed and no username";
393 disorder_error(0, "cookie did not work and no username available");
394 goto error;
395 }
396 }
397 if(!(res = authhash(nonce, nl, password, algorithm))) {
398 c->last = "error computing authorization hash";
399 goto error;
400 }
401 if((rc = disorder_simple(c, 0, "user", username, res, (char *)0)))
402 goto error_rc;
403 c->user = xstrdup(username);
404 xfree(res);
405 free_strings(nrvec, rvec);
406 xfree(nonce);
407 xfree(sa);
408 xfree(r);
409 return 0;
410error:
411 rc = -1;
412error_rc:
413 if(c->fpin) {
414 fclose(c->fpin);
415 c->fpin = 0;
416 }
417 if(c->fpout) {
418 fclose(c->fpout);
419 c->fpout = 0;
420 }
421 if(fd2 != -1) close(fd2);
422 if(fd != -1) close(fd);
423 return rc;
424}
425
426/** @brief Connect a client with a specified username and password
427 * @param c Client
428 * @param username Username to log in with
429 * @param password Password to log in with
430 * @return 0 on success, non-0 on error
431 */
432int disorder_connect_user(disorder_client *c,
433 const char *username,
434 const char *password) {
435 return disorder_connect_generic(config,
436 c,
437 username,
438 password,
439 0);
440}
441
442/** @brief Connect a client
443 * @param c Client
444 * @return 0 on success, non-0 on error
445 *
446 * The connection will use the username and password found in @ref
447 * config, or directly from the database if no password is found and
448 * the database is readable (usually only for root).
449 */
450int disorder_connect(disorder_client *c) {
451 const char *username, *password;
452
453 if(!(username = config->username)) {
454 c->last = "no username";
455 disorder_error(0, "no username configured");
456 return -1;
457 }
458 password = config->password;
459 /* Maybe we can read the database */
460 if(!password && trackdb_readable()) {
461 trackdb_init(TRACKDB_NO_RECOVER|TRACKDB_NO_UPGRADE);
462 trackdb_open(TRACKDB_READ_ONLY);
463 password = trackdb_get_password(username);
464 trackdb_close();
465 }
466 if(!password) {
467 /* Oh well */
468 c->last = "no password";
469 disorder_error(0, "no password configured for user '%s'", username);
470 return -1;
471 }
472 return disorder_connect_generic(config,
473 c,
474 username,
475 password,
476 0);
477}
478
479/** @brief Connect a client
480 * @param c Client
481 * @param cookie Cookie to log in with, or NULL
482 * @return 0 on success, non-0 on error
483 *
484 * If @p cookie is NULL or does not work then we attempt to log in as
485 * guest instead (so when the cookie expires only an extra round trip
486 * is needed rathre than a complete new login).
487 */
488int disorder_connect_cookie(disorder_client *c,
489 const char *cookie) {
490 return disorder_connect_generic(config,
491 c,
492 "guest",
493 "",
494 cookie);
495}
496
497/** @brief Close a client
498 * @param c Client
499 * @return 0 on succcess, non-0 on errior
500 *
501 * The client is still closed even on error. It might well be
502 * appropriate to ignore the return value.
503 */
504int disorder_close(disorder_client *c) {
505 int ret = 0;
506
507 if(c->fpin) {
508 if(fclose(c->fpin) < 0) {
509 byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno));
510 disorder_error(errno, "error calling fclose");
511 ret = -1;
512 }
513 c->fpin = 0;
514 }
515 if(c->fpout) {
516 if(fclose(c->fpout) < 0) {
517 byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno));
518 disorder_error(errno, "error calling fclose");
519 ret = -1;
520 }
521 c->fpout = 0;
522 }
523 xfree(c->ident);
524 c->ident = 0;
525 xfree(c->user);
526 c->user = 0;
527 return ret;
528}
529
530static void client_error(const char *msg,
531 void attribute((unused)) *u) {
532 disorder_error(0, "error parsing reply: %s", msg);
533}
534
535/** @brief Get currently playing track
536 * @param c Client
537 * @param qp Where to store track information
538 * @return 0 on success, non-0 on error
539 *
540 * @p qp gets NULL if no track is playing.
541 */
542int disorder_playing(disorder_client *c, struct queue_entry **qp) {
543 char *r;
544 struct queue_entry *q;
545 int rc;
546
547 if((rc = disorder_simple(c, &r, "playing", (char *)0)))
548 return rc;
549 if(r) {
550 q = xmalloc(sizeof *q);
551 if(queue_unmarshall(q, r, client_error, 0))
552 return -1;
553 *qp = q;
554 } else
555 *qp = 0;
556 return 0;
557}
558
559/** @brief Fetch the queue, recent list, etc */
560static int disorder_somequeue(disorder_client *c,
561 const char *cmd, struct queue_entry **qp) {
562 struct queue_entry *qh, **qt = &qh, *q;
563 char *l;
564 int rc;
565
566 if((rc = disorder_simple(c, 0, cmd, (char *)0)))
567 return rc;
568 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
569 if(!strcmp(l, ".")) {
570 *qt = 0;
571 *qp = qh;
572 xfree(l);
573 return 0;
574 }
575 q = xmalloc(sizeof *q);
576 if(!queue_unmarshall(q, l, client_error, 0)) {
577 *qt = q;
578 qt = &q->next;
579 }
580 xfree(l);
581 }
582 if(ferror(c->fpin)) {
583 byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
584 disorder_error(errno, "error reading %s", c->ident);
585 } else {
586 c->last = "input error: unexpxected EOF";
587 disorder_error(0, "error reading %s: unexpected EOF", c->ident);
588 }
589 return -1;
590}
591
592/** @brief Read a dot-stuffed list
593 * @param c Client
594 * @param vecp Where to store list (UTF-8)
595 * @param nvecp Where to store number of items, or NULL
596 * @return 0 on success, non-0 on error
597 *
598 * The list will have a final NULL not counted in @p nvecp.
599 */
600static int readlist(disorder_client *c, char ***vecp, int *nvecp) {
601 char *l;
602 struct vector v;
603
604 vector_init(&v);
605 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
606 if(!strcmp(l, ".")) {
607 vector_terminate(&v);
608 if(nvecp)
609 *nvecp = v.nvec;
610 *vecp = v.vec;
611 xfree(l);
612 return 0;
613 }
614 vector_append(&v, xstrdup(l + (*l == '.')));
615 xfree(l);
616 }
617 if(ferror(c->fpin)) {
618 byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
619 disorder_error(errno, "error reading %s", c->ident);
620 } else {
621 c->last = "input error: unexpxected EOF";
622 disorder_error(0, "error reading %s: unexpected EOF", c->ident);
623 }
624 return -1;
625}
626
627/** @brief Issue a comamnd and get a list response
628 * @param c Client
629 * @param vecp Where to store list (UTF-8)
630 * @param nvecp Where to store number of items, or NULL
631 * @param cmd Command
632 * @return 0 on success, non-0 on error
633 *
634 * The remaining arguments are command arguments, terminated by (char
635 * *)0. They should be in UTF-8.
636 *
637 * 5xx responses count as errors.
638 *
639 * See disorder_simple().
640 */
641static int disorder_simple_list(disorder_client *c,
642 char ***vecp, int *nvecp,
643 const char *cmd, ...) {
644 va_list ap;
645 int ret;
646
647 va_start(ap, cmd);
648 ret = disorder_simple_v(c, 0, cmd, ap);
649 va_end(ap);
650 if(ret) return ret;
651 return readlist(c, vecp, nvecp);
652}
653
654/** @brief Return the user we logged in with
655 * @param c Client
656 * @return User name (owned by @p c, don't modify)
657 */
658char *disorder_user(disorder_client *c) {
659 return c->user;
660}
661
662static void pref_error_handler(const char *msg,
663 void attribute((unused)) *u) {
664 disorder_error(0, "error handling 'prefs' reply: %s", msg);
665}
666
667/** @brief Get all preferences for a trcak
668 * @param c Client
669 * @param track Track name
670 * @param kp Where to store linked list of preferences
671 * @return 0 on success, non-0 on error
672 */
673int disorder_prefs(disorder_client *c, const char *track, struct kvp **kp) {
674 char **vec, **pvec;
675 int nvec, npvec, n, rc;
676 struct kvp *k;
677
678 if((rc = disorder_simple_list(c, &vec, &nvec, "prefs", track, (char *)0)))
679 return rc;
680 for(n = 0; n < nvec; ++n) {
681 if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pref_error_handler, 0)))
682 return -1;
683 if(npvec != 2) {
684 pref_error_handler("malformed response", 0);
685 return -1;
686 }
687 *kp = k = xmalloc(sizeof *k);
688 k->name = pvec[0];
689 k->value = pvec[1];
690 kp = &k->next;
691 xfree(pvec);
692 }
693 free_strings(nvec, vec);
694 *kp = 0;
695 return 0;
696}
697
698/** @brief Parse a boolean response
699 * @param cmd Command for use in error messsage
700 * @param value Result from server
701 * @param flagp Where to store result
702 * @return 0 on success, non-0 on error
703 */
704static int boolean(const char *cmd, const char *value,
705 int *flagp) {
706 if(!strcmp(value, "yes")) *flagp = 1;
707 else if(!strcmp(value, "no")) *flagp = 0;
708 else {
709 disorder_error(0, "malformed response to '%s'", cmd);
710 return -1;
711 }
712 return 0;
713}
714
715/** @brief Set volume
716 * @param c Client
717 * @param left New left channel value
718 * @param right New right channel value
719 * @return 0 on success, non-0 on error
720 */
721int disorder_set_volume(disorder_client *c, int left, int right) {
722 char *ls, *rs;
723
724 if(byte_asprintf(&ls, "%d", left) < 0
725 || byte_asprintf(&rs, "%d", right) < 0)
726 return -1;
727 return disorder_simple(c, 0, "volume", ls, rs, (char *)0);
728}
729
730/** @brief Get volume
731 * @param c Client
732 * @param left Where to store left channel value
733 * @param right Where to store right channel value
734 * @return 0 on success, non-0 on error
735 */
736int disorder_get_volume(disorder_client *c, int *left, int *right) {
737 char *r;
738 int rc;
739
740 if((rc = disorder_simple(c, &r, "volume", (char *)0)))
741 return rc;
742 if(sscanf(r, "%d %d", left, right) != 2) {
743 c->last = "malformed volume response";
744 disorder_error(0, "error parsing response to 'volume': '%s'", r);
745 return -1;
746 }
747 return 0;
748}
749
750/** @brief Log to a sink
751 * @param c Client
752 * @param s Sink to write log lines to
753 * @return 0 on success, non-0 on error
754 */
755int disorder_log(disorder_client *c, struct sink *s) {
756 char *l;
757 int rc;
758
759 if((rc = disorder_simple(c, 0, "log", (char *)0)))
760 return rc;
761 while(inputline(c->ident, c->fpin, &l, '\n') >= 0 && strcmp(l, "."))
762 if(sink_printf(s, "%s\n", l) < 0) return -1;
763 if(ferror(c->fpin) || feof(c->fpin)) {
764 byte_xasprintf((char **)&c->last, "input error: %s",
765 ferror(c->fpin) ? strerror(errno) : "unexpxected EOF");
766 return -1;
767 }
768 return 0;
769}
770
771/** @brief Get recently added tracks
772 * @param c Client
773 * @param vecp Where to store pointer to list (UTF-8)
774 * @param nvecp Where to store count
775 * @param max Maximum tracks to fetch, or 0 for all available
776 * @return 0 on success, non-0 on error
777 */
778int disorder_new_tracks(disorder_client *c,
779 char ***vecp, int *nvecp,
780 int max) {
781 char limit[32];
782
783 sprintf(limit, "%d", max);
784 return disorder_simple_list(c, vecp, nvecp, "new", limit, (char *)0);
785}
786
787/** @brief Get server's RTP address information
788 * @param c Client
789 * @param addressp Where to store address (UTF-8)
790 * @param portp Where to store port (UTF-8)
791 * @return 0 on success, non-0 on error
792 */
793int disorder_rtp_address(disorder_client *c, char **addressp, char **portp) {
794 char *r;
795 int rc, n;
796 char **vec;
797
798 if((rc = disorder_simple(c, &r, "rtp-address", (char *)0)))
799 return rc;
800 vec = split(r, &n, SPLIT_QUOTES, 0, 0);
801 if(n != 2) {
802 c->last = "malformed RTP address";
803 disorder_error(0, "malformed rtp-address reply");
804 return -1;
805 }
806 *addressp = vec[0];
807 *portp = vec[1];
808 return 0;
809}
810
811/** @brief Get details of a scheduled event
812 * @param c Client
813 * @param id Event ID
814 * @param actiondatap Where to put details
815 * @return 0 on success, non-0 on error
816 */
817int disorder_schedule_get(disorder_client *c, const char *id,
818 struct kvp **actiondatap) {
819 char **lines, **bits;
820 int rc, nbits;
821
822 *actiondatap = 0;
823 if((rc = disorder_simple_list(c, &lines, NULL,
824 "schedule-get", id, (char *)0)))
825 return rc;
826 while(*lines) {
827 if(!(bits = split(*lines++, &nbits, SPLIT_QUOTES, 0, 0))) {
828 disorder_error(0, "invalid schedule-get reply: cannot split line");
829 return -1;
830 }
831 if(nbits != 2) {
832 disorder_error(0, "invalid schedule-get reply: wrong number of fields");
833 return -1;
834 }
835 kvp_set(actiondatap, bits[0], bits[1]);
836 }
837 return 0;
838}
839
840/** @brief Add a scheduled event
841 * @param c Client
842 * @param when When to trigger the event
843 * @param priority Event priority ("normal" or "junk")
844 * @param action What action to perform
845 * @param ... Action-specific arguments
846 * @return 0 on success, non-0 on error
847 *
848 * For action @c "play" the next argument is the track.
849 *
850 * For action @c "set-global" next argument is the global preference name
851 * and the final argument the value to set it to, or (char *)0 to unset it.
852 */
853int disorder_schedule_add(disorder_client *c,
854 time_t when,
855 const char *priority,
856 const char *action,
857 ...) {
858 va_list ap;
859 char when_str[64];
860 int rc;
861
862 snprintf(when_str, sizeof when_str, "%lld", (long long)when);
863 va_start(ap, action);
864 if(!strcmp(action, "play"))
865 rc = disorder_simple(c, 0, "schedule-add", when_str, priority,
866 action, va_arg(ap, char *),
867 (char *)0);
868 else if(!strcmp(action, "set-global")) {
869 const char *key = va_arg(ap, char *);
870 const char *value = va_arg(ap, char *);
871 rc = disorder_simple(c, 0,"schedule-add", when_str, priority,
872 action, key, value,
873 (char *)0);
874 } else
875 disorder_fatal(0, "unknown action '%s'", action);
876 va_end(ap);
877 return rc;
878}
879
880#include "client-stubs.c"
881
882/*
883Local Variables:
884c-basic-offset:2
885comment-column:40
886End:
887*/