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 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * 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, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file lib/client.c
21 * @brief Simple C client
23 * See @ref lib/eclient.c for an asynchronous-capable client
29 #include <sys/types.h>
30 #include <sys/socket.h>
31 #include <netinet/in.h>
46 #include "inputline.h"
53 #include "client-common.h"
58 /** @brief Client handle contents */
59 struct disorder_client
{
60 /** @brief Stream to read from */
62 /** @brief Stream to write to */
64 /** @brief Peer description */
66 /** @brief Username */
68 /** @brief Report errors to @c stderr */
70 /** @brief Last error string */
74 /** @brief Create a new client
75 * @param verbose If nonzero, write extra junk to stderr
76 * @return Pointer to new client
78 * You must call disorder_connect(), disorder_connect_user() or
79 * disorder_connect_cookie() to connect it. Use disorder_close() to
80 * dispose of the client when finished with it.
82 disorder_client
*disorder_new(int verbose
) {
83 disorder_client
*c
= xmalloc(sizeof (struct disorder_client
));
89 /** @brief Read a response line
91 * @param rp Where to store response, or NULL (UTF-8)
92 * @return Response code 0-999 or -1 on error
94 static int response(disorder_client
*c
, char **rp
) {
97 if(inputline(c
->ident
, c
->fpin
, &r
, '\n')) {
98 byte_xasprintf((char **)&c
->last
, "input error: %s", strerror(errno
));
101 D(("response: %s", r
));
104 if(r
[0] >= '0' && r
[0] <= '9'
105 && r
[1] >= '0' && r
[1] <= '9'
106 && r
[2] >= '0' && r
[2] <= '9'
109 return (r
[0] * 10 + r
[1]) * 10 + r
[2] - 111 * '0';
111 c
->last
= "invalid reply format";
112 error(0, "invalid reply format from %s", c
->ident
);
117 /** @brief Return last response string
119 * @return Last response string (UTF-8, English) or NULL
121 const char *disorder_last(disorder_client
*c
) {
125 /** @brief Read and partially parse a response
127 * @param rp Where to store response text (or NULL) (UTF-8)
128 * @return 0 on success, non-0 on error
130 * 5xx responses count as errors.
132 * @p rp will NOT be filled in for xx9 responses (where it is just
133 * commentary for a command where it would normally be meaningful).
135 * NB that the response will NOT be converted to the local encoding.
137 static int check_response(disorder_client
*c
, char **rp
) {
141 if((rc
= response(c
, &r
)) == -1)
143 else if(rc
/ 100 == 2) {
145 *rp
= (rc
% 10 == 9) ?
0 : xstrdup(r
+ 4);
149 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 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)
207 for(nbody
= 0; body
[nbody
]; ++nbody
)
209 for(int n
= 0; n
< nbody
; ++n
) {
210 if(body
[n
][0] == '.')
211 if(fputc('.', c
->fpout
) < 0)
213 if(fputs(body
[n
], c
->fpout
) < 0)
215 if(fputc('\n', c
->fpout
) < 0)
218 if(fputs(".\n", c
->fpout
) < 0)
224 return check_response(c
, rp
);
226 byte_xasprintf((char **)&c
->last
, "write error: %s", strerror(errno
));
227 error(errno
, "error writing to %s", c
->ident
);
231 /** @brief Issue a command and parse a simple response
233 * @param rp Where to store result, or NULL (UTF-8)
235 * @return 0 on success, non-0 on error
237 * The remaining arguments are command arguments, terminated by (char
238 * *)0. They should be in UTF-8.
240 * 5xx responses count as errors.
242 * @p rp will NOT be filled in for xx9 responses (where it is just
243 * commentary for a command where it would normally be meaningful).
245 * NB that the response will NOT be converted to the local encoding
246 * nor will quotes be stripped. See dequote().
248 static int disorder_simple(disorder_client
*c
,
250 const char *cmd
, ...) {
255 ret
= disorder_simple_v(c
, rp
, cmd
, 0, 0, ap
);
260 /** @brief Issue a command with a body and parse a simple response
262 * @param rp Where to store result, or NULL (UTF-8)
263 * @param body Pointer to body
264 * @param nbody Size of body
266 * @return 0 on success, non-0 on error
268 * See disorder_simple().
270 static int disorder_simple_body(disorder_client
*c
,
272 char **body
, int nbody
,
273 const char *cmd
, ...) {
278 ret
= disorder_simple_v(c
, rp
, cmd
, body
, nbody
, ap
);
283 /** @brief Dequote a result string
284 * @param rc 0 on success, non-0 on error
285 * @param rp Where result string is stored (UTF-8)
288 * This is used as a wrapper around disorder_simple() to dequote
291 static int dequote(int rc
, char **rp
) {
295 if((rr
= split(*rp
, 0, SPLIT_QUOTES
, 0, 0)) && *rr
) {
299 error(0, "invalid reply: %s", *rp
);
304 /** @brief Generic connection routine
305 * @param conf Configuration to follow
307 * @param username Username to log in with or NULL
308 * @param password Password to log in with or NULL
309 * @param cookie Cookie to log in with or NULL
310 * @return 0 on success, non-0 on error
312 * @p cookie is tried first if not NULL. If it is NULL then @p
313 * username must not be. If @p username is not NULL then nor may @p
316 int disorder_connect_generic(struct config
*conf
,
318 const char *username
,
319 const char *password
,
320 const char *cookie
) {
321 int fd
= -1, fd2
= -1, nrvec
, rc
;
322 unsigned char *nonce
;
326 const char *protocol
, *algorithm
, *challenge
;
330 if((salen
= find_server(conf
, &sa
, &c
->ident
)) == (socklen_t
)-1)
332 c
->fpin
= c
->fpout
= 0;
333 if((fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0) {
334 byte_xasprintf((char **)&c
->last
, "socket: %s", strerror(errno
));
335 error(errno
, "error calling socket");
338 if(connect(fd
, sa
, salen
) < 0) {
339 byte_xasprintf((char **)&c
->last
, "connect: %s", strerror(errno
));
340 error(errno
, "error calling connect");
343 if((fd2
= dup(fd
)) < 0) {
344 byte_xasprintf((char **)&c
->last
, "dup: %s", strerror(errno
));
345 error(errno
, "error calling dup");
348 if(!(c
->fpin
= fdopen(fd
, "rb"))) {
349 byte_xasprintf((char **)&c
->last
, "fdopen: %s", strerror(errno
));
350 error(errno
, "error calling fdopen");
354 if(!(c
->fpout
= fdopen(fd2
, "wb"))) {
355 byte_xasprintf((char **)&c
->last
, "fdopen: %s", strerror(errno
));
356 error(errno
, "error calling fdopen");
360 if((rc
= disorder_simple(c
, &r
, 0, (const char *)0)))
362 if(!(rvec
= split(r
, &nrvec
, SPLIT_QUOTES
, 0, 0)))
365 c
->last
= "cannot parse server greeting";
366 error(0, "cannot parse server greeting %s", r
);
370 if(strcmp(protocol
, "2")) {
371 c
->last
= "unknown protocol version";
372 error(0, "unknown protocol version: %s", protocol
);
377 if(!(nonce
= unhex(challenge
, &nl
)))
380 if(!dequote(disorder_simple(c
, &c
->user
, "cookie", cookie
, (char *)0),
382 return 0; /* success */
384 c
->last
= "cookie failed and no username";
385 error(0, "cookie did not work and no username available");
389 if(!(res
= authhash(nonce
, nl
, password
, algorithm
))) {
390 c
->last
= "error computing authorization hash";
393 if((rc
= disorder_simple(c
, 0, "user", username
, res
, (char *)0)))
395 c
->user
= xstrdup(username
);
408 if(fd2
!= -1) close(fd2
);
409 if(fd
!= -1) close(fd
);
413 /** @brief Connect a client with a specified username and password
415 * @param username Username to log in with
416 * @param password Password to log in with
417 * @return 0 on success, non-0 on error
419 int disorder_connect_user(disorder_client
*c
,
420 const char *username
,
421 const char *password
) {
422 return disorder_connect_generic(config
,
429 /** @brief Connect a client
431 * @return 0 on success, non-0 on error
433 * The connection will use the username and password found in @ref
434 * config, or directly from the database if no password is found and
435 * the database is readable (usually only for root).
437 int disorder_connect(disorder_client
*c
) {
438 const char *username
, *password
;
440 if(!(username
= config
->username
)) {
441 c
->last
= "no username";
442 error(0, "no username configured");
445 password
= config
->password
;
446 /* Maybe we can read the database */
447 if(!password
&& trackdb_readable()) {
448 trackdb_init(TRACKDB_NO_RECOVER
|TRACKDB_NO_UPGRADE
);
449 trackdb_open(TRACKDB_READ_ONLY
);
450 password
= trackdb_get_password(username
);
455 c
->last
= "no password";
456 error(0, "no password configured");
459 return disorder_connect_generic(config
,
466 /** @brief Connect a client
468 * @param cookie Cookie to log in with, or NULL
469 * @return 0 on success, non-0 on error
471 * If @p cookie is NULL or does not work then we attempt to log in as
472 * guest instead (so when the cookie expires only an extra round trip
473 * is needed rathre than a complete new login).
475 int disorder_connect_cookie(disorder_client
*c
,
476 const char *cookie
) {
477 return disorder_connect_generic(config
,
484 /** @brief Close a client
486 * @return 0 on succcess, non-0 on errior
488 * The client is still closed even on error. It might well be
489 * appropriate to ignore the return value.
491 int disorder_close(disorder_client
*c
) {
495 if(fclose(c
->fpin
) < 0) {
496 byte_xasprintf((char **)&c
->last
, "fclose: %s", strerror(errno
));
497 error(errno
, "error calling fclose");
503 if(fclose(c
->fpout
) < 0) {
504 byte_xasprintf((char **)&c
->last
, "fclose: %s", strerror(errno
));
505 error(errno
, "error calling fclose");
515 /** @brief Play a track
517 * @param track Track to play (UTF-8)
518 * @return 0 on success, non-0 on error
520 int disorder_play(disorder_client
*c
, const char *track
) {
521 return disorder_simple(c
, 0, "play", track
, (char *)0);
524 /** @brief Remove a track
526 * @param track Track to remove (UTF-8)
527 * @return 0 on success, non-0 on error
529 int disorder_remove(disorder_client
*c
, const char *track
) {
530 return disorder_simple(c
, 0, "remove", track
, (char *)0);
533 /** @brief Move a track
535 * @param track Track to move (UTF-8)
536 * @param delta Distance to move by
537 * @return 0 on success, non-0 on error
539 int disorder_move(disorder_client
*c
, const char *track
, int delta
) {
542 byte_snprintf(d
, sizeof d
, "%d", delta
);
543 return disorder_simple(c
, 0, "move", track
, d
, (char *)0);
546 /** @brief Enable play
548 * @return 0 on success, non-0 on error
550 int disorder_enable(disorder_client
*c
) {
551 return disorder_simple(c
, 0, "enable", (char *)0);
554 /** @brief Disable play
556 * @return 0 on success, non-0 on error
558 int disorder_disable(disorder_client
*c
) {
559 return disorder_simple(c
, 0, "disable", (char *)0);
562 /** @brief Scratch the currently playing track
563 * @param id Playing track ID or NULL (UTF-8)
565 * @return 0 on success, non-0 on error
567 int disorder_scratch(disorder_client
*c
, const char *id
) {
568 return disorder_simple(c
, 0, "scratch", id
, (char *)0);
571 /** @brief Shut down the server
573 * @return 0 on success, non-0 on error
575 int disorder_shutdown(disorder_client
*c
) {
576 return disorder_simple(c
, 0, "shutdown", (char *)0);
579 /** @brief Make the server re-read its configuration
581 * @return 0 on success, non-0 on error
583 int disorder_reconfigure(disorder_client
*c
) {
584 return disorder_simple(c
, 0, "reconfigure", (char *)0);
587 /** @brief Rescan tracks
589 * @return 0 on success, non-0 on error
591 int disorder_rescan(disorder_client
*c
) {
592 return disorder_simple(c
, 0, "rescan", (char *)0);
595 /** @brief Get server version number
597 * @param rp Where to store version string (UTF-8)
598 * @return 0 on success, non-0 on error
600 int disorder_version(disorder_client
*c
, char **rp
) {
601 return dequote(disorder_simple(c
, rp
, "version", (char *)0), rp
);
604 static void client_error(const char *msg
,
605 void attribute((unused
)) *u
) {
606 error(0, "error parsing reply: %s", msg
);
609 /** @brief Get currently playing track
611 * @param qp Where to store track information
612 * @return 0 on success, non-0 on error
614 * @p qp gets NULL if no track is playing.
616 int disorder_playing(disorder_client
*c
, struct queue_entry
**qp
) {
618 struct queue_entry
*q
;
621 if((rc
= disorder_simple(c
, &r
, "playing", (char *)0)))
624 q
= xmalloc(sizeof *q
);
625 if(queue_unmarshall(q
, r
, client_error
, 0))
633 /** @brief Fetch the queue, recent list, etc */
634 static int disorder_somequeue(disorder_client
*c
,
635 const char *cmd
, struct queue_entry
**qp
) {
636 struct queue_entry
*qh
, **qt
= &qh
, *q
;
640 if((rc
= disorder_simple(c
, 0, cmd
, (char *)0)))
642 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0) {
643 if(!strcmp(l
, ".")) {
648 q
= xmalloc(sizeof *q
);
649 if(!queue_unmarshall(q
, l
, client_error
, 0)) {
654 if(ferror(c
->fpin
)) {
655 byte_xasprintf((char **)&c
->last
, "input error: %s", strerror(errno
));
656 error(errno
, "error reading %s", c
->ident
);
658 c
->last
= "input error: unexpxected EOF";
659 error(0, "error reading %s: unexpected EOF", c
->ident
);
664 /** @brief Get recently played tracks
666 * @param qp Where to store track information
667 * @return 0 on success, non-0 on error
669 * The last entry in the list is the most recently played track.
671 int disorder_recent(disorder_client
*c
, struct queue_entry
**qp
) {
672 return disorder_somequeue(c
, "recent", qp
);
677 * @param qp Where to store track information
678 * @return 0 on success, non-0 on error
680 * The first entry in the list will be played next.
682 int disorder_queue(disorder_client
*c
, struct queue_entry
**qp
) {
683 return disorder_somequeue(c
, "queue", qp
);
686 /** @brief Read a dot-stuffed list
688 * @param vecp Where to store list (UTF-8)
689 * @param nvecp Where to store number of items, or NULL
690 * @return 0 on success, non-0 on error
692 * The list will have a final NULL not counted in @p nvecp.
694 static int readlist(disorder_client
*c
, char ***vecp
, int *nvecp
) {
699 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0) {
700 if(!strcmp(l
, ".")) {
701 vector_terminate(&v
);
707 vector_append(&v
, l
+ (*l
== '.'));
709 if(ferror(c
->fpin
)) {
710 byte_xasprintf((char **)&c
->last
, "input error: %s", strerror(errno
));
711 error(errno
, "error reading %s", c
->ident
);
713 c
->last
= "input error: unexpxected EOF";
714 error(0, "error reading %s: unexpected EOF", c
->ident
);
719 /** @brief Issue a comamnd and get a list response
721 * @param vecp Where to store list (UTF-8)
722 * @param nvecp Where to store number of items, or NULL
724 * @return 0 on success, non-0 on error
726 * The remaining arguments are command arguments, terminated by (char
727 * *)0. They should be in UTF-8.
729 * 5xx responses count as errors.
731 * See disorder_simple().
733 static int disorder_simple_list(disorder_client
*c
,
734 char ***vecp
, int *nvecp
,
735 const char *cmd
, ...) {
740 ret
= disorder_simple_v(c
, 0, cmd
, 0, 0, ap
);
743 return readlist(c
, vecp
, nvecp
);
746 /** @brief List directories below @p dir
748 * @param dir Directory to list, or NULL for root (UTF-8)
749 * @param re Regexp that results must match, or NULL (UTF-8)
750 * @param vecp Where to store list (UTF-8)
751 * @param nvecp Where to store number of items, or NULL
752 * @return 0 on success, non-0 on error
754 int disorder_directories(disorder_client
*c
, const char *dir
, const char *re
,
755 char ***vecp
, int *nvecp
) {
756 return disorder_simple_list(c
, vecp
, nvecp
, "dirs", dir
, re
, (char *)0);
759 /** @brief List files below @p dir
761 * @param dir Directory to list, or NULL for root (UTF-8)
762 * @param re Regexp that results must match, or NULL (UTF-8)
763 * @param vecp Where to store list (UTF-8)
764 * @param nvecp Where to store number of items, or NULL
765 * @return 0 on success, non-0 on error
767 int disorder_files(disorder_client
*c
, const char *dir
, const char *re
,
768 char ***vecp
, int *nvecp
) {
769 return disorder_simple_list(c
, vecp
, nvecp
, "files", dir
, re
, (char *)0);
772 /** @brief List files and directories below @p dir
774 * @param dir Directory to list, or NULL for root (UTF-8)
775 * @param re Regexp that results must match, or NULL (UTF-8)
776 * @param vecp Where to store list (UTF-8)
777 * @param nvecp Where to store number of items, or NULL
778 * @return 0 on success, non-0 on error
780 int disorder_allfiles(disorder_client
*c
, const char *dir
, const char *re
,
781 char ***vecp
, int *nvecp
) {
782 return disorder_simple_list(c
, vecp
, nvecp
, "allfiles", dir
, re
, (char *)0);
785 /** @brief Return the user we logged in with
787 * @return User name (owned by @p c, don't modify)
789 char *disorder_user(disorder_client
*c
) {
793 /** @brief Set a track preference
795 * @param track Track name (UTF-8)
796 * @param key Preference name (UTF-8)
797 * @param value Preference value (UTF-8)
798 * @return 0 on success, non-0 on error
800 int disorder_set(disorder_client
*c
, const char *track
,
801 const char *key
, const char *value
) {
802 return disorder_simple(c
, 0, "set", track
, key
, value
, (char *)0);
805 /** @brief Unset a track preference
807 * @param track Track name (UTF-8)
808 * @param key Preference name (UTF-8)
809 * @return 0 on success, non-0 on error
811 int disorder_unset(disorder_client
*c
, const char *track
,
813 return disorder_simple(c
, 0, "unset", track
, key
, (char *)0);
816 /** @brief Get a track preference
818 * @param track Track name (UTF-8)
819 * @param key Preference name (UTF-8)
820 * @param valuep Where to store preference value (UTF-8)
821 * @return 0 on success, non-0 on error
823 int disorder_get(disorder_client
*c
,
824 const char *track
, const char *key
, char **valuep
) {
825 return dequote(disorder_simple(c
, valuep
, "get", track
, key
, (char *)0),
829 static void pref_error_handler(const char *msg
,
830 void attribute((unused
)) *u
) {
831 error(0, "error handling 'prefs' reply: %s", msg
);
834 /** @brief Get all preferences for a trcak
836 * @param track Track name
837 * @param kp Where to store linked list of preferences
838 * @return 0 on success, non-0 on error
840 int disorder_prefs(disorder_client
*c
, const char *track
, struct kvp
**kp
) {
842 int nvec
, npvec
, n
, rc
;
845 if((rc
= disorder_simple_list(c
, &vec
, &nvec
, "prefs", track
, (char *)0)))
847 for(n
= 0; n
< nvec
; ++n
) {
848 if(!(pvec
= split(vec
[n
], &npvec
, SPLIT_QUOTES
, pref_error_handler
, 0)))
851 pref_error_handler("malformed response", 0);
854 *kp
= k
= xmalloc(sizeof *k
);
863 /** @brief Parse a boolean response
864 * @param cmd Command for use in error messsage
865 * @param value Result from server
866 * @param flagp Where to store result
867 * @return 0 on success, non-0 on error
869 static int boolean(const char *cmd
, const char *value
,
871 if(!strcmp(value
, "yes")) *flagp
= 1;
872 else if(!strcmp(value
, "no")) *flagp
= 0;
874 error(0, "malformed response to '%s'", cmd
);
880 /** @brief Test whether a track exists
882 * @param track Track name (UTF-8)
883 * @param existsp Where to store result (non-0 iff does exist)
884 * @return 0 on success, non-0 on error
886 int disorder_exists(disorder_client
*c
, const char *track
, int *existsp
) {
890 if((rc
= disorder_simple(c
, &v
, "exists", track
, (char *)0)))
892 return boolean("exists", v
, existsp
);
895 /** @brief Test whether playing is enabled
897 * @param enabledp Where to store result (non-0 iff enabled)
898 * @return 0 on success, non-0 on error
900 int disorder_enabled(disorder_client
*c
, int *enabledp
) {
904 if((rc
= disorder_simple(c
, &v
, "enabled", (char *)0)))
906 return boolean("enabled", v
, enabledp
);
909 /** @brief Get the length of a track
911 * @param track Track name (UTF-8)
912 * @param valuep Where to store length in seconds
913 * @return 0 on success, non-0 on error
915 * If the length is unknown 0 is returned.
917 int disorder_length(disorder_client
*c
, const char *track
,
922 if((rc
= disorder_simple(c
, &value
, "length", track
, (char *)0)))
924 *valuep
= atol(value
);
928 /** @brief Search for tracks
930 * @param terms Search terms (UTF-8)
931 * @param vecp Where to store list (UTF-8)
932 * @param nvecp Where to store number of items, or NULL
933 * @return 0 on success, non-0 on error
935 int disorder_search(disorder_client
*c
, const char *terms
,
936 char ***vecp
, int *nvecp
) {
937 return disorder_simple_list(c
, vecp
, nvecp
, "search", terms
, (char *)0);
940 /** @brief Enable random play
942 * @return 0 on success, non-0 on error
944 int disorder_random_enable(disorder_client
*c
) {
945 return disorder_simple(c
, 0, "random-enable", (char *)0);
948 /** @brief Disable random play
950 * @return 0 on success, non-0 on error
952 int disorder_random_disable(disorder_client
*c
) {
953 return disorder_simple(c
, 0, "random-disable", (char *)0);
956 /** @brief Test whether random play is enabled
958 * @param enabledp Where to store result (non-0 iff enabled)
959 * @return 0 on success, non-0 on error
961 int disorder_random_enabled(disorder_client
*c
, int *enabledp
) {
965 if((rc
= disorder_simple(c
, &v
, "random-enabled", (char *)0)))
967 return boolean("random-enabled", v
, enabledp
);
970 /** @brief Get server stats
972 * @param vecp Where to store list (UTF-8)
973 * @param nvecp Where to store number of items, or NULL
974 * @return 0 on success, non-0 on error
976 int disorder_stats(disorder_client
*c
,
977 char ***vecp
, int *nvecp
) {
978 return disorder_simple_list(c
, vecp
, nvecp
, "stats", (char *)0);
981 /** @brief Set volume
983 * @param left New left channel value
984 * @param right New right channel value
985 * @return 0 on success, non-0 on error
987 int disorder_set_volume(disorder_client
*c
, int left
, int right
) {
990 if(byte_asprintf(&ls
, "%d", left
) < 0
991 || byte_asprintf(&rs
, "%d", right
) < 0)
993 return disorder_simple(c
, 0, "volume", ls
, rs
, (char *)0);
996 /** @brief Get volume
998 * @param left Where to store left channel value
999 * @param right Where to store right channel value
1000 * @return 0 on success, non-0 on error
1002 int disorder_get_volume(disorder_client
*c
, int *left
, int *right
) {
1006 if((rc
= disorder_simple(c
, &r
, "volume", (char *)0)))
1008 if(sscanf(r
, "%d %d", left
, right
) != 2) {
1009 c
->last
= "malformed volume response";
1010 error(0, "error parsing response to 'volume': '%s'", r
);
1016 /** @brief Log to a sink
1018 * @param s Sink to write log lines to
1019 * @return 0 on success, non-0 on error
1021 int disorder_log(disorder_client
*c
, struct sink
*s
) {
1025 if((rc
= disorder_simple(c
, 0, "log", (char *)0)))
1027 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0 && strcmp(l
, "."))
1028 if(sink_printf(s
, "%s\n", l
) < 0) return -1;
1029 if(ferror(c
->fpin
) || feof(c
->fpin
)) {
1030 byte_xasprintf((char **)&c
->last
, "input error: %s",
1031 ferror(c
->fpin
) ?
strerror(errno
) : "unexpxected EOF");
1037 /** @brief Look up a track name part
1039 * @param partp Where to store result (UTF-8)
1040 * @param track Track name (UTF-8)
1041 * @param context Context (usually "sort" or "display") (UTF-8)
1042 * @param part Track part (UTF-8)
1043 * @return 0 on success, non-0 on error
1045 int disorder_part(disorder_client
*c
, char **partp
,
1046 const char *track
, const char *context
, const char *part
) {
1047 return dequote(disorder_simple(c
, partp
, "part",
1048 track
, context
, part
, (char *)0), partp
);
1051 /** @brief Resolve aliases
1053 * @param trackp Where to store canonical name (UTF-8)
1054 * @param track Track name (UTF-8)
1055 * @return 0 on success, non-0 on error
1057 int disorder_resolve(disorder_client
*c
, char **trackp
, const char *track
) {
1058 return dequote(disorder_simple(c
, trackp
, "resolve", track
, (char *)0),
1062 /** @brief Pause the current track
1064 * @return 0 on success, non-0 on error
1066 int disorder_pause(disorder_client
*c
) {
1067 return disorder_simple(c
, 0, "pause", (char *)0);
1070 /** @brief Resume the current track
1072 * @return 0 on success, non-0 on error
1074 int disorder_resume(disorder_client
*c
) {
1075 return disorder_simple(c
, 0, "resume", (char *)0);
1078 /** @brief List all known tags
1080 * @param vecp Where to store list (UTF-8)
1081 * @param nvecp Where to store number of items, or NULL
1082 * @return 0 on success, non-0 on error
1084 int disorder_tags(disorder_client
*c
,
1085 char ***vecp
, int *nvecp
) {
1086 return disorder_simple_list(c
, vecp
, nvecp
, "tags", (char *)0);
1089 /** @brief List all known users
1091 * @param vecp Where to store list (UTF-8)
1092 * @param nvecp Where to store number of items, or NULL
1093 * @return 0 on success, non-0 on error
1095 int disorder_users(disorder_client
*c
,
1096 char ***vecp
, int *nvecp
) {
1097 return disorder_simple_list(c
, vecp
, nvecp
, "users", (char *)0);
1100 /** @brief Get recently added tracks
1102 * @param vecp Where to store pointer to list (UTF-8)
1103 * @param nvecp Where to store count
1104 * @param max Maximum tracks to fetch, or 0 for all available
1105 * @return 0 on success, non-0 on error
1107 int disorder_new_tracks(disorder_client
*c
,
1108 char ***vecp
, int *nvecp
,
1112 sprintf(limit
, "%d", max
);
1113 return disorder_simple_list(c
, vecp
, nvecp
, "new", limit
, (char *)0);
1116 /** @brief Set a global preference
1118 * @param key Preference name (UTF-8)
1119 * @param value Preference value (UTF-8)
1120 * @return 0 on success, non-0 on error
1122 int disorder_set_global(disorder_client
*c
,
1123 const char *key
, const char *value
) {
1124 return disorder_simple(c
, 0, "set-global", key
, value
, (char *)0);
1127 /** @brief Unset a global preference
1129 * @param key Preference name (UTF-8)
1130 * @return 0 on success, non-0 on error
1132 int disorder_unset_global(disorder_client
*c
, const char *key
) {
1133 return disorder_simple(c
, 0, "unset-global", key
, (char *)0);
1136 /** @brief Get a global preference
1138 * @param key Preference name (UTF-8)
1139 * @param valuep Where to store preference value (UTF-8)
1140 * @return 0 on success, non-0 on error
1142 int disorder_get_global(disorder_client
*c
, const char *key
, char **valuep
) {
1143 return dequote(disorder_simple(c
, valuep
, "get-global", key
, (char *)0),
1147 /** @brief Get server's RTP address information
1149 * @param addressp Where to store address (UTF-8)
1150 * @param portp Where to store port (UTF-8)
1151 * @return 0 on success, non-0 on error
1153 int disorder_rtp_address(disorder_client
*c
, char **addressp
, char **portp
) {
1158 if((rc
= disorder_simple(c
, &r
, "rtp-address", (char *)0)))
1160 vec
= split(r
, &n
, SPLIT_QUOTES
, 0, 0);
1162 c
->last
= "malformed RTP address";
1163 error(0, "malformed rtp-address reply");
1171 /** @brief Create a user
1173 * @param user Username
1174 * @param password Password
1175 * @param rights Initial rights or NULL to use default
1176 * @return 0 on success, non-0 on error
1178 int disorder_adduser(disorder_client
*c
,
1179 const char *user
, const char *password
,
1180 const char *rights
) {
1181 return disorder_simple(c
, 0, "adduser", user
, password
, rights
, (char *)0);
1184 /** @brief Delete a user
1186 * @param user Username
1187 * @return 0 on success, non-0 on error
1189 int disorder_deluser(disorder_client
*c
, const char *user
) {
1190 return disorder_simple(c
, 0, "deluser", user
, (char *)0);
1193 /** @brief Get user information
1195 * @param user Username
1196 * @param key Property name (UTF-8)
1197 * @param valuep Where to store value (UTF-8)
1198 * @return 0 on success, non-0 on error
1200 int disorder_userinfo(disorder_client
*c
, const char *user
, const char *key
,
1202 return dequote(disorder_simple(c
, valuep
, "userinfo", user
, key
, (char *)0),
1206 /** @brief Set user information
1208 * @param user Username
1209 * @param key Property name (UTF-8)
1210 * @param value New property value (UTF-8)
1211 * @return 0 on success, non-0 on error
1213 int disorder_edituser(disorder_client
*c
, const char *user
,
1214 const char *key
, const char *value
) {
1215 return disorder_simple(c
, 0, "edituser", user
, key
, value
, (char *)0);
1218 /** @brief Register a user
1220 * @param user Username
1221 * @param password Password
1222 * @param email Email address (UTF-8)
1223 * @param confirmp Where to store confirmation string
1224 * @return 0 on success, non-0 on error
1226 int disorder_register(disorder_client
*c
, const char *user
,
1227 const char *password
, const char *email
,
1229 return dequote(disorder_simple(c
, confirmp
, "register",
1230 user
, password
, email
, (char *)0),
1234 /** @brief Confirm a user
1236 * @param confirm Confirmation string
1237 * @return 0 on success, non-0 on error
1239 int disorder_confirm(disorder_client
*c
, const char *confirm
) {
1243 if(!(rc
= dequote(disorder_simple(c
, &u
, "confirm", confirm
, (char *)0),
1249 /** @brief Make a cookie for this login
1251 * @param cookiep Where to store cookie string
1252 * @return 0 on success, non-0 on error
1254 int disorder_make_cookie(disorder_client
*c
, char **cookiep
) {
1255 return dequote(disorder_simple(c
, cookiep
, "make-cookie", (char *)0),
1259 /** @brief Revoke the cookie used by this session
1261 * @return 0 on success, non-0 on error
1263 int disorder_revoke(disorder_client
*c
) {
1264 return disorder_simple(c
, 0, "revoke", (char *)0);
1267 /** @brief Request a password reminder email
1269 * @param user Username
1270 * @return 0 on success, non-0 on error
1272 int disorder_reminder(disorder_client
*c
, const char *user
) {
1273 return disorder_simple(c
, 0, "reminder", user
, (char *)0);
1276 /** @brief List scheduled events
1278 * @param idsp Where to put list of event IDs
1279 * @param nidsp Where to put count of event IDs, or NULL
1280 * @return 0 on success, non-0 on error
1282 int disorder_schedule_list(disorder_client
*c
, char ***idsp
, int *nidsp
) {
1283 return disorder_simple_list(c
, idsp
, nidsp
, "schedule-list", (char *)0);
1286 /** @brief Delete a scheduled event
1288 * @param id Event ID to delete
1289 * @return 0 on success, non-0 on error
1291 int disorder_schedule_del(disorder_client
*c
, const char *id
) {
1292 return disorder_simple(c
, 0, "schedule-del", id
, (char *)0);
1295 /** @brief Get details of a scheduled event
1297 * @param id Event ID
1298 * @param actiondatap Where to put details
1299 * @return 0 on success, non-0 on error
1301 int disorder_schedule_get(disorder_client
*c
, const char *id
,
1302 struct kvp
**actiondatap
) {
1303 char **lines
, **bits
;
1307 if((rc
= disorder_simple_list(c
, &lines
, NULL
,
1308 "schedule-get", id
, (char *)0)))
1311 if(!(bits
= split(*lines
++, &nbits
, SPLIT_QUOTES
, 0, 0))) {
1312 error(0, "invalid schedule-get reply: cannot split line");
1316 error(0, "invalid schedule-get reply: wrong number of fields");
1319 kvp_set(actiondatap
, bits
[0], bits
[1]);
1324 /** @brief Add a scheduled event
1326 * @param when When to trigger the event
1327 * @param priority Event priority ("normal" or "junk")
1328 * @param action What action to perform
1329 * @param ... Action-specific arguments
1330 * @return 0 on success, non-0 on error
1332 * For action @c "play" the next argument is the track.
1334 * For action @c "set-global" next argument is the global preference name
1335 * and the final argument the value to set it to, or (char *)0 to unset it.
1337 int disorder_schedule_add(disorder_client
*c
,
1339 const char *priority
,
1346 snprintf(when_str
, sizeof when_str
, "%lld", (long long)when
);
1347 va_start(ap
, action
);
1348 if(!strcmp(action
, "play"))
1349 rc
= disorder_simple(c
, 0, "schedule-add", when_str
, priority
,
1350 action
, va_arg(ap
, char *),
1352 else if(!strcmp(action
, "set-global")) {
1353 const char *key
= va_arg(ap
, char *);
1354 const char *value
= va_arg(ap
, char *);
1355 rc
= disorder_simple(c
, 0,"schedule-add", when_str
, priority
,
1359 fatal(0, "unknown action '%s'", action
);
1364 /** @brief Delete a playlist
1366 * @param playlist Playlist to delete
1367 * @return 0 on success, non-0 on error
1369 int disorder_playlist_delete(disorder_client
*c
,
1370 const char *playlist
) {
1371 return disorder_simple(c
, 0, "playlist-delete", playlist
, (char *)0);
1374 /** @brief Get the contents of a playlist
1376 * @param playlist Playlist to get
1377 * @param tracksp Where to put list of tracks
1378 * @param ntracksp Where to put count of tracks
1379 * @return 0 on success, non-0 on error
1381 int disorder_playlist_get(disorder_client
*c
, const char *playlist
,
1382 char ***tracksp
, int *ntracksp
) {
1383 return disorder_simple_list(c
, tracksp
, ntracksp
,
1384 "playlist-get", playlist
, (char *)0);
1387 /** @brief List all readable playlists
1389 * @param playlistsp Where to put list of playlists
1390 * @param nplaylistsp Where to put count of playlists
1391 * @return 0 on success, non-0 on error
1393 int disorder_playlists(disorder_client
*c
,
1394 char ***playlistsp
, int *nplaylistsp
) {
1395 return disorder_simple_list(c
, playlistsp
, nplaylistsp
,
1396 "playlists", (char *)0);
1399 /** @brief Get the sharing status of a playlist
1401 * @param playlist Playlist to inspect
1402 * @param sharep Where to put sharing status
1403 * @return 0 on success, non-0 on error
1405 * Possible @p sharep values are @c public, @c private and @c shared.
1407 int disorder_playlist_get_share(disorder_client
*c
, const char *playlist
,
1409 return disorder_simple(c
, sharep
,
1410 "playlist-get-share", playlist
, (char *)0);
1413 /** @brief Get the sharing status of a playlist
1415 * @param playlist Playlist to modify
1416 * @param share New sharing status
1417 * @return 0 on success, non-0 on error
1419 * Possible @p share values are @c public, @c private and @c shared.
1421 int disorder_playlist_set_share(disorder_client
*c
, const char *playlist
,
1422 const char *share
) {
1423 return disorder_simple(c
, 0,
1424 "playlist-set-share", playlist
, share
, (char *)0);
1427 /** @brief Lock a playlist for modifications
1429 * @param playlist Playlist to lock
1430 * @return 0 on success, non-0 on error
1432 int disorder_playlist_lock(disorder_client
*c
, const char *playlist
) {
1433 return disorder_simple(c
, 0,
1434 "playlist-lock", playlist
, (char *)0);
1437 /** @brief Unlock the locked playlist
1439 * @return 0 on success, non-0 on error
1441 int disorder_playlist_unlock(disorder_client
*c
) {
1442 return disorder_simple(c
, 0,
1443 "playlist-unlock", (char *)0);
1446 /** @brief Set the contents of a playlst
1448 * @param playlist Playlist to modify
1449 * @param tracks List of tracks
1450 * @param ntracks Length of @p tracks (or -1 to count up to the first NULL)
1451 * @return 0 on success, non-0 on error
1453 int disorder_playlist_set(disorder_client
*c
,
1454 const char *playlist
,
1457 return disorder_simple_body(c
, 0, tracks
, ntracks
,
1458 "playlist-set", playlist
, (char *)0);