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