2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2009 Richard Kettlewell
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.
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.
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/>.
18 /** @file lib/client.c
19 * @brief Simple C client
21 * See @ref lib/eclient.c for an asynchronous-capable client
27 #include <sys/types.h>
28 #include <sys/socket.h>
29 #include <netinet/in.h>
44 #include "inputline.h"
51 #include "client-common.h"
56 /** @brief Client handle contents */
57 struct disorder_client {
58 /** @brief Stream to read from */
60 /** @brief Stream to write to */
62 /** @brief Peer description */
64 /** @brief Username */
66 /** @brief Report errors to @c stderr */
68 /** @brief Last error string */
72 /** @brief Create a new client
73 * @param verbose If nonzero, write extra junk to stderr
74 * @return Pointer to new client
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.
80 disorder_client *disorder_new(int verbose) {
81 disorder_client *c = xmalloc(sizeof (struct disorder_client));
87 /** @brief Read a response line
89 * @param rp Where to store response, or NULL (UTF-8)
90 * @return Response code 0-999 or -1 on error
92 static int response(disorder_client *c, char **rp) {
95 if(inputline(c->ident, c->fpin, &r, '\n')) {
96 byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
99 D(("response: %s", r));
102 if(r[0] >= '0' && r[0] <= '9'
103 && r[1] >= '0' && r[1] <= '9'
104 && r[2] >= '0' && r[2] <= '9'
107 return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0';
109 c->last = "invalid reply format";
110 disorder_error(0, "invalid reply format from %s", c->ident);
115 /** @brief Return last response string
117 * @return Last response string (UTF-8, English) or NULL
119 const char *disorder_last(disorder_client *c) {
123 /** @brief Read and partially parse a response
125 * @param rp Where to store response text (or NULL) (UTF-8)
126 * @return 0 on success, non-0 on error
128 * 5xx responses count as errors.
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).
133 * NB that the response will NOT be converted to the local encoding.
135 static int check_response(disorder_client *c, char **rp) {
139 if((rc = response(c, &r)) == -1)
141 else if(rc / 100 == 2) {
143 *rp = (rc % 10 == 9) ? 0 : xstrdup(r + 4);
148 disorder_error(0, "from %s: %s", c->ident, utf82mb(r));
154 /** @brief Issue a command and parse a simple response
156 * @param rp Where to store result, or NULL
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
163 * 5xx responses count as errors.
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).
168 * NB that the response will NOT be converted to the local encoding
169 * nor will quotes be stripped. See dequote().
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.
175 * Usually you would call this via one of the following interfaces:
176 * - disorder_simple()
177 * - disorder_simple_body()
178 * - disorder_simple_list()
180 static int disorder_simple_v(disorder_client *c,
183 char **body, int nbody,
189 c->last = "not connected";
190 disorder_error(0, "not connected to server");
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));
200 dynstr_append(&d, '\n');
201 dynstr_terminate(&d);
202 D(("command: %s", d.vec));
203 if(fputs(d.vec, c->fpout) < 0)
208 for(nbody = 0; body[nbody]; ++nbody)
210 for(int n = 0; n < nbody; ++n) {
211 if(body[n][0] == '.')
212 if(fputc('.', c->fpout) < 0)
214 if(fputs(body[n], c->fpout) < 0)
216 if(fputc('\n', c->fpout) < 0)
219 if(fputs(".\n", c->fpout) < 0)
225 return check_response(c, rp);
227 byte_xasprintf((char **)&c->last, "write error: %s", strerror(errno));
228 disorder_error(errno, "error writing to %s", c->ident);
232 /** @brief Issue a command and parse a simple response
234 * @param rp Where to store result, or NULL (UTF-8)
236 * @return 0 on success, non-0 on error
238 * The remaining arguments are command arguments, terminated by (char
239 * *)0. They should be in UTF-8.
241 * 5xx responses count as errors.
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).
246 * NB that the response will NOT be converted to the local encoding
247 * nor will quotes be stripped. See dequote().
249 static int disorder_simple(disorder_client *c,
251 const char *cmd, ...) {
256 ret = disorder_simple_v(c, rp, cmd, 0, 0, ap);
261 /** @brief Issue a command with a body and parse a simple response
263 * @param rp Where to store result, or NULL (UTF-8)
264 * @param body Pointer to body
265 * @param nbody Size of body
267 * @return 0 on success, non-0 on error
269 * See disorder_simple().
271 static int disorder_simple_body(disorder_client *c,
273 char **body, int nbody,
274 const char *cmd, ...) {
279 ret = disorder_simple_v(c, rp, cmd, body, nbody, ap);
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)
289 * This is used as a wrapper around disorder_simple() to dequote
292 static int dequote(int rc, char **rp) {
296 if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) {
302 disorder_error(0, "invalid reply: %s", *rp);
307 /** @brief Generic connection routine
308 * @param conf Configuration to follow
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
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
319 int disorder_connect_generic(struct config *conf,
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;
328 char *r = NULL, **rvec = NULL;
329 const char *protocol, *algorithm, *challenge;
330 struct sockaddr *sa = NULL;
333 if((salen = find_server(conf, &sa, &c->ident)) == (socklen_t)-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");
341 if(connect(fd, sa, salen) < 0) {
342 byte_xasprintf((char **)&c->last, "connect: %s", strerror(errno));
343 disorder_error(errno, "error calling connect");
346 if((fd2 = dup(fd)) < 0) {
347 byte_xasprintf((char **)&c->last, "dup: %s", strerror(errno));
348 disorder_error(errno, "error calling dup");
351 if(!(c->fpin = fdopen(fd, "rb"))) {
352 byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno));
353 disorder_error(errno, "error calling fdopen");
357 if(!(c->fpout = fdopen(fd2, "wb"))) {
358 byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno));
359 disorder_error(errno, "error calling fdopen");
363 if((rc = disorder_simple(c, &r, 0, (const char *)0)))
365 if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
368 c->last = "cannot parse server greeting";
369 disorder_error(0, "cannot parse server greeting %s", r);
373 if(strcmp(protocol, "2")) {
374 c->last = "unknown protocol version";
375 disorder_error(0, "unknown protocol version: %s", protocol);
380 if(!(nonce = unhex(challenge, &nl)))
383 if(!dequote(disorder_simple(c, &c->user, "cookie", cookie, (char *)0),
385 return 0; /* success */
387 c->last = "cookie failed and no username";
388 disorder_error(0, "cookie did not work and no username available");
392 if(!(res = authhash(nonce, nl, password, algorithm))) {
393 c->last = "error computing authorization hash";
396 if((rc = disorder_simple(c, 0, "user", username, res, (char *)0)))
398 c->user = xstrdup(username);
400 free_strings(nrvec, rvec);
416 if(fd2 != -1) close(fd2);
417 if(fd != -1) close(fd);
421 /** @brief Connect a client with a specified username and password
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
427 int disorder_connect_user(disorder_client *c,
428 const char *username,
429 const char *password) {
430 return disorder_connect_generic(config,
437 /** @brief Connect a client
439 * @return 0 on success, non-0 on error
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).
445 int disorder_connect(disorder_client *c) {
446 const char *username, *password;
448 if(!(username = config->username)) {
449 c->last = "no username";
450 disorder_error(0, "no username configured");
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);
463 c->last = "no password";
464 disorder_error(0, "no password configured for user '%s'", username);
467 return disorder_connect_generic(config,
474 /** @brief Connect a client
476 * @param cookie Cookie to log in with, or NULL
477 * @return 0 on success, non-0 on error
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).
483 int disorder_connect_cookie(disorder_client *c,
484 const char *cookie) {
485 return disorder_connect_generic(config,
492 /** @brief Close a client
494 * @return 0 on succcess, non-0 on errior
496 * The client is still closed even on error. It might well be
497 * appropriate to ignore the return value.
499 int disorder_close(disorder_client *c) {
503 if(fclose(c->fpin) < 0) {
504 byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno));
505 disorder_error(errno, "error calling fclose");
511 if(fclose(c->fpout) < 0) {
512 byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno));
513 disorder_error(errno, "error calling fclose");
525 /** @brief Play a track
527 * @param track Track to play (UTF-8)
528 * @return 0 on success, non-0 on error
530 int disorder_play(disorder_client *c, const char *track) {
531 return disorder_simple(c, 0, "play", track, (char *)0);
534 /** @brief Remove a track
536 * @param track Track to remove (UTF-8)
537 * @return 0 on success, non-0 on error
539 int disorder_remove(disorder_client *c, const char *track) {
540 return disorder_simple(c, 0, "remove", track, (char *)0);
543 /** @brief Move a track
545 * @param track Track to move (UTF-8)
546 * @param delta Distance to move by
547 * @return 0 on success, non-0 on error
549 int disorder_move(disorder_client *c, const char *track, int delta) {
552 byte_snprintf(d, sizeof d, "%d", delta);
553 return disorder_simple(c, 0, "move", track, d, (char *)0);
556 /** @brief Enable play
558 * @return 0 on success, non-0 on error
560 int disorder_enable(disorder_client *c) {
561 return disorder_simple(c, 0, "enable", (char *)0);
564 /** @brief Disable play
566 * @return 0 on success, non-0 on error
568 int disorder_disable(disorder_client *c) {
569 return disorder_simple(c, 0, "disable", (char *)0);
572 /** @brief Scratch the currently playing track
573 * @param id Playing track ID or NULL (UTF-8)
575 * @return 0 on success, non-0 on error
577 int disorder_scratch(disorder_client *c, const char *id) {
578 return disorder_simple(c, 0, "scratch", id, (char *)0);
581 /** @brief Shut down the server
583 * @return 0 on success, non-0 on error
585 int disorder_shutdown(disorder_client *c) {
586 return disorder_simple(c, 0, "shutdown", (char *)0);
589 /** @brief Make the server re-read its configuration
591 * @return 0 on success, non-0 on error
593 int disorder_reconfigure(disorder_client *c) {
594 return disorder_simple(c, 0, "reconfigure", (char *)0);
597 /** @brief Rescan tracks
599 * @return 0 on success, non-0 on error
601 int disorder_rescan(disorder_client *c) {
602 return disorder_simple(c, 0, "rescan", (char *)0);
605 /** @brief Get server version number
607 * @param rp Where to store version string (UTF-8)
608 * @return 0 on success, non-0 on error
610 int disorder_version(disorder_client *c, char **rp) {
611 return dequote(disorder_simple(c, rp, "version", (char *)0), rp);
614 static void client_error(const char *msg,
615 void attribute((unused)) *u) {
616 disorder_error(0, "error parsing reply: %s", msg);
619 /** @brief Get currently playing track
621 * @param qp Where to store track information
622 * @return 0 on success, non-0 on error
624 * @p qp gets NULL if no track is playing.
626 int disorder_playing(disorder_client *c, struct queue_entry **qp) {
628 struct queue_entry *q;
631 if((rc = disorder_simple(c, &r, "playing", (char *)0)))
634 q = xmalloc(sizeof *q);
635 if(queue_unmarshall(q, r, client_error, 0))
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;
650 if((rc = disorder_simple(c, 0, cmd, (char *)0)))
652 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
653 if(!strcmp(l, ".")) {
659 q = xmalloc(sizeof *q);
660 if(!queue_unmarshall(q, l, client_error, 0)) {
666 if(ferror(c->fpin)) {
667 byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
668 disorder_error(errno, "error reading %s", c->ident);
670 c->last = "input error: unexpxected EOF";
671 disorder_error(0, "error reading %s: unexpected EOF", c->ident);
676 /** @brief Get recently played tracks
678 * @param qp Where to store track information
679 * @return 0 on success, non-0 on error
681 * The last entry in the list is the most recently played track.
683 int disorder_recent(disorder_client *c, struct queue_entry **qp) {
684 return disorder_somequeue(c, "recent", qp);
689 * @param qp Where to store track information
690 * @return 0 on success, non-0 on error
692 * The first entry in the list will be played next.
694 int disorder_queue(disorder_client *c, struct queue_entry **qp) {
695 return disorder_somequeue(c, "queue", qp);
698 /** @brief Read a dot-stuffed list
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
704 * The list will have a final NULL not counted in @p nvecp.
706 static int readlist(disorder_client *c, char ***vecp, int *nvecp) {
711 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
712 if(!strcmp(l, ".")) {
713 vector_terminate(&v);
720 vector_append(&v, xstrdup(l + (*l == '.')));
723 if(ferror(c->fpin)) {
724 byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
725 disorder_error(errno, "error reading %s", c->ident);
727 c->last = "input error: unexpxected EOF";
728 disorder_error(0, "error reading %s: unexpected EOF", c->ident);
733 /** @brief Issue a comamnd and get a list response
735 * @param vecp Where to store list (UTF-8)
736 * @param nvecp Where to store number of items, or NULL
738 * @return 0 on success, non-0 on error
740 * The remaining arguments are command arguments, terminated by (char
741 * *)0. They should be in UTF-8.
743 * 5xx responses count as errors.
745 * See disorder_simple().
747 static int disorder_simple_list(disorder_client *c,
748 char ***vecp, int *nvecp,
749 const char *cmd, ...) {
754 ret = disorder_simple_v(c, 0, cmd, 0, 0, ap);
757 return readlist(c, vecp, nvecp);
760 /** @brief List directories below @p dir
762 * @param dir Directory to list, or NULL for root (UTF-8)
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
768 int 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);
773 /** @brief List files below @p dir
775 * @param dir Directory to list, or NULL for root (UTF-8)
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
781 int 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);
786 /** @brief List files and directories below @p dir
788 * @param dir Directory to list, or NULL for root (UTF-8)
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
794 int 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);
799 /** @brief Return the user we logged in with
801 * @return User name (owned by @p c, don't modify)
803 char *disorder_user(disorder_client *c) {
807 /** @brief Set a track preference
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
814 int 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);
819 /** @brief Unset a track preference
821 * @param track Track name (UTF-8)
822 * @param key Preference name (UTF-8)
823 * @return 0 on success, non-0 on error
825 int disorder_unset(disorder_client *c, const char *track,
827 return disorder_simple(c, 0, "unset", track, key, (char *)0);
830 /** @brief Get a track preference
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
837 int disorder_get(disorder_client *c,
838 const char *track, const char *key, char **valuep) {
839 return dequote(disorder_simple(c, valuep, "get", track, key, (char *)0),
843 static void pref_error_handler(const char *msg,
844 void attribute((unused)) *u) {
845 disorder_error(0, "error handling 'prefs' reply: %s", msg);
848 /** @brief Get all preferences for a trcak
850 * @param track Track name
851 * @param kp Where to store linked list of preferences
852 * @return 0 on success, non-0 on error
854 int disorder_prefs(disorder_client *c, const char *track, struct kvp **kp) {
856 int nvec, npvec, n, rc;
859 if((rc = disorder_simple_list(c, &vec, &nvec, "prefs", track, (char *)0)))
861 for(n = 0; n < nvec; ++n) {
862 if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pref_error_handler, 0)))
865 pref_error_handler("malformed response", 0);
868 *kp = k = xmalloc(sizeof *k);
877 /** @brief Parse a boolean response
878 * @param cmd Command for use in error messsage
879 * @param value Result from server
880 * @param flagp Where to store result
881 * @return 0 on success, non-0 on error
883 static int boolean(const char *cmd, const char *value,
885 if(!strcmp(value, "yes")) *flagp = 1;
886 else if(!strcmp(value, "no")) *flagp = 0;
888 disorder_error(0, "malformed response to '%s'", cmd);
894 /** @brief Test whether a track exists
896 * @param track Track name (UTF-8)
897 * @param existsp Where to store result (non-0 iff does exist)
898 * @return 0 on success, non-0 on error
900 int disorder_exists(disorder_client *c, const char *track, int *existsp) {
904 if((rc = disorder_simple(c, &v, "exists", track, (char *)0)))
906 return boolean("exists", v, existsp);
909 /** @brief Test whether playing is enabled
911 * @param enabledp Where to store result (non-0 iff enabled)
912 * @return 0 on success, non-0 on error
914 int disorder_enabled(disorder_client *c, int *enabledp) {
918 if((rc = disorder_simple(c, &v, "enabled", (char *)0)))
920 return boolean("enabled", v, enabledp);
923 /** @brief Get the length of a track
925 * @param track Track name (UTF-8)
926 * @param valuep Where to store length in seconds
927 * @return 0 on success, non-0 on error
929 * If the length is unknown 0 is returned.
931 int disorder_length(disorder_client *c, const char *track,
936 if((rc = disorder_simple(c, &value, "length", track, (char *)0)))
938 *valuep = atol(value);
942 /** @brief Search for tracks
944 * @param terms Search terms (UTF-8)
945 * @param vecp Where to store list (UTF-8)
946 * @param nvecp Where to store number of items, or NULL
947 * @return 0 on success, non-0 on error
949 int disorder_search(disorder_client *c, const char *terms,
950 char ***vecp, int *nvecp) {
951 return disorder_simple_list(c, vecp, nvecp, "search", terms, (char *)0);
954 /** @brief Enable random play
956 * @return 0 on success, non-0 on error
958 int disorder_random_enable(disorder_client *c) {
959 return disorder_simple(c, 0, "random-enable", (char *)0);
962 /** @brief Disable random play
964 * @return 0 on success, non-0 on error
966 int disorder_random_disable(disorder_client *c) {
967 return disorder_simple(c, 0, "random-disable", (char *)0);
970 /** @brief Test whether random play is enabled
972 * @param enabledp Where to store result (non-0 iff enabled)
973 * @return 0 on success, non-0 on error
975 int disorder_random_enabled(disorder_client *c, int *enabledp) {
979 if((rc = disorder_simple(c, &v, "random-enabled", (char *)0)))
981 return boolean("random-enabled", v, enabledp);
984 /** @brief Get server stats
986 * @param vecp Where to store list (UTF-8)
987 * @param nvecp Where to store number of items, or NULL
988 * @return 0 on success, non-0 on error
990 int disorder_stats(disorder_client *c,
991 char ***vecp, int *nvecp) {
992 return disorder_simple_list(c, vecp, nvecp, "stats", (char *)0);
995 /** @brief Set volume
997 * @param left New left channel value
998 * @param right New right channel value
999 * @return 0 on success, non-0 on error
1001 int disorder_set_volume(disorder_client *c, int left, int right) {
1004 if(byte_asprintf(&ls, "%d", left) < 0
1005 || byte_asprintf(&rs, "%d", right) < 0)
1007 return disorder_simple(c, 0, "volume", ls, rs, (char *)0);
1010 /** @brief Get volume
1012 * @param left Where to store left channel value
1013 * @param right Where to store right channel value
1014 * @return 0 on success, non-0 on error
1016 int disorder_get_volume(disorder_client *c, int *left, int *right) {
1020 if((rc = disorder_simple(c, &r, "volume", (char *)0)))
1022 if(sscanf(r, "%d %d", left, right) != 2) {
1023 c->last = "malformed volume response";
1024 disorder_error(0, "error parsing response to 'volume': '%s'", r);
1030 /** @brief Log to a sink
1032 * @param s Sink to write log lines to
1033 * @return 0 on success, non-0 on error
1035 int disorder_log(disorder_client *c, struct sink *s) {
1039 if((rc = disorder_simple(c, 0, "log", (char *)0)))
1041 while(inputline(c->ident, c->fpin, &l, '\n') >= 0 && strcmp(l, "."))
1042 if(sink_printf(s, "%s\n", l) < 0) return -1;
1043 if(ferror(c->fpin) || feof(c->fpin)) {
1044 byte_xasprintf((char **)&c->last, "input error: %s",
1045 ferror(c->fpin) ? strerror(errno) : "unexpxected EOF");
1051 /** @brief Look up a track name part
1053 * @param partp Where to store result (UTF-8)
1054 * @param track Track name (UTF-8)
1055 * @param context Context (usually "sort" or "display") (UTF-8)
1056 * @param part Track part (UTF-8)
1057 * @return 0 on success, non-0 on error
1059 int disorder_part(disorder_client *c, char **partp,
1060 const char *track, const char *context, const char *part) {
1061 return dequote(disorder_simple(c, partp, "part",
1062 track, context, part, (char *)0), partp);
1065 /** @brief Resolve aliases
1067 * @param trackp Where to store canonical name (UTF-8)
1068 * @param track Track name (UTF-8)
1069 * @return 0 on success, non-0 on error
1071 int disorder_resolve(disorder_client *c, char **trackp, const char *track) {
1072 return dequote(disorder_simple(c, trackp, "resolve", track, (char *)0),
1076 /** @brief Pause the current track
1078 * @return 0 on success, non-0 on error
1080 int disorder_pause(disorder_client *c) {
1081 return disorder_simple(c, 0, "pause", (char *)0);
1084 /** @brief Resume the current track
1086 * @return 0 on success, non-0 on error
1088 int disorder_resume(disorder_client *c) {
1089 return disorder_simple(c, 0, "resume", (char *)0);
1092 /** @brief List all known tags
1094 * @param vecp Where to store list (UTF-8)
1095 * @param nvecp Where to store number of items, or NULL
1096 * @return 0 on success, non-0 on error
1098 int disorder_tags(disorder_client *c,
1099 char ***vecp, int *nvecp) {
1100 return disorder_simple_list(c, vecp, nvecp, "tags", (char *)0);
1103 /** @brief List all known users
1105 * @param vecp Where to store list (UTF-8)
1106 * @param nvecp Where to store number of items, or NULL
1107 * @return 0 on success, non-0 on error
1109 int disorder_users(disorder_client *c,
1110 char ***vecp, int *nvecp) {
1111 return disorder_simple_list(c, vecp, nvecp, "users", (char *)0);
1114 /** @brief Get recently added tracks
1116 * @param vecp Where to store pointer to list (UTF-8)
1117 * @param nvecp Where to store count
1118 * @param max Maximum tracks to fetch, or 0 for all available
1119 * @return 0 on success, non-0 on error
1121 int disorder_new_tracks(disorder_client *c,
1122 char ***vecp, int *nvecp,
1126 sprintf(limit, "%d", max);
1127 return disorder_simple_list(c, vecp, nvecp, "new", limit, (char *)0);
1130 /** @brief Set a global preference
1132 * @param key Preference name (UTF-8)
1133 * @param value Preference value (UTF-8)
1134 * @return 0 on success, non-0 on error
1136 int disorder_set_global(disorder_client *c,
1137 const char *key, const char *value) {
1138 return disorder_simple(c, 0, "set-global", key, value, (char *)0);
1141 /** @brief Unset a global preference
1143 * @param key Preference name (UTF-8)
1144 * @return 0 on success, non-0 on error
1146 int disorder_unset_global(disorder_client *c, const char *key) {
1147 return disorder_simple(c, 0, "unset-global", key, (char *)0);
1150 /** @brief Get a global preference
1152 * @param key Preference name (UTF-8)
1153 * @param valuep Where to store preference value (UTF-8)
1154 * @return 0 on success, non-0 on error
1156 int disorder_get_global(disorder_client *c, const char *key, char **valuep) {
1157 return dequote(disorder_simple(c, valuep, "get-global", key, (char *)0),
1161 /** @brief Get server's RTP address information
1163 * @param addressp Where to store address (UTF-8)
1164 * @param portp Where to store port (UTF-8)
1165 * @return 0 on success, non-0 on error
1167 int disorder_rtp_address(disorder_client *c, char **addressp, char **portp) {
1172 if((rc = disorder_simple(c, &r, "rtp-address", (char *)0)))
1174 vec = split(r, &n, SPLIT_QUOTES, 0, 0);
1176 c->last = "malformed RTP address";
1177 disorder_error(0, "malformed rtp-address reply");
1185 /** @brief Create a user
1187 * @param user Username
1188 * @param password Password
1189 * @param rights Initial rights or NULL to use default
1190 * @return 0 on success, non-0 on error
1192 int disorder_adduser(disorder_client *c,
1193 const char *user, const char *password,
1194 const char *rights) {
1195 return disorder_simple(c, 0, "adduser", user, password, rights, (char *)0);
1198 /** @brief Delete a user
1200 * @param user Username
1201 * @return 0 on success, non-0 on error
1203 int disorder_deluser(disorder_client *c, const char *user) {
1204 return disorder_simple(c, 0, "deluser", user, (char *)0);
1207 /** @brief Get user information
1209 * @param user Username
1210 * @param key Property name (UTF-8)
1211 * @param valuep Where to store value (UTF-8)
1212 * @return 0 on success, non-0 on error
1214 int disorder_userinfo(disorder_client *c, const char *user, const char *key,
1216 return dequote(disorder_simple(c, valuep, "userinfo", user, key, (char *)0),
1220 /** @brief Set user information
1222 * @param user Username
1223 * @param key Property name (UTF-8)
1224 * @param value New property value (UTF-8)
1225 * @return 0 on success, non-0 on error
1227 int disorder_edituser(disorder_client *c, const char *user,
1228 const char *key, const char *value) {
1229 return disorder_simple(c, 0, "edituser", user, key, value, (char *)0);
1232 /** @brief Register a user
1234 * @param user Username
1235 * @param password Password
1236 * @param email Email address (UTF-8)
1237 * @param confirmp Where to store confirmation string
1238 * @return 0 on success, non-0 on error
1240 int disorder_register(disorder_client *c, const char *user,
1241 const char *password, const char *email,
1243 return dequote(disorder_simple(c, confirmp, "register",
1244 user, password, email, (char *)0),
1248 /** @brief Confirm a user
1250 * @param confirm Confirmation string
1251 * @return 0 on success, non-0 on error
1253 int disorder_confirm(disorder_client *c, const char *confirm) {
1257 if(!(rc = dequote(disorder_simple(c, &u, "confirm", confirm, (char *)0),
1263 /** @brief Make a cookie for this login
1265 * @param cookiep Where to store cookie string
1266 * @return 0 on success, non-0 on error
1268 int disorder_make_cookie(disorder_client *c, char **cookiep) {
1269 return dequote(disorder_simple(c, cookiep, "make-cookie", (char *)0),
1273 /** @brief Revoke the cookie used by this session
1275 * @return 0 on success, non-0 on error
1277 int disorder_revoke(disorder_client *c) {
1278 return disorder_simple(c, 0, "revoke", (char *)0);
1281 /** @brief Request a password reminder email
1283 * @param user Username
1284 * @return 0 on success, non-0 on error
1286 int disorder_reminder(disorder_client *c, const char *user) {
1287 return disorder_simple(c, 0, "reminder", user, (char *)0);
1290 /** @brief List scheduled events
1292 * @param idsp Where to put list of event IDs
1293 * @param nidsp Where to put count of event IDs, or NULL
1294 * @return 0 on success, non-0 on error
1296 int disorder_schedule_list(disorder_client *c, char ***idsp, int *nidsp) {
1297 return disorder_simple_list(c, idsp, nidsp, "schedule-list", (char *)0);
1300 /** @brief Delete a scheduled event
1302 * @param id Event ID to delete
1303 * @return 0 on success, non-0 on error
1305 int disorder_schedule_del(disorder_client *c, const char *id) {
1306 return disorder_simple(c, 0, "schedule-del", id, (char *)0);
1309 /** @brief Get details of a scheduled event
1311 * @param id Event ID
1312 * @param actiondatap Where to put details
1313 * @return 0 on success, non-0 on error
1315 int disorder_schedule_get(disorder_client *c, const char *id,
1316 struct kvp **actiondatap) {
1317 char **lines, **bits;
1321 if((rc = disorder_simple_list(c, &lines, NULL,
1322 "schedule-get", id, (char *)0)))
1325 if(!(bits = split(*lines++, &nbits, SPLIT_QUOTES, 0, 0))) {
1326 disorder_error(0, "invalid schedule-get reply: cannot split line");
1330 disorder_error(0, "invalid schedule-get reply: wrong number of fields");
1333 kvp_set(actiondatap, bits[0], bits[1]);
1338 /** @brief Add a scheduled event
1340 * @param when When to trigger the event
1341 * @param priority Event priority ("normal" or "junk")
1342 * @param action What action to perform
1343 * @param ... Action-specific arguments
1344 * @return 0 on success, non-0 on error
1346 * For action @c "play" the next argument is the track.
1348 * For action @c "set-global" next argument is the global preference name
1349 * and the final argument the value to set it to, or (char *)0 to unset it.
1351 int disorder_schedule_add(disorder_client *c,
1353 const char *priority,
1360 snprintf(when_str, sizeof when_str, "%lld", (long long)when);
1361 va_start(ap, action);
1362 if(!strcmp(action, "play"))
1363 rc = disorder_simple(c, 0, "schedule-add", when_str, priority,
1364 action, va_arg(ap, char *),
1366 else if(!strcmp(action, "set-global")) {
1367 const char *key = va_arg(ap, char *);
1368 const char *value = va_arg(ap, char *);
1369 rc = disorder_simple(c, 0,"schedule-add", when_str, priority,
1373 disorder_fatal(0, "unknown action '%s'", action);
1378 /** @brief Adopt a track
1380 * @param id Track ID to adopt
1381 * @return 0 on success, non-0 on error
1383 int disorder_adopt(disorder_client *c, const char *id) {
1384 return disorder_simple(c, 0, "adopt", id, (char *)0);
1387 /** @brief Delete a playlist
1389 * @param playlist Playlist to delete
1390 * @return 0 on success, non-0 on error
1392 int disorder_playlist_delete(disorder_client *c,
1393 const char *playlist) {
1394 return disorder_simple(c, 0, "playlist-delete", playlist, (char *)0);
1397 /** @brief Get the contents of a playlist
1399 * @param playlist Playlist to get
1400 * @param tracksp Where to put list of tracks
1401 * @param ntracksp Where to put count of tracks
1402 * @return 0 on success, non-0 on error
1404 int disorder_playlist_get(disorder_client *c, const char *playlist,
1405 char ***tracksp, int *ntracksp) {
1406 return disorder_simple_list(c, tracksp, ntracksp,
1407 "playlist-get", playlist, (char *)0);
1410 /** @brief List all readable playlists
1412 * @param playlistsp Where to put list of playlists
1413 * @param nplaylistsp Where to put count of playlists
1414 * @return 0 on success, non-0 on error
1416 int disorder_playlists(disorder_client *c,
1417 char ***playlistsp, int *nplaylistsp) {
1418 return disorder_simple_list(c, playlistsp, nplaylistsp,
1419 "playlists", (char *)0);
1422 /** @brief Get the sharing status of a playlist
1424 * @param playlist Playlist to inspect
1425 * @param sharep Where to put sharing status
1426 * @return 0 on success, non-0 on error
1428 * Possible @p sharep values are @c public, @c private and @c shared.
1430 int disorder_playlist_get_share(disorder_client *c, const char *playlist,
1432 return disorder_simple(c, sharep,
1433 "playlist-get-share", playlist, (char *)0);
1436 /** @brief Get the sharing status of a playlist
1438 * @param playlist Playlist to modify
1439 * @param share New sharing status
1440 * @return 0 on success, non-0 on error
1442 * Possible @p share values are @c public, @c private and @c shared.
1444 int disorder_playlist_set_share(disorder_client *c, const char *playlist,
1445 const char *share) {
1446 return disorder_simple(c, 0,
1447 "playlist-set-share", playlist, share, (char *)0);
1450 /** @brief Lock a playlist for modifications
1452 * @param playlist Playlist to lock
1453 * @return 0 on success, non-0 on error
1455 int disorder_playlist_lock(disorder_client *c, const char *playlist) {
1456 return disorder_simple(c, 0,
1457 "playlist-lock", playlist, (char *)0);
1460 /** @brief Unlock the locked playlist
1462 * @return 0 on success, non-0 on error
1464 int disorder_playlist_unlock(disorder_client *c) {
1465 return disorder_simple(c, 0,
1466 "playlist-unlock", (char *)0);
1469 /** @brief Set the contents of a playlst
1471 * @param playlist Playlist to modify
1472 * @param tracks List of tracks
1473 * @param ntracks Length of @p tracks (or -1 to count up to the first NULL)
1474 * @return 0 on success, non-0 on error
1476 int disorder_playlist_set(disorder_client *c,
1477 const char *playlist,
1480 return disorder_simple_body(c, 0, tracks, ntracks,
1481 "playlist-set", playlist, (char *)0);