2 * This file is part of DisOrder.
3 * Copyright (C) 2006-2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file lib/eclient.c
21 * @brief Client code for event-driven programs
26 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
39 #include "configuration.h"
46 #include "inputline.h"
53 #include "client-common.h"
55 /* TODO: more commands */
57 /** @brief How often to send data to the server when receiving logs */
58 #define LOG_PROD_INTERVAL 10
60 /* Types *********************************************************************/
62 /** @brief Client state */
64 state_disconnected, /**< @brief not connected */
65 state_connecting, /**< @brief waiting for connect() */
66 state_connected, /**< @brief connected but not authenticated */
67 state_idle, /**< @brief not doing anything */
68 state_cmdresponse, /**< @brief waiting for command resonse */
69 state_body, /**< @brief accumulating body */
70 state_log, /**< @brief monitoring log */
73 /** @brief Names for @ref client_state */
74 static const char *const states[] = {
84 struct operation; /* forward decl */
86 /** @brief Type of an operation callback */
87 typedef void operation_callback(disorder_eclient *c, struct operation *op);
89 /** @brief A pending operation.
91 * This can be either a command or part of the authentication protocol. In the
92 * former case new commands are appended to the list, in the latter case they
93 * are inserted at the front. */
95 struct operation *next; /**< @brief next operation */
96 char *cmd; /**< @brief command to send or 0 */
97 char **body; /**< @brief command body */
98 operation_callback *opcallback; /**< @brief internal completion callback */
99 void (*completed)(); /**< @brief user completion callback or 0 */
100 void *v; /**< @brief data for COMPLETED */
101 disorder_eclient *client; /**< @brief owning client */
103 /** @brief true if sent to server
105 * This is cleared by disorder_eclient_close(), forcing all queued
106 * commands to be transparently resent.
111 /** @brief Client structure */
112 struct disorder_eclient {
114 int fd; /**< @brief connection to server */
115 enum client_state state; /**< @brief current state */
116 int authenticated; /**< @brief true when authenicated */
117 struct dynstr output; /**< @brief output buffer */
118 struct dynstr input; /**< @brief input buffer */
119 int eof; /**< @brief input buffer is at EOF */
120 const disorder_eclient_callbacks *callbacks; /**< @brief error callbacks */
121 void *u; /**< @brief user data */
122 struct operation *ops; /**< @brief queue of operations */
123 struct operation **opstail; /**< @brief queue tail */
124 /* accumulated response */
125 int rc; /**< @brief response code */
126 char *line; /**< @brief complete line */
127 struct vector vec; /**< @brief body */
129 const disorder_eclient_log_callbacks *log_callbacks;
130 /**< @brief log callbacks
132 * Once disorder_eclient_log() has been issued this is always set. When we
133 * re-connect it is checked to re-issue the log command.
135 void *log_v; /**< @brief user data */
136 unsigned long statebits; /**< @brief latest state */
139 /**< @brief last time we sent a prod
141 * When we are receiving log data we send a "prod" byte to the server from
142 * time to time so that we detect broken connections reasonably quickly. The
143 * server just ignores these bytes.
146 /** @brief Protocol version */
149 /** @brief True if enabled */
153 /* Forward declarations ******************************************************/
155 static int start_connect(disorder_eclient *c);
156 static void process_line(disorder_eclient *c, char *line);
157 static void maybe_connected(disorder_eclient *c);
158 static void authbanner_opcallback(disorder_eclient *c,
159 struct operation *op);
160 static void authuser_opcallback(disorder_eclient *c,
161 struct operation *op);
162 static void complete(disorder_eclient *c);
163 static void send_output(disorder_eclient *c);
164 static void put(disorder_eclient *c, const char *s, size_t n);
165 static void read_input(disorder_eclient *c);
166 static void stash_command(disorder_eclient *c,
168 operation_callback *opcallback,
175 static void log_opcallback(disorder_eclient *c, struct operation *op);
176 static void logline(disorder_eclient *c, const char *line);
177 static void logentry_completed(disorder_eclient *c, int nvec, char **vec);
178 static void logentry_failed(disorder_eclient *c, int nvec, char **vec);
179 static void logentry_moved(disorder_eclient *c, int nvec, char **vec);
180 static void logentry_playing(disorder_eclient *c, int nvec, char **vec);
181 static void logentry_queue(disorder_eclient *c, int nvec, char **vec);
182 static void logentry_recent_added(disorder_eclient *c, int nvec, char **vec);
183 static void logentry_recent_removed(disorder_eclient *c, int nvec, char **vec);
184 static void logentry_removed(disorder_eclient *c, int nvec, char **vec);
185 static void logentry_scratched(disorder_eclient *c, int nvec, char **vec);
186 static void logentry_state(disorder_eclient *c, int nvec, char **vec);
187 static void logentry_volume(disorder_eclient *c, int nvec, char **vec);
188 static void logentry_rescanned(disorder_eclient *c, int nvec, char **vec);
189 static void logentry_user_add(disorder_eclient *c, int nvec, char **vec);
190 static void logentry_user_confirm(disorder_eclient *c, int nvec, char **vec);
191 static void logentry_user_delete(disorder_eclient *c, int nvec, char **vec);
192 static void logentry_user_edit(disorder_eclient *c, int nvec, char **vec);
193 static void logentry_rights_changed(disorder_eclient *c, int nvec, char **vec);
195 /* Tables ********************************************************************/
197 /** @brief One possible log entry */
198 struct logentry_handler {
199 const char *name; /**< @brief Entry name */
200 int min; /**< @brief Minimum arguments */
201 int max; /**< @brief Maximum arguments */
202 void (*handler)(disorder_eclient *c,
204 char **vec); /**< @brief Handler function */
207 /** @brief Table for parsing log entries */
208 static const struct logentry_handler logentry_handlers[] = {
209 #define LE(X, MIN, MAX) { #X, MIN, MAX, logentry_##X }
214 LE(queue, 2, INT_MAX),
215 LE(recent_added, 2, INT_MAX),
216 LE(recent_removed, 1, 1),
219 LE(rights_changed, 1, 1),
223 LE(user_confirm, 1, 1),
224 LE(user_delete, 1, 1),
229 /* Setup and teardown ********************************************************/
231 /** @brief Create a new client
233 * Does NOT connect the client - connections are made (and re-made) on demand.
235 disorder_eclient *disorder_eclient_new(const disorder_eclient_callbacks *cb,
237 disorder_eclient *c = xmalloc(sizeof *c);
238 D(("disorder_eclient_new"));
242 c->opstail = &c->ops;
244 vector_init(&c->vec);
245 dynstr_init(&c->input);
246 dynstr_init(&c->output);
250 /** @brief Disconnect a client
251 * @param c Client to disconnect
253 * NB that this routine just disconnnects the TCP connection. It does not
254 * destroy the client! If you continue to use it then it will attempt to
257 void disorder_eclient_close(disorder_eclient *c) {
258 struct operation *op;
260 D(("disorder_eclient_close"));
262 D(("disorder_eclient_close closing fd %d", c->fd));
263 c->callbacks->poll(c->u, c, c->fd, 0);
266 c->state = state_disconnected;
272 c->authenticated = 0;
273 /* We'll need to resend all operations */
274 for(op = c->ops; op; op = op->next)
276 /* Drop our use a hint that we're disconnected */
277 if(c->log_callbacks && c->log_callbacks->state)
278 c->log_callbacks->state(c->log_v, c->statebits);
281 /** @brief Permit new connection activity */
282 void disorder_eclient_enable_connect(disorder_eclient *c) {
286 /** @brief Suppress new connection activity */
287 void disorder_eclient_disable_connect(disorder_eclient *c) {
291 /** @brief Return current state */
292 unsigned long disorder_eclient_state(const disorder_eclient *c) {
293 return c->statebits | (c->state > state_connected ? DISORDER_CONNECTED : 0);
296 /* Error reporting ***********************************************************/
298 /** @brief called when a connection error occurs
300 * After this called we will be disconnected (by disorder_eclient_close()),
301 * so there will be a reconnection before any commands can be sent.
303 static int comms_error(disorder_eclient *c, const char *fmt, ...) {
309 byte_xvasprintf(&s, fmt, ap);
311 disorder_eclient_close(c);
312 c->callbacks->comms_error(c->u, s);
316 /** @brief called when the server reports an error */
317 static int protocol_error(disorder_eclient *c, struct operation *op,
318 int code, const char *fmt, ...) {
322 D(("protocol_error"));
324 byte_xvasprintf(&s, fmt, ap);
326 c->callbacks->protocol_error(c->u, op->v, code, s);
330 /* State machine *************************************************************/
332 /** @brief Send an operation (into the output buffer)
333 * @param op Operation to send
335 static void op_send(struct operation *op) {
336 disorder_eclient *const c = op->client;
337 put(c, op->cmd, strlen(op->cmd));
339 for(int n = 0; op->body[n]; ++n) {
340 if(op->body[n][0] == '.')
342 put(c, op->body[n], strlen(op->body[n]));
350 /** @brief Called when there's something to do
352 * @param mode bitmap of @ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE.
354 * This should be called from by your code when the file descriptor is readable
355 * or writable (as requested by the @c poll callback, see @ref
356 * disorder_eclient_callbacks) and in any case from time to time (with @p mode
357 * = 0) to allow for retries to work.
359 void disorder_eclient_polled(disorder_eclient *c, unsigned mode) {
360 struct operation *op;
363 D(("disorder_eclient_polled fd=%d state=%s mode=[%s %s]",
364 c->fd, states[c->state],
365 mode & DISORDER_POLL_READ ? "READ" : "",
366 mode & DISORDER_POLL_WRITE ? "WRITE" : ""));
367 /* The pattern here is to check each possible state in turn and try to
368 * advance (though on error we might go back). If we advance we leave open
369 * the possibility of falling through to the next state, but we set the mode
370 * bits to 0, to avoid false positives (which matter more in some cases than
373 if(c->state == state_disconnected) {
374 D(("state_disconnected"));
375 /* If there is no password yet then we cannot connect */
376 if(!config->password) {
377 comms_error(c, "no password is configured");
381 /* Only try to connect if enabled */
384 /* might now be state_disconnected (on error), state_connecting (slow
385 * connect) or state_connected (fast connect). If state_disconnected then
386 * we just rely on a periodic callback from the event loop sometime. */
390 if(c->state == state_connecting && mode) {
391 D(("state_connecting"));
393 /* Might be state_disconnected (on error) or state_connected (on success).
394 * In the former case we rely on the event loop for a periodic callback to
399 if(c->state == state_connected) {
400 D(("state_connected"));
401 /* We just connected. Initiate the authentication protocol. */
402 stash_command(c, 1/*queuejump*/, authbanner_opcallback,
403 0/*completed*/, 0/*v*/, -1/*nbody*/, 0/*body*/, 0/*cmd*/);
404 /* We never stay is state_connected very long. We could in principle jump
405 * straight to state_cmdresponse since there's actually no command to
406 * send, but that would arguably be cheating. */
407 c->state = state_idle;
410 if(c->state == state_idle) {
412 /* We are connected, and have finished any command we set off, look for
416 if(c->authenticated) {
417 /* Transmit all unsent operations */
418 for(op = c->ops; op; op = op->next) {
423 /* Just send the head operation */
424 if(c->ops->cmd && !c->ops->sent)
427 /* Awaiting response for the operation at the head of the list */
428 c->state = state_cmdresponse;
431 c->callbacks->report(c->u, 0);
434 /* Queue up a byte to send */
435 if(c->state == state_log
436 && c->output.nvec == 0
437 && time(&now) - c->last_prod > LOG_PROD_INTERVAL) {
442 if(c->state == state_cmdresponse
443 || c->state == state_body
444 || c->state == state_log) {
445 D(("state_%s", states[c->state]));
446 /* We are awaiting a response */
447 if(mode & DISORDER_POLL_WRITE) send_output(c);
448 if(mode & DISORDER_POLL_READ) read_input(c);
449 /* There are a couple of reasons we might want to re-enter the state
450 * machine from the top. state_idle is obvious: there may be further
451 * commands to process. Re-entering on state_disconnected means that we
452 * immediately retry connection if a comms error occurs during a command.
453 * This is different to the case where a connection fails, where we await a
454 * spontaneous call to initiate the retry. */
456 case state_disconnected: /* lost connection */
457 case state_idle: /* completed a command */
459 disorder_eclient_polled(c, 0);
466 /* Figure out what to set the mode to */
468 case state_disconnected:
469 D(("state_disconnected (2)"));
470 /* Probably an error occurred. Await a retry. */
473 case state_connecting:
474 D(("state_connecting (2)"));
475 /* Waiting for connect to complete */
476 mode = DISORDER_POLL_READ|DISORDER_POLL_WRITE;
478 case state_connected:
479 D(("state_connected (2)"));
480 assert(!"should never be in state_connected here");
483 D(("state_idle (2)"));
484 /* Connected but nothing to do. */
487 case state_cmdresponse:
490 D(("state_%s (2)", states[c->state]));
491 /* Gathering a response. Wait for input. */
492 mode = DISORDER_POLL_READ;
493 /* Flush any pending output. */
494 if(c->output.nvec) mode |= DISORDER_POLL_WRITE;
497 D(("fd=%d new mode [%s %s]",
499 mode & DISORDER_POLL_READ ? "READ" : "",
500 mode & DISORDER_POLL_WRITE ? "WRITE" : ""));
501 if(c->fd != -1) c->callbacks->poll(c->u, c, c->fd, mode);
504 /** @brief Called to start connecting */
505 static int start_connect(disorder_eclient *c) {
509 D(("start_connect"));
510 if((len = find_server(config, &sa, &c->ident)) == (socklen_t)-1)
511 return comms_error(c, "cannot look up server"); /* TODO better error */
516 if((c->fd = socket(sa->sa_family, SOCK_STREAM, 0)) < 0)
517 return comms_error(c, "socket: %s", strerror(errno));
521 if(connect(c->fd, sa, len) < 0) {
525 c->state = state_connecting;
526 /* We are called from _polled so the state machine will get to do its
530 /* Signal the error to the caller. */
531 return comms_error(c, "connecting to %s: %s", c->ident, strerror(errno));
534 c->state = state_connected;
538 /** @brief Called when poll triggers while waiting for a connection */
539 static void maybe_connected(disorder_eclient *c) {
540 /* We either connected, or got an error. */
542 socklen_t len = sizeof err;
544 D(("maybe_connected"));
545 /* Work around over-enthusiastic error slippage */
546 if(getsockopt(c->fd, SOL_SOCKET, SO_ERROR, &err, &len) < 0)
549 /* The connection failed */
550 comms_error(c, "connecting to %s: %s", c->ident, strerror(err));
551 /* sets state_disconnected */
555 /* The connection succeeded */
556 c->state = state_connected;
557 byte_xasprintf(&r, "connected to %s", c->ident);
558 c->callbacks->report(c->u, r);
559 /* If this is a log client we expect to get a bunch of updates from the
560 * server straight away */
564 /* Authentication ************************************************************/
566 /** @brief Called with the greeting from the server */
567 static void authbanner_opcallback(disorder_eclient *c,
568 struct operation *op) {
570 const unsigned char *nonce;
574 const char *protocol, *algorithm, *challenge;
576 D(("authbanner_opcallback"));
578 || !(rvec = split(c->line + 4, &nrvec, SPLIT_QUOTES, 0, 0))
580 /* Banner told us to go away, or was malformed. We cannot proceed. */
581 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
582 disorder_eclient_close(c);
602 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
603 disorder_eclient_close(c);
606 c->protocol = atoi(protocol);
607 if(c->protocol < 1 || c->protocol > 2) {
608 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
609 disorder_eclient_close(c);
612 nonce = unhex(challenge, &nonce_len);
613 res = authhash(nonce, nonce_len, config->password, algorithm);
615 protocol_error(c, op, c->rc, "%s: unknown authentication algorithm '%s'",
616 c->ident, algorithm);
617 disorder_eclient_close(c);
620 stash_command(c, 1/*queuejump*/, authuser_opcallback, 0/*completed*/, 0/*v*/,
621 -1/*nbody*/, 0/*body*/,
622 "user", quoteutf8(config->username), quoteutf8(res),
626 /** @brief Called with the response to the @c user command */
627 static void authuser_opcallback(disorder_eclient *c,
628 struct operation *op) {
631 D(("authuser_opcallback"));
632 if(c->rc / 100 != 2) {
633 /* Wrong password or something. We cannot proceed. */
634 protocol_error(c, op, c->rc, "%s: %s", c->ident, c->line);
636 disorder_eclient_close(c);
639 /* OK, we're authenticated now. */
640 c->authenticated = 1;
641 byte_xasprintf(&r, "authenticated with %s", c->ident);
642 c->callbacks->report(c->u, r);
643 if(c->log_callbacks && !(c->ops && c->ops->opcallback == log_opcallback))
644 /* We are a log client, switch to logging mode */
645 stash_command(c, 0/*queuejump*/, log_opcallback, 0/*completed*/, c->log_v,
646 -1/*nbody*/, 0/*body*/,
650 /* Output ********************************************************************/
652 /* Chop N bytes off the front of a dynstr */
653 static void consume(struct dynstr *d, int n) {
654 D(("consume %d", n));
655 assert(d->nvec >= n);
656 memmove(d->vec, d->vec + n, d->nvec - n);
660 /* Write some bytes */
661 static void put(disorder_eclient *c, const char *s, size_t n) {
662 D(("put %d %.*s", c->fd, (int)n, s));
663 dynstr_append_bytes(&c->output, s, n);
666 /* Called when we can write to our FD, or at any other time */
667 static void send_output(disorder_eclient *c) {
670 D(("send_output %d bytes pending", c->output.nvec));
671 if(c->state > state_connecting && c->output.nvec) {
672 n = write(c->fd, c->output.vec, c->output.nvec);
679 comms_error(c, "writing to %s: %s", c->ident, strerror(errno));
683 consume(&c->output, n);
687 /* Input *********************************************************************/
689 /* Called when c->fd might be readable, or at any other time */
690 static void read_input(disorder_eclient *c) {
695 D(("read_input in state %s", states[c->state]));
696 if(c->state <= state_connected) return; /* ignore bogus calls */
697 /* read some more input */
698 n = read(c->fd, buffer, sizeof buffer);
705 comms_error(c, "reading from %s: %s", c->ident, strerror(errno));
708 return; /* no new input to process */
710 D(("read %d bytes: [%.*s]", n, n, buffer));
711 dynstr_append_bytes(&c->input, buffer, n);
714 /* might have more than one line to process */
715 while(c->state > state_connecting
716 && (nl = memchr(c->input.vec, '\n', c->input.nvec))) {
717 process_line(c, xstrndup(c->input.vec, nl - c->input.vec));
718 /* we might have disconnected along the way, which zogs the input buffer */
719 if(c->state > state_connecting)
720 consume(&c->input, (nl - c->input.vec) + 1);
723 comms_error(c, "reading from %s: server disconnected", c->ident);
724 c->authenticated = 0;
728 /* called with a line that has just been read */
729 static void process_line(disorder_eclient *c, char *line) {
730 D(("process_line %d [%s]", c->fd, line));
732 case state_cmdresponse:
733 /* This is the first line of a response */
734 if(!(line[0] >= '0' && line[0] <= '9'
735 && line[1] >= '0' && line[1] <= '9'
736 && line[2] >= '0' && line[2] <= '9'
738 fatal(0, "invalid response from server: %s", line);
739 c->rc = (line[0] * 10 + line[1]) * 10 + line[2] - 111 * '0';
743 /* We need to collect the body. */
744 c->state = state_body;
745 vector_init(&c->vec);
748 assert(c->log_callbacks != 0);
749 if(c->log_callbacks->connected)
750 c->log_callbacks->connected(c->log_v);
751 c->state = state_log;
754 /* We've got the whole response. Go into the idle state so the state
755 * machine knows we're done and then call the operation callback. */
761 if(strcmp(line, ".")) {
762 /* A line from the body */
763 vector_append(&c->vec, line + (line[0] == '.'));
765 /* End of the body. */
766 vector_terminate(&c->vec);
771 if(strcmp(line, ".")) {
772 logline(c, line + (line[0] == '.'));
777 assert(!"wrong state for location");
782 /* Called when an operation completes */
783 static void complete(disorder_eclient *c) {
784 struct operation *op;
787 /* Pop the operation off the queue */
790 if(c->opstail == &op->next)
791 c->opstail = &c->ops;
792 /* If we've pipelined a command ahead then we go straight to cmdresponser.
793 * Otherwise we go to idle, which will arrange further sends. */
794 c->state = c->ops && c->ops->sent ? state_cmdresponse : state_idle;
795 op->opcallback(c, op);
796 /* Note that we always call the opcallback even on error, though command
797 * opcallbacks generally always do the same error handling, i.e. just call
798 * protocol_error(). It's the auth* opcallbacks that have different
802 /* Operation setup ***********************************************************/
804 static void stash_command_vector(disorder_eclient *c,
806 operation_callback *opcallback,
813 struct operation *op = xmalloc(sizeof *op);
819 for(n = 0; n < ncmd; ++n) {
821 dynstr_append(&d, ' ');
822 dynstr_append_string(&d, quoteutf8(cmd[n]));
824 dynstr_append(&d, '\n');
825 dynstr_terminate(&d);
828 op->cmd = 0; /* usually, awaiting challenge */
830 op->body = xcalloc(nbody + 1, sizeof (char *));
831 for(n = 0; n < nbody; ++n)
832 op->body[n] = xstrdup(body[n]);
836 op->opcallback = opcallback;
837 op->completed = completed;
841 assert(op->sent == 0);
843 /* Authentication operations jump the queue of useful commands */
846 if(c->opstail == &c->ops)
847 c->opstail = &op->next;
848 for(op = c->ops; op; op = op->next)
852 c->opstail = &op->next;
856 static void vstash_command(disorder_eclient *c,
858 operation_callback *opcallback,
863 const char *cmd, va_list ap) {
867 D(("vstash_command %s", cmd ? cmd : "NULL"));
870 vector_append(&vec, (char *)cmd);
871 while((arg = va_arg(ap, char *)))
872 vector_append(&vec, arg);
873 stash_command_vector(c, queuejump, opcallback, completed, v,
874 nbody, body, vec.nvec, vec.vec);
876 stash_command_vector(c, queuejump, opcallback, completed, v,
881 static void stash_command(disorder_eclient *c,
883 operation_callback *opcallback,
893 vstash_command(c, queuejump, opcallback, completed, v, nbody, body, cmd, ap);
897 /* Command support ***********************************************************/
899 static const char *errorstring(disorder_eclient *c) {
902 byte_xasprintf(&s, "%s: %s: %d", c->ident, c->line, c->rc);
906 /* for commands with a quoted string response */
907 static void string_response_opcallback(disorder_eclient *c,
908 struct operation *op) {
909 disorder_eclient_string_response *completed
910 = (disorder_eclient_string_response *)op->completed;
912 D(("string_response_callback"));
913 if(c->rc / 100 == 2 || c->rc == 555) {
916 completed(op->v, NULL, NULL);
917 else if(c->protocol >= 2) {
918 char **rr = split(c->line + 4, 0, SPLIT_QUOTES, 0, 0);
921 completed(op->v, NULL, *rr);
923 /* TODO error message a is bit lame but generally indicates a server
924 * bug rather than anything the user can address */
925 completed(op->v, "error parsing response", NULL);
927 completed(op->v, NULL, c->line + 4);
930 completed(op->v, errorstring(c), NULL);
933 /* for commands with a simple integer response */
934 static void integer_response_opcallback(disorder_eclient *c,
935 struct operation *op) {
936 disorder_eclient_integer_response *completed
937 = (disorder_eclient_integer_response *)op->completed;
939 D(("string_response_callback"));
940 if(c->rc / 100 == 2) {
944 e = xstrtol(&n, c->line + 4, 0, 10);
946 completed(op->v, strerror(e), 0);
948 completed(op->v, 0, n);
950 completed(op->v, errorstring(c), 0);
953 /* for commands with no response */
954 static void no_response_opcallback(disorder_eclient *c,
955 struct operation *op) {
956 disorder_eclient_no_response *completed
957 = (disorder_eclient_no_response *)op->completed;
959 D(("no_response_callback"));
961 completed(op->v, NULL);
963 completed(op->v, errorstring(c));
966 /* error callback for queue_unmarshall */
967 static void eclient_queue_error(const char *msg,
969 struct operation *op = u;
971 /* TODO don't use protocol_error here */
972 protocol_error(op->client, op, -1, "error parsing queue entry: %s", msg);
975 /* for commands that expect a queue dump */
976 static void queue_response_opcallback(disorder_eclient *c,
977 struct operation *op) {
978 disorder_eclient_queue_response *const completed
979 = (disorder_eclient_queue_response *)op->completed;
981 int parse_failed = 0;
982 struct queue_entry *q, *qh = 0, **qtail = &qh, *qlast = 0;
984 D(("queue_response_callback"));
985 if(c->rc / 100 == 2) {
986 /* parse the queue */
987 for(n = 0; n < c->vec.nvec; ++n) {
988 q = xmalloc(sizeof *q);
989 D(("queue_unmarshall %s", c->vec.vec[n]));
990 if(!queue_unmarshall(q, c->vec.vec[n], NULL, op)) {
998 /* Currently we pass the partial queue to the callback along with the
999 * error. This might not be very useful in practice... */
1001 completed(op->v, "cannot parse result", qh);
1003 completed(op->v, 0, qh);
1005 completed(op->v, errorstring(c), 0);
1009 static void playing_response_opcallback(disorder_eclient *c,
1010 struct operation *op) {
1011 disorder_eclient_queue_response *const completed
1012 = (disorder_eclient_queue_response *)op->completed;
1013 struct queue_entry *q;
1015 D(("playing_response_callback"));
1016 if(c->rc / 100 == 2) {
1017 switch(c->rc % 10) {
1019 if(queue_unmarshall(q = xmalloc(sizeof *q), c->line + 4,
1021 completed(op->v, "cannot parse result", 0);
1023 completed(op->v, 0, q);
1026 completed(op->v, 0, 0);
1029 completed(op->v, errorstring(c), 0);
1033 completed(op->v, errorstring(c), 0);
1036 /* for commands that expect a list of some sort */
1037 static void list_response_opcallback(disorder_eclient *c,
1038 struct operation *op) {
1039 disorder_eclient_list_response *const completed =
1040 (disorder_eclient_list_response *)op->completed;
1042 D(("list_response_callback"));
1043 if(c->rc / 100 == 2)
1044 completed(op->v, NULL, c->vec.nvec, c->vec.vec);
1045 else if(c->rc == 555)
1046 completed(op->v, NULL, -1, NULL);
1048 completed(op->v, errorstring(c), 0, 0);
1052 static void volume_response_opcallback(disorder_eclient *c,
1053 struct operation *op) {
1054 disorder_eclient_volume_response *completed
1055 = (disorder_eclient_volume_response *)op->completed;
1058 D(("volume_response_callback"));
1059 if(c->rc / 100 == 2) {
1061 if(sscanf(c->line + 4, "%d %d", &l, &r) != 2 || l < 0 || r < 0)
1062 completed(op->v, "cannot parse volume response", 0, 0);
1064 completed(op->v, 0, l, r);
1067 completed(op->v, errorstring(c), 0, 0);
1070 static int simple(disorder_eclient *c,
1071 operation_callback *opcallback,
1072 void (*completed)(),
1074 const char *cmd, ...) {
1078 vstash_command(c, 0/*queuejump*/, opcallback, completed, v, -1, 0, cmd, ap);
1080 /* Give the state machine a kick, since we might be in state_idle */
1081 disorder_eclient_polled(c, 0);
1085 static int simple_body(disorder_eclient *c,
1086 operation_callback *opcallback,
1087 void (*completed)(),
1091 const char *cmd, ...) {
1095 vstash_command(c, 0/*queuejump*/, opcallback, completed, v, nbody, body, cmd, ap);
1097 /* Give the state machine a kick, since we might be in state_idle */
1098 disorder_eclient_polled(c, 0);
1102 /* Commands ******************************************************************/
1104 int disorder_eclient_version(disorder_eclient *c,
1105 disorder_eclient_string_response *completed,
1107 return simple(c, string_response_opcallback, (void (*)())completed, v,
1108 "version", (char *)0);
1111 int disorder_eclient_namepart(disorder_eclient *c,
1112 disorder_eclient_string_response *completed,
1114 const char *context,
1117 return simple(c, string_response_opcallback, (void (*)())completed, v,
1118 "part", track, context, part, (char *)0);
1121 int disorder_eclient_play(disorder_eclient *c,
1123 disorder_eclient_no_response *completed,
1125 return simple(c, no_response_opcallback, (void (*)())completed, v,
1126 "play", track, (char *)0);
1129 int disorder_eclient_pause(disorder_eclient *c,
1130 disorder_eclient_no_response *completed,
1132 return simple(c, no_response_opcallback, (void (*)())completed, v,
1133 "pause", (char *)0);
1136 int disorder_eclient_resume(disorder_eclient *c,
1137 disorder_eclient_no_response *completed,
1139 return simple(c, no_response_opcallback, (void (*)())completed, v,
1140 "resume", (char *)0);
1143 int disorder_eclient_scratch(disorder_eclient *c,
1145 disorder_eclient_no_response *completed,
1147 return simple(c, no_response_opcallback, (void (*)())completed, v,
1148 "scratch", id, (char *)0);
1151 int disorder_eclient_scratch_playing(disorder_eclient *c,
1152 disorder_eclient_no_response *completed,
1154 return disorder_eclient_scratch(c, 0, completed, v);
1157 int disorder_eclient_remove(disorder_eclient *c,
1159 disorder_eclient_no_response *completed,
1161 return simple(c, no_response_opcallback, (void (*)())completed, v,
1162 "remove", id, (char *)0);
1165 int disorder_eclient_moveafter(disorder_eclient *c,
1169 disorder_eclient_no_response *completed,
1175 vector_append(&vec, (char *)"moveafter");
1176 vector_append(&vec, (char *)target);
1177 for(n = 0; n < nids; ++n)
1178 vector_append(&vec, (char *)ids[n]);
1179 stash_command_vector(c, 0/*queuejump*/, no_response_opcallback, completed, v,
1180 -1, 0, vec.nvec, vec.vec);
1181 disorder_eclient_polled(c, 0);
1185 int disorder_eclient_recent(disorder_eclient *c,
1186 disorder_eclient_queue_response *completed,
1188 return simple(c, queue_response_opcallback, (void (*)())completed, v,
1189 "recent", (char *)0);
1192 int disorder_eclient_queue(disorder_eclient *c,
1193 disorder_eclient_queue_response *completed,
1195 return simple(c, queue_response_opcallback, (void (*)())completed, v,
1196 "queue", (char *)0);
1199 int disorder_eclient_files(disorder_eclient *c,
1200 disorder_eclient_list_response *completed,
1204 return simple(c, list_response_opcallback, (void (*)())completed, v,
1205 "files", dir, re, (char *)0);
1208 int disorder_eclient_dirs(disorder_eclient *c,
1209 disorder_eclient_list_response *completed,
1213 return simple(c, list_response_opcallback, (void (*)())completed, v,
1214 "dirs", dir, re, (char *)0);
1217 int disorder_eclient_playing(disorder_eclient *c,
1218 disorder_eclient_queue_response *completed,
1220 return simple(c, playing_response_opcallback, (void (*)())completed, v,
1221 "playing", (char *)0);
1224 int disorder_eclient_length(disorder_eclient *c,
1225 disorder_eclient_integer_response *completed,
1228 return simple(c, integer_response_opcallback, (void (*)())completed, v,
1229 "length", track, (char *)0);
1232 int disorder_eclient_volume(disorder_eclient *c,
1233 disorder_eclient_volume_response *completed,
1236 char sl[64], sr[64];
1238 if(l < 0 && r < 0) {
1239 return simple(c, volume_response_opcallback, (void (*)())completed, v,
1240 "volume", (char *)0);
1241 } else if(l >= 0 && r >= 0) {
1244 byte_snprintf(sl, sizeof sl, "%d", l);
1245 byte_snprintf(sr, sizeof sr, "%d", r);
1246 return simple(c, volume_response_opcallback, (void (*)())completed, v,
1247 "volume", sl, sr, (char *)0);
1249 assert(!"invalid arguments to disorder_eclient_volume");
1250 return -1; /* gcc is being dim */
1254 int disorder_eclient_enable(disorder_eclient *c,
1255 disorder_eclient_no_response *completed,
1257 return simple(c, no_response_opcallback, (void (*)())completed, v,
1258 "enable", (char *)0);
1261 int disorder_eclient_disable(disorder_eclient *c,
1262 disorder_eclient_no_response *completed,
1264 return simple(c, no_response_opcallback, (void (*)())completed, v,
1265 "disable", (char *)0);
1268 int disorder_eclient_random_enable(disorder_eclient *c,
1269 disorder_eclient_no_response *completed,
1271 return simple(c, no_response_opcallback, (void (*)())completed, v,
1272 "random-enable", (char *)0);
1275 int disorder_eclient_random_disable(disorder_eclient *c,
1276 disorder_eclient_no_response *completed,
1278 return simple(c, no_response_opcallback, (void (*)())completed, v,
1279 "random-disable", (char *)0);
1282 int disorder_eclient_get(disorder_eclient *c,
1283 disorder_eclient_string_response *completed,
1284 const char *track, const char *pref,
1286 return simple(c, string_response_opcallback, (void (*)())completed, v,
1287 "get", track, pref, (char *)0);
1290 int disorder_eclient_set(disorder_eclient *c,
1291 disorder_eclient_no_response *completed,
1292 const char *track, const char *pref,
1295 return simple(c, no_response_opcallback, (void (*)())completed, v,
1296 "set", track, pref, value, (char *)0);
1299 int disorder_eclient_unset(disorder_eclient *c,
1300 disorder_eclient_no_response *completed,
1301 const char *track, const char *pref,
1303 return simple(c, no_response_opcallback, (void (*)())completed, v,
1304 "unset", track, pref, (char *)0);
1307 int disorder_eclient_resolve(disorder_eclient *c,
1308 disorder_eclient_string_response *completed,
1311 return simple(c, string_response_opcallback, (void (*)())completed, v,
1312 "resolve", track, (char *)0);
1315 int disorder_eclient_search(disorder_eclient *c,
1316 disorder_eclient_list_response *completed,
1319 if(!split(terms, 0, SPLIT_QUOTES, 0, 0)) return -1;
1320 return simple(c, list_response_opcallback, (void (*)())completed, v,
1321 "search", terms, (char *)0);
1324 int disorder_eclient_nop(disorder_eclient *c,
1325 disorder_eclient_no_response *completed,
1327 return simple(c, no_response_opcallback, (void (*)())completed, v,
1331 /** @brief Get the last @p max added tracks
1333 * @param completed Called with list
1334 * @param max Number of tracks to get, 0 for all
1335 * @param v Passed to @p completed
1337 * The first track in the list is the most recently added.
1339 int disorder_eclient_new_tracks(disorder_eclient *c,
1340 disorder_eclient_list_response *completed,
1345 sprintf(limit, "%d", max);
1346 return simple(c, list_response_opcallback, (void (*)())completed, v,
1347 "new", limit, (char *)0);
1350 static void rtp_response_opcallback(disorder_eclient *c,
1351 struct operation *op) {
1352 disorder_eclient_list_response *const completed =
1353 (disorder_eclient_list_response *)op->completed;
1354 D(("rtp_response_opcallback"));
1355 if(c->rc / 100 == 2) {
1357 char **vec = split(c->line + 4, &nvec, SPLIT_QUOTES, 0, 0);
1360 completed(op->v, NULL, nvec, vec);
1362 completed(op->v, "error parsing response", 0, 0);
1364 completed(op->v, errorstring(c), 0, 0);
1367 /** @brief Determine the RTP target address
1369 * @param completed Called with address details
1370 * @param v Passed to @p completed
1372 * The address details will be two elements, the first being the hostname and
1373 * the second the service (port).
1375 int disorder_eclient_rtp_address(disorder_eclient *c,
1376 disorder_eclient_list_response *completed,
1378 return simple(c, rtp_response_opcallback, (void (*)())completed, v,
1379 "rtp-address", (char *)0);
1382 /** @brief Get the list of users
1384 * @param completed Called with list of users
1385 * @param v Passed to @p completed
1387 * The user list is not sorted in any particular order.
1389 int disorder_eclient_users(disorder_eclient *c,
1390 disorder_eclient_list_response *completed,
1392 return simple(c, list_response_opcallback, (void (*)())completed, v,
1393 "users", (char *)0);
1396 /** @brief Delete a user
1398 * @param completed Called on completion
1399 * @param user User to delete
1400 * @param v Passed to @p completed
1402 int disorder_eclient_deluser(disorder_eclient *c,
1403 disorder_eclient_no_response *completed,
1406 return simple(c, no_response_opcallback, (void (*)())completed, v,
1407 "deluser", user, (char *)0);
1410 /** @brief Get a user property
1412 * @param completed Called on completion
1413 * @param user User to look up
1414 * @param property Property to look up
1415 * @param v Passed to @p completed
1417 int disorder_eclient_userinfo(disorder_eclient *c,
1418 disorder_eclient_string_response *completed,
1420 const char *property,
1422 return simple(c, string_response_opcallback, (void (*)())completed, v,
1423 "userinfo", user, property, (char *)0);
1426 /** @brief Modify a user property
1428 * @param completed Called on completion
1429 * @param user User to modify
1430 * @param property Property to modify
1431 * @param value New property value
1432 * @param v Passed to @p completed
1434 int disorder_eclient_edituser(disorder_eclient *c,
1435 disorder_eclient_no_response *completed,
1437 const char *property,
1440 return simple(c, no_response_opcallback, (void (*)())completed, v,
1441 "edituser", user, property, value, (char *)0);
1444 /** @brief Create a new user
1446 * @param completed Called on completion
1447 * @param user User to create
1448 * @param password Initial password
1449 * @param rights Initial rights or NULL
1450 * @param v Passed to @p completed
1452 int disorder_eclient_adduser(disorder_eclient *c,
1453 disorder_eclient_no_response *completed,
1455 const char *password,
1458 return simple(c, no_response_opcallback, (void (*)())completed, v,
1459 "adduser", user, password, rights, (char *)0);
1462 /** @brief Get the list of playlists
1464 * @param completed Called with list of playlists
1465 * @param v Passed to @p completed
1467 * The playlist list is not sorted in any particular order.
1469 int disorder_eclient_playlists(disorder_eclient *c,
1470 disorder_eclient_list_response *completed,
1472 return simple(c, list_response_opcallback, (void (*)())completed, v,
1473 "playlists", (char *)0);
1476 /** @brief Delete a playlist
1478 * @param completed Called on completion
1479 * @param playlist Playlist to delete
1480 * @param v Passed to @p completed
1482 int disorder_eclient_playlist_delete(disorder_eclient *c,
1483 disorder_eclient_no_response *completed,
1484 const char *playlist,
1486 return simple(c, no_response_opcallback, (void (*)())completed, v,
1487 "playlist-delete", playlist, (char *)0);
1490 /** @brief Lock a playlist
1492 * @param completed Called on completion
1493 * @param playlist Playlist to lock
1494 * @param v Passed to @p completed
1496 int disorder_eclient_playlist_lock(disorder_eclient *c,
1497 disorder_eclient_no_response *completed,
1498 const char *playlist,
1500 return simple(c, no_response_opcallback, (void (*)())completed, v,
1501 "playlist-lock", playlist, (char *)0);
1504 /** @brief Unlock the locked a playlist
1506 * @param completed Called on completion
1507 * @param v Passed to @p completed
1509 int disorder_eclient_playlist_unlock(disorder_eclient *c,
1510 disorder_eclient_no_response *completed,
1512 return simple(c, no_response_opcallback, (void (*)())completed, v,
1513 "playlist-unlock", (char *)0);
1516 /** @brief Set a playlist's sharing
1518 * @param completed Called on completion
1519 * @param playlist Playlist to modify
1520 * @param sharing @c "public" or @c "private"
1521 * @param v Passed to @p completed
1523 int disorder_eclient_playlist_set_share(disorder_eclient *c,
1524 disorder_eclient_no_response *completed,
1525 const char *playlist,
1526 const char *sharing,
1528 return simple(c, no_response_opcallback, (void (*)())completed, v,
1529 "playlist-set-share", playlist, sharing, (char *)0);
1532 /** @brief Get a playlist's sharing
1534 * @param completed Called with sharing status
1535 * @param playlist Playlist to inspect
1536 * @param v Passed to @p completed
1538 int disorder_eclient_playlist_get_share(disorder_eclient *c,
1539 disorder_eclient_string_response *completed,
1540 const char *playlist,
1542 return simple(c, string_response_opcallback, (void (*)())completed, v,
1543 "playlist-get-share", playlist, (char *)0);
1546 /** @brief Set a playlist
1548 * @param completed Called on completion
1549 * @param playlist Playlist to modify
1550 * @param tracks List of tracks
1551 * @param ntracks Number of tracks
1552 * @param v Passed to @p completed
1554 int disorder_eclient_playlist_set(disorder_eclient *c,
1555 disorder_eclient_no_response *completed,
1556 const char *playlist,
1560 return simple_body(c, no_response_opcallback, (void (*)())completed, v,
1562 "playlist-set", playlist, (char *)0);
1565 /** @brief Get a playlist's contents
1567 * @param completed Called with playlist contents
1568 * @param playlist Playlist to inspect
1569 * @param v Passed to @p completed
1571 int disorder_eclient_playlist_get(disorder_eclient *c,
1572 disorder_eclient_list_response *completed,
1573 const char *playlist,
1575 return simple(c, list_response_opcallback, (void (*)())completed, v,
1576 "playlist-get", playlist, (char *)0);
1579 /* Log clients ***************************************************************/
1581 /** @brief Monitor the server log
1583 * @param callbacks Functions to call when anything happens
1584 * @param v Passed to @p callbacks functions
1586 * Once a client is being used for logging it cannot be used for anything else.
1587 * There is magic in authuser_opcallback() to re-submit the @c log command
1588 * after reconnection.
1590 * NB that the @c state callback may be called from within this function,
1591 * i.e. not solely later on from the event loop callback.
1593 int disorder_eclient_log(disorder_eclient *c,
1594 const disorder_eclient_log_callbacks *callbacks,
1596 if(c->log_callbacks) return -1;
1597 c->log_callbacks = callbacks;
1599 /* Repoort initial state */
1600 if(c->log_callbacks->state)
1601 c->log_callbacks->state(c->log_v, c->statebits);
1602 stash_command(c, 0/*queuejump*/, log_opcallback, 0/*completed*/, v,
1603 -1, 0, "log", (char *)0);
1604 disorder_eclient_polled(c, 0);
1608 /* If we get here we've stopped being a log client */
1609 static void log_opcallback(disorder_eclient *c,
1610 struct operation attribute((unused)) *op) {
1611 D(("log_opcallback"));
1612 c->log_callbacks = 0;
1616 /* error callback for log line parsing */
1617 static void logline_error(const char *msg, void *u) {
1618 disorder_eclient *c = u;
1619 /* TODO don't use protocol_error here */
1620 protocol_error(c, c->ops, -1, "error parsing log line: %s", msg);
1623 /* process a single log line */
1624 static void logline(disorder_eclient *c, const char *line) {
1629 D(("logline [%s]", line));
1630 vec = split(line, &nvec, SPLIT_QUOTES, logline_error, c);
1631 if(nvec < 2) return; /* probably an error, already
1633 if(sscanf(vec[0], "%"SCNxMAX, &when) != 1) {
1634 /* probably the wrong side of a format change */
1635 /* TODO don't use protocol_error here */
1636 protocol_error(c, c->ops, -1, "invalid log timestamp '%s'", vec[0]);
1639 /* TODO: do something with the time */
1640 //fprintf(stderr, "log key: %s\n", vec[1]);
1641 n = TABLE_FIND(logentry_handlers, name, vec[1]);
1643 //fprintf(stderr, "...not found\n");
1644 return; /* probably a future command */
1648 if(nvec < logentry_handlers[n].min || nvec > logentry_handlers[n].max) {
1649 //fprintf(stderr, "...wrong # args\n");
1652 logentry_handlers[n].handler(c, nvec, vec);
1655 static void logentry_completed(disorder_eclient *c,
1656 int attribute((unused)) nvec, char **vec) {
1657 c->statebits &= ~DISORDER_PLAYING;
1658 if(c->log_callbacks->completed)
1659 c->log_callbacks->completed(c->log_v, vec[0]);
1660 if(c->log_callbacks->state)
1661 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
1664 static void logentry_failed(disorder_eclient *c,
1665 int attribute((unused)) nvec, char **vec) {
1666 c->statebits &= ~DISORDER_PLAYING;
1667 if(c->log_callbacks->failed)
1668 c->log_callbacks->failed(c->log_v, vec[0], vec[1]);
1669 if(c->log_callbacks->state)
1670 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
1673 static void logentry_moved(disorder_eclient *c,
1674 int attribute((unused)) nvec, char **vec) {
1675 if(c->log_callbacks->moved)
1676 c->log_callbacks->moved(c->log_v, vec[0]);
1679 static void logentry_playing(disorder_eclient *c,
1680 int attribute((unused)) nvec, char **vec) {
1681 c->statebits |= DISORDER_PLAYING;
1682 if(c->log_callbacks->playing)
1683 c->log_callbacks->playing(c->log_v, vec[0], vec[1]);
1684 if(c->log_callbacks->state)
1685 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
1688 static void logentry_queue(disorder_eclient *c,
1689 int attribute((unused)) nvec, char **vec) {
1690 struct queue_entry *q;
1692 if(!c->log_callbacks->queue) return;
1693 q = xmalloc(sizeof *q);
1694 if(queue_unmarshall_vec(q, nvec, vec, eclient_queue_error, c))
1696 c->log_callbacks->queue(c->log_v, q);
1699 static void logentry_recent_added(disorder_eclient *c,
1700 int attribute((unused)) nvec, char **vec) {
1701 struct queue_entry *q;
1703 if(!c->log_callbacks->recent_added) return;
1704 q = xmalloc(sizeof *q);
1705 if(queue_unmarshall_vec(q, nvec, vec, eclient_queue_error, c))
1707 c->log_callbacks->recent_added(c->log_v, q);
1710 static void logentry_recent_removed(disorder_eclient *c,
1711 int attribute((unused)) nvec, char **vec) {
1712 if(c->log_callbacks->recent_removed)
1713 c->log_callbacks->recent_removed(c->log_v, vec[0]);
1716 static void logentry_removed(disorder_eclient *c,
1717 int attribute((unused)) nvec, char **vec) {
1718 if(c->log_callbacks->removed)
1719 c->log_callbacks->removed(c->log_v, vec[0], vec[1]);
1722 static void logentry_rescanned(disorder_eclient *c,
1723 int attribute((unused)) nvec,
1724 char attribute((unused)) **vec) {
1725 if(c->log_callbacks->rescanned)
1726 c->log_callbacks->rescanned(c->log_v);
1729 static void logentry_scratched(disorder_eclient *c,
1730 int attribute((unused)) nvec, char **vec) {
1731 c->statebits &= ~DISORDER_PLAYING;
1732 if(c->log_callbacks->scratched)
1733 c->log_callbacks->scratched(c->log_v, vec[0], vec[1]);
1734 if(c->log_callbacks->state)
1735 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
1738 static void logentry_user_add(disorder_eclient *c,
1739 int attribute((unused)) nvec, char **vec) {
1740 if(c->log_callbacks->user_add)
1741 c->log_callbacks->user_add(c->log_v, vec[0]);
1744 static void logentry_user_confirm(disorder_eclient *c,
1745 int attribute((unused)) nvec, char **vec) {
1746 if(c->log_callbacks->user_confirm)
1747 c->log_callbacks->user_confirm(c->log_v, vec[0]);
1750 static void logentry_user_delete(disorder_eclient *c,
1751 int attribute((unused)) nvec, char **vec) {
1752 if(c->log_callbacks->user_delete)
1753 c->log_callbacks->user_delete(c->log_v, vec[0]);
1756 static void logentry_user_edit(disorder_eclient *c,
1757 int attribute((unused)) nvec, char **vec) {
1758 if(c->log_callbacks->user_edit)
1759 c->log_callbacks->user_edit(c->log_v, vec[0], vec[1]);
1762 static void logentry_rights_changed(disorder_eclient *c,
1763 int attribute((unused)) nvec, char **vec) {
1764 if(c->log_callbacks->rights_changed) {
1766 if(!parse_rights(vec[0], &r, 0/*report*/))
1767 c->log_callbacks->rights_changed(c->log_v, r);
1771 static const struct {
1774 const char *disable;
1775 } statestrings[] = {
1776 { DISORDER_PLAYING_ENABLED, "enable_play", "disable_play" },
1777 { DISORDER_RANDOM_ENABLED, "enable_random", "disable_random" },
1778 { DISORDER_TRACK_PAUSED, "pause", "resume" },
1779 { DISORDER_PLAYING, "playing", "completed" },
1780 { DISORDER_PLAYING, 0, "scratched" },
1781 { DISORDER_PLAYING, 0, "failed" },
1783 #define NSTATES (int)(sizeof statestrings / sizeof *statestrings)
1785 static void logentry_state(disorder_eclient *c,
1786 int attribute((unused)) nvec, char **vec) {
1789 for(n = 0; n < NSTATES; ++n)
1790 if(statestrings[n].enable && !strcmp(vec[0], statestrings[n].enable)) {
1791 c->statebits |= statestrings[n].bit;
1793 } else if(statestrings[n].disable && !strcmp(vec[0], statestrings[n].disable)) {
1794 c->statebits &= ~statestrings[n].bit;
1797 if(c->log_callbacks->state)
1798 c->log_callbacks->state(c->log_v, c->statebits | DISORDER_CONNECTED);
1801 static void logentry_volume(disorder_eclient *c,
1802 int attribute((unused)) nvec, char **vec) {
1805 if(!c->log_callbacks->volume) return;
1806 if(xstrtol(&l, vec[0], 0, 10)
1807 || xstrtol(&r, vec[1], 0, 10)
1808 || l < 0 || l > INT_MAX
1809 || r < 0 || r > INT_MAX)
1811 c->log_callbacks->volume(c->log_v, (int)l, (int)r);
1814 /** @brief Convert @p statebits to a string */
1815 char *disorder_eclient_interpret_state(unsigned long statebits) {
1819 static const struct {
1823 { DISORDER_PLAYING_ENABLED, "playing_enabled" },
1824 { DISORDER_RANDOM_ENABLED, "random_enabled" },
1825 { DISORDER_TRACK_PAUSED, "track_paused" },
1826 { DISORDER_PLAYING, "playing" },
1827 { DISORDER_CONNECTED, "connected" },
1829 #define NBITS (sizeof bits / sizeof *bits)
1833 dynstr_append(d, '0');
1834 for(n = 0; n < NBITS; ++n)
1835 if(statebits & bits[n].bit) {
1837 dynstr_append(d, '|');
1838 dynstr_append_string(d, bits[n].name);
1839 statebits ^= bits[n].bit;
1845 dynstr_append(d, '|');
1846 sprintf(s, "%#lx", statebits);
1847 dynstr_append_string(d, s);
1849 dynstr_terminate(d);
1858 indent-tabs-mode:nil