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