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 | ||
08af2413 RK |
154 | /** @brief Marker for a command body */ |
155 | static const char disorder_body[1]; | |
156 | ||
c4e2cfdd RK |
157 | /** @brief Issue a command and parse a simple response |
158 | * @param c Client | |
159 | * @param rp Where to store result, or NULL | |
160 | * @param cmd Command | |
161 | * @param ap Arguments (UTF-8), terminated by (char *)0 | |
162 | * @return 0 on success, non-0 on error | |
163 | * | |
164 | * 5xx responses count as errors. | |
165 | * | |
166 | * @p rp will NOT be filled in for xx9 responses (where it is just | |
167 | * commentary for a command where it would normally be meaningful). | |
168 | * | |
169 | * NB that the response will NOT be converted to the local encoding | |
170 | * nor will quotes be stripped. See dequote(). | |
39c53343 | 171 | * |
08af2413 RK |
172 | * Put @ref disorder_body in the argument list followed by a char ** |
173 | * and int giving the body to follow the command. If the int is @c -1 | |
174 | * then the list is assumed to be NULL-terminated. | |
39c53343 RK |
175 | * |
176 | * Usually you would call this via one of the following interfaces: | |
177 | * - disorder_simple() | |
39c53343 | 178 | * - disorder_simple_list() |
c4e2cfdd | 179 | */ |
460b9539 | 180 | static int disorder_simple_v(disorder_client *c, |
181 | char **rp, | |
39c53343 | 182 | const char *cmd, |
39c53343 | 183 | va_list ap) { |
460b9539 | 184 | const char *arg; |
185 | struct dynstr d; | |
08af2413 RK |
186 | char **body = NULL; |
187 | int nbody = 0; | |
188 | int has_body = 0; | |
460b9539 | 189 | |
62ef2216 | 190 | if(!c->fpout) { |
e9e8a16d | 191 | c->last = "not connected"; |
2e9ba080 | 192 | disorder_error(0, "not connected to server"); |
62ef2216 | 193 | return -1; |
194 | } | |
460b9539 | 195 | if(cmd) { |
196 | dynstr_init(&d); | |
197 | dynstr_append_string(&d, cmd); | |
198 | while((arg = va_arg(ap, const char *))) { | |
08af2413 RK |
199 | if(arg == disorder_body) { |
200 | body = va_arg(ap, char **); | |
201 | nbody = va_arg(ap, int); | |
202 | has_body = 1; | |
203 | } else { | |
204 | dynstr_append(&d, ' '); | |
205 | dynstr_append_string(&d, quoteutf8(arg)); | |
206 | } | |
460b9539 | 207 | } |
208 | dynstr_append(&d, '\n'); | |
209 | dynstr_terminate(&d); | |
210 | D(("command: %s", d.vec)); | |
39c53343 RK |
211 | if(fputs(d.vec, c->fpout) < 0) |
212 | goto write_error; | |
f74f4f32 | 213 | xfree(d.vec); |
08af2413 | 214 | if(has_body) { |
39c53343 RK |
215 | if(nbody < 0) |
216 | for(nbody = 0; body[nbody]; ++nbody) | |
217 | ; | |
218 | for(int n = 0; n < nbody; ++n) { | |
219 | if(body[n][0] == '.') | |
220 | if(fputc('.', c->fpout) < 0) | |
221 | goto write_error; | |
222 | if(fputs(body[n], c->fpout) < 0) | |
223 | goto write_error; | |
224 | if(fputc('\n', c->fpout) < 0) | |
225 | goto write_error; | |
226 | } | |
227 | if(fputs(".\n", c->fpout) < 0) | |
228 | goto write_error; | |
460b9539 | 229 | } |
39c53343 RK |
230 | if(fflush(c->fpout)) |
231 | goto write_error; | |
460b9539 | 232 | } |
233 | return check_response(c, rp); | |
39c53343 RK |
234 | write_error: |
235 | byte_xasprintf((char **)&c->last, "write error: %s", strerror(errno)); | |
2e9ba080 | 236 | disorder_error(errno, "error writing to %s", c->ident); |
39c53343 | 237 | return -1; |
460b9539 | 238 | } |
239 | ||
c4e2cfdd RK |
240 | /** @brief Issue a command and parse a simple response |
241 | * @param c Client | |
242 | * @param rp Where to store result, or NULL (UTF-8) | |
243 | * @param cmd Command | |
244 | * @return 0 on success, non-0 on error | |
245 | * | |
246 | * The remaining arguments are command arguments, terminated by (char | |
247 | * *)0. They should be in UTF-8. | |
248 | * | |
249 | * 5xx responses count as errors. | |
250 | * | |
251 | * @p rp will NOT be filled in for xx9 responses (where it is just | |
252 | * commentary for a command where it would normally be meaningful). | |
253 | * | |
254 | * NB that the response will NOT be converted to the local encoding | |
255 | * nor will quotes be stripped. See dequote(). | |
460b9539 | 256 | */ |
257 | static int disorder_simple(disorder_client *c, | |
258 | char **rp, | |
259 | const char *cmd, ...) { | |
260 | va_list ap; | |
261 | int ret; | |
262 | ||
263 | va_start(ap, cmd); | |
08af2413 | 264 | ret = disorder_simple_v(c, rp, cmd, ap); |
460b9539 | 265 | va_end(ap); |
266 | return ret; | |
267 | } | |
268 | ||
0227f67d RK |
269 | /** @brief Dequote a result string |
270 | * @param rc 0 on success, non-0 on error | |
271 | * @param rp Where result string is stored (UTF-8) | |
272 | * @return @p rc | |
273 | * | |
274 | * This is used as a wrapper around disorder_simple() to dequote | |
275 | * results in place. | |
276 | */ | |
277 | static int dequote(int rc, char **rp) { | |
278 | char **rr; | |
2bead829 | 279 | |
0227f67d RK |
280 | if(!rc) { |
281 | if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) { | |
f74f4f32 | 282 | xfree(*rp); |
0227f67d | 283 | *rp = *rr; |
f74f4f32 | 284 | xfree(rr); |
0227f67d RK |
285 | return 0; |
286 | } | |
2e9ba080 | 287 | disorder_error(0, "invalid reply: %s", *rp); |
460b9539 | 288 | } |
2bead829 | 289 | return rc; |
460b9539 | 290 | } |
291 | ||
0227f67d | 292 | /** @brief Generic connection routine |
319d7107 | 293 | * @param conf Configuration to follow |
c4e2cfdd | 294 | * @param c Client |
0227f67d RK |
295 | * @param username Username to log in with or NULL |
296 | * @param password Password to log in with or NULL | |
297 | * @param cookie Cookie to log in with or NULL | |
c4e2cfdd RK |
298 | * @return 0 on success, non-0 on error |
299 | * | |
0227f67d RK |
300 | * @p cookie is tried first if not NULL. If it is NULL then @p |
301 | * username must not be. If @p username is not NULL then nor may @p | |
302 | * password be. | |
c4e2cfdd | 303 | */ |
319d7107 RK |
304 | int disorder_connect_generic(struct config *conf, |
305 | disorder_client *c, | |
306 | const char *username, | |
307 | const char *password, | |
308 | const char *cookie) { | |
f74f4f32 RK |
309 | int fd = -1, fd2 = -1, nrvec = 0, rc; |
310 | unsigned char *nonce = NULL; | |
460b9539 | 311 | size_t nl; |
f74f4f32 RK |
312 | char *res = NULL; |
313 | char *r = NULL, **rvec = NULL; | |
7b32e917 | 314 | const char *protocol, *algorithm, *challenge; |
f74f4f32 | 315 | struct sockaddr *sa = NULL; |
0227f67d | 316 | socklen_t salen; |
460b9539 | 317 | |
319d7107 | 318 | if((salen = find_server(conf, &sa, &c->ident)) == (socklen_t)-1) |
460b9539 | 319 | return -1; |
460b9539 | 320 | c->fpin = c->fpout = 0; |
321 | if((fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) { | |
e9e8a16d | 322 | byte_xasprintf((char **)&c->last, "socket: %s", strerror(errno)); |
2e9ba080 | 323 | disorder_error(errno, "error calling socket"); |
460b9539 | 324 | return -1; |
325 | } | |
0227f67d | 326 | if(connect(fd, sa, salen) < 0) { |
e9e8a16d | 327 | byte_xasprintf((char **)&c->last, "connect: %s", strerror(errno)); |
2e9ba080 | 328 | disorder_error(errno, "error calling connect"); |
460b9539 | 329 | goto error; |
330 | } | |
331 | if((fd2 = dup(fd)) < 0) { | |
e9e8a16d | 332 | byte_xasprintf((char **)&c->last, "dup: %s", strerror(errno)); |
2e9ba080 | 333 | disorder_error(errno, "error calling dup"); |
460b9539 | 334 | goto error; |
335 | } | |
336 | if(!(c->fpin = fdopen(fd, "rb"))) { | |
e9e8a16d | 337 | byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno)); |
2e9ba080 | 338 | disorder_error(errno, "error calling fdopen"); |
460b9539 | 339 | goto error; |
340 | } | |
341 | fd = -1; | |
342 | if(!(c->fpout = fdopen(fd2, "wb"))) { | |
e9e8a16d | 343 | byte_xasprintf((char **)&c->last, "fdopen: %s", strerror(errno)); |
2e9ba080 | 344 | disorder_error(errno, "error calling fdopen"); |
460b9539 | 345 | goto error; |
346 | } | |
347 | fd2 = -1; | |
2bead829 | 348 | if((rc = disorder_simple(c, &r, 0, (const char *)0))) |
349 | goto error_rc; | |
637fdea3 | 350 | if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0))) |
2bead829 | 351 | goto error; |
7b32e917 | 352 | if(nrvec != 3) { |
e9e8a16d | 353 | c->last = "cannot parse server greeting"; |
2e9ba080 | 354 | disorder_error(0, "cannot parse server greeting %s", r); |
2bead829 | 355 | goto error; |
637fdea3 | 356 | } |
f74f4f32 | 357 | protocol = rvec[0]; |
7b32e917 | 358 | if(strcmp(protocol, "2")) { |
e9e8a16d | 359 | c->last = "unknown protocol version"; |
2e9ba080 | 360 | disorder_error(0, "unknown protocol version: %s", protocol); |
2bead829 | 361 | goto error; |
7b32e917 | 362 | } |
f74f4f32 RK |
363 | algorithm = rvec[1]; |
364 | challenge = rvec[2]; | |
7b32e917 | 365 | if(!(nonce = unhex(challenge, &nl))) |
2bead829 | 366 | goto error; |
0227f67d RK |
367 | if(cookie) { |
368 | if(!dequote(disorder_simple(c, &c->user, "cookie", cookie, (char *)0), | |
369 | &c->user)) | |
370 | return 0; /* success */ | |
371 | if(!username) { | |
e9e8a16d | 372 | c->last = "cookie failed and no username"; |
2e9ba080 | 373 | disorder_error(0, "cookie did not work and no username available"); |
2bead829 | 374 | goto error; |
0227f67d RK |
375 | } |
376 | } | |
e9e8a16d RK |
377 | if(!(res = authhash(nonce, nl, password, algorithm))) { |
378 | c->last = "error computing authorization hash"; | |
379 | goto error; | |
380 | } | |
2bead829 | 381 | if((rc = disorder_simple(c, 0, "user", username, res, (char *)0))) |
382 | goto error_rc; | |
460b9539 | 383 | c->user = xstrdup(username); |
f74f4f32 RK |
384 | xfree(res); |
385 | free_strings(nrvec, rvec); | |
386 | xfree(nonce); | |
387 | xfree(sa); | |
388 | xfree(r); | |
460b9539 | 389 | return 0; |
390 | error: | |
2bead829 | 391 | rc = -1; |
392 | error_rc: | |
393 | if(c->fpin) { | |
394 | fclose(c->fpin); | |
395 | c->fpin = 0; | |
396 | } | |
397 | if(c->fpout) { | |
398 | fclose(c->fpout); | |
399 | c->fpout = 0; | |
400 | } | |
460b9539 | 401 | if(fd2 != -1) close(fd2); |
402 | if(fd != -1) close(fd); | |
2bead829 | 403 | return rc; |
460b9539 | 404 | } |
405 | ||
fdf98378 | 406 | /** @brief Connect a client with a specified username and password |
407 | * @param c Client | |
408 | * @param username Username to log in with | |
409 | * @param password Password to log in with | |
410 | * @return 0 on success, non-0 on error | |
411 | */ | |
412 | int disorder_connect_user(disorder_client *c, | |
413 | const char *username, | |
414 | const char *password) { | |
319d7107 RK |
415 | return disorder_connect_generic(config, |
416 | c, | |
fdf98378 | 417 | username, |
418 | password, | |
419 | 0); | |
420 | } | |
421 | ||
0227f67d RK |
422 | /** @brief Connect a client |
423 | * @param c Client | |
424 | * @return 0 on success, non-0 on error | |
425 | * | |
426 | * The connection will use the username and password found in @ref | |
427 | * config, or directly from the database if no password is found and | |
428 | * the database is readable (usually only for root). | |
429 | */ | |
430 | int disorder_connect(disorder_client *c) { | |
431 | const char *username, *password; | |
432 | ||
433 | if(!(username = config->username)) { | |
e9e8a16d | 434 | c->last = "no username"; |
2e9ba080 | 435 | disorder_error(0, "no username configured"); |
0227f67d RK |
436 | return -1; |
437 | } | |
438 | password = config->password; | |
199c2a23 RK |
439 | /* Maybe we can read the database */ |
440 | if(!password && trackdb_readable()) { | |
0227f67d RK |
441 | trackdb_init(TRACKDB_NO_RECOVER|TRACKDB_NO_UPGRADE); |
442 | trackdb_open(TRACKDB_READ_ONLY); | |
443 | password = trackdb_get_password(username); | |
444 | trackdb_close(); | |
445 | } | |
446 | if(!password) { | |
447 | /* Oh well */ | |
e9e8a16d | 448 | c->last = "no password"; |
2e9ba080 | 449 | disorder_error(0, "no password configured for user '%s'", username); |
0227f67d RK |
450 | return -1; |
451 | } | |
319d7107 RK |
452 | return disorder_connect_generic(config, |
453 | c, | |
0227f67d RK |
454 | username, |
455 | password, | |
456 | 0); | |
457 | } | |
458 | ||
459 | /** @brief Connect a client | |
460 | * @param c Client | |
461 | * @param cookie Cookie to log in with, or NULL | |
462 | * @return 0 on success, non-0 on error | |
463 | * | |
464 | * If @p cookie is NULL or does not work then we attempt to log in as | |
465 | * guest instead (so when the cookie expires only an extra round trip | |
466 | * is needed rathre than a complete new login). | |
467 | */ | |
468 | int disorder_connect_cookie(disorder_client *c, | |
469 | const char *cookie) { | |
319d7107 RK |
470 | return disorder_connect_generic(config, |
471 | c, | |
0227f67d RK |
472 | "guest", |
473 | "", | |
474 | cookie); | |
475 | } | |
476 | ||
c4e2cfdd RK |
477 | /** @brief Close a client |
478 | * @param c Client | |
479 | * @return 0 on succcess, non-0 on errior | |
480 | * | |
481 | * The client is still closed even on error. It might well be | |
482 | * appropriate to ignore the return value. | |
483 | */ | |
460b9539 | 484 | int disorder_close(disorder_client *c) { |
485 | int ret = 0; | |
486 | ||
487 | if(c->fpin) { | |
488 | if(fclose(c->fpin) < 0) { | |
e9e8a16d | 489 | byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno)); |
2e9ba080 | 490 | disorder_error(errno, "error calling fclose"); |
460b9539 | 491 | ret = -1; |
492 | } | |
493 | c->fpin = 0; | |
494 | } | |
495 | if(c->fpout) { | |
496 | if(fclose(c->fpout) < 0) { | |
e9e8a16d | 497 | byte_xasprintf((char **)&c->last, "fclose: %s", strerror(errno)); |
2e9ba080 | 498 | disorder_error(errno, "error calling fclose"); |
460b9539 | 499 | ret = -1; |
500 | } | |
501 | c->fpout = 0; | |
502 | } | |
f74f4f32 | 503 | xfree(c->ident); |
0227f67d | 504 | c->ident = 0; |
f74f4f32 | 505 | xfree(c->user); |
0227f67d | 506 | c->user = 0; |
375d9478 | 507 | return ret; |
460b9539 | 508 | } |
509 | ||
c4e2cfdd RK |
510 | /** @brief Move a track |
511 | * @param c Client | |
512 | * @param track Track to move (UTF-8) | |
513 | * @param delta Distance to move by | |
514 | * @return 0 on success, non-0 on error | |
515 | */ | |
460b9539 | 516 | int disorder_move(disorder_client *c, const char *track, int delta) { |
517 | char d[16]; | |
518 | ||
519 | byte_snprintf(d, sizeof d, "%d", delta); | |
520 | return disorder_simple(c, 0, "move", track, d, (char *)0); | |
521 | } | |
522 | ||
460b9539 | 523 | static void client_error(const char *msg, |
524 | void attribute((unused)) *u) { | |
2e9ba080 | 525 | disorder_error(0, "error parsing reply: %s", msg); |
460b9539 | 526 | } |
527 | ||
c4e2cfdd RK |
528 | /** @brief Get currently playing track |
529 | * @param c Client | |
530 | * @param qp Where to store track information | |
531 | * @return 0 on success, non-0 on error | |
532 | * | |
533 | * @p qp gets NULL if no track is playing. | |
534 | */ | |
460b9539 | 535 | int disorder_playing(disorder_client *c, struct queue_entry **qp) { |
536 | char *r; | |
537 | struct queue_entry *q; | |
2bead829 | 538 | int rc; |
460b9539 | 539 | |
2bead829 | 540 | if((rc = disorder_simple(c, &r, "playing", (char *)0))) |
541 | return rc; | |
460b9539 | 542 | if(r) { |
543 | q = xmalloc(sizeof *q); | |
544 | if(queue_unmarshall(q, r, client_error, 0)) | |
545 | return -1; | |
546 | *qp = q; | |
547 | } else | |
548 | *qp = 0; | |
549 | return 0; | |
550 | } | |
551 | ||
0590cedc | 552 | /** @brief Fetch the queue, recent list, etc */ |
460b9539 | 553 | static int disorder_somequeue(disorder_client *c, |
554 | const char *cmd, struct queue_entry **qp) { | |
555 | struct queue_entry *qh, **qt = &qh, *q; | |
556 | char *l; | |
2bead829 | 557 | int rc; |
460b9539 | 558 | |
2bead829 | 559 | if((rc = disorder_simple(c, 0, cmd, (char *)0))) |
560 | return rc; | |
460b9539 | 561 | while(inputline(c->ident, c->fpin, &l, '\n') >= 0) { |
562 | if(!strcmp(l, ".")) { | |
563 | *qt = 0; | |
564 | *qp = qh; | |
b251ac34 | 565 | xfree(l); |
460b9539 | 566 | return 0; |
567 | } | |
568 | q = xmalloc(sizeof *q); | |
569 | if(!queue_unmarshall(q, l, client_error, 0)) { | |
570 | *qt = q; | |
571 | qt = &q->next; | |
572 | } | |
b251ac34 | 573 | xfree(l); |
460b9539 | 574 | } |
e9e8a16d RK |
575 | if(ferror(c->fpin)) { |
576 | byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno)); | |
2e9ba080 | 577 | disorder_error(errno, "error reading %s", c->ident); |
e9e8a16d RK |
578 | } else { |
579 | c->last = "input error: unexpxected EOF"; | |
2e9ba080 | 580 | disorder_error(0, "error reading %s: unexpected EOF", c->ident); |
e9e8a16d | 581 | } |
460b9539 | 582 | return -1; |
583 | } | |
584 | ||
c4e2cfdd RK |
585 | /** @brief Read a dot-stuffed list |
586 | * @param c Client | |
587 | * @param vecp Where to store list (UTF-8) | |
588 | * @param nvecp Where to store number of items, or NULL | |
589 | * @return 0 on success, non-0 on error | |
590 | * | |
591 | * The list will have a final NULL not counted in @p nvecp. | |
592 | */ | |
460b9539 | 593 | static int readlist(disorder_client *c, char ***vecp, int *nvecp) { |
594 | char *l; | |
595 | struct vector v; | |
596 | ||
597 | vector_init(&v); | |
598 | while(inputline(c->ident, c->fpin, &l, '\n') >= 0) { | |
599 | if(!strcmp(l, ".")) { | |
600 | vector_terminate(&v); | |
601 | if(nvecp) | |
602 | *nvecp = v.nvec; | |
603 | *vecp = v.vec; | |
5d2b941b | 604 | xfree(l); |
460b9539 | 605 | return 0; |
606 | } | |
5d2b941b RK |
607 | vector_append(&v, xstrdup(l + (*l == '.'))); |
608 | xfree(l); | |
460b9539 | 609 | } |
e9e8a16d RK |
610 | if(ferror(c->fpin)) { |
611 | byte_xasprintf((char **)&c->last, "input error: %s", strerror(errno)); | |
2e9ba080 | 612 | disorder_error(errno, "error reading %s", c->ident); |
e9e8a16d RK |
613 | } else { |
614 | c->last = "input error: unexpxected EOF"; | |
2e9ba080 | 615 | disorder_error(0, "error reading %s: unexpected EOF", c->ident); |
e9e8a16d | 616 | } |
460b9539 | 617 | return -1; |
618 | } | |
619 | ||
c4e2cfdd RK |
620 | /** @brief Issue a comamnd and get a list response |
621 | * @param c Client | |
622 | * @param vecp Where to store list (UTF-8) | |
623 | * @param nvecp Where to store number of items, or NULL | |
624 | * @param cmd Command | |
625 | * @return 0 on success, non-0 on error | |
626 | * | |
627 | * The remaining arguments are command arguments, terminated by (char | |
628 | * *)0. They should be in UTF-8. | |
629 | * | |
630 | * 5xx responses count as errors. | |
39c53343 RK |
631 | * |
632 | * See disorder_simple(). | |
c4e2cfdd | 633 | */ |
460b9539 | 634 | static int disorder_simple_list(disorder_client *c, |
635 | char ***vecp, int *nvecp, | |
636 | const char *cmd, ...) { | |
637 | va_list ap; | |
638 | int ret; | |
639 | ||
640 | va_start(ap, cmd); | |
08af2413 | 641 | ret = disorder_simple_v(c, 0, cmd, ap); |
460b9539 | 642 | va_end(ap); |
643 | if(ret) return ret; | |
644 | return readlist(c, vecp, nvecp); | |
645 | } | |
646 | ||
c4e2cfdd RK |
647 | /** @brief Return the user we logged in with |
648 | * @param c Client | |
649 | * @return User name (owned by @p c, don't modify) | |
650 | */ | |
460b9539 | 651 | char *disorder_user(disorder_client *c) { |
652 | return c->user; | |
653 | } | |
654 | ||
460b9539 | 655 | static void pref_error_handler(const char *msg, |
656 | void attribute((unused)) *u) { | |
2e9ba080 | 657 | disorder_error(0, "error handling 'prefs' reply: %s", msg); |
460b9539 | 658 | } |
659 | ||
158d0961 | 660 | /** @brief Get all preferences for a trcak |
c4e2cfdd RK |
661 | * @param c Client |
662 | * @param track Track name | |
663 | * @param kp Where to store linked list of preferences | |
664 | * @return 0 on success, non-0 on error | |
665 | */ | |
460b9539 | 666 | int disorder_prefs(disorder_client *c, const char *track, struct kvp **kp) { |
667 | char **vec, **pvec; | |
2bead829 | 668 | int nvec, npvec, n, rc; |
460b9539 | 669 | struct kvp *k; |
670 | ||
2bead829 | 671 | if((rc = disorder_simple_list(c, &vec, &nvec, "prefs", track, (char *)0))) |
672 | return rc; | |
460b9539 | 673 | for(n = 0; n < nvec; ++n) { |
674 | if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pref_error_handler, 0))) | |
675 | return -1; | |
676 | if(npvec != 2) { | |
677 | pref_error_handler("malformed response", 0); | |
678 | return -1; | |
679 | } | |
680 | *kp = k = xmalloc(sizeof *k); | |
681 | k->name = pvec[0]; | |
682 | k->value = pvec[1]; | |
683 | kp = &k->next; | |
0d047a12 | 684 | xfree(pvec); |
460b9539 | 685 | } |
0d047a12 | 686 | free_strings(nvec, vec); |
460b9539 | 687 | *kp = 0; |
688 | return 0; | |
689 | } | |
690 | ||
c4e2cfdd RK |
691 | /** @brief Parse a boolean response |
692 | * @param cmd Command for use in error messsage | |
693 | * @param value Result from server | |
694 | * @param flagp Where to store result | |
695 | * @return 0 on success, non-0 on error | |
696 | */ | |
460b9539 | 697 | static int boolean(const char *cmd, const char *value, |
698 | int *flagp) { | |
699 | if(!strcmp(value, "yes")) *flagp = 1; | |
700 | else if(!strcmp(value, "no")) *flagp = 0; | |
701 | else { | |
2e9ba080 | 702 | disorder_error(0, "malformed response to '%s'", cmd); |
460b9539 | 703 | return -1; |
704 | } | |
705 | return 0; | |
706 | } | |
707 | ||
c4e2cfdd RK |
708 | /** @brief Set volume |
709 | * @param c Client | |
710 | * @param left New left channel value | |
711 | * @param right New right channel value | |
712 | * @return 0 on success, non-0 on error | |
713 | */ | |
460b9539 | 714 | int disorder_set_volume(disorder_client *c, int left, int right) { |
715 | char *ls, *rs; | |
716 | ||
717 | if(byte_asprintf(&ls, "%d", left) < 0 | |
718 | || byte_asprintf(&rs, "%d", right) < 0) | |
719 | return -1; | |
2bead829 | 720 | return disorder_simple(c, 0, "volume", ls, rs, (char *)0); |
460b9539 | 721 | } |
722 | ||
c4e2cfdd RK |
723 | /** @brief Get volume |
724 | * @param c Client | |
725 | * @param left Where to store left channel value | |
726 | * @param right Where to store right channel value | |
727 | * @return 0 on success, non-0 on error | |
728 | */ | |
460b9539 | 729 | int disorder_get_volume(disorder_client *c, int *left, int *right) { |
730 | char *r; | |
2bead829 | 731 | int rc; |
460b9539 | 732 | |
2bead829 | 733 | if((rc = disorder_simple(c, &r, "volume", (char *)0))) |
734 | return rc; | |
460b9539 | 735 | if(sscanf(r, "%d %d", left, right) != 2) { |
e9e8a16d | 736 | c->last = "malformed volume response"; |
2e9ba080 | 737 | disorder_error(0, "error parsing response to 'volume': '%s'", r); |
460b9539 | 738 | return -1; |
739 | } | |
740 | return 0; | |
741 | } | |
742 | ||
c4e2cfdd RK |
743 | /** @brief Log to a sink |
744 | * @param c Client | |
745 | * @param s Sink to write log lines to | |
746 | * @return 0 on success, non-0 on error | |
747 | */ | |
460b9539 | 748 | int disorder_log(disorder_client *c, struct sink *s) { |
749 | char *l; | |
2bead829 | 750 | int rc; |
460b9539 | 751 | |
2bead829 | 752 | if((rc = disorder_simple(c, 0, "log", (char *)0))) |
753 | return rc; | |
460b9539 | 754 | while(inputline(c->ident, c->fpin, &l, '\n') >= 0 && strcmp(l, ".")) |
755 | if(sink_printf(s, "%s\n", l) < 0) return -1; | |
e9e8a16d RK |
756 | if(ferror(c->fpin) || feof(c->fpin)) { |
757 | byte_xasprintf((char **)&c->last, "input error: %s", | |
758 | ferror(c->fpin) ? strerror(errno) : "unexpxected EOF"); | |
759 | return -1; | |
760 | } | |
460b9539 | 761 | return 0; |
762 | } | |
763 | ||
c4e2cfdd | 764 | /** @brief Get recently added tracks |
2a10b70b | 765 | * @param c Client |
c4e2cfdd | 766 | * @param vecp Where to store pointer to list (UTF-8) |
2a10b70b RK |
767 | * @param nvecp Where to store count |
768 | * @param max Maximum tracks to fetch, or 0 for all available | |
769 | * @return 0 on success, non-0 on error | |
770 | */ | |
771 | int disorder_new_tracks(disorder_client *c, | |
772 | char ***vecp, int *nvecp, | |
773 | int max) { | |
774 | char limit[32]; | |
775 | ||
776 | sprintf(limit, "%d", max); | |
777 | return disorder_simple_list(c, vecp, nvecp, "new", limit, (char *)0); | |
778 | } | |
779 | ||
c4e2cfdd RK |
780 | /** @brief Get server's RTP address information |
781 | * @param c Client | |
782 | * @param addressp Where to store address (UTF-8) | |
783 | * @param portp Where to store port (UTF-8) | |
784 | * @return 0 on success, non-0 on error | |
785 | */ | |
ca831831 RK |
786 | int disorder_rtp_address(disorder_client *c, char **addressp, char **portp) { |
787 | char *r; | |
788 | int rc, n; | |
789 | char **vec; | |
790 | ||
791 | if((rc = disorder_simple(c, &r, "rtp-address", (char *)0))) | |
792 | return rc; | |
793 | vec = split(r, &n, SPLIT_QUOTES, 0, 0); | |
794 | if(n != 2) { | |
e9e8a16d | 795 | c->last = "malformed RTP address"; |
2e9ba080 | 796 | disorder_error(0, "malformed rtp-address reply"); |
ca831831 RK |
797 | return -1; |
798 | } | |
799 | *addressp = vec[0]; | |
800 | *portp = vec[1]; | |
801 | return 0; | |
802 | } | |
803 | ||
758aa6c3 RK |
804 | /** @brief Get details of a scheduled event |
805 | * @param c Client | |
806 | * @param id Event ID | |
807 | * @param actiondatap Where to put details | |
808 | * @return 0 on success, non-0 on error | |
809 | */ | |
810 | int disorder_schedule_get(disorder_client *c, const char *id, | |
811 | struct kvp **actiondatap) { | |
812 | char **lines, **bits; | |
813 | int rc, nbits; | |
814 | ||
815 | *actiondatap = 0; | |
816 | if((rc = disorder_simple_list(c, &lines, NULL, | |
817 | "schedule-get", id, (char *)0))) | |
818 | return rc; | |
819 | while(*lines) { | |
820 | if(!(bits = split(*lines++, &nbits, SPLIT_QUOTES, 0, 0))) { | |
2e9ba080 | 821 | disorder_error(0, "invalid schedule-get reply: cannot split line"); |
758aa6c3 RK |
822 | return -1; |
823 | } | |
824 | if(nbits != 2) { | |
2e9ba080 | 825 | disorder_error(0, "invalid schedule-get reply: wrong number of fields"); |
758aa6c3 RK |
826 | return -1; |
827 | } | |
828 | kvp_set(actiondatap, bits[0], bits[1]); | |
829 | } | |
830 | return 0; | |
831 | } | |
832 | ||
833 | /** @brief Add a scheduled event | |
834 | * @param c Client | |
835 | * @param when When to trigger the event | |
836 | * @param priority Event priority ("normal" or "junk") | |
837 | * @param action What action to perform | |
838 | * @param ... Action-specific arguments | |
839 | * @return 0 on success, non-0 on error | |
840 | * | |
841 | * For action @c "play" the next argument is the track. | |
842 | * | |
843 | * For action @c "set-global" next argument is the global preference name | |
844 | * and the final argument the value to set it to, or (char *)0 to unset it. | |
845 | */ | |
846 | int disorder_schedule_add(disorder_client *c, | |
847 | time_t when, | |
848 | const char *priority, | |
849 | const char *action, | |
850 | ...) { | |
851 | va_list ap; | |
852 | char when_str[64]; | |
853 | int rc; | |
854 | ||
855 | snprintf(when_str, sizeof when_str, "%lld", (long long)when); | |
856 | va_start(ap, action); | |
857 | if(!strcmp(action, "play")) | |
6c13a317 RK |
858 | rc = disorder_simple(c, 0, "schedule-add", when_str, priority, |
859 | action, va_arg(ap, char *), | |
860 | (char *)0); | |
861 | else if(!strcmp(action, "set-global")) { | |
862 | const char *key = va_arg(ap, char *); | |
863 | const char *value = va_arg(ap, char *); | |
864 | rc = disorder_simple(c, 0,"schedule-add", when_str, priority, | |
865 | action, key, value, | |
866 | (char *)0); | |
867 | } else | |
2e9ba080 | 868 | disorder_fatal(0, "unknown action '%s'", action); |
758aa6c3 RK |
869 | va_end(ap); |
870 | return rc; | |
871 | } | |
872 | ||
7788b7c7 RK |
873 | #include "client-stubs.c" |
874 | ||
460b9539 | 875 | /* |
876 | Local Variables: | |
877 | c-basic-offset:2 | |
878 | comment-column:40 | |
879 | End: | |
880 | */ |