chiark / gitweb /
server half of noticed.db
[disorder] / lib / eclient.c
CommitLineData
460b9539 1/*
2 * This file is part of DisOrder.
1020001c 3 * Copyright (C) 2006, 2007 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 */
0e4472a0 20/** @file lib/eclient.c
21 * @brief Client code for event-driven programs
22 */
460b9539 23
24#include <config.h>
25#include "types.h"
26
27#include <sys/types.h>
28#include <sys/socket.h>
29#include <netinet/in.h>
30#include <sys/un.h>
31#include <string.h>
32#include <stdio.h>
33#include <unistd.h>
34#include <errno.h>
35#include <netdb.h>
36#include <stdlib.h>
37#include <assert.h>
38#include <inttypes.h>
39#include <stddef.h>
40
41#include "log.h"
42#include "mem.h"
43#include "configuration.h"
44#include "queue.h"
45#include "eclient.h"
46#include "charset.h"
47#include "hex.h"
48#include "split.h"
49#include "vector.h"
50#include "inputline.h"
51#include "kvp.h"
52#include "syscalls.h"
53#include "printf.h"
54#include "addr.h"
55#include "authhash.h"
56#include "table.h"
57#include "client-common.h"
58
59/* TODO: more commands */
60
61/* Types *********************************************************************/
62
0e4472a0 63/** @brief Client state */
460b9539 64enum client_state {
0e4472a0 65 state_disconnected, /**< @brief not connected */
66 state_connecting, /**< @brief waiting for connect() */
67 state_connected, /**< @brief connected but not authenticated */
68 state_idle, /**< @brief not doing anything */
69 state_cmdresponse, /**< @brief waiting for command resonse */
70 state_body, /**< @brief accumulating body */
71 state_log, /**< @brief monitoring log */
460b9539 72};
73
0e4472a0 74/** @brief Names for @ref client_state */
460b9539 75static const char *const states[] = {
76 "disconnected",
77 "connecting",
78 "connected",
79 "idle",
80 "cmdresponse",
81 "body",
82 "log"
83};
84
85struct operation; /* forward decl */
86
0e4472a0 87/** @brief Type of an operation callback */
460b9539 88typedef void operation_callback(disorder_eclient *c, struct operation *op);
89
0e4472a0 90/** @brief A pending operation.
91 *
92 * This can be either a command or part of the authentication protocol. In the
93 * former case new commands are appended to the list, in the latter case they
94 * are inserted at the front. */
460b9539 95struct operation {
0e4472a0 96 struct operation *next; /**< @brief next operation */
97 char *cmd; /**< @brief command to send or 0 */
98 operation_callback *opcallback; /**< @brief internal completion callback */
99 void (*completed)(); /**< @brief user completion callback or 0 */
100 void *v; /**< @brief data for COMPLETED */
101 disorder_eclient *client; /**< @brief owning client */
ad58ebcc 102
103 /** @brief true if sent to server
104 *
105 * This is cleared by disorder_eclient_close(), forcing all queued
106 * commands to be transparently resent.
107 */
108 int sent;
460b9539 109};
110
0e4472a0 111/** @brief Client structure */
460b9539 112struct disorder_eclient {
113 const char *ident;
0e4472a0 114 int fd; /**< @brief connection to server */
115 enum client_state state; /**< @brief current state */
116 int authenticated; /**< @brief true when authenicated */
117 struct dynstr output; /**< @brief output buffer */
118 struct dynstr input; /**< @brief input buffer */
119 int eof; /**< @brief input buffer is at EOF */
120 const disorder_eclient_callbacks *callbacks; /**< @brief error callbacks */
121 void *u; /**< @brief user data */
122 struct operation *ops; /**< @brief queue of operations */
123 struct operation **opstail; /**< @brief queue tail */
460b9539 124 /* accumulated response */
0e4472a0 125 int rc; /**< @brief response code */
126 char *line; /**< @brief complete line */
127 struct vector vec; /**< @brief body */
128 const disorder_eclient_log_callbacks *log_callbacks; /**< @brief log callbacks */
129 void *log_v; /**< @brief user data */
8f763f1b 130 unsigned long statebits; /**< @brief latest state */
460b9539 131};
132
133/* Forward declarations ******************************************************/
134
135static int start_connect(void *cc,
136 const struct sockaddr *sa,
137 socklen_t len,
138 const char *ident);
139static void process_line(disorder_eclient *c, char *line);
140static int start_connect(void *cc,
141 const struct sockaddr *sa,
142 socklen_t len,
143 const char *ident);
144static void maybe_connected(disorder_eclient *c);
145static void authbanner_opcallback(disorder_eclient *c,
146 struct operation *op);
147static void authuser_opcallback(disorder_eclient *c,
148 struct operation *op);
149static void complete(disorder_eclient *c);
150static void send_output(disorder_eclient *c);
151static void put(disorder_eclient *c, const char *s, size_t n);
152static void read_input(disorder_eclient *c);
153static void stash_command(disorder_eclient *c,
154 int queuejump,
155 operation_callback *opcallback,
156 void (*completed)(),
157 void *v,
158 const char *cmd,
159 ...);
160static void log_opcallback(disorder_eclient *c, struct operation *op);
161static void logline(disorder_eclient *c, const char *line);
162static void logentry_completed(disorder_eclient *c, int nvec, char **vec);
163static void logentry_failed(disorder_eclient *c, int nvec, char **vec);
164static void logentry_moved(disorder_eclient *c, int nvec, char **vec);
165static void logentry_playing(disorder_eclient *c, int nvec, char **vec);
166static void logentry_queue(disorder_eclient *c, int nvec, char **vec);
167static void logentry_recent_added(disorder_eclient *c, int nvec, char **vec);
168static void logentry_recent_removed(disorder_eclient *c, int nvec, char **vec);
169static void logentry_removed(disorder_eclient *c, int nvec, char **vec);
170static void logentry_scratched(disorder_eclient *c, int nvec, char **vec);
171static void logentry_state(disorder_eclient *c, int nvec, char **vec);
172static void logentry_volume(disorder_eclient *c, int nvec, char **vec);
173
174/* Tables ********************************************************************/
175
0e4472a0 176/** @brief One possible log entry */
177struct logentry_handler {
178 const char *name; /**< @brief Entry name */
179 int min; /**< @brief Minimum arguments */
180 int max; /**< @brief Maximum arguments */
460b9539 181 void (*handler)(disorder_eclient *c,
182 int nvec,
0e4472a0 183 char **vec); /**< @brief Handler function */
184};
185
186/** @brief Table for parsing log entries */
187static const struct logentry_handler logentry_handlers[] = {
460b9539 188#define LE(X, MIN, MAX) { #X, MIN, MAX, logentry_##X }
189 LE(completed, 1, 1),
190 LE(failed, 2, 2),
191 LE(moved, 1, 1),
192 LE(playing, 1, 2),
193 LE(queue, 2, INT_MAX),
194 LE(recent_added, 2, INT_MAX),
195 LE(recent_removed, 1, 1),
196 LE(removed, 1, 2),
197 LE(scratched, 2, 2),
198 LE(state, 1, 1),
199 LE(volume, 2, 2)
200};
201
202/* Setup and teardown ********************************************************/
203
0e4472a0 204/** @brief Create a new client
205 *
206 * Does NOT connect the client - connections are made (and re-made) on demand.
207 */
460b9539 208disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb,
209 void *u) {
210 disorder_eclient *c = xmalloc(sizeof *c);
211 D(("disorder_eclient_new"));
212 c->fd = -1;
213 c->callbacks = cb;
214 c->u = u;
215 c->opstail = &c->ops;
216 vector_init(&c->vec);
217 dynstr_init(&c->input);
218 dynstr_init(&c->output);
1c0d78bd
RK
219 if(!config->password) {
220 error(0, "no password set");
221 return 0;
222 }
460b9539 223 return c;
224}
225
ad58ebcc 226/** @brief Disconnect a client
227 * @param c Client to disconnect
228 *
229 * NB that this routine just disconnnects the TCP connection. It does not
230 * destroy the client! If you continue to use it then it will attempt to
231 * reconnect.
232 */
460b9539 233void disorder_eclient_close(disorder_eclient *c) {
234 struct operation *op;
235
236 D(("disorder_eclient_close"));
237 if(c->fd != -1) {
238 D(("disorder_eclient_close closing fd %d", c->fd));
239 c->callbacks->poll(c->u, c, c->fd, 0);
240 xclose(c->fd);
241 c->fd = -1;
242 c->state = state_disconnected;
8f763f1b 243 c->statebits = 0;
460b9539 244 }
245 c->output.nvec = 0;
246 c->input.nvec = 0;
247 c->eof = 0;
248 c->authenticated = 0;
249 /* We'll need to resend all operations */
250 for(op = c->ops; op; op = op->next)
251 op->sent = 0;
6d1302f0
RK
252 /* Drop our use a hint that we're disconnected */
253 if(c->log_callbacks && c->log_callbacks->state)
254 c->log_callbacks->state(c->log_v, c->statebits);
460b9539 255}
256
8f763f1b
RK
257/** @brief Return current state */
258unsigned long disorder_eclient_state(const disorder_eclient *c) {
259 return c->statebits | (c->state > state_connected ? DISORDER_CONNECTED : 0);
ad58ebcc 260}
261
460b9539 262/* Error reporting ***********************************************************/
263
ad58ebcc 264/** @brief called when a connection error occurs
265 *
266 * After this called we will be disconnected (by disorder_eclient_close()),
267 * so there will be a reconnection before any commands can be sent.
268 */
460b9539 269static int comms_error(disorder_eclient *c, const char *fmt, ...) {
270 va_list ap;
271 char *s;
272
273 D(("comms_error"));
274 va_start(ap, fmt);
275 byte_xvasprintf(&s, fmt, ap);
276 va_end(ap);
277 disorder_eclient_close(c);
278 c->callbacks->comms_error(c->u, s);
279 return -1;
280}
281
0e4472a0 282/** @brief called when the server reports an error */
460b9539 283static int protocol_error(disorder_eclient *c, struct operation *op,
284 int code, const char *fmt, ...) {
285 va_list ap;
286 char *s;
287
288 D(("protocol_error"));
289 va_start(ap, fmt);
290 byte_xvasprintf(&s, fmt, ap);
291 va_end(ap);
292 c->callbacks->protocol_error(c->u, op->v, code, s);
293 return -1;
294}
295
296/* State machine *************************************************************/
297
0e4472a0 298/** @brief Called when there's something to do
299 * @param c Client
300 * @param mode bitmap of @ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE.
301 *
302 * This should be called from by your code when the file descriptor is readable
303 * or writable (as requested by the @c poll callback, see @ref
304 * disorder_eclient_callbacks) and in any case from time to time (with @p mode
305 * = 0) to allow for retries to work.
306 */
460b9539 307void disorder_eclient_polled(disorder_eclient *c, unsigned mode) {
308 struct operation *op;
ad58ebcc 309
460b9539 310 D(("disorder_eclient_polled fd=%d state=%s mode=[%s %s]",
311 c->fd, states[c->state],
312 mode & DISORDER_POLL_READ ? "READ" : "",
313 mode & DISORDER_POLL_WRITE ? "WRITE" : ""));
314 /* The pattern here is to check each possible state in turn and try to
315 * advance (though on error we might go back). If we advance we leave open
316 * the possibility of falling through to the next state, but we set the mode
317 * bits to 0, to avoid false positives (which matter more in some cases than
318 * others). */
319
320 if(c->state == state_disconnected) {
321 D(("state_disconnected"));
322 with_sockaddr(c, start_connect);
323 /* might now be state_disconnected (on error), state_connecting (slow
324 * connect) or state_connected (fast connect). If state_disconnected then
325 * we just rely on a periodic callback from the event loop sometime. */
326 mode = 0;
327 }
328
329 if(c->state == state_connecting && mode) {
330 D(("state_connecting"));
331 maybe_connected(c);
332 /* Might be state_disconnected (on error) or state_connected (on success).
333 * In the former case we rely on the event loop for a periodic callback to
334 * retry. */
335 mode = 0;
336 }
337
338 if(c->state == state_connected) {
339 D(("state_connected"));
340 /* We just connected. Initiate the authentication protocol. */
341 stash_command(c, 1/*queuejump*/, authbanner_opcallback,
342 0/*completed*/, 0/*v*/, 0/*cmd*/);
343 /* We never stay is state_connected very long. We could in principle jump
344 * straight to state_cmdresponse since there's actually no command to
345 * send, but that would arguably be cheating. */
346 c->state = state_idle;
347 }
348
349 if(c->state == state_idle) {
350 D(("state_idle"));
351 /* We are connected, and have finished any command we set off, look for
352 * some work to do */
353 if(c->ops) {
354 D(("have ops"));
355 if(c->authenticated) {
356 /* Transmit all unsent operations */
357 for(op = c->ops; op; op = op->next) {
358 if(!op->sent) {
359 put(c, op->cmd, strlen(op->cmd));
360 op->sent = 1;
361 }
362 }
363 } else {
364 /* Just send the head operation */
365 if(c->ops->cmd && !c->ops->sent) {
366 put(c, c->ops->cmd, strlen(c->ops->cmd));
367 c->ops->sent = 1;
368 }
369 }
370 /* Awaiting response for the operation at the head of the list */
371 c->state = state_cmdresponse;
372 } else
373 /* genuinely idle */
374 c->callbacks->report(c->u, 0);
375 }
376
377 if(c->state == state_cmdresponse
378 || c->state == state_body
379 || c->state == state_log) {
380 D(("state_%s", states[c->state]));
381 /* We are awaiting a response */
382 if(mode & DISORDER_POLL_WRITE) send_output(c);
383 if(mode & DISORDER_POLL_READ) read_input(c);
384 /* There are a couple of reasons we might want to re-enter the state
385 * machine from the top. state_idle is obvious: there may be further
386 * commands to process. Re-entering on state_disconnected means that we
387 * immediately retry connection if a comms error occurs during a command.
388 * This is different to the case where a connection fails, where we await a
389 * spontaneous call to initiate the retry. */
390 switch(c->state) {
391 case state_disconnected: /* lost connection */
392 case state_idle: /* completed a command */
393 D(("retrying"));
394 disorder_eclient_polled(c, 0);
395 return;
396 default:
397 break;
398 }
399 }
400
401 /* Figure out what to set the mode to */
402 switch(c->state) {
403 case state_disconnected:
404 D(("state_disconnected (2)"));
405 /* Probably an error occurred. Await a retry. */
406 mode = 0;
407 break;
408 case state_connecting:
409 D(("state_connecting (2)"));
410 /* Waiting for connect to complete */
411 mode = DISORDER_POLL_READ|DISORDER_POLL_WRITE;
412 break;
413 case state_connected:
414 D(("state_connected (2)"));
415 assert(!"should never be in state_connected here");
416 break;
417 case state_idle:
418 D(("state_idle (2)"));
419 /* Connected but nothing to do. */
420 mode = 0;
421 break;
422 case state_cmdresponse:
423 case state_body:
424 case state_log:
425 D(("state_%s (2)", states[c->state]));
426 /* Gathering a response. Wait for input. */
427 mode = DISORDER_POLL_READ;
428 /* Flush any pending output. */
429 if(c->output.nvec) mode |= DISORDER_POLL_WRITE;
430 break;
431 }
432 D(("fd=%d new mode [%s %s]",
433 c->fd,
434 mode & DISORDER_POLL_READ ? "READ" : "",
435 mode & DISORDER_POLL_WRITE ? "WRITE" : ""));
436 if(c->fd != -1) c->callbacks->poll(c->u, c, c->fd, mode);
437}
438
0e4472a0 439/** @brief Called to start connecting */
460b9539 440static int start_connect(void *cc,
441 const struct sockaddr *sa,
442 socklen_t len,
443 const char *ident) {
444 disorder_eclient *c = cc;
445
446 D(("start_connect"));
447 c->ident = xstrdup(ident);
448 if(c->fd != -1) {
449 xclose(c->fd);
450 c->fd = -1;
451 }
452 if((c->fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0)
453 return comms_error(c, "socket: %s", strerror(errno));
454 c->eof = 0;
455 nonblock(c->fd);
456 if(connect(c->fd, sa, len) < 0) {
457 switch(errno) {
458 case EINTR:
459 case EINPROGRESS:
460 c->state = state_connecting;
461 /* We are called from _polled so the state machine will get to do its
462 * thing */
463 return 0;
464 default:
465 /* Signal the error to the caller. */
466 return comms_error(c, "connecting to %s: %s", ident, strerror(errno));
467 }
468 } else
469 c->state = state_connected;
470 return 0;
471}
472
ad58ebcc 473/** @brief Called when poll triggers while waiting for a connection */
460b9539 474static void maybe_connected(disorder_eclient *c) {
475 /* We either connected, or got an error. */
476 int err;
477 socklen_t len = sizeof err;
478
479 D(("maybe_connected"));
480 /* Work around over-enthusiastic error slippage */
481 if(getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
482 err = errno;
483 if(err) {
484 /* The connection failed */
485 comms_error(c, "connecting to %s: %s", c->ident, strerror(err));
486 /* sets state_disconnected */
487 } else {
e836b1aa 488 char *r;
489
460b9539 490 /* The connection succeeded */
491 c->state = state_connected;
e836b1aa 492 byte_xasprintf(&r, "connected to %s", c->ident);
493 c->callbacks->report(c->u, r);
6d1302f0
RK
494 /* If this is a log client we expect to get a bunch of updates from the
495 * server straight away */
460b9539 496 }
497}
498
499/* Authentication ************************************************************/
500
501static void authbanner_opcallback(disorder_eclient *c,
502 struct operation *op) {
503 size_t nonce_len;
504 const unsigned char *nonce;
505 const char *res;
637fdea3
RK
506 char **rvec;
507 int nrvec;
508 const char *algo = "SHA1";
460b9539 509
510 D(("authbanner_opcallback"));
637fdea3
RK
511 if(c->rc / 100 != 2
512 || !(rvec = split(c->line + 4, &nrvec, SPLIT_QUOTES, 0, 0))
513 || nrvec < 1) {
514 /* Banner told us to go away, or was malformed. We cannot proceed. */
460b9539 515 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
516 disorder_eclient_close(c);
517 return;
518 }
637fdea3
RK
519 if(nrvec > 1) {
520 algo = *rvec++;
521 --nrvec;
522 }
d7167e89 523 nonce = unhex(rvec[0], &nonce_len);
637fdea3
RK
524 res = authhash(nonce, nonce_len, config->password, algo);
525 if(!res) {
526 protocol_error(c, op, c->rc, "%s: unknown authentication algorithm '%s'",
527 c->ident, algo);
528 disorder_eclient_close(c);
529 return;
530 }
460b9539 531 stash_command(c, 1/*queuejump*/, authuser_opcallback, 0/*completed*/, 0/*v*/,
532 "user", quoteutf8(config->username), quoteutf8(res),
533 (char *)0);
534}
535
536static void authuser_opcallback(disorder_eclient *c,
537 struct operation *op) {
e836b1aa 538 char *r;
539
460b9539 540 D(("authuser_opcallback"));
541 if(c->rc / 100 != 2) {
542 /* Wrong password or something. We cannot proceed. */
543 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
544 disorder_eclient_close(c);
545 return;
546 }
547 /* OK, we're authenticated now. */
548 c->authenticated = 1;
e836b1aa 549 byte_xasprintf(&r, "authenticated with %s", c->ident);
550 c->callbacks->report(c->u, r);
460b9539 551 if(c->log_callbacks && !(c->ops && c->ops->opcallback == log_opcallback))
552 /* We are a log client, switch to logging mode */
553 stash_command(c, 0/*queuejump*/, log_opcallback, 0/*completed*/, c->log_v,
554 "log", (char *)0);
555}
556
557/* Output ********************************************************************/
558
559/* Chop N bytes off the front of a dynstr */
560static void consume(struct dynstr *d, int n) {
561 D(("consume %d", n));
562 assert(d->nvec >= n);
563 memmove(d->vec, d->vec + n, d->nvec - n);
564 d->nvec -= n;
565}
566
567/* Write some bytes */
568static void put(disorder_eclient *c, const char *s, size_t n) {
569 D(("put %d %.*s", c->fd, (int)n, s));
570 dynstr_append_bytes(&c->output, s, n);
571}
572
573/* Called when we can write to our FD, or at any other time */
574static void send_output(disorder_eclient *c) {
575 int n;
576
577 D(("send_output %d bytes pending", c->output.nvec));
578 if(c->state > state_connecting && c->output.nvec) {
579 n = write(c->fd, c->output.vec, c->output.nvec);
580 if(n < 0) {
581 switch(errno) {
582 case EINTR:
583 case EAGAIN:
584 break;
585 default:
586 comms_error(c, "writing to %s: %s", c->ident, strerror(errno));
587 break;
588 }
589 } else
590 consume(&c->output, n);
591 }
592}
593
594/* Input *********************************************************************/
595
596/* Called when c->fd might be readable, or at any other time */
597static void read_input(disorder_eclient *c) {
598 char *nl;
599 int n;
600 char buffer[512];
601
602 D(("read_input in state %s", states[c->state]));
603 if(c->state <= state_connected) return; /* ignore bogus calls */
604 /* read some more input */
605 n = read(c->fd, buffer, sizeof buffer);
606 if(n < 0) {
607 switch(errno) {
608 case EINTR:
609 case EAGAIN:
610 break;
611 default:
612 comms_error(c, "reading from %s: %s", c->ident, strerror(errno));
613 break;
614 }
615 return; /* no new input to process */
616 } else if(n) {
617 D(("read %d bytes: [%.*s]", n, n, buffer));
618 dynstr_append_bytes(&c->input, buffer, n);
619 } else
620 c->eof = 1;
621 /* might have more than one line to process */
622 while(c->state > state_connecting
623 && (nl = memchr(c->input.vec, '\n', c->input.nvec))) {
624 process_line(c, xstrndup(c->input.vec, nl - c->input.vec));
625 /* we might have disconnected along the way, which zogs the input buffer */
626 if(c->state > state_connecting)
627 consume(&c->input, (nl - c->input.vec) + 1);
628 }
346ba8d5 629 if(c->eof) {
460b9539 630 comms_error(c, "reading from %s: server disconnected", c->ident);
346ba8d5 631 c->authenticated = 0;
632 }
460b9539 633}
634
635/* called with a line that has just been read */
636static void process_line(disorder_eclient *c, char *line) {
637 D(("process_line %d [%s]", c->fd, line));
638 switch(c->state) {
639 case state_cmdresponse:
640 /* This is the first line of a response */
641 if(!(line[0] >= '0' && line[0] <= '9'
642 && line[1] >= '0' && line[1] <= '9'
643 && line[2] >= '0' && line[2] <= '9'
644 && line[3] == ' '))
645 fatal(0, "invalid response from server: %s", line);
646 c->rc = (line[0] * 10 + line[1]) * 10 + line[2] - 111 * '0';
647 c->line = line;
648 switch(c->rc % 10) {
649 case 3:
650 /* We need to collect the body. */
651 c->state = state_body;
652 c->vec.nvec = 0;
653 break;
654 case 4:
655 assert(c->log_callbacks != 0);
656 if(c->log_callbacks->connected)
657 c->log_callbacks->connected(c->log_v);
658 c->state = state_log;
659 break;
660 default:
661 /* We've got the whole response. Go into the idle state so the state
662 * machine knows we're done and then call the operation callback. */
663 complete(c);
664 break;
665 }
666 break;
667 case state_body:
668 if(strcmp(line, ".")) {
669 /* A line from the body */
670 vector_append(&c->vec, line + (line[0] == '.'));
671 } else {
672 /* End of the body. */
673 vector_terminate(&c->vec);
674 complete(c);
675 }
676 break;
677 case state_log:
678 if(strcmp(line, ".")) {
679 logline(c, line + (line[0] == '.'));
680 } else
681 complete(c);
682 break;
683 default:
684 assert(!"wrong state for location");
685 break;
686 }
687}
688
689/* Called when an operation completes */
690static void complete(disorder_eclient *c) {
691 struct operation *op;
692
693 D(("complete"));
694 /* Pop the operation off the queue */
695 op = c->ops;
696 c->ops = op->next;
697 if(c->opstail == &op->next)
698 c->opstail = &c->ops;
699 /* If we've pipelined a command ahead then we go straight to cmdresponser.
700 * Otherwise we go to idle, which will arrange further sends. */
701 c->state = c->ops && c->ops->sent ? state_cmdresponse : state_idle;
702 op->opcallback(c, op);
703 /* Note that we always call the opcallback even on error, though command
704 * opcallbacks generally always do the same error handling, i.e. just call
705 * protocol_error(). It's the auth* opcallbacks that have different
706 * behaviour. */
707}
708
709/* Operation setup ***********************************************************/
710
711static void stash_command_vector(disorder_eclient *c,
712 int queuejump,
713 operation_callback *opcallback,
714 void (*completed)(),
715 void *v,
716 int ncmd,
717 char **cmd) {
718 struct operation *op = xmalloc(sizeof *op);
719 struct dynstr d;
720 int n;
721
722 if(cmd) {
723 dynstr_init(&d);
724 for(n = 0; n < ncmd; ++n) {
725 if(n)
726 dynstr_append(&d, ' ');
727 dynstr_append_string(&d, quoteutf8(cmd[n]));
728 }
729 dynstr_append(&d, '\n');
730 dynstr_terminate(&d);
731 op->cmd = d.vec;
732 } else
733 op->cmd = 0; /* usually, awaiting challenge */
734 op->opcallback = opcallback;
735 op->completed = completed;
736 op->v = v;
737 op->next = 0;
738 op->client = c;
739 assert(op->sent == 0);
740 if(queuejump) {
741 /* Authentication operations jump the queue of useful commands */
742 op->next = c->ops;
743 c->ops = op;
744 if(c->opstail == &c->ops)
745 c->opstail = &op->next;
746 for(op = c->ops; op; op = op->next)
747 assert(!op->sent);
748 } else {
749 *c->opstail = op;
750 c->opstail = &op->next;
751 }
752}
753
754static void vstash_command(disorder_eclient *c,
755 int queuejump,
756 operation_callback *opcallback,
757 void (*completed)(),
758 void *v,
759 const char *cmd, va_list ap) {
760 char *arg;
761 struct vector vec;
762
763 D(("vstash_command %s", cmd ? cmd : "NULL"));
764 if(cmd) {
765 vector_init(&vec);
766 vector_append(&vec, (char *)cmd);
767 while((arg = va_arg(ap, char *)))
768 vector_append(&vec, arg);
769 stash_command_vector(c, queuejump, opcallback, completed, v,
770 vec.nvec, vec.vec);
771 } else
772 stash_command_vector(c, queuejump, opcallback, completed, v, 0, 0);
773}
774
775static void stash_command(disorder_eclient *c,
776 int queuejump,
777 operation_callback *opcallback,
778 void (*completed)(),
779 void *v,
780 const char *cmd,
781 ...) {
782 va_list ap;
783
784 va_start(ap, cmd);
785 vstash_command(c, queuejump, opcallback, completed, v, cmd, ap);
786 va_end(ap);
787}
788
789/* Command support ***********************************************************/
790
791/* for commands with a simple string response */
792static void string_response_opcallback(disorder_eclient *c,
793 struct operation *op) {
794 D(("string_response_callback"));
795 if(c->rc / 100 == 2) {
796 if(op->completed)
797 ((disorder_eclient_string_response *)op->completed)(op->v, c->line + 4);
798 } else
799 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
800}
801
802/* for commands with a simple integer response */
803static void integer_response_opcallback(disorder_eclient *c,
804 struct operation *op) {
805 D(("string_response_callback"));
806 if(c->rc / 100 == 2) {
807 if(op->completed)
808 ((disorder_eclient_integer_response *)op->completed)
809 (op->v, strtol(c->line + 4, 0, 10));
810 } else
811 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
812}
813
814/* for commands with no response */
815static void no_response_opcallback(disorder_eclient *c,
816 struct operation *op) {
817 D(("no_response_callback"));
818 if(c->rc / 100 == 2) {
819 if(op->completed)
820 ((disorder_eclient_no_response *)op->completed)(op->v);
821 } else
822 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
823}
824
825/* error callback for queue_unmarshall */
826static void eclient_queue_error(const char *msg,
827 void *u) {
828 struct operation *op = u;
829
830 protocol_error(op->client, op, -1, "error parsing queue entry: %s", msg);
831}
832
833/* for commands that expect a queue dump */
834static void queue_response_opcallback(disorder_eclient *c,
835 struct operation *op) {
836 int n;
837 struct queue_entry *q, *qh = 0, **qtail = &qh, *qlast = 0;
838
839 D(("queue_response_callback"));
840 if(c->rc / 100 == 2) {
841 /* parse the queue */
842 for(n = 0; n < c->vec.nvec; ++n) {
843 q = xmalloc(sizeof *q);
844 D(("queue_unmarshall %s", c->vec.vec[n]));
845 if(!queue_unmarshall(q, c->vec.vec[n], eclient_queue_error, op)) {
846 q->prev = qlast;
847 *qtail = q;
848 qtail = &q->next;
849 qlast = q;
850 }
851 }
852 if(op->completed)
853 ((disorder_eclient_queue_response *)op->completed)(op->v, qh);
854 } else
855 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
856}
857
858/* for 'playing' */
859static void playing_response_opcallback(disorder_eclient *c,
860 struct operation *op) {
861 struct queue_entry *q;
862
863 D(("playing_response_callback"));
864 if(c->rc / 100 == 2) {
865 switch(c->rc % 10) {
866 case 2:
867 if(queue_unmarshall(q = xmalloc(sizeof *q), c->line + 4,
868 eclient_queue_error, c))
869 return;
870 break;
871 case 9:
872 q = 0;
873 break;
874 default:
875 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
876 return;
877 }
878 if(op->completed)
879 ((disorder_eclient_queue_response *)op->completed)(op->v, q);
880 } else
881 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
882}
883
884/* for commands that expect a list of some sort */
885static void list_response_opcallback(disorder_eclient *c,
886 struct operation *op) {
887 D(("list_response_callback"));
888 if(c->rc / 100 == 2) {
889 if(op->completed)
890 ((disorder_eclient_list_response *)op->completed)(op->v,
891 c->vec.nvec,
892 c->vec.vec);
893 } else
894 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
895}
896
897/* for volume */
898static void volume_response_opcallback(disorder_eclient *c,
899 struct operation *op) {
900 int l, r;
901
902 D(("volume_response_callback"));
903 if(c->rc / 100 == 2) {
904 if(op->completed) {
905 if(sscanf(c->line + 4, "%d %d", &l, &r) != 2 || l < 0 || r < 0)
906 protocol_error(c, op, -1, "%s: invalid volume response: %s",
907 c->ident, c->line);
908 else
909 ((disorder_eclient_volume_response *)op->completed)(op->v, l, r);
910 }
911 } else
912 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
913}
914
915static int simple(disorder_eclient *c,
916 operation_callback *opcallback,
917 void (*completed)(),
918 void *v,
919 const char *cmd, ...) {
920 va_list ap;
921
922 va_start(ap, cmd);
923 vstash_command(c, 0/*queuejump*/, opcallback, completed, v, cmd, ap);
924 va_end(ap);
925 /* Give the state machine a kick, since we might be in state_idle */
926 disorder_eclient_polled(c, 0);
927 return 0;
928}
929
930/* Commands ******************************************************************/
931
932int disorder_eclient_version(disorder_eclient *c,
933 disorder_eclient_string_response *completed,
934 void *v) {
935 return simple(c, string_response_opcallback, (void (*)())completed, v,
936 "version", (char *)0);
937}
938
939int disorder_eclient_namepart(disorder_eclient *c,
940 disorder_eclient_string_response *completed,
941 const char *track,
942 const char *context,
943 const char *part,
944 void *v) {
945 return simple(c, string_response_opcallback, (void (*)())completed, v,
946 "part", track, context, part, (char *)0);
947}
948
949int disorder_eclient_play(disorder_eclient *c,
950 const char *track,
951 disorder_eclient_no_response *completed,
952 void *v) {
953 return simple(c, no_response_opcallback, (void (*)())completed, v,
954 "play", track, (char *)0);
955}
956
957int disorder_eclient_pause(disorder_eclient *c,
958 disorder_eclient_no_response *completed,
959 void *v) {
960 return simple(c, no_response_opcallback, (void (*)())completed, v,
961 "pause", (char *)0);
962}
963
964int disorder_eclient_resume(disorder_eclient *c,
965 disorder_eclient_no_response *completed,
966 void *v) {
967 return simple(c, no_response_opcallback, (void (*)())completed, v,
968 "resume", (char *)0);
969}
970
971int disorder_eclient_scratch(disorder_eclient *c,
972 const char *id,
973 disorder_eclient_no_response *completed,
974 void *v) {
975 return simple(c, no_response_opcallback, (void (*)())completed, v,
976 "scratch", id, (char *)0);
977}
978
979int disorder_eclient_scratch_playing(disorder_eclient *c,
980 disorder_eclient_no_response *completed,
981 void *v) {
982 return disorder_eclient_scratch(c, 0, completed, v);
983}
984
985int disorder_eclient_remove(disorder_eclient *c,
986 const char *id,
987 disorder_eclient_no_response *completed,
988 void *v) {
989 return simple(c, no_response_opcallback, (void (*)())completed, v,
990 "remove", id, (char *)0);
991}
992
993int disorder_eclient_moveafter(disorder_eclient *c,
994 const char *target,
995 int nids,
996 const char **ids,
997 disorder_eclient_no_response *completed,
998 void *v) {
999 struct vector vec;
1000 int n;
1001
1002 vector_init(&vec);
1003 vector_append(&vec, (char *)"moveafter");
1004 vector_append(&vec, (char *)target);
1005 for(n = 0; n < nids; ++n)
1006 vector_append(&vec, (char *)ids[n]);
1007 stash_command_vector(c, 0/*queuejump*/, no_response_opcallback, completed, v,
1008 vec.nvec, vec.vec);
1009 disorder_eclient_polled(c, 0);
1010 return 0;
1011}
1012
1013int disorder_eclient_recent(disorder_eclient *c,
1014 disorder_eclient_queue_response *completed,
1015 void *v) {
1016 return simple(c, queue_response_opcallback, (void (*)())completed, v,
1017 "recent", (char *)0);
1018}
1019
1020int disorder_eclient_queue(disorder_eclient *c,
1021 disorder_eclient_queue_response *completed,
1022 void *v) {
1023 return simple(c, queue_response_opcallback, (void (*)())completed, v,
1024 "queue", (char *)0);
1025}
1026
1027int disorder_eclient_files(disorder_eclient *c,
1028 disorder_eclient_list_response *completed,
1029 const char *dir,
1030 const char *re,
1031 void *v) {
1032 return simple(c, list_response_opcallback, (void (*)())completed, v,
1033 "files", dir, re, (char *)0);
1034}
1035
1036int disorder_eclient_dirs(disorder_eclient *c,
1037 disorder_eclient_list_response *completed,
1038 const char *dir,
1039 const char *re,
1040 void *v) {
1041 return simple(c, list_response_opcallback, (void (*)())completed, v,
1042 "dirs", dir, re, (char *)0);
1043}
1044
1045int disorder_eclient_playing(disorder_eclient *c,
1046 disorder_eclient_queue_response *completed,
1047 void *v) {
1048 return simple(c, playing_response_opcallback, (void (*)())completed, v,
1049 "playing", (char *)0);
1050}
1051
1052int disorder_eclient_length(disorder_eclient *c,
1053 disorder_eclient_integer_response *completed,
1054 const char *track,
1055 void *v) {
1056 return simple(c, integer_response_opcallback, (void (*)())completed, v,
1057 "length", track, (char *)0);
1058}
1059
1060int disorder_eclient_volume(disorder_eclient *c,
1061 disorder_eclient_volume_response *completed,
1062 int l, int r,
1063 void *v) {
1064 char sl[64], sr[64];
1065
1066 if(l < 0 && r < 0) {
1067 return simple(c, volume_response_opcallback, (void (*)())completed, v,
1068 "volume", (char *)0);
1069 } else if(l >= 0 && r >= 0) {
1070 assert(l <= 100);
1071 assert(r <= 100);
1072 byte_snprintf(sl, sizeof sl, "%d", l);
1073 byte_snprintf(sr, sizeof sr, "%d", r);
1074 return simple(c, volume_response_opcallback, (void (*)())completed, v,
1075 "volume", sl, sr, (char *)0);
1076 } else {
1077 assert(!"invalid arguments to disorder_eclient_volume");
1078 return -1; /* gcc is being dim */
1079 }
1080}
1081
1082int disorder_eclient_enable(disorder_eclient *c,
1083 disorder_eclient_no_response *completed,
1084 void *v) {
1085 return simple(c, no_response_opcallback, (void (*)())completed, v,
1086 "enable", (char *)0);
1087}
1088
1089int disorder_eclient_disable(disorder_eclient *c,
1090 disorder_eclient_no_response *completed,
1091 void *v){
1092 return simple(c, no_response_opcallback, (void (*)())completed, v,
1093 "disable", (char *)0);
1094}
1095
1096int disorder_eclient_random_enable(disorder_eclient *c,
1097 disorder_eclient_no_response *completed,
1098 void *v){
1099 return simple(c, no_response_opcallback, (void (*)())completed, v,
1100 "random-enable", (char *)0);
1101}
1102
1103int disorder_eclient_random_disable(disorder_eclient *c,
1104 disorder_eclient_no_response *completed,
1105 void *v){
1106 return simple(c, no_response_opcallback, (void (*)())completed, v,
1107 "random-disable", (char *)0);
1108}
1109
1110int disorder_eclient_get(disorder_eclient *c,
1111 disorder_eclient_string_response *completed,
1112 const char *track, const char *pref,
1113 void *v) {
1114 return simple(c, string_response_opcallback, (void (*)())completed, v,
1115 "get", track, pref, (char *)0);
1116}
1117
1118int disorder_eclient_set(disorder_eclient *c,
1119 disorder_eclient_no_response *completed,
1120 const char *track, const char *pref,
1121 const char *value,
1122 void *v) {
1123 return simple(c, no_response_opcallback, (void (*)())completed, v,
1124 "set", track, pref, value, (char *)0);
1125}
1126
1127int disorder_eclient_unset(disorder_eclient *c,
1128 disorder_eclient_no_response *completed,
1129 const char *track, const char *pref,
1130 void *v) {
1131 return simple(c, no_response_opcallback, (void (*)())completed, v,
1132 "unset", track, pref, (char *)0);
1133}
1134
1135int disorder_eclient_resolve(disorder_eclient *c,
1136 disorder_eclient_string_response *completed,
1137 const char *track,
1138 void *v) {
1139 return simple(c, string_response_opcallback, (void (*)())completed, v,
1140 "resolve", track, (char *)0);
1141}
1142
1143int disorder_eclient_search(disorder_eclient *c,
1144 disorder_eclient_list_response *completed,
1145 const char *terms,
1146 void *v) {
1147 if(!split(terms, 0, SPLIT_QUOTES, 0, 0)) return -1;
1148 return simple(c, list_response_opcallback, (void (*)())completed, v,
1149 "search", terms, (char *)0);
1150}
1151
7858930d 1152int disorder_eclient_nop(disorder_eclient *c,
1153 disorder_eclient_no_response *completed,
1154 void *v) {
1155 return simple(c, no_response_opcallback, (void (*)())completed, v,
1156 "nop", (char *)0);
1157}
1158
2a10b70b
RK
1159/** @brief Get the last @p max added tracks
1160 * @param c Client
1161 * @param completed Called with list
1162 * @param max Number of tracks to get, 0 for all
1163 * @param v Passed to @p completed
1164 *
1165 * The first track in the list is the most recently added.
1166 */
1167int disorder_eclient_new_tracks(disorder_eclient *c,
1168 disorder_eclient_list_response *completed,
1169 int max,
1170 void *v) {
1171 char limit[32];
1172
1173 sprintf(limit, "%d", max);
1174 return simple(c, list_response_opcallback, (void (*)())completed, v,
1175 "new", limit, (char *)0);
1176}
1177
460b9539 1178/* Log clients ***************************************************************/
1179
60b95b6e 1180/** @brief Monitor the server log
1181 * @param c Client
1182 * @param callbacks Functions to call when anything happens
1183 * @param v Passed to @p callbacks functions
1184 *
1185 * Once a client is being used for logging it cannot be used for anything else.
1186 * There is magic in authuser_opcallback() to re-submit the @c log command
1187 * after reconnection.
6d1302f0
RK
1188 *
1189 * NB that the @c state callback may be called from within this function,
1190 * i.e. not solely later on from the event loop callback.
60b95b6e 1191 */
460b9539 1192int disorder_eclient_log(disorder_eclient *c,
1193 const disorder_eclient_log_callbacks *callbacks,
1194 void *v) {
1195 if(c->log_callbacks) return -1;
1196 c->log_callbacks = callbacks;
1197 c->log_v = v;
6d1302f0
RK
1198 /* Repoort initial state */
1199 if(c->log_callbacks->state)
1200 c->log_callbacks->state(c->log_v, c->statebits);
460b9539 1201 stash_command(c, 0/*queuejump*/, log_opcallback, 0/*completed*/, v,
1202 "log", (char *)0);
1203 return 0;
1204}
1205
1206/* If we get here we've stopped being a log client */
1207static void log_opcallback(disorder_eclient *c,
1208 struct operation attribute((unused)) *op) {
1209 D(("log_opcallback"));
1210 c->log_callbacks = 0;
1211 c->log_v = 0;
1212}
1213
1214/* error callback for log line parsing */
1215static void logline_error(const char *msg, void *u) {
1216 disorder_eclient *c = u;
1217 protocol_error(c, c->ops, -1, "error parsing log line: %s", msg);
1218}
1219
1220/* process a single log line */
1221static void logline(disorder_eclient *c, const char *line) {
1222 int nvec, n;
1223 char **vec;
1224 uintmax_t when;
1225
6cd155a8 1226 D(("logline [%s]", line));
460b9539 1227 vec = split(line, &nvec, SPLIT_QUOTES, logline_error, c);
1228 if(nvec < 2) return; /* probably an error, already
1229 * reported */
1230 if(sscanf(vec[0], "%"SCNxMAX, &when) != 1) {
1231 /* probably the wrong side of a format change */
1232 protocol_error(c, c->ops, -1, "invalid log timestamp '%s'", vec[0]);
1233 return;
1234 }
1235 /* TODO: do something with the time */
1236 n = TABLE_FIND(logentry_handlers, struct logentry_handler, name, vec[1]);
1237 if(n < 0) return; /* probably a future command */
1238 vec += 2;
1239 nvec -= 2;
1240 if(nvec < logentry_handlers[n].min || nvec > logentry_handlers[n].max)
1241 return;
1242 logentry_handlers[n].handler(c, nvec, vec);
1243}
1244
1245static void logentry_completed(disorder_eclient *c,
1246 int attribute((unused)) nvec, char **vec) {
1247 if(!c->log_callbacks->completed) return;
277f7f59 1248 c->statebits &= ~DISORDER_PLAYING;
460b9539 1249 c->log_callbacks->completed(c->log_v, vec[0]);
8f763f1b
RK
1250 if(c->log_callbacks->state)
1251 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1252}
1253
1254static void logentry_failed(disorder_eclient *c,
1255 int attribute((unused)) nvec, char **vec) {
1256 if(!c->log_callbacks->failed)return;
277f7f59 1257 c->statebits &= ~DISORDER_PLAYING;
460b9539 1258 c->log_callbacks->failed(c->log_v, vec[0], vec[1]);
8f763f1b
RK
1259 if(c->log_callbacks->state)
1260 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1261}
1262
1263static void logentry_moved(disorder_eclient *c,
1264 int attribute((unused)) nvec, char **vec) {
1265 if(!c->log_callbacks->moved) return;
1266 c->log_callbacks->moved(c->log_v, vec[0]);
1267}
1268
1269static void logentry_playing(disorder_eclient *c,
1270 int attribute((unused)) nvec, char **vec) {
1271 if(!c->log_callbacks->playing) return;
277f7f59 1272 c->statebits |= DISORDER_PLAYING;
460b9539 1273 c->log_callbacks->playing(c->log_v, vec[0], vec[1]);
8f763f1b
RK
1274 if(c->log_callbacks->state)
1275 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1276}
1277
1278static void logentry_queue(disorder_eclient *c,
1279 int attribute((unused)) nvec, char **vec) {
1280 struct queue_entry *q;
1281
1282 if(!c->log_callbacks->completed) return;
1283 q = xmalloc(sizeof *q);
1284 if(queue_unmarshall_vec(q, nvec, vec, eclient_queue_error, c))
1285 return; /* bogus */
1286 c->log_callbacks->queue(c->log_v, q);
1287}
1288
1289static void logentry_recent_added(disorder_eclient *c,
1290 int attribute((unused)) nvec, char **vec) {
1291 struct queue_entry *q;
1292
1293 if(!c->log_callbacks->recent_added) return;
1294 q = xmalloc(sizeof *q);
1295 if(queue_unmarshall_vec(q, nvec, vec, eclient_queue_error, c))
1296 return; /* bogus */
1297 c->log_callbacks->recent_added(c->log_v, q);
1298}
1299
1300static void logentry_recent_removed(disorder_eclient *c,
1301 int attribute((unused)) nvec, char **vec) {
1302 if(!c->log_callbacks->recent_removed) return;
1303 c->log_callbacks->recent_removed(c->log_v, vec[0]);
1304}
1305
1306static void logentry_removed(disorder_eclient *c,
1307 int attribute((unused)) nvec, char **vec) {
1308 if(!c->log_callbacks->removed) return;
1309 c->log_callbacks->removed(c->log_v, vec[0], vec[1]);
1310}
1311
1312static void logentry_scratched(disorder_eclient *c,
1313 int attribute((unused)) nvec, char **vec) {
1314 if(!c->log_callbacks->scratched) return;
277f7f59 1315 c->statebits &= ~DISORDER_PLAYING;
460b9539 1316 c->log_callbacks->scratched(c->log_v, vec[0], vec[1]);
8f763f1b
RK
1317 if(c->log_callbacks->state)
1318 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1319}
1320
1321static const struct {
1322 unsigned long bit;
1323 const char *enable;
1324 const char *disable;
1325} statestrings[] = {
1326 { DISORDER_PLAYING_ENABLED, "enable_play", "disable_play" },
1327 { DISORDER_RANDOM_ENABLED, "enable_random", "disable_random" },
1328 { DISORDER_TRACK_PAUSED, "pause", "resume" },
e56e1cc0
RK
1329 { DISORDER_PLAYING, "playing", "completed" },
1330 { DISORDER_PLAYING, 0, "scratched" },
1331 { DISORDER_PLAYING, 0, "failed" },
460b9539 1332};
6cd155a8 1333#define NSTATES (int)(sizeof statestrings / sizeof *statestrings)
460b9539 1334
1335static void logentry_state(disorder_eclient *c,
1336 int attribute((unused)) nvec, char **vec) {
1337 int n;
1338
1339 for(n = 0; n < NSTATES; ++n)
e56e1cc0 1340 if(statestrings[n].enable && !strcmp(vec[0], statestrings[n].enable)) {
460b9539 1341 c->statebits |= statestrings[n].bit;
1342 break;
e56e1cc0 1343 } else if(statestrings[n].disable && !strcmp(vec[0], statestrings[n].disable)) {
460b9539 1344 c->statebits &= ~statestrings[n].bit;
1345 break;
1346 }
1347 if(!c->log_callbacks->state) return;
8f763f1b 1348 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1349}
1350
1351static void logentry_volume(disorder_eclient *c,
1352 int attribute((unused)) nvec, char **vec) {
1353 long l, r;
1354
1355 if(!c->log_callbacks->volume) return;
1356 if(xstrtol(&l, vec[0], 0, 10)
1357 || xstrtol(&r, vec[1], 0, 10)
1358 || l < 0 || l > INT_MAX
1359 || r < 0 || r > INT_MAX)
1360 return; /* bogus */
1361 c->log_callbacks->volume(c->log_v, (int)l, (int)r);
1362}
1363
00959300
RK
1364/** @brief Convert @p statebits to a string */
1365char *disorder_eclient_interpret_state(unsigned long statebits) {
1366 struct dynstr d[1];
1367 size_t n;
1368
1369 static const struct {
1370 unsigned long bit;
1371 const char *name;
1372 } bits[] = {
1373 { DISORDER_PLAYING_ENABLED, "playing_enabled" },
1374 { DISORDER_RANDOM_ENABLED, "random_enabled" },
1375 { DISORDER_TRACK_PAUSED, "track_paused" },
1376 { DISORDER_PLAYING, "playing" },
1377 { DISORDER_CONNECTED, "connected" },
1378 };
1379#define NBITS (sizeof bits / sizeof *bits)
1380
1381 dynstr_init(d);
6d1302f0
RK
1382 if(!statebits)
1383 dynstr_append(d, '0');
00959300
RK
1384 for(n = 0; n < NBITS; ++n)
1385 if(statebits & bits[n].bit) {
1386 if(d->nvec)
1387 dynstr_append(d, '|');
1388 dynstr_append_string(d, bits[n].name);
1389 statebits ^= bits[n].bit;
1390 }
1391 if(statebits) {
1392 char s[20];
1393
1394 if(d->nvec)
1395 dynstr_append(d, '|');
1396 sprintf(s, "%#lx", statebits);
1397 dynstr_append_string(d, s);
1398 }
1399 dynstr_terminate(d);
1400 return d->vec;
1401}
1402
460b9539 1403/*
1404Local Variables:
1405c-basic-offset:2
1406comment-column:40
1407fill-column:79
1408indent-tabs-mode:nil
1409End:
1410*/