chiark / gitweb /
disobedience, playrtp: Have `playrtp' handle volume control.
[disorder] / lib / client.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
cca89d7c 3 * Copyright (C) 2004-13 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>
cca89d7c
RK
28#if HAVE_SYS_SOCKET_H
29# include <sys/socket.h>
30#endif
31#if HAVE_NETINET_IN_H
32# include <netinet/in.h>
33#endif
34#if HAVE_SYS_UN_H
35# include <sys/un.h>
36#endif
37#if HAVE_UNISTD_H
38# include <unistd.h>
39#endif
460b9539 40#include <errno.h>
cca89d7c
RK
41#if HAVE_NETDB_H
42# include <netdb.h>
43#endif
460b9539 44
45#include "log.h"
46#include "mem.h"
460b9539 47#include "queue.h"
48#include "client.h"
49#include "charset.h"
50#include "hex.h"
51#include "split.h"
52#include "vector.h"
53#include "inputline.h"
54#include "kvp.h"
55#include "syscalls.h"
56#include "printf.h"
57#include "sink.h"
58#include "addr.h"
59#include "authhash.h"
60#include "client-common.h"
5df73aeb 61#include "rights.h"
758aa6c3 62#include "kvp.h"
96f6312a 63#include "socketio.h"
460b9539 64
0590cedc 65/** @brief Client handle contents */
460b9539 66struct disorder_client {
0590cedc 67 /** @brief Stream to read from */
96f6312a 68 struct source *input;
0590cedc 69 /** @brief Stream to write to */
96f6312a 70 struct sink *output;
0590cedc 71 /** @brief Peer description */
460b9539 72 char *ident;
0590cedc 73 /** @brief Username */
460b9539 74 char *user;
0590cedc 75 /** @brief Report errors to @c stderr */
460b9539 76 int verbose;
0590cedc 77 /** @brief Last error string */
e9e8a16d 78 const char *last;
96af1d7e
RK
79 /** @brief Address family */
80 int family;
96f6312a
RK
81 /** @brief True if open */
82 int open;
83 /** @brief Socket I/O context */
84 struct socketio sio;
ff92debd
MW
85 /** @brief Whether to try to open a privileged connection */
86 int trypriv;
460b9539 87};
88
c4e2cfdd
RK
89/** @brief Create a new client
90 * @param verbose If nonzero, write extra junk to stderr
91 * @return Pointer to new client
92 *
fdf98378 93 * You must call disorder_connect(), disorder_connect_user() or
94 * disorder_connect_cookie() to connect it. Use disorder_close() to
95 * dispose of the client when finished with it.
c4e2cfdd 96 */
460b9539 97disorder_client *disorder_new(int verbose) {
98 disorder_client *c = xmalloc(sizeof (struct disorder_client));
99
100 c->verbose = verbose;
96af1d7e 101 c->family = -1;
ff92debd 102 c->trypriv = 1;
460b9539 103 return c;
104}
105
ff92debd
MW
106/** @brief Don't try to make a privileged connection
107 * @param c Client
108 *
109 * You must call this before any of the connection functions (e.g.,
110 * disorder_connect(), disorder_connect_user()), if at all.
111 */
112void disorder_force_unpriv(disorder_client *c) {
113 assert(!c->open);
114 c->trypriv = 0;
115}
116
96af1d7e
RK
117/** @brief Return the address family used by this client */
118int disorder_client_af(disorder_client *c) {
119 return c->family;
120}
121
c4e2cfdd
RK
122/** @brief Read a response line
123 * @param c Client
124 * @param rp Where to store response, or NULL (UTF-8)
125 * @return Response code 0-999 or -1 on error
126 */
460b9539 127static int response(disorder_client *c, char **rp) {
128 char *r;
96f6312a 129 char errbuf[1024];
460b9539 130
96f6312a
RK
131 if(inputlines(c->ident, c->input, &r, '\n')) {
132 byte_xasprintf((char **)&c->last, "input error: %s",
133 format_error(c->input->eclass, source_err(c->input), errbuf, sizeof errbuf));
460b9539 134 return -1;
e9e8a16d 135 }
460b9539 136 D(("response: %s", r));
137 if(rp)
138 *rp = r;
139 if(r[0] >= '0' && r[0] <= '9'
140 && r[1] >= '0' && r[1] <= '9'
141 && r[2] >= '0' && r[2] <= '9'
bb037be2 142 && r[3] == ' ') {
143 c->last = r + 4;
460b9539 144 return (r[0] * 10 + r[1]) * 10 + r[2] - 111 * '0';
bb037be2 145 } else {
e9e8a16d 146 c->last = "invalid reply format";
2e9ba080 147 disorder_error(0, "invalid reply format from %s", c->ident);
460b9539 148 return -1;
149 }
150}
151
bb037be2 152/** @brief Return last response string
153 * @param c Client
154 * @return Last response string (UTF-8, English) or NULL
155 */
156const char *disorder_last(disorder_client *c) {
157 return c->last;
158}
159
c4e2cfdd
RK
160/** @brief Read and partially parse a response
161 * @param c Client
162 * @param rp Where to store response text (or NULL) (UTF-8)
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.
460b9539 171 */
172static int check_response(disorder_client *c, char **rp) {
173 int rc;
174 char *r;
175
176 if((rc = response(c, &r)) == -1)
177 return -1;
178 else if(rc / 100 == 2) {
179 if(rp)
180 *rp = (rc % 10 == 9) ? 0 : xstrdup(r + 4);
f74f4f32 181 xfree(r);
460b9539 182 return 0;
183 } else {
184 if(c->verbose)
2e9ba080 185 disorder_error(0, "from %s: %s", c->ident, utf82mb(r));
f74f4f32 186 xfree(r);
2bead829 187 return rc;
460b9539 188 }
189}
190
c4e2cfdd
RK
191/** @brief Issue a command and parse a simple response
192 * @param c Client
193 * @param rp Where to store result, or NULL
194 * @param cmd Command
195 * @param ap Arguments (UTF-8), terminated by (char *)0
196 * @return 0 on success, non-0 on error
197 *
198 * 5xx responses count as errors.
199 *
200 * @p rp will NOT be filled in for xx9 responses (where it is just
201 * commentary for a command where it would normally be meaningful).
202 *
203 * NB that the response will NOT be converted to the local encoding
204 * nor will quotes be stripped. See dequote().
39c53343 205 *
ad131c25 206 * Put @ref disorder__body in the argument list followed by a char **
08af2413 207 * and int giving the body to follow the command. If the int is @c -1
0bc1d67c
RK
208 * then the list is assumed to be NULL-terminated. This may be used
209 * only once.
210 *
ad131c25 211 * Put @ref disorder__list in the argument list followed by a char **
0bc1d67c
RK
212 * and int giving a list of arguments to include. If the int is @c -1
213 * then the list is assumed to be NULL-terminated. This may be used
214 * any number of times.
39c53343 215 *
bcb2af72
RK
216 * Put @ref disorder__integer in the argument list followed by a long to
217 * send its value in decimal. This may be used any number of times.
218 *
219 * Put @ref disorder__time in the argument list followed by a time_t
220 * to send its value in decimal. This may be used any number of
221 * times.
222 *
39c53343
RK
223 * Usually you would call this via one of the following interfaces:
224 * - disorder_simple()
c4e2cfdd 225 */
460b9539 226static int disorder_simple_v(disorder_client *c,
227 char **rp,
39c53343 228 const char *cmd,
39c53343 229 va_list ap) {
460b9539 230 const char *arg;
231 struct dynstr d;
08af2413
RK
232 char **body = NULL;
233 int nbody = 0;
234 int has_body = 0;
96f6312a 235 char errbuf[1024];
460b9539 236
96f6312a 237 if(!c->open) {
e9e8a16d 238 c->last = "not connected";
2e9ba080 239 disorder_error(0, "not connected to server");
62ef2216 240 return -1;
241 }
460b9539 242 if(cmd) {
243 dynstr_init(&d);
244 dynstr_append_string(&d, cmd);
245 while((arg = va_arg(ap, const char *))) {
ad131c25 246 if(arg == disorder__body) {
08af2413
RK
247 body = va_arg(ap, char **);
248 nbody = va_arg(ap, int);
249 has_body = 1;
ad131c25 250 } else if(arg == disorder__list) {
0bc1d67c
RK
251 char **list = va_arg(ap, char **);
252 int nlist = va_arg(ap, int);
05b3f1f6 253 int n;
0bc1d67c
RK
254 if(nlist < 0) {
255 for(nlist = 0; list[nlist]; ++nlist)
256 ;
257 }
05b3f1f6 258 for(n = 0; n < nlist; ++n) {
0bc1d67c
RK
259 dynstr_append(&d, ' ');
260 dynstr_append_string(&d, quoteutf8(arg));
261 }
bcb2af72
RK
262 } else if(arg == disorder__integer) {
263 long n = va_arg(ap, long);
264 char buffer[16];
3c95f40e 265 byte_snprintf(buffer, sizeof buffer, "%ld", n);
bcb2af72
RK
266 dynstr_append(&d, ' ');
267 dynstr_append_string(&d, buffer);
268 } else if(arg == disorder__time) {
269 time_t n = va_arg(ap, time_t);
270 char buffer[16];
3c95f40e 271 byte_snprintf(buffer, sizeof buffer, "%lld", (long long)n);
bcb2af72
RK
272 dynstr_append(&d, ' ');
273 dynstr_append_string(&d, buffer);
08af2413
RK
274 } else {
275 dynstr_append(&d, ' ');
276 dynstr_append_string(&d, quoteutf8(arg));
277 }
460b9539 278 }
279 dynstr_append(&d, '\n');
280 dynstr_terminate(&d);
281 D(("command: %s", d.vec));
96f6312a 282 if(sink_write(c->output, d.vec, d.nvec) < 0)
39c53343 283 goto write_error;
f74f4f32 284 xfree(d.vec);
08af2413 285 if(has_body) {
05b3f1f6 286 int n;
39c53343
RK
287 if(nbody < 0)
288 for(nbody = 0; body[nbody]; ++nbody)
289 ;
05b3f1f6 290 for(n = 0; n < nbody; ++n) {
39c53343 291 if(body[n][0] == '.')
96f6312a 292 if(sink_writec(c->output, '.') < 0)
39c53343 293 goto write_error;
96f6312a 294 if(sink_writes(c->output, body[n]) < 0)
39c53343 295 goto write_error;
96f6312a 296 if(sink_writec(c->output, '\n') < 0)
39c53343
RK
297 goto write_error;
298 }
96f6312a 299 if(sink_writes(c->output, ".\n") < 0)
39c53343 300 goto write_error;
460b9539 301 }
96f6312a 302 if(sink_flush(c->output))
39c53343 303 goto write_error;
460b9539 304 }
305 return check_response(c, rp);
39c53343 306write_error:
96f6312a
RK
307 byte_xasprintf((char **)&c->last, "write error: %s",
308 format_error(c->output->eclass, sink_err(c->output), errbuf, sizeof errbuf));
309 disorder_error(0, "%s: %s", c->ident, c->last);
39c53343 310 return -1;
460b9539 311}
312
c4e2cfdd
RK
313/** @brief Issue a command and parse a simple response
314 * @param c Client
315 * @param rp Where to store result, or NULL (UTF-8)
316 * @param cmd Command
317 * @return 0 on success, non-0 on error
318 *
319 * The remaining arguments are command arguments, terminated by (char
320 * *)0. They should be in UTF-8.
321 *
322 * 5xx responses count as errors.
323 *
324 * @p rp will NOT be filled in for xx9 responses (where it is just
325 * commentary for a command where it would normally be meaningful).
326 *
327 * NB that the response will NOT be converted to the local encoding
328 * nor will quotes be stripped. See dequote().
460b9539 329 */
330static int disorder_simple(disorder_client *c,
331 char **rp,
332 const char *cmd, ...) {
333 va_list ap;
334 int ret;
335
336 va_start(ap, cmd);
08af2413 337 ret = disorder_simple_v(c, rp, cmd, ap);
460b9539 338 va_end(ap);
339 return ret;
340}
341
dab87ecc
RK
342/** @brief Issue a command and split the response
343 * @param c Client
344 * @param vecp Where to store results
345 * @param nvecp Where to store count of results
346 * @param expected Expected count (or -1 to not check)
347 * @param cmd Command
348 * @return 0 on success, non-0 on error
349 *
350 * The remaining arguments are command arguments, terminated by (char
351 * *)0. They should be in UTF-8.
352 *
353 * 5xx responses count as errors.
354 *
355 * @p rp will NOT be filled in for xx9 responses (where it is just
356 * commentary for a command where it would normally be meaningful).
357 *
358 * NB that the response will NOT be converted to the local encoding
359 * nor will quotes be stripped. See dequote().
360 */
361static int disorder_simple_split(disorder_client *c,
362 char ***vecp,
363 int *nvecp,
364 int expected,
365 const char *cmd, ...) {
366 va_list ap;
367 int ret;
368 char *r;
369 char **vec;
370 int nvec;
371
372 va_start(ap, cmd);
373 ret = disorder_simple_v(c, &r, cmd, ap);
374 va_end(ap);
375 if(!ret) {
376 vec = split(r, &nvec, SPLIT_QUOTES, 0, 0);
377 xfree(r);
378 if(expected < 0 || nvec == expected) {
379 *vecp = vec;
380 *nvecp = nvec;
381 } else {
382 disorder_error(0, "malformed reply to %s", cmd);
383 c->last = "malformed reply";
384 ret = -1;
385 free_strings(nvec, vec);
386 }
387 }
388 if(ret) {
389 *vecp = NULL;
390 *nvecp = 0;
391 }
392 return ret;
393}
394
0227f67d
RK
395/** @brief Dequote a result string
396 * @param rc 0 on success, non-0 on error
397 * @param rp Where result string is stored (UTF-8)
398 * @return @p rc
399 *
400 * This is used as a wrapper around disorder_simple() to dequote
401 * results in place.
402 */
403static int dequote(int rc, char **rp) {
404 char **rr;
2bead829 405
0227f67d
RK
406 if(!rc) {
407 if((rr = split(*rp, 0, SPLIT_QUOTES, 0, 0)) && *rr) {
f74f4f32 408 xfree(*rp);
0227f67d 409 *rp = *rr;
f74f4f32 410 xfree(rr);
0227f67d
RK
411 return 0;
412 }
2e9ba080 413 disorder_error(0, "invalid reply: %s", *rp);
460b9539 414 }
2bead829 415 return rc;
460b9539 416}
417
0227f67d 418/** @brief Generic connection routine
319d7107 419 * @param conf Configuration to follow
c4e2cfdd 420 * @param c Client
0227f67d
RK
421 * @param username Username to log in with or NULL
422 * @param password Password to log in with or NULL
423 * @param cookie Cookie to log in with or NULL
c4e2cfdd
RK
424 * @return 0 on success, non-0 on error
425 *
0227f67d
RK
426 * @p cookie is tried first if not NULL. If it is NULL then @p
427 * username must not be. If @p username is not NULL then nor may @p
428 * password be.
c4e2cfdd 429 */
319d7107
RK
430int disorder_connect_generic(struct config *conf,
431 disorder_client *c,
432 const char *username,
433 const char *password,
434 const char *cookie) {
96f6312a
RK
435 SOCKET sd = INVALID_SOCKET;
436 int nrvec = 0, rc;
f74f4f32 437 unsigned char *nonce = NULL;
460b9539 438 size_t nl;
f74f4f32
RK
439 char *res = NULL;
440 char *r = NULL, **rvec = NULL;
7b32e917 441 const char *protocol, *algorithm, *challenge;
f74f4f32 442 struct sockaddr *sa = NULL;
0227f67d 443 socklen_t salen;
96f6312a 444 char errbuf[1024];
460b9539 445
ff92debd
MW
446 if((salen = disorder_find_server(conf,
447 (c->trypriv ? 0 : DISORDER_FS_NOTPRIV),
448 &sa, &c->ident)) == (socklen_t)-1)
460b9539 449 return -1;
96f6312a
RK
450 c->input = 0;
451 c->output = 0;
452 if((sd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0) {
453 byte_xasprintf((char **)&c->last, "socket: %s",
454 format_error(ec_socket, socket_error(), errbuf, sizeof errbuf));
455 disorder_error(0, "%s", c->last);
460b9539 456 return -1;
457 }
96af1d7e 458 c->family = sa->sa_family;
96f6312a
RK
459 if(connect(sd, sa, salen) < 0) {
460 byte_xasprintf((char **)&c->last, "connect: %s",
461 format_error(ec_socket, socket_error(), errbuf, sizeof errbuf));
462 disorder_error(0, "%s", c->last);
460b9539 463 goto error;
464 }
96f6312a
RK
465 socketio_init(&c->sio, sd);
466 c->open = 1;
467 sd = INVALID_SOCKET;
468 c->output = sink_socketio(&c->sio);
469 c->input = source_socketio(&c->sio);
2bead829 470 if((rc = disorder_simple(c, &r, 0, (const char *)0)))
471 goto error_rc;
637fdea3 472 if(!(rvec = split(r, &nrvec, SPLIT_QUOTES, 0, 0)))
2bead829 473 goto error;
7b32e917 474 if(nrvec != 3) {
e9e8a16d 475 c->last = "cannot parse server greeting";
2e9ba080 476 disorder_error(0, "cannot parse server greeting %s", r);
2bead829 477 goto error;
637fdea3 478 }
f74f4f32 479 protocol = rvec[0];
7b32e917 480 if(strcmp(protocol, "2")) {
e9e8a16d 481 c->last = "unknown protocol version";
2e9ba080 482 disorder_error(0, "unknown protocol version: %s", protocol);
2bead829 483 goto error;
7b32e917 484 }
f74f4f32
RK
485 algorithm = rvec[1];
486 challenge = rvec[2];
7b32e917 487 if(!(nonce = unhex(challenge, &nl)))
2bead829 488 goto error;
0227f67d
RK
489 if(cookie) {
490 if(!dequote(disorder_simple(c, &c->user, "cookie", cookie, (char *)0),
491 &c->user))
492 return 0; /* success */
493 if(!username) {
e9e8a16d 494 c->last = "cookie failed and no username";
2e9ba080 495 disorder_error(0, "cookie did not work and no username available");
2bead829 496 goto error;
0227f67d
RK
497 }
498 }
e9e8a16d
RK
499 if(!(res = authhash(nonce, nl, password, algorithm))) {
500 c->last = "error computing authorization hash";
501 goto error;
502 }
2bead829 503 if((rc = disorder_simple(c, 0, "user", username, res, (char *)0)))
504 goto error_rc;
460b9539 505 c->user = xstrdup(username);
f74f4f32
RK
506 xfree(res);
507 free_strings(nrvec, rvec);
508 xfree(nonce);
509 xfree(sa);
510 xfree(r);
460b9539 511 return 0;
512error:
2bead829 513 rc = -1;
514error_rc:
96f6312a
RK
515 xfree(c->output);
516 c->output = NULL;
517 xfree(c->input);
518 c->input = NULL;
519 if(c->open) { socketio_close(&c->sio); c->open = 0; }
520 if(sd != INVALID_SOCKET) closesocket(sd);
2bead829 521 return rc;
460b9539 522}
523
fdf98378 524/** @brief Connect a client with a specified username and password
525 * @param c Client
526 * @param username Username to log in with
527 * @param password Password to log in with
528 * @return 0 on success, non-0 on error
529 */
530int disorder_connect_user(disorder_client *c,
531 const char *username,
532 const char *password) {
319d7107
RK
533 return disorder_connect_generic(config,
534 c,
fdf98378 535 username,
536 password,
537 0);
538}
539
0227f67d
RK
540/** @brief Connect a client
541 * @param c Client
542 * @return 0 on success, non-0 on error
543 *
544 * The connection will use the username and password found in @ref
545 * config, or directly from the database if no password is found and
546 * the database is readable (usually only for root).
547 */
548int disorder_connect(disorder_client *c) {
549 const char *username, *password;
550
551 if(!(username = config->username)) {
e9e8a16d 552 c->last = "no username";
2e9ba080 553 disorder_error(0, "no username configured");
0227f67d
RK
554 return -1;
555 }
556 password = config->password;
1be9101e
RK
557 /* If we're connecting as 'root' guess that we're the system root
558 * user (or the jukebox user), both of which can use the privileged
559 * socket. They can also furtle with the db directly: that is why
560 * privileged socket does not represent a privilege escalation. */
561 if(!password
562 && !strcmp(username, "root"))
563 password = "anything will do for root";
0227f67d
RK
564 if(!password) {
565 /* Oh well */
e9e8a16d 566 c->last = "no password";
2e9ba080 567 disorder_error(0, "no password configured for user '%s'", username);
0227f67d
RK
568 return -1;
569 }
319d7107
RK
570 return disorder_connect_generic(config,
571 c,
0227f67d
RK
572 username,
573 password,
574 0);
575}
576
577/** @brief Connect a client
578 * @param c Client
579 * @param cookie Cookie to log in with, or NULL
580 * @return 0 on success, non-0 on error
581 *
582 * If @p cookie is NULL or does not work then we attempt to log in as
583 * guest instead (so when the cookie expires only an extra round trip
70adc86b 584 * is needed rather than a complete new login).
0227f67d
RK
585 */
586int disorder_connect_cookie(disorder_client *c,
587 const char *cookie) {
319d7107
RK
588 return disorder_connect_generic(config,
589 c,
0227f67d
RK
590 "guest",
591 "",
592 cookie);
593}
594
c4e2cfdd
RK
595/** @brief Close a client
596 * @param c Client
597 * @return 0 on succcess, non-0 on errior
598 *
599 * The client is still closed even on error. It might well be
600 * appropriate to ignore the return value.
601 */
460b9539 602int disorder_close(disorder_client *c) {
603 int ret = 0;
604
96f6312a
RK
605 if(c->open)
606 socketio_close(&c->sio);
607 xfree(c->output);
608 c->output = NULL;
609 xfree(c->input);
610 c->input = NULL;
f74f4f32 611 xfree(c->ident);
0227f67d 612 c->ident = 0;
f74f4f32 613 xfree(c->user);
0227f67d 614 c->user = 0;
375d9478 615 return ret;
460b9539 616}
617
460b9539 618static void client_error(const char *msg,
619 void attribute((unused)) *u) {
2e9ba080 620 disorder_error(0, "error parsing reply: %s", msg);
460b9539 621}
622
ec9c0462 623/** @brief Get a single queue entry
c4e2cfdd 624 * @param c Client
ec9c0462 625 * @param cmd Command
c4e2cfdd
RK
626 * @param qp Where to store track information
627 * @return 0 on success, non-0 on error
c4e2cfdd 628 */
ec9c0462
RK
629static int onequeue(disorder_client *c, const char *cmd,
630 struct queue_entry **qp) {
460b9539 631 char *r;
632 struct queue_entry *q;
2bead829 633 int rc;
460b9539 634
ec9c0462 635 if((rc = disorder_simple(c, &r, cmd, (char *)0)))
2bead829 636 return rc;
460b9539 637 if(r) {
638 q = xmalloc(sizeof *q);
639 if(queue_unmarshall(q, r, client_error, 0))
640 return -1;
641 *qp = q;
642 } else
643 *qp = 0;
644 return 0;
645}
646
0590cedc 647/** @brief Fetch the queue, recent list, etc */
c12575c6
RK
648static int readqueue(disorder_client *c,
649 struct queue_entry **qp) {
460b9539 650 struct queue_entry *qh, **qt = &qh, *q;
651 char *l;
96f6312a 652 char errbuf[1024];
460b9539 653
96f6312a 654 while(inputlines(c->ident, c->input, &l, '\n') >= 0) {
460b9539 655 if(!strcmp(l, ".")) {
656 *qt = 0;
657 *qp = qh;
b251ac34 658 xfree(l);
460b9539 659 return 0;
660 }
661 q = xmalloc(sizeof *q);
662 if(!queue_unmarshall(q, l, client_error, 0)) {
663 *qt = q;
664 qt = &q->next;
665 }
b251ac34 666 xfree(l);
460b9539 667 }
96f6312a
RK
668 if(source_err(c->input)) {
669 byte_xasprintf((char **)&c->last, "input error: %s",
670 format_error(c->input->eclass, source_err(c->input), errbuf, sizeof errbuf));
e9e8a16d 671 } else {
c12575c6 672 c->last = "input error: unexpected EOF";
e9e8a16d 673 }
96f6312a 674 disorder_error(0, "%s: %s", c->ident, c->last);
460b9539 675 return -1;
676}
677
c4e2cfdd
RK
678/** @brief Read a dot-stuffed list
679 * @param c Client
680 * @param vecp Where to store list (UTF-8)
681 * @param nvecp Where to store number of items, or NULL
682 * @return 0 on success, non-0 on error
683 *
684 * The list will have a final NULL not counted in @p nvecp.
685 */
460b9539 686static int readlist(disorder_client *c, char ***vecp, int *nvecp) {
687 char *l;
688 struct vector v;
96f6312a 689 char errbuf[1024];
460b9539 690
691 vector_init(&v);
96f6312a 692 while(inputlines(c->ident, c->input, &l, '\n') >= 0) {
460b9539 693 if(!strcmp(l, ".")) {
694 vector_terminate(&v);
695 if(nvecp)
696 *nvecp = v.nvec;
697 *vecp = v.vec;
5d2b941b 698 xfree(l);
460b9539 699 return 0;
700 }
5d2b941b
RK
701 vector_append(&v, xstrdup(l + (*l == '.')));
702 xfree(l);
460b9539 703 }
96f6312a
RK
704 if(source_err(c->input)) {
705 byte_xasprintf((char **)&c->last, "input error: %s",
706 format_error(c->input->eclass, source_err(c->input), errbuf, sizeof errbuf));
e9e8a16d
RK
707 } else {
708 c->last = "input error: unexpxected EOF";
e9e8a16d 709 }
96f6312a 710 disorder_error(0, "%s: %s", c->ident, c->last);
460b9539 711 return -1;
712}
713
c4e2cfdd
RK
714/** @brief Return the user we logged in with
715 * @param c Client
716 * @return User name (owned by @p c, don't modify)
717 */
460b9539 718char *disorder_user(disorder_client *c) {
719 return c->user;
720}
721
5dc19ffd 722static void pairlist_error_handler(const char *msg,
460b9539 723 void attribute((unused)) *u) {
5dc19ffd 724 disorder_error(0, "error handling key-value pair reply: %s", msg);
460b9539 725}
726
5dc19ffd 727/** @brief Get a list of key-value pairs
c4e2cfdd 728 * @param c Client
c4e2cfdd 729 * @param kp Where to store linked list of preferences
5dc19ffd
RK
730 * @param cmd Command
731 * @param ... Arguments
c4e2cfdd
RK
732 * @return 0 on success, non-0 on error
733 */
5dc19ffd 734static int pairlist(disorder_client *c, struct kvp **kp, const char *cmd, ...) {
460b9539 735 char **vec, **pvec;
2bead829 736 int nvec, npvec, n, rc;
460b9539 737 struct kvp *k;
5dc19ffd 738 va_list ap;
460b9539 739
5dc19ffd
RK
740 va_start(ap, cmd);
741 rc = disorder_simple_v(c, 0, cmd, ap);
742 va_end(ap);
743 if(rc)
2bead829 744 return rc;
5dc19ffd
RK
745 if((rc = readlist(c, &vec, &nvec)))
746 return rc;
460b9539 747 for(n = 0; n < nvec; ++n) {
5dc19ffd 748 if(!(pvec = split(vec[n], &npvec, SPLIT_QUOTES, pairlist_error_handler, 0)))
460b9539 749 return -1;
750 if(npvec != 2) {
5dc19ffd 751 pairlist_error_handler("malformed response", 0);
460b9539 752 return -1;
753 }
754 *kp = k = xmalloc(sizeof *k);
755 k->name = pvec[0];
756 k->value = pvec[1];
757 kp = &k->next;
0d047a12 758 xfree(pvec);
460b9539 759 }
0d047a12 760 free_strings(nvec, vec);
460b9539 761 *kp = 0;
762 return 0;
763}
764
9e42afcd
RK
765#if _WIN32
766# define boolean bodge_boolean
767#endif
768
c4e2cfdd
RK
769/** @brief Parse a boolean response
770 * @param cmd Command for use in error messsage
771 * @param value Result from server
772 * @param flagp Where to store result
773 * @return 0 on success, non-0 on error
774 */
460b9539 775static int boolean(const char *cmd, const char *value,
776 int *flagp) {
777 if(!strcmp(value, "yes")) *flagp = 1;
778 else if(!strcmp(value, "no")) *flagp = 0;
779 else {
2e9ba080 780 disorder_error(0, "malformed response to '%s'", cmd);
460b9539 781 return -1;
782 }
783 return 0;
784}
785
c4e2cfdd
RK
786/** @brief Log to a sink
787 * @param c Client
788 * @param s Sink to write log lines to
789 * @return 0 on success, non-0 on error
790 */
460b9539 791int disorder_log(disorder_client *c, struct sink *s) {
792 char *l;
2bead829 793 int rc;
96f6312a 794 char errbuf[1024];
460b9539 795
2bead829 796 if((rc = disorder_simple(c, 0, "log", (char *)0)))
797 return rc;
96f6312a 798 while(inputlines(c->ident, c->input, &l, '\n') >= 0 && strcmp(l, "."))
460b9539 799 if(sink_printf(s, "%s\n", l) < 0) return -1;
96f6312a 800 if(source_err(c->input)) {
e9e8a16d 801 byte_xasprintf((char **)&c->last, "input error: %s",
96f6312a
RK
802 format_error(c->input->eclass, source_err(c->input), errbuf, sizeof errbuf));
803 return -1;
804 } else if(source_eof(c->input)) {
805 byte_xasprintf((char **)&c->last, "input error: unexpected EOF");
e9e8a16d
RK
806 return -1;
807 }
9e42afcd 808
460b9539 809 return 0;
810}
811
7788b7c7
RK
812#include "client-stubs.c"
813
460b9539 814/*
815Local Variables:
816c-basic-offset:2
817comment-column:40
818End:
819*/