chiark / gitweb /
log: more general error message formatting
[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. */
a808e4a7 589 protocol_error(c, op, c->rc, "%s [%s]", c->line, c->ident);
460b9539 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:
a808e4a7 610 protocol_error(c, op, c->rc, "%s [%s]", c->line, c->ident);
416609cf 611 disorder_eclient_close(c);
b8b4ef7f 612 return;
416609cf 613 }
b8b4ef7f 614 c->protocol = atoi(protocol);
615 if(c->protocol < 1 || c->protocol > 2) {
a808e4a7 616 protocol_error(c, op, c->rc, "%s [%s]", c->line, c->ident);
416609cf 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 622 if(!res) {
a808e4a7
RK
623 protocol_error(c, op, c->rc, "unknown authentication algorithm '%s' [%s]",
624 algorithm, c->ident);
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. */
a808e4a7 642 protocol_error(c, op, c->rc, "%s [%s]", c->line, c->ident);
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);
bcb2af72
RK
891 } else if(arg == disorder__integer) {
892 long n = va_arg(ap, long);
893 char buffer[16];
894 snprintf(buffer, sizeof buffer, "%ld", n);
895 vector_append(&vec, xstrdup(buffer));
896 } else if(arg == disorder__time) {
897 time_t n = va_arg(ap, time_t);
898 char buffer[16];
899 snprintf(buffer, sizeof buffer, "%lld", (long long)n);
900 vector_append(&vec, xstrdup(buffer));
ad131c25
RK
901 } else
902 vector_append(&vec, arg);
903 }
460b9539 904 stash_command_vector(c, queuejump, opcallback, completed, v,
53740beb 905 nbody, body, vec.nvec, vec.vec);
460b9539 906 } else
53740beb
RK
907 stash_command_vector(c, queuejump, opcallback, completed, v,
908 nbody, body,
909 0, 0);
460b9539 910}
911
912static void stash_command(disorder_eclient *c,
913 int queuejump,
914 operation_callback *opcallback,
915 void (*completed)(),
916 void *v,
53740beb
RK
917 int nbody,
918 char **body,
460b9539 919 const char *cmd,
920 ...) {
921 va_list ap;
922
923 va_start(ap, cmd);
53740beb 924 vstash_command(c, queuejump, opcallback, completed, v, nbody, body, cmd, ap);
460b9539 925 va_end(ap);
926}
927
928/* Command support ***********************************************************/
929
06bfbba4
RK
930static const char *errorstring(disorder_eclient *c) {
931 char *s;
932
a808e4a7 933 byte_xasprintf(&s, "%s [%s]", c->line, c->ident);
06bfbba4
RK
934 return s;
935}
936
7b32e917 937/* for commands with a quoted string response */
460b9539 938static void string_response_opcallback(disorder_eclient *c,
939 struct operation *op) {
06bfbba4
RK
940 disorder_eclient_string_response *completed
941 = (disorder_eclient_string_response *)op->completed;
942
460b9539 943 D(("string_response_callback"));
ad0c0f36
RK
944 if(completed) {
945 if(c->rc / 100 == 2 || c->rc == 555) {
1bd1b63c 946 if(c->rc == 555)
06bfbba4 947 completed(op->v, NULL, NULL);
1bd1b63c 948 else if(c->protocol >= 2) {
b8b4ef7f 949 char **rr = split(c->line + 4, 0, SPLIT_QUOTES, 0, 0);
950
951 if(rr && *rr)
06bfbba4 952 completed(op->v, NULL, *rr);
b8b4ef7f 953 else
06bfbba4
RK
954 /* TODO error message a is bit lame but generally indicates a server
955 * bug rather than anything the user can address */
956 completed(op->v, "error parsing response", NULL);
b8b4ef7f 957 } else
06bfbba4 958 completed(op->v, NULL, c->line + 4);
ad0c0f36
RK
959 } else
960 completed(op->v, errorstring(c), NULL);
961 }
460b9539 962}
963
964/* for commands with a simple integer response */
965static void integer_response_opcallback(disorder_eclient *c,
966 struct operation *op) {
658c8a35
RK
967 disorder_eclient_integer_response *completed
968 = (disorder_eclient_integer_response *)op->completed;
969
460b9539 970 D(("string_response_callback"));
971 if(c->rc / 100 == 2) {
658c8a35
RK
972 long n;
973 int e;
974
975 e = xstrtol(&n, c->line + 4, 0, 10);
976 if(e)
977 completed(op->v, strerror(e), 0);
978 else
979 completed(op->v, 0, n);
460b9539 980 } else
658c8a35 981 completed(op->v, errorstring(c), 0);
460b9539 982}
983
984/* for commands with no response */
985static void no_response_opcallback(disorder_eclient *c,
986 struct operation *op) {
53fa91bb
RK
987 disorder_eclient_no_response *completed
988 = (disorder_eclient_no_response *)op->completed;
989
460b9539 990 D(("no_response_callback"));
ad0c0f36
RK
991 if(completed) {
992 if(c->rc / 100 == 2)
993 completed(op->v, NULL);
994 else
995 completed(op->v, errorstring(c));
996 }
460b9539 997}
998
999/* error callback for queue_unmarshall */
1000static void eclient_queue_error(const char *msg,
1001 void *u) {
1002 struct operation *op = u;
1003
1f3ce240 1004 /* TODO don't use protocol_error here */
460b9539 1005 protocol_error(op->client, op, -1, "error parsing queue entry: %s", msg);
1006}
1007
1008/* for commands that expect a queue dump */
1009static void queue_response_opcallback(disorder_eclient *c,
1010 struct operation *op) {
3035257f
RK
1011 disorder_eclient_queue_response *const completed
1012 = (disorder_eclient_queue_response *)op->completed;
460b9539 1013 int n;
3035257f 1014 int parse_failed = 0;
460b9539 1015 struct queue_entry *q, *qh = 0, **qtail = &qh, *qlast = 0;
1016
1017 D(("queue_response_callback"));
1018 if(c->rc / 100 == 2) {
1019 /* parse the queue */
1020 for(n = 0; n < c->vec.nvec; ++n) {
1021 q = xmalloc(sizeof *q);
1022 D(("queue_unmarshall %s", c->vec.vec[n]));
4190a4d9 1023 if(!queue_unmarshall(q, c->vec.vec[n], NULL, op)) {
460b9539 1024 q->prev = qlast;
1025 *qtail = q;
1026 qtail = &q->next;
1027 qlast = q;
3035257f
RK
1028 } else
1029 parse_failed = 1;
460b9539 1030 }
3035257f
RK
1031 /* Currently we pass the partial queue to the callback along with the
1032 * error. This might not be very useful in practice... */
1033 if(parse_failed)
1034 completed(op->v, "cannot parse result", qh);
1035 else
1036 completed(op->v, 0, qh);
460b9539 1037 } else
3035257f 1038 completed(op->v, errorstring(c), 0);
460b9539 1039}
1040
1041/* for 'playing' */
1042static void playing_response_opcallback(disorder_eclient *c,
1043 struct operation *op) {
3035257f
RK
1044 disorder_eclient_queue_response *const completed
1045 = (disorder_eclient_queue_response *)op->completed;
460b9539 1046 struct queue_entry *q;
1047
1048 D(("playing_response_callback"));
1049 if(c->rc / 100 == 2) {
1050 switch(c->rc % 10) {
1051 case 2:
1052 if(queue_unmarshall(q = xmalloc(sizeof *q), c->line + 4,
4190a4d9 1053 NULL, c))
3035257f
RK
1054 completed(op->v, "cannot parse result", 0);
1055 else
1056 completed(op->v, 0, q);
460b9539 1057 break;
1058 case 9:
3035257f 1059 completed(op->v, 0, 0);
460b9539 1060 break;
1061 default:
3035257f
RK
1062 completed(op->v, errorstring(c), 0);
1063 break;
460b9539 1064 }
460b9539 1065 } else
3035257f 1066 completed(op->v, errorstring(c), 0);
460b9539 1067}
1068
1069/* for commands that expect a list of some sort */
1070static void list_response_opcallback(disorder_eclient *c,
1071 struct operation *op) {
4190a4d9
RK
1072 disorder_eclient_list_response *const completed =
1073 (disorder_eclient_list_response *)op->completed;
1074
460b9539 1075 D(("list_response_callback"));
4190a4d9
RK
1076 if(c->rc / 100 == 2)
1077 completed(op->v, NULL, c->vec.nvec, c->vec.vec);
53740beb
RK
1078 else if(c->rc == 555)
1079 completed(op->v, NULL, -1, NULL);
4190a4d9
RK
1080 else
1081 completed(op->v, errorstring(c), 0, 0);
460b9539 1082}
1083
1084/* for volume */
ad131c25
RK
1085static void pair_integer_response_opcallback(disorder_eclient *c,
1086 struct operation *op) {
1087 disorder_eclient_pair_integer_response *completed
1088 = (disorder_eclient_pair_integer_response *)op->completed;
1089 long l, r;
460b9539 1090
1091 D(("volume_response_callback"));
1092 if(c->rc / 100 == 2) {
1093 if(op->completed) {
ad131c25 1094 if(sscanf(c->line + 4, "%ld %ld", &l, &r) != 2 || l < 0 || r < 0)
699517af 1095 completed(op->v, "cannot parse volume response", 0, 0);
460b9539 1096 else
699517af 1097 completed(op->v, 0, l, r);
460b9539 1098 }
1099 } else
699517af 1100 completed(op->v, errorstring(c), 0, 0);
460b9539 1101}
1102
1103static int simple(disorder_eclient *c,
1104 operation_callback *opcallback,
1105 void (*completed)(),
1106 void *v,
1107 const char *cmd, ...) {
1108 va_list ap;
1109
1110 va_start(ap, cmd);
53740beb
RK
1111 vstash_command(c, 0/*queuejump*/, opcallback, completed, v, -1, 0, cmd, ap);
1112 va_end(ap);
1113 /* Give the state machine a kick, since we might be in state_idle */
1114 disorder_eclient_polled(c, 0);
1115 return 0;
1116}
1117
460b9539 1118/* Commands ******************************************************************/
460b9539 1119
1120int disorder_eclient_scratch_playing(disorder_eclient *c,
1121 disorder_eclient_no_response *completed,
1122 void *v) {
ad131c25 1123 return disorder_eclient_scratch(c, completed, 0, v);
2a10b70b
RK
1124}
1125
a99c4e9a
RK
1126static void rtp_response_opcallback(disorder_eclient *c,
1127 struct operation *op) {
4190a4d9
RK
1128 disorder_eclient_list_response *const completed =
1129 (disorder_eclient_list_response *)op->completed;
a99c4e9a
RK
1130 D(("rtp_response_opcallback"));
1131 if(c->rc / 100 == 2) {
4190a4d9
RK
1132 int nvec;
1133 char **vec = split(c->line + 4, &nvec, SPLIT_QUOTES, 0, 0);
a99c4e9a 1134
4190a4d9
RK
1135 if(vec)
1136 completed(op->v, NULL, nvec, vec);
1137 else
1138 completed(op->v, "error parsing response", 0, 0);
a99c4e9a 1139 } else
4190a4d9 1140 completed(op->v, errorstring(c), 0, 0);
a99c4e9a
RK
1141}
1142
1143/** @brief Determine the RTP target address
1144 * @param c Client
1145 * @param completed Called with address details
1146 * @param v Passed to @p completed
1147 *
1148 * The address details will be two elements, the first being the hostname and
1149 * the second the service (port).
1150 */
1151int disorder_eclient_rtp_address(disorder_eclient *c,
1152 disorder_eclient_list_response *completed,
1153 void *v) {
1154 return simple(c, rtp_response_opcallback, (void (*)())completed, v,
1155 "rtp-address", (char *)0);
1156}
1157
460b9539 1158/* Log clients ***************************************************************/
1159
60b95b6e 1160/** @brief Monitor the server log
1161 * @param c Client
1162 * @param callbacks Functions to call when anything happens
1163 * @param v Passed to @p callbacks functions
1164 *
1165 * Once a client is being used for logging it cannot be used for anything else.
1166 * There is magic in authuser_opcallback() to re-submit the @c log command
1167 * after reconnection.
6d1302f0
RK
1168 *
1169 * NB that the @c state callback may be called from within this function,
1170 * i.e. not solely later on from the event loop callback.
60b95b6e 1171 */
460b9539 1172int disorder_eclient_log(disorder_eclient *c,
1173 const disorder_eclient_log_callbacks *callbacks,
1174 void *v) {
1175 if(c->log_callbacks) return -1;
1176 c->log_callbacks = callbacks;
1177 c->log_v = v;
6d1302f0
RK
1178 /* Repoort initial state */
1179 if(c->log_callbacks->state)
1180 c->log_callbacks->state(c->log_v, c->statebits);
460b9539 1181 stash_command(c, 0/*queuejump*/, log_opcallback, 0/*completed*/, v,
53740beb 1182 -1, 0, "log", (char *)0);
bcc46dc3 1183 disorder_eclient_polled(c, 0);
460b9539 1184 return 0;
1185}
1186
1187/* If we get here we've stopped being a log client */
1188static void log_opcallback(disorder_eclient *c,
1189 struct operation attribute((unused)) *op) {
1190 D(("log_opcallback"));
1191 c->log_callbacks = 0;
1192 c->log_v = 0;
1193}
1194
1195/* error callback for log line parsing */
1196static void logline_error(const char *msg, void *u) {
1197 disorder_eclient *c = u;
1f3ce240 1198 /* TODO don't use protocol_error here */
460b9539 1199 protocol_error(c, c->ops, -1, "error parsing log line: %s", msg);
1200}
1201
1202/* process a single log line */
1203static void logline(disorder_eclient *c, const char *line) {
1204 int nvec, n;
1205 char **vec;
1206 uintmax_t when;
1207
6cd155a8 1208 D(("logline [%s]", line));
460b9539 1209 vec = split(line, &nvec, SPLIT_QUOTES, logline_error, c);
1210 if(nvec < 2) return; /* probably an error, already
1211 * reported */
1212 if(sscanf(vec[0], "%"SCNxMAX, &when) != 1) {
1213 /* probably the wrong side of a format change */
1f3ce240 1214 /* TODO don't use protocol_error here */
460b9539 1215 protocol_error(c, c->ops, -1, "invalid log timestamp '%s'", vec[0]);
1216 return;
1217 }
1218 /* TODO: do something with the time */
fa7d5980 1219 //fprintf(stderr, "log key: %s\n", vec[1]);
ba937f01 1220 n = TABLE_FIND(logentry_handlers, name, vec[1]);
fa7d5980
RK
1221 if(n < 0) {
1222 //fprintf(stderr, "...not found\n");
1223 return; /* probably a future command */
1224 }
460b9539 1225 vec += 2;
1226 nvec -= 2;
fa7d5980
RK
1227 if(nvec < logentry_handlers[n].min || nvec > logentry_handlers[n].max) {
1228 //fprintf(stderr, "...wrong # args\n");
460b9539 1229 return;
fa7d5980 1230 }
460b9539 1231 logentry_handlers[n].handler(c, nvec, vec);
1232}
1233
1234static void logentry_completed(disorder_eclient *c,
1235 int attribute((unused)) nvec, char **vec) {
277f7f59 1236 c->statebits &= ~DISORDER_PLAYING;
bcc46dc3
RK
1237 if(c->log_callbacks->completed)
1238 c->log_callbacks->completed(c->log_v, vec[0]);
8f763f1b
RK
1239 if(c->log_callbacks->state)
1240 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1241}
1242
1243static void logentry_failed(disorder_eclient *c,
1244 int attribute((unused)) nvec, char **vec) {
277f7f59 1245 c->statebits &= ~DISORDER_PLAYING;
bcc46dc3
RK
1246 if(c->log_callbacks->failed)
1247 c->log_callbacks->failed(c->log_v, vec[0], vec[1]);
8f763f1b
RK
1248 if(c->log_callbacks->state)
1249 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1250}
1251
1252static void logentry_moved(disorder_eclient *c,
1253 int attribute((unused)) nvec, char **vec) {
bcc46dc3
RK
1254 if(c->log_callbacks->moved)
1255 c->log_callbacks->moved(c->log_v, vec[0]);
460b9539 1256}
1257
1258static void logentry_playing(disorder_eclient *c,
1259 int attribute((unused)) nvec, char **vec) {
277f7f59 1260 c->statebits |= DISORDER_PLAYING;
bcc46dc3
RK
1261 if(c->log_callbacks->playing)
1262 c->log_callbacks->playing(c->log_v, vec[0], vec[1]);
8f763f1b
RK
1263 if(c->log_callbacks->state)
1264 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1265}
1266
1267static void logentry_queue(disorder_eclient *c,
1268 int attribute((unused)) nvec, char **vec) {
1269 struct queue_entry *q;
1270
bcc46dc3 1271 if(!c->log_callbacks->queue) return;
460b9539 1272 q = xmalloc(sizeof *q);
1273 if(queue_unmarshall_vec(q, nvec, vec, eclient_queue_error, c))
1274 return; /* bogus */
1275 c->log_callbacks->queue(c->log_v, q);
1276}
1277
1278static void logentry_recent_added(disorder_eclient *c,
1279 int attribute((unused)) nvec, char **vec) {
1280 struct queue_entry *q;
1281
1282 if(!c->log_callbacks->recent_added) 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->recent_added(c->log_v, q);
1287}
1288
1289static void logentry_recent_removed(disorder_eclient *c,
1290 int attribute((unused)) nvec, char **vec) {
bcc46dc3
RK
1291 if(c->log_callbacks->recent_removed)
1292 c->log_callbacks->recent_removed(c->log_v, vec[0]);
460b9539 1293}
1294
1295static void logentry_removed(disorder_eclient *c,
1296 int attribute((unused)) nvec, char **vec) {
bcc46dc3
RK
1297 if(c->log_callbacks->removed)
1298 c->log_callbacks->removed(c->log_v, vec[0], vec[1]);
460b9539 1299}
1300
e025abff
RK
1301static void logentry_rescanned(disorder_eclient *c,
1302 int attribute((unused)) nvec,
1303 char attribute((unused)) **vec) {
bcc46dc3
RK
1304 if(c->log_callbacks->rescanned)
1305 c->log_callbacks->rescanned(c->log_v);
e025abff
RK
1306}
1307
460b9539 1308static void logentry_scratched(disorder_eclient *c,
1309 int attribute((unused)) nvec, char **vec) {
277f7f59 1310 c->statebits &= ~DISORDER_PLAYING;
bcc46dc3
RK
1311 if(c->log_callbacks->scratched)
1312 c->log_callbacks->scratched(c->log_v, vec[0], vec[1]);
8f763f1b
RK
1313 if(c->log_callbacks->state)
1314 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1315}
1316
58d2aad2
RK
1317static void logentry_user_add(disorder_eclient *c,
1318 int attribute((unused)) nvec, char **vec) {
1319 if(c->log_callbacks->user_add)
1320 c->log_callbacks->user_add(c->log_v, vec[0]);
1321}
1322
1323static void logentry_user_confirm(disorder_eclient *c,
1324 int attribute((unused)) nvec, char **vec) {
1325 if(c->log_callbacks->user_confirm)
1326 c->log_callbacks->user_confirm(c->log_v, vec[0]);
1327}
1328
1329static void logentry_user_delete(disorder_eclient *c,
1330 int attribute((unused)) nvec, char **vec) {
1331 if(c->log_callbacks->user_delete)
1332 c->log_callbacks->user_delete(c->log_v, vec[0]);
1333}
1334
1335static void logentry_user_edit(disorder_eclient *c,
1336 int attribute((unused)) nvec, char **vec) {
1337 if(c->log_callbacks->user_edit)
1338 c->log_callbacks->user_edit(c->log_v, vec[0], vec[1]);
1339}
1340
ad492e00
RK
1341static void logentry_rights_changed(disorder_eclient *c,
1342 int attribute((unused)) nvec, char **vec) {
1343 if(c->log_callbacks->rights_changed) {
1344 rights_type r;
fa7d5980 1345 if(!parse_rights(vec[0], &r, 0/*report*/))
ad492e00
RK
1346 c->log_callbacks->rights_changed(c->log_v, r);
1347 }
1348}
1349
5c102112
RK
1350static void logentry_playlist_created(disorder_eclient *c,
1351 int attribute((unused)) nvec,
1352 char **vec) {
1353 if(c->log_callbacks->playlist_created)
1354 c->log_callbacks->playlist_created(c->log_v, vec[0], vec[1]);
1355}
1356
1357static void logentry_playlist_deleted(disorder_eclient *c,
1358 int attribute((unused)) nvec,
1359 char **vec) {
1360 if(c->log_callbacks->playlist_deleted)
1361 c->log_callbacks->playlist_deleted(c->log_v, vec[0]);
1362}
1363
1364static void logentry_playlist_modified(disorder_eclient *c,
1365 int attribute((unused)) nvec,
1366 char **vec) {
1367 if(c->log_callbacks->playlist_modified)
1368 c->log_callbacks->playlist_modified(c->log_v, vec[0], vec[1]);
1369}
1370
460b9539 1371static const struct {
1372 unsigned long bit;
1373 const char *enable;
1374 const char *disable;
1375} statestrings[] = {
1376 { DISORDER_PLAYING_ENABLED, "enable_play", "disable_play" },
1377 { DISORDER_RANDOM_ENABLED, "enable_random", "disable_random" },
1378 { DISORDER_TRACK_PAUSED, "pause", "resume" },
e56e1cc0
RK
1379 { DISORDER_PLAYING, "playing", "completed" },
1380 { DISORDER_PLAYING, 0, "scratched" },
1381 { DISORDER_PLAYING, 0, "failed" },
460b9539 1382};
6cd155a8 1383#define NSTATES (int)(sizeof statestrings / sizeof *statestrings)
460b9539 1384
1385static void logentry_state(disorder_eclient *c,
1386 int attribute((unused)) nvec, char **vec) {
1387 int n;
1388
1389 for(n = 0; n < NSTATES; ++n)
e56e1cc0 1390 if(statestrings[n].enable && !strcmp(vec[0], statestrings[n].enable)) {
460b9539 1391 c->statebits |= statestrings[n].bit;
1392 break;
e56e1cc0 1393 } else if(statestrings[n].disable && !strcmp(vec[0], statestrings[n].disable)) {
460b9539 1394 c->statebits &= ~statestrings[n].bit;
1395 break;
1396 }
bcc46dc3
RK
1397 if(c->log_callbacks->state)
1398 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1399}
1400
1401static void logentry_volume(disorder_eclient *c,
1402 int attribute((unused)) nvec, char **vec) {
1403 long l, r;
1404
1405 if(!c->log_callbacks->volume) return;
1406 if(xstrtol(&l, vec[0], 0, 10)
1407 || xstrtol(&r, vec[1], 0, 10)
1408 || l < 0 || l > INT_MAX
1409 || r < 0 || r > INT_MAX)
1410 return; /* bogus */
1411 c->log_callbacks->volume(c->log_v, (int)l, (int)r);
1412}
1413
00959300
RK
1414/** @brief Convert @p statebits to a string */
1415char *disorder_eclient_interpret_state(unsigned long statebits) {
1416 struct dynstr d[1];
1417 size_t n;
1418
1419 static const struct {
1420 unsigned long bit;
1421 const char *name;
1422 } bits[] = {
1423 { DISORDER_PLAYING_ENABLED, "playing_enabled" },
1424 { DISORDER_RANDOM_ENABLED, "random_enabled" },
1425 { DISORDER_TRACK_PAUSED, "track_paused" },
1426 { DISORDER_PLAYING, "playing" },
1427 { DISORDER_CONNECTED, "connected" },
1428 };
1429#define NBITS (sizeof bits / sizeof *bits)
1430
1431 dynstr_init(d);
6d1302f0
RK
1432 if(!statebits)
1433 dynstr_append(d, '0');
00959300
RK
1434 for(n = 0; n < NBITS; ++n)
1435 if(statebits & bits[n].bit) {
1436 if(d->nvec)
1437 dynstr_append(d, '|');
1438 dynstr_append_string(d, bits[n].name);
1439 statebits ^= bits[n].bit;
1440 }
1441 if(statebits) {
1442 char s[20];
1443
1444 if(d->nvec)
1445 dynstr_append(d, '|');
1446 sprintf(s, "%#lx", statebits);
1447 dynstr_append_string(d, s);
1448 }
1449 dynstr_terminate(d);
1450 return d->vec;
1451}
1452
9433fabf
RK
1453static void logentry_adopted(disorder_eclient *c,
1454 int attribute((unused)) nvec, char **vec) {
1455 if(c->log_callbacks->adopted)
1456 c->log_callbacks->adopted(c->log_v, vec[0], vec[1]);
1457}
1458
ad0c0f36
RK
1459static void logentry_global_pref(disorder_eclient *c,
1460 int nvec, char **vec) {
1461 if(c->log_callbacks->global_pref)
1462 c->log_callbacks->global_pref(c->log_v, vec[0], nvec > 1 ? vec[1] : 0);
1463}
1464
ad131c25
RK
1465#include "eclient-stubs.c"
1466
460b9539 1467/*
1468Local Variables:
1469c-basic-offset:2
1470comment-column:40
1471fill-column:79
1472indent-tabs-mode:nil
1473End:
1474*/