d357a8ca67af471db37307bd33e70bebff37d7af
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
30 #include <sys/types.h>
31 #include <sys/socket.h>
32 #include <netinet/in.h>
44 #include "configuration.h"
51 #include "inputline.h"
58 #include "client-common.h"
63 /** @brief Client handle contents */
64 struct disorder_client
{
65 /** @brief Stream to read from */
67 /** @brief Stream to write to */
69 /** @brief Peer description */
71 /** @brief Username */
73 /** @brief Report errors to @c stderr */
75 /** @brief Last error string */
79 /** @brief Create a new client
80 * @param verbose If nonzero, write extra junk to stderr
81 * @return Pointer to new client
83 * You must call disorder_connect(), disorder_connect_user() or
84 * disorder_connect_cookie() to connect it. Use disorder_close() to
85 * dispose of the client when finished with it.
87 disorder_client
*disorder_new(int verbose
) {
88 disorder_client
*c
= xmalloc(sizeof (struct disorder_client
));
94 /** @brief Read a response line
96 * @param rp Where to store response, or NULL (UTF-8)
97 * @return Response code 0-999 or -1 on error
99 static int response(disorder_client
*c
, char **rp
) {
102 if(inputline(c
->ident
, c
->fpin
, &r
, '\n')) {
103 byte_xasprintf((char **)&c
->last
, "input error: %s", strerror(errno
));
106 D(("response: %s", r
));
109 if(r
[0] >= '0' && r
[0] <= '9'
110 && r
[1] >= '0' && r
[1] <= '9'
111 && r
[2] >= '0' && r
[2] <= '9'
114 return (r
[0] * 10 + r
[1]) * 10 + r
[2] - 111 * '0';
116 c
->last
= "invalid reply format";
117 error(0, "invalid reply format from %s", c
->ident
);
122 /** @brief Return last response string
124 * @return Last response string (UTF-8, English) or NULL
126 const char *disorder_last(disorder_client
*c
) {
130 /** @brief Read and partially parse a response
132 * @param rp Where to store response text (or NULL) (UTF-8)
133 * @return 0 on success, non-0 on error
135 * 5xx responses count as errors.
137 * @p rp will NOT be filled in for xx9 responses (where it is just
138 * commentary for a command where it would normally be meaningful).
140 * NB that the response will NOT be converted to the local encoding.
142 static int check_response(disorder_client
*c
, char **rp
) {
146 if((rc
= response(c
, &r
)) == -1)
148 else if(rc
/ 100 == 2) {
150 *rp
= (rc
% 10 == 9) ?
0 : xstrdup(r
+ 4);
154 error(0, "from %s: %s", c
->ident
, utf82mb(r
));
159 /** @brief Issue a command and parse a simple response
161 * @param rp Where to store result, or NULL
163 * @param ap Arguments (UTF-8), terminated by (char *)0
164 * @return 0 on success, non-0 on error
166 * 5xx responses count as errors.
168 * @p rp will NOT be filled in for xx9 responses (where it is just
169 * commentary for a command where it would normally be meaningful).
171 * NB that the response will NOT be converted to the local encoding
172 * nor will quotes be stripped. See dequote().
174 static int disorder_simple_v(disorder_client
*c
,
176 const char *cmd
, va_list ap
) {
181 c
->last
= "not connected";
182 error(0, "not connected to server");
187 dynstr_append_string(&d
, cmd
);
188 while((arg
= va_arg(ap
, const char *))) {
189 dynstr_append(&d
, ' ');
190 dynstr_append_string(&d
, quoteutf8(arg
));
192 dynstr_append(&d
, '\n');
193 dynstr_terminate(&d
);
194 D(("command: %s", d
.vec
));
195 if(fputs(d
.vec
, c
->fpout
) < 0 || fflush(c
->fpout
)) {
196 byte_xasprintf((char **)&c
->last
, "write error: %s", strerror(errno
));
197 error(errno
, "error writing to %s", c
->ident
);
201 return check_response(c
, rp
);
204 /** @brief Issue a command and parse a simple response
206 * @param rp Where to store result, or NULL (UTF-8)
208 * @return 0 on success, non-0 on error
210 * The remaining arguments are command arguments, terminated by (char
211 * *)0. They should be in UTF-8.
213 * 5xx responses count as errors.
215 * @p rp will NOT be filled in for xx9 responses (where it is just
216 * commentary for a command where it would normally be meaningful).
218 * NB that the response will NOT be converted to the local encoding
219 * nor will quotes be stripped. See dequote().
221 static int disorder_simple(disorder_client
*c
,
223 const char *cmd
, ...) {
228 ret
= disorder_simple_v(c
, rp
, cmd
, ap
);
233 /** @brief Dequote a result string
234 * @param rc 0 on success, non-0 on error
235 * @param rp Where result string is stored (UTF-8)
238 * This is used as a wrapper around disorder_simple() to dequote
241 static int dequote(int rc
, char **rp
) {
245 if((rr
= split(*rp
, 0, SPLIT_QUOTES
, 0, 0)) && *rr
) {
249 error(0, "invalid reply: %s", *rp
);
254 /** @brief Generic connection routine
256 * @param username Username to log in with or NULL
257 * @param password Password to log in with or NULL
258 * @param cookie Cookie to log in with or NULL
259 * @return 0 on success, non-0 on error
261 * @p cookie is tried first if not NULL. If it is NULL then @p
262 * username must not be. If @p username is not NULL then nor may @p
265 static int disorder_connect_generic(disorder_client
*c
,
266 const char *username
,
267 const char *password
,
268 const char *cookie
) {
269 int fd
= -1, fd2
= -1, nrvec
, rc
;
270 unsigned char *nonce
;
274 const char *protocol
, *algorithm
, *challenge
;
278 if((salen
= find_server(&sa
, &c
->ident
)) == (socklen_t
)-1)
280 c
->fpin
= c
->fpout
= 0;
281 if((fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0) {
282 byte_xasprintf((char **)&c
->last
, "socket: %s", strerror(errno
));
283 error(errno
, "error calling socket");
286 if(connect(fd
, sa
, salen
) < 0) {
287 byte_xasprintf((char **)&c
->last
, "connect: %s", strerror(errno
));
288 error(errno
, "error calling connect");
291 if((fd2
= dup(fd
)) < 0) {
292 byte_xasprintf((char **)&c
->last
, "dup: %s", strerror(errno
));
293 error(errno
, "error calling dup");
296 if(!(c
->fpin
= fdopen(fd
, "rb"))) {
297 byte_xasprintf((char **)&c
->last
, "fdopen: %s", strerror(errno
));
298 error(errno
, "error calling fdopen");
302 if(!(c
->fpout
= fdopen(fd2
, "wb"))) {
303 byte_xasprintf((char **)&c
->last
, "fdopen: %s", strerror(errno
));
304 error(errno
, "error calling fdopen");
308 if((rc
= disorder_simple(c
, &r
, 0, (const char *)0)))
310 if(!(rvec
= split(r
, &nrvec
, SPLIT_QUOTES
, 0, 0)))
313 c
->last
= "cannot parse server greeting";
314 error(0, "cannot parse server greeting %s", r
);
318 if(strcmp(protocol
, "2")) {
319 c
->last
= "unknown protocol version";
320 error(0, "unknown protocol version: %s", protocol
);
325 if(!(nonce
= unhex(challenge
, &nl
)))
328 if(!dequote(disorder_simple(c
, &c
->user
, "cookie", cookie
, (char *)0),
330 return 0; /* success */
332 c
->last
= "cookie failed and no username";
333 error(0, "cookie did not work and no username available");
337 if(!(res
= authhash(nonce
, nl
, password
, algorithm
))) {
338 c
->last
= "error computing authorization hash";
341 if((rc
= disorder_simple(c
, 0, "user", username
, res
, (char *)0)))
343 c
->user
= xstrdup(username
);
356 if(fd2
!= -1) close(fd2
);
357 if(fd
!= -1) close(fd
);
361 /** @brief Connect a client with a specified username and password
363 * @param username Username to log in with
364 * @param password Password to log in with
365 * @return 0 on success, non-0 on error
367 int disorder_connect_user(disorder_client
*c
,
368 const char *username
,
369 const char *password
) {
370 return disorder_connect_generic(c
,
376 /** @brief Connect a client
378 * @return 0 on success, non-0 on error
380 * The connection will use the username and password found in @ref
381 * config, or directly from the database if no password is found and
382 * the database is readable (usually only for root).
384 int disorder_connect(disorder_client
*c
) {
385 const char *username
, *password
;
387 if(!(username
= config
->username
)) {
388 c
->last
= "no username";
389 error(0, "no username configured");
392 password
= config
->password
;
393 /* Maybe we can read the database */
394 if(!password
&& trackdb_readable()) {
395 trackdb_init(TRACKDB_NO_RECOVER
|TRACKDB_NO_UPGRADE
);
396 trackdb_open(TRACKDB_READ_ONLY
);
397 password
= trackdb_get_password(username
);
402 c
->last
= "no password";
403 error(0, "no password configured");
406 return disorder_connect_generic(c
,
412 /** @brief Connect a client
414 * @param cookie Cookie to log in with, or NULL
415 * @return 0 on success, non-0 on error
417 * If @p cookie is NULL or does not work then we attempt to log in as
418 * guest instead (so when the cookie expires only an extra round trip
419 * is needed rathre than a complete new login).
421 int disorder_connect_cookie(disorder_client
*c
,
422 const char *cookie
) {
423 return disorder_connect_generic(c
,
429 /** @brief Close a client
431 * @return 0 on succcess, non-0 on errior
433 * The client is still closed even on error. It might well be
434 * appropriate to ignore the return value.
436 int disorder_close(disorder_client
*c
) {
440 if(fclose(c
->fpin
) < 0) {
441 byte_xasprintf((char **)&c
->last
, "fclose: %s", strerror(errno
));
442 error(errno
, "error calling fclose");
448 if(fclose(c
->fpout
) < 0) {
449 byte_xasprintf((char **)&c
->last
, "fclose: %s", strerror(errno
));
450 error(errno
, "error calling fclose");
460 /** @brief Play a track
462 * @param track Track to play (UTF-8)
463 * @return 0 on success, non-0 on error
465 int disorder_play(disorder_client
*c
, const char *track
) {
466 return disorder_simple(c
, 0, "play", track
, (char *)0);
469 /** @brief Remove a track
471 * @param track Track to remove (UTF-8)
472 * @return 0 on success, non-0 on error
474 int disorder_remove(disorder_client
*c
, const char *track
) {
475 return disorder_simple(c
, 0, "remove", track
, (char *)0);
478 /** @brief Move a track
480 * @param track Track to move (UTF-8)
481 * @param delta Distance to move by
482 * @return 0 on success, non-0 on error
484 int disorder_move(disorder_client
*c
, const char *track
, int delta
) {
487 byte_snprintf(d
, sizeof d
, "%d", delta
);
488 return disorder_simple(c
, 0, "move", track
, d
, (char *)0);
491 /** @brief Enable play
493 * @return 0 on success, non-0 on error
495 int disorder_enable(disorder_client
*c
) {
496 return disorder_simple(c
, 0, "enable", (char *)0);
499 /** @brief Disable play
501 * @return 0 on success, non-0 on error
503 int disorder_disable(disorder_client
*c
) {
504 return disorder_simple(c
, 0, "disable", (char *)0);
507 /** @brief Scratch the currently playing track
508 * @param id Playing track ID or NULL (UTF-8)
510 * @return 0 on success, non-0 on error
512 int disorder_scratch(disorder_client
*c
, const char *id
) {
513 return disorder_simple(c
, 0, "scratch", id
, (char *)0);
516 /** @brief Shut down the server
518 * @return 0 on success, non-0 on error
520 int disorder_shutdown(disorder_client
*c
) {
521 return disorder_simple(c
, 0, "shutdown", (char *)0);
524 /** @brief Make the server re-read its configuration
526 * @return 0 on success, non-0 on error
528 int disorder_reconfigure(disorder_client
*c
) {
529 return disorder_simple(c
, 0, "reconfigure", (char *)0);
532 /** @brief Rescan tracks
534 * @return 0 on success, non-0 on error
536 int disorder_rescan(disorder_client
*c
) {
537 return disorder_simple(c
, 0, "rescan", (char *)0);
540 /** @brief Get server version number
542 * @param rp Where to store version string (UTF-8)
543 * @return 0 on success, non-0 on error
545 int disorder_version(disorder_client
*c
, char **rp
) {
546 return dequote(disorder_simple(c
, rp
, "version", (char *)0), rp
);
549 static void client_error(const char *msg
,
550 void attribute((unused
)) *u
) {
551 error(0, "error parsing reply: %s", msg
);
554 /** @brief Get currently playing track
556 * @param qp Where to store track information
557 * @return 0 on success, non-0 on error
559 * @p qp gets NULL if no track is playing.
561 int disorder_playing(disorder_client
*c
, struct queue_entry
**qp
) {
563 struct queue_entry
*q
;
566 if((rc
= disorder_simple(c
, &r
, "playing", (char *)0)))
569 q
= xmalloc(sizeof *q
);
570 if(queue_unmarshall(q
, r
, client_error
, 0))
578 /** @brief Fetch the queue, recent list, etc */
579 static int disorder_somequeue(disorder_client
*c
,
580 const char *cmd
, struct queue_entry
**qp
) {
581 struct queue_entry
*qh
, **qt
= &qh
, *q
;
585 if((rc
= disorder_simple(c
, 0, cmd
, (char *)0)))
587 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0) {
588 if(!strcmp(l
, ".")) {
593 q
= xmalloc(sizeof *q
);
594 if(!queue_unmarshall(q
, l
, client_error
, 0)) {
599 if(ferror(c
->fpin
)) {
600 byte_xasprintf((char **)&c
->last
, "input error: %s", strerror(errno
));
601 error(errno
, "error reading %s", c
->ident
);
603 c
->last
= "input error: unexpxected EOF";
604 error(0, "error reading %s: unexpected EOF", c
->ident
);
609 /** @brief Get recently played tracks
611 * @param qp Where to store track information
612 * @return 0 on success, non-0 on error
614 * The last entry in the list is the most recently played track.
616 int disorder_recent(disorder_client
*c
, struct queue_entry
**qp
) {
617 return disorder_somequeue(c
, "recent", qp
);
622 * @param qp Where to store track information
623 * @return 0 on success, non-0 on error
625 * The first entry in the list will be played next.
627 int disorder_queue(disorder_client
*c
, struct queue_entry
**qp
) {
628 return disorder_somequeue(c
, "queue", qp
);
631 /** @brief Read a dot-stuffed list
633 * @param vecp Where to store list (UTF-8)
634 * @param nvecp Where to store number of items, or NULL
635 * @return 0 on success, non-0 on error
637 * The list will have a final NULL not counted in @p nvecp.
639 static int readlist(disorder_client
*c
, char ***vecp
, int *nvecp
) {
644 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0) {
645 if(!strcmp(l
, ".")) {
646 vector_terminate(&v
);
652 vector_append(&v
, l
+ (*l
== '.'));
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 Issue a comamnd and get a list response
666 * @param vecp Where to store list (UTF-8)
667 * @param nvecp Where to store number of items, or NULL
669 * @return 0 on success, non-0 on error
671 * The remaining arguments are command arguments, terminated by (char
672 * *)0. They should be in UTF-8.
674 * 5xx responses count as errors.
676 static int disorder_simple_list(disorder_client
*c
,
677 char ***vecp
, int *nvecp
,
678 const char *cmd
, ...) {
683 ret
= disorder_simple_v(c
, 0, cmd
, ap
);
686 return readlist(c
, vecp
, nvecp
);
689 /** @brief List directories below @p dir
691 * @param dir Directory to list, or NULL for root (UTF-8)
692 * @param re Regexp that results must match, or NULL (UTF-8)
693 * @param vecp Where to store list (UTF-8)
694 * @param nvecp Where to store number of items, or NULL
695 * @return 0 on success, non-0 on error
697 int disorder_directories(disorder_client
*c
, const char *dir
, const char *re
,
698 char ***vecp
, int *nvecp
) {
699 return disorder_simple_list(c
, vecp
, nvecp
, "dirs", dir
, re
, (char *)0);
702 /** @brief List files below @p dir
704 * @param dir Directory to list, or NULL for root (UTF-8)
705 * @param re Regexp that results must match, or NULL (UTF-8)
706 * @param vecp Where to store list (UTF-8)
707 * @param nvecp Where to store number of items, or NULL
708 * @return 0 on success, non-0 on error
710 int disorder_files(disorder_client
*c
, const char *dir
, const char *re
,
711 char ***vecp
, int *nvecp
) {
712 return disorder_simple_list(c
, vecp
, nvecp
, "files", dir
, re
, (char *)0);
715 /** @brief List files and directories below @p dir
717 * @param dir Directory to list, or NULL for root (UTF-8)
718 * @param re Regexp that results must match, or NULL (UTF-8)
719 * @param vecp Where to store list (UTF-8)
720 * @param nvecp Where to store number of items, or NULL
721 * @return 0 on success, non-0 on error
723 int disorder_allfiles(disorder_client
*c
, const char *dir
, const char *re
,
724 char ***vecp
, int *nvecp
) {
725 return disorder_simple_list(c
, vecp
, nvecp
, "allfiles", dir
, re
, (char *)0);
728 /** @brief Return the user we logged in with
730 * @return User name (owned by @p c, don't modify)
732 char *disorder_user(disorder_client
*c
) {
736 /** @brief Set a track preference
738 * @param track Track name (UTF-8)
739 * @param key Preference name (UTF-8)
740 * @param value Preference value (UTF-8)
741 * @return 0 on success, non-0 on error
743 int disorder_set(disorder_client
*c
, const char *track
,
744 const char *key
, const char *value
) {
745 return disorder_simple(c
, 0, "set", track
, key
, value
, (char *)0);
748 /** @brief Unset a track preference
750 * @param track Track name (UTF-8)
751 * @param key Preference name (UTF-8)
752 * @return 0 on success, non-0 on error
754 int disorder_unset(disorder_client
*c
, const char *track
,
756 return disorder_simple(c
, 0, "unset", track
, key
, (char *)0);
759 /** @brief Get a track preference
761 * @param track Track name (UTF-8)
762 * @param key Preference name (UTF-8)
763 * @param valuep Where to store preference value (UTF-8)
764 * @return 0 on success, non-0 on error
766 int disorder_get(disorder_client
*c
,
767 const char *track
, const char *key
, char **valuep
) {
768 return dequote(disorder_simple(c
, valuep
, "get", track
, key
, (char *)0),
772 static void pref_error_handler(const char *msg
,
773 void attribute((unused
)) *u
) {
774 error(0, "error handling 'prefs' reply: %s", msg
);
777 /** @brief Get all preferences for a trcak
779 * @param track Track name
780 * @param kp Where to store linked list of preferences
781 * @return 0 on success, non-0 on error
783 int disorder_prefs(disorder_client
*c
, const char *track
, struct kvp
**kp
) {
785 int nvec
, npvec
, n
, rc
;
788 if((rc
= disorder_simple_list(c
, &vec
, &nvec
, "prefs", track
, (char *)0)))
790 for(n
= 0; n
< nvec
; ++n
) {
791 if(!(pvec
= split(vec
[n
], &npvec
, SPLIT_QUOTES
, pref_error_handler
, 0)))
794 pref_error_handler("malformed response", 0);
797 *kp
= k
= xmalloc(sizeof *k
);
806 /** @brief Parse a boolean response
807 * @param cmd Command for use in error messsage
808 * @param value Result from server
809 * @param flagp Where to store result
810 * @return 0 on success, non-0 on error
812 static int boolean(const char *cmd
, const char *value
,
814 if(!strcmp(value
, "yes")) *flagp
= 1;
815 else if(!strcmp(value
, "no")) *flagp
= 0;
817 error(0, "malformed response to '%s'", cmd
);
823 /** @brief Test whether a track exists
825 * @param track Track name (UTF-8)
826 * @param existsp Where to store result (non-0 iff does exist)
827 * @return 0 on success, non-0 on error
829 int disorder_exists(disorder_client
*c
, const char *track
, int *existsp
) {
833 if((rc
= disorder_simple(c
, &v
, "exists", track
, (char *)0)))
835 return boolean("exists", v
, existsp
);
838 /** @brief Test whether playing is enabled
840 * @param enabledp Where to store result (non-0 iff enabled)
841 * @return 0 on success, non-0 on error
843 int disorder_enabled(disorder_client
*c
, int *enabledp
) {
847 if((rc
= disorder_simple(c
, &v
, "enabled", (char *)0)))
849 return boolean("enabled", v
, enabledp
);
852 /** @brief Get the length of a track
854 * @param track Track name (UTF-8)
855 * @param valuep Where to store length in seconds
856 * @return 0 on success, non-0 on error
858 * If the length is unknown 0 is returned.
860 int disorder_length(disorder_client
*c
, const char *track
,
865 if((rc
= disorder_simple(c
, &value
, "length", track
, (char *)0)))
867 *valuep
= atol(value
);
871 /** @brief Search for tracks
873 * @param terms Search terms (UTF-8)
874 * @param vecp Where to store list (UTF-8)
875 * @param nvecp Where to store number of items, or NULL
876 * @return 0 on success, non-0 on error
878 int disorder_search(disorder_client
*c
, const char *terms
,
879 char ***vecp
, int *nvecp
) {
880 return disorder_simple_list(c
, vecp
, nvecp
, "search", terms
, (char *)0);
883 /** @brief Enable random play
885 * @return 0 on success, non-0 on error
887 int disorder_random_enable(disorder_client
*c
) {
888 return disorder_simple(c
, 0, "random-enable", (char *)0);
891 /** @brief Disable random play
893 * @return 0 on success, non-0 on error
895 int disorder_random_disable(disorder_client
*c
) {
896 return disorder_simple(c
, 0, "random-disable", (char *)0);
899 /** @brief Test whether random play is enabled
901 * @param enabledp Where to store result (non-0 iff enabled)
902 * @return 0 on success, non-0 on error
904 int disorder_random_enabled(disorder_client
*c
, int *enabledp
) {
908 if((rc
= disorder_simple(c
, &v
, "random-enabled", (char *)0)))
910 return boolean("random-enabled", v
, enabledp
);
913 /** @brief Get server stats
915 * @param vecp Where to store list (UTF-8)
916 * @param nvecp Where to store number of items, or NULL
917 * @return 0 on success, non-0 on error
919 int disorder_stats(disorder_client
*c
,
920 char ***vecp
, int *nvecp
) {
921 return disorder_simple_list(c
, vecp
, nvecp
, "stats", (char *)0);
924 /** @brief Set volume
926 * @param left New left channel value
927 * @param right New right channel value
928 * @return 0 on success, non-0 on error
930 int disorder_set_volume(disorder_client
*c
, int left
, int right
) {
933 if(byte_asprintf(&ls
, "%d", left
) < 0
934 || byte_asprintf(&rs
, "%d", right
) < 0)
936 return disorder_simple(c
, 0, "volume", ls
, rs
, (char *)0);
939 /** @brief Get volume
941 * @param left Where to store left channel value
942 * @param right Where to store right channel value
943 * @return 0 on success, non-0 on error
945 int disorder_get_volume(disorder_client
*c
, int *left
, int *right
) {
949 if((rc
= disorder_simple(c
, &r
, "volume", (char *)0)))
951 if(sscanf(r
, "%d %d", left
, right
) != 2) {
952 c
->last
= "malformed volume response";
953 error(0, "error parsing response to 'volume': '%s'", r
);
959 /** @brief Log to a sink
961 * @param s Sink to write log lines to
962 * @return 0 on success, non-0 on error
964 int disorder_log(disorder_client
*c
, struct sink
*s
) {
968 if((rc
= disorder_simple(c
, 0, "log", (char *)0)))
970 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0 && strcmp(l
, "."))
971 if(sink_printf(s
, "%s\n", l
) < 0) return -1;
972 if(ferror(c
->fpin
) || feof(c
->fpin
)) {
973 byte_xasprintf((char **)&c
->last
, "input error: %s",
974 ferror(c
->fpin
) ?
strerror(errno
) : "unexpxected EOF");
980 /** @brief Look up a track name part
982 * @param partp Where to store result (UTF-8)
983 * @param track Track name (UTF-8)
984 * @param context Context (usually "sort" or "display") (UTF-8)
985 * @param part Track part (UTF-8)
986 * @return 0 on success, non-0 on error
988 int disorder_part(disorder_client
*c
, char **partp
,
989 const char *track
, const char *context
, const char *part
) {
990 return dequote(disorder_simple(c
, partp
, "part",
991 track
, context
, part
, (char *)0), partp
);
994 /** @brief Resolve aliases
996 * @param trackp Where to store canonical name (UTF-8)
997 * @param track Track name (UTF-8)
998 * @return 0 on success, non-0 on error
1000 int disorder_resolve(disorder_client
*c
, char **trackp
, const char *track
) {
1001 return dequote(disorder_simple(c
, trackp
, "resolve", track
, (char *)0),
1005 /** @brief Pause the current track
1007 * @return 0 on success, non-0 on error
1009 int disorder_pause(disorder_client
*c
) {
1010 return disorder_simple(c
, 0, "pause", (char *)0);
1013 /** @brief Resume the current track
1015 * @return 0 on success, non-0 on error
1017 int disorder_resume(disorder_client
*c
) {
1018 return disorder_simple(c
, 0, "resume", (char *)0);
1021 /** @brief List all known tags
1023 * @param vecp Where to store list (UTF-8)
1024 * @param nvecp Where to store number of items, or NULL
1025 * @return 0 on success, non-0 on error
1027 int disorder_tags(disorder_client
*c
,
1028 char ***vecp
, int *nvecp
) {
1029 return disorder_simple_list(c
, vecp
, nvecp
, "tags", (char *)0);
1032 /** @brief List all known users
1034 * @param vecp Where to store list (UTF-8)
1035 * @param nvecp Where to store number of items, or NULL
1036 * @return 0 on success, non-0 on error
1038 int disorder_users(disorder_client
*c
,
1039 char ***vecp
, int *nvecp
) {
1040 return disorder_simple_list(c
, vecp
, nvecp
, "users", (char *)0);
1043 /** @brief Get recently added tracks
1045 * @param vecp Where to store pointer to list (UTF-8)
1046 * @param nvecp Where to store count
1047 * @param max Maximum tracks to fetch, or 0 for all available
1048 * @return 0 on success, non-0 on error
1050 int disorder_new_tracks(disorder_client
*c
,
1051 char ***vecp
, int *nvecp
,
1055 sprintf(limit
, "%d", max
);
1056 return disorder_simple_list(c
, vecp
, nvecp
, "new", limit
, (char *)0);
1059 /** @brief Set a global preference
1061 * @param key Preference name (UTF-8)
1062 * @param value Preference value (UTF-8)
1063 * @return 0 on success, non-0 on error
1065 int disorder_set_global(disorder_client
*c
,
1066 const char *key
, const char *value
) {
1067 return disorder_simple(c
, 0, "set-global", key
, value
, (char *)0);
1070 /** @brief Unset a global preference
1072 * @param key Preference name (UTF-8)
1073 * @return 0 on success, non-0 on error
1075 int disorder_unset_global(disorder_client
*c
, const char *key
) {
1076 return disorder_simple(c
, 0, "unset-global", key
, (char *)0);
1079 /** @brief Get a global preference
1081 * @param key Preference name (UTF-8)
1082 * @param valuep Where to store preference value (UTF-8)
1083 * @return 0 on success, non-0 on error
1085 int disorder_get_global(disorder_client
*c
, const char *key
, char **valuep
) {
1086 return dequote(disorder_simple(c
, valuep
, "get-global", key
, (char *)0),
1090 /** @brief Get server's RTP address information
1092 * @param addressp Where to store address (UTF-8)
1093 * @param portp Where to store port (UTF-8)
1094 * @return 0 on success, non-0 on error
1096 int disorder_rtp_address(disorder_client
*c
, char **addressp
, char **portp
) {
1101 if((rc
= disorder_simple(c
, &r
, "rtp-address", (char *)0)))
1103 vec
= split(r
, &n
, SPLIT_QUOTES
, 0, 0);
1105 c
->last
= "malformed RTP address";
1106 error(0, "malformed rtp-address reply");
1114 /** @brief Create a user
1116 * @param user Username
1117 * @param password Password
1118 * @param rights Initial rights or NULL to use default
1119 * @return 0 on success, non-0 on error
1121 int disorder_adduser(disorder_client
*c
,
1122 const char *user
, const char *password
,
1123 const char *rights
) {
1124 return disorder_simple(c
, 0, "adduser", user
, password
, rights
, (char *)0);
1127 /** @brief Delete a user
1129 * @param user Username
1130 * @return 0 on success, non-0 on error
1132 int disorder_deluser(disorder_client
*c
, const char *user
) {
1133 return disorder_simple(c
, 0, "deluser", user
, (char *)0);
1136 /** @brief Get user information
1138 * @param user Username
1139 * @param key Property name (UTF-8)
1140 * @param valuep Where to store value (UTF-8)
1141 * @return 0 on success, non-0 on error
1143 int disorder_userinfo(disorder_client
*c
, const char *user
, const char *key
,
1145 return dequote(disorder_simple(c
, valuep
, "userinfo", user
, key
, (char *)0),
1149 /** @brief Set user information
1151 * @param user Username
1152 * @param key Property name (UTF-8)
1153 * @param value New property value (UTF-8)
1154 * @return 0 on success, non-0 on error
1156 int disorder_edituser(disorder_client
*c
, const char *user
,
1157 const char *key
, const char *value
) {
1158 return disorder_simple(c
, 0, "edituser", user
, key
, value
, (char *)0);
1161 /** @brief Register a user
1163 * @param user Username
1164 * @param password Password
1165 * @param email Email address (UTF-8)
1166 * @param confirmp Where to store confirmation string
1167 * @return 0 on success, non-0 on error
1169 int disorder_register(disorder_client
*c
, const char *user
,
1170 const char *password
, const char *email
,
1172 return dequote(disorder_simple(c
, confirmp
, "register",
1173 user
, password
, email
, (char *)0),
1177 /** @brief Confirm a user
1179 * @param confirm Confirmation string
1180 * @return 0 on success, non-0 on error
1182 int disorder_confirm(disorder_client
*c
, const char *confirm
) {
1186 if(!(rc
= dequote(disorder_simple(c
, &u
, "confirm", confirm
, (char *)0),
1192 /** @brief Make a cookie for this login
1194 * @param cookiep Where to store cookie string
1195 * @return 0 on success, non-0 on error
1197 int disorder_make_cookie(disorder_client
*c
, char **cookiep
) {
1198 return dequote(disorder_simple(c
, cookiep
, "make-cookie", (char *)0),
1202 /** @brief Revoke the cookie used by this session
1204 * @return 0 on success, non-0 on error
1206 int disorder_revoke(disorder_client
*c
) {
1207 return disorder_simple(c
, 0, "revoke", (char *)0);
1210 /** @brief Request a password reminder email
1212 * @param user Username
1213 * @return 0 on success, non-0 on error
1215 int disorder_reminder(disorder_client
*c
, const char *user
) {
1216 return disorder_simple(c
, 0, "reminder", user
, (char *)0);
1219 /** @brief List scheduled events
1221 * @param idsp Where to put list of event IDs
1222 * @param nidsp Where to put count of event IDs, or NULL
1223 * @return 0 on success, non-0 on error
1225 int disorder_schedule_list(disorder_client
*c
, char ***idsp
, int *nidsp
) {
1226 return disorder_simple_list(c
, idsp
, nidsp
, "schedule-list", (char *)0);
1229 /** @brief Delete a scheduled event
1231 * @param id Event ID to delete
1232 * @return 0 on success, non-0 on error
1234 int disorder_schedule_del(disorder_client
*c
, const char *id
) {
1235 return disorder_simple(c
, 0, "schedule-del", id
, (char *)0);
1238 /** @brief Get details of a scheduled event
1240 * @param id Event ID
1241 * @param actiondatap Where to put details
1242 * @return 0 on success, non-0 on error
1244 int disorder_schedule_get(disorder_client
*c
, const char *id
,
1245 struct kvp
**actiondatap
) {
1246 char **lines
, **bits
;
1250 if((rc
= disorder_simple_list(c
, &lines
, NULL
,
1251 "schedule-get", id
, (char *)0)))
1254 if(!(bits
= split(*lines
++, &nbits
, SPLIT_QUOTES
, 0, 0))) {
1255 error(0, "invalid schedule-get reply: cannot split line");
1259 error(0, "invalid schedule-get reply: wrong number of fields");
1262 kvp_set(actiondatap
, bits
[0], bits
[1]);
1267 /** @brief Add a scheduled event
1269 * @param when When to trigger the event
1270 * @param priority Event priority ("normal" or "junk")
1271 * @param action What action to perform
1272 * @param ... Action-specific arguments
1273 * @return 0 on success, non-0 on error
1275 * For action @c "play" the next argument is the track.
1277 * For action @c "set-global" next argument is the global preference name
1278 * and the final argument the value to set it to, or (char *)0 to unset it.
1280 int disorder_schedule_add(disorder_client
*c
,
1282 const char *priority
,
1289 snprintf(when_str
, sizeof when_str
, "%lld", (long long)when
);
1290 va_start(ap
, action
);
1291 if(!strcmp(action
, "play"))
1292 rc
= disorder_simple(c
, 0, "schedule-add", when_str
, priority
,
1293 action
, va_arg(ap
, char *),
1295 else if(!strcmp(action
, "set-global")) {
1296 const char *key
= va_arg(ap
, char *);
1297 const char *value
= va_arg(ap
, char *);
1298 rc
= disorder_simple(c
, 0,"schedule-add", when_str
, priority
,
1302 fatal(0, "unknown action '%s'", action
);