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 char **body
; /**< @brief command body */
96 operation_callback
*opcallback
; /**< @brief internal completion callback */
97 void (*completed
)(); /**< @brief user completion callback or 0 */
98 void *v
; /**< @brief data for COMPLETED */
99 disorder_eclient
*client
; /**< @brief owning client */
101 /** @brief true if sent to server
103 * This is cleared by disorder_eclient_close(), forcing all queued
104 * commands to be transparently resent.
109 /** @brief Client structure */
110 struct disorder_eclient
{
112 int fd
; /**< @brief connection to server */
113 enum client_state state
; /**< @brief current state */
114 int authenticated
; /**< @brief true when authenicated */
115 struct dynstr output
; /**< @brief output buffer */
116 struct dynstr input
; /**< @brief input buffer */
117 int eof
; /**< @brief input buffer is at EOF */
118 const disorder_eclient_callbacks
*callbacks
; /**< @brief error callbacks */
119 void *u
; /**< @brief user data */
120 struct operation
*ops
; /**< @brief queue of operations */
121 struct operation
**opstail
; /**< @brief queue tail */
122 /* accumulated response */
123 int rc
; /**< @brief response code */
124 char *line
; /**< @brief complete line */
125 struct vector vec
; /**< @brief body */
127 const disorder_eclient_log_callbacks
*log_callbacks
;
128 /**< @brief log callbacks
130 * Once disorder_eclient_log() has been issued this is always set. When we
131 * re-connect it is checked to re-issue the log command.
133 void *log_v
; /**< @brief user data */
134 unsigned long statebits
; /**< @brief latest state */
137 /**< @brief last time we sent a prod
139 * When we are receiving log data we send a "prod" byte to the server from
140 * time to time so that we detect broken connections reasonably quickly. The
141 * server just ignores these bytes.
144 /** @brief Protocol version */
147 /** @brief True if enabled */
151 /* Forward declarations ******************************************************/
153 static int start_connect(disorder_eclient
*c
);
154 static void process_line(disorder_eclient
*c
, char *line
);
155 static void maybe_connected(disorder_eclient
*c
);
156 static void authbanner_opcallback(disorder_eclient
*c
,
157 struct operation
*op
);
158 static void authuser_opcallback(disorder_eclient
*c
,
159 struct operation
*op
);
160 static void complete(disorder_eclient
*c
);
161 static void send_output(disorder_eclient
*c
);
162 static void put(disorder_eclient
*c
, const char *s
, size_t n
);
163 static void read_input(disorder_eclient
*c
);
164 static void stash_command(disorder_eclient
*c
,
166 operation_callback
*opcallback
,
173 static void log_opcallback(disorder_eclient
*c
, struct operation
*op
);
174 static void logline(disorder_eclient
*c
, const char *line
);
175 static void logentry_completed(disorder_eclient
*c
, int nvec
, char **vec
);
176 static void logentry_failed(disorder_eclient
*c
, int nvec
, char **vec
);
177 static void logentry_moved(disorder_eclient
*c
, int nvec
, char **vec
);
178 static void logentry_playing(disorder_eclient
*c
, int nvec
, char **vec
);
179 static void logentry_queue(disorder_eclient
*c
, int nvec
, char **vec
);
180 static void logentry_recent_added(disorder_eclient
*c
, int nvec
, char **vec
);
181 static void logentry_recent_removed(disorder_eclient
*c
, int nvec
, char **vec
);
182 static void logentry_removed(disorder_eclient
*c
, int nvec
, char **vec
);
183 static void logentry_scratched(disorder_eclient
*c
, int nvec
, char **vec
);
184 static void logentry_state(disorder_eclient
*c
, int nvec
, char **vec
);
185 static void logentry_volume(disorder_eclient
*c
, int nvec
, char **vec
);
186 static void logentry_rescanned(disorder_eclient
*c
, int nvec
, char **vec
);
187 static void logentry_user_add(disorder_eclient
*c
, int nvec
, char **vec
);
188 static void logentry_user_confirm(disorder_eclient
*c
, int nvec
, char **vec
);
189 static void logentry_user_delete(disorder_eclient
*c
, int nvec
, char **vec
);
190 static void logentry_user_edit(disorder_eclient
*c
, int nvec
, char **vec
);
191 static void logentry_rights_changed(disorder_eclient
*c
, int nvec
, char **vec
);
192 static void logentry_adopted(disorder_eclient
*c
, int nvec
, char **vec
);
193 static void logentry_playlist_created(disorder_eclient
*c
, int nvec
, char **vec
);
194 static void logentry_playlist_deleted(disorder_eclient
*c
, int nvec
, char **vec
);
195 static void logentry_playlist_modified(disorder_eclient
*c
, int nvec
, char **vec
);
197 /* Tables ********************************************************************/
199 /** @brief One possible log entry */
200 struct logentry_handler
{
201 const char *name
; /**< @brief Entry name */
202 int min
; /**< @brief Minimum arguments */
203 int max
; /**< @brief Maximum arguments */
204 void (*handler
)(disorder_eclient
*c
,
206 char **vec
); /**< @brief Handler function */
209 /** @brief Table for parsing log entries */
210 static const struct logentry_handler logentry_handlers
[] = {
211 #define LE(X, MIN, MAX) { #X, MIN, MAX, logentry_##X }
217 LE(playlist_created
, 2, 2),
218 LE(playlist_deleted
, 1, 1),
219 LE(playlist_modified
, 2, 2),
220 LE(queue
, 2, INT_MAX
),
221 LE(recent_added
, 2, INT_MAX
),
222 LE(recent_removed
, 1, 1),
225 LE(rights_changed
, 1, 1),
229 LE(user_confirm
, 1, 1),
230 LE(user_delete
, 1, 1),
235 /* Setup and teardown ********************************************************/
237 /** @brief Create a new client
239 * Does NOT connect the client - connections are made (and re-made) on demand.
241 disorder_eclient
*disorder_eclient_new(const disorder_eclient_callbacks
*cb
,
243 disorder_eclient
*c
= xmalloc(sizeof *c
);
244 D(("disorder_eclient_new"));
248 c
->opstail
= &c
->ops
;
250 vector_init(&c
->vec
);
251 dynstr_init(&c
->input
);
252 dynstr_init(&c
->output
);
256 /** @brief Disconnect a client
257 * @param c Client to disconnect
259 * NB that this routine just disconnnects the TCP connection. It does not
260 * destroy the client! If you continue to use it then it will attempt to
263 void disorder_eclient_close(disorder_eclient
*c
) {
264 struct operation
*op
;
266 D(("disorder_eclient_close"));
268 D(("disorder_eclient_close closing fd %d", c
->fd
));
269 c
->callbacks
->poll(c
->u
, c
, c
->fd
, 0);
272 c
->state
= state_disconnected
;
278 c
->authenticated
= 0;
279 /* We'll need to resend all operations */
280 for(op
= c
->ops
; op
; op
= op
->next
)
282 /* Drop our use a hint that we're disconnected */
283 if(c
->log_callbacks
&& c
->log_callbacks
->state
)
284 c
->log_callbacks
->state(c
->log_v
, c
->statebits
);
287 /** @brief Permit new connection activity */
288 void disorder_eclient_enable_connect(disorder_eclient
*c
) {
292 /** @brief Suppress new connection activity */
293 void disorder_eclient_disable_connect(disorder_eclient
*c
) {
297 /** @brief Return current state */
298 unsigned long disorder_eclient_state(const disorder_eclient
*c
) {
299 return c
->statebits
| (c
->state
> state_connected ? DISORDER_CONNECTED
: 0);
302 /* Error reporting ***********************************************************/
304 /** @brief called when a connection error occurs
306 * After this called we will be disconnected (by disorder_eclient_close()),
307 * so there will be a reconnection before any commands can be sent.
309 static int comms_error(disorder_eclient
*c
, const char *fmt
, ...) {
315 byte_xvasprintf(&s
, fmt
, ap
);
317 disorder_eclient_close(c
);
318 c
->callbacks
->comms_error(c
->u
, s
);
322 /** @brief called when the server reports an error */
323 static int protocol_error(disorder_eclient
*c
, struct operation
*op
,
324 int code
, const char *fmt
, ...) {
328 D(("protocol_error"));
330 byte_xvasprintf(&s
, fmt
, ap
);
332 c
->callbacks
->protocol_error(c
->u
, op
->v
, code
, s
);
336 /* State machine *************************************************************/
338 /** @brief Send an operation (into the output buffer)
339 * @param op Operation to send
341 static void op_send(struct operation
*op
) {
342 disorder_eclient
*const c
= op
->client
;
343 put(c
, op
->cmd
, strlen(op
->cmd
));
345 for(int n
= 0; op
->body
[n
]; ++n
) {
346 if(op
->body
[n
][0] == '.')
348 put(c
, op
->body
[n
], strlen(op
->body
[n
]));
356 /** @brief Called when there's something to do
358 * @param mode bitmap of @ref DISORDER_POLL_READ and/or @ref DISORDER_POLL_WRITE.
360 * This should be called from by your code when the file descriptor is readable
361 * or writable (as requested by the @c poll callback, see @ref
362 * disorder_eclient_callbacks) and in any case from time to time (with @p mode
363 * = 0) to allow for retries to work.
365 void disorder_eclient_polled(disorder_eclient
*c
, unsigned mode
) {
366 struct operation
*op
;
369 D(("disorder_eclient_polled fd=%d state=%s mode=[%s %s]",
370 c
->fd
, states
[c
->state
],
371 mode
& DISORDER_POLL_READ ?
"READ" : "",
372 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
373 /* The pattern here is to check each possible state in turn and try to
374 * advance (though on error we might go back). If we advance we leave open
375 * the possibility of falling through to the next state, but we set the mode
376 * bits to 0, to avoid false positives (which matter more in some cases than
379 if(c
->state
== state_disconnected
) {
380 D(("state_disconnected"));
381 /* If there is no password yet then we cannot connect */
382 if(!config
->password
) {
383 comms_error(c
, "no password is configured");
387 /* Only try to connect if enabled */
390 /* might now be state_disconnected (on error), state_connecting (slow
391 * connect) or state_connected (fast connect). If state_disconnected then
392 * we just rely on a periodic callback from the event loop sometime. */
396 if(c
->state
== state_connecting
&& mode
) {
397 D(("state_connecting"));
399 /* Might be state_disconnected (on error) or state_connected (on success).
400 * In the former case we rely on the event loop for a periodic callback to
405 if(c
->state
== state_connected
) {
406 D(("state_connected"));
407 /* We just connected. Initiate the authentication protocol. */
408 stash_command(c
, 1/*queuejump*/, authbanner_opcallback
,
409 0/*completed*/, 0/*v*/, -1/*nbody*/, 0/*body*/, 0/*cmd*/);
410 /* We never stay is state_connected very long. We could in principle jump
411 * straight to state_cmdresponse since there's actually no command to
412 * send, but that would arguably be cheating. */
413 c
->state
= state_idle
;
416 if(c
->state
== state_idle
) {
418 /* We are connected, and have finished any command we set off, look for
422 if(c
->authenticated
) {
423 /* Transmit all unsent operations */
424 for(op
= c
->ops
; op
; op
= op
->next
) {
429 /* Just send the head operation */
430 if(c
->ops
->cmd
&& !c
->ops
->sent
)
433 /* Awaiting response for the operation at the head of the list */
434 c
->state
= state_cmdresponse
;
437 c
->callbacks
->report(c
->u
, 0);
440 /* Queue up a byte to send */
441 if(c
->state
== state_log
442 && c
->output
.nvec
== 0
443 && xtime(&now
) - c
->last_prod
> LOG_PROD_INTERVAL
) {
448 if(c
->state
== state_cmdresponse
449 || c
->state
== state_body
450 || c
->state
== state_log
) {
451 D(("state_%s", states
[c
->state
]));
452 /* We are awaiting a response */
453 if(mode
& DISORDER_POLL_WRITE
) send_output(c
);
454 if(mode
& DISORDER_POLL_READ
) read_input(c
);
455 /* There are a couple of reasons we might want to re-enter the state
456 * machine from the top. state_idle is obvious: there may be further
457 * commands to process. Re-entering on state_disconnected means that we
458 * immediately retry connection if a comms error occurs during a command.
459 * This is different to the case where a connection fails, where we await a
460 * spontaneous call to initiate the retry. */
462 case state_disconnected
: /* lost connection */
463 case state_idle
: /* completed a command */
465 disorder_eclient_polled(c
, 0);
472 /* Figure out what to set the mode to */
474 case state_disconnected
:
475 D(("state_disconnected (2)"));
476 /* Probably an error occurred. Await a retry. */
479 case state_connecting
:
480 D(("state_connecting (2)"));
481 /* Waiting for connect to complete */
482 mode
= DISORDER_POLL_READ
|DISORDER_POLL_WRITE
;
484 case state_connected
:
485 D(("state_connected (2)"));
486 assert(!"should never be in state_connected here");
489 D(("state_idle (2)"));
490 /* Connected but nothing to do. */
493 case state_cmdresponse
:
496 D(("state_%s (2)", states
[c
->state
]));
497 /* Gathering a response. Wait for input. */
498 mode
= DISORDER_POLL_READ
;
499 /* Flush any pending output. */
500 if(c
->output
.nvec
) mode
|= DISORDER_POLL_WRITE
;
503 D(("fd=%d new mode [%s %s]",
505 mode
& DISORDER_POLL_READ ?
"READ" : "",
506 mode
& DISORDER_POLL_WRITE ?
"WRITE" : ""));
507 if(c
->fd
!= -1) c
->callbacks
->poll(c
->u
, c
, c
->fd
, mode
);
510 /** @brief Called to start connecting */
511 static int start_connect(disorder_eclient
*c
) {
515 D(("start_connect"));
516 if((len
= find_server(config
, &sa
, &c
->ident
)) == (socklen_t
)-1)
517 return comms_error(c
, "cannot look up server"); /* TODO better error */
522 if((c
->fd
= socket(sa
->sa_family
, SOCK_STREAM
, 0)) < 0)
523 return comms_error(c
, "socket: %s", strerror(errno
));
527 if(connect(c
->fd
, sa
, len
) < 0) {
531 c
->state
= state_connecting
;
532 /* We are called from _polled so the state machine will get to do its
536 /* Signal the error to the caller. */
537 return comms_error(c
, "connecting to %s: %s", c
->ident
, strerror(errno
));
540 c
->state
= state_connected
;
544 /** @brief Called when poll triggers while waiting for a connection */
545 static void maybe_connected(disorder_eclient
*c
) {
546 /* We either connected, or got an error. */
548 socklen_t len
= sizeof err
;
550 D(("maybe_connected"));
551 /* Work around over-enthusiastic error slippage */
552 if(getsockopt(c
->fd
, SOL_SOCKET
, SO_ERROR
, &err
, &len
) < 0)
555 /* The connection failed */
556 comms_error(c
, "connecting to %s: %s", c
->ident
, strerror(err
));
557 /* sets state_disconnected */
561 /* The connection succeeded */
562 c
->state
= state_connected
;
563 byte_xasprintf(&r
, "connected to %s", c
->ident
);
564 c
->callbacks
->report(c
->u
, r
);
565 /* If this is a log client we expect to get a bunch of updates from the
566 * server straight away */
570 /* Authentication ************************************************************/
572 /** @brief Called with the greeting from the server */
573 static void authbanner_opcallback(disorder_eclient
*c
,
574 struct operation
*op
) {
576 const unsigned char *nonce
;
580 const char *protocol
, *algorithm
, *challenge
;
582 D(("authbanner_opcallback"));
584 || !(rvec
= split(c
->line
+ 4, &nrvec
, SPLIT_QUOTES
, 0, 0))
586 /* Banner told us to go away, or was malformed. We cannot proceed. */
587 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
588 disorder_eclient_close(c
);
608 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
609 disorder_eclient_close(c
);
612 c
->protocol
= atoi(protocol
);
613 if(c
->protocol
< 1 || c
->protocol
> 2) {
614 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
615 disorder_eclient_close(c
);
618 nonce
= unhex(challenge
, &nonce_len
);
619 res
= authhash(nonce
, nonce_len
, config
->password
, algorithm
);
621 protocol_error(c
, op
, c
->rc
, "%s: unknown authentication algorithm '%s'",
622 c
->ident
, algorithm
);
623 disorder_eclient_close(c
);
626 stash_command(c
, 1/*queuejump*/, authuser_opcallback
, 0/*completed*/, 0/*v*/,
627 -1/*nbody*/, 0/*body*/,
628 "user", quoteutf8(config
->username
), quoteutf8(res
),
632 /** @brief Called with the response to the @c user command */
633 static void authuser_opcallback(disorder_eclient
*c
,
634 struct operation
*op
) {
637 D(("authuser_opcallback"));
638 if(c
->rc
/ 100 != 2) {
639 /* Wrong password or something. We cannot proceed. */
640 protocol_error(c
, op
, c
->rc
, "%s: %s", c
->ident
, c
->line
);
642 disorder_eclient_close(c
);
645 /* OK, we're authenticated now. */
646 c
->authenticated
= 1;
647 byte_xasprintf(&r
, "authenticated with %s", c
->ident
);
648 c
->callbacks
->report(c
->u
, r
);
649 if(c
->log_callbacks
&& !(c
->ops
&& c
->ops
->opcallback
== log_opcallback
))
650 /* We are a log client, switch to logging mode */
651 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, c
->log_v
,
652 -1/*nbody*/, 0/*body*/,
656 /* Output ********************************************************************/
658 /* Chop N bytes off the front of a dynstr */
659 static void consume(struct dynstr
*d
, int n
) {
660 D(("consume %d", n
));
661 assert(d
->nvec
>= n
);
662 memmove(d
->vec
, d
->vec
+ n
, d
->nvec
- n
);
666 /* Write some bytes */
667 static void put(disorder_eclient
*c
, const char *s
, size_t n
) {
668 D(("put %d %.*s", c
->fd
, (int)n
, s
));
669 dynstr_append_bytes(&c
->output
, s
, n
);
672 /* Called when we can write to our FD, or at any other time */
673 static void send_output(disorder_eclient
*c
) {
676 D(("send_output %d bytes pending", c
->output
.nvec
));
677 if(c
->state
> state_connecting
&& c
->output
.nvec
) {
678 n
= write(c
->fd
, c
->output
.vec
, c
->output
.nvec
);
685 comms_error(c
, "writing to %s: %s", c
->ident
, strerror(errno
));
689 consume(&c
->output
, n
);
693 /* Input *********************************************************************/
695 /* Called when c->fd might be readable, or at any other time */
696 static void read_input(disorder_eclient
*c
) {
701 D(("read_input in state %s", states
[c
->state
]));
702 if(c
->state
<= state_connected
) return; /* ignore bogus calls */
703 /* read some more input */
704 n
= read(c
->fd
, buffer
, sizeof buffer
);
711 comms_error(c
, "reading from %s: %s", c
->ident
, strerror(errno
));
714 return; /* no new input to process */
716 D(("read %d bytes: [%.*s]", n
, n
, buffer
));
717 dynstr_append_bytes(&c
->input
, buffer
, n
);
720 /* might have more than one line to process */
721 while(c
->state
> state_connecting
722 && (nl
= memchr(c
->input
.vec
, '\n', c
->input
.nvec
))) {
723 process_line(c
, xstrndup(c
->input
.vec
, nl
- c
->input
.vec
));
724 /* we might have disconnected along the way, which zogs the input buffer */
725 if(c
->state
> state_connecting
)
726 consume(&c
->input
, (nl
- c
->input
.vec
) + 1);
729 comms_error(c
, "reading from %s: server disconnected", c
->ident
);
730 c
->authenticated
= 0;
734 /* called with a line that has just been read */
735 static void process_line(disorder_eclient
*c
, char *line
) {
736 D(("process_line %d [%s]", c
->fd
, line
));
738 case state_cmdresponse
:
739 /* This is the first line of a response */
740 if(!(line
[0] >= '0' && line
[0] <= '9'
741 && line
[1] >= '0' && line
[1] <= '9'
742 && line
[2] >= '0' && line
[2] <= '9'
744 fatal(0, "invalid response from server: %s", line
);
745 c
->rc
= (line
[0] * 10 + line
[1]) * 10 + line
[2] - 111 * '0';
749 /* We need to collect the body. */
750 c
->state
= state_body
;
751 vector_init(&c
->vec
);
754 assert(c
->log_callbacks
!= 0);
755 if(c
->log_callbacks
->connected
)
756 c
->log_callbacks
->connected(c
->log_v
);
757 c
->state
= state_log
;
760 /* We've got the whole response. Go into the idle state so the state
761 * machine knows we're done and then call the operation callback. */
767 if(strcmp(line
, ".")) {
768 /* A line from the body */
769 vector_append(&c
->vec
, line
+ (line
[0] == '.'));
771 /* End of the body. */
772 vector_terminate(&c
->vec
);
777 if(strcmp(line
, ".")) {
778 logline(c
, line
+ (line
[0] == '.'));
783 assert(!"wrong state for location");
788 /* Called when an operation completes */
789 static void complete(disorder_eclient
*c
) {
790 struct operation
*op
;
793 /* Pop the operation off the queue */
796 if(c
->opstail
== &op
->next
)
797 c
->opstail
= &c
->ops
;
798 /* If we've pipelined a command ahead then we go straight to cmdresponser.
799 * Otherwise we go to idle, which will arrange further sends. */
800 c
->state
= c
->ops
&& c
->ops
->sent ? state_cmdresponse
: state_idle
;
801 op
->opcallback(c
, op
);
802 /* Note that we always call the opcallback even on error, though command
803 * opcallbacks generally always do the same error handling, i.e. just call
804 * protocol_error(). It's the auth* opcallbacks that have different
808 /* Operation setup ***********************************************************/
810 static void stash_command_vector(disorder_eclient
*c
,
812 operation_callback
*opcallback
,
819 struct operation
*op
= xmalloc(sizeof *op
);
825 for(n
= 0; n
< ncmd
; ++n
) {
827 dynstr_append(&d
, ' ');
828 dynstr_append_string(&d
, quoteutf8(cmd
[n
]));
830 dynstr_append(&d
, '\n');
831 dynstr_terminate(&d
);
834 op
->cmd
= 0; /* usually, awaiting challenge */
836 op
->body
= xcalloc(nbody
+ 1, sizeof (char *));
837 for(n
= 0; n
< nbody
; ++n
)
838 op
->body
[n
] = xstrdup(body
[n
]);
842 op
->opcallback
= opcallback
;
843 op
->completed
= completed
;
847 assert(op
->sent
== 0);
849 /* Authentication operations jump the queue of useful commands */
852 if(c
->opstail
== &c
->ops
)
853 c
->opstail
= &op
->next
;
854 for(op
= c
->ops
; op
; op
= op
->next
)
858 c
->opstail
= &op
->next
;
862 static void vstash_command(disorder_eclient
*c
,
864 operation_callback
*opcallback
,
869 const char *cmd
, va_list ap
) {
873 D(("vstash_command %s", cmd ? cmd
: "NULL"));
876 vector_append(&vec
, (char *)cmd
);
877 while((arg
= va_arg(ap
, char *)))
878 vector_append(&vec
, arg
);
879 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
,
880 nbody
, body
, vec
.nvec
, vec
.vec
);
882 stash_command_vector(c
, queuejump
, opcallback
, completed
, v
,
887 static void stash_command(disorder_eclient
*c
,
889 operation_callback
*opcallback
,
899 vstash_command(c
, queuejump
, opcallback
, completed
, v
, nbody
, body
, cmd
, ap
);
903 /* Command support ***********************************************************/
905 static const char *errorstring(disorder_eclient
*c
) {
908 byte_xasprintf(&s
, "%s: %s: %d", c
->ident
, c
->line
, c
->rc
);
912 /* for commands with a quoted string response */
913 static void string_response_opcallback(disorder_eclient
*c
,
914 struct operation
*op
) {
915 disorder_eclient_string_response
*completed
916 = (disorder_eclient_string_response
*)op
->completed
;
918 D(("string_response_callback"));
919 if(c
->rc
/ 100 == 2 || c
->rc
== 555) {
922 completed(op
->v
, NULL
, NULL
);
923 else if(c
->protocol
>= 2) {
924 char **rr
= split(c
->line
+ 4, 0, SPLIT_QUOTES
, 0, 0);
927 completed(op
->v
, NULL
, *rr
);
929 /* TODO error message a is bit lame but generally indicates a server
930 * bug rather than anything the user can address */
931 completed(op
->v
, "error parsing response", NULL
);
933 completed(op
->v
, NULL
, c
->line
+ 4);
936 completed(op
->v
, errorstring(c
), NULL
);
939 /* for commands with a simple integer response */
940 static void integer_response_opcallback(disorder_eclient
*c
,
941 struct operation
*op
) {
942 disorder_eclient_integer_response
*completed
943 = (disorder_eclient_integer_response
*)op
->completed
;
945 D(("string_response_callback"));
946 if(c
->rc
/ 100 == 2) {
950 e
= xstrtol(&n
, c
->line
+ 4, 0, 10);
952 completed(op
->v
, strerror(e
), 0);
954 completed(op
->v
, 0, n
);
956 completed(op
->v
, errorstring(c
), 0);
959 /* for commands with no response */
960 static void no_response_opcallback(disorder_eclient
*c
,
961 struct operation
*op
) {
962 disorder_eclient_no_response
*completed
963 = (disorder_eclient_no_response
*)op
->completed
;
965 D(("no_response_callback"));
967 completed(op
->v
, NULL
);
969 completed(op
->v
, errorstring(c
));
972 /* error callback for queue_unmarshall */
973 static void eclient_queue_error(const char *msg
,
975 struct operation
*op
= u
;
977 /* TODO don't use protocol_error here */
978 protocol_error(op
->client
, op
, -1, "error parsing queue entry: %s", msg
);
981 /* for commands that expect a queue dump */
982 static void queue_response_opcallback(disorder_eclient
*c
,
983 struct operation
*op
) {
984 disorder_eclient_queue_response
*const completed
985 = (disorder_eclient_queue_response
*)op
->completed
;
987 int parse_failed
= 0;
988 struct queue_entry
*q
, *qh
= 0, **qtail
= &qh
, *qlast
= 0;
990 D(("queue_response_callback"));
991 if(c
->rc
/ 100 == 2) {
992 /* parse the queue */
993 for(n
= 0; n
< c
->vec
.nvec
; ++n
) {
994 q
= xmalloc(sizeof *q
);
995 D(("queue_unmarshall %s", c
->vec
.vec
[n
]));
996 if(!queue_unmarshall(q
, c
->vec
.vec
[n
], NULL
, op
)) {
1004 /* Currently we pass the partial queue to the callback along with the
1005 * error. This might not be very useful in practice... */
1007 completed(op
->v
, "cannot parse result", qh
);
1009 completed(op
->v
, 0, qh
);
1011 completed(op
->v
, errorstring(c
), 0);
1015 static void playing_response_opcallback(disorder_eclient
*c
,
1016 struct operation
*op
) {
1017 disorder_eclient_queue_response
*const completed
1018 = (disorder_eclient_queue_response
*)op
->completed
;
1019 struct queue_entry
*q
;
1021 D(("playing_response_callback"));
1022 if(c
->rc
/ 100 == 2) {
1023 switch(c
->rc
% 10) {
1025 if(queue_unmarshall(q
= xmalloc(sizeof *q
), c
->line
+ 4,
1027 completed(op
->v
, "cannot parse result", 0);
1029 completed(op
->v
, 0, q
);
1032 completed(op
->v
, 0, 0);
1035 completed(op
->v
, errorstring(c
), 0);
1039 completed(op
->v
, errorstring(c
), 0);
1042 /* for commands that expect a list of some sort */
1043 static void list_response_opcallback(disorder_eclient
*c
,
1044 struct operation
*op
) {
1045 disorder_eclient_list_response
*const completed
=
1046 (disorder_eclient_list_response
*)op
->completed
;
1048 D(("list_response_callback"));
1049 if(c
->rc
/ 100 == 2)
1050 completed(op
->v
, NULL
, c
->vec
.nvec
, c
->vec
.vec
);
1051 else if(c
->rc
== 555)
1052 completed(op
->v
, NULL
, -1, NULL
);
1054 completed(op
->v
, errorstring(c
), 0, 0);
1058 static void volume_response_opcallback(disorder_eclient
*c
,
1059 struct operation
*op
) {
1060 disorder_eclient_volume_response
*completed
1061 = (disorder_eclient_volume_response
*)op
->completed
;
1064 D(("volume_response_callback"));
1065 if(c
->rc
/ 100 == 2) {
1067 if(sscanf(c
->line
+ 4, "%d %d", &l
, &r
) != 2 || l
< 0 || r
< 0)
1068 completed(op
->v
, "cannot parse volume response", 0, 0);
1070 completed(op
->v
, 0, l
, r
);
1073 completed(op
->v
, errorstring(c
), 0, 0);
1076 static int simple(disorder_eclient
*c
,
1077 operation_callback
*opcallback
,
1078 void (*completed
)(),
1080 const char *cmd
, ...) {
1084 vstash_command(c
, 0/*queuejump*/, opcallback
, completed
, v
, -1, 0, cmd
, ap
);
1086 /* Give the state machine a kick, since we might be in state_idle */
1087 disorder_eclient_polled(c
, 0);
1091 static int simple_body(disorder_eclient
*c
,
1092 operation_callback
*opcallback
,
1093 void (*completed
)(),
1097 const char *cmd
, ...) {
1101 vstash_command(c
, 0/*queuejump*/, opcallback
, completed
, v
, nbody
, body
, cmd
, ap
);
1103 /* Give the state machine a kick, since we might be in state_idle */
1104 disorder_eclient_polled(c
, 0);
1108 /* Commands ******************************************************************/
1110 int disorder_eclient_version(disorder_eclient
*c
,
1111 disorder_eclient_string_response
*completed
,
1113 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1114 "version", (char *)0);
1117 int disorder_eclient_namepart(disorder_eclient
*c
,
1118 disorder_eclient_string_response
*completed
,
1120 const char *context
,
1123 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1124 "part", track
, context
, part
, (char *)0);
1127 int disorder_eclient_play(disorder_eclient
*c
,
1129 disorder_eclient_no_response
*completed
,
1131 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1132 "play", track
, (char *)0);
1135 int disorder_eclient_pause(disorder_eclient
*c
,
1136 disorder_eclient_no_response
*completed
,
1138 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1139 "pause", (char *)0);
1142 int disorder_eclient_resume(disorder_eclient
*c
,
1143 disorder_eclient_no_response
*completed
,
1145 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1146 "resume", (char *)0);
1149 int disorder_eclient_scratch(disorder_eclient
*c
,
1151 disorder_eclient_no_response
*completed
,
1153 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1154 "scratch", id
, (char *)0);
1157 int disorder_eclient_scratch_playing(disorder_eclient
*c
,
1158 disorder_eclient_no_response
*completed
,
1160 return disorder_eclient_scratch(c
, 0, completed
, v
);
1163 int disorder_eclient_remove(disorder_eclient
*c
,
1165 disorder_eclient_no_response
*completed
,
1167 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1168 "remove", id
, (char *)0);
1171 int disorder_eclient_moveafter(disorder_eclient
*c
,
1175 disorder_eclient_no_response
*completed
,
1181 vector_append(&vec
, (char *)"moveafter");
1182 vector_append(&vec
, (char *)target
);
1183 for(n
= 0; n
< nids
; ++n
)
1184 vector_append(&vec
, (char *)ids
[n
]);
1185 stash_command_vector(c
, 0/*queuejump*/, no_response_opcallback
, completed
, v
,
1186 -1, 0, vec
.nvec
, vec
.vec
);
1187 disorder_eclient_polled(c
, 0);
1191 int disorder_eclient_recent(disorder_eclient
*c
,
1192 disorder_eclient_queue_response
*completed
,
1194 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1195 "recent", (char *)0);
1198 int disorder_eclient_queue(disorder_eclient
*c
,
1199 disorder_eclient_queue_response
*completed
,
1201 return simple(c
, queue_response_opcallback
, (void (*)())completed
, v
,
1202 "queue", (char *)0);
1205 int disorder_eclient_files(disorder_eclient
*c
,
1206 disorder_eclient_list_response
*completed
,
1210 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1211 "files", dir
, re
, (char *)0);
1214 int disorder_eclient_dirs(disorder_eclient
*c
,
1215 disorder_eclient_list_response
*completed
,
1219 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1220 "dirs", dir
, re
, (char *)0);
1223 int disorder_eclient_playing(disorder_eclient
*c
,
1224 disorder_eclient_queue_response
*completed
,
1226 return simple(c
, playing_response_opcallback
, (void (*)())completed
, v
,
1227 "playing", (char *)0);
1230 int disorder_eclient_length(disorder_eclient
*c
,
1231 disorder_eclient_integer_response
*completed
,
1234 return simple(c
, integer_response_opcallback
, (void (*)())completed
, v
,
1235 "length", track
, (char *)0);
1238 int disorder_eclient_volume(disorder_eclient
*c
,
1239 disorder_eclient_volume_response
*completed
,
1242 char sl
[64], sr
[64];
1244 if(l
< 0 && r
< 0) {
1245 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1246 "volume", (char *)0);
1247 } else if(l
>= 0 && r
>= 0) {
1250 byte_snprintf(sl
, sizeof sl
, "%d", l
);
1251 byte_snprintf(sr
, sizeof sr
, "%d", r
);
1252 return simple(c
, volume_response_opcallback
, (void (*)())completed
, v
,
1253 "volume", sl
, sr
, (char *)0);
1255 assert(!"invalid arguments to disorder_eclient_volume");
1256 return -1; /* gcc is being dim */
1260 int disorder_eclient_enable(disorder_eclient
*c
,
1261 disorder_eclient_no_response
*completed
,
1263 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1264 "enable", (char *)0);
1267 int disorder_eclient_disable(disorder_eclient
*c
,
1268 disorder_eclient_no_response
*completed
,
1270 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1271 "disable", (char *)0);
1274 int disorder_eclient_random_enable(disorder_eclient
*c
,
1275 disorder_eclient_no_response
*completed
,
1277 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1278 "random-enable", (char *)0);
1281 int disorder_eclient_random_disable(disorder_eclient
*c
,
1282 disorder_eclient_no_response
*completed
,
1284 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1285 "random-disable", (char *)0);
1288 int disorder_eclient_get(disorder_eclient
*c
,
1289 disorder_eclient_string_response
*completed
,
1290 const char *track
, const char *pref
,
1292 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1293 "get", track
, pref
, (char *)0);
1296 int disorder_eclient_set(disorder_eclient
*c
,
1297 disorder_eclient_no_response
*completed
,
1298 const char *track
, const char *pref
,
1301 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1302 "set", track
, pref
, value
, (char *)0);
1305 int disorder_eclient_unset(disorder_eclient
*c
,
1306 disorder_eclient_no_response
*completed
,
1307 const char *track
, const char *pref
,
1309 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1310 "unset", track
, pref
, (char *)0);
1313 int disorder_eclient_resolve(disorder_eclient
*c
,
1314 disorder_eclient_string_response
*completed
,
1317 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1318 "resolve", track
, (char *)0);
1321 int disorder_eclient_search(disorder_eclient
*c
,
1322 disorder_eclient_list_response
*completed
,
1325 if(!split(terms
, 0, SPLIT_QUOTES
, 0, 0)) return -1;
1326 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1327 "search", terms
, (char *)0);
1330 int disorder_eclient_nop(disorder_eclient
*c
,
1331 disorder_eclient_no_response
*completed
,
1333 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1337 /** @brief Get the last @p max added tracks
1339 * @param completed Called with list
1340 * @param max Number of tracks to get, 0 for all
1341 * @param v Passed to @p completed
1343 * The first track in the list is the most recently added.
1345 int disorder_eclient_new_tracks(disorder_eclient
*c
,
1346 disorder_eclient_list_response
*completed
,
1351 sprintf(limit
, "%d", max
);
1352 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1353 "new", limit
, (char *)0);
1356 static void rtp_response_opcallback(disorder_eclient
*c
,
1357 struct operation
*op
) {
1358 disorder_eclient_list_response
*const completed
=
1359 (disorder_eclient_list_response
*)op
->completed
;
1360 D(("rtp_response_opcallback"));
1361 if(c
->rc
/ 100 == 2) {
1363 char **vec
= split(c
->line
+ 4, &nvec
, SPLIT_QUOTES
, 0, 0);
1366 completed(op
->v
, NULL
, nvec
, vec
);
1368 completed(op
->v
, "error parsing response", 0, 0);
1370 completed(op
->v
, errorstring(c
), 0, 0);
1373 /** @brief Determine the RTP target address
1375 * @param completed Called with address details
1376 * @param v Passed to @p completed
1378 * The address details will be two elements, the first being the hostname and
1379 * the second the service (port).
1381 int disorder_eclient_rtp_address(disorder_eclient
*c
,
1382 disorder_eclient_list_response
*completed
,
1384 return simple(c
, rtp_response_opcallback
, (void (*)())completed
, v
,
1385 "rtp-address", (char *)0);
1388 /** @brief Get the list of users
1390 * @param completed Called with list of users
1391 * @param v Passed to @p completed
1393 * The user list is not sorted in any particular order.
1395 int disorder_eclient_users(disorder_eclient
*c
,
1396 disorder_eclient_list_response
*completed
,
1398 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1399 "users", (char *)0);
1402 /** @brief Delete a user
1404 * @param completed Called on completion
1405 * @param user User to delete
1406 * @param v Passed to @p completed
1408 int disorder_eclient_deluser(disorder_eclient
*c
,
1409 disorder_eclient_no_response
*completed
,
1412 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1413 "deluser", user
, (char *)0);
1416 /** @brief Get a user property
1418 * @param completed Called on completion
1419 * @param user User to look up
1420 * @param property Property to look up
1421 * @param v Passed to @p completed
1423 int disorder_eclient_userinfo(disorder_eclient
*c
,
1424 disorder_eclient_string_response
*completed
,
1426 const char *property
,
1428 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1429 "userinfo", user
, property
, (char *)0);
1432 /** @brief Modify a user property
1434 * @param completed Called on completion
1435 * @param user User to modify
1436 * @param property Property to modify
1437 * @param value New property value
1438 * @param v Passed to @p completed
1440 int disorder_eclient_edituser(disorder_eclient
*c
,
1441 disorder_eclient_no_response
*completed
,
1443 const char *property
,
1446 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1447 "edituser", user
, property
, value
, (char *)0);
1450 /** @brief Create a new user
1452 * @param completed Called on completion
1453 * @param user User to create
1454 * @param password Initial password
1455 * @param rights Initial rights or NULL
1456 * @param v Passed to @p completed
1458 int disorder_eclient_adduser(disorder_eclient
*c
,
1459 disorder_eclient_no_response
*completed
,
1461 const char *password
,
1464 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1465 "adduser", user
, password
, rights
, (char *)0);
1468 /** @brief Adopt a track
1470 * @param completed Called on completion
1471 * @param id Track ID
1472 * @param v Passed to @p completed
1474 int disorder_eclient_adopt(disorder_eclient
*c
,
1475 disorder_eclient_no_response
*completed
,
1478 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1479 "adopt", id
, (char *)0);
1482 /** @brief Get the list of playlists
1484 * @param completed Called with list of playlists
1485 * @param v Passed to @p completed
1487 * The playlist list is not sorted in any particular order.
1489 int disorder_eclient_playlists(disorder_eclient
*c
,
1490 disorder_eclient_list_response
*completed
,
1492 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1493 "playlists", (char *)0);
1496 /** @brief Delete a playlist
1498 * @param completed Called on completion
1499 * @param playlist Playlist to delete
1500 * @param v Passed to @p completed
1502 int disorder_eclient_playlist_delete(disorder_eclient
*c
,
1503 disorder_eclient_no_response
*completed
,
1504 const char *playlist
,
1506 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1507 "playlist-delete", playlist
, (char *)0);
1510 /** @brief Lock a playlist
1512 * @param completed Called on completion
1513 * @param playlist Playlist to lock
1514 * @param v Passed to @p completed
1516 int disorder_eclient_playlist_lock(disorder_eclient
*c
,
1517 disorder_eclient_no_response
*completed
,
1518 const char *playlist
,
1520 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1521 "playlist-lock", playlist
, (char *)0);
1524 /** @brief Unlock the locked a playlist
1526 * @param completed Called on completion
1527 * @param v Passed to @p completed
1529 int disorder_eclient_playlist_unlock(disorder_eclient
*c
,
1530 disorder_eclient_no_response
*completed
,
1532 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1533 "playlist-unlock", (char *)0);
1536 /** @brief Set a playlist's sharing
1538 * @param completed Called on completion
1539 * @param playlist Playlist to modify
1540 * @param sharing @c "public" or @c "private"
1541 * @param v Passed to @p completed
1543 int disorder_eclient_playlist_set_share(disorder_eclient
*c
,
1544 disorder_eclient_no_response
*completed
,
1545 const char *playlist
,
1546 const char *sharing
,
1548 return simple(c
, no_response_opcallback
, (void (*)())completed
, v
,
1549 "playlist-set-share", playlist
, sharing
, (char *)0);
1552 /** @brief Get a playlist's sharing
1554 * @param completed Called with sharing status
1555 * @param playlist Playlist to inspect
1556 * @param v Passed to @p completed
1558 int disorder_eclient_playlist_get_share(disorder_eclient
*c
,
1559 disorder_eclient_string_response
*completed
,
1560 const char *playlist
,
1562 return simple(c
, string_response_opcallback
, (void (*)())completed
, v
,
1563 "playlist-get-share", playlist
, (char *)0);
1566 /** @brief Set a playlist
1568 * @param completed Called on completion
1569 * @param playlist Playlist to modify
1570 * @param tracks List of tracks
1571 * @param ntracks Number of tracks
1572 * @param v Passed to @p completed
1574 int disorder_eclient_playlist_set(disorder_eclient
*c
,
1575 disorder_eclient_no_response
*completed
,
1576 const char *playlist
,
1580 return simple_body(c
, no_response_opcallback
, (void (*)())completed
, v
,
1582 "playlist-set", playlist
, (char *)0);
1585 /** @brief Get a playlist's contents
1587 * @param completed Called with playlist contents
1588 * @param playlist Playlist to inspect
1589 * @param v Passed to @p completed
1591 int disorder_eclient_playlist_get(disorder_eclient
*c
,
1592 disorder_eclient_list_response
*completed
,
1593 const char *playlist
,
1595 return simple(c
, list_response_opcallback
, (void (*)())completed
, v
,
1596 "playlist-get", playlist
, (char *)0);
1599 /* Log clients ***************************************************************/
1601 /** @brief Monitor the server log
1603 * @param callbacks Functions to call when anything happens
1604 * @param v Passed to @p callbacks functions
1606 * Once a client is being used for logging it cannot be used for anything else.
1607 * There is magic in authuser_opcallback() to re-submit the @c log command
1608 * after reconnection.
1610 * NB that the @c state callback may be called from within this function,
1611 * i.e. not solely later on from the event loop callback.
1613 int disorder_eclient_log(disorder_eclient
*c
,
1614 const disorder_eclient_log_callbacks
*callbacks
,
1616 if(c
->log_callbacks
) return -1;
1617 c
->log_callbacks
= callbacks
;
1619 /* Repoort initial state */
1620 if(c
->log_callbacks
->state
)
1621 c
->log_callbacks
->state(c
->log_v
, c
->statebits
);
1622 stash_command(c
, 0/*queuejump*/, log_opcallback
, 0/*completed*/, v
,
1623 -1, 0, "log", (char *)0);
1624 disorder_eclient_polled(c
, 0);
1628 /* If we get here we've stopped being a log client */
1629 static void log_opcallback(disorder_eclient
*c
,
1630 struct operation
attribute((unused
)) *op
) {
1631 D(("log_opcallback"));
1632 c
->log_callbacks
= 0;
1636 /* error callback for log line parsing */
1637 static void logline_error(const char *msg
, void *u
) {
1638 disorder_eclient
*c
= u
;
1639 /* TODO don't use protocol_error here */
1640 protocol_error(c
, c
->ops
, -1, "error parsing log line: %s", msg
);
1643 /* process a single log line */
1644 static void logline(disorder_eclient
*c
, const char *line
) {
1649 D(("logline [%s]", line
));
1650 vec
= split(line
, &nvec
, SPLIT_QUOTES
, logline_error
, c
);
1651 if(nvec
< 2) return; /* probably an error, already
1653 if(sscanf(vec
[0], "%"SCNxMAX
, &when
) != 1) {
1654 /* probably the wrong side of a format change */
1655 /* TODO don't use protocol_error here */
1656 protocol_error(c
, c
->ops
, -1, "invalid log timestamp '%s'", vec
[0]);
1659 /* TODO: do something with the time */
1660 //fprintf(stderr, "log key: %s\n", vec[1]);
1661 n
= TABLE_FIND(logentry_handlers
, name
, vec
[1]);
1663 //fprintf(stderr, "...not found\n");
1664 return; /* probably a future command */
1668 if(nvec
< logentry_handlers
[n
].min
|| nvec
> logentry_handlers
[n
].max
) {
1669 //fprintf(stderr, "...wrong # args\n");
1672 logentry_handlers
[n
].handler(c
, nvec
, vec
);
1675 static void logentry_completed(disorder_eclient
*c
,
1676 int attribute((unused
)) nvec
, char **vec
) {
1677 c
->statebits
&= ~DISORDER_PLAYING
;
1678 if(c
->log_callbacks
->completed
)
1679 c
->log_callbacks
->completed(c
->log_v
, vec
[0]);
1680 if(c
->log_callbacks
->state
)
1681 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1684 static void logentry_failed(disorder_eclient
*c
,
1685 int attribute((unused
)) nvec
, char **vec
) {
1686 c
->statebits
&= ~DISORDER_PLAYING
;
1687 if(c
->log_callbacks
->failed
)
1688 c
->log_callbacks
->failed(c
->log_v
, vec
[0], vec
[1]);
1689 if(c
->log_callbacks
->state
)
1690 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1693 static void logentry_moved(disorder_eclient
*c
,
1694 int attribute((unused
)) nvec
, char **vec
) {
1695 if(c
->log_callbacks
->moved
)
1696 c
->log_callbacks
->moved(c
->log_v
, vec
[0]);
1699 static void logentry_playing(disorder_eclient
*c
,
1700 int attribute((unused
)) nvec
, char **vec
) {
1701 c
->statebits
|= DISORDER_PLAYING
;
1702 if(c
->log_callbacks
->playing
)
1703 c
->log_callbacks
->playing(c
->log_v
, vec
[0], vec
[1]);
1704 if(c
->log_callbacks
->state
)
1705 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1708 static void logentry_queue(disorder_eclient
*c
,
1709 int attribute((unused
)) nvec
, char **vec
) {
1710 struct queue_entry
*q
;
1712 if(!c
->log_callbacks
->queue
) return;
1713 q
= xmalloc(sizeof *q
);
1714 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1716 c
->log_callbacks
->queue(c
->log_v
, q
);
1719 static void logentry_recent_added(disorder_eclient
*c
,
1720 int attribute((unused
)) nvec
, char **vec
) {
1721 struct queue_entry
*q
;
1723 if(!c
->log_callbacks
->recent_added
) return;
1724 q
= xmalloc(sizeof *q
);
1725 if(queue_unmarshall_vec(q
, nvec
, vec
, eclient_queue_error
, c
))
1727 c
->log_callbacks
->recent_added(c
->log_v
, q
);
1730 static void logentry_recent_removed(disorder_eclient
*c
,
1731 int attribute((unused
)) nvec
, char **vec
) {
1732 if(c
->log_callbacks
->recent_removed
)
1733 c
->log_callbacks
->recent_removed(c
->log_v
, vec
[0]);
1736 static void logentry_removed(disorder_eclient
*c
,
1737 int attribute((unused
)) nvec
, char **vec
) {
1738 if(c
->log_callbacks
->removed
)
1739 c
->log_callbacks
->removed(c
->log_v
, vec
[0], vec
[1]);
1742 static void logentry_rescanned(disorder_eclient
*c
,
1743 int attribute((unused
)) nvec
,
1744 char attribute((unused
)) **vec
) {
1745 if(c
->log_callbacks
->rescanned
)
1746 c
->log_callbacks
->rescanned(c
->log_v
);
1749 static void logentry_scratched(disorder_eclient
*c
,
1750 int attribute((unused
)) nvec
, char **vec
) {
1751 c
->statebits
&= ~DISORDER_PLAYING
;
1752 if(c
->log_callbacks
->scratched
)
1753 c
->log_callbacks
->scratched(c
->log_v
, vec
[0], vec
[1]);
1754 if(c
->log_callbacks
->state
)
1755 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1758 static void logentry_user_add(disorder_eclient
*c
,
1759 int attribute((unused
)) nvec
, char **vec
) {
1760 if(c
->log_callbacks
->user_add
)
1761 c
->log_callbacks
->user_add(c
->log_v
, vec
[0]);
1764 static void logentry_user_confirm(disorder_eclient
*c
,
1765 int attribute((unused
)) nvec
, char **vec
) {
1766 if(c
->log_callbacks
->user_confirm
)
1767 c
->log_callbacks
->user_confirm(c
->log_v
, vec
[0]);
1770 static void logentry_user_delete(disorder_eclient
*c
,
1771 int attribute((unused
)) nvec
, char **vec
) {
1772 if(c
->log_callbacks
->user_delete
)
1773 c
->log_callbacks
->user_delete(c
->log_v
, vec
[0]);
1776 static void logentry_user_edit(disorder_eclient
*c
,
1777 int attribute((unused
)) nvec
, char **vec
) {
1778 if(c
->log_callbacks
->user_edit
)
1779 c
->log_callbacks
->user_edit(c
->log_v
, vec
[0], vec
[1]);
1782 static void logentry_rights_changed(disorder_eclient
*c
,
1783 int attribute((unused
)) nvec
, char **vec
) {
1784 if(c
->log_callbacks
->rights_changed
) {
1786 if(!parse_rights(vec
[0], &r
, 0/*report*/))
1787 c
->log_callbacks
->rights_changed(c
->log_v
, r
);
1791 static void logentry_playlist_created(disorder_eclient
*c
,
1792 int attribute((unused
)) nvec
,
1794 if(c
->log_callbacks
->playlist_created
)
1795 c
->log_callbacks
->playlist_created(c
->log_v
, vec
[0], vec
[1]);
1798 static void logentry_playlist_deleted(disorder_eclient
*c
,
1799 int attribute((unused
)) nvec
,
1801 if(c
->log_callbacks
->playlist_deleted
)
1802 c
->log_callbacks
->playlist_deleted(c
->log_v
, vec
[0]);
1805 static void logentry_playlist_modified(disorder_eclient
*c
,
1806 int attribute((unused
)) nvec
,
1808 if(c
->log_callbacks
->playlist_modified
)
1809 c
->log_callbacks
->playlist_modified(c
->log_v
, vec
[0], vec
[1]);
1812 static const struct {
1815 const char *disable
;
1816 } statestrings
[] = {
1817 { DISORDER_PLAYING_ENABLED
, "enable_play", "disable_play" },
1818 { DISORDER_RANDOM_ENABLED
, "enable_random", "disable_random" },
1819 { DISORDER_TRACK_PAUSED
, "pause", "resume" },
1820 { DISORDER_PLAYING
, "playing", "completed" },
1821 { DISORDER_PLAYING
, 0, "scratched" },
1822 { DISORDER_PLAYING
, 0, "failed" },
1824 #define NSTATES (int)(sizeof statestrings / sizeof *statestrings)
1826 static void logentry_state(disorder_eclient
*c
,
1827 int attribute((unused
)) nvec
, char **vec
) {
1830 for(n
= 0; n
< NSTATES
; ++n
)
1831 if(statestrings
[n
].enable
&& !strcmp(vec
[0], statestrings
[n
].enable
)) {
1832 c
->statebits
|= statestrings
[n
].bit
;
1834 } else if(statestrings
[n
].disable
&& !strcmp(vec
[0], statestrings
[n
].disable
)) {
1835 c
->statebits
&= ~statestrings
[n
].bit
;
1838 if(c
->log_callbacks
->state
)
1839 c
->log_callbacks
->state(c
->log_v
, c
->statebits
| DISORDER_CONNECTED
);
1842 static void logentry_volume(disorder_eclient
*c
,
1843 int attribute((unused
)) nvec
, char **vec
) {
1846 if(!c
->log_callbacks
->volume
) return;
1847 if(xstrtol(&l
, vec
[0], 0, 10)
1848 || xstrtol(&r
, vec
[1], 0, 10)
1849 || l
< 0 || l
> INT_MAX
1850 || r
< 0 || r
> INT_MAX
)
1852 c
->log_callbacks
->volume(c
->log_v
, (int)l
, (int)r
);
1855 /** @brief Convert @p statebits to a string */
1856 char *disorder_eclient_interpret_state(unsigned long statebits
) {
1860 static const struct {
1864 { DISORDER_PLAYING_ENABLED
, "playing_enabled" },
1865 { DISORDER_RANDOM_ENABLED
, "random_enabled" },
1866 { DISORDER_TRACK_PAUSED
, "track_paused" },
1867 { DISORDER_PLAYING
, "playing" },
1868 { DISORDER_CONNECTED
, "connected" },
1870 #define NBITS (sizeof bits / sizeof *bits)
1874 dynstr_append(d
, '0');
1875 for(n
= 0; n
< NBITS
; ++n
)
1876 if(statebits
& bits
[n
].bit
) {
1878 dynstr_append(d
, '|');
1879 dynstr_append_string(d
, bits
[n
].name
);
1880 statebits
^= bits
[n
].bit
;
1886 dynstr_append(d
, '|');
1887 sprintf(s
, "%#lx", statebits
);
1888 dynstr_append_string(d
, s
);
1890 dynstr_terminate(d
);
1894 static void logentry_adopted(disorder_eclient
*c
,
1895 int attribute((unused
)) nvec
, char **vec
) {
1896 if(c
->log_callbacks
->adopted
)
1897 c
->log_callbacks
->adopted(c
->log_v
, vec
[0], vec
[1]);
1905 indent-tabs-mode:nil