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 ap Arguments (UTF-8), terminated by (char *)0
157 * @return 0 on success, non-0 on error
159 * 5xx responses count as errors.
161 * @p rp will NOT be filled in for xx9 responses (where it is just
162 * commentary for a command where it would normally be meaningful).
164 * NB that the response will NOT be converted to the local encoding
165 * nor will quotes be stripped. See dequote().
167 static int disorder_simple_v(disorder_client
*c
,
169 const char *cmd
, va_list ap
) {
174 c
->last
= "not connected";
175 error(0, "not connected to server");
180 dynstr_append_string(&d
, cmd
);
181 while((arg
= va_arg(ap
, const char *))) {
182 dynstr_append(&d
, ' ');
183 dynstr_append_string(&d
, quoteutf8(arg
));
185 dynstr_append(&d
, '\n');
186 dynstr_terminate(&d
);
187 D(("command: %s", d
.vec
));
188 if(fputs(d
.vec
, c
->fpout
) < 0 || fflush(c
->fpout
)) {
189 byte_xasprintf((char **)&c
->last
, "write error: %s", strerror(errno
));
190 error(errno
, "error writing to %s", c
->ident
);
194 return check_response(c
, rp
);
197 /** @brief Issue a command and parse a simple response
199 * @param rp Where to store result, or NULL (UTF-8)
201 * @return 0 on success, non-0 on error
203 * The remaining arguments are command arguments, terminated by (char
204 * *)0. They should be in UTF-8.
206 * 5xx responses count as errors.
208 * @p rp will NOT be filled in for xx9 responses (where it is just
209 * commentary for a command where it would normally be meaningful).
211 * NB that the response will NOT be converted to the local encoding
212 * nor will quotes be stripped. See dequote().
214 static int disorder_simple(disorder_client
*c
,
216 const char *cmd
, ...) {
221 ret
= disorder_simple_v(c
, rp
, cmd
, ap
);
226 /** @brief Dequote a result string
227 * @param rc 0 on success, non-0 on error
228 * @param rp Where result string is stored (UTF-8)
231 * This is used as a wrapper around disorder_simple() to dequote
234 static int dequote(int rc
, char **rp
) {
238 if((rr
= split(*rp
, 0, SPLIT_QUOTES
, 0, 0)) && *rr
) {
242 error(0, "invalid reply: %s", *rp
);
247 /** @brief Generic connection routine
248 * @param conf Configuration to follow
250 * @param username Username to log in with or NULL
251 * @param password Password to log in with or NULL
252 * @param cookie Cookie to log in with or NULL
253 * @return 0 on success, non-0 on error
255 * @p cookie is tried first if not NULL. If it is NULL then @p
256 * username must not be. If @p username is not NULL then nor may @p
259 int disorder_connect_generic(struct config
*conf
,
261 const char *username
,
262 const char *password
,
263 const char *cookie
) {
264 int fd
= -1, fd2
= -1, nrvec
, rc
;
265 unsigned char *nonce
;
269 const char *protocol
, *algorithm
, *challenge
;
273 if((salen
= find_server(conf
, &sa
, &c
->ident
)) == (socklen_t
)-1)
275 c
->fpin
= c
->fpout
= 0;
276 if((fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0) {
277 byte_xasprintf((char **)&c
->last
, "socket: %s", strerror(errno
));
278 error(errno
, "error calling socket");
281 if(connect(fd
, sa
, salen
) < 0) {
282 byte_xasprintf((char **)&c
->last
, "connect: %s", strerror(errno
));
283 error(errno
, "error calling connect");
286 if((fd2
= dup(fd
)) < 0) {
287 byte_xasprintf((char **)&c
->last
, "dup: %s", strerror(errno
));
288 error(errno
, "error calling dup");
291 if(!(c
->fpin
= fdopen(fd
, "rb"))) {
292 byte_xasprintf((char **)&c
->last
, "fdopen: %s", strerror(errno
));
293 error(errno
, "error calling fdopen");
297 if(!(c
->fpout
= fdopen(fd2
, "wb"))) {
298 byte_xasprintf((char **)&c
->last
, "fdopen: %s", strerror(errno
));
299 error(errno
, "error calling fdopen");
303 if((rc
= disorder_simple(c
, &r
, 0, (const char *)0)))
305 if(!(rvec
= split(r
, &nrvec
, SPLIT_QUOTES
, 0, 0)))
308 c
->last
= "cannot parse server greeting";
309 error(0, "cannot parse server greeting %s", r
);
313 if(strcmp(protocol
, "2")) {
314 c
->last
= "unknown protocol version";
315 error(0, "unknown protocol version: %s", protocol
);
320 if(!(nonce
= unhex(challenge
, &nl
)))
323 if(!dequote(disorder_simple(c
, &c
->user
, "cookie", cookie
, (char *)0),
325 return 0; /* success */
327 c
->last
= "cookie failed and no username";
328 error(0, "cookie did not work and no username available");
332 if(!(res
= authhash(nonce
, nl
, password
, algorithm
))) {
333 c
->last
= "error computing authorization hash";
336 if((rc
= disorder_simple(c
, 0, "user", username
, res
, (char *)0)))
338 c
->user
= xstrdup(username
);
351 if(fd2
!= -1) close(fd2
);
352 if(fd
!= -1) close(fd
);
356 /** @brief Connect a client with a specified username and password
358 * @param username Username to log in with
359 * @param password Password to log in with
360 * @return 0 on success, non-0 on error
362 int disorder_connect_user(disorder_client
*c
,
363 const char *username
,
364 const char *password
) {
365 return disorder_connect_generic(config
,
372 /** @brief Connect a client
374 * @return 0 on success, non-0 on error
376 * The connection will use the username and password found in @ref
377 * config, or directly from the database if no password is found and
378 * the database is readable (usually only for root).
380 int disorder_connect(disorder_client
*c
) {
381 const char *username
, *password
;
383 if(!(username
= config
->username
)) {
384 c
->last
= "no username";
385 error(0, "no username configured");
388 password
= config
->password
;
389 /* Maybe we can read the database */
390 if(!password
&& trackdb_readable()) {
391 trackdb_init(TRACKDB_NO_RECOVER
|TRACKDB_NO_UPGRADE
);
392 trackdb_open(TRACKDB_READ_ONLY
);
393 password
= trackdb_get_password(username
);
398 c
->last
= "no password";
399 error(0, "no password configured for user '%s'", username
);
402 return disorder_connect_generic(config
,
409 /** @brief Connect a client
411 * @param cookie Cookie to log in with, or NULL
412 * @return 0 on success, non-0 on error
414 * If @p cookie is NULL or does not work then we attempt to log in as
415 * guest instead (so when the cookie expires only an extra round trip
416 * is needed rathre than a complete new login).
418 int disorder_connect_cookie(disorder_client
*c
,
419 const char *cookie
) {
420 return disorder_connect_generic(config
,
427 /** @brief Close a client
429 * @return 0 on succcess, non-0 on errior
431 * The client is still closed even on error. It might well be
432 * appropriate to ignore the return value.
434 int disorder_close(disorder_client
*c
) {
438 if(fclose(c
->fpin
) < 0) {
439 byte_xasprintf((char **)&c
->last
, "fclose: %s", strerror(errno
));
440 error(errno
, "error calling fclose");
446 if(fclose(c
->fpout
) < 0) {
447 byte_xasprintf((char **)&c
->last
, "fclose: %s", strerror(errno
));
448 error(errno
, "error calling fclose");
458 /** @brief Play a track
460 * @param track Track to play (UTF-8)
461 * @return 0 on success, non-0 on error
463 int disorder_play(disorder_client
*c
, const char *track
) {
464 return disorder_simple(c
, 0, "play", track
, (char *)0);
467 /** @brief Remove a track
469 * @param track Track to remove (UTF-8)
470 * @return 0 on success, non-0 on error
472 int disorder_remove(disorder_client
*c
, const char *track
) {
473 return disorder_simple(c
, 0, "remove", track
, (char *)0);
476 /** @brief Move a track
478 * @param track Track to move (UTF-8)
479 * @param delta Distance to move by
480 * @return 0 on success, non-0 on error
482 int disorder_move(disorder_client
*c
, const char *track
, int delta
) {
485 byte_snprintf(d
, sizeof d
, "%d", delta
);
486 return disorder_simple(c
, 0, "move", track
, d
, (char *)0);
489 /** @brief Enable play
491 * @return 0 on success, non-0 on error
493 int disorder_enable(disorder_client
*c
) {
494 return disorder_simple(c
, 0, "enable", (char *)0);
497 /** @brief Disable play
499 * @return 0 on success, non-0 on error
501 int disorder_disable(disorder_client
*c
) {
502 return disorder_simple(c
, 0, "disable", (char *)0);
505 /** @brief Scratch the currently playing track
506 * @param id Playing track ID or NULL (UTF-8)
508 * @return 0 on success, non-0 on error
510 int disorder_scratch(disorder_client
*c
, const char *id
) {
511 return disorder_simple(c
, 0, "scratch", id
, (char *)0);
514 /** @brief Shut down the server
516 * @return 0 on success, non-0 on error
518 int disorder_shutdown(disorder_client
*c
) {
519 return disorder_simple(c
, 0, "shutdown", (char *)0);
522 /** @brief Make the server re-read its configuration
524 * @return 0 on success, non-0 on error
526 int disorder_reconfigure(disorder_client
*c
) {
527 return disorder_simple(c
, 0, "reconfigure", (char *)0);
530 /** @brief Rescan tracks
532 * @return 0 on success, non-0 on error
534 int disorder_rescan(disorder_client
*c
) {
535 return disorder_simple(c
, 0, "rescan", (char *)0);
538 /** @brief Get server version number
540 * @param rp Where to store version string (UTF-8)
541 * @return 0 on success, non-0 on error
543 int disorder_version(disorder_client
*c
, char **rp
) {
544 return dequote(disorder_simple(c
, rp
, "version", (char *)0), rp
);
547 static void client_error(const char *msg
,
548 void attribute((unused
)) *u
) {
549 error(0, "error parsing reply: %s", msg
);
552 /** @brief Get currently playing track
554 * @param qp Where to store track information
555 * @return 0 on success, non-0 on error
557 * @p qp gets NULL if no track is playing.
559 int disorder_playing(disorder_client
*c
, struct queue_entry
**qp
) {
561 struct queue_entry
*q
;
564 if((rc
= disorder_simple(c
, &r
, "playing", (char *)0)))
567 q
= xmalloc(sizeof *q
);
568 if(queue_unmarshall(q
, r
, client_error
, 0))
576 /** @brief Fetch the queue, recent list, etc */
577 static int disorder_somequeue(disorder_client
*c
,
578 const char *cmd
, struct queue_entry
**qp
) {
579 struct queue_entry
*qh
, **qt
= &qh
, *q
;
583 if((rc
= disorder_simple(c
, 0, cmd
, (char *)0)))
585 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0) {
586 if(!strcmp(l
, ".")) {
591 q
= xmalloc(sizeof *q
);
592 if(!queue_unmarshall(q
, l
, client_error
, 0)) {
597 if(ferror(c
->fpin
)) {
598 byte_xasprintf((char **)&c
->last
, "input error: %s", strerror(errno
));
599 error(errno
, "error reading %s", c
->ident
);
601 c
->last
= "input error: unexpxected EOF";
602 error(0, "error reading %s: unexpected EOF", c
->ident
);
607 /** @brief Get recently played tracks
609 * @param qp Where to store track information
610 * @return 0 on success, non-0 on error
612 * The last entry in the list is the most recently played track.
614 int disorder_recent(disorder_client
*c
, struct queue_entry
**qp
) {
615 return disorder_somequeue(c
, "recent", qp
);
620 * @param qp Where to store track information
621 * @return 0 on success, non-0 on error
623 * The first entry in the list will be played next.
625 int disorder_queue(disorder_client
*c
, struct queue_entry
**qp
) {
626 return disorder_somequeue(c
, "queue", qp
);
629 /** @brief Read a dot-stuffed list
631 * @param vecp Where to store list (UTF-8)
632 * @param nvecp Where to store number of items, or NULL
633 * @return 0 on success, non-0 on error
635 * The list will have a final NULL not counted in @p nvecp.
637 static int readlist(disorder_client
*c
, char ***vecp
, int *nvecp
) {
642 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0) {
643 if(!strcmp(l
, ".")) {
644 vector_terminate(&v
);
650 vector_append(&v
, l
+ (*l
== '.'));
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 Issue a comamnd and get a list response
664 * @param vecp Where to store list (UTF-8)
665 * @param nvecp Where to store number of items, or NULL
667 * @return 0 on success, non-0 on error
669 * The remaining arguments are command arguments, terminated by (char
670 * *)0. They should be in UTF-8.
672 * 5xx responses count as errors.
674 static int disorder_simple_list(disorder_client
*c
,
675 char ***vecp
, int *nvecp
,
676 const char *cmd
, ...) {
681 ret
= disorder_simple_v(c
, 0, cmd
, ap
);
684 return readlist(c
, vecp
, nvecp
);
687 /** @brief List directories below @p dir
689 * @param dir Directory to list, or NULL for root (UTF-8)
690 * @param re Regexp that results must match, or NULL (UTF-8)
691 * @param vecp Where to store list (UTF-8)
692 * @param nvecp Where to store number of items, or NULL
693 * @return 0 on success, non-0 on error
695 int disorder_directories(disorder_client
*c
, const char *dir
, const char *re
,
696 char ***vecp
, int *nvecp
) {
697 return disorder_simple_list(c
, vecp
, nvecp
, "dirs", dir
, re
, (char *)0);
700 /** @brief List files below @p dir
702 * @param dir Directory to list, or NULL for root (UTF-8)
703 * @param re Regexp that results must match, or NULL (UTF-8)
704 * @param vecp Where to store list (UTF-8)
705 * @param nvecp Where to store number of items, or NULL
706 * @return 0 on success, non-0 on error
708 int disorder_files(disorder_client
*c
, const char *dir
, const char *re
,
709 char ***vecp
, int *nvecp
) {
710 return disorder_simple_list(c
, vecp
, nvecp
, "files", dir
, re
, (char *)0);
713 /** @brief List files and directories below @p dir
715 * @param dir Directory to list, or NULL for root (UTF-8)
716 * @param re Regexp that results must match, or NULL (UTF-8)
717 * @param vecp Where to store list (UTF-8)
718 * @param nvecp Where to store number of items, or NULL
719 * @return 0 on success, non-0 on error
721 int disorder_allfiles(disorder_client
*c
, const char *dir
, const char *re
,
722 char ***vecp
, int *nvecp
) {
723 return disorder_simple_list(c
, vecp
, nvecp
, "allfiles", dir
, re
, (char *)0);
726 /** @brief Return the user we logged in with
728 * @return User name (owned by @p c, don't modify)
730 char *disorder_user(disorder_client
*c
) {
734 /** @brief Set a track preference
736 * @param track Track name (UTF-8)
737 * @param key Preference name (UTF-8)
738 * @param value Preference value (UTF-8)
739 * @return 0 on success, non-0 on error
741 int disorder_set(disorder_client
*c
, const char *track
,
742 const char *key
, const char *value
) {
743 return disorder_simple(c
, 0, "set", track
, key
, value
, (char *)0);
746 /** @brief Unset a track preference
748 * @param track Track name (UTF-8)
749 * @param key Preference name (UTF-8)
750 * @return 0 on success, non-0 on error
752 int disorder_unset(disorder_client
*c
, const char *track
,
754 return disorder_simple(c
, 0, "unset", track
, key
, (char *)0);
757 /** @brief Get a track preference
759 * @param track Track name (UTF-8)
760 * @param key Preference name (UTF-8)
761 * @param valuep Where to store preference value (UTF-8)
762 * @return 0 on success, non-0 on error
764 int disorder_get(disorder_client
*c
,
765 const char *track
, const char *key
, char **valuep
) {
766 return dequote(disorder_simple(c
, valuep
, "get", track
, key
, (char *)0),
770 static void pref_error_handler(const char *msg
,
771 void attribute((unused
)) *u
) {
772 error(0, "error handling 'prefs' reply: %s", msg
);
775 /** @brief Get all preferences for a trcak
777 * @param track Track name
778 * @param kp Where to store linked list of preferences
779 * @return 0 on success, non-0 on error
781 int disorder_prefs(disorder_client
*c
, const char *track
, struct kvp
**kp
) {
783 int nvec
, npvec
, n
, rc
;
786 if((rc
= disorder_simple_list(c
, &vec
, &nvec
, "prefs", track
, (char *)0)))
788 for(n
= 0; n
< nvec
; ++n
) {
789 if(!(pvec
= split(vec
[n
], &npvec
, SPLIT_QUOTES
, pref_error_handler
, 0)))
792 pref_error_handler("malformed response", 0);
795 *kp
= k
= xmalloc(sizeof *k
);
804 /** @brief Parse a boolean response
805 * @param cmd Command for use in error messsage
806 * @param value Result from server
807 * @param flagp Where to store result
808 * @return 0 on success, non-0 on error
810 static int boolean(const char *cmd
, const char *value
,
812 if(!strcmp(value
, "yes")) *flagp
= 1;
813 else if(!strcmp(value
, "no")) *flagp
= 0;
815 error(0, "malformed response to '%s'", cmd
);
821 /** @brief Test whether a track exists
823 * @param track Track name (UTF-8)
824 * @param existsp Where to store result (non-0 iff does exist)
825 * @return 0 on success, non-0 on error
827 int disorder_exists(disorder_client
*c
, const char *track
, int *existsp
) {
831 if((rc
= disorder_simple(c
, &v
, "exists", track
, (char *)0)))
833 return boolean("exists", v
, existsp
);
836 /** @brief Test whether playing is enabled
838 * @param enabledp Where to store result (non-0 iff enabled)
839 * @return 0 on success, non-0 on error
841 int disorder_enabled(disorder_client
*c
, int *enabledp
) {
845 if((rc
= disorder_simple(c
, &v
, "enabled", (char *)0)))
847 return boolean("enabled", v
, enabledp
);
850 /** @brief Get the length of a track
852 * @param track Track name (UTF-8)
853 * @param valuep Where to store length in seconds
854 * @return 0 on success, non-0 on error
856 * If the length is unknown 0 is returned.
858 int disorder_length(disorder_client
*c
, const char *track
,
863 if((rc
= disorder_simple(c
, &value
, "length", track
, (char *)0)))
865 *valuep
= atol(value
);
869 /** @brief Search for tracks
871 * @param terms Search terms (UTF-8)
872 * @param vecp Where to store list (UTF-8)
873 * @param nvecp Where to store number of items, or NULL
874 * @return 0 on success, non-0 on error
876 int disorder_search(disorder_client
*c
, const char *terms
,
877 char ***vecp
, int *nvecp
) {
878 return disorder_simple_list(c
, vecp
, nvecp
, "search", terms
, (char *)0);
881 /** @brief Enable random play
883 * @return 0 on success, non-0 on error
885 int disorder_random_enable(disorder_client
*c
) {
886 return disorder_simple(c
, 0, "random-enable", (char *)0);
889 /** @brief Disable random play
891 * @return 0 on success, non-0 on error
893 int disorder_random_disable(disorder_client
*c
) {
894 return disorder_simple(c
, 0, "random-disable", (char *)0);
897 /** @brief Test whether random play is enabled
899 * @param enabledp Where to store result (non-0 iff enabled)
900 * @return 0 on success, non-0 on error
902 int disorder_random_enabled(disorder_client
*c
, int *enabledp
) {
906 if((rc
= disorder_simple(c
, &v
, "random-enabled", (char *)0)))
908 return boolean("random-enabled", v
, enabledp
);
911 /** @brief Get server stats
913 * @param vecp Where to store list (UTF-8)
914 * @param nvecp Where to store number of items, or NULL
915 * @return 0 on success, non-0 on error
917 int disorder_stats(disorder_client
*c
,
918 char ***vecp
, int *nvecp
) {
919 return disorder_simple_list(c
, vecp
, nvecp
, "stats", (char *)0);
922 /** @brief Set volume
924 * @param left New left channel value
925 * @param right New right channel value
926 * @return 0 on success, non-0 on error
928 int disorder_set_volume(disorder_client
*c
, int left
, int right
) {
931 if(byte_asprintf(&ls
, "%d", left
) < 0
932 || byte_asprintf(&rs
, "%d", right
) < 0)
934 return disorder_simple(c
, 0, "volume", ls
, rs
, (char *)0);
937 /** @brief Get volume
939 * @param left Where to store left channel value
940 * @param right Where to store right channel value
941 * @return 0 on success, non-0 on error
943 int disorder_get_volume(disorder_client
*c
, int *left
, int *right
) {
947 if((rc
= disorder_simple(c
, &r
, "volume", (char *)0)))
949 if(sscanf(r
, "%d %d", left
, right
) != 2) {
950 c
->last
= "malformed volume response";
951 error(0, "error parsing response to 'volume': '%s'", r
);
957 /** @brief Log to a sink
959 * @param s Sink to write log lines to
960 * @return 0 on success, non-0 on error
962 int disorder_log(disorder_client
*c
, struct sink
*s
) {
966 if((rc
= disorder_simple(c
, 0, "log", (char *)0)))
968 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0 && strcmp(l
, "."))
969 if(sink_printf(s
, "%s\n", l
) < 0) return -1;
970 if(ferror(c
->fpin
) || feof(c
->fpin
)) {
971 byte_xasprintf((char **)&c
->last
, "input error: %s",
972 ferror(c
->fpin
) ?
strerror(errno
) : "unexpxected EOF");
978 /** @brief Look up a track name part
980 * @param partp Where to store result (UTF-8)
981 * @param track Track name (UTF-8)
982 * @param context Context (usually "sort" or "display") (UTF-8)
983 * @param part Track part (UTF-8)
984 * @return 0 on success, non-0 on error
986 int disorder_part(disorder_client
*c
, char **partp
,
987 const char *track
, const char *context
, const char *part
) {
988 return dequote(disorder_simple(c
, partp
, "part",
989 track
, context
, part
, (char *)0), partp
);
992 /** @brief Resolve aliases
994 * @param trackp Where to store canonical name (UTF-8)
995 * @param track Track name (UTF-8)
996 * @return 0 on success, non-0 on error
998 int disorder_resolve(disorder_client
*c
, char **trackp
, const char *track
) {
999 return dequote(disorder_simple(c
, trackp
, "resolve", track
, (char *)0),
1003 /** @brief Pause the current track
1005 * @return 0 on success, non-0 on error
1007 int disorder_pause(disorder_client
*c
) {
1008 return disorder_simple(c
, 0, "pause", (char *)0);
1011 /** @brief Resume the current track
1013 * @return 0 on success, non-0 on error
1015 int disorder_resume(disorder_client
*c
) {
1016 return disorder_simple(c
, 0, "resume", (char *)0);
1019 /** @brief List all known tags
1021 * @param vecp Where to store list (UTF-8)
1022 * @param nvecp Where to store number of items, or NULL
1023 * @return 0 on success, non-0 on error
1025 int disorder_tags(disorder_client
*c
,
1026 char ***vecp
, int *nvecp
) {
1027 return disorder_simple_list(c
, vecp
, nvecp
, "tags", (char *)0);
1030 /** @brief List all known users
1032 * @param vecp Where to store list (UTF-8)
1033 * @param nvecp Where to store number of items, or NULL
1034 * @return 0 on success, non-0 on error
1036 int disorder_users(disorder_client
*c
,
1037 char ***vecp
, int *nvecp
) {
1038 return disorder_simple_list(c
, vecp
, nvecp
, "users", (char *)0);
1041 /** @brief Get recently added tracks
1043 * @param vecp Where to store pointer to list (UTF-8)
1044 * @param nvecp Where to store count
1045 * @param max Maximum tracks to fetch, or 0 for all available
1046 * @return 0 on success, non-0 on error
1048 int disorder_new_tracks(disorder_client
*c
,
1049 char ***vecp
, int *nvecp
,
1053 sprintf(limit
, "%d", max
);
1054 return disorder_simple_list(c
, vecp
, nvecp
, "new", limit
, (char *)0);
1057 /** @brief Set a global preference
1059 * @param key Preference name (UTF-8)
1060 * @param value Preference value (UTF-8)
1061 * @return 0 on success, non-0 on error
1063 int disorder_set_global(disorder_client
*c
,
1064 const char *key
, const char *value
) {
1065 return disorder_simple(c
, 0, "set-global", key
, value
, (char *)0);
1068 /** @brief Unset a global preference
1070 * @param key Preference name (UTF-8)
1071 * @return 0 on success, non-0 on error
1073 int disorder_unset_global(disorder_client
*c
, const char *key
) {
1074 return disorder_simple(c
, 0, "unset-global", key
, (char *)0);
1077 /** @brief Get a global preference
1079 * @param key Preference name (UTF-8)
1080 * @param valuep Where to store preference value (UTF-8)
1081 * @return 0 on success, non-0 on error
1083 int disorder_get_global(disorder_client
*c
, const char *key
, char **valuep
) {
1084 return dequote(disorder_simple(c
, valuep
, "get-global", key
, (char *)0),
1088 /** @brief Get server's RTP address information
1090 * @param addressp Where to store address (UTF-8)
1091 * @param portp Where to store port (UTF-8)
1092 * @return 0 on success, non-0 on error
1094 int disorder_rtp_address(disorder_client
*c
, char **addressp
, char **portp
) {
1099 if((rc
= disorder_simple(c
, &r
, "rtp-address", (char *)0)))
1101 vec
= split(r
, &n
, SPLIT_QUOTES
, 0, 0);
1103 c
->last
= "malformed RTP address";
1104 error(0, "malformed rtp-address reply");
1112 /** @brief Create a user
1114 * @param user Username
1115 * @param password Password
1116 * @param rights Initial rights or NULL to use default
1117 * @return 0 on success, non-0 on error
1119 int disorder_adduser(disorder_client
*c
,
1120 const char *user
, const char *password
,
1121 const char *rights
) {
1122 return disorder_simple(c
, 0, "adduser", user
, password
, rights
, (char *)0);
1125 /** @brief Delete a user
1127 * @param user Username
1128 * @return 0 on success, non-0 on error
1130 int disorder_deluser(disorder_client
*c
, const char *user
) {
1131 return disorder_simple(c
, 0, "deluser", user
, (char *)0);
1134 /** @brief Get user information
1136 * @param user Username
1137 * @param key Property name (UTF-8)
1138 * @param valuep Where to store value (UTF-8)
1139 * @return 0 on success, non-0 on error
1141 int disorder_userinfo(disorder_client
*c
, const char *user
, const char *key
,
1143 return dequote(disorder_simple(c
, valuep
, "userinfo", user
, key
, (char *)0),
1147 /** @brief Set user information
1149 * @param user Username
1150 * @param key Property name (UTF-8)
1151 * @param value New property value (UTF-8)
1152 * @return 0 on success, non-0 on error
1154 int disorder_edituser(disorder_client
*c
, const char *user
,
1155 const char *key
, const char *value
) {
1156 return disorder_simple(c
, 0, "edituser", user
, key
, value
, (char *)0);
1159 /** @brief Register a user
1161 * @param user Username
1162 * @param password Password
1163 * @param email Email address (UTF-8)
1164 * @param confirmp Where to store confirmation string
1165 * @return 0 on success, non-0 on error
1167 int disorder_register(disorder_client
*c
, const char *user
,
1168 const char *password
, const char *email
,
1170 return dequote(disorder_simple(c
, confirmp
, "register",
1171 user
, password
, email
, (char *)0),
1175 /** @brief Confirm a user
1177 * @param confirm Confirmation string
1178 * @return 0 on success, non-0 on error
1180 int disorder_confirm(disorder_client
*c
, const char *confirm
) {
1184 if(!(rc
= dequote(disorder_simple(c
, &u
, "confirm", confirm
, (char *)0),
1190 /** @brief Make a cookie for this login
1192 * @param cookiep Where to store cookie string
1193 * @return 0 on success, non-0 on error
1195 int disorder_make_cookie(disorder_client
*c
, char **cookiep
) {
1196 return dequote(disorder_simple(c
, cookiep
, "make-cookie", (char *)0),
1200 /** @brief Revoke the cookie used by this session
1202 * @return 0 on success, non-0 on error
1204 int disorder_revoke(disorder_client
*c
) {
1205 return disorder_simple(c
, 0, "revoke", (char *)0);
1208 /** @brief Request a password reminder email
1210 * @param user Username
1211 * @return 0 on success, non-0 on error
1213 int disorder_reminder(disorder_client
*c
, const char *user
) {
1214 return disorder_simple(c
, 0, "reminder", user
, (char *)0);
1217 /** @brief List scheduled events
1219 * @param idsp Where to put list of event IDs
1220 * @param nidsp Where to put count of event IDs, or NULL
1221 * @return 0 on success, non-0 on error
1223 int disorder_schedule_list(disorder_client
*c
, char ***idsp
, int *nidsp
) {
1224 return disorder_simple_list(c
, idsp
, nidsp
, "schedule-list", (char *)0);
1227 /** @brief Delete a scheduled event
1229 * @param id Event ID to delete
1230 * @return 0 on success, non-0 on error
1232 int disorder_schedule_del(disorder_client
*c
, const char *id
) {
1233 return disorder_simple(c
, 0, "schedule-del", id
, (char *)0);
1236 /** @brief Get details of a scheduled event
1238 * @param id Event ID
1239 * @param actiondatap Where to put details
1240 * @return 0 on success, non-0 on error
1242 int disorder_schedule_get(disorder_client
*c
, const char *id
,
1243 struct kvp
**actiondatap
) {
1244 char **lines
, **bits
;
1248 if((rc
= disorder_simple_list(c
, &lines
, NULL
,
1249 "schedule-get", id
, (char *)0)))
1252 if(!(bits
= split(*lines
++, &nbits
, SPLIT_QUOTES
, 0, 0))) {
1253 error(0, "invalid schedule-get reply: cannot split line");
1257 error(0, "invalid schedule-get reply: wrong number of fields");
1260 kvp_set(actiondatap
, bits
[0], bits
[1]);
1265 /** @brief Add a scheduled event
1267 * @param when When to trigger the event
1268 * @param priority Event priority ("normal" or "junk")
1269 * @param action What action to perform
1270 * @param ... Action-specific arguments
1271 * @return 0 on success, non-0 on error
1273 * For action @c "play" the next argument is the track.
1275 * For action @c "set-global" next argument is the global preference name
1276 * and the final argument the value to set it to, or (char *)0 to unset it.
1278 int disorder_schedule_add(disorder_client
*c
,
1280 const char *priority
,
1287 snprintf(when_str
, sizeof when_str
, "%lld", (long long)when
);
1288 va_start(ap
, action
);
1289 if(!strcmp(action
, "play"))
1290 rc
= disorder_simple(c
, 0, "schedule-add", when_str
, priority
,
1291 action
, va_arg(ap
, char *),
1293 else if(!strcmp(action
, "set-global")) {
1294 const char *key
= va_arg(ap
, char *);
1295 const char *value
= va_arg(ap
, char *);
1296 rc
= disorder_simple(c
, 0,"schedule-add", when_str
, priority
,
1300 fatal(0, "unknown action '%s'", action
);
1305 /** @brief Adopt a track
1307 * @param id Track ID to adopt
1308 * @return 0 on success, non-0 on error
1310 int disorder_adopt(disorder_client
*c
, const char *id
) {
1311 return disorder_simple(c
, 0, "adopt", id
, (char *)0);