Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
f74f4f32 | 3 | * Copyright (C) 2004-2009 Richard Kettlewell |
460b9539 | 4 | * |
e7eb3a27 | 5 | * This program is free software: you can redistribute it and/or modify |
460b9539 | 6 | * it under the terms of the GNU General Public License as published by |
e7eb3a27 | 7 | * the Free Software Foundation, either version 3 of the License, or |
460b9539 | 8 | * (at your option) any later version. |
e7eb3a27 RK |
9 | * |
10 | * This program is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
460b9539 | 15 | * You should have received a copy of the GNU General Public License |
e7eb3a27 | 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
460b9539 | 17 | */ |
c4e2cfdd RK |
18 | /** @file lib/client.c |
19 | * @brief Simple C client | |
20 | * | |
158d0961 | 21 | * See @ref lib/eclient.c for an asynchronous-capable client |
c4e2cfdd RK |
22 | * implementation. |
23 | */ | |
460b9539 | 24 | |
05b75f8d | 25 | #include "common.h" |
460b9539 | 26 | |
27 | #include <sys/types.h> | |
28 | #include <sys/socket.h> | |
29 | #include <netinet/in.h> | |
30 | #include <sys/un.h> | |
460b9539 | 31 | #include <unistd.h> |
32 | #include <errno.h> | |
33 | #include <netdb.h> | |
f0feb22e | 34 | #include <pcre.h> |
460b9539 | 35 | |
36 | #include "log.h" | |
37 | #include "mem.h" | |
460b9539 | 38 | #include "queue.h" |
39 | #include "client.h" | |
40 | #include "charset.h" | |
41 | #include "hex.h" | |
42 | #include "split.h" | |
43 | #include "vector.h" | |
44 | #include "inputline.h" | |
45 | #include "kvp.h" | |
46 | #include "syscalls.h" | |
47 | #include "printf.h" | |
48 | #include "sink.h" | |
49 | #include "addr.h" | |
50 | #include "authhash.h" | |
51 | #include "client-common.h" | |
5df73aeb | 52 | #include "rights.h" |
f0feb22e | 53 | #include "trackdb.h" |
758aa6c3 | 54 | #include "kvp.h" |
460b9539 | 55 | |
0590cedc | 56 | /** @brief Client handle contents */ |
460b9539 | 57 | struct disorder_client { |
0590cedc RK |
58 | /** @brief Stream to read from */ |
59 | FILE *fpin; | |
60 | /** @brief Stream to write to */ | |
61 | FILE *fpout; | |
62 | /** @brief Peer description */ | |
460b9539 | 63 | char *ident; |
0590cedc | 64 | /** @brief Username */ |
460b9539 | 65 | char *user; |
0590cedc | 66 | /** @brief Report errors to @c stderr */ |
460b9539 | 67 | int verbose; |
0590cedc | 68 | /** @brief Last error string */ |
e9e8a16d | 69 | const char *last; |
460b9539 | 70 | }; |
71 | ||
c4e2cfdd RK |
72 | /** @brief Create a new client |
73 | * @param verbose If nonzero, write extra junk to stderr | |
74 | * @return Pointer to new client | |
75 | * | |
fdf98378 | 76 | * You must call disorder_connect(), disorder_connect_user() or |
77 | * disorder_connect_cookie() to connect it. Use disorder_close() to | |
78 | * dispose of the client when finished with it. | |
c4e2cfdd | 79 | */ |
460b9539 | 80 | disorder_client *disorder_new(int verbose) { |
81 | disorder_client *c = xmalloc(sizeof (struct disorder_client)); | |
82 | ||
83 | c->verbose = verbose; | |
84 | return c; | |
85 | } | |
86 | ||
c4e2cfdd RK |
87 | /** @brief Read a response line |
88 | * @param c Client | |
89 | * @param rp Where to store response, or NULL (UTF-8) | |
90 | * @return Response code 0-999 or -1 on error | |
91 | */ | |
460b9539 | 92 | static int response(disorder_client *c, char **rp) { |
93 | char *r; | |
94 | ||
e9e8a16d RK |
95 | if(inputline(c->ident, c->fpin, &r, '\n')) { |
96 | byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno)); | |
460b9539 | 97 | return -1; |
e9e8a16d | 98 | } |
460b9539 | 99 | D(("response: %s", r)); |
100 | if(rp) | |
101 | *rp = r; | |
102 | if(r[0] >= '0' && r[0] <= '9' | |
103 | && r[1] >= '0' && r[1] <= '9' | |
104 | && r[2] >= '0' && r[2] <= '9' | |
bb037be2 | 105 | && r[3] == ' ') { |
106 | c->last = r + 4; | |
460b9539 | 107 | return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0'; |
bb037be2 | 108 | } else { |
e9e8a16d | 109 | c->last = "invalid reply format"; |
2e9ba080 | 110 | disorder_error(0, "invalid reply format from %s", c->ident); |
460b9539 | 111 | return -1; |
112 | } | |
113 | } | |
114 | ||
bb037be2 | 115 | /** @brief Return last response string |
116 | * @param c Client | |
117 | * @return Last response string (UTF-8, English) or NULL | |
118 | */ | |
119 | const char *disorder_last(disorder_client *c) { | |
120 | return c->last; | |
121 | } | |
122 | ||
c4e2cfdd RK |
123 | /** @brief Read and partially parse a response |
124 | * @param c Client | |
125 | * @param rp Where to store response text (or NULL) (UTF-8) | |
126 | * @return 0 on success, non-0 on error | |
127 | * | |
128 | * 5xx responses count as errors. | |
129 | * | |
130 | * @p rp will NOT be filled in for xx9 responses (where it is just | |
131 | * commentary for a command where it would normally be meaningful). | |
132 | * | |
133 | * NB that the response will NOT be converted to the local encoding. | |
460b9539 | 134 | */ |
135 | static int check_response(disorder_client *c, char **rp) { | |
136 | int rc; | |
137 | char *r; | |
138 | ||
139 | if((rc = response(c, &r)) == -1) | |
140 | return -1; | |
141 | else if(rc / 100 == 2) { | |
142 | if(rp) | |
143 | *rp = (rc % 10 == 9) ? 0 : xstrdup(r + 4); | |
f74f4f32 | 144 | xfree(r); |
460b9539 | 145 | return 0; |
146 | } else { | |
147 | if(c->verbose) | |
2e9ba080 | 148 | disorder_error(0, "from %s: %s", c->ident, utf82mb(r)); |
f74f4f32 | 149 | xfree(r); |
2bead829 | 150 | return rc; |
460b9539 | 151 | } |
152 | } | |
153 | ||
c4e2cfdd RK |
154 | /** @brief Issue a command and parse a simple response |
155 | * @param c Client | |
156 | * @param rp Where to store result, or NULL | |
157 | * @param cmd Command | |
39c53343 RK |
158 | * @param body Body or NULL |
159 | * @param nbody Length of body or -1 | |
c4e2cfdd RK |
160 | * @param ap Arguments (UTF-8), terminated by (char *)0 |
161 | * @return 0 on success, non-0 on error | |
162 | * | |
163 | * 5xx responses count as errors. | |
164 | * | |
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). | |
167 | * | |
168 | * NB that the response will NOT be converted to the local encoding | |
169 | * nor will quotes be stripped. See dequote(). | |
39c53343 RK |
170 | * |
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. | |
174 | * | |
175 | * Usually you would call this via one of the following interfaces: | |
176 | * - disorder_simple() | |
177 | * - disorder_simple_body() | |
178 | * - disorder_simple_list() | |
c4e2cfdd | 179 | */ |
460b9539 | 180 | static int disorder_simple_v(disorder_client *c, |
181 | char **rp, | |
39c53343 RK |
182 | const char *cmd, |
183 | char **body, int nbody, | |
184 | va_list ap) { | |
460b9539 | 185 | const char *arg; |
186 | struct dynstr d; | |
187 | ||
62ef2216 | 188 | if(!c->fpout) { |
e9e8a16d | 189 | c->last = "not connected"; |
2e9ba080 | 190 | disorder_error(0, "not connected to server"); |
62ef2216 | 191 | return -1; |
192 | } | |
460b9539 | 193 | if(cmd) { |
194 | dynstr_init(&d); | |
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)); | |
199 | } | |
200 | dynstr_append(&d, '\n'); | |
201 | dynstr_terminate(&d); | |
202 | D(("command: %s", d.vec)); | |
39c53343 RK |
203 | if(fputs(d.vec, c->fpout) < 0) |
204 | goto write_error; | |
f74f4f32 | 205 | xfree(d.vec); |
39c53343 RK |
206 | if(body) { |
207 | if(nbody < 0) | |
208 | for(nbody = 0; body[nbody]; ++nbody) | |
209 | ; | |
210 | for(int n = 0; n < nbody; ++n) { | |
211 | if(body[n][0] == '.') | |
212 | if(fputc('.', c->fpout) < 0) | |
213 | goto write_error; | |
214 | if(fputs(body[n], c->fpout) < 0) | |
215 | goto write_error; | |
216 | if(fputc('\n', c->fpout) < 0) | |
217 | goto write_error; | |
218 | } | |
219 | if(fputs(".\n", c->fpout) < 0) | |
220 | goto write_error; | |
460b9539 | 221 | } |
39c53343 RK |
222 | if(fflush(c->fpout)) |
223 | goto write_error; | |
460b9539 | 224 | } |
225 | return check_response(c, rp); | |
39c53343 RK |
226 | write_error: |
227 | byte_xasprintf((char **)&c->last, "write error: %s", strerror(errno)); | |
2e9ba080 | 228 | disorder_error(errno, "error writing to %s", c->ident); |
39c53343 | 229 | return -1; |
460b9539 | 230 | } |
231 | ||
c4e2cfdd RK |
232 | /** @brief Issue a command and parse a simple response |
233 | * @param c Client | |
234 | * @param rp Where to store result, or NULL (UTF-8) | |
235 | * @param cmd Command | |
236 | * @return 0 on success, non-0 on error | |
237 | * | |
238 | * The remaining arguments are command arguments, terminated by (char | |
239 | * *)0. They should be in UTF-8. | |
240 | * | |
241 | * 5xx responses count as errors. | |
242 | * | |
243 | * @p rp will NOT be filled in for xx9 responses (where it is just | |
244 | * commentary for a command where it would normally be meaningful). | |
245 | * | |
246 | * NB that the response will NOT be converted to the local encoding | |
247 | * nor will quotes be stripped. See dequote(). | |
460b9539 | 248 | */ |
249 | static int disorder_simple(disorder_client *c, | |
250 | char **rp, | |
251 | const char *cmd, ...) { | |
252 | va_list ap; | |
253 | int ret; | |
254 | ||
255 | va_start(ap, cmd); | |
39c53343 RK |
256 | ret = disorder_simple_v(c, rp, cmd, 0, 0, ap); |
257 | va_end(ap); | |
258 | return ret; | |
259 | } | |
260 | ||
261 | /** @brief Issue a command with a body and parse a simple response | |
262 | * @param c Client | |
263 | * @param rp Where to store result, or NULL (UTF-8) | |
264 | * @param body Pointer to body | |
265 | * @param nbody Size of body | |
266 | * @param cmd Command | |
267 | * @return 0 on success, non-0 on error | |
268 | * | |
269 | * See disorder_simple(). | |
270 | */ | |
271 | static int disorder_simple_body(disorder_client *c, | |
272 | char **rp, | |
273 | char **body, int nbody, | |
274 | const char *cmd, ...) { | |
275 | va_list ap; | |
276 | int ret; | |
277 | ||
278 | va_start(ap, cmd); | |
279 | ret = disorder_simple_v(c, rp, cmd, body, nbody, ap); | |
460b9539 | 280 | va_end(ap); |
281 | return ret; | |
282 | } | |
283 | ||
0227f67d RK |
284 | /** @brief Dequote a result string |
285 | * @param rc 0 on success, non-0 on error | |
286 | * @param rp Where result string is stored (UTF-8) | |
287 | * @return @p rc | |
288 | * | |
289 | * This is used as a wrapper around disorder_simple() to dequote | |
290 | * results in place. | |
291 | */ | |
292 | static int dequote(int rc, char **rp) { | |
293 | char **rr; | |
2bead829 | 294 | |
0227f67d RK |
295 | if(!rc) { |
296 | if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) { | |
f74f4f32 | 297 | xfree(*rp); |
0227f67d | 298 | *rp = *rr; |
f74f4f32 | 299 | xfree(rr); |
0227f67d RK |
300 | return 0; |
301 | } | |
2e9ba080 | 302 | disorder_error(0, "invalid reply: %s", *rp); |
460b9539 | 303 | } |
2bead829 | 304 | return rc; |
460b9539 | 305 | } |
306 | ||
0227f67d | 307 | /** @brief Generic connection routine |
319d7107 | 308 | * @param conf Configuration to follow |
c4e2cfdd | 309 | * @param c Client |
0227f67d RK |
310 | * @param username Username to log in with or NULL |
311 | * @param password Password to log in with or NULL | |
312 | * @param cookie Cookie to log in with or NULL | |
c4e2cfdd RK |
313 | * @return 0 on success, non-0 on error |
314 | * | |
0227f67d RK |
315 | * @p cookie is tried first if not NULL. If it is NULL then @p |
316 | * username must not be. If @p username is not NULL then nor may @p | |
317 | * password be. | |
c4e2cfdd | 318 | */ |
319d7107 RK |
319 | int disorder_connect_generic(struct config *conf, |
320 | disorder_client *c, | |
321 | const char *username, | |
322 | const char *password, | |
323 | const char *cookie) { | |
f74f4f32 RK |
324 | int fd = -1, fd2 = -1, nrvec = 0, rc; |
325 | unsigned char *nonce = NULL; | |
460b9539 | 326 | size_t nl; |
f74f4f32 RK |
327 | char *res = NULL; |
328 | char *r = NULL, **rvec = NULL; | |
7b32e917 | 329 | const char *protocol, *algorithm, *challenge; |
f74f4f32 | 330 | struct sockaddr *sa = NULL; |
0227f67d | 331 | socklen_t salen; |
460b9539 | 332 | |
319d7107 | 333 | if((salen = find_server(conf, &sa, &c->ident)) == (socklen_t)-1) |
460b9539 | 334 | return -1; |
460b9539 | 335 | c->fpin = c->fpout = 0; |
336 | if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) { | |
e9e8a16d | 337 | byte_xasprintf((char **)&c->last, "socket: %s", strerror(errno)); |
2e9ba080 | 338 | disorder_error(errno, "error calling socket"); |
460b9539 | 339 | return -1; |
340 | } | |
0227f67d | 341 | if(connect(fd, sa, salen) < 0) { |
e9e8a16d | 342 | byte_xasprintf((char **)&c->last, "connect: %s", strerror(errno)); |
2e9ba080 | 343 | disorder_error(errno, "error calling connect"); |
460b9539 | 344 | goto error; |
345 | } | |
346 | if((fd2 = dup(fd)) < 0) { | |
e9e8a16d | 347 | byte_xasprintf((char **)&c->last, "dup: %s", strerror(errno)); |
2e9ba080 | 348 | disorder_error(errno, "error calling dup"); |
460b9539 | 349 | goto error; |
350 | } | |
351 | if(!(c->fpin = fdopen(fd, "rb"))) { | |
e9e8a16d | 352 | byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno)); |
2e9ba080 | 353 | disorder_error(errno, "error calling fdopen"); |
460b9539 | 354 | goto error; |
355 | } | |
356 | fd = -1; | |
357 | if(!(c->fpout = fdopen(fd2, "wb"))) { | |
e9e8a16d | 358 | byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno)); |
2e9ba080 | 359 | disorder_error(errno, "error calling fdopen"); |
460b9539 | 360 | goto error; |
361 | } | |
362 | fd2 = -1; | |
2bead829 | 363 | if((rc = disorder_simple(c, &r, 0, (const char *)0))) |
364 | goto error_rc; | |
637fdea3 | 365 | if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0))) |
2bead829 | 366 | goto error; |
7b32e917 | 367 | if(nrvec != 3) { |
e9e8a16d | 368 | c->last = "cannot parse server greeting"; |
2e9ba080 | 369 | disorder_error(0, "cannot parse server greeting %s", r); |
2bead829 | 370 | goto error; |
637fdea3 | 371 | } |
f74f4f32 | 372 | protocol = rvec[0]; |
7b32e917 | 373 | if(strcmp(protocol, "2")) { |
e9e8a16d | 374 | c->last = "unknown protocol version"; |
2e9ba080 | 375 | disorder_error(0, "unknown protocol version: %s", protocol); |
2bead829 | 376 | goto error; |
7b32e917 | 377 | } |
f74f4f32 RK |
378 | algorithm = rvec[1]; |
379 | challenge = rvec[2]; | |
7b32e917 | 380 | if(!(nonce = unhex(challenge, &nl))) |
2bead829 | 381 | goto error; |
0227f67d RK |
382 | if(cookie) { |
383 | if(!dequote(disorder_simple(c, &c->user, "cookie", cookie, (char *)0), | |
384 | &c->user)) | |
385 | return 0; /* success */ | |
386 | if(!username) { | |
e9e8a16d | 387 | c->last = "cookie failed and no username"; |
2e9ba080 | 388 | disorder_error(0, "cookie did not work and no username available"); |
2bead829 | 389 | goto error; |
0227f67d RK |
390 | } |
391 | } | |
e9e8a16d RK |
392 | if(!(res = authhash(nonce, nl, password, algorithm))) { |
393 | c->last = "error computing authorization hash"; | |
394 | goto error; | |
395 | } | |
2bead829 | 396 | if((rc = disorder_simple(c, 0, "user", username, res, (char *)0))) |
397 | goto error_rc; | |
460b9539 | 398 | c->user = xstrdup(username); |
f74f4f32 RK |
399 | xfree(res); |
400 | free_strings(nrvec, rvec); | |
401 | xfree(nonce); | |
402 | xfree(sa); | |
403 | xfree(r); | |
460b9539 | 404 | return 0; |
405 | error: | |
2bead829 | 406 | rc = -1; |
407 | error_rc: | |
408 | if(c->fpin) { | |
409 | fclose(c->fpin); | |
410 | c->fpin = 0; | |
411 | } | |
412 | if(c->fpout) { | |
413 | fclose(c->fpout); | |
414 | c->fpout = 0; | |
415 | } | |
460b9539 | 416 | if(fd2 != -1) close(fd2); |
417 | if(fd != -1) close(fd); | |
2bead829 | 418 | return rc; |
460b9539 | 419 | } |
420 | ||
fdf98378 | 421 | /** @brief Connect a client with a specified username and password |
422 | * @param c Client | |
423 | * @param username Username to log in with | |
424 | * @param password Password to log in with | |
425 | * @return 0 on success, non-0 on error | |
426 | */ | |
427 | int disorder_connect_user(disorder_client *c, | |
428 | const char *username, | |
429 | const char *password) { | |
319d7107 RK |
430 | return disorder_connect_generic(config, |
431 | c, | |
fdf98378 | 432 | username, |
433 | password, | |
434 | 0); | |
435 | } | |
436 | ||
0227f67d RK |
437 | /** @brief Connect a client |
438 | * @param c Client | |
439 | * @return 0 on success, non-0 on error | |
440 | * | |
441 | * The connection will use the username and password found in @ref | |
442 | * config, or directly from the database if no password is found and | |
443 | * the database is readable (usually only for root). | |
444 | */ | |
445 | int disorder_connect(disorder_client *c) { | |
446 | const char *username, *password; | |
447 | ||
448 | if(!(username = config->username)) { | |
e9e8a16d | 449 | c->last = "no username"; |
2e9ba080 | 450 | disorder_error(0, "no username configured"); |
0227f67d RK |
451 | return -1; |
452 | } | |
453 | password = config->password; | |
199c2a23 RK |
454 | /* Maybe we can read the database */ |
455 | if(!password && trackdb_readable()) { | |
0227f67d RK |
456 | trackdb_init(TRACKDB_NO_RECOVER|TRACKDB_NO_UPGRADE); |
457 | trackdb_open(TRACKDB_READ_ONLY); | |
458 | password = trackdb_get_password(username); | |
459 | trackdb_close(); | |
460 | } | |
461 | if(!password) { | |
462 | /* Oh well */ | |
e9e8a16d | 463 | c->last = "no password"; |
2e9ba080 | 464 | disorder_error(0, "no password configured for user '%s'", username); |
0227f67d RK |
465 | return -1; |
466 | } | |
319d7107 RK |
467 | return disorder_connect_generic(config, |
468 | c, | |
0227f67d RK |
469 | username, |
470 | password, | |
471 | 0); | |
472 | } | |
473 | ||
474 | /** @brief Connect a client | |
475 | * @param c Client | |
476 | * @param cookie Cookie to log in with, or NULL | |
477 | * @return 0 on success, non-0 on error | |
478 | * | |
479 | * If @p cookie is NULL or does not work then we attempt to log in as | |
480 | * guest instead (so when the cookie expires only an extra round trip | |
481 | * is needed rathre than a complete new login). | |
482 | */ | |
483 | int disorder_connect_cookie(disorder_client *c, | |
484 | const char *cookie) { | |
319d7107 RK |
485 | return disorder_connect_generic(config, |
486 | c, | |
0227f67d RK |
487 | "guest", |
488 | "", | |
489 | cookie); | |
490 | } | |
491 | ||
c4e2cfdd RK |
492 | /** @brief Close a client |
493 | * @param c Client | |
494 | * @return 0 on succcess, non-0 on errior | |
495 | * | |
496 | * The client is still closed even on error. It might well be | |
497 | * appropriate to ignore the return value. | |
498 | */ | |
460b9539 | 499 | int disorder_close(disorder_client *c) { |
500 | int ret = 0; | |
501 | ||
502 | if(c->fpin) { | |
503 | if(fclose(c->fpin) < 0) { | |
e9e8a16d | 504 | byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno)); |
2e9ba080 | 505 | disorder_error(errno, "error calling fclose"); |
460b9539 | 506 | ret = -1; |
507 | } | |
508 | c->fpin = 0; | |
509 | } | |
510 | if(c->fpout) { | |
511 | if(fclose(c->fpout) < 0) { | |
e9e8a16d | 512 | byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno)); |
2e9ba080 | 513 | disorder_error(errno, "error calling fclose"); |
460b9539 | 514 | ret = -1; |
515 | } | |
516 | c->fpout = 0; | |
517 | } | |
f74f4f32 | 518 | xfree(c->ident); |
0227f67d | 519 | c->ident = 0; |
f74f4f32 | 520 | xfree(c->user); |
0227f67d | 521 | c->user = 0; |
460b9539 | 522 | return 0; |
523 | } | |
524 | ||
c4e2cfdd RK |
525 | /** @brief Play a track |
526 | * @param c Client | |
527 | * @param track Track to play (UTF-8) | |
528 | * @return 0 on success, non-0 on error | |
529 | */ | |
460b9539 | 530 | int disorder_play(disorder_client *c, const char *track) { |
531 | return disorder_simple(c, 0, "play", track, (char *)0); | |
532 | } | |
533 | ||
c4e2cfdd RK |
534 | /** @brief Remove a track |
535 | * @param c Client | |
536 | * @param track Track to remove (UTF-8) | |
537 | * @return 0 on success, non-0 on error | |
538 | */ | |
460b9539 | 539 | int disorder_remove(disorder_client *c, const char *track) { |
540 | return disorder_simple(c, 0, "remove", track, (char *)0); | |
541 | } | |
542 | ||
c4e2cfdd RK |
543 | /** @brief Move a track |
544 | * @param c Client | |
545 | * @param track Track to move (UTF-8) | |
546 | * @param delta Distance to move by | |
547 | * @return 0 on success, non-0 on error | |
548 | */ | |
460b9539 | 549 | int disorder_move(disorder_client *c, const char *track, int delta) { |
550 | char d[16]; | |
551 | ||
552 | byte_snprintf(d, sizeof d, "%d", delta); | |
553 | return disorder_simple(c, 0, "move", track, d, (char *)0); | |
554 | } | |
555 | ||
c4e2cfdd RK |
556 | /** @brief Enable play |
557 | * @param c Client | |
558 | * @return 0 on success, non-0 on error | |
559 | */ | |
460b9539 | 560 | int disorder_enable(disorder_client *c) { |
561 | return disorder_simple(c, 0, "enable", (char *)0); | |
562 | } | |
563 | ||
c4e2cfdd RK |
564 | /** @brief Disable play |
565 | * @param c Client | |
566 | * @return 0 on success, non-0 on error | |
567 | */ | |
460b9539 | 568 | int disorder_disable(disorder_client *c) { |
569 | return disorder_simple(c, 0, "disable", (char *)0); | |
570 | } | |
571 | ||
c4e2cfdd RK |
572 | /** @brief Scratch the currently playing track |
573 | * @param id Playing track ID or NULL (UTF-8) | |
574 | * @param c Client | |
575 | * @return 0 on success, non-0 on error | |
576 | */ | |
460b9539 | 577 | int disorder_scratch(disorder_client *c, const char *id) { |
578 | return disorder_simple(c, 0, "scratch", id, (char *)0); | |
579 | } | |
580 | ||
c4e2cfdd RK |
581 | /** @brief Shut down the server |
582 | * @param c Client | |
583 | * @return 0 on success, non-0 on error | |
584 | */ | |
460b9539 | 585 | int disorder_shutdown(disorder_client *c) { |
586 | return disorder_simple(c, 0, "shutdown", (char *)0); | |
587 | } | |
588 | ||
c4e2cfdd RK |
589 | /** @brief Make the server re-read its configuration |
590 | * @param c Client | |
591 | * @return 0 on success, non-0 on error | |
592 | */ | |
460b9539 | 593 | int disorder_reconfigure(disorder_client *c) { |
594 | return disorder_simple(c, 0, "reconfigure", (char *)0); | |
595 | } | |
596 | ||
c4e2cfdd RK |
597 | /** @brief Rescan tracks |
598 | * @param c Client | |
599 | * @return 0 on success, non-0 on error | |
600 | */ | |
460b9539 | 601 | int disorder_rescan(disorder_client *c) { |
602 | return disorder_simple(c, 0, "rescan", (char *)0); | |
603 | } | |
604 | ||
c4e2cfdd RK |
605 | /** @brief Get server version number |
606 | * @param c Client | |
607 | * @param rp Where to store version string (UTF-8) | |
608 | * @return 0 on success, non-0 on error | |
609 | */ | |
460b9539 | 610 | int disorder_version(disorder_client *c, char **rp) { |
7b32e917 | 611 | return dequote(disorder_simple(c, rp, "version", (char *)0), rp); |
460b9539 | 612 | } |
613 | ||
614 | static void client_error(const char *msg, | |
615 | void attribute((unused)) *u) { | |
2e9ba080 | 616 | disorder_error(0, "error parsing reply: %s", msg); |
460b9539 | 617 | } |
618 | ||
c4e2cfdd RK |
619 | /** @brief Get currently playing track |
620 | * @param c Client | |
621 | * @param qp Where to store track information | |
622 | * @return 0 on success, non-0 on error | |
623 | * | |
624 | * @p qp gets NULL if no track is playing. | |
625 | */ | |
460b9539 | 626 | int disorder_playing(disorder_client *c, struct queue_entry **qp) { |
627 | char *r; | |
628 | struct queue_entry *q; | |
2bead829 | 629 | int rc; |
460b9539 | 630 | |
2bead829 | 631 | if((rc = disorder_simple(c, &r, "playing", (char *)0))) |
632 | return rc; | |
460b9539 | 633 | if(r) { |
634 | q = xmalloc(sizeof *q); | |
635 | if(queue_unmarshall(q, r, client_error, 0)) | |
636 | return -1; | |
637 | *qp = q; | |
638 | } else | |
639 | *qp = 0; | |
640 | return 0; | |
641 | } | |
642 | ||
0590cedc | 643 | /** @brief Fetch the queue, recent list, etc */ |
460b9539 | 644 | static int disorder_somequeue(disorder_client *c, |
645 | const char *cmd, struct queue_entry **qp) { | |
646 | struct queue_entry *qh, **qt = &qh, *q; | |
647 | char *l; | |
2bead829 | 648 | int rc; |
460b9539 | 649 | |
2bead829 | 650 | if((rc = disorder_simple(c, 0, cmd, (char *)0))) |
651 | return rc; | |
460b9539 | 652 | while(inputline(c->ident, c->fpin, &l, '\n') >= 0) { |
653 | if(!strcmp(l, ".")) { | |
654 | *qt = 0; | |
655 | *qp = qh; | |
656 | return 0; | |
657 | } | |
658 | q = xmalloc(sizeof *q); | |
659 | if(!queue_unmarshall(q, l, client_error, 0)) { | |
660 | *qt = q; | |
661 | qt = &q->next; | |
662 | } | |
663 | } | |
e9e8a16d RK |
664 | if(ferror(c->fpin)) { |
665 | byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno)); | |
2e9ba080 | 666 | disorder_error(errno, "error reading %s", c->ident); |
e9e8a16d RK |
667 | } else { |
668 | c->last = "input error: unexpxected EOF"; | |
2e9ba080 | 669 | disorder_error(0, "error reading %s: unexpected EOF", c->ident); |
e9e8a16d | 670 | } |
460b9539 | 671 | return -1; |
672 | } | |
673 | ||
c4e2cfdd RK |
674 | /** @brief Get recently played tracks |
675 | * @param c Client | |
676 | * @param qp Where to store track information | |
677 | * @return 0 on success, non-0 on error | |
678 | * | |
679 | * The last entry in the list is the most recently played track. | |
680 | */ | |
460b9539 | 681 | int disorder_recent(disorder_client *c, struct queue_entry **qp) { |
682 | return disorder_somequeue(c, "recent", qp); | |
683 | } | |
684 | ||
c4e2cfdd RK |
685 | /** @brief Get queue |
686 | * @param c Client | |
687 | * @param qp Where to store track information | |
688 | * @return 0 on success, non-0 on error | |
689 | * | |
690 | * The first entry in the list will be played next. | |
691 | */ | |
460b9539 | 692 | int disorder_queue(disorder_client *c, struct queue_entry **qp) { |
693 | return disorder_somequeue(c, "queue", qp); | |
694 | } | |
695 | ||
c4e2cfdd RK |
696 | /** @brief Read a dot-stuffed list |
697 | * @param c Client | |
698 | * @param vecp Where to store list (UTF-8) | |
699 | * @param nvecp Where to store number of items, or NULL | |
700 | * @return 0 on success, non-0 on error | |
701 | * | |
702 | * The list will have a final NULL not counted in @p nvecp. | |
703 | */ | |
460b9539 | 704 | static int readlist(disorder_client *c, char ***vecp, int *nvecp) { |
705 | char *l; | |
706 | struct vector v; | |
707 | ||
708 | vector_init(&v); | |
709 | while(inputline(c->ident, c->fpin, &l, '\n') >= 0) { | |
710 | if(!strcmp(l, ".")) { | |
711 | vector_terminate(&v); | |
712 | if(nvecp) | |
713 | *nvecp = v.nvec; | |
714 | *vecp = v.vec; | |
715 | return 0; | |
716 | } | |
717 | vector_append(&v, l + (*l == '.')); | |
718 | } | |
e9e8a16d RK |
719 | if(ferror(c->fpin)) { |
720 | byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno)); | |
2e9ba080 | 721 | disorder_error(errno, "error reading %s", c->ident); |
e9e8a16d RK |
722 | } else { |
723 | c->last = "input error: unexpxected EOF"; | |
2e9ba080 | 724 | disorder_error(0, "error reading %s: unexpected EOF", c->ident); |
e9e8a16d | 725 | } |
460b9539 | 726 | return -1; |
727 | } | |
728 | ||
c4e2cfdd RK |
729 | /** @brief Issue a comamnd and get a list response |
730 | * @param c Client | |
731 | * @param vecp Where to store list (UTF-8) | |
732 | * @param nvecp Where to store number of items, or NULL | |
733 | * @param cmd Command | |
734 | * @return 0 on success, non-0 on error | |
735 | * | |
736 | * The remaining arguments are command arguments, terminated by (char | |
737 | * *)0. They should be in UTF-8. | |
738 | * | |
739 | * 5xx responses count as errors. | |
39c53343 RK |
740 | * |
741 | * See disorder_simple(). | |
c4e2cfdd | 742 | */ |
460b9539 | 743 | static int disorder_simple_list(disorder_client *c, |
744 | char ***vecp, int *nvecp, | |
745 | const char *cmd, ...) { | |
746 | va_list ap; | |
747 | int ret; | |
748 | ||
749 | va_start(ap, cmd); | |
39c53343 | 750 | ret = disorder_simple_v(c, 0, cmd, 0, 0, ap); |
460b9539 | 751 | va_end(ap); |
752 | if(ret) return ret; | |
753 | return readlist(c, vecp, nvecp); | |
754 | } | |
755 | ||
c4e2cfdd RK |
756 | /** @brief List directories below @p dir |
757 | * @param c Client | |
0227f67d | 758 | * @param dir Directory to list, or NULL for root (UTF-8) |
c4e2cfdd RK |
759 | * @param re Regexp that results must match, or NULL (UTF-8) |
760 | * @param vecp Where to store list (UTF-8) | |
761 | * @param nvecp Where to store number of items, or NULL | |
762 | * @return 0 on success, non-0 on error | |
763 | */ | |
460b9539 | 764 | int disorder_directories(disorder_client *c, const char *dir, const char *re, |
765 | char ***vecp, int *nvecp) { | |
766 | return disorder_simple_list(c, vecp, nvecp, "dirs", dir, re, (char *)0); | |
767 | } | |
768 | ||
c4e2cfdd RK |
769 | /** @brief List files below @p dir |
770 | * @param c Client | |
0227f67d | 771 | * @param dir Directory to list, or NULL for root (UTF-8) |
c4e2cfdd RK |
772 | * @param re Regexp that results must match, or NULL (UTF-8) |
773 | * @param vecp Where to store list (UTF-8) | |
774 | * @param nvecp Where to store number of items, or NULL | |
775 | * @return 0 on success, non-0 on error | |
776 | */ | |
460b9539 | 777 | int disorder_files(disorder_client *c, const char *dir, const char *re, |
778 | char ***vecp, int *nvecp) { | |
779 | return disorder_simple_list(c, vecp, nvecp, "files", dir, re, (char *)0); | |
780 | } | |
781 | ||
c4e2cfdd RK |
782 | /** @brief List files and directories below @p dir |
783 | * @param c Client | |
0227f67d | 784 | * @param dir Directory to list, or NULL for root (UTF-8) |
c4e2cfdd RK |
785 | * @param re Regexp that results must match, or NULL (UTF-8) |
786 | * @param vecp Where to store list (UTF-8) | |
787 | * @param nvecp Where to store number of items, or NULL | |
788 | * @return 0 on success, non-0 on error | |
789 | */ | |
460b9539 | 790 | int disorder_allfiles(disorder_client *c, const char *dir, const char *re, |
791 | char ***vecp, int *nvecp) { | |
792 | return disorder_simple_list(c, vecp, nvecp, "allfiles", dir, re, (char *)0); | |
793 | } | |
794 | ||
c4e2cfdd RK |
795 | /** @brief Return the user we logged in with |
796 | * @param c Client | |
797 | * @return User name (owned by @p c, don't modify) | |
798 | */ | |
460b9539 | 799 | char *disorder_user(disorder_client *c) { |
800 | return c->user; | |
801 | } | |
802 | ||
c4e2cfdd RK |
803 | /** @brief Set a track preference |
804 | * @param c Client | |
805 | * @param track Track name (UTF-8) | |
806 | * @param key Preference name (UTF-8) | |
807 | * @param value Preference value (UTF-8) | |
808 | * @return 0 on success, non-0 on error | |
809 | */ | |
460b9539 | 810 | int disorder_set(disorder_client *c, const char *track, |
811 | const char *key, const char *value) { | |
812 | return disorder_simple(c, 0, "set", track, key, value, (char *)0); | |
813 | } | |
814 | ||
c4e2cfdd RK |
815 | /** @brief Unset a track preference |
816 | * @param c Client | |
817 | * @param track Track name (UTF-8) | |
818 | * @param key Preference name (UTF-8) | |
819 | * @return 0 on success, non-0 on error | |
820 | */ | |
460b9539 | 821 | int disorder_unset(disorder_client *c, const char *track, |
822 | const char *key) { | |
823 | return disorder_simple(c, 0, "unset", track, key, (char *)0); | |
824 | } | |
825 | ||
c4e2cfdd RK |
826 | /** @brief Get a track preference |
827 | * @param c Client | |
828 | * @param track Track name (UTF-8) | |
829 | * @param key Preference name (UTF-8) | |
830 | * @param valuep Where to store preference value (UTF-8) | |
831 | * @return 0 on success, non-0 on error | |
832 | */ | |
460b9539 | 833 | int disorder_get(disorder_client *c, |
834 | const char *track, const char *key, char **valuep) { | |
7b32e917 RK |
835 | return dequote(disorder_simple(c, valuep, "get", track, key, (char *)0), |
836 | valuep); | |
460b9539 | 837 | } |
838 | ||
839 | static void pref_error_handler(const char *msg, | |
840 | void attribute((unused)) *u) { | |
2e9ba080 | 841 | disorder_error(0, "error handling 'prefs' reply: %s", msg); |
460b9539 | 842 | } |
843 | ||
158d0961 | 844 | /** @brief Get all preferences for a trcak |
c4e2cfdd RK |
845 | * @param c Client |
846 | * @param track Track name | |
847 | * @param kp Where to store linked list of preferences | |
848 | * @return 0 on success, non-0 on error | |
849 | */ | |
460b9539 | 850 | int disorder_prefs(disorder_client *c, const char *track, struct kvp **kp) { |
851 | char **vec, **pvec; | |
2bead829 | 852 | int nvec, npvec, n, rc; |
460b9539 | 853 | struct kvp *k; |
854 | ||
2bead829 | 855 | if((rc = disorder_simple_list(c, &vec, &nvec, "prefs", track, (char *)0))) |
856 | return rc; | |
460b9539 | 857 | for(n = 0; n < nvec; ++n) { |
858 | if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pref_error_handler, 0))) | |
859 | return -1; | |
860 | if(npvec != 2) { | |
861 | pref_error_handler("malformed response", 0); | |
862 | return -1; | |
863 | } | |
864 | *kp = k = xmalloc(sizeof *k); | |
865 | k->name = pvec[0]; | |
866 | k->value = pvec[1]; | |
867 | kp = &k->next; | |
868 | } | |
869 | *kp = 0; | |
870 | return 0; | |
871 | } | |
872 | ||
c4e2cfdd RK |
873 | /** @brief Parse a boolean response |
874 | * @param cmd Command for use in error messsage | |
875 | * @param value Result from server | |
876 | * @param flagp Where to store result | |
877 | * @return 0 on success, non-0 on error | |
878 | */ | |
460b9539 | 879 | static int boolean(const char *cmd, const char *value, |
880 | int *flagp) { | |
881 | if(!strcmp(value, "yes")) *flagp = 1; | |
882 | else if(!strcmp(value, "no")) *flagp = 0; | |
883 | else { | |
2e9ba080 | 884 | disorder_error(0, "malformed response to '%s'", cmd); |
460b9539 | 885 | return -1; |
886 | } | |
887 | return 0; | |
888 | } | |
889 | ||
c4e2cfdd RK |
890 | /** @brief Test whether a track exists |
891 | * @param c Client | |
892 | * @param track Track name (UTF-8) | |
893 | * @param existsp Where to store result (non-0 iff does exist) | |
894 | * @return 0 on success, non-0 on error | |
895 | */ | |
460b9539 | 896 | int disorder_exists(disorder_client *c, const char *track, int *existsp) { |
897 | char *v; | |
2bead829 | 898 | int rc; |
460b9539 | 899 | |
2bead829 | 900 | if((rc = disorder_simple(c, &v, "exists", track, (char *)0))) |
901 | return rc; | |
460b9539 | 902 | return boolean("exists", v, existsp); |
903 | } | |
904 | ||
c4e2cfdd RK |
905 | /** @brief Test whether playing is enabled |
906 | * @param c Client | |
907 | * @param enabledp Where to store result (non-0 iff enabled) | |
908 | * @return 0 on success, non-0 on error | |
909 | */ | |
460b9539 | 910 | int disorder_enabled(disorder_client *c, int *enabledp) { |
911 | char *v; | |
2bead829 | 912 | int rc; |
460b9539 | 913 | |
2bead829 | 914 | if((rc = disorder_simple(c, &v, "enabled", (char *)0))) |
915 | return rc; | |
460b9539 | 916 | return boolean("enabled", v, enabledp); |
917 | } | |
918 | ||
c4e2cfdd RK |
919 | /** @brief Get the length of a track |
920 | * @param c Client | |
921 | * @param track Track name (UTF-8) | |
922 | * @param valuep Where to store length in seconds | |
923 | * @return 0 on success, non-0 on error | |
924 | * | |
925 | * If the length is unknown 0 is returned. | |
926 | */ | |
460b9539 | 927 | int disorder_length(disorder_client *c, const char *track, |
928 | long *valuep) { | |
929 | char *value; | |
2bead829 | 930 | int rc; |
460b9539 | 931 | |
2bead829 | 932 | if((rc = disorder_simple(c, &value, "length", track, (char *)0))) |
933 | return rc; | |
460b9539 | 934 | *valuep = atol(value); |
935 | return 0; | |
936 | } | |
937 | ||
c4e2cfdd RK |
938 | /** @brief Search for tracks |
939 | * @param c Client | |
940 | * @param terms Search terms (UTF-8) | |
941 | * @param vecp Where to store list (UTF-8) | |
942 | * @param nvecp Where to store number of items, or NULL | |
943 | * @return 0 on success, non-0 on error | |
944 | */ | |
460b9539 | 945 | int disorder_search(disorder_client *c, const char *terms, |
946 | char ***vecp, int *nvecp) { | |
947 | return disorder_simple_list(c, vecp, nvecp, "search", terms, (char *)0); | |
948 | } | |
949 | ||
c4e2cfdd RK |
950 | /** @brief Enable random play |
951 | * @param c Client | |
952 | * @return 0 on success, non-0 on error | |
953 | */ | |
460b9539 | 954 | int disorder_random_enable(disorder_client *c) { |
955 | return disorder_simple(c, 0, "random-enable", (char *)0); | |
956 | } | |
957 | ||
c4e2cfdd RK |
958 | /** @brief Disable random play |
959 | * @param c Client | |
960 | * @return 0 on success, non-0 on error | |
961 | */ | |
460b9539 | 962 | int disorder_random_disable(disorder_client *c) { |
963 | return disorder_simple(c, 0, "random-disable", (char *)0); | |
964 | } | |
965 | ||
c4e2cfdd RK |
966 | /** @brief Test whether random play is enabled |
967 | * @param c Client | |
158d0961 | 968 | * @param enabledp Where to store result (non-0 iff enabled) |
c4e2cfdd RK |
969 | * @return 0 on success, non-0 on error |
970 | */ | |
460b9539 | 971 | int disorder_random_enabled(disorder_client *c, int *enabledp) { |
972 | char *v; | |
2bead829 | 973 | int rc; |
460b9539 | 974 | |
2bead829 | 975 | if((rc = disorder_simple(c, &v, "random-enabled", (char *)0))) |
976 | return rc; | |
460b9539 | 977 | return boolean("random-enabled", v, enabledp); |
978 | } | |
979 | ||
c4e2cfdd RK |
980 | /** @brief Get server stats |
981 | * @param c Client | |
982 | * @param vecp Where to store list (UTF-8) | |
983 | * @param nvecp Where to store number of items, or NULL | |
984 | * @return 0 on success, non-0 on error | |
985 | */ | |
460b9539 | 986 | int disorder_stats(disorder_client *c, |
987 | char ***vecp, int *nvecp) { | |
988 | return disorder_simple_list(c, vecp, nvecp, "stats", (char *)0); | |
989 | } | |
990 | ||
c4e2cfdd RK |
991 | /** @brief Set volume |
992 | * @param c Client | |
993 | * @param left New left channel value | |
994 | * @param right New right channel value | |
995 | * @return 0 on success, non-0 on error | |
996 | */ | |
460b9539 | 997 | int disorder_set_volume(disorder_client *c, int left, int right) { |
998 | char *ls, *rs; | |
999 | ||
1000 | if(byte_asprintf(&ls, "%d", left) < 0 | |
1001 | || byte_asprintf(&rs, "%d", right) < 0) | |
1002 | return -1; | |
2bead829 | 1003 | return disorder_simple(c, 0, "volume", ls, rs, (char *)0); |
460b9539 | 1004 | } |
1005 | ||
c4e2cfdd RK |
1006 | /** @brief Get volume |
1007 | * @param c Client | |
1008 | * @param left Where to store left channel value | |
1009 | * @param right Where to store right channel value | |
1010 | * @return 0 on success, non-0 on error | |
1011 | */ | |
460b9539 | 1012 | int disorder_get_volume(disorder_client *c, int *left, int *right) { |
1013 | char *r; | |
2bead829 | 1014 | int rc; |
460b9539 | 1015 | |
2bead829 | 1016 | if((rc = disorder_simple(c, &r, "volume", (char *)0))) |
1017 | return rc; | |
460b9539 | 1018 | if(sscanf(r, "%d %d", left, right) != 2) { |
e9e8a16d | 1019 | c->last = "malformed volume response"; |
2e9ba080 | 1020 | disorder_error(0, "error parsing response to 'volume': '%s'", r); |
460b9539 | 1021 | return -1; |
1022 | } | |
1023 | return 0; | |
1024 | } | |
1025 | ||
c4e2cfdd RK |
1026 | /** @brief Log to a sink |
1027 | * @param c Client | |
1028 | * @param s Sink to write log lines to | |
1029 | * @return 0 on success, non-0 on error | |
1030 | */ | |
460b9539 | 1031 | int disorder_log(disorder_client *c, struct sink *s) { |
1032 | char *l; | |
2bead829 | 1033 | int rc; |
460b9539 | 1034 | |
2bead829 | 1035 | if((rc = disorder_simple(c, 0, "log", (char *)0))) |
1036 | return rc; | |
460b9539 | 1037 | while(inputline(c->ident, c->fpin, &l, '\n') >= 0 && strcmp(l, ".")) |
1038 | if(sink_printf(s, "%s\n", l) < 0) return -1; | |
e9e8a16d RK |
1039 | if(ferror(c->fpin) || feof(c->fpin)) { |
1040 | byte_xasprintf((char **)&c->last, "input error: %s", | |
1041 | ferror(c->fpin) ? strerror(errno) : "unexpxected EOF"); | |
1042 | return -1; | |
1043 | } | |
460b9539 | 1044 | return 0; |
1045 | } | |
1046 | ||
c4e2cfdd RK |
1047 | /** @brief Look up a track name part |
1048 | * @param c Client | |
1049 | * @param partp Where to store result (UTF-8) | |
1050 | * @param track Track name (UTF-8) | |
1051 | * @param context Context (usually "sort" or "display") (UTF-8) | |
1052 | * @param part Track part (UTF-8) | |
1053 | * @return 0 on success, non-0 on error | |
1054 | */ | |
460b9539 | 1055 | int disorder_part(disorder_client *c, char **partp, |
1056 | const char *track, const char *context, const char *part) { | |
7b32e917 RK |
1057 | return dequote(disorder_simple(c, partp, "part", |
1058 | track, context, part, (char *)0), partp); | |
460b9539 | 1059 | } |
1060 | ||
c4e2cfdd RK |
1061 | /** @brief Resolve aliases |
1062 | * @param c Client | |
158d0961 | 1063 | * @param trackp Where to store canonical name (UTF-8) |
c4e2cfdd RK |
1064 | * @param track Track name (UTF-8) |
1065 | * @return 0 on success, non-0 on error | |
1066 | */ | |
460b9539 | 1067 | int disorder_resolve(disorder_client *c, char **trackp, const char *track) { |
7b32e917 RK |
1068 | return dequote(disorder_simple(c, trackp, "resolve", track, (char *)0), |
1069 | trackp); | |
460b9539 | 1070 | } |
1071 | ||
c4e2cfdd RK |
1072 | /** @brief Pause the current track |
1073 | * @param c Client | |
1074 | * @return 0 on success, non-0 on error | |
1075 | */ | |
460b9539 | 1076 | int disorder_pause(disorder_client *c) { |
1077 | return disorder_simple(c, 0, "pause", (char *)0); | |
1078 | } | |
1079 | ||
c4e2cfdd RK |
1080 | /** @brief Resume the current track |
1081 | * @param c Client | |
1082 | * @return 0 on success, non-0 on error | |
1083 | */ | |
460b9539 | 1084 | int disorder_resume(disorder_client *c) { |
1085 | return disorder_simple(c, 0, "resume", (char *)0); | |
1086 | } | |
1087 | ||
c4e2cfdd RK |
1088 | /** @brief List all known tags |
1089 | * @param c Client | |
1090 | * @param vecp Where to store list (UTF-8) | |
1091 | * @param nvecp Where to store number of items, or NULL | |
1092 | * @return 0 on success, non-0 on error | |
1093 | */ | |
460b9539 | 1094 | int disorder_tags(disorder_client *c, |
1095 | char ***vecp, int *nvecp) { | |
1096 | return disorder_simple_list(c, vecp, nvecp, "tags", (char *)0); | |
1097 | } | |
1098 | ||
c4e2cfdd RK |
1099 | /** @brief List all known users |
1100 | * @param c Client | |
1101 | * @param vecp Where to store list (UTF-8) | |
1102 | * @param nvecp Where to store number of items, or NULL | |
1103 | * @return 0 on success, non-0 on error | |
1104 | */ | |
c3be4f19 RK |
1105 | int disorder_users(disorder_client *c, |
1106 | char ***vecp, int *nvecp) { | |
1107 | return disorder_simple_list(c, vecp, nvecp, "users", (char *)0); | |
1108 | } | |
1109 | ||
c4e2cfdd | 1110 | /** @brief Get recently added tracks |
2a10b70b | 1111 | * @param c Client |
c4e2cfdd | 1112 | * @param vecp Where to store pointer to list (UTF-8) |
2a10b70b RK |
1113 | * @param nvecp Where to store count |
1114 | * @param max Maximum tracks to fetch, or 0 for all available | |
1115 | * @return 0 on success, non-0 on error | |
1116 | */ | |
1117 | int disorder_new_tracks(disorder_client *c, | |
1118 | char ***vecp, int *nvecp, | |
1119 | int max) { | |
1120 | char limit[32]; | |
1121 | ||
1122 | sprintf(limit, "%d", max); | |
1123 | return disorder_simple_list(c, vecp, nvecp, "new", limit, (char *)0); | |
1124 | } | |
1125 | ||
c4e2cfdd RK |
1126 | /** @brief Set a global preference |
1127 | * @param c Client | |
1128 | * @param key Preference name (UTF-8) | |
1129 | * @param value Preference value (UTF-8) | |
1130 | * @return 0 on success, non-0 on error | |
1131 | */ | |
460b9539 | 1132 | int disorder_set_global(disorder_client *c, |
1133 | const char *key, const char *value) { | |
1134 | return disorder_simple(c, 0, "set-global", key, value, (char *)0); | |
1135 | } | |
1136 | ||
c4e2cfdd RK |
1137 | /** @brief Unset a global preference |
1138 | * @param c Client | |
1139 | * @param key Preference name (UTF-8) | |
1140 | * @return 0 on success, non-0 on error | |
1141 | */ | |
460b9539 | 1142 | int disorder_unset_global(disorder_client *c, const char *key) { |
1143 | return disorder_simple(c, 0, "unset-global", key, (char *)0); | |
1144 | } | |
1145 | ||
c4e2cfdd RK |
1146 | /** @brief Get a global preference |
1147 | * @param c Client | |
1148 | * @param key Preference name (UTF-8) | |
1149 | * @param valuep Where to store preference value (UTF-8) | |
1150 | * @return 0 on success, non-0 on error | |
1151 | */ | |
460b9539 | 1152 | int disorder_get_global(disorder_client *c, const char *key, char **valuep) { |
7b32e917 RK |
1153 | return dequote(disorder_simple(c, valuep, "get-global", key, (char *)0), |
1154 | valuep); | |
460b9539 | 1155 | } |
1156 | ||
c4e2cfdd RK |
1157 | /** @brief Get server's RTP address information |
1158 | * @param c Client | |
1159 | * @param addressp Where to store address (UTF-8) | |
1160 | * @param portp Where to store port (UTF-8) | |
1161 | * @return 0 on success, non-0 on error | |
1162 | */ | |
ca831831 RK |
1163 | int disorder_rtp_address(disorder_client *c, char **addressp, char **portp) { |
1164 | char *r; | |
1165 | int rc, n; | |
1166 | char **vec; | |
1167 | ||
1168 | if((rc = disorder_simple(c, &r, "rtp-address", (char *)0))) | |
1169 | return rc; | |
1170 | vec = split(r, &n, SPLIT_QUOTES, 0, 0); | |
1171 | if(n != 2) { | |
e9e8a16d | 1172 | c->last = "malformed RTP address"; |
2e9ba080 | 1173 | disorder_error(0, "malformed rtp-address reply"); |
ca831831 RK |
1174 | return -1; |
1175 | } | |
1176 | *addressp = vec[0]; | |
1177 | *portp = vec[1]; | |
1178 | return 0; | |
1179 | } | |
1180 | ||
c4e2cfdd RK |
1181 | /** @brief Create a user |
1182 | * @param c Client | |
1183 | * @param user Username | |
1184 | * @param password Password | |
1185 | * @param rights Initial rights or NULL to use default | |
1186 | * @return 0 on success, non-0 on error | |
1187 | */ | |
f0feb22e | 1188 | int disorder_adduser(disorder_client *c, |
0f55e905 RK |
1189 | const char *user, const char *password, |
1190 | const char *rights) { | |
1191 | return disorder_simple(c, 0, "adduser", user, password, rights, (char *)0); | |
f0feb22e RK |
1192 | } |
1193 | ||
c4e2cfdd RK |
1194 | /** @brief Delete a user |
1195 | * @param c Client | |
1196 | * @param user Username | |
1197 | * @return 0 on success, non-0 on error | |
1198 | */ | |
f0feb22e RK |
1199 | int disorder_deluser(disorder_client *c, const char *user) { |
1200 | return disorder_simple(c, 0, "deluser", user, (char *)0); | |
1201 | } | |
1202 | ||
c4e2cfdd RK |
1203 | /** @brief Get user information |
1204 | * @param c Client | |
1205 | * @param user Username | |
1206 | * @param key Property name (UTF-8) | |
1207 | * @param valuep Where to store value (UTF-8) | |
1208 | * @return 0 on success, non-0 on error | |
1209 | */ | |
a55c70c7 RK |
1210 | int disorder_userinfo(disorder_client *c, const char *user, const char *key, |
1211 | char **valuep) { | |
7b32e917 RK |
1212 | return dequote(disorder_simple(c, valuep, "userinfo", user, key, (char *)0), |
1213 | valuep); | |
5df73aeb RK |
1214 | } |
1215 | ||
c4e2cfdd RK |
1216 | /** @brief Set user information |
1217 | * @param c Client | |
1218 | * @param user Username | |
1219 | * @param key Property name (UTF-8) | |
1220 | * @param value New property value (UTF-8) | |
1221 | * @return 0 on success, non-0 on error | |
1222 | */ | |
5df73aeb RK |
1223 | int disorder_edituser(disorder_client *c, const char *user, |
1224 | const char *key, const char *value) { | |
1225 | return disorder_simple(c, 0, "edituser", user, key, value, (char *)0); | |
1226 | } | |
1227 | ||
c4e2cfdd RK |
1228 | /** @brief Register a user |
1229 | * @param c Client | |
1230 | * @param user Username | |
1231 | * @param password Password | |
1232 | * @param email Email address (UTF-8) | |
c4e2cfdd RK |
1233 | * @param confirmp Where to store confirmation string |
1234 | * @return 0 on success, non-0 on error | |
1235 | */ | |
ba39faf6 RK |
1236 | int disorder_register(disorder_client *c, const char *user, |
1237 | const char *password, const char *email, | |
1238 | char **confirmp) { | |
1239 | return dequote(disorder_simple(c, confirmp, "register", | |
1240 | user, password, email, (char *)0), | |
1241 | confirmp); | |
1242 | } | |
1243 | ||
c4e2cfdd RK |
1244 | /** @brief Confirm a user |
1245 | * @param c Client | |
1246 | * @param confirm Confirmation string | |
1247 | * @return 0 on success, non-0 on error | |
1248 | */ | |
ba39faf6 | 1249 | int disorder_confirm(disorder_client *c, const char *confirm) { |
30365519 | 1250 | char *u; |
1251 | int rc; | |
1252 | ||
1253 | if(!(rc = dequote(disorder_simple(c, &u, "confirm", confirm, (char *)0), | |
1254 | &u))) | |
1255 | c->user = u; | |
1256 | return rc; | |
ba39faf6 RK |
1257 | } |
1258 | ||
c4e2cfdd RK |
1259 | /** @brief Make a cookie for this login |
1260 | * @param c Client | |
1261 | * @param cookiep Where to store cookie string | |
1262 | * @return 0 on success, non-0 on error | |
1263 | */ | |
07969f9b RK |
1264 | int disorder_make_cookie(disorder_client *c, char **cookiep) { |
1265 | return dequote(disorder_simple(c, cookiep, "make-cookie", (char *)0), | |
1266 | cookiep); | |
1267 | } | |
1268 | ||
b42ffad6 | 1269 | /** @brief Revoke the cookie used by this session |
1270 | * @param c Client | |
1271 | * @return 0 on success, non-0 on error | |
1272 | */ | |
1273 | int disorder_revoke(disorder_client *c) { | |
1274 | return disorder_simple(c, 0, "revoke", (char *)0); | |
1275 | } | |
1276 | ||
6207d2f3 | 1277 | /** @brief Request a password reminder email |
1278 | * @param c Client | |
1279 | * @param user Username | |
1280 | * @return 0 on success, non-0 on error | |
1281 | */ | |
1282 | int disorder_reminder(disorder_client *c, const char *user) { | |
1283 | return disorder_simple(c, 0, "reminder", user, (char *)0); | |
1284 | } | |
1285 | ||
758aa6c3 RK |
1286 | /** @brief List scheduled events |
1287 | * @param c Client | |
1288 | * @param idsp Where to put list of event IDs | |
1289 | * @param nidsp Where to put count of event IDs, or NULL | |
1290 | * @return 0 on success, non-0 on error | |
1291 | */ | |
1292 | int disorder_schedule_list(disorder_client *c, char ***idsp, int *nidsp) { | |
1293 | return disorder_simple_list(c, idsp, nidsp, "schedule-list", (char *)0); | |
1294 | } | |
1295 | ||
1296 | /** @brief Delete a scheduled event | |
1297 | * @param c Client | |
1298 | * @param id Event ID to delete | |
1299 | * @return 0 on success, non-0 on error | |
1300 | */ | |
1301 | int disorder_schedule_del(disorder_client *c, const char *id) { | |
1302 | return disorder_simple(c, 0, "schedule-del", id, (char *)0); | |
1303 | } | |
1304 | ||
1305 | /** @brief Get details of a scheduled event | |
1306 | * @param c Client | |
1307 | * @param id Event ID | |
1308 | * @param actiondatap Where to put details | |
1309 | * @return 0 on success, non-0 on error | |
1310 | */ | |
1311 | int disorder_schedule_get(disorder_client *c, const char *id, | |
1312 | struct kvp **actiondatap) { | |
1313 | char **lines, **bits; | |
1314 | int rc, nbits; | |
1315 | ||
1316 | *actiondatap = 0; | |
1317 | if((rc = disorder_simple_list(c, &lines, NULL, | |
1318 | "schedule-get", id, (char *)0))) | |
1319 | return rc; | |
1320 | while(*lines) { | |
1321 | if(!(bits = split(*lines++, &nbits, SPLIT_QUOTES, 0, 0))) { | |
2e9ba080 | 1322 | disorder_error(0, "invalid schedule-get reply: cannot split line"); |
758aa6c3 RK |
1323 | return -1; |
1324 | } | |
1325 | if(nbits != 2) { | |
2e9ba080 | 1326 | disorder_error(0, "invalid schedule-get reply: wrong number of fields"); |
758aa6c3 RK |
1327 | return -1; |
1328 | } | |
1329 | kvp_set(actiondatap, bits[0], bits[1]); | |
1330 | } | |
1331 | return 0; | |
1332 | } | |
1333 | ||
1334 | /** @brief Add a scheduled event | |
1335 | * @param c Client | |
1336 | * @param when When to trigger the event | |
1337 | * @param priority Event priority ("normal" or "junk") | |
1338 | * @param action What action to perform | |
1339 | * @param ... Action-specific arguments | |
1340 | * @return 0 on success, non-0 on error | |
1341 | * | |
1342 | * For action @c "play" the next argument is the track. | |
1343 | * | |
1344 | * For action @c "set-global" next argument is the global preference name | |
1345 | * and the final argument the value to set it to, or (char *)0 to unset it. | |
1346 | */ | |
1347 | int disorder_schedule_add(disorder_client *c, | |
1348 | time_t when, | |
1349 | const char *priority, | |
1350 | const char *action, | |
1351 | ...) { | |
1352 | va_list ap; | |
1353 | char when_str[64]; | |
1354 | int rc; | |
1355 | ||
1356 | snprintf(when_str, sizeof when_str, "%lld", (long long)when); | |
1357 | va_start(ap, action); | |
1358 | if(!strcmp(action, "play")) | |
6c13a317 RK |
1359 | rc = disorder_simple(c, 0, "schedule-add", when_str, priority, |
1360 | action, va_arg(ap, char *), | |
1361 | (char *)0); | |
1362 | else if(!strcmp(action, "set-global")) { | |
1363 | const char *key = va_arg(ap, char *); | |
1364 | const char *value = va_arg(ap, char *); | |
1365 | rc = disorder_simple(c, 0,"schedule-add", when_str, priority, | |
1366 | action, key, value, | |
1367 | (char *)0); | |
1368 | } else | |
2e9ba080 | 1369 | disorder_fatal(0, "unknown action '%s'", action); |
758aa6c3 RK |
1370 | va_end(ap); |
1371 | return rc; | |
1372 | } | |
1373 | ||
d42e98ca RK |
1374 | /** @brief Adopt a track |
1375 | * @param c Client | |
1376 | * @param id Track ID to adopt | |
1377 | * @return 0 on success, non-0 on error | |
1378 | */ | |
1379 | int disorder_adopt(disorder_client *c, const char *id) { | |
1380 | return disorder_simple(c, 0, "adopt", id, (char *)0); | |
1381 | } | |
1382 | ||
39c53343 RK |
1383 | /** @brief Delete a playlist |
1384 | * @param c Client | |
1385 | * @param playlist Playlist to delete | |
1386 | * @return 0 on success, non-0 on error | |
1387 | */ | |
1388 | int disorder_playlist_delete(disorder_client *c, | |
1389 | const char *playlist) { | |
1390 | return disorder_simple(c, 0, "playlist-delete", playlist, (char *)0); | |
1391 | } | |
1392 | ||
1393 | /** @brief Get the contents of a playlist | |
1394 | * @param c Client | |
1395 | * @param playlist Playlist to get | |
1396 | * @param tracksp Where to put list of tracks | |
1397 | * @param ntracksp Where to put count of tracks | |
1398 | * @return 0 on success, non-0 on error | |
1399 | */ | |
1400 | int disorder_playlist_get(disorder_client *c, const char *playlist, | |
1401 | char ***tracksp, int *ntracksp) { | |
1402 | return disorder_simple_list(c, tracksp, ntracksp, | |
1403 | "playlist-get", playlist, (char *)0); | |
1404 | } | |
1405 | ||
1406 | /** @brief List all readable playlists | |
1407 | * @param c Client | |
1408 | * @param playlistsp Where to put list of playlists | |
1409 | * @param nplaylistsp Where to put count of playlists | |
1410 | * @return 0 on success, non-0 on error | |
1411 | */ | |
1412 | int disorder_playlists(disorder_client *c, | |
1413 | char ***playlistsp, int *nplaylistsp) { | |
1414 | return disorder_simple_list(c, playlistsp, nplaylistsp, | |
1415 | "playlists", (char *)0); | |
1416 | } | |
1417 | ||
1418 | /** @brief Get the sharing status of a playlist | |
1419 | * @param c Client | |
1420 | * @param playlist Playlist to inspect | |
1421 | * @param sharep Where to put sharing status | |
1422 | * @return 0 on success, non-0 on error | |
1423 | * | |
1424 | * Possible @p sharep values are @c public, @c private and @c shared. | |
1425 | */ | |
1426 | int disorder_playlist_get_share(disorder_client *c, const char *playlist, | |
1427 | char **sharep) { | |
1428 | return disorder_simple(c, sharep, | |
1429 | "playlist-get-share", playlist, (char *)0); | |
1430 | } | |
1431 | ||
1432 | /** @brief Get the sharing status of a playlist | |
1433 | * @param c Client | |
1434 | * @param playlist Playlist to modify | |
1435 | * @param share New sharing status | |
1436 | * @return 0 on success, non-0 on error | |
1437 | * | |
1438 | * Possible @p share values are @c public, @c private and @c shared. | |
1439 | */ | |
1440 | int disorder_playlist_set_share(disorder_client *c, const char *playlist, | |
1441 | const char *share) { | |
1442 | return disorder_simple(c, 0, | |
1443 | "playlist-set-share", playlist, share, (char *)0); | |
1444 | } | |
1445 | ||
1446 | /** @brief Lock a playlist for modifications | |
1447 | * @param c Client | |
1448 | * @param playlist Playlist to lock | |
1449 | * @return 0 on success, non-0 on error | |
1450 | */ | |
1451 | int disorder_playlist_lock(disorder_client *c, const char *playlist) { | |
1452 | return disorder_simple(c, 0, | |
1453 | "playlist-lock", playlist, (char *)0); | |
1454 | } | |
1455 | ||
1456 | /** @brief Unlock the locked playlist | |
1457 | * @param c Client | |
1458 | * @return 0 on success, non-0 on error | |
1459 | */ | |
1460 | int disorder_playlist_unlock(disorder_client *c) { | |
1461 | return disorder_simple(c, 0, | |
1462 | "playlist-unlock", (char *)0); | |
1463 | } | |
1464 | ||
1465 | /** @brief Set the contents of a playlst | |
1466 | * @param c Client | |
1467 | * @param playlist Playlist to modify | |
1468 | * @param tracks List of tracks | |
1469 | * @param ntracks Length of @p tracks (or -1 to count up to the first NULL) | |
1470 | * @return 0 on success, non-0 on error | |
1471 | */ | |
1472 | int disorder_playlist_set(disorder_client *c, | |
1473 | const char *playlist, | |
1474 | char **tracks, | |
1475 | int ntracks) { | |
1476 | return disorder_simple_body(c, 0, tracks, ntracks, | |
1477 | "playlist-set", playlist, (char *)0); | |
1478 | } | |
1479 | ||
460b9539 | 1480 | /* |
1481 | Local Variables: | |
1482 | c-basic-offset:2 | |
1483 | comment-column:40 | |
1484 | End: | |
1485 | */ |