2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 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"
62 /** @brief Client handle contents */
63 struct disorder_client
{
64 /** @brief Stream to read from */
66 /** @brief Stream to write to */
68 /** @brief Peer description */
70 /** @brief Username */
72 /** @brief Report errors to @c stderr */
74 /** @brief Last error string */
78 /** @brief Create a new client
79 * @param verbose If nonzero, write extra junk to stderr
80 * @return Pointer to new client
82 * You must call disorder_connect(), disorder_connect_user() or
83 * disorder_connect_cookie() to connect it. Use disorder_close() to
84 * dispose of the client when finished with it.
86 disorder_client
*disorder_new(int verbose
) {
87 disorder_client
*c
= xmalloc(sizeof (struct disorder_client
));
93 /** @brief Read a response line
95 * @param rp Where to store response, or NULL (UTF-8)
96 * @return Response code 0-999 or -1 on error
98 static int response(disorder_client
*c
, char **rp
) {
101 if(inputline(c
->ident
, c
->fpin
, &r
, '\n'))
103 D(("response: %s", r
));
106 if(r
[0] >= '0' && r
[0] <= '9'
107 && r
[1] >= '0' && r
[1] <= '9'
108 && r
[2] >= '0' && r
[2] <= '9'
111 return (r
[0] * 10 + r
[1]) * 10 + r
[2] - 111 * '0';
113 error(0, "invalid reply format from %s", c
->ident
);
118 /** @brief Return last response string
120 * @return Last response string (UTF-8, English) or NULL
122 const char *disorder_last(disorder_client
*c
) {
126 /** @brief Read and partially parse a response
128 * @param rp Where to store response text (or NULL) (UTF-8)
129 * @return 0 on success, non-0 on error
131 * 5xx responses count as errors.
133 * @p rp will NOT be filled in for xx9 responses (where it is just
134 * commentary for a command where it would normally be meaningful).
136 * NB that the response will NOT be converted to the local encoding.
138 static int check_response(disorder_client
*c
, char **rp
) {
142 if((rc
= response(c
, &r
)) == -1)
144 else if(rc
/ 100 == 2) {
146 *rp
= (rc
% 10 == 9) ?
0 : xstrdup(r
+ 4);
150 error(0, "from %s: %s", c
->ident
, utf82mb(r
));
155 /** @brief Issue a command and parse a simple response
157 * @param rp Where to store result, or NULL
159 * @param ap Arguments (UTF-8), terminated by (char *)0
160 * @return 0 on success, non-0 on error
162 * 5xx responses count as errors.
164 * @p rp will NOT be filled in for xx9 responses (where it is just
165 * commentary for a command where it would normally be meaningful).
167 * NB that the response will NOT be converted to the local encoding
168 * nor will quotes be stripped. See dequote().
170 static int disorder_simple_v(disorder_client
*c
,
172 const char *cmd
, va_list ap
) {
177 error(0, "not connected to server");
182 dynstr_append_string(&d
, cmd
);
183 while((arg
= va_arg(ap
, const char *))) {
184 dynstr_append(&d
, ' ');
185 dynstr_append_string(&d
, quoteutf8(arg
));
187 dynstr_append(&d
, '\n');
188 dynstr_terminate(&d
);
189 D(("command: %s", d
.vec
));
190 if(fputs(d
.vec
, c
->fpout
) < 0 || fflush(c
->fpout
)) {
191 error(errno
, "error writing to %s", c
->ident
);
195 return check_response(c
, rp
);
198 /** @brief Issue a command and parse a simple response
200 * @param rp Where to store result, or NULL (UTF-8)
202 * @return 0 on success, non-0 on error
204 * The remaining arguments are command arguments, terminated by (char
205 * *)0. They should be in UTF-8.
207 * 5xx responses count as errors.
209 * @p rp will NOT be filled in for xx9 responses (where it is just
210 * commentary for a command where it would normally be meaningful).
212 * NB that the response will NOT be converted to the local encoding
213 * nor will quotes be stripped. See dequote().
215 static int disorder_simple(disorder_client
*c
,
217 const char *cmd
, ...) {
222 ret
= disorder_simple_v(c
, rp
, cmd
, ap
);
227 /** @brief Dequote a result string
228 * @param rc 0 on success, non-0 on error
229 * @param rp Where result string is stored (UTF-8)
232 * This is used as a wrapper around disorder_simple() to dequote
235 static int dequote(int rc
, char **rp
) {
239 if((rr
= split(*rp
, 0, SPLIT_QUOTES
, 0, 0)) && *rr
) {
243 error(0, "invalid reply: %s", *rp
);
248 /** @brief Generic connection routine
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 static int disorder_connect_generic(disorder_client
*c
,
260 const char *username
,
261 const char *password
,
262 const char *cookie
) {
263 int fd
= -1, fd2
= -1, nrvec
, rc
;
264 unsigned char *nonce
;
268 const char *protocol
, *algorithm
, *challenge
;
272 if((salen
= find_server(&sa
, &c
->ident
)) == (socklen_t
)-1)
274 c
->fpin
= c
->fpout
= 0;
275 if((fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0) {
276 error(errno
, "error calling socket");
279 if(connect(fd
, sa
, salen
) < 0) {
280 error(errno
, "error calling connect");
283 if((fd2
= dup(fd
)) < 0) {
284 error(errno
, "error calling dup");
287 if(!(c
->fpin
= fdopen(fd
, "rb"))) {
288 error(errno
, "error calling fdopen");
292 if(!(c
->fpout
= fdopen(fd2
, "wb"))) {
293 error(errno
, "error calling fdopen");
297 if((rc
= disorder_simple(c
, &r
, 0, (const char *)0)))
299 if(!(rvec
= split(r
, &nrvec
, SPLIT_QUOTES
, 0, 0)))
302 error(0, "cannot parse server greeting %s", r
);
306 if(strcmp(protocol
, "2")) {
307 error(0, "unknown protocol version: %s", protocol
);
312 if(!(nonce
= unhex(challenge
, &nl
)))
315 if(!dequote(disorder_simple(c
, &c
->user
, "cookie", cookie
, (char *)0),
317 return 0; /* success */
319 error(0, "cookie did not work and no username available");
323 if(!(res
= authhash(nonce
, nl
, password
, algorithm
))) goto error
;
324 if((rc
= disorder_simple(c
, 0, "user", username
, res
, (char *)0)))
326 c
->user
= xstrdup(username
);
339 if(fd2
!= -1) close(fd2
);
340 if(fd
!= -1) close(fd
);
344 /** @brief Connect a client with a specified username and password
346 * @param username Username to log in with
347 * @param password Password to log in with
348 * @return 0 on success, non-0 on error
350 int disorder_connect_user(disorder_client
*c
,
351 const char *username
,
352 const char *password
) {
353 return disorder_connect_generic(c
,
359 /** @brief Connect a client
361 * @return 0 on success, non-0 on error
363 * The connection will use the username and password found in @ref
364 * config, or directly from the database if no password is found and
365 * the database is readable (usually only for root).
367 int disorder_connect(disorder_client
*c
) {
368 const char *username
, *password
;
370 if(!(username
= config
->username
)) {
371 error(0, "no username configured");
374 password
= config
->password
;
376 /* Maybe we can read the database */
377 /* TODO failure to open the database should not be fatal */
378 trackdb_init(TRACKDB_NO_RECOVER
|TRACKDB_NO_UPGRADE
);
379 trackdb_open(TRACKDB_READ_ONLY
);
380 password
= trackdb_get_password(username
);
385 error(0, "no password configured");
388 return disorder_connect_generic(c
,
394 /** @brief Connect a client
396 * @param cookie Cookie to log in with, or NULL
397 * @return 0 on success, non-0 on error
399 * If @p cookie is NULL or does not work then we attempt to log in as
400 * guest instead (so when the cookie expires only an extra round trip
401 * is needed rathre than a complete new login).
403 int disorder_connect_cookie(disorder_client
*c
,
404 const char *cookie
) {
405 return disorder_connect_generic(c
,
411 /** @brief Close a client
413 * @return 0 on succcess, non-0 on errior
415 * The client is still closed even on error. It might well be
416 * appropriate to ignore the return value.
418 int disorder_close(disorder_client
*c
) {
422 if(fclose(c
->fpin
) < 0) {
423 error(errno
, "error calling fclose");
429 if(fclose(c
->fpout
) < 0) {
430 error(errno
, "error calling fclose");
440 /** @brief Play a track
442 * @param track Track to play (UTF-8)
443 * @return 0 on success, non-0 on error
445 int disorder_play(disorder_client
*c
, const char *track
) {
446 return disorder_simple(c
, 0, "play", track
, (char *)0);
449 /** @brief Remove a track
451 * @param track Track to remove (UTF-8)
452 * @return 0 on success, non-0 on error
454 int disorder_remove(disorder_client
*c
, const char *track
) {
455 return disorder_simple(c
, 0, "remove", track
, (char *)0);
458 /** @brief Move a track
460 * @param track Track to move (UTF-8)
461 * @param delta Distance to move by
462 * @return 0 on success, non-0 on error
464 int disorder_move(disorder_client
*c
, const char *track
, int delta
) {
467 byte_snprintf(d
, sizeof d
, "%d", delta
);
468 return disorder_simple(c
, 0, "move", track
, d
, (char *)0);
471 /** @brief Enable play
473 * @return 0 on success, non-0 on error
475 int disorder_enable(disorder_client
*c
) {
476 return disorder_simple(c
, 0, "enable", (char *)0);
479 /** @brief Disable play
481 * @return 0 on success, non-0 on error
483 int disorder_disable(disorder_client
*c
) {
484 return disorder_simple(c
, 0, "disable", (char *)0);
487 /** @brief Scratch the currently playing track
488 * @param id Playing track ID or NULL (UTF-8)
490 * @return 0 on success, non-0 on error
492 int disorder_scratch(disorder_client
*c
, const char *id
) {
493 return disorder_simple(c
, 0, "scratch", id
, (char *)0);
496 /** @brief Shut down the server
498 * @return 0 on success, non-0 on error
500 int disorder_shutdown(disorder_client
*c
) {
501 return disorder_simple(c
, 0, "shutdown", (char *)0);
504 /** @brief Make the server re-read its configuration
506 * @return 0 on success, non-0 on error
508 int disorder_reconfigure(disorder_client
*c
) {
509 return disorder_simple(c
, 0, "reconfigure", (char *)0);
512 /** @brief Rescan tracks
514 * @return 0 on success, non-0 on error
516 int disorder_rescan(disorder_client
*c
) {
517 return disorder_simple(c
, 0, "rescan", (char *)0);
520 /** @brief Get server version number
522 * @param rp Where to store version string (UTF-8)
523 * @return 0 on success, non-0 on error
525 int disorder_version(disorder_client
*c
, char **rp
) {
526 return dequote(disorder_simple(c
, rp
, "version", (char *)0), rp
);
529 static void client_error(const char *msg
,
530 void attribute((unused
)) *u
) {
531 error(0, "error parsing reply: %s", msg
);
534 /** @brief Get currently playing track
536 * @param qp Where to store track information
537 * @return 0 on success, non-0 on error
539 * @p qp gets NULL if no track is playing.
541 int disorder_playing(disorder_client
*c
, struct queue_entry
**qp
) {
543 struct queue_entry
*q
;
546 if((rc
= disorder_simple(c
, &r
, "playing", (char *)0)))
549 q
= xmalloc(sizeof *q
);
550 if(queue_unmarshall(q
, r
, client_error
, 0))
558 /** @brief Fetch the queue, recent list, etc */
559 static int disorder_somequeue(disorder_client
*c
,
560 const char *cmd
, struct queue_entry
**qp
) {
561 struct queue_entry
*qh
, **qt
= &qh
, *q
;
565 if((rc
= disorder_simple(c
, 0, cmd
, (char *)0)))
567 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0) {
568 if(!strcmp(l
, ".")) {
573 q
= xmalloc(sizeof *q
);
574 if(!queue_unmarshall(q
, l
, client_error
, 0)) {
580 error(errno
, "error reading %s", c
->ident
);
582 error(0, "error reading %s: unexpected EOF", c
->ident
);
586 /** @brief Get recently played tracks
588 * @param qp Where to store track information
589 * @return 0 on success, non-0 on error
591 * The last entry in the list is the most recently played track.
593 int disorder_recent(disorder_client
*c
, struct queue_entry
**qp
) {
594 return disorder_somequeue(c
, "recent", qp
);
599 * @param qp Where to store track information
600 * @return 0 on success, non-0 on error
602 * The first entry in the list will be played next.
604 int disorder_queue(disorder_client
*c
, struct queue_entry
**qp
) {
605 return disorder_somequeue(c
, "queue", qp
);
608 /** @brief Read a dot-stuffed list
610 * @param vecp Where to store list (UTF-8)
611 * @param nvecp Where to store number of items, or NULL
612 * @return 0 on success, non-0 on error
614 * The list will have a final NULL not counted in @p nvecp.
616 static int readlist(disorder_client
*c
, char ***vecp
, int *nvecp
) {
621 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0) {
622 if(!strcmp(l
, ".")) {
623 vector_terminate(&v
);
629 vector_append(&v
, l
+ (*l
== '.'));
632 error(errno
, "error reading %s", c
->ident
);
634 error(0, "error reading %s: unexpected EOF", c
->ident
);
638 /** @brief Issue a comamnd and get a list response
640 * @param vecp Where to store list (UTF-8)
641 * @param nvecp Where to store number of items, or NULL
643 * @return 0 on success, non-0 on error
645 * The remaining arguments are command arguments, terminated by (char
646 * *)0. They should be in UTF-8.
648 * 5xx responses count as errors.
650 static int disorder_simple_list(disorder_client
*c
,
651 char ***vecp
, int *nvecp
,
652 const char *cmd
, ...) {
657 ret
= disorder_simple_v(c
, 0, cmd
, ap
);
660 return readlist(c
, vecp
, nvecp
);
663 /** @brief List directories below @p dir
665 * @param dir Directory to list, or NULL for root (UTF-8)
666 * @param re Regexp that results must match, or NULL (UTF-8)
667 * @param vecp Where to store list (UTF-8)
668 * @param nvecp Where to store number of items, or NULL
669 * @return 0 on success, non-0 on error
671 int disorder_directories(disorder_client
*c
, const char *dir
, const char *re
,
672 char ***vecp
, int *nvecp
) {
673 return disorder_simple_list(c
, vecp
, nvecp
, "dirs", dir
, re
, (char *)0);
676 /** @brief List files below @p dir
678 * @param dir Directory to list, or NULL for root (UTF-8)
679 * @param re Regexp that results must match, or NULL (UTF-8)
680 * @param vecp Where to store list (UTF-8)
681 * @param nvecp Where to store number of items, or NULL
682 * @return 0 on success, non-0 on error
684 int disorder_files(disorder_client
*c
, const char *dir
, const char *re
,
685 char ***vecp
, int *nvecp
) {
686 return disorder_simple_list(c
, vecp
, nvecp
, "files", dir
, re
, (char *)0);
689 /** @brief List files and 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_allfiles(disorder_client
*c
, const char *dir
, const char *re
,
698 char ***vecp
, int *nvecp
) {
699 return disorder_simple_list(c
, vecp
, nvecp
, "allfiles", dir
, re
, (char *)0);
702 /** @brief Return the user we logged in with
704 * @return User name (owned by @p c, don't modify)
706 char *disorder_user(disorder_client
*c
) {
710 /** @brief Set a track preference
712 * @param track Track name (UTF-8)
713 * @param key Preference name (UTF-8)
714 * @param value Preference value (UTF-8)
715 * @return 0 on success, non-0 on error
717 int disorder_set(disorder_client
*c
, const char *track
,
718 const char *key
, const char *value
) {
719 return disorder_simple(c
, 0, "set", track
, key
, value
, (char *)0);
722 /** @brief Unset a track preference
724 * @param track Track name (UTF-8)
725 * @param key Preference name (UTF-8)
726 * @return 0 on success, non-0 on error
728 int disorder_unset(disorder_client
*c
, const char *track
,
730 return disorder_simple(c
, 0, "unset", track
, key
, (char *)0);
733 /** @brief Get a track preference
735 * @param track Track name (UTF-8)
736 * @param key Preference name (UTF-8)
737 * @param valuep Where to store preference value (UTF-8)
738 * @return 0 on success, non-0 on error
740 int disorder_get(disorder_client
*c
,
741 const char *track
, const char *key
, char **valuep
) {
742 return dequote(disorder_simple(c
, valuep
, "get", track
, key
, (char *)0),
746 static void pref_error_handler(const char *msg
,
747 void attribute((unused
)) *u
) {
748 error(0, "error handling 'prefs' reply: %s", msg
);
751 /** @brief Get all preferences for a trcak
753 * @param track Track name
754 * @param kp Where to store linked list of preferences
755 * @return 0 on success, non-0 on error
757 int disorder_prefs(disorder_client
*c
, const char *track
, struct kvp
**kp
) {
759 int nvec
, npvec
, n
, rc
;
762 if((rc
= disorder_simple_list(c
, &vec
, &nvec
, "prefs", track
, (char *)0)))
764 for(n
= 0; n
< nvec
; ++n
) {
765 if(!(pvec
= split(vec
[n
], &npvec
, SPLIT_QUOTES
, pref_error_handler
, 0)))
768 pref_error_handler("malformed response", 0);
771 *kp
= k
= xmalloc(sizeof *k
);
780 /** @brief Parse a boolean response
781 * @param cmd Command for use in error messsage
782 * @param value Result from server
783 * @param flagp Where to store result
784 * @return 0 on success, non-0 on error
786 static int boolean(const char *cmd
, const char *value
,
788 if(!strcmp(value
, "yes")) *flagp
= 1;
789 else if(!strcmp(value
, "no")) *flagp
= 0;
791 error(0, "malformed response to '%s'", cmd
);
797 /** @brief Test whether a track exists
799 * @param track Track name (UTF-8)
800 * @param existsp Where to store result (non-0 iff does exist)
801 * @return 0 on success, non-0 on error
803 int disorder_exists(disorder_client
*c
, const char *track
, int *existsp
) {
807 if((rc
= disorder_simple(c
, &v
, "exists", track
, (char *)0)))
809 return boolean("exists", v
, existsp
);
812 /** @brief Test whether playing is enabled
814 * @param enabledp Where to store result (non-0 iff enabled)
815 * @return 0 on success, non-0 on error
817 int disorder_enabled(disorder_client
*c
, int *enabledp
) {
821 if((rc
= disorder_simple(c
, &v
, "enabled", (char *)0)))
823 return boolean("enabled", v
, enabledp
);
826 /** @brief Get the length of a track
828 * @param track Track name (UTF-8)
829 * @param valuep Where to store length in seconds
830 * @return 0 on success, non-0 on error
832 * If the length is unknown 0 is returned.
834 int disorder_length(disorder_client
*c
, const char *track
,
839 if((rc
= disorder_simple(c
, &value
, "length", track
, (char *)0)))
841 *valuep
= atol(value
);
845 /** @brief Search for tracks
847 * @param terms Search terms (UTF-8)
848 * @param vecp Where to store list (UTF-8)
849 * @param nvecp Where to store number of items, or NULL
850 * @return 0 on success, non-0 on error
852 int disorder_search(disorder_client
*c
, const char *terms
,
853 char ***vecp
, int *nvecp
) {
854 return disorder_simple_list(c
, vecp
, nvecp
, "search", terms
, (char *)0);
857 /** @brief Enable random play
859 * @return 0 on success, non-0 on error
861 int disorder_random_enable(disorder_client
*c
) {
862 return disorder_simple(c
, 0, "random-enable", (char *)0);
865 /** @brief Disable random play
867 * @return 0 on success, non-0 on error
869 int disorder_random_disable(disorder_client
*c
) {
870 return disorder_simple(c
, 0, "random-disable", (char *)0);
873 /** @brief Test whether random play is enabled
875 * @param enabledp Where to store result (non-0 iff enabled)
876 * @return 0 on success, non-0 on error
878 int disorder_random_enabled(disorder_client
*c
, int *enabledp
) {
882 if((rc
= disorder_simple(c
, &v
, "random-enabled", (char *)0)))
884 return boolean("random-enabled", v
, enabledp
);
887 /** @brief Get server stats
889 * @param vecp Where to store list (UTF-8)
890 * @param nvecp Where to store number of items, or NULL
891 * @return 0 on success, non-0 on error
893 int disorder_stats(disorder_client
*c
,
894 char ***vecp
, int *nvecp
) {
895 return disorder_simple_list(c
, vecp
, nvecp
, "stats", (char *)0);
898 /** @brief Set volume
900 * @param left New left channel value
901 * @param right New right channel value
902 * @return 0 on success, non-0 on error
904 int disorder_set_volume(disorder_client
*c
, int left
, int right
) {
907 if(byte_asprintf(&ls
, "%d", left
) < 0
908 || byte_asprintf(&rs
, "%d", right
) < 0)
910 return disorder_simple(c
, 0, "volume", ls
, rs
, (char *)0);
913 /** @brief Get volume
915 * @param left Where to store left channel value
916 * @param right Where to store right channel value
917 * @return 0 on success, non-0 on error
919 int disorder_get_volume(disorder_client
*c
, int *left
, int *right
) {
923 if((rc
= disorder_simple(c
, &r
, "volume", (char *)0)))
925 if(sscanf(r
, "%d %d", left
, right
) != 2) {
926 error(0, "error parsing response to 'volume': '%s'", r
);
932 /** @brief Log to a sink
934 * @param s Sink to write log lines to
935 * @return 0 on success, non-0 on error
937 int disorder_log(disorder_client
*c
, struct sink
*s
) {
941 if((rc
= disorder_simple(c
, 0, "log", (char *)0)))
943 while(inputline(c
->ident
, c
->fpin
, &l
, '\n') >= 0 && strcmp(l
, "."))
944 if(sink_printf(s
, "%s\n", l
) < 0) return -1;
945 if(ferror(c
->fpin
) || feof(c
->fpin
)) return -1;
949 /** @brief Look up a track name part
951 * @param partp Where to store result (UTF-8)
952 * @param track Track name (UTF-8)
953 * @param context Context (usually "sort" or "display") (UTF-8)
954 * @param part Track part (UTF-8)
955 * @return 0 on success, non-0 on error
957 int disorder_part(disorder_client
*c
, char **partp
,
958 const char *track
, const char *context
, const char *part
) {
959 return dequote(disorder_simple(c
, partp
, "part",
960 track
, context
, part
, (char *)0), partp
);
963 /** @brief Resolve aliases
965 * @param trackp Where to store canonical name (UTF-8)
966 * @param track Track name (UTF-8)
967 * @return 0 on success, non-0 on error
969 int disorder_resolve(disorder_client
*c
, char **trackp
, const char *track
) {
970 return dequote(disorder_simple(c
, trackp
, "resolve", track
, (char *)0),
974 /** @brief Pause the current track
976 * @return 0 on success, non-0 on error
978 int disorder_pause(disorder_client
*c
) {
979 return disorder_simple(c
, 0, "pause", (char *)0);
982 /** @brief Resume the current track
984 * @return 0 on success, non-0 on error
986 int disorder_resume(disorder_client
*c
) {
987 return disorder_simple(c
, 0, "resume", (char *)0);
990 /** @brief List all known tags
992 * @param vecp Where to store list (UTF-8)
993 * @param nvecp Where to store number of items, or NULL
994 * @return 0 on success, non-0 on error
996 int disorder_tags(disorder_client
*c
,
997 char ***vecp
, int *nvecp
) {
998 return disorder_simple_list(c
, vecp
, nvecp
, "tags", (char *)0);
1001 /** @brief List all known users
1003 * @param vecp Where to store list (UTF-8)
1004 * @param nvecp Where to store number of items, or NULL
1005 * @return 0 on success, non-0 on error
1007 int disorder_users(disorder_client
*c
,
1008 char ***vecp
, int *nvecp
) {
1009 return disorder_simple_list(c
, vecp
, nvecp
, "users", (char *)0);
1012 /** @brief Get recently added tracks
1014 * @param vecp Where to store pointer to list (UTF-8)
1015 * @param nvecp Where to store count
1016 * @param max Maximum tracks to fetch, or 0 for all available
1017 * @return 0 on success, non-0 on error
1019 int disorder_new_tracks(disorder_client
*c
,
1020 char ***vecp
, int *nvecp
,
1024 sprintf(limit
, "%d", max
);
1025 return disorder_simple_list(c
, vecp
, nvecp
, "new", limit
, (char *)0);
1028 /** @brief Set a global preference
1030 * @param key Preference name (UTF-8)
1031 * @param value Preference value (UTF-8)
1032 * @return 0 on success, non-0 on error
1034 int disorder_set_global(disorder_client
*c
,
1035 const char *key
, const char *value
) {
1036 return disorder_simple(c
, 0, "set-global", key
, value
, (char *)0);
1039 /** @brief Unset a global preference
1041 * @param key Preference name (UTF-8)
1042 * @return 0 on success, non-0 on error
1044 int disorder_unset_global(disorder_client
*c
, const char *key
) {
1045 return disorder_simple(c
, 0, "unset-global", key
, (char *)0);
1048 /** @brief Get a global preference
1050 * @param key Preference name (UTF-8)
1051 * @param valuep Where to store preference value (UTF-8)
1052 * @return 0 on success, non-0 on error
1054 int disorder_get_global(disorder_client
*c
, const char *key
, char **valuep
) {
1055 return dequote(disorder_simple(c
, valuep
, "get-global", key
, (char *)0),
1059 /** @brief Get server's RTP address information
1061 * @param addressp Where to store address (UTF-8)
1062 * @param portp Where to store port (UTF-8)
1063 * @return 0 on success, non-0 on error
1065 int disorder_rtp_address(disorder_client
*c
, char **addressp
, char **portp
) {
1070 if((rc
= disorder_simple(c
, &r
, "rtp-address", (char *)0)))
1072 vec
= split(r
, &n
, SPLIT_QUOTES
, 0, 0);
1074 error(0, "malformed rtp-address reply");
1082 /** @brief Create a user
1084 * @param user Username
1085 * @param password Password
1086 * @param rights Initial rights or NULL to use default
1087 * @return 0 on success, non-0 on error
1089 int disorder_adduser(disorder_client
*c
,
1090 const char *user
, const char *password
,
1091 const char *rights
) {
1092 return disorder_simple(c
, 0, "adduser", user
, password
, rights
, (char *)0);
1095 /** @brief Delete a user
1097 * @param user Username
1098 * @return 0 on success, non-0 on error
1100 int disorder_deluser(disorder_client
*c
, const char *user
) {
1101 return disorder_simple(c
, 0, "deluser", user
, (char *)0);
1104 /** @brief Get user information
1106 * @param user Username
1107 * @param key Property name (UTF-8)
1108 * @param valuep Where to store value (UTF-8)
1109 * @return 0 on success, non-0 on error
1111 int disorder_userinfo(disorder_client
*c
, const char *user
, const char *key
,
1113 return dequote(disorder_simple(c
, valuep
, "userinfo", user
, key
, (char *)0),
1117 /** @brief Set user information
1119 * @param user Username
1120 * @param key Property name (UTF-8)
1121 * @param value New property value (UTF-8)
1122 * @return 0 on success, non-0 on error
1124 int disorder_edituser(disorder_client
*c
, const char *user
,
1125 const char *key
, const char *value
) {
1126 return disorder_simple(c
, 0, "edituser", user
, key
, value
, (char *)0);
1129 /** @brief Register a user
1131 * @param user Username
1132 * @param password Password
1133 * @param email Email address (UTF-8)
1134 * @param confirmp Where to store confirmation string
1135 * @return 0 on success, non-0 on error
1137 int disorder_register(disorder_client
*c
, const char *user
,
1138 const char *password
, const char *email
,
1140 return dequote(disorder_simple(c
, confirmp
, "register",
1141 user
, password
, email
, (char *)0),
1145 /** @brief Confirm a user
1147 * @param confirm Confirmation string
1148 * @return 0 on success, non-0 on error
1150 int disorder_confirm(disorder_client
*c
, const char *confirm
) {
1154 if(!(rc
= dequote(disorder_simple(c
, &u
, "confirm", confirm
, (char *)0),
1160 /** @brief Make a cookie for this login
1162 * @param cookiep Where to store cookie string
1163 * @return 0 on success, non-0 on error
1165 int disorder_make_cookie(disorder_client
*c
, char **cookiep
) {
1166 return dequote(disorder_simple(c
, cookiep
, "make-cookie", (char *)0),
1170 /** @brief Revoke the cookie used by this session
1172 * @return 0 on success, non-0 on error
1174 int disorder_revoke(disorder_client
*c
) {
1175 return disorder_simple(c
, 0, "revoke", (char *)0);
1178 /** @brief Request a password reminder email
1180 * @param user Username
1181 * @return 0 on success, non-0 on error
1183 int disorder_reminder(disorder_client
*c
, const char *user
) {
1184 return disorder_simple(c
, 0, "reminder", user
, (char *)0);