chiark / gitweb /
play command
[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);
879 while((arg = va_arg(ap, char *)))
880 vector_append(&vec, arg);
881 stash_command_vector(c, queuejump, opcallback, completed, v,
53740beb 882 nbody, body, vec.nvec, vec.vec);
460b9539 883 } else
53740beb
RK
884 stash_command_vector(c, queuejump, opcallback, completed, v,
885 nbody, body,
886 0, 0);
460b9539 887}
888
889static void stash_command(disorder_eclient *c,
890 int queuejump,
891 operation_callback *opcallback,
892 void (*completed)(),
893 void *v,
53740beb
RK
894 int nbody,
895 char **body,
460b9539 896 const char *cmd,
897 ...) {
898 va_list ap;
899
900 va_start(ap, cmd);
53740beb 901 vstash_command(c, queuejump, opcallback, completed, v, nbody, body, cmd, ap);
460b9539 902 va_end(ap);
903}
904
905/* Command support ***********************************************************/
906
06bfbba4
RK
907static const char *errorstring(disorder_eclient *c) {
908 char *s;
909
910 byte_xasprintf(&s, "%s: %s: %d", c->ident, c->line, c->rc);
911 return s;
912}
913
7b32e917 914/* for commands with a quoted string response */
460b9539 915static void string_response_opcallback(disorder_eclient *c,
916 struct operation *op) {
06bfbba4
RK
917 disorder_eclient_string_response *completed
918 = (disorder_eclient_string_response *)op->completed;
919
460b9539 920 D(("string_response_callback"));
ad0c0f36
RK
921 if(completed) {
922 if(c->rc / 100 == 2 || c->rc == 555) {
1bd1b63c 923 if(c->rc == 555)
06bfbba4 924 completed(op->v, NULL, NULL);
1bd1b63c 925 else if(c->protocol >= 2) {
b8b4ef7f 926 char **rr = split(c->line + 4, 0, SPLIT_QUOTES, 0, 0);
927
928 if(rr && *rr)
06bfbba4 929 completed(op->v, NULL, *rr);
b8b4ef7f 930 else
06bfbba4
RK
931 /* TODO error message a is bit lame but generally indicates a server
932 * bug rather than anything the user can address */
933 completed(op->v, "error parsing response", NULL);
b8b4ef7f 934 } else
06bfbba4 935 completed(op->v, NULL, c->line + 4);
ad0c0f36
RK
936 } else
937 completed(op->v, errorstring(c), NULL);
938 }
460b9539 939}
940
941/* for commands with a simple integer response */
942static void integer_response_opcallback(disorder_eclient *c,
943 struct operation *op) {
658c8a35
RK
944 disorder_eclient_integer_response *completed
945 = (disorder_eclient_integer_response *)op->completed;
946
460b9539 947 D(("string_response_callback"));
948 if(c->rc / 100 == 2) {
658c8a35
RK
949 long n;
950 int e;
951
952 e = xstrtol(&n, c->line + 4, 0, 10);
953 if(e)
954 completed(op->v, strerror(e), 0);
955 else
956 completed(op->v, 0, n);
460b9539 957 } else
658c8a35 958 completed(op->v, errorstring(c), 0);
460b9539 959}
960
961/* for commands with no response */
962static void no_response_opcallback(disorder_eclient *c,
963 struct operation *op) {
53fa91bb
RK
964 disorder_eclient_no_response *completed
965 = (disorder_eclient_no_response *)op->completed;
966
460b9539 967 D(("no_response_callback"));
ad0c0f36
RK
968 if(completed) {
969 if(c->rc / 100 == 2)
970 completed(op->v, NULL);
971 else
972 completed(op->v, errorstring(c));
973 }
460b9539 974}
975
976/* error callback for queue_unmarshall */
977static void eclient_queue_error(const char *msg,
978 void *u) {
979 struct operation *op = u;
980
1f3ce240 981 /* TODO don't use protocol_error here */
460b9539 982 protocol_error(op->client, op, -1, "error parsing queue entry: %s", msg);
983}
984
985/* for commands that expect a queue dump */
986static void queue_response_opcallback(disorder_eclient *c,
987 struct operation *op) {
3035257f
RK
988 disorder_eclient_queue_response *const completed
989 = (disorder_eclient_queue_response *)op->completed;
460b9539 990 int n;
3035257f 991 int parse_failed = 0;
460b9539 992 struct queue_entry *q, *qh = 0, **qtail = &qh, *qlast = 0;
993
994 D(("queue_response_callback"));
995 if(c->rc / 100 == 2) {
996 /* parse the queue */
997 for(n = 0; n < c->vec.nvec; ++n) {
998 q = xmalloc(sizeof *q);
999 D(("queue_unmarshall %s", c->vec.vec[n]));
4190a4d9 1000 if(!queue_unmarshall(q, c->vec.vec[n], NULL, op)) {
460b9539 1001 q->prev = qlast;
1002 *qtail = q;
1003 qtail = &q->next;
1004 qlast = q;
3035257f
RK
1005 } else
1006 parse_failed = 1;
460b9539 1007 }
3035257f
RK
1008 /* Currently we pass the partial queue to the callback along with the
1009 * error. This might not be very useful in practice... */
1010 if(parse_failed)
1011 completed(op->v, "cannot parse result", qh);
1012 else
1013 completed(op->v, 0, qh);
460b9539 1014 } else
3035257f 1015 completed(op->v, errorstring(c), 0);
460b9539 1016}
1017
1018/* for 'playing' */
1019static void playing_response_opcallback(disorder_eclient *c,
1020 struct operation *op) {
3035257f
RK
1021 disorder_eclient_queue_response *const completed
1022 = (disorder_eclient_queue_response *)op->completed;
460b9539 1023 struct queue_entry *q;
1024
1025 D(("playing_response_callback"));
1026 if(c->rc / 100 == 2) {
1027 switch(c->rc % 10) {
1028 case 2:
1029 if(queue_unmarshall(q = xmalloc(sizeof *q), c->line + 4,
4190a4d9 1030 NULL, c))
3035257f
RK
1031 completed(op->v, "cannot parse result", 0);
1032 else
1033 completed(op->v, 0, q);
460b9539 1034 break;
1035 case 9:
3035257f 1036 completed(op->v, 0, 0);
460b9539 1037 break;
1038 default:
3035257f
RK
1039 completed(op->v, errorstring(c), 0);
1040 break;
460b9539 1041 }
460b9539 1042 } else
3035257f 1043 completed(op->v, errorstring(c), 0);
460b9539 1044}
1045
1046/* for commands that expect a list of some sort */
1047static void list_response_opcallback(disorder_eclient *c,
1048 struct operation *op) {
4190a4d9
RK
1049 disorder_eclient_list_response *const completed =
1050 (disorder_eclient_list_response *)op->completed;
1051
460b9539 1052 D(("list_response_callback"));
4190a4d9
RK
1053 if(c->rc / 100 == 2)
1054 completed(op->v, NULL, c->vec.nvec, c->vec.vec);
53740beb
RK
1055 else if(c->rc == 555)
1056 completed(op->v, NULL, -1, NULL);
4190a4d9
RK
1057 else
1058 completed(op->v, errorstring(c), 0, 0);
460b9539 1059}
1060
1061/* for volume */
1062static void volume_response_opcallback(disorder_eclient *c,
1063 struct operation *op) {
699517af
RK
1064 disorder_eclient_volume_response *completed
1065 = (disorder_eclient_volume_response *)op->completed;
460b9539 1066 int l, r;
1067
1068 D(("volume_response_callback"));
1069 if(c->rc / 100 == 2) {
1070 if(op->completed) {
1071 if(sscanf(c->line + 4, "%d %d", &l, &r) != 2 || l < 0 || r < 0)
699517af 1072 completed(op->v, "cannot parse volume response", 0, 0);
460b9539 1073 else
699517af 1074 completed(op->v, 0, l, r);
460b9539 1075 }
1076 } else
699517af 1077 completed(op->v, errorstring(c), 0, 0);
460b9539 1078}
1079
1080static int simple(disorder_eclient *c,
1081 operation_callback *opcallback,
1082 void (*completed)(),
1083 void *v,
1084 const char *cmd, ...) {
1085 va_list ap;
1086
1087 va_start(ap, cmd);
53740beb
RK
1088 vstash_command(c, 0/*queuejump*/, opcallback, completed, v, -1, 0, cmd, ap);
1089 va_end(ap);
1090 /* Give the state machine a kick, since we might be in state_idle */
1091 disorder_eclient_polled(c, 0);
1092 return 0;
1093}
1094
1095static int simple_body(disorder_eclient *c,
1096 operation_callback *opcallback,
1097 void (*completed)(),
1098 void *v,
1099 int nbody,
1100 char **body,
1101 const char *cmd, ...) {
1102 va_list ap;
1103
1104 va_start(ap, cmd);
1105 vstash_command(c, 0/*queuejump*/, opcallback, completed, v, nbody, body, cmd, ap);
460b9539 1106 va_end(ap);
1107 /* Give the state machine a kick, since we might be in state_idle */
1108 disorder_eclient_polled(c, 0);
1109 return 0;
1110}
1111
1112/* Commands ******************************************************************/
1113
1114int disorder_eclient_version(disorder_eclient *c,
1115 disorder_eclient_string_response *completed,
1116 void *v) {
1117 return simple(c, string_response_opcallback, (void (*)())completed, v,
1118 "version", (char *)0);
1119}
1120
1121int disorder_eclient_namepart(disorder_eclient *c,
1122 disorder_eclient_string_response *completed,
1123 const char *track,
1124 const char *context,
1125 const char *part,
1126 void *v) {
1127 return simple(c, string_response_opcallback, (void (*)())completed, v,
1128 "part", track, context, part, (char *)0);
1129}
1130
1131int disorder_eclient_play(disorder_eclient *c,
1132 const char *track,
1133 disorder_eclient_no_response *completed,
1134 void *v) {
1135 return simple(c, no_response_opcallback, (void (*)())completed, v,
1136 "play", track, (char *)0);
1137}
1138
7a853280
RK
1139int disorder_eclient_playafter(disorder_eclient *c,
1140 const char *target,
1141 int ntracks,
1142 const char **tracks,
1143 disorder_eclient_no_response *completed,
1144 void *v) {
1145 struct vector vec;
1146 int n;
1147
1148 if(!target)
1149 target = "";
1150 vector_init(&vec);
1151 vector_append(&vec, (char *)"playafter");
1152 vector_append(&vec, (char *)target);
1153 for(n = 0; n < ntracks; ++n)
1154 vector_append(&vec, (char *)tracks[n]);
1155 stash_command_vector(c, 0/*queuejump*/, no_response_opcallback, completed, v,
1156 -1, 0, vec.nvec, vec.vec);
1157 disorder_eclient_polled(c, 0);
1158 return 0;
1159}
1160
460b9539 1161int disorder_eclient_pause(disorder_eclient *c,
1162 disorder_eclient_no_response *completed,
1163 void *v) {
1164 return simple(c, no_response_opcallback, (void (*)())completed, v,
1165 "pause", (char *)0);
1166}
1167
1168int disorder_eclient_resume(disorder_eclient *c,
1169 disorder_eclient_no_response *completed,
1170 void *v) {
1171 return simple(c, no_response_opcallback, (void (*)())completed, v,
1172 "resume", (char *)0);
1173}
1174
1175int disorder_eclient_scratch(disorder_eclient *c,
1176 const char *id,
1177 disorder_eclient_no_response *completed,
1178 void *v) {
1179 return simple(c, no_response_opcallback, (void (*)())completed, v,
1180 "scratch", id, (char *)0);
1181}
1182
1183int disorder_eclient_scratch_playing(disorder_eclient *c,
1184 disorder_eclient_no_response *completed,
1185 void *v) {
1186 return disorder_eclient_scratch(c, 0, completed, v);
1187}
1188
1189int disorder_eclient_remove(disorder_eclient *c,
1190 const char *id,
1191 disorder_eclient_no_response *completed,
1192 void *v) {
1193 return simple(c, no_response_opcallback, (void (*)())completed, v,
1194 "remove", id, (char *)0);
1195}
1196
1197int disorder_eclient_moveafter(disorder_eclient *c,
1198 const char *target,
1199 int nids,
1200 const char **ids,
1201 disorder_eclient_no_response *completed,
1202 void *v) {
1203 struct vector vec;
1204 int n;
1205
1206 vector_init(&vec);
1207 vector_append(&vec, (char *)"moveafter");
1208 vector_append(&vec, (char *)target);
1209 for(n = 0; n < nids; ++n)
1210 vector_append(&vec, (char *)ids[n]);
1211 stash_command_vector(c, 0/*queuejump*/, no_response_opcallback, completed, v,
53740beb 1212 -1, 0, vec.nvec, vec.vec);
460b9539 1213 disorder_eclient_polled(c, 0);
1214 return 0;
1215}
1216
1217int disorder_eclient_recent(disorder_eclient *c,
1218 disorder_eclient_queue_response *completed,
1219 void *v) {
1220 return simple(c, queue_response_opcallback, (void (*)())completed, v,
1221 "recent", (char *)0);
1222}
1223
1224int disorder_eclient_queue(disorder_eclient *c,
1225 disorder_eclient_queue_response *completed,
1226 void *v) {
1227 return simple(c, queue_response_opcallback, (void (*)())completed, v,
1228 "queue", (char *)0);
1229}
1230
1231int disorder_eclient_files(disorder_eclient *c,
1232 disorder_eclient_list_response *completed,
1233 const char *dir,
1234 const char *re,
1235 void *v) {
1236 return simple(c, list_response_opcallback, (void (*)())completed, v,
1237 "files", dir, re, (char *)0);
1238}
1239
1240int disorder_eclient_dirs(disorder_eclient *c,
1241 disorder_eclient_list_response *completed,
1242 const char *dir,
1243 const char *re,
1244 void *v) {
1245 return simple(c, list_response_opcallback, (void (*)())completed, v,
1246 "dirs", dir, re, (char *)0);
1247}
1248
1249int disorder_eclient_playing(disorder_eclient *c,
1250 disorder_eclient_queue_response *completed,
1251 void *v) {
1252 return simple(c, playing_response_opcallback, (void (*)())completed, v,
1253 "playing", (char *)0);
1254}
1255
1256int disorder_eclient_length(disorder_eclient *c,
1257 disorder_eclient_integer_response *completed,
1258 const char *track,
1259 void *v) {
1260 return simple(c, integer_response_opcallback, (void (*)())completed, v,
1261 "length", track, (char *)0);
1262}
1263
1264int disorder_eclient_volume(disorder_eclient *c,
1265 disorder_eclient_volume_response *completed,
1266 int l, int r,
1267 void *v) {
1268 char sl[64], sr[64];
1269
1270 if(l < 0 && r < 0) {
1271 return simple(c, volume_response_opcallback, (void (*)())completed, v,
1272 "volume", (char *)0);
1273 } else if(l >= 0 && r >= 0) {
1274 assert(l <= 100);
1275 assert(r <= 100);
1276 byte_snprintf(sl, sizeof sl, "%d", l);
1277 byte_snprintf(sr, sizeof sr, "%d", r);
1278 return simple(c, volume_response_opcallback, (void (*)())completed, v,
1279 "volume", sl, sr, (char *)0);
1280 } else {
1281 assert(!"invalid arguments to disorder_eclient_volume");
1282 return -1; /* gcc is being dim */
1283 }
1284}
1285
1286int disorder_eclient_enable(disorder_eclient *c,
1287 disorder_eclient_no_response *completed,
1288 void *v) {
1289 return simple(c, no_response_opcallback, (void (*)())completed, v,
1290 "enable", (char *)0);
1291}
1292
1293int disorder_eclient_disable(disorder_eclient *c,
1294 disorder_eclient_no_response *completed,
1295 void *v){
1296 return simple(c, no_response_opcallback, (void (*)())completed, v,
1297 "disable", (char *)0);
1298}
1299
1300int disorder_eclient_random_enable(disorder_eclient *c,
1301 disorder_eclient_no_response *completed,
1302 void *v){
1303 return simple(c, no_response_opcallback, (void (*)())completed, v,
1304 "random-enable", (char *)0);
1305}
1306
1307int disorder_eclient_random_disable(disorder_eclient *c,
1308 disorder_eclient_no_response *completed,
1309 void *v){
1310 return simple(c, no_response_opcallback, (void (*)())completed, v,
1311 "random-disable", (char *)0);
1312}
1313
1314int disorder_eclient_get(disorder_eclient *c,
1315 disorder_eclient_string_response *completed,
1316 const char *track, const char *pref,
1317 void *v) {
1318 return simple(c, string_response_opcallback, (void (*)())completed, v,
1319 "get", track, pref, (char *)0);
1320}
1321
1322int disorder_eclient_set(disorder_eclient *c,
1323 disorder_eclient_no_response *completed,
1324 const char *track, const char *pref,
1325 const char *value,
1326 void *v) {
1327 return simple(c, no_response_opcallback, (void (*)())completed, v,
1328 "set", track, pref, value, (char *)0);
1329}
1330
1331int disorder_eclient_unset(disorder_eclient *c,
1332 disorder_eclient_no_response *completed,
1333 const char *track, const char *pref,
1334 void *v) {
1335 return simple(c, no_response_opcallback, (void (*)())completed, v,
1336 "unset", track, pref, (char *)0);
1337}
1338
1339int disorder_eclient_resolve(disorder_eclient *c,
1340 disorder_eclient_string_response *completed,
1341 const char *track,
1342 void *v) {
1343 return simple(c, string_response_opcallback, (void (*)())completed, v,
1344 "resolve", track, (char *)0);
1345}
1346
1347int disorder_eclient_search(disorder_eclient *c,
1348 disorder_eclient_list_response *completed,
1349 const char *terms,
1350 void *v) {
1351 if(!split(terms, 0, SPLIT_QUOTES, 0, 0)) return -1;
1352 return simple(c, list_response_opcallback, (void (*)())completed, v,
1353 "search", terms, (char *)0);
1354}
1355
7858930d 1356int disorder_eclient_nop(disorder_eclient *c,
1357 disorder_eclient_no_response *completed,
1358 void *v) {
1359 return simple(c, no_response_opcallback, (void (*)())completed, v,
1360 "nop", (char *)0);
1361}
1362
ad0c0f36
RK
1363int disorder_eclient_get_global(disorder_eclient *c,
1364 disorder_eclient_string_response *completed,
1365 const char *pref,
1366 void *v) {
1367 return simple(c, string_response_opcallback, (void (*)())completed, v,
1368 "get-global", pref, (char *)0);
1369}
1370
1371int disorder_eclient_set_global(disorder_eclient *c,
1372 disorder_eclient_no_response *completed,
1373 const char *pref,
1374 const char *value,
1375 void *v) {
1376 return simple(c, no_response_opcallback, (void (*)())completed, v,
1377 "set-global", pref, value, (char *)0);
1378}
1379
1380int disorder_eclient_unset_global(disorder_eclient *c,
1381 disorder_eclient_no_response *completed,
1382 const char *pref,
1383 void *v) {
1384 return simple(c, no_response_opcallback, (void (*)())completed, v,
1385 "unset-global", pref, (char *)0);
1386}
1387
2a10b70b
RK
1388/** @brief Get the last @p max added tracks
1389 * @param c Client
1390 * @param completed Called with list
1391 * @param max Number of tracks to get, 0 for all
1392 * @param v Passed to @p completed
1393 *
1394 * The first track in the list is the most recently added.
1395 */
1396int disorder_eclient_new_tracks(disorder_eclient *c,
1397 disorder_eclient_list_response *completed,
1398 int max,
1399 void *v) {
1400 char limit[32];
1401
1402 sprintf(limit, "%d", max);
1403 return simple(c, list_response_opcallback, (void (*)())completed, v,
1404 "new", limit, (char *)0);
1405}
1406
a99c4e9a
RK
1407static void rtp_response_opcallback(disorder_eclient *c,
1408 struct operation *op) {
4190a4d9
RK
1409 disorder_eclient_list_response *const completed =
1410 (disorder_eclient_list_response *)op->completed;
a99c4e9a
RK
1411 D(("rtp_response_opcallback"));
1412 if(c->rc / 100 == 2) {
4190a4d9
RK
1413 int nvec;
1414 char **vec = split(c->line + 4, &nvec, SPLIT_QUOTES, 0, 0);
a99c4e9a 1415
4190a4d9
RK
1416 if(vec)
1417 completed(op->v, NULL, nvec, vec);
1418 else
1419 completed(op->v, "error parsing response", 0, 0);
a99c4e9a 1420 } else
4190a4d9 1421 completed(op->v, errorstring(c), 0, 0);
a99c4e9a
RK
1422}
1423
1424/** @brief Determine the RTP target address
1425 * @param c Client
1426 * @param completed Called with address details
1427 * @param v Passed to @p completed
1428 *
1429 * The address details will be two elements, the first being the hostname and
1430 * the second the service (port).
1431 */
1432int disorder_eclient_rtp_address(disorder_eclient *c,
1433 disorder_eclient_list_response *completed,
1434 void *v) {
1435 return simple(c, rtp_response_opcallback, (void (*)())completed, v,
1436 "rtp-address", (char *)0);
1437}
1438
ffc4dbaf
RK
1439/** @brief Get the list of users
1440 * @param c Client
1441 * @param completed Called with list of users
1442 * @param v Passed to @p completed
1443 *
1444 * The user list is not sorted in any particular order.
1445 */
1446int disorder_eclient_users(disorder_eclient *c,
1447 disorder_eclient_list_response *completed,
1448 void *v) {
1449 return simple(c, list_response_opcallback, (void (*)())completed, v,
1450 "users", (char *)0);
1451}
1452
4aa8a0a4
RK
1453/** @brief Delete a user
1454 * @param c Client
1455 * @param completed Called on completion
1456 * @param user User to delete
1457 * @param v Passed to @p completed
1458 */
1459int disorder_eclient_deluser(disorder_eclient *c,
1460 disorder_eclient_no_response *completed,
1461 const char *user,
1462 void *v) {
1463 return simple(c, no_response_opcallback, (void (*)())completed, v,
1464 "deluser", user, (char *)0);
1465}
1466
e61aef23
RK
1467/** @brief Get a user property
1468 * @param c Client
1469 * @param completed Called on completion
1470 * @param user User to look up
1471 * @param property Property to look up
1472 * @param v Passed to @p completed
1473 */
1474int disorder_eclient_userinfo(disorder_eclient *c,
1475 disorder_eclient_string_response *completed,
1476 const char *user,
1477 const char *property,
1478 void *v) {
1479 return simple(c, string_response_opcallback, (void (*)())completed, v,
1480 "userinfo", user, property, (char *)0);
1481}
1482
1483/** @brief Modify a user property
1484 * @param c Client
1485 * @param completed Called on completion
1486 * @param user User to modify
1487 * @param property Property to modify
1488 * @param value New property value
1489 * @param v Passed to @p completed
1490 */
1491int disorder_eclient_edituser(disorder_eclient *c,
1492 disorder_eclient_no_response *completed,
1493 const char *user,
1494 const char *property,
1495 const char *value,
1496 void *v) {
1497 return simple(c, no_response_opcallback, (void (*)())completed, v,
1498 "edituser", user, property, value, (char *)0);
1499}
1500
0d719587
RK
1501/** @brief Create a new user
1502 * @param c Client
1503 * @param completed Called on completion
1504 * @param user User to create
1505 * @param password Initial password
1506 * @param rights Initial rights or NULL
1507 * @param v Passed to @p completed
1508 */
1509int disorder_eclient_adduser(disorder_eclient *c,
1510 disorder_eclient_no_response *completed,
1511 const char *user,
1512 const char *password,
1513 const char *rights,
1514 void *v) {
1515 return simple(c, no_response_opcallback, (void (*)())completed, v,
1516 "adduser", user, password, rights, (char *)0);
1517}
e61aef23 1518
d42e98ca
RK
1519/** @brief Adopt a track
1520 * @param c Client
1521 * @param completed Called on completion
1522 * @param id Track ID
1523 * @param v Passed to @p completed
1524 */
1525int disorder_eclient_adopt(disorder_eclient *c,
1526 disorder_eclient_no_response *completed,
1527 const char *id,
1528 void *v) {
1529 return simple(c, no_response_opcallback, (void (*)())completed, v,
1530 "adopt", id, (char *)0);
1531}
1532
53740beb
RK
1533/** @brief Get the list of playlists
1534 * @param c Client
1535 * @param completed Called with list of playlists
1536 * @param v Passed to @p completed
1537 *
1538 * The playlist list is not sorted in any particular order.
1539 */
1540int disorder_eclient_playlists(disorder_eclient *c,
1541 disorder_eclient_list_response *completed,
1542 void *v) {
1543 return simple(c, list_response_opcallback, (void (*)())completed, v,
1544 "playlists", (char *)0);
1545}
1546
1547/** @brief Delete a playlist
1548 * @param c Client
1549 * @param completed Called on completion
1550 * @param playlist Playlist to delete
1551 * @param v Passed to @p completed
1552 */
1553int disorder_eclient_playlist_delete(disorder_eclient *c,
1554 disorder_eclient_no_response *completed,
1555 const char *playlist,
1556 void *v) {
1557 return simple(c, no_response_opcallback, (void (*)())completed, v,
1558 "playlist-delete", playlist, (char *)0);
1559}
1560
1561/** @brief Lock a playlist
1562 * @param c Client
1563 * @param completed Called on completion
1564 * @param playlist Playlist to lock
1565 * @param v Passed to @p completed
1566 */
1567int disorder_eclient_playlist_lock(disorder_eclient *c,
1568 disorder_eclient_no_response *completed,
1569 const char *playlist,
1570 void *v) {
1571 return simple(c, no_response_opcallback, (void (*)())completed, v,
1572 "playlist-lock", playlist, (char *)0);
1573}
1574
1575/** @brief Unlock the locked a playlist
1576 * @param c Client
1577 * @param completed Called on completion
1578 * @param v Passed to @p completed
1579 */
1580int disorder_eclient_playlist_unlock(disorder_eclient *c,
1581 disorder_eclient_no_response *completed,
1582 void *v) {
1583 return simple(c, no_response_opcallback, (void (*)())completed, v,
1584 "playlist-unlock", (char *)0);
1585}
1586
1587/** @brief Set a playlist's sharing
1588 * @param c Client
1589 * @param completed Called on completion
1590 * @param playlist Playlist to modify
1591 * @param sharing @c "public" or @c "private"
1592 * @param v Passed to @p completed
1593 */
1594int disorder_eclient_playlist_set_share(disorder_eclient *c,
1595 disorder_eclient_no_response *completed,
1596 const char *playlist,
1597 const char *sharing,
1598 void *v) {
1599 return simple(c, no_response_opcallback, (void (*)())completed, v,
1600 "playlist-set-share", playlist, sharing, (char *)0);
1601}
1602
1603/** @brief Get a playlist's sharing
1604 * @param c Client
1605 * @param completed Called with sharing status
1606 * @param playlist Playlist to inspect
1607 * @param v Passed to @p completed
1608 */
1609int disorder_eclient_playlist_get_share(disorder_eclient *c,
1610 disorder_eclient_string_response *completed,
1611 const char *playlist,
1612 void *v) {
1613 return simple(c, string_response_opcallback, (void (*)())completed, v,
1614 "playlist-get-share", playlist, (char *)0);
1615}
1616
1617/** @brief Set a playlist
1618 * @param c Client
1619 * @param completed Called on completion
1620 * @param playlist Playlist to modify
1621 * @param tracks List of tracks
1622 * @param ntracks Number of tracks
1623 * @param v Passed to @p completed
1624 */
1625int disorder_eclient_playlist_set(disorder_eclient *c,
1626 disorder_eclient_no_response *completed,
1627 const char *playlist,
1628 char **tracks,
1629 int ntracks,
1630 void *v) {
1631 return simple_body(c, no_response_opcallback, (void (*)())completed, v,
1632 ntracks, tracks,
1633 "playlist-set", playlist, (char *)0);
1634}
1635
1636/** @brief Get a playlist's contents
1637 * @param c Client
1638 * @param completed Called with playlist contents
1639 * @param playlist Playlist to inspect
1640 * @param v Passed to @p completed
1641 */
1642int disorder_eclient_playlist_get(disorder_eclient *c,
1643 disorder_eclient_list_response *completed,
1644 const char *playlist,
1645 void *v) {
1646 return simple(c, list_response_opcallback, (void (*)())completed, v,
1647 "playlist-get", playlist, (char *)0);
1648}
1649
460b9539 1650/* Log clients ***************************************************************/
1651
60b95b6e 1652/** @brief Monitor the server log
1653 * @param c Client
1654 * @param callbacks Functions to call when anything happens
1655 * @param v Passed to @p callbacks functions
1656 *
1657 * Once a client is being used for logging it cannot be used for anything else.
1658 * There is magic in authuser_opcallback() to re-submit the @c log command
1659 * after reconnection.
6d1302f0
RK
1660 *
1661 * NB that the @c state callback may be called from within this function,
1662 * i.e. not solely later on from the event loop callback.
60b95b6e 1663 */
460b9539 1664int disorder_eclient_log(disorder_eclient *c,
1665 const disorder_eclient_log_callbacks *callbacks,
1666 void *v) {
1667 if(c->log_callbacks) return -1;
1668 c->log_callbacks = callbacks;
1669 c->log_v = v;
6d1302f0
RK
1670 /* Repoort initial state */
1671 if(c->log_callbacks->state)
1672 c->log_callbacks->state(c->log_v, c->statebits);
460b9539 1673 stash_command(c, 0/*queuejump*/, log_opcallback, 0/*completed*/, v,
53740beb 1674 -1, 0, "log", (char *)0);
bcc46dc3 1675 disorder_eclient_polled(c, 0);
460b9539 1676 return 0;
1677}
1678
1679/* If we get here we've stopped being a log client */
1680static void log_opcallback(disorder_eclient *c,
1681 struct operation attribute((unused)) *op) {
1682 D(("log_opcallback"));
1683 c->log_callbacks = 0;
1684 c->log_v = 0;
1685}
1686
1687/* error callback for log line parsing */
1688static void logline_error(const char *msg, void *u) {
1689 disorder_eclient *c = u;
1f3ce240 1690 /* TODO don't use protocol_error here */
460b9539 1691 protocol_error(c, c->ops, -1, "error parsing log line: %s", msg);
1692}
1693
1694/* process a single log line */
1695static void logline(disorder_eclient *c, const char *line) {
1696 int nvec, n;
1697 char **vec;
1698 uintmax_t when;
1699
6cd155a8 1700 D(("logline [%s]", line));
460b9539 1701 vec = split(line, &nvec, SPLIT_QUOTES, logline_error, c);
1702 if(nvec < 2) return; /* probably an error, already
1703 * reported */
1704 if(sscanf(vec[0], "%"SCNxMAX, &when) != 1) {
1705 /* probably the wrong side of a format change */
1f3ce240 1706 /* TODO don't use protocol_error here */
460b9539 1707 protocol_error(c, c->ops, -1, "invalid log timestamp '%s'", vec[0]);
1708 return;
1709 }
1710 /* TODO: do something with the time */
fa7d5980 1711 //fprintf(stderr, "log key: %s\n", vec[1]);
ba937f01 1712 n = TABLE_FIND(logentry_handlers, name, vec[1]);
fa7d5980
RK
1713 if(n < 0) {
1714 //fprintf(stderr, "...not found\n");
1715 return; /* probably a future command */
1716 }
460b9539 1717 vec += 2;
1718 nvec -= 2;
fa7d5980
RK
1719 if(nvec < logentry_handlers[n].min || nvec > logentry_handlers[n].max) {
1720 //fprintf(stderr, "...wrong # args\n");
460b9539 1721 return;
fa7d5980 1722 }
460b9539 1723 logentry_handlers[n].handler(c, nvec, vec);
1724}
1725
1726static void logentry_completed(disorder_eclient *c,
1727 int attribute((unused)) nvec, char **vec) {
277f7f59 1728 c->statebits &= ~DISORDER_PLAYING;
bcc46dc3
RK
1729 if(c->log_callbacks->completed)
1730 c->log_callbacks->completed(c->log_v, vec[0]);
8f763f1b
RK
1731 if(c->log_callbacks->state)
1732 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1733}
1734
1735static void logentry_failed(disorder_eclient *c,
1736 int attribute((unused)) nvec, char **vec) {
277f7f59 1737 c->statebits &= ~DISORDER_PLAYING;
bcc46dc3
RK
1738 if(c->log_callbacks->failed)
1739 c->log_callbacks->failed(c->log_v, vec[0], vec[1]);
8f763f1b
RK
1740 if(c->log_callbacks->state)
1741 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1742}
1743
1744static void logentry_moved(disorder_eclient *c,
1745 int attribute((unused)) nvec, char **vec) {
bcc46dc3
RK
1746 if(c->log_callbacks->moved)
1747 c->log_callbacks->moved(c->log_v, vec[0]);
460b9539 1748}
1749
1750static void logentry_playing(disorder_eclient *c,
1751 int attribute((unused)) nvec, char **vec) {
277f7f59 1752 c->statebits |= DISORDER_PLAYING;
bcc46dc3
RK
1753 if(c->log_callbacks->playing)
1754 c->log_callbacks->playing(c->log_v, vec[0], vec[1]);
8f763f1b
RK
1755 if(c->log_callbacks->state)
1756 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1757}
1758
1759static void logentry_queue(disorder_eclient *c,
1760 int attribute((unused)) nvec, char **vec) {
1761 struct queue_entry *q;
1762
bcc46dc3 1763 if(!c->log_callbacks->queue) return;
460b9539 1764 q = xmalloc(sizeof *q);
1765 if(queue_unmarshall_vec(q, nvec, vec, eclient_queue_error, c))
1766 return; /* bogus */
1767 c->log_callbacks->queue(c->log_v, q);
1768}
1769
1770static void logentry_recent_added(disorder_eclient *c,
1771 int attribute((unused)) nvec, char **vec) {
1772 struct queue_entry *q;
1773
1774 if(!c->log_callbacks->recent_added) return;
1775 q = xmalloc(sizeof *q);
1776 if(queue_unmarshall_vec(q, nvec, vec, eclient_queue_error, c))
1777 return; /* bogus */
1778 c->log_callbacks->recent_added(c->log_v, q);
1779}
1780
1781static void logentry_recent_removed(disorder_eclient *c,
1782 int attribute((unused)) nvec, char **vec) {
bcc46dc3
RK
1783 if(c->log_callbacks->recent_removed)
1784 c->log_callbacks->recent_removed(c->log_v, vec[0]);
460b9539 1785}
1786
1787static void logentry_removed(disorder_eclient *c,
1788 int attribute((unused)) nvec, char **vec) {
bcc46dc3
RK
1789 if(c->log_callbacks->removed)
1790 c->log_callbacks->removed(c->log_v, vec[0], vec[1]);
460b9539 1791}
1792
e025abff
RK
1793static void logentry_rescanned(disorder_eclient *c,
1794 int attribute((unused)) nvec,
1795 char attribute((unused)) **vec) {
bcc46dc3
RK
1796 if(c->log_callbacks->rescanned)
1797 c->log_callbacks->rescanned(c->log_v);
e025abff
RK
1798}
1799
460b9539 1800static void logentry_scratched(disorder_eclient *c,
1801 int attribute((unused)) nvec, char **vec) {
277f7f59 1802 c->statebits &= ~DISORDER_PLAYING;
bcc46dc3
RK
1803 if(c->log_callbacks->scratched)
1804 c->log_callbacks->scratched(c->log_v, vec[0], vec[1]);
8f763f1b
RK
1805 if(c->log_callbacks->state)
1806 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1807}
1808
58d2aad2
RK
1809static void logentry_user_add(disorder_eclient *c,
1810 int attribute((unused)) nvec, char **vec) {
1811 if(c->log_callbacks->user_add)
1812 c->log_callbacks->user_add(c->log_v, vec[0]);
1813}
1814
1815static void logentry_user_confirm(disorder_eclient *c,
1816 int attribute((unused)) nvec, char **vec) {
1817 if(c->log_callbacks->user_confirm)
1818 c->log_callbacks->user_confirm(c->log_v, vec[0]);
1819}
1820
1821static void logentry_user_delete(disorder_eclient *c,
1822 int attribute((unused)) nvec, char **vec) {
1823 if(c->log_callbacks->user_delete)
1824 c->log_callbacks->user_delete(c->log_v, vec[0]);
1825}
1826
1827static void logentry_user_edit(disorder_eclient *c,
1828 int attribute((unused)) nvec, char **vec) {
1829 if(c->log_callbacks->user_edit)
1830 c->log_callbacks->user_edit(c->log_v, vec[0], vec[1]);
1831}
1832
ad492e00
RK
1833static void logentry_rights_changed(disorder_eclient *c,
1834 int attribute((unused)) nvec, char **vec) {
1835 if(c->log_callbacks->rights_changed) {
1836 rights_type r;
fa7d5980 1837 if(!parse_rights(vec[0], &r, 0/*report*/))
ad492e00
RK
1838 c->log_callbacks->rights_changed(c->log_v, r);
1839 }
1840}
1841
5c102112
RK
1842static void logentry_playlist_created(disorder_eclient *c,
1843 int attribute((unused)) nvec,
1844 char **vec) {
1845 if(c->log_callbacks->playlist_created)
1846 c->log_callbacks->playlist_created(c->log_v, vec[0], vec[1]);
1847}
1848
1849static void logentry_playlist_deleted(disorder_eclient *c,
1850 int attribute((unused)) nvec,
1851 char **vec) {
1852 if(c->log_callbacks->playlist_deleted)
1853 c->log_callbacks->playlist_deleted(c->log_v, vec[0]);
1854}
1855
1856static void logentry_playlist_modified(disorder_eclient *c,
1857 int attribute((unused)) nvec,
1858 char **vec) {
1859 if(c->log_callbacks->playlist_modified)
1860 c->log_callbacks->playlist_modified(c->log_v, vec[0], vec[1]);
1861}
1862
460b9539 1863static const struct {
1864 unsigned long bit;
1865 const char *enable;
1866 const char *disable;
1867} statestrings[] = {
1868 { DISORDER_PLAYING_ENABLED, "enable_play", "disable_play" },
1869 { DISORDER_RANDOM_ENABLED, "enable_random", "disable_random" },
1870 { DISORDER_TRACK_PAUSED, "pause", "resume" },
e56e1cc0
RK
1871 { DISORDER_PLAYING, "playing", "completed" },
1872 { DISORDER_PLAYING, 0, "scratched" },
1873 { DISORDER_PLAYING, 0, "failed" },
460b9539 1874};
6cd155a8 1875#define NSTATES (int)(sizeof statestrings / sizeof *statestrings)
460b9539 1876
1877static void logentry_state(disorder_eclient *c,
1878 int attribute((unused)) nvec, char **vec) {
1879 int n;
1880
1881 for(n = 0; n < NSTATES; ++n)
e56e1cc0 1882 if(statestrings[n].enable && !strcmp(vec[0], statestrings[n].enable)) {
460b9539 1883 c->statebits |= statestrings[n].bit;
1884 break;
e56e1cc0 1885 } else if(statestrings[n].disable && !strcmp(vec[0], statestrings[n].disable)) {
460b9539 1886 c->statebits &= ~statestrings[n].bit;
1887 break;
1888 }
bcc46dc3
RK
1889 if(c->log_callbacks->state)
1890 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
460b9539 1891}
1892
1893static void logentry_volume(disorder_eclient *c,
1894 int attribute((unused)) nvec, char **vec) {
1895 long l, r;
1896
1897 if(!c->log_callbacks->volume) return;
1898 if(xstrtol(&l, vec[0], 0, 10)
1899 || xstrtol(&r, vec[1], 0, 10)
1900 || l < 0 || l > INT_MAX
1901 || r < 0 || r > INT_MAX)
1902 return; /* bogus */
1903 c->log_callbacks->volume(c->log_v, (int)l, (int)r);
1904}
1905
00959300
RK
1906/** @brief Convert @p statebits to a string */
1907char *disorder_eclient_interpret_state(unsigned long statebits) {
1908 struct dynstr d[1];
1909 size_t n;
1910
1911 static const struct {
1912 unsigned long bit;
1913 const char *name;
1914 } bits[] = {
1915 { DISORDER_PLAYING_ENABLED, "playing_enabled" },
1916 { DISORDER_RANDOM_ENABLED, "random_enabled" },
1917 { DISORDER_TRACK_PAUSED, "track_paused" },
1918 { DISORDER_PLAYING, "playing" },
1919 { DISORDER_CONNECTED, "connected" },
1920 };
1921#define NBITS (sizeof bits / sizeof *bits)
1922
1923 dynstr_init(d);
6d1302f0
RK
1924 if(!statebits)
1925 dynstr_append(d, '0');
00959300
RK
1926 for(n = 0; n < NBITS; ++n)
1927 if(statebits & bits[n].bit) {
1928 if(d->nvec)
1929 dynstr_append(d, '|');
1930 dynstr_append_string(d, bits[n].name);
1931 statebits ^= bits[n].bit;
1932 }
1933 if(statebits) {
1934 char s[20];
1935
1936 if(d->nvec)
1937 dynstr_append(d, '|');
1938 sprintf(s, "%#lx", statebits);
1939 dynstr_append_string(d, s);
1940 }
1941 dynstr_terminate(d);
1942 return d->vec;
1943}
1944
9433fabf
RK
1945static void logentry_adopted(disorder_eclient *c,
1946 int attribute((unused)) nvec, char **vec) {
1947 if(c->log_callbacks->adopted)
1948 c->log_callbacks->adopted(c->log_v, vec[0], vec[1]);
1949}
1950
ad0c0f36
RK
1951static void logentry_global_pref(disorder_eclient *c,
1952 int nvec, char **vec) {
1953 if(c->log_callbacks->global_pref)
1954 c->log_callbacks->global_pref(c->log_v, vec[0], nvec > 1 ? vec[1] : 0);
1955}
1956
460b9539 1957/*
1958Local Variables:
1959c-basic-offset:2
1960comment-column:40
1961fill-column:79
1962indent-tabs-mode:nil
1963End:
1964*/