2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 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 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);
147 error(0, "from %s: %s", c->ident, utf82mb(r));
152 /** @brief Issue a command and parse a simple response
154 * @param rp Where to store result, or NULL
156 * @param body Body or NULL
157 * @param nbody Length of body or -1
158 * @param ap Arguments (UTF-8), terminated by (char *)0
159 * @return 0 on success, non-0 on error
161 * 5xx responses count as errors.
163 * @p rp will NOT be filled in for xx9 responses (where it is just
164 * commentary for a command where it would normally be meaningful).
166 * NB that the response will NOT be converted to the local encoding
167 * nor will quotes be stripped. See dequote().
169 * If @p body is not NULL then the body is sent immediately after the
170 * command. @p nbody should be the number of lines or @c -1 to count
171 * them if @p body is NULL-terminated.
173 * Usually you would call this via one of the following interfaces:
174 * - disorder_simple()
175 * - disorder_simple_body()
176 * - disorder_simple_list()
178 static int disorder_simple_v(disorder_client *c,
181 char **body, int nbody,
187 c->last = "not connected";
188 error(0, "not connected to server");
193 dynstr_append_string(&d, cmd);
194 while((arg = va_arg(ap, const char *))) {
195 dynstr_append(&d, ' ');
196 dynstr_append_string(&d, quoteutf8(arg));
198 dynstr_append(&d, '\n');
199 dynstr_terminate(&d);
200 D(("command: %s", d.vec));
201 if(fputs(d.vec, c->fpout) < 0)
205 for(nbody = 0; body[nbody]; ++nbody)
207 for(int n = 0; n < nbody; ++n) {
208 if(body[n][0] == '.')
209 if(fputc('.', c->fpout) < 0)
211 if(fputs(body[n], c->fpout) < 0)
213 if(fputc('\n', c->fpout) < 0)
216 if(fputs(".\n", c->fpout) < 0)
222 return check_response(c, rp);
224 byte_xasprintf((char **)&c->last, "write error: %s", strerror(errno));
225 error(errno, "error writing to %s", c->ident);
229 /** @brief Issue a command and parse a simple response
231 * @param rp Where to store result, or NULL (UTF-8)
233 * @return 0 on success, non-0 on error
235 * The remaining arguments are command arguments, terminated by (char
236 * *)0. They should be in UTF-8.
238 * 5xx responses count as errors.
240 * @p rp will NOT be filled in for xx9 responses (where it is just
241 * commentary for a command where it would normally be meaningful).
243 * NB that the response will NOT be converted to the local encoding
244 * nor will quotes be stripped. See dequote().
246 static int disorder_simple(disorder_client *c,
248 const char *cmd, ...) {
253 ret = disorder_simple_v(c, rp, cmd, 0, 0, ap);
258 /** @brief Issue a command with a body and parse a simple response
260 * @param rp Where to store result, or NULL (UTF-8)
261 * @param body Pointer to body
262 * @param nbody Size of body
264 * @return 0 on success, non-0 on error
266 * See disorder_simple().
268 static int disorder_simple_body(disorder_client *c,
270 char **body, int nbody,
271 const char *cmd, ...) {
276 ret = disorder_simple_v(c, rp, cmd, body, nbody, ap);
281 /** @brief Dequote a result string
282 * @param rc 0 on success, non-0 on error
283 * @param rp Where result string is stored (UTF-8)
286 * This is used as a wrapper around disorder_simple() to dequote
289 static int dequote(int rc, char **rp) {
293 if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) {
297 error(0, "invalid reply: %s", *rp);
302 /** @brief Generic connection routine
303 * @param conf Configuration to follow
305 * @param username Username to log in with or NULL
306 * @param password Password to log in with or NULL
307 * @param cookie Cookie to log in with or NULL
308 * @return 0 on success, non-0 on error
310 * @p cookie is tried first if not NULL. If it is NULL then @p
311 * username must not be. If @p username is not NULL then nor may @p
314 int disorder_connect_generic(struct config *conf,
316 const char *username,
317 const char *password,
318 const char *cookie) {
319 int fd = -1, fd2 = -1, nrvec, rc;
320 unsigned char *nonce;
324 const char *protocol, *algorithm, *challenge;
328 if((salen = find_server(conf, &sa, &c->ident)) == (socklen_t)-1)
330 c->fpin = c->fpout = 0;
331 if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
332 byte_xasprintf((char **)&c->last, "socket: %s", strerror(errno));
333 error(errno, "error calling socket");
336 if(connect(fd, sa, salen) < 0) {
337 byte_xasprintf((char **)&c->last, "connect: %s", strerror(errno));
338 error(errno, "error calling connect");
341 if((fd2 = dup(fd)) < 0) {
342 byte_xasprintf((char **)&c->last, "dup: %s", strerror(errno));
343 error(errno, "error calling dup");
346 if(!(c->fpin = fdopen(fd, "rb"))) {
347 byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno));
348 error(errno, "error calling fdopen");
352 if(!(c->fpout = fdopen(fd2, "wb"))) {
353 byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno));
354 error(errno, "error calling fdopen");
358 if((rc = disorder_simple(c, &r, 0, (const char *)0)))
360 if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
363 c->last = "cannot parse server greeting";
364 error(0, "cannot parse server greeting %s", r);
368 if(strcmp(protocol, "2")) {
369 c->last = "unknown protocol version";
370 error(0, "unknown protocol version: %s", protocol);
375 if(!(nonce = unhex(challenge, &nl)))
378 if(!dequote(disorder_simple(c, &c->user, "cookie", cookie, (char *)0),
380 return 0; /* success */
382 c->last = "cookie failed and no username";
383 error(0, "cookie did not work and no username available");
387 if(!(res = authhash(nonce, nl, password, algorithm))) {
388 c->last = "error computing authorization hash";
391 if((rc = disorder_simple(c, 0, "user", username, res, (char *)0)))
393 c->user = xstrdup(username);
406 if(fd2 != -1) close(fd2);
407 if(fd != -1) close(fd);
411 /** @brief Connect a client with a specified username and password
413 * @param username Username to log in with
414 * @param password Password to log in with
415 * @return 0 on success, non-0 on error
417 int disorder_connect_user(disorder_client *c,
418 const char *username,
419 const char *password) {
420 return disorder_connect_generic(config,
427 /** @brief Connect a client
429 * @return 0 on success, non-0 on error
431 * The connection will use the username and password found in @ref
432 * config, or directly from the database if no password is found and
433 * the database is readable (usually only for root).
435 int disorder_connect(disorder_client *c) {
436 const char *username, *password;
438 if(!(username = config->username)) {
439 c->last = "no username";
440 error(0, "no username configured");
443 password = config->password;
444 /* Maybe we can read the database */
445 if(!password && trackdb_readable()) {
446 trackdb_init(TRACKDB_NO_RECOVER|TRACKDB_NO_UPGRADE);
447 trackdb_open(TRACKDB_READ_ONLY);
448 password = trackdb_get_password(username);
453 c->last = "no password";
454 error(0, "no password configured");
457 return disorder_connect_generic(config,
464 /** @brief Connect a client
466 * @param cookie Cookie to log in with, or NULL
467 * @return 0 on success, non-0 on error
469 * If @p cookie is NULL or does not work then we attempt to log in as
470 * guest instead (so when the cookie expires only an extra round trip
471 * is needed rathre than a complete new login).
473 int disorder_connect_cookie(disorder_client *c,
474 const char *cookie) {
475 return disorder_connect_generic(config,
482 /** @brief Close a client
484 * @return 0 on succcess, non-0 on errior
486 * The client is still closed even on error. It might well be
487 * appropriate to ignore the return value.
489 int disorder_close(disorder_client *c) {
493 if(fclose(c->fpin) < 0) {
494 byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno));
495 error(errno, "error calling fclose");
501 if(fclose(c->fpout) < 0) {
502 byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno));
503 error(errno, "error calling fclose");
513 /** @brief Play a track
515 * @param track Track to play (UTF-8)
516 * @return 0 on success, non-0 on error
518 int disorder_play(disorder_client *c, const char *track) {
519 return disorder_simple(c, 0, "play", track, (char *)0);
522 /** @brief Remove a track
524 * @param track Track to remove (UTF-8)
525 * @return 0 on success, non-0 on error
527 int disorder_remove(disorder_client *c, const char *track) {
528 return disorder_simple(c, 0, "remove", track, (char *)0);
531 /** @brief Move a track
533 * @param track Track to move (UTF-8)
534 * @param delta Distance to move by
535 * @return 0 on success, non-0 on error
537 int disorder_move(disorder_client *c, const char *track, int delta) {
540 byte_snprintf(d, sizeof d, "%d", delta);
541 return disorder_simple(c, 0, "move", track, d, (char *)0);
544 /** @brief Enable play
546 * @return 0 on success, non-0 on error
548 int disorder_enable(disorder_client *c) {
549 return disorder_simple(c, 0, "enable", (char *)0);
552 /** @brief Disable play
554 * @return 0 on success, non-0 on error
556 int disorder_disable(disorder_client *c) {
557 return disorder_simple(c, 0, "disable", (char *)0);
560 /** @brief Scratch the currently playing track
561 * @param id Playing track ID or NULL (UTF-8)
563 * @return 0 on success, non-0 on error
565 int disorder_scratch(disorder_client *c, const char *id) {
566 return disorder_simple(c, 0, "scratch", id, (char *)0);
569 /** @brief Shut down the server
571 * @return 0 on success, non-0 on error
573 int disorder_shutdown(disorder_client *c) {
574 return disorder_simple(c, 0, "shutdown", (char *)0);
577 /** @brief Make the server re-read its configuration
579 * @return 0 on success, non-0 on error
581 int disorder_reconfigure(disorder_client *c) {
582 return disorder_simple(c, 0, "reconfigure", (char *)0);
585 /** @brief Rescan tracks
587 * @return 0 on success, non-0 on error
589 int disorder_rescan(disorder_client *c) {
590 return disorder_simple(c, 0, "rescan", (char *)0);
593 /** @brief Get server version number
595 * @param rp Where to store version string (UTF-8)
596 * @return 0 on success, non-0 on error
598 int disorder_version(disorder_client *c, char **rp) {
599 return dequote(disorder_simple(c, rp, "version", (char *)0), rp);
602 static void client_error(const char *msg,
603 void attribute((unused)) *u) {
604 error(0, "error parsing reply: %s", msg);
607 /** @brief Get currently playing track
609 * @param qp Where to store track information
610 * @return 0 on success, non-0 on error
612 * @p qp gets NULL if no track is playing.
614 int disorder_playing(disorder_client *c, struct queue_entry **qp) {
616 struct queue_entry *q;
619 if((rc = disorder_simple(c, &r, "playing", (char *)0)))
622 q = xmalloc(sizeof *q);
623 if(queue_unmarshall(q, r, client_error, 0))
631 /** @brief Fetch the queue, recent list, etc */
632 static int disorder_somequeue(disorder_client *c,
633 const char *cmd, struct queue_entry **qp) {
634 struct queue_entry *qh, **qt = &qh, *q;
638 if((rc = disorder_simple(c, 0, cmd, (char *)0)))
640 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
641 if(!strcmp(l, ".")) {
646 q = xmalloc(sizeof *q);
647 if(!queue_unmarshall(q, l, client_error, 0)) {
652 if(ferror(c->fpin)) {
653 byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
654 error(errno, "error reading %s", c->ident);
656 c->last = "input error: unexpxected EOF";
657 error(0, "error reading %s: unexpected EOF", c->ident);
662 /** @brief Get recently played tracks
664 * @param qp Where to store track information
665 * @return 0 on success, non-0 on error
667 * The last entry in the list is the most recently played track.
669 int disorder_recent(disorder_client *c, struct queue_entry **qp) {
670 return disorder_somequeue(c, "recent", qp);
675 * @param qp Where to store track information
676 * @return 0 on success, non-0 on error
678 * The first entry in the list will be played next.
680 int disorder_queue(disorder_client *c, struct queue_entry **qp) {
681 return disorder_somequeue(c, "queue", qp);
684 /** @brief Read a dot-stuffed list
686 * @param vecp Where to store list (UTF-8)
687 * @param nvecp Where to store number of items, or NULL
688 * @return 0 on success, non-0 on error
690 * The list will have a final NULL not counted in @p nvecp.
692 static int readlist(disorder_client *c, char ***vecp, int *nvecp) {
697 while(inputline(c->ident, c->fpin, &l, '\n') >= 0) {
698 if(!strcmp(l, ".")) {
699 vector_terminate(&v);
705 vector_append(&v, l + (*l == '.'));
707 if(ferror(c->fpin)) {
708 byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno));
709 error(errno, "error reading %s", c->ident);
711 c->last = "input error: unexpxected EOF";
712 error(0, "error reading %s: unexpected EOF", c->ident);
717 /** @brief Issue a comamnd and get a list response
719 * @param vecp Where to store list (UTF-8)
720 * @param nvecp Where to store number of items, or NULL
722 * @return 0 on success, non-0 on error
724 * The remaining arguments are command arguments, terminated by (char
725 * *)0. They should be in UTF-8.
727 * 5xx responses count as errors.
729 * See disorder_simple().
731 static int disorder_simple_list(disorder_client *c,
732 char ***vecp, int *nvecp,
733 const char *cmd, ...) {
738 ret = disorder_simple_v(c, 0, cmd, 0, 0, ap);
741 return readlist(c, vecp, nvecp);
744 /** @brief List directories below @p dir
746 * @param dir Directory to list, or NULL for root (UTF-8)
747 * @param re Regexp that results must match, or NULL (UTF-8)
748 * @param vecp Where to store list (UTF-8)
749 * @param nvecp Where to store number of items, or NULL
750 * @return 0 on success, non-0 on error
752 int disorder_directories(disorder_client *c, const char *dir, const char *re,
753 char ***vecp, int *nvecp) {
754 return disorder_simple_list(c, vecp, nvecp, "dirs", dir, re, (char *)0);
757 /** @brief List files below @p dir
759 * @param dir Directory to list, or NULL for root (UTF-8)
760 * @param re Regexp that results must match, or NULL (UTF-8)
761 * @param vecp Where to store list (UTF-8)
762 * @param nvecp Where to store number of items, or NULL
763 * @return 0 on success, non-0 on error
765 int disorder_files(disorder_client *c, const char *dir, const char *re,
766 char ***vecp, int *nvecp) {
767 return disorder_simple_list(c, vecp, nvecp, "files", dir, re, (char *)0);
770 /** @brief List files and directories below @p dir
772 * @param dir Directory to list, or NULL for root (UTF-8)
773 * @param re Regexp that results must match, or NULL (UTF-8)
774 * @param vecp Where to store list (UTF-8)
775 * @param nvecp Where to store number of items, or NULL
776 * @return 0 on success, non-0 on error
778 int disorder_allfiles(disorder_client *c, const char *dir, const char *re,
779 char ***vecp, int *nvecp) {
780 return disorder_simple_list(c, vecp, nvecp, "allfiles", dir, re, (char *)0);
783 /** @brief Return the user we logged in with
785 * @return User name (owned by @p c, don't modify)
787 char *disorder_user(disorder_client *c) {
791 /** @brief Set a track preference
793 * @param track Track name (UTF-8)
794 * @param key Preference name (UTF-8)
795 * @param value Preference value (UTF-8)
796 * @return 0 on success, non-0 on error
798 int disorder_set(disorder_client *c, const char *track,
799 const char *key, const char *value) {
800 return disorder_simple(c, 0, "set", track, key, value, (char *)0);
803 /** @brief Unset a track preference
805 * @param track Track name (UTF-8)
806 * @param key Preference name (UTF-8)
807 * @return 0 on success, non-0 on error
809 int disorder_unset(disorder_client *c, const char *track,
811 return disorder_simple(c, 0, "unset", track, key, (char *)0);
814 /** @brief Get a track preference
816 * @param track Track name (UTF-8)
817 * @param key Preference name (UTF-8)
818 * @param valuep Where to store preference value (UTF-8)
819 * @return 0 on success, non-0 on error
821 int disorder_get(disorder_client *c,
822 const char *track, const char *key, char **valuep) {
823 return dequote(disorder_simple(c, valuep, "get", track, key, (char *)0),
827 static void pref_error_handler(const char *msg,
828 void attribute((unused)) *u) {
829 error(0, "error handling 'prefs' reply: %s", msg);
832 /** @brief Get all preferences for a trcak
834 * @param track Track name
835 * @param kp Where to store linked list of preferences
836 * @return 0 on success, non-0 on error
838 int disorder_prefs(disorder_client *c, const char *track, struct kvp **kp) {
840 int nvec, npvec, n, rc;
843 if((rc = disorder_simple_list(c, &vec, &nvec, "prefs", track, (char *)0)))
845 for(n = 0; n < nvec; ++n) {
846 if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pref_error_handler, 0)))
849 pref_error_handler("malformed response", 0);
852 *kp = k = xmalloc(sizeof *k);
861 /** @brief Parse a boolean response
862 * @param cmd Command for use in error messsage
863 * @param value Result from server
864 * @param flagp Where to store result
865 * @return 0 on success, non-0 on error
867 static int boolean(const char *cmd, const char *value,
869 if(!strcmp(value, "yes")) *flagp = 1;
870 else if(!strcmp(value, "no")) *flagp = 0;
872 error(0, "malformed response to '%s'", cmd);
878 /** @brief Test whether a track exists
880 * @param track Track name (UTF-8)
881 * @param existsp Where to store result (non-0 iff does exist)
882 * @return 0 on success, non-0 on error
884 int disorder_exists(disorder_client *c, const char *track, int *existsp) {
888 if((rc = disorder_simple(c, &v, "exists", track, (char *)0)))
890 return boolean("exists", v, existsp);
893 /** @brief Test whether playing is enabled
895 * @param enabledp Where to store result (non-0 iff enabled)
896 * @return 0 on success, non-0 on error
898 int disorder_enabled(disorder_client *c, int *enabledp) {
902 if((rc = disorder_simple(c, &v, "enabled", (char *)0)))
904 return boolean("enabled", v, enabledp);
907 /** @brief Get the length of a track
909 * @param track Track name (UTF-8)
910 * @param valuep Where to store length in seconds
911 * @return 0 on success, non-0 on error
913 * If the length is unknown 0 is returned.
915 int disorder_length(disorder_client *c, const char *track,
920 if((rc = disorder_simple(c, &value, "length", track, (char *)0)))
922 *valuep = atol(value);
926 /** @brief Search for tracks
928 * @param terms Search terms (UTF-8)
929 * @param vecp Where to store list (UTF-8)
930 * @param nvecp Where to store number of items, or NULL
931 * @return 0 on success, non-0 on error
933 int disorder_search(disorder_client *c, const char *terms,
934 char ***vecp, int *nvecp) {
935 return disorder_simple_list(c, vecp, nvecp, "search", terms, (char *)0);
938 /** @brief Enable random play
940 * @return 0 on success, non-0 on error
942 int disorder_random_enable(disorder_client *c) {
943 return disorder_simple(c, 0, "random-enable", (char *)0);
946 /** @brief Disable random play
948 * @return 0 on success, non-0 on error
950 int disorder_random_disable(disorder_client *c) {
951 return disorder_simple(c, 0, "random-disable", (char *)0);
954 /** @brief Test whether random play is enabled
956 * @param enabledp Where to store result (non-0 iff enabled)
957 * @return 0 on success, non-0 on error
959 int disorder_random_enabled(disorder_client *c, int *enabledp) {
963 if((rc = disorder_simple(c, &v, "random-enabled", (char *)0)))
965 return boolean("random-enabled", v, enabledp);
968 /** @brief Get server stats
970 * @param vecp Where to store list (UTF-8)
971 * @param nvecp Where to store number of items, or NULL
972 * @return 0 on success, non-0 on error
974 int disorder_stats(disorder_client *c,
975 char ***vecp, int *nvecp) {
976 return disorder_simple_list(c, vecp, nvecp, "stats", (char *)0);
979 /** @brief Set volume
981 * @param left New left channel value
982 * @param right New right channel value
983 * @return 0 on success, non-0 on error
985 int disorder_set_volume(disorder_client *c, int left, int right) {
988 if(byte_asprintf(&ls, "%d", left) < 0
989 || byte_asprintf(&rs, "%d", right) < 0)
991 return disorder_simple(c, 0, "volume", ls, rs, (char *)0);
994 /** @brief Get volume
996 * @param left Where to store left channel value
997 * @param right Where to store right channel value
998 * @return 0 on success, non-0 on error
1000 int disorder_get_volume(disorder_client *c, int *left, int *right) {
1004 if((rc = disorder_simple(c, &r, "volume", (char *)0)))
1006 if(sscanf(r, "%d %d", left, right) != 2) {
1007 c->last = "malformed volume response";
1008 error(0, "error parsing response to 'volume': '%s'", r);
1014 /** @brief Log to a sink
1016 * @param s Sink to write log lines to
1017 * @return 0 on success, non-0 on error
1019 int disorder_log(disorder_client *c, struct sink *s) {
1023 if((rc = disorder_simple(c, 0, "log", (char *)0)))
1025 while(inputline(c->ident, c->fpin, &l, '\n') >= 0 && strcmp(l, "."))
1026 if(sink_printf(s, "%s\n", l) < 0) return -1;
1027 if(ferror(c->fpin) || feof(c->fpin)) {
1028 byte_xasprintf((char **)&c->last, "input error: %s",
1029 ferror(c->fpin) ? strerror(errno) : "unexpxected EOF");
1035 /** @brief Look up a track name part
1037 * @param partp Where to store result (UTF-8)
1038 * @param track Track name (UTF-8)
1039 * @param context Context (usually "sort" or "display") (UTF-8)
1040 * @param part Track part (UTF-8)
1041 * @return 0 on success, non-0 on error
1043 int disorder_part(disorder_client *c, char **partp,
1044 const char *track, const char *context, const char *part) {
1045 return dequote(disorder_simple(c, partp, "part",
1046 track, context, part, (char *)0), partp);
1049 /** @brief Resolve aliases
1051 * @param trackp Where to store canonical name (UTF-8)
1052 * @param track Track name (UTF-8)
1053 * @return 0 on success, non-0 on error
1055 int disorder_resolve(disorder_client *c, char **trackp, const char *track) {
1056 return dequote(disorder_simple(c, trackp, "resolve", track, (char *)0),
1060 /** @brief Pause the current track
1062 * @return 0 on success, non-0 on error
1064 int disorder_pause(disorder_client *c) {
1065 return disorder_simple(c, 0, "pause", (char *)0);
1068 /** @brief Resume the current track
1070 * @return 0 on success, non-0 on error
1072 int disorder_resume(disorder_client *c) {
1073 return disorder_simple(c, 0, "resume", (char *)0);
1076 /** @brief List all known tags
1078 * @param vecp Where to store list (UTF-8)
1079 * @param nvecp Where to store number of items, or NULL
1080 * @return 0 on success, non-0 on error
1082 int disorder_tags(disorder_client *c,
1083 char ***vecp, int *nvecp) {
1084 return disorder_simple_list(c, vecp, nvecp, "tags", (char *)0);
1087 /** @brief List all known users
1089 * @param vecp Where to store list (UTF-8)
1090 * @param nvecp Where to store number of items, or NULL
1091 * @return 0 on success, non-0 on error
1093 int disorder_users(disorder_client *c,
1094 char ***vecp, int *nvecp) {
1095 return disorder_simple_list(c, vecp, nvecp, "users", (char *)0);
1098 /** @brief Get recently added tracks
1100 * @param vecp Where to store pointer to list (UTF-8)
1101 * @param nvecp Where to store count
1102 * @param max Maximum tracks to fetch, or 0 for all available
1103 * @return 0 on success, non-0 on error
1105 int disorder_new_tracks(disorder_client *c,
1106 char ***vecp, int *nvecp,
1110 sprintf(limit, "%d", max);
1111 return disorder_simple_list(c, vecp, nvecp, "new", limit, (char *)0);
1114 /** @brief Set a global preference
1116 * @param key Preference name (UTF-8)
1117 * @param value Preference value (UTF-8)
1118 * @return 0 on success, non-0 on error
1120 int disorder_set_global(disorder_client *c,
1121 const char *key, const char *value) {
1122 return disorder_simple(c, 0, "set-global", key, value, (char *)0);
1125 /** @brief Unset a global preference
1127 * @param key Preference name (UTF-8)
1128 * @return 0 on success, non-0 on error
1130 int disorder_unset_global(disorder_client *c, const char *key) {
1131 return disorder_simple(c, 0, "unset-global", key, (char *)0);
1134 /** @brief Get a global preference
1136 * @param key Preference name (UTF-8)
1137 * @param valuep Where to store preference value (UTF-8)
1138 * @return 0 on success, non-0 on error
1140 int disorder_get_global(disorder_client *c, const char *key, char **valuep) {
1141 return dequote(disorder_simple(c, valuep, "get-global", key, (char *)0),
1145 /** @brief Get server's RTP address information
1147 * @param addressp Where to store address (UTF-8)
1148 * @param portp Where to store port (UTF-8)
1149 * @return 0 on success, non-0 on error
1151 int disorder_rtp_address(disorder_client *c, char **addressp, char **portp) {
1156 if((rc = disorder_simple(c, &r, "rtp-address", (char *)0)))
1158 vec = split(r, &n, SPLIT_QUOTES, 0, 0);
1160 c->last = "malformed RTP address";
1161 error(0, "malformed rtp-address reply");
1169 /** @brief Create a user
1171 * @param user Username
1172 * @param password Password
1173 * @param rights Initial rights or NULL to use default
1174 * @return 0 on success, non-0 on error
1176 int disorder_adduser(disorder_client *c,
1177 const char *user, const char *password,
1178 const char *rights) {
1179 return disorder_simple(c, 0, "adduser", user, password, rights, (char *)0);
1182 /** @brief Delete a user
1184 * @param user Username
1185 * @return 0 on success, non-0 on error
1187 int disorder_deluser(disorder_client *c, const char *user) {
1188 return disorder_simple(c, 0, "deluser", user, (char *)0);
1191 /** @brief Get user information
1193 * @param user Username
1194 * @param key Property name (UTF-8)
1195 * @param valuep Where to store value (UTF-8)
1196 * @return 0 on success, non-0 on error
1198 int disorder_userinfo(disorder_client *c, const char *user, const char *key,
1200 return dequote(disorder_simple(c, valuep, "userinfo", user, key, (char *)0),
1204 /** @brief Set user information
1206 * @param user Username
1207 * @param key Property name (UTF-8)
1208 * @param value New property value (UTF-8)
1209 * @return 0 on success, non-0 on error
1211 int disorder_edituser(disorder_client *c, const char *user,
1212 const char *key, const char *value) {
1213 return disorder_simple(c, 0, "edituser", user, key, value, (char *)0);
1216 /** @brief Register a user
1218 * @param user Username
1219 * @param password Password
1220 * @param email Email address (UTF-8)
1221 * @param confirmp Where to store confirmation string
1222 * @return 0 on success, non-0 on error
1224 int disorder_register(disorder_client *c, const char *user,
1225 const char *password, const char *email,
1227 return dequote(disorder_simple(c, confirmp, "register",
1228 user, password, email, (char *)0),
1232 /** @brief Confirm a user
1234 * @param confirm Confirmation string
1235 * @return 0 on success, non-0 on error
1237 int disorder_confirm(disorder_client *c, const char *confirm) {
1241 if(!(rc = dequote(disorder_simple(c, &u, "confirm", confirm, (char *)0),
1247 /** @brief Make a cookie for this login
1249 * @param cookiep Where to store cookie string
1250 * @return 0 on success, non-0 on error
1252 int disorder_make_cookie(disorder_client *c, char **cookiep) {
1253 return dequote(disorder_simple(c, cookiep, "make-cookie", (char *)0),
1257 /** @brief Revoke the cookie used by this session
1259 * @return 0 on success, non-0 on error
1261 int disorder_revoke(disorder_client *c) {
1262 return disorder_simple(c, 0, "revoke", (char *)0);
1265 /** @brief Request a password reminder email
1267 * @param user Username
1268 * @return 0 on success, non-0 on error
1270 int disorder_reminder(disorder_client *c, const char *user) {
1271 return disorder_simple(c, 0, "reminder", user, (char *)0);
1274 /** @brief List scheduled events
1276 * @param idsp Where to put list of event IDs
1277 * @param nidsp Where to put count of event IDs, or NULL
1278 * @return 0 on success, non-0 on error
1280 int disorder_schedule_list(disorder_client *c, char ***idsp, int *nidsp) {
1281 return disorder_simple_list(c, idsp, nidsp, "schedule-list", (char *)0);
1284 /** @brief Delete a scheduled event
1286 * @param id Event ID to delete
1287 * @return 0 on success, non-0 on error
1289 int disorder_schedule_del(disorder_client *c, const char *id) {
1290 return disorder_simple(c, 0, "schedule-del", id, (char *)0);
1293 /** @brief Get details of a scheduled event
1295 * @param id Event ID
1296 * @param actiondatap Where to put details
1297 * @return 0 on success, non-0 on error
1299 int disorder_schedule_get(disorder_client *c, const char *id,
1300 struct kvp **actiondatap) {
1301 char **lines, **bits;
1305 if((rc = disorder_simple_list(c, &lines, NULL,
1306 "schedule-get", id, (char *)0)))
1309 if(!(bits = split(*lines++, &nbits, SPLIT_QUOTES, 0, 0))) {
1310 error(0, "invalid schedule-get reply: cannot split line");
1314 error(0, "invalid schedule-get reply: wrong number of fields");
1317 kvp_set(actiondatap, bits[0], bits[1]);
1322 /** @brief Add a scheduled event
1324 * @param when When to trigger the event
1325 * @param priority Event priority ("normal" or "junk")
1326 * @param action What action to perform
1327 * @param ... Action-specific arguments
1328 * @return 0 on success, non-0 on error
1330 * For action @c "play" the next argument is the track.
1332 * For action @c "set-global" next argument is the global preference name
1333 * and the final argument the value to set it to, or (char *)0 to unset it.
1335 int disorder_schedule_add(disorder_client *c,
1337 const char *priority,
1344 snprintf(when_str, sizeof when_str, "%lld", (long long)when);
1345 va_start(ap, action);
1346 if(!strcmp(action, "play"))
1347 rc = disorder_simple(c, 0, "schedule-add", when_str, priority,
1348 action, va_arg(ap, char *),
1350 else if(!strcmp(action, "set-global")) {
1351 const char *key = va_arg(ap, char *);
1352 const char *value = va_arg(ap, char *);
1353 rc = disorder_simple(c, 0,"schedule-add", when_str, priority,
1357 fatal(0, "unknown action '%s'", action);
1362 /** @brief Adopt a track
1364 * @param id Track ID to adopt
1365 * @return 0 on success, non-0 on error
1367 int disorder_adopt(disorder_client *c, const char *id) {
1368 return disorder_simple(c, 0, "adopt", id, (char *)0);
1371 /** @brief Delete a playlist
1373 * @param playlist Playlist to delete
1374 * @return 0 on success, non-0 on error
1376 int disorder_playlist_delete(disorder_client *c,
1377 const char *playlist) {
1378 return disorder_simple(c, 0, "playlist-delete", playlist, (char *)0);
1381 /** @brief Get the contents of a playlist
1383 * @param playlist Playlist to get
1384 * @param tracksp Where to put list of tracks
1385 * @param ntracksp Where to put count of tracks
1386 * @return 0 on success, non-0 on error
1388 int disorder_playlist_get(disorder_client *c, const char *playlist,
1389 char ***tracksp, int *ntracksp) {
1390 return disorder_simple_list(c, tracksp, ntracksp,
1391 "playlist-get", playlist, (char *)0);
1394 /** @brief List all readable playlists
1396 * @param playlistsp Where to put list of playlists
1397 * @param nplaylistsp Where to put count of playlists
1398 * @return 0 on success, non-0 on error
1400 int disorder_playlists(disorder_client *c,
1401 char ***playlistsp, int *nplaylistsp) {
1402 return disorder_simple_list(c, playlistsp, nplaylistsp,
1403 "playlists", (char *)0);
1406 /** @brief Get the sharing status of a playlist
1408 * @param playlist Playlist to inspect
1409 * @param sharep Where to put sharing status
1410 * @return 0 on success, non-0 on error
1412 * Possible @p sharep values are @c public, @c private and @c shared.
1414 int disorder_playlist_get_share(disorder_client *c, const char *playlist,
1416 return disorder_simple(c, sharep,
1417 "playlist-get-share", playlist, (char *)0);
1420 /** @brief Get the sharing status of a playlist
1422 * @param playlist Playlist to modify
1423 * @param share New sharing status
1424 * @return 0 on success, non-0 on error
1426 * Possible @p share values are @c public, @c private and @c shared.
1428 int disorder_playlist_set_share(disorder_client *c, const char *playlist,
1429 const char *share) {
1430 return disorder_simple(c, 0,
1431 "playlist-set-share", playlist, share, (char *)0);
1434 /** @brief Lock a playlist for modifications
1436 * @param playlist Playlist to lock
1437 * @return 0 on success, non-0 on error
1439 int disorder_playlist_lock(disorder_client *c, const char *playlist) {
1440 return disorder_simple(c, 0,
1441 "playlist-lock", playlist, (char *)0);
1444 /** @brief Unlock the locked playlist
1446 * @return 0 on success, non-0 on error
1448 int disorder_playlist_unlock(disorder_client *c) {
1449 return disorder_simple(c, 0,
1450 "playlist-unlock", (char *)0);
1453 /** @brief Set the contents of a playlst
1455 * @param playlist Playlist to modify
1456 * @param tracks List of tracks
1457 * @param ntracks Length of @p tracks (or -1 to count up to the first NULL)
1458 * @return 0 on success, non-0 on error
1460 int disorder_playlist_set(disorder_client *c,
1461 const char *playlist,
1464 return disorder_simple_body(c, 0, tracks, ntracks,
1465 "playlist-set", playlist, (char *)0);