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 3 of the License, or
8 * (at your option) any later version.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 /** @file lib/eclient.c
19 * @brief Client code for event-driven programs
24 #include <sys/types.h>
25 #include <sys/socket.h>
26 #include <netinet/in.h>
37 #include "configuration.h"
44 #include "inputline.h"
51 #include "client-common.h"
53 /* TODO: more commands */
55 /** @brief How often to send data to the server when receiving logs */
56 #define LOG_PROD_INTERVAL 10
58 /* Types *********************************************************************/
60 /** @brief Client state */
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 */
71 /** @brief Names for @ref client_state */
72 static const char *const states
[] = {
82 struct operation
; /* forward decl */
84 /** @brief Type of an operation callback */
85 typedef void operation_callback(disorder_eclient
*c
, struct operation
*op
);
87 /** @brief A pending operation.
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. */
93 struct operation
*next
; /**< @brief next operation */
94 char *cmd
; /**< @brief command to send or 0 */
95 operation_callback
*opcallback
; /**< @brief internal completion callback */
96 void (*completed
)(); /**< @brief user completion callback or 0 */
97 void *v
; /**< @brief data for COMPLETED */
98 disorder_eclient
*client
; /**< @brief owning client */
100 /** @brief true if sent to server
102 * This is cleared by disorder_eclient_close(), forcing all queued
103 * commands to be transparently resent.
108 /** @brief Client structure */
109 struct disorder_eclient
{
111 int fd
; /**< @brief connection to server */
112 enum client_state state
; /**< @brief current state */
113 int authenticated
; /**< @brief true when authenicated */
114 struct dynstr output
; /**< @brief output buffer */
115 struct dynstr input
; /**< @brief input buffer */
116 int eof
; /**< @brief input buffer is at EOF */
117 const disorder_eclient_callbacks
*callbacks
; /**< @brief error callbacks */
118 void *u
; /**< @brief user data */
119 struct operation
*ops
; /**< @brief queue of operations */
120 struct operation
**opstail
; /**< @brief queue tail */
121 /* accumulated response */
122 int rc
; /**< @brief response code */
123 char *line
; /**< @brief complete line */
124 struct vector vec
; /**< @brief body */
126 const disorder_eclient_log_callbacks
*log_callbacks
;
127 /**< @brief log callbacks
129 * Once disorder_eclient_log() has been issued this is always set. When we
130 * re-connect it is checked to re-issue the log command.
132 void *log_v
; /**< @brief user data */
133 unsigned long statebits
; /**< @brief latest state */
136 /**< @brief last time we sent a prod
138 * When we are receiving log data we send a "prod" byte to the server from
139 * time to time so that we detect broken connections reasonably quickly. The
140 * server just ignores these bytes.
143 /** @brief Protocol version */
146 /** @brief True if enabled */
150 /* Forward declarations ******************************************************/
152 static int start_connect(disorder_eclient
*c
);
153 static void process_line(disorder_eclient
*c
, char *line
);
154 static void maybe_connected(disorder_eclient
*c
);
155 static void authbanner_opcallback(disorder_eclient
*c
,
156 struct operation
*op
);
157 static void authuser_opcallback(disorder_eclient
*c
,
158 struct operation
*op
);
159 static void complete(disorder_eclient
*c
);
160 static void send_output(disorder_eclient
*c
);
161 static void put(disorder_eclient
*c
, const char *s
, size_t n
);
162 static void read_input(disorder_eclient
*c
);
163 static void stash_command(disorder_eclient
*c
,
165 operation_callback
*opcallback
,
170 static void log_opcallback(disorder_eclient
*c
, struct operation
*op
);
171 static void logline(disorder_eclient
*c
, const char *line
);
172 static void logentry_completed(disorder_eclient
*c
, int nvec
, char **vec
);
173 static void logentry_failed(disorder_eclient
*c
, int nvec
, char **vec
);
174 static void logentry_moved(disorder_eclient
*c
, int nvec
, char **vec
);
175 static void logentry_playing(disorder_eclient
*c
, int nvec
, char **vec
);
176 static void logentry_queue(disorder_eclient
*c
, int nvec
, char **vec
);
177 static void logentry_recent_added(disorder_eclient
*c
, int nvec
, char **vec
);
178 static void logentry_recent_removed(disorder_eclient
*c
, int nvec
, char **vec
);
179 static void logentry_removed(disorder_eclient
*c
, int nvec
, char **vec
);
180 static void logentry_scratched(disorder_eclient
*c
, int nvec
, char **vec
);
181 static void logentry_state(disorder_eclient
*c
, int nvec
, char **vec
);
182 static void logentry_volume(disorder_eclient
*c
, int nvec
, char **vec
);
183 static void logentry_rescanned(disorder_eclient
*c
, int nvec
, char **vec
);
184 static void logentry_user_add(disorder_eclient
*c
, int nvec
, char **vec
);
185 static void logentry_user_confirm(disorder_eclient
*c
, int nvec
, char **vec
);
186 static void logentry_user_delete(disorder_eclient
*c
, int nvec
, char **vec
);
187 static void logentry_user_edit(disorder_eclient
*c
, int nvec
, char **vec
);
188 static void logentry_rights_changed(disorder_eclient
*c
, int nvec
, char **vec
);
189 static void logentry_adopted(disorder_eclient
*c
, int nvec
, char **vec
);
191 /* Tables ********************************************************************/
193 /** @brief One possible log entry */
194 struct logentry_handler
{
195 const char *name
; /**< @brief Entry name */
196 int min
; /**< @brief Minimum arguments */
197 int max
; /**< @brief Maximum arguments */
198 void (*handler
)(disorder_eclient
*c
,
200 char **vec
); /**< @brief Handler function */
203 /** @brief Table for parsing log entries */
204 static const struct logentry_handler logentry_handlers
[] = {
205 #define LE(X, MIN, MAX) { #X, MIN, MAX, logentry_##X }
211 LE(queue
, 2, INT_MAX
),
212 LE(recent_added
, 2, INT_MAX
),
213 LE(recent_removed
, 1, 1),
216 LE(rights_changed
, 1, 1),
220 LE(user_confirm
, 1, 1),
221 LE(user_delete
, 1, 1),
226 /* Setup and teardown ********************************************************/
228 /** @brief Create a new client
230 * Does NOT connect the client - connections are made (and re-made) on demand.
232 disorder_eclient
*disorder_eclient_new(const disorder_eclient_callbacks
*cb
,
234 disorder_eclient
*c
= xmalloc(sizeof *c
);
235 D(("disorder_eclient_new"));
239 c
->opstail
= &c
->ops
;
241 vector_init(&c
->vec
);
242 dynstr_init(&c
->input
);
243 dynstr_init(&c
->output
);
247 /** @brief Disconnect a client
248 * @param c Client to disconnect
250 * NB that this routine just disconnnects the TCP connection. It does not
251 * destroy the client! If you continue to use it then it will attempt to
254 void disorder_eclient_close(disorder_eclient
*c
) {
255 struct operation
*op
;
257 D(("disorder_eclient_close"));
259 D(("disorder_eclient_close closing fd %d", c
->fd
));
260 c
->callbacks
->poll(c
->u
, c
, c
->fd
, 0);
263 c
->state
= state_disconnected
;
269 c
->authenticated
= 0;
270 /* We'll need to resend all operations */
271 for(op
= c
->ops
; op
; op
= op
->next
)
273 /* Drop our use a hint that we're disconnected */
274 if(c
->log_callbacks
&& c
->log_callbacks
->state
)
275 c
->log_callbacks
->state(c
->log_v
, c
->statebits
);
278 /** @brief Permit new connection activity */
279 void disorder_eclient_enable_connect(disorder_eclient
*c
) {
283 /** @brief Suppress new connection activity */
284 void disorder_eclient_disable_connect(disorder_eclient
*c
) {
288 /** @brief Return current state */
289 unsigned long disorder_eclient_state(const disorder_eclient
*c
) {
290 return c
->statebits
| (c
->state
> state_connected ? DISORDER_CONNECTED
: 0);
293 /* Error reporting ***********************************************************/
295 /** @brief called when a connection error occurs
297 * After this called we will be disconnected (by disorder_eclient_close()),
298 * so there will be a reconnection before any commands can be sent.
300 static int comms_error(disorder_eclient
*c
, const char *fmt
, ...) {
306 byte_xvasprintf(&s
, fmt
, ap
);
308 disorder_eclient_close(c
);
309 c
->callbacks
->comms_error(c
->u
, s
);
313 /** @brief called when the server reports an error */
314 static int protocol_error(disorder_eclient
*c
, struct operation
*op
,
315 int code
, const char *fmt
, ...) {
319 D(("protocol_error"));
321 byte_xvasprintf(&s
, fmt
, ap
);
323 c
->callbacks
->protocol_error(c
->u
, op
->v
, code
, s
);
327 /* State machine *************************************************************/
329 /** @brief Called when there's something to do
331 * @param mode bitmap of @ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE.
333 * This should be called from by your code when the file descriptor is readable
334 * or writable (as requested by the @c poll callback, see @ref
335 * disorder_eclient_callbacks) and in any case from time to time (with @p mode
336 * = 0) to allow for retries to work.
338 void disorder_eclient_polled(disorder_eclient
*c
, unsigned mode
) {
339 struct operation
*op
;
342 D(("disorder_eclient_polled fd=%d state=%s mode=[%s %s]",
343 c
->fd
, states
[c
->state
],
344 mode
& DISORDER_POLL_READ ?
"READ" : "",
345 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
346 /* The pattern here is to check each possible state in turn and try to
347 * advance (though on error we might go back). If we advance we leave open
348 * the possibility of falling through to the next state, but we set the mode
349 * bits to 0, to avoid false positives (which matter more in some cases than
352 if(c
->state
== state_disconnected
) {
353 D(("state_disconnected"));
354 /* If there is no password yet then we cannot connect */
355 if(!config
->password
) {
356 comms_error(c
, "no password is configured");
360 /* Only try to connect if enabled */
363 /* might now be state_disconnected (on error), state_connecting (slow
364 * connect) or state_connected (fast connect). If state_disconnected then
365 * we just rely on a periodic callback from the event loop sometime. */
369 if(c
->state
== state_connecting
&& mode
) {
370 D(("state_connecting"));
372 /* Might be state_disconnected (on error) or state_connected (on success).
373 * In the former case we rely on the event loop for a periodic callback to
378 if(c
->state
== state_connected
) {
379 D(("state_connected"));
380 /* We just connected. Initiate the authentication protocol. */
381 stash_command(c
, 1/*queuejump*/, authbanner_opcallback
,
382 0/*completed*/, 0/*v*/, 0/*cmd*/);
383 /* We never stay is state_connected very long. We could in principle jump
384 * straight to state_cmdresponse since there's actually no command to
385 * send, but that would arguably be cheating. */
386 c
->state
= state_idle
;
389 if(c
->state
== state_idle
) {
391 /* We are connected, and have finished any command we set off, look for
395 if(c
->authenticated
) {
396 /* Transmit all unsent operations */
397 for(op
= c
->ops
; op
; op
= op
->next
) {
399 put(c
, op
->cmd
, strlen(op
->cmd
));
404 /* Just send the head operation */
405 if(c
->ops
->cmd
&& !c
->ops
->sent
) {
406 put(c
, c
->ops
->cmd
, strlen(c
->ops
->cmd
));
410 /* Awaiting response for the operation at the head of the list */
411 c
->state
= state_cmdresponse
;
414 c
->callbacks
->report(c
->u
, 0);
417 /* Queue up a byte to send */
418 if(c
->state
== state_log
419 && c
->output
.nvec
== 0
420 && time(&now
) - c
->last_prod
> LOG_PROD_INTERVAL
) {
425 if(c
->state
== state_cmdresponse
426 || c
->state
== state_body
427 || c
->state
== state_log
) {
428 D(("state_%s", states
[c
->state
]));
429 /* We are awaiting a response */
430 if(mode
& DISORDER_POLL_WRITE
) send_output(c
);
431 if(mode
& DISORDER_POLL_READ
) read_input(c
);
432 /* There are a couple of reasons we might want to re-enter the state
433 * machine from the top. state_idle is obvious: there may be further
434 * commands to process. Re-entering on state_disconnected means that we
435 * immediately retry connection if a comms error occurs during a command.
436 * This is different to the case where a connection fails, where we await a
437 * spontaneous call to initiate the retry. */
439 case state_disconnected
: /* lost connection */
440 case state_idle
: /* completed a command */
442 disorder_eclient_polled(c
, 0);
449 /* Figure out what to set the mode to */
451 case state_disconnected
:
452 D(("state_disconnected (2)"));
453 /* Probably an error occurred. Await a retry. */
456 case state_connecting
:
457 D(("state_connecting (2)"));
458 /* Waiting for connect to complete */
459 mode
= DISORDER_POLL_READ
|DISORDER_POLL_WRITE
;
461 case state_connected
:
462 D(("state_connected (2)"));
463 assert(!"should never be in state_connected here");
466 D(("state_idle (2)"));
467 /* Connected but nothing to do. */
470 case state_cmdresponse
:
473 D(("state_%s (2)", states
[c
->state
]));
474 /* Gathering a response. Wait for input. */
475 mode
= DISORDER_POLL_READ
;
476 /* Flush any pending output. */
477 if(c
->output
.nvec
) mode
|= DISORDER_POLL_WRITE
;
480 D(("fd=%d new mode [%s %s]",
482 mode
& DISORDER_POLL_READ ?
"READ" : "",
483 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
484 if(c
->fd
!= -1) c
->callbacks
->poll(c
->u
, c
, c
->fd
, mode
);
487 /** @brief Called to start connecting */
488 static int start_connect(disorder_eclient
*c
) {
492 D(("start_connect"));
493 if((len
= find_server(config
, &sa
, &c
->ident
)) == (socklen_t
)-1)
494 return comms_error(c
, "cannot look up server"); /* TODO better error */
499 if((c
->fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0)
500 return comms_error(c
, "socket: %s", strerror(errno
));
504 if(connect(c
->fd
, sa
, len
) < 0) {
508 c
->state
= state_connecting
;
509 /* We are called from _polled so the state machine will get to do its
513 /* Signal the error to the caller. */
514 return comms_error(c
, "connecting to %s: %s", c
->ident
, strerror(errno
));
517 c
->state
= state_connected
;
521 /** @brief Called when poll triggers while waiting for a connection */
522 static void maybe_connected(disorder_eclient
*c
) {
523 /* We either connected, or got an error. */
525 socklen_t len
= sizeof err
;
527 D(("maybe_connected"));
528 /* Work around over-enthusiastic error slippage */
529 if(getsockopt(c
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
) < 0)
532 /* The connection failed */
533 comms_error(c
, "connecting to %s: %s", c
->ident
, strerror(err
));
534 /* sets state_disconnected */
538 /* The connection succeeded */
539 c
->state
= state_connected
;
540 byte_xasprintf(&r
, "connected to %s", c
->ident
);
541 c
->callbacks
->report(c
->u
, r
);
542 /* If this is a log client we expect to get a bunch of updates from the
543 * server straight away */
547 /* Authentication ************************************************************/
549 /** @brief Called with the greeting from the server */
550 static void authbanner_opcallback(disorder_eclient
*c
,
551 struct operation
*op
) {
553 const unsigned char *nonce
;
557 const char *protocol
, *algorithm
, *challenge
;
559 D(("authbanner_opcallback"));
561 || !(rvec
= split(c
->line
+ 4, &nrvec
, SPLIT_QUOTES
, 0, 0))
563 /* Banner told us to go away, or was malformed. We cannot proceed. */
564 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
565 disorder_eclient_close(c
);
585 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
586 disorder_eclient_close(c
);
589 c
->protocol
= atoi(protocol
);
590 if(c
->protocol
< 1 || c
->protocol
> 2) {
591 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
592 disorder_eclient_close(c
);
595 nonce
= unhex(challenge
, &nonce_len
);
596 res
= authhash(nonce
, nonce_len
, config
->password
, algorithm
);
598 protocol_error(c
, op
, c
->rc
, "%s: unknown authentication algorithm '%s'",
599 c
->ident
, algorithm
);
600 disorder_eclient_close(c
);
603 stash_command(c
, 1/*queuejump*/, authuser_opcallback
, 0/*completed*/, 0/*v*/,
604 "user", quoteutf8(config
->username
), quoteutf8(res
),
608 /** @brief Called with the response to the @c user command */
609 static void authuser_opcallback(disorder_eclient
*c
,
610 struct operation
*op
) {
613 D(("authuser_opcallback"));
614 if(c
->rc
/ 100 != 2) {
615 /* Wrong password or something. We cannot proceed. */
616 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
618 disorder_eclient_close(c
);
621 /* OK, we're authenticated now. */
622 c
->authenticated
= 1;
623 byte_xasprintf(&r
, "authenticated with %s", c
->ident
);
624 c
->callbacks
->report(c
->u
, r
);
625 if(c
->log_callbacks
&& !(c
->ops
&& c
->ops
->opcallback
== log_opcallback
))
626 /* We are a log client, switch to logging mode */
627 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, c
->log_v
,
631 /* Output ********************************************************************/
633 /* Chop N bytes off the front of a dynstr */
634 static void consume(struct dynstr
*d
, int n
) {
635 D(("consume %d", n
));
636 assert(d
->nvec
>= n
);
637 memmove(d
->vec
, d
->vec
+ n
, d
->nvec
- n
);
641 /* Write some bytes */
642 static void put(disorder_eclient
*c
, const char *s
, size_t n
) {
643 D(("put %d %.*s", c
->fd
, (int)n
, s
));
644 dynstr_append_bytes(&c
->output
, s
, n
);
647 /* Called when we can write to our FD, or at any other time */
648 static void send_output(disorder_eclient
*c
) {
651 D(("send_output %d bytes pending", c
->output
.nvec
));
652 if(c
->state
> state_connecting
&& c
->output
.nvec
) {
653 n
= write(c
->fd
, c
->output
.vec
, c
->output
.nvec
);
660 comms_error(c
, "writing to %s: %s", c
->ident
, strerror(errno
));
664 consume(&c
->output
, n
);
668 /* Input *********************************************************************/
670 /* Called when c->fd might be readable, or at any other time */
671 static void read_input(disorder_eclient
*c
) {
676 D(("read_input in state %s", states
[c
->state
]));
677 if(c
->state
<= state_connected
) return; /* ignore bogus calls */
678 /* read some more input */
679 n
= read(c
->fd
, buffer
, sizeof buffer
);
686 comms_error(c
, "reading from %s: %s", c
->ident
, strerror(errno
));
689 return; /* no new input to process */
691 D(("read %d bytes: [%.*s]", n
, n
, buffer
));
692 dynstr_append_bytes(&c
->input
, buffer
, n
);
695 /* might have more than one line to process */
696 while(c
->state
> state_connecting
697 && (nl
= memchr(c
->input
.vec
, '\n', c
->input
.nvec
))) {
698 process_line(c
, xstrndup(c
->input
.vec
, nl
- c
->input
.vec
));
699 /* we might have disconnected along the way, which zogs the input buffer */
700 if(c
->state
> state_connecting
)
701 consume(&c
->input
, (nl
- c
->input
.vec
) + 1);
704 comms_error(c
, "reading from %s: server disconnected", c
->ident
);
705 c
->authenticated
= 0;
709 /* called with a line that has just been read */
710 static void process_line(disorder_eclient
*c
, char *line
) {
711 D(("process_line %d [%s]", c
->fd
, line
));
713 case state_cmdresponse
:
714 /* This is the first line of a response */
715 if(!(line
[0] >= '0' && line
[0] <= '9'
716 && line
[1] >= '0' && line
[1] <= '9'
717 && line
[2] >= '0' && line
[2] <= '9'
719 fatal(0, "invalid response from server: %s", line
);
720 c
->rc
= (line
[0] * 10 + line
[1]) * 10 + line
[2] - 111 * '0';
724 /* We need to collect the body. */
725 c
->state
= state_body
;
726 vector_init(&c
->vec
);
729 assert(c
->log_callbacks
!= 0);
730 if(c
->log_callbacks
->connected
)
731 c
->log_callbacks
->connected(c
->log_v
);
732 c
->state
= state_log
;
735 /* We've got the whole response. Go into the idle state so the state
736 * machine knows we're done and then call the operation callback. */
742 if(strcmp(line
, ".")) {
743 /* A line from the body */
744 vector_append(&c
->vec
, line
+ (line
[0] == '.'));
746 /* End of the body. */
747 vector_terminate(&c
->vec
);
752 if(strcmp(line
, ".")) {
753 logline(c
, line
+ (line
[0] == '.'));
758 assert(!"wrong state for location");
763 /* Called when an operation completes */
764 static void complete(disorder_eclient
*c
) {
765 struct operation
*op
;
768 /* Pop the operation off the queue */
771 if(c
->opstail
== &op
->next
)
772 c
->opstail
= &c
->ops
;
773 /* If we've pipelined a command ahead then we go straight to cmdresponser.
774 * Otherwise we go to idle, which will arrange further sends. */
775 c
->state
= c
->ops
&& c
->ops
->sent ? state_cmdresponse
: state_idle
;
776 op
->opcallback(c
, op
);
777 /* Note that we always call the opcallback even on error, though command
778 * opcallbacks generally always do the same error handling, i.e. just call
779 * protocol_error(). It's the auth* opcallbacks that have different
783 /* Operation setup ***********************************************************/
785 static void stash_command_vector(disorder_eclient
*c
,
787 operation_callback
*opcallback
,
792 struct operation
*op
= xmalloc(sizeof *op
);
798 for(n
= 0; n
< ncmd
; ++n
) {
800 dynstr_append(&d
, ' ');
801 dynstr_append_string(&d
, quoteutf8(cmd
[n
]));
803 dynstr_append(&d
, '\n');
804 dynstr_terminate(&d
);
807 op
->cmd
= 0; /* usually, awaiting challenge */
808 op
->opcallback
= opcallback
;
809 op
->completed
= completed
;
813 assert(op
->sent
== 0);
815 /* Authentication operations jump the queue of useful commands */
818 if(c
->opstail
== &c
->ops
)
819 c
->opstail
= &op
->next
;
820 for(op
= c
->ops
; op
; op
= op
->next
)
824 c
->opstail
= &op
->next
;
828 static void vstash_command(disorder_eclient
*c
,
830 operation_callback
*opcallback
,
833 const char *cmd
, va_list ap
) {
837 D(("vstash_command %s", cmd ? cmd
: "NULL"));
840 vector_append(&vec
, (char *)cmd
);
841 while((arg
= va_arg(ap
, char *)))
842 vector_append(&vec
, arg
);
843 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
,
846 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
, 0, 0);
849 static void stash_command(disorder_eclient
*c
,
851 operation_callback
*opcallback
,
859 vstash_command(c
, queuejump
, opcallback
, completed
, v
, cmd
, ap
);
863 /* Command support ***********************************************************/
865 static const char *errorstring(disorder_eclient
*c
) {
868 byte_xasprintf(&s
, "%s: %s: %d", c
->ident
, c
->line
, c
->rc
);
872 /* for commands with a quoted string response */
873 static void string_response_opcallback(disorder_eclient
*c
,
874 struct operation
*op
) {
875 disorder_eclient_string_response
*completed
876 = (disorder_eclient_string_response
*)op
->completed
;
878 D(("string_response_callback"));
879 if(c
->rc
/ 100 == 2 || c
->rc
== 555) {
882 completed(op
->v
, NULL
, NULL
);
883 else if(c
->protocol
>= 2) {
884 char **rr
= split(c
->line
+ 4, 0, SPLIT_QUOTES
, 0, 0);
887 completed(op
->v
, NULL
, *rr
);
889 /* TODO error message a is bit lame but generally indicates a server
890 * bug rather than anything the user can address */
891 completed(op
->v
, "error parsing response", NULL
);
893 completed(op
->v
, NULL
, c
->line
+ 4);
896 completed(op
->v
, errorstring(c
), NULL
);
899 /* for commands with a simple integer response */
900 static void integer_response_opcallback(disorder_eclient
*c
,
901 struct operation
*op
) {
902 disorder_eclient_integer_response
*completed
903 = (disorder_eclient_integer_response
*)op
->completed
;
905 D(("string_response_callback"));
906 if(c
->rc
/ 100 == 2) {
910 e
= xstrtol(&n
, c
->line
+ 4, 0, 10);
912 completed(op
->v
, strerror(e
), 0);
914 completed(op
->v
, 0, n
);
916 completed(op
->v
, errorstring(c
), 0);
919 /* for commands with no response */
920 static void no_response_opcallback(disorder_eclient
*c
,
921 struct operation
*op
) {
922 disorder_eclient_no_response
*completed
923 = (disorder_eclient_no_response
*)op
->completed
;
925 D(("no_response_callback"));
927 completed(op
->v
, NULL
);
929 completed(op
->v
, errorstring(c
));
932 /* error callback for queue_unmarshall */
933 static void eclient_queue_error(const char *msg
,
935 struct operation
*op
= u
;
937 /* TODO don't use protocol_error here */
938 protocol_error(op
->client
, op
, -1, "error parsing queue entry: %s", msg
);
941 /* for commands that expect a queue dump */
942 static void queue_response_opcallback(disorder_eclient
*c
,
943 struct operation
*op
) {
944 disorder_eclient_queue_response
*const completed
945 = (disorder_eclient_queue_response
*)op
->completed
;
947 int parse_failed
= 0;
948 struct queue_entry
*q
, *qh
= 0, **qtail
= &qh
, *qlast
= 0;
950 D(("queue_response_callback"));
951 if(c
->rc
/ 100 == 2) {
952 /* parse the queue */
953 for(n
= 0; n
< c
->vec
.nvec
; ++n
) {
954 q
= xmalloc(sizeof *q
);
955 D(("queue_unmarshall %s", c
->vec
.vec
[n
]));
956 if(!queue_unmarshall(q
, c
->vec
.vec
[n
], NULL
, op
)) {
964 /* Currently we pass the partial queue to the callback along with the
965 * error. This might not be very useful in practice... */
967 completed(op
->v
, "cannot parse result", qh
);
969 completed(op
->v
, 0, qh
);
971 completed(op
->v
, errorstring(c
), 0);
975 static void playing_response_opcallback(disorder_eclient
*c
,
976 struct operation
*op
) {
977 disorder_eclient_queue_response
*const completed
978 = (disorder_eclient_queue_response
*)op
->completed
;
979 struct queue_entry
*q
;
981 D(("playing_response_callback"));
982 if(c
->rc
/ 100 == 2) {
985 if(queue_unmarshall(q
= xmalloc(sizeof *q
), c
->line
+ 4,
987 completed(op
->v
, "cannot parse result", 0);
989 completed(op
->v
, 0, q
);
992 completed(op
->v
, 0, 0);
995 completed(op
->v
, errorstring(c
), 0);
999 completed(op
->v
, errorstring(c
), 0);
1002 /* for commands that expect a list of some sort */
1003 static void list_response_opcallback(disorder_eclient
*c
,
1004 struct operation
*op
) {
1005 disorder_eclient_list_response
*const completed
=
1006 (disorder_eclient_list_response
*)op
->completed
;
1008 D(("list_response_callback"));
1009 if(c
->rc
/ 100 == 2)
1010 completed(op
->v
, NULL
, c
->vec
.nvec
, c
->vec
.vec
);
1012 completed(op
->v
, errorstring(c
), 0, 0);
1016 static void volume_response_opcallback(disorder_eclient
*c
,
1017 struct operation
*op
) {
1018 disorder_eclient_volume_response
*completed
1019 = (disorder_eclient_volume_response
*)op
->completed
;
1022 D(("volume_response_callback"));
1023 if(c
->rc
/ 100 == 2) {
1025 if(sscanf(c
->line
+ 4, "%d %d", &l
, &r
) != 2 || l
< 0 || r
< 0)
1026 completed(op
->v
, "cannot parse volume response", 0, 0);
1028 completed(op
->v
, 0, l
, r
);
1031 completed(op
->v
, errorstring(c
), 0, 0);
1034 static int simple(disorder_eclient
*c
,
1035 operation_callback
*opcallback
,
1036 void (*completed
)(),
1038 const char *cmd
, ...) {
1042 vstash_command(c
, 0/*queuejump*/, opcallback
, completed
, v
, cmd
, ap
);
1044 /* Give the state machine a kick, since we might be in state_idle */
1045 disorder_eclient_polled(c
, 0);
1049 /* Commands ******************************************************************/
1051 int disorder_eclient_version(disorder_eclient
*c
,
1052 disorder_eclient_string_response
*completed
,
1054 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1055 "version", (char *)0);
1058 int disorder_eclient_namepart(disorder_eclient
*c
,
1059 disorder_eclient_string_response
*completed
,
1061 const char *context
,
1064 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1065 "part", track
, context
, part
, (char *)0);
1068 int disorder_eclient_play(disorder_eclient
*c
,
1070 disorder_eclient_no_response
*completed
,
1072 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1073 "play", track
, (char *)0);
1076 int disorder_eclient_pause(disorder_eclient
*c
,
1077 disorder_eclient_no_response
*completed
,
1079 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1080 "pause", (char *)0);
1083 int disorder_eclient_resume(disorder_eclient
*c
,
1084 disorder_eclient_no_response
*completed
,
1086 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1087 "resume", (char *)0);
1090 int disorder_eclient_scratch(disorder_eclient
*c
,
1092 disorder_eclient_no_response
*completed
,
1094 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1095 "scratch", id
, (char *)0);
1098 int disorder_eclient_scratch_playing(disorder_eclient
*c
,
1099 disorder_eclient_no_response
*completed
,
1101 return disorder_eclient_scratch(c
, 0, completed
, v
);
1104 int disorder_eclient_remove(disorder_eclient
*c
,
1106 disorder_eclient_no_response
*completed
,
1108 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1109 "remove", id
, (char *)0);
1112 int disorder_eclient_moveafter(disorder_eclient
*c
,
1116 disorder_eclient_no_response
*completed
,
1122 vector_append(&vec
, (char *)"moveafter");
1123 vector_append(&vec
, (char *)target
);
1124 for(n
= 0; n
< nids
; ++n
)
1125 vector_append(&vec
, (char *)ids
[n
]);
1126 stash_command_vector(c
, 0/*queuejump*/, no_response_opcallback
, completed
, v
,
1128 disorder_eclient_polled(c
, 0);
1132 int disorder_eclient_recent(disorder_eclient
*c
,
1133 disorder_eclient_queue_response
*completed
,
1135 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1136 "recent", (char *)0);
1139 int disorder_eclient_queue(disorder_eclient
*c
,
1140 disorder_eclient_queue_response
*completed
,
1142 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1143 "queue", (char *)0);
1146 int disorder_eclient_files(disorder_eclient
*c
,
1147 disorder_eclient_list_response
*completed
,
1151 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1152 "files", dir
, re
, (char *)0);
1155 int disorder_eclient_dirs(disorder_eclient
*c
,
1156 disorder_eclient_list_response
*completed
,
1160 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1161 "dirs", dir
, re
, (char *)0);
1164 int disorder_eclient_playing(disorder_eclient
*c
,
1165 disorder_eclient_queue_response
*completed
,
1167 return simple(c
, playing_response_opcallback
, (void (*)())completed
, v
,
1168 "playing", (char *)0);
1171 int disorder_eclient_length(disorder_eclient
*c
,
1172 disorder_eclient_integer_response
*completed
,
1175 return simple(c
, integer_response_opcallback
, (void (*)())completed
, v
,
1176 "length", track
, (char *)0);
1179 int disorder_eclient_volume(disorder_eclient
*c
,
1180 disorder_eclient_volume_response
*completed
,
1183 char sl
[64], sr
[64];
1185 if(l
< 0 && r
< 0) {
1186 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1187 "volume", (char *)0);
1188 } else if(l
>= 0 && r
>= 0) {
1191 byte_snprintf(sl
, sizeof sl
, "%d", l
);
1192 byte_snprintf(sr
, sizeof sr
, "%d", r
);
1193 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1194 "volume", sl
, sr
, (char *)0);
1196 assert(!"invalid arguments to disorder_eclient_volume");
1197 return -1; /* gcc is being dim */
1201 int disorder_eclient_enable(disorder_eclient
*c
,
1202 disorder_eclient_no_response
*completed
,
1204 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1205 "enable", (char *)0);
1208 int disorder_eclient_disable(disorder_eclient
*c
,
1209 disorder_eclient_no_response
*completed
,
1211 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1212 "disable", (char *)0);
1215 int disorder_eclient_random_enable(disorder_eclient
*c
,
1216 disorder_eclient_no_response
*completed
,
1218 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1219 "random-enable", (char *)0);
1222 int disorder_eclient_random_disable(disorder_eclient
*c
,
1223 disorder_eclient_no_response
*completed
,
1225 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1226 "random-disable", (char *)0);
1229 int disorder_eclient_get(disorder_eclient
*c
,
1230 disorder_eclient_string_response
*completed
,
1231 const char *track
, const char *pref
,
1233 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1234 "get", track
, pref
, (char *)0);
1237 int disorder_eclient_set(disorder_eclient
*c
,
1238 disorder_eclient_no_response
*completed
,
1239 const char *track
, const char *pref
,
1242 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1243 "set", track
, pref
, value
, (char *)0);
1246 int disorder_eclient_unset(disorder_eclient
*c
,
1247 disorder_eclient_no_response
*completed
,
1248 const char *track
, const char *pref
,
1250 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1251 "unset", track
, pref
, (char *)0);
1254 int disorder_eclient_resolve(disorder_eclient
*c
,
1255 disorder_eclient_string_response
*completed
,
1258 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1259 "resolve", track
, (char *)0);
1262 int disorder_eclient_search(disorder_eclient
*c
,
1263 disorder_eclient_list_response
*completed
,
1266 if(!split(terms
, 0, SPLIT_QUOTES
, 0, 0)) return -1;
1267 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1268 "search", terms
, (char *)0);
1271 int disorder_eclient_nop(disorder_eclient
*c
,
1272 disorder_eclient_no_response
*completed
,
1274 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1278 /** @brief Get the last @p max added tracks
1280 * @param completed Called with list
1281 * @param max Number of tracks to get, 0 for all
1282 * @param v Passed to @p completed
1284 * The first track in the list is the most recently added.
1286 int disorder_eclient_new_tracks(disorder_eclient
*c
,
1287 disorder_eclient_list_response
*completed
,
1292 sprintf(limit
, "%d", max
);
1293 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1294 "new", limit
, (char *)0);
1297 static void rtp_response_opcallback(disorder_eclient
*c
,
1298 struct operation
*op
) {
1299 disorder_eclient_list_response
*const completed
=
1300 (disorder_eclient_list_response
*)op
->completed
;
1301 D(("rtp_response_opcallback"));
1302 if(c
->rc
/ 100 == 2) {
1304 char **vec
= split(c
->line
+ 4, &nvec
, SPLIT_QUOTES
, 0, 0);
1307 completed(op
->v
, NULL
, nvec
, vec
);
1309 completed(op
->v
, "error parsing response", 0, 0);
1311 completed(op
->v
, errorstring(c
), 0, 0);
1314 /** @brief Determine the RTP target address
1316 * @param completed Called with address details
1317 * @param v Passed to @p completed
1319 * The address details will be two elements, the first being the hostname and
1320 * the second the service (port).
1322 int disorder_eclient_rtp_address(disorder_eclient
*c
,
1323 disorder_eclient_list_response
*completed
,
1325 return simple(c
, rtp_response_opcallback
, (void (*)())completed
, v
,
1326 "rtp-address", (char *)0);
1329 /** @brief Get the list of users
1331 * @param completed Called with list of users
1332 * @param v Passed to @p completed
1334 * The user list is not sorted in any particular order.
1336 int disorder_eclient_users(disorder_eclient
*c
,
1337 disorder_eclient_list_response
*completed
,
1339 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1340 "users", (char *)0);
1343 /** @brief Delete a user
1345 * @param completed Called on completion
1346 * @param user User to delete
1347 * @param v Passed to @p completed
1349 int disorder_eclient_deluser(disorder_eclient
*c
,
1350 disorder_eclient_no_response
*completed
,
1353 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1354 "deluser", user
, (char *)0);
1357 /** @brief Get a user property
1359 * @param completed Called on completion
1360 * @param user User to look up
1361 * @param property Property to look up
1362 * @param v Passed to @p completed
1364 int disorder_eclient_userinfo(disorder_eclient
*c
,
1365 disorder_eclient_string_response
*completed
,
1367 const char *property
,
1369 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1370 "userinfo", user
, property
, (char *)0);
1373 /** @brief Modify a user property
1375 * @param completed Called on completion
1376 * @param user User to modify
1377 * @param property Property to modify
1378 * @param value New property value
1379 * @param v Passed to @p completed
1381 int disorder_eclient_edituser(disorder_eclient
*c
,
1382 disorder_eclient_no_response
*completed
,
1384 const char *property
,
1387 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1388 "edituser", user
, property
, value
, (char *)0);
1391 /** @brief Create a new user
1393 * @param completed Called on completion
1394 * @param user User to create
1395 * @param password Initial password
1396 * @param rights Initial rights or NULL
1397 * @param v Passed to @p completed
1399 int disorder_eclient_adduser(disorder_eclient
*c
,
1400 disorder_eclient_no_response
*completed
,
1402 const char *password
,
1405 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1406 "adduser", user
, password
, rights
, (char *)0);
1409 /** @brief Adopt a track
1411 * @param completed Called on completion
1412 * @param id Track ID
1413 * @param v Passed to @p completed
1415 int disorder_eclient_adopt(disorder_eclient
*c
,
1416 disorder_eclient_no_response
*completed
,
1419 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1420 "adopt", id
, (char *)0);
1423 /* Log clients ***************************************************************/
1425 /** @brief Monitor the server log
1427 * @param callbacks Functions to call when anything happens
1428 * @param v Passed to @p callbacks functions
1430 * Once a client is being used for logging it cannot be used for anything else.
1431 * There is magic in authuser_opcallback() to re-submit the @c log command
1432 * after reconnection.
1434 * NB that the @c state callback may be called from within this function,
1435 * i.e. not solely later on from the event loop callback.
1437 int disorder_eclient_log(disorder_eclient
*c
,
1438 const disorder_eclient_log_callbacks
*callbacks
,
1440 if(c
->log_callbacks
) return -1;
1441 c
->log_callbacks
= callbacks
;
1443 /* Repoort initial state */
1444 if(c
->log_callbacks
->state
)
1445 c
->log_callbacks
->state(c
->log_v
, c
->statebits
);
1446 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, v
,
1448 disorder_eclient_polled(c
, 0);
1452 /* If we get here we've stopped being a log client */
1453 static void log_opcallback(disorder_eclient
*c
,
1454 struct operation
attribute((unused
)) *op
) {
1455 D(("log_opcallback"));
1456 c
->log_callbacks
= 0;
1460 /* error callback for log line parsing */
1461 static void logline_error(const char *msg
, void *u
) {
1462 disorder_eclient
*c
= u
;
1463 /* TODO don't use protocol_error here */
1464 protocol_error(c
, c
->ops
, -1, "error parsing log line: %s", msg
);
1467 /* process a single log line */
1468 static void logline(disorder_eclient
*c
, const char *line
) {
1473 D(("logline [%s]", line
));
1474 vec
= split(line
, &nvec
, SPLIT_QUOTES
, logline_error
, c
);
1475 if(nvec
< 2) return; /* probably an error, already
1477 if(sscanf(vec
[0], "%"SCNxMAX
, &when
) != 1) {
1478 /* probably the wrong side of a format change */
1479 /* TODO don't use protocol_error here */
1480 protocol_error(c
, c
->ops
, -1, "invalid log timestamp '%s'", vec
[0]);
1483 /* TODO: do something with the time */
1484 //fprintf(stderr, "log key: %s\n", vec[1]);
1485 n
= TABLE_FIND(logentry_handlers
, name
, vec
[1]);
1487 //fprintf(stderr, "...not found\n");
1488 return; /* probably a future command */
1492 if(nvec
< logentry_handlers
[n
].min
|| nvec
> logentry_handlers
[n
].max
) {
1493 //fprintf(stderr, "...wrong # args\n");
1496 logentry_handlers
[n
].handler(c
, nvec
, vec
);
1499 static void logentry_completed(disorder_eclient
*c
,
1500 int attribute((unused
)) nvec
, char **vec
) {
1501 c
->statebits
&= ~DISORDER_PLAYING
;
1502 if(c
->log_callbacks
->completed
)
1503 c
->log_callbacks
->completed(c
->log_v
, vec
[0]);
1504 if(c
->log_callbacks
->state
)
1505 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1508 static void logentry_failed(disorder_eclient
*c
,
1509 int attribute((unused
)) nvec
, char **vec
) {
1510 c
->statebits
&= ~DISORDER_PLAYING
;
1511 if(c
->log_callbacks
->failed
)
1512 c
->log_callbacks
->failed(c
->log_v
, vec
[0], vec
[1]);
1513 if(c
->log_callbacks
->state
)
1514 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1517 static void logentry_moved(disorder_eclient
*c
,
1518 int attribute((unused
)) nvec
, char **vec
) {
1519 if(c
->log_callbacks
->moved
)
1520 c
->log_callbacks
->moved(c
->log_v
, vec
[0]);
1523 static void logentry_playing(disorder_eclient
*c
,
1524 int attribute((unused
)) nvec
, char **vec
) {
1525 c
->statebits
|= DISORDER_PLAYING
;
1526 if(c
->log_callbacks
->playing
)
1527 c
->log_callbacks
->playing(c
->log_v
, vec
[0], vec
[1]);
1528 if(c
->log_callbacks
->state
)
1529 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1532 static void logentry_queue(disorder_eclient
*c
,
1533 int attribute((unused
)) nvec
, char **vec
) {
1534 struct queue_entry
*q
;
1536 if(!c
->log_callbacks
->queue
) return;
1537 q
= xmalloc(sizeof *q
);
1538 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1540 c
->log_callbacks
->queue(c
->log_v
, q
);
1543 static void logentry_recent_added(disorder_eclient
*c
,
1544 int attribute((unused
)) nvec
, char **vec
) {
1545 struct queue_entry
*q
;
1547 if(!c
->log_callbacks
->recent_added
) return;
1548 q
= xmalloc(sizeof *q
);
1549 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1551 c
->log_callbacks
->recent_added(c
->log_v
, q
);
1554 static void logentry_recent_removed(disorder_eclient
*c
,
1555 int attribute((unused
)) nvec
, char **vec
) {
1556 if(c
->log_callbacks
->recent_removed
)
1557 c
->log_callbacks
->recent_removed(c
->log_v
, vec
[0]);
1560 static void logentry_removed(disorder_eclient
*c
,
1561 int attribute((unused
)) nvec
, char **vec
) {
1562 if(c
->log_callbacks
->removed
)
1563 c
->log_callbacks
->removed(c
->log_v
, vec
[0], vec
[1]);
1566 static void logentry_rescanned(disorder_eclient
*c
,
1567 int attribute((unused
)) nvec
,
1568 char attribute((unused
)) **vec
) {
1569 if(c
->log_callbacks
->rescanned
)
1570 c
->log_callbacks
->rescanned(c
->log_v
);
1573 static void logentry_scratched(disorder_eclient
*c
,
1574 int attribute((unused
)) nvec
, char **vec
) {
1575 c
->statebits
&= ~DISORDER_PLAYING
;
1576 if(c
->log_callbacks
->scratched
)
1577 c
->log_callbacks
->scratched(c
->log_v
, vec
[0], vec
[1]);
1578 if(c
->log_callbacks
->state
)
1579 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1582 static void logentry_user_add(disorder_eclient
*c
,
1583 int attribute((unused
)) nvec
, char **vec
) {
1584 if(c
->log_callbacks
->user_add
)
1585 c
->log_callbacks
->user_add(c
->log_v
, vec
[0]);
1588 static void logentry_user_confirm(disorder_eclient
*c
,
1589 int attribute((unused
)) nvec
, char **vec
) {
1590 if(c
->log_callbacks
->user_confirm
)
1591 c
->log_callbacks
->user_confirm(c
->log_v
, vec
[0]);
1594 static void logentry_user_delete(disorder_eclient
*c
,
1595 int attribute((unused
)) nvec
, char **vec
) {
1596 if(c
->log_callbacks
->user_delete
)
1597 c
->log_callbacks
->user_delete(c
->log_v
, vec
[0]);
1600 static void logentry_user_edit(disorder_eclient
*c
,
1601 int attribute((unused
)) nvec
, char **vec
) {
1602 if(c
->log_callbacks
->user_edit
)
1603 c
->log_callbacks
->user_edit(c
->log_v
, vec
[0], vec
[1]);
1606 static void logentry_rights_changed(disorder_eclient
*c
,
1607 int attribute((unused
)) nvec
, char **vec
) {
1608 if(c
->log_callbacks
->rights_changed
) {
1610 if(!parse_rights(vec
[0], &r
, 0/*report*/))
1611 c
->log_callbacks
->rights_changed(c
->log_v
, r
);
1615 static const struct {
1618 const char *disable
;
1619 } statestrings
[] = {
1620 { DISORDER_PLAYING_ENABLED
, "enable_play", "disable_play" },
1621 { DISORDER_RANDOM_ENABLED
, "enable_random", "disable_random" },
1622 { DISORDER_TRACK_PAUSED
, "pause", "resume" },
1623 { DISORDER_PLAYING
, "playing", "completed" },
1624 { DISORDER_PLAYING
, 0, "scratched" },
1625 { DISORDER_PLAYING
, 0, "failed" },
1627 #define NSTATES (int)(sizeof statestrings / sizeof *statestrings)
1629 static void logentry_state(disorder_eclient
*c
,
1630 int attribute((unused
)) nvec
, char **vec
) {
1633 for(n
= 0; n
< NSTATES
; ++n
)
1634 if(statestrings
[n
].enable
&& !strcmp(vec
[0], statestrings
[n
].enable
)) {
1635 c
->statebits
|= statestrings
[n
].bit
;
1637 } else if(statestrings
[n
].disable
&& !strcmp(vec
[0], statestrings
[n
].disable
)) {
1638 c
->statebits
&= ~statestrings
[n
].bit
;
1641 if(c
->log_callbacks
->state
)
1642 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1645 static void logentry_volume(disorder_eclient
*c
,
1646 int attribute((unused
)) nvec
, char **vec
) {
1649 if(!c
->log_callbacks
->volume
) return;
1650 if(xstrtol(&l
, vec
[0], 0, 10)
1651 || xstrtol(&r
, vec
[1], 0, 10)
1652 || l
< 0 || l
> INT_MAX
1653 || r
< 0 || r
> INT_MAX
)
1655 c
->log_callbacks
->volume(c
->log_v
, (int)l
, (int)r
);
1658 /** @brief Convert @p statebits to a string */
1659 char *disorder_eclient_interpret_state(unsigned long statebits
) {
1663 static const struct {
1667 { DISORDER_PLAYING_ENABLED
, "playing_enabled" },
1668 { DISORDER_RANDOM_ENABLED
, "random_enabled" },
1669 { DISORDER_TRACK_PAUSED
, "track_paused" },
1670 { DISORDER_PLAYING
, "playing" },
1671 { DISORDER_CONNECTED
, "connected" },
1673 #define NBITS (sizeof bits / sizeof *bits)
1677 dynstr_append(d
, '0');
1678 for(n
= 0; n
< NBITS
; ++n
)
1679 if(statebits
& bits
[n
].bit
) {
1681 dynstr_append(d
, '|');
1682 dynstr_append_string(d
, bits
[n
].name
);
1683 statebits
^= bits
[n
].bit
;
1689 dynstr_append(d
, '|');
1690 sprintf(s
, "%#lx", statebits
);
1691 dynstr_append_string(d
, s
);
1693 dynstr_terminate(d
);
1697 static void logentry_adopted(disorder_eclient
*c
,
1698 int attribute((unused
)) nvec
, char **vec
) {
1699 if(c
->log_callbacks
->adopted
)
1700 c
->log_callbacks
->adopted(c
->log_v
, vec
[0], vec
[1]);
1708 indent-tabs-mode:nil