2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
25 #include <sys/types.h>
26 #include <sys/socket.h>
39 #include <netinet/in.h>
51 #include "configuration.h"
61 #include "trackname.h"
67 # define NONCE_SIZE 16
70 int volume_left, volume_right; /* last known volume */
84 unsigned char nonce[NONCE_SIZE];
85 ev_reader_callback *reader;
86 struct eventlog_output *lo;
87 const struct listener *l;
90 static int reader_callback(ev_source *ev,
98 static const char *noyes[] = { "no", "yes" };
100 static int writer_error(ev_source attribute((unused)) *ev,
106 D(("server writer_error %d %d", fd, errno_value));
107 if(errno_value == 0) {
111 D(("server writer_error closes %d", fd));
112 xclose(fd); /* reader is done too, close */
114 D(("server writer_error shutdown %d SHUT_WR", fd));
115 xshutdown(fd, SHUT_WR); /* reader is not done yet */
118 if(errno_value != EPIPE)
119 error(errno_value, "S%x write error on socket", c->tag);
121 ev_reader_cancel(c->r);
127 static int reader_error(ev_source attribute((unused)) *ev,
133 D(("server reader_error %d %d", fd, errno_value));
134 error(errno, "S%x read error on socket", c->tag);
135 ev_writer_cancel(c->w);
140 /* return true if we are talking to a trusted user */
141 static int trusted(struct conn *c) {
144 for(n = 0; (n < config->trust.n
145 && strcmp(config->trust.s[n], c->who)); ++n)
147 return n < config->trust.n;
150 static int c_disable(struct conn *c, char **vec, int nvec) {
152 disable_playing(c->who);
153 else if(nvec == 1 && !strcmp(vec[0], "now"))
154 disable_playing(c->who);
156 sink_writes(ev_writer_sink(c->w), "550 invalid argument\n");
157 return 1; /* completed */
159 sink_writes(ev_writer_sink(c->w), "250 OK\n");
160 return 1; /* completed */
163 static int c_enable(struct conn *c,
164 char attribute((unused)) **vec,
165 int attribute((unused)) nvec) {
166 enable_playing(c->who, c->ev);
167 /* Enable implicitly unpauses if there is nothing playing */
168 if(paused && !playing) resume_playing(c->who);
169 sink_writes(ev_writer_sink(c->w), "250 OK\n");
170 return 1; /* completed */
173 static int c_enabled(struct conn *c,
174 char attribute((unused)) **vec,
175 int attribute((unused)) nvec) {
176 sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[playing_is_enabled()]);
177 return 1; /* completed */
180 static int c_play(struct conn *c, char **vec,
181 int attribute((unused)) nvec) {
183 struct queue_entry *q;
185 if(!trackdb_exists(vec[0])) {
186 sink_writes(ev_writer_sink(c->w), "550 track is not in database\n");
189 if(!(track = trackdb_resolve(vec[0]))) {
190 sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n");
193 q = queue_add(track, c->who, WHERE_BEFORE_RANDOM);
195 /* If we added the first track, and something is playing, then prepare the
196 * new track. If nothing is playing then we don't bother as it wouldn't gain
198 if(q == qhead.next && playing)
200 sink_writes(ev_writer_sink(c->w), "250 queued\n");
201 /* If the queue was empty but we are for some reason paused then
203 if(!playing) resume_playing(0);
205 return 1; /* completed */
208 static int c_remove(struct conn *c, char **vec,
209 int attribute((unused)) nvec) {
210 struct queue_entry *q;
212 if(!(q = queue_find(vec[0]))) {
213 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
216 if(config->restrictions & RESTRICT_REMOVE) {
217 /* can only remove tracks that you submitted */
218 if(!q->submitter || strcmp(q->submitter, c->who)) {
219 sink_writes(ev_writer_sink(c->w), "550 you didn't submit that track!\n");
223 queue_remove(q, c->who);
224 /* De-prepare the track. */
226 /* If we removed the random track then add another one. */
227 if(q->state == playing_random)
229 /* Prepare whatever the next head track is. */
230 if(qhead.next != &qhead)
231 prepare(c->ev, qhead.next);
233 sink_writes(ev_writer_sink(c->w), "250 removed\n");
234 return 1; /* completed */
237 static int c_scratch(struct conn *c,
241 sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n");
242 return 1; /* completed */
244 if(config->restrictions & RESTRICT_SCRATCH) {
245 /* can only scratch tracks you submitted and randomly selected ones */
246 if(playing->submitter && strcmp(playing->submitter, c->who)) {
247 sink_writes(ev_writer_sink(c->w), "550 you didn't submit that track!\n");
251 scratch(c->who, nvec == 1 ? vec[0] : 0);
252 /* If you scratch an unpaused track then it is automatically unpaused */
254 sink_writes(ev_writer_sink(c->w), "250 scratched\n");
255 return 1; /* completed */
258 static int c_pause(struct conn *c,
259 char attribute((unused)) **vec,
260 int attribute((unused)) nvec) {
262 sink_writes(ev_writer_sink(c->w), "250 nothing is playing\n");
263 return 1; /* completed */
266 sink_writes(ev_writer_sink(c->w), "250 already paused\n");
267 return 1; /* completed */
269 if(pause_playing(c->who) < 0)
270 sink_writes(ev_writer_sink(c->w), "550 cannot pause this track\n");
272 sink_writes(ev_writer_sink(c->w), "250 paused\n");
276 static int c_resume(struct conn *c,
277 char attribute((unused)) **vec,
278 int attribute((unused)) nvec) {
280 sink_writes(ev_writer_sink(c->w), "250 not paused\n");
281 return 1; /* completed */
283 resume_playing(c->who);
284 sink_writes(ev_writer_sink(c->w), "250 paused\n");
288 static int c_shutdown(struct conn *c,
289 char attribute((unused)) **vec,
290 int attribute((unused)) nvec) {
291 info("S%x shut down by %s", c->tag, c->who);
292 sink_writes(ev_writer_sink(c->w), "250 shutting down\n");
293 ev_writer_flush(c->w);
297 static int c_reconfigure(struct conn *c,
298 char attribute((unused)) **vec,
299 int attribute((unused)) nvec) {
300 info("S%x reconfigure by %s", c->tag, c->who);
301 if(reconfigure(c->ev, 1))
302 sink_writes(ev_writer_sink(c->w), "550 error reading new config\n");
304 sink_writes(ev_writer_sink(c->w), "250 installed new config\n");
305 return 1; /* completed */
308 static int c_rescan(struct conn *c,
309 char attribute((unused)) **vec,
310 int attribute((unused)) nvec) {
311 info("S%x rescan by %s", c->tag, c->who);
312 trackdb_rescan(c->ev);
313 sink_writes(ev_writer_sink(c->w), "250 initiated rescan\n");
314 return 1; /* completed */
317 static int c_version(struct conn *c,
318 char attribute((unused)) **vec,
319 int attribute((unused)) nvec) {
320 /* VERSION had better only use the basic character set */
321 sink_printf(ev_writer_sink(c->w), "251 %s\n", disorder_version_string);
322 return 1; /* completed */
325 static int c_playing(struct conn *c,
326 char attribute((unused)) **vec,
327 int attribute((unused)) nvec) {
329 queue_fix_sofar(playing);
330 playing->expected = 0;
331 sink_printf(ev_writer_sink(c->w), "252 %s\n", queue_marshall(playing));
333 sink_printf(ev_writer_sink(c->w), "259 nothing playing\n");
334 return 1; /* completed */
337 static int c_become(struct conn *c,
339 int attribute((unused)) nvec) {
341 sink_writes(ev_writer_sink(c->w), "230 OK\n");
345 static int c_user(struct conn *c,
347 int attribute((unused)) nvec) {
352 struct sockaddr_in in;
353 struct sockaddr_in6 in6;
359 sink_writes(ev_writer_sink(c->w), "530 already authenticated\n");
362 /* get connection data */
364 if(getpeername(c->fd, &u.sa, &l) < 0) {
365 error(errno, "S%x error calling getpeername", c->tag);
366 sink_writes(ev_writer_sink(c->w), "530 authentication failure\n");
369 if(c->l->pf != PF_UNIX) {
370 if((n = getnameinfo(&u.sa, l,
371 host, sizeof host, 0, 0, NI_NUMERICHOST))) {
372 error(0, "S%x error calling getnameinfo: %s", c->tag, gai_strerror(n));
373 sink_writes(ev_writer_sink(c->w), "530 authentication failure\n");
378 for(n = 0; n < config->allow.n
379 && strcmp(config->allow.s[n].s[0], vec[0]); ++n)
381 /* if it's a real user check whether the response is right */
382 if(n < config->allow.n) {
383 res = authhash(c->nonce, sizeof c->nonce, config->allow.s[n].s[1]);
384 if(res && !strcmp(res, vec[1])) {
386 /* currently we only bother logging remote connections */
387 if(c->l->pf != PF_UNIX)
388 info("S%x %s connected from %s", c->tag, vec[0], host);
389 sink_writes(ev_writer_sink(c->w), "230 OK\n");
393 /* oops, response was wrong */
394 if(c->l->pf != PF_UNIX)
395 info("S%x authentication failure for %s from %s", c->tag, vec[0], host);
397 info("S%x authentication failure for %s", c->tag, vec[0]);
398 sink_writes(ev_writer_sink(c->w), "530 authentication failed\n");
402 static int c_recent(struct conn *c,
403 char attribute((unused)) **vec,
404 int attribute((unused)) nvec) {
405 const struct queue_entry *q;
407 sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n");
408 for(q = phead.next; q != &phead; q = q->next)
409 sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q));
410 sink_writes(ev_writer_sink(c->w), ".\n");
411 return 1; /* completed */
414 static int c_queue(struct conn *c,
415 char attribute((unused)) **vec,
416 int attribute((unused)) nvec) {
417 struct queue_entry *q;
422 sink_writes(ev_writer_sink(c->w), "253 Tracks follow\n");
423 if(playing_is_enabled() && !paused) {
425 queue_fix_sofar(playing);
426 if((l = trackdb_get(playing->track, "_length"))
427 && (length = atol(l))) {
429 when += length - playing->sofar + config->gap;
432 /* Nothing is playing but playing is enabled, so whatever is
433 * first in the queue can be expected to start immediately. */
436 for(q = qhead.next; q != &qhead; q = q->next) {
437 /* fill in estimated start time */
439 sink_printf(ev_writer_sink(c->w), " %s\n", queue_marshall(q));
440 /* update for next track */
442 if((l = trackdb_get(q->track, "_length"))
443 && (length = atol(l)))
444 when += length + config->gap;
449 sink_writes(ev_writer_sink(c->w), ".\n");
450 return 1; /* completed */
453 static int output_list(struct conn *c, char **vec) {
455 sink_printf(ev_writer_sink(c->w), "%s\n", *vec++);
456 sink_writes(ev_writer_sink(c->w), ".\n");
460 static int files_dirs(struct conn *c,
463 enum trackdb_listable what) {
464 const char *dir, *re, *errstr;
470 case 0: dir = 0; re = 0; break;
471 case 1: dir = vec[0]; re = 0; break;
472 case 2: dir = vec[0]; re = vec[1]; break;
475 /* A bit of a bodge to make sure the args don't trample on cache keys */
476 if(dir && strchr(dir, '\n')) {
477 sink_writes(ev_writer_sink(c->w), "550 invalid directory name\n");
480 if(re && strchr(re, '\n')) {
481 sink_writes(ev_writer_sink(c->w), "550 invalid regexp\n");
484 /* We bother eliminating "" because the web interface is relatively
485 * likely to send it */
487 byte_xasprintf(&key, "%d\n%s\n%s", (int)what, dir ? dir : "", re);
488 fvec = (char **)cache_get(&cache_files_type, key);
490 /* Got a cache hit, don't store the answer in the cache */
493 rec = 0; /* quieten compiler */
495 /* Cache miss, we'll do the lookup and key != 0 so we'll store the answer
497 if(!(rec = pcre_compile(re, PCRE_CASELESS|PCRE_UTF8,
498 &errstr, &erroffset, 0))) {
499 sink_printf(ev_writer_sink(c->w), "550 Error compiling regexp: %s\n",
503 /* It only counts as a miss if the regexp was valid. */
504 ++cache_files_misses;
507 /* No regexp, don't bother caching the result */
513 /* No cache hit (either because a miss, or because we did not look) so do
516 fvec = trackdb_list(dir, 0, what, rec);
518 fvec = trackdb_list(0, 0, what, rec);
521 /* Put the answer in the cache */
522 cache_put(&cache_files_type, key, fvec);
523 sink_writes(ev_writer_sink(c->w), "253 Listing follow\n");
524 return output_list(c, fvec);
527 static int c_files(struct conn *c,
530 return files_dirs(c, vec, nvec, trackdb_files);
533 static int c_dirs(struct conn *c,
536 return files_dirs(c, vec, nvec, trackdb_directories);
539 static int c_allfiles(struct conn *c,
542 return files_dirs(c, vec, nvec, trackdb_directories|trackdb_files);
545 static int c_get(struct conn *c,
547 int attribute((unused)) nvec) {
550 if(vec[1][0] != '_' && (v = trackdb_get(vec[0], vec[1])))
551 sink_printf(ev_writer_sink(c->w), "252 %s\n", v);
553 sink_writes(ev_writer_sink(c->w), "550 not found\n");
557 static int c_length(struct conn *c,
559 int attribute((unused)) nvec) {
560 const char *track, *v;
562 if(!(track = trackdb_resolve(vec[0]))) {
563 sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n");
566 if((v = trackdb_get(track, "_length")))
567 sink_printf(ev_writer_sink(c->w), "252 %s\n", v);
569 sink_writes(ev_writer_sink(c->w), "550 not found\n");
573 static int c_set(struct conn *c,
575 int attribute((unused)) nvec) {
576 if(vec[1][0] != '_' && !trackdb_set(vec[0], vec[1], vec[2]))
577 sink_writes(ev_writer_sink(c->w), "250 OK\n");
579 sink_writes(ev_writer_sink(c->w), "550 not found\n");
583 static int c_prefs(struct conn *c,
585 int attribute((unused)) nvec) {
588 k = trackdb_get_all(vec[0]);
589 sink_writes(ev_writer_sink(c->w), "253 prefs follow\n");
590 for(; k; k = k->next)
591 if(k->name[0] != '_') /* omit internal values */
592 sink_printf(ev_writer_sink(c->w),
593 " %s %s\n", quoteutf8(k->name), quoteutf8(k->value));
594 sink_writes(ev_writer_sink(c->w), ".\n");
598 static int c_exists(struct conn *c,
600 int attribute((unused)) nvec) {
601 sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[trackdb_exists(vec[0])]);
605 static void search_parse_error(const char *msg, void *u) {
606 *(const char **)u = msg;
609 static int c_search(struct conn *c,
611 int attribute((unused)) nvec) {
612 char **terms, **results;
613 int nterms, nresults, n;
614 const char *e = "unknown error";
616 /* This is a bit of a bodge. Initially it's there to make the eclient
617 * interface a bit more convenient to add searching to, but it has the more
618 * compelling advantage that if everything uses it, then interpretation of
619 * user-supplied search strings will be the same everywhere. */
620 if(!(terms = split(vec[0], &nterms, SPLIT_QUOTES, search_parse_error, &e))) {
621 sink_printf(ev_writer_sink(c->w), "550 %s\n", e);
623 results = trackdb_search(terms, nterms, &nresults);
624 sink_printf(ev_writer_sink(c->w), "253 %d matches\n", nresults);
625 for(n = 0; n < nresults; ++n)
626 sink_printf(ev_writer_sink(c->w), "%s\n", results[n]);
627 sink_writes(ev_writer_sink(c->w), ".\n");
632 static int c_random_enable(struct conn *c,
633 char attribute((unused)) **vec,
634 int attribute((unused)) nvec) {
635 enable_random(c->who, c->ev);
636 /* Enable implicitly unpauses if there is nothing playing */
637 if(paused && !playing) resume_playing(c->who);
638 sink_writes(ev_writer_sink(c->w), "250 OK\n");
639 return 1; /* completed */
642 static int c_random_disable(struct conn *c,
643 char attribute((unused)) **vec,
644 int attribute((unused)) nvec) {
645 disable_random(c->who);
646 sink_writes(ev_writer_sink(c->w), "250 OK\n");
647 return 1; /* completed */
650 static int c_random_enabled(struct conn *c,
651 char attribute((unused)) **vec,
652 int attribute((unused)) nvec) {
653 sink_printf(ev_writer_sink(c->w), "252 %s\n", noyes[random_is_enabled()]);
654 return 1; /* completed */
657 static int c_stats(struct conn *c,
658 char attribute((unused)) **vec,
659 int attribute((unused)) nvec) {
663 v = trackdb_stats(&nv);
664 sink_printf(ev_writer_sink(c->w), "253 stats\n");
665 for(n = 0; n < nv; ++n) {
667 sink_writes(ev_writer_sink(c->w), ".");
668 sink_printf(ev_writer_sink(c->w), "%s\n", v[n]);
670 sink_writes(ev_writer_sink(c->w), ".\n");
674 static int c_volume(struct conn *c,
685 l = r = atoi(vec[0]);
696 if(mixer_control(&l, &r, set))
697 sink_writes(ev_writer_sink(c->w), "550 error accessing mixer\n");
699 sink_printf(ev_writer_sink(c->w), "252 %d %d\n", l, r);
700 if(l != volume_left || r != volume_right) {
703 snprintf(lb, sizeof lb, "%d", l);
704 snprintf(rb, sizeof rb, "%d", r);
705 eventlog("volume", lb, rb, (char *)0);
711 /* we are logging, and some data is available to read */
712 static int logging_reader_callback(ev_source *ev,
721 /* don't log to this conn any more */
722 eventlog_remove(c->lo);
723 /* terminate the log output */
724 sink_writes(ev_writer_sink(c->w), ".\n");
725 /* restore the reader callback */
726 c->reader = reader_callback;
727 /* ...and exit via it */
728 return c->reader(ev, reader, fd, ptr, bytes, eof, u);
731 static void logclient(const char *msg, void *user) {
732 struct conn *c = user;
734 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" %s\n",
735 (uintmax_t)time(0), msg);
738 static int c_log(struct conn *c,
739 char attribute((unused)) **vec,
740 int attribute((unused)) nvec) {
743 sink_writes(ev_writer_sink(c->w), "254 OK\n");
744 /* pump out initial state */
746 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
748 playing_is_enabled() ? "enable_play" : "disable_play");
749 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
751 random_is_enabled() ? "enable_random" : "disable_random");
752 sink_printf(ev_writer_sink(c->w), "%"PRIxMAX" state %s\n",
754 paused ? "pause" : "resume");
755 c->lo = xmalloc(sizeof *c->lo);
756 c->lo->fn = logclient;
759 c->reader = logging_reader_callback;
763 static void post_move_cleanup(void) {
764 struct queue_entry *q;
766 /* If we have caused the random track to not be at the end then we make it no
767 * longer be random. */
768 for(q = qhead.next; q != &qhead; q = q->next)
769 if(q->state == playing_random && q->next != &qhead)
770 q->state = playing_unplayed;
771 /* That might mean we need to add a new random track. */
776 static int c_move(struct conn *c,
778 int attribute((unused)) nvec) {
779 struct queue_entry *q;
782 if(config->restrictions & RESTRICT_MOVE) {
784 sink_writes(ev_writer_sink(c->w),
785 "550 only trusted users can move tracks\n");
789 if(!(q = queue_find(vec[0]))) {
790 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
793 n = queue_move(q, atoi(vec[1]), c->who);
795 sink_printf(ev_writer_sink(c->w), "252 %d\n", n);
796 /* If we've moved to the head of the queue then prepare the track. */
802 static int c_moveafter(struct conn *c,
804 int attribute((unused)) nvec) {
805 struct queue_entry *q, **qs;
808 if(config->restrictions & RESTRICT_MOVE) {
810 sink_writes(ev_writer_sink(c->w),
811 "550 only trusted users can move tracks\n");
816 if(!(q = queue_find(vec[0]))) {
817 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
824 qs = xcalloc(nvec, sizeof *qs);
825 for(n = 0; n < nvec; ++n)
826 if(!(qs[n] = queue_find(vec[n]))) {
827 sink_writes(ev_writer_sink(c->w), "550 no such track on the queue\n");
830 queue_moveafter(q, nvec, qs, c->who);
832 sink_printf(ev_writer_sink(c->w), "250 Moved tracks\n");
833 /* If we've moved to the head of the queue then prepare the track. */
839 static int c_part(struct conn *c,
841 int attribute((unused)) nvec) {
842 sink_printf(ev_writer_sink(c->w), "252 %s\n",
843 trackdb_getpart(vec[0], vec[1], vec[2]));
847 static int c_resolve(struct conn *c,
849 int attribute((unused)) nvec) {
852 if(!(track = trackdb_resolve(vec[0]))) {
853 sink_writes(ev_writer_sink(c->w), "550 cannot resolve track\n");
856 sink_printf(ev_writer_sink(c->w), "252 %s\n", track);
860 static int c_tags(struct conn *c,
861 char attribute((unused)) **vec,
862 int attribute((unused)) nvec) {
863 char **tags = trackdb_alltags();
865 sink_printf(ev_writer_sink(c->w), "253 Tag list follows\n");
867 sink_printf(ev_writer_sink(c->w), "%s%s\n",
868 **tags == '.' ? "." : "", *tags);
871 sink_writes(ev_writer_sink(c->w), ".\n");
872 return 1; /* completed */
876 static int c_set_global(struct conn *c,
878 int attribute((unused)) nvec) {
879 trackdb_set_global(vec[0], vec[1], c->who);
880 sink_printf(ev_writer_sink(c->w), "250 OK\n");
884 static int c_get_global(struct conn *c,
886 int attribute((unused)) nvec) {
887 const char *s = trackdb_get_global(vec[0]);
890 sink_printf(ev_writer_sink(c->w), "252 %s\n", s);
892 sink_writes(ev_writer_sink(c->w), "550 not found\n");
896 #define C_AUTH 0001 /* must be authenticated */
897 #define C_TRUSTED 0002 /* must be trusted user */
899 static const struct command {
901 int minargs, maxargs;
902 int (*fn)(struct conn *, char **, int);
905 { "allfiles", 0, 2, c_allfiles, C_AUTH },
906 { "become", 1, 1, c_become, C_AUTH|C_TRUSTED },
907 { "dirs", 0, 2, c_dirs, C_AUTH },
908 { "disable", 0, 1, c_disable, C_AUTH },
909 { "enable", 0, 0, c_enable, C_AUTH },
910 { "enabled", 0, 0, c_enabled, C_AUTH },
911 { "exists", 1, 1, c_exists, C_AUTH },
912 { "files", 0, 2, c_files, C_AUTH },
913 { "get", 2, 2, c_get, C_AUTH },
914 { "get-global", 1, 1, c_get_global, C_AUTH },
915 { "length", 1, 1, c_length, C_AUTH },
916 { "log", 0, 0, c_log, C_AUTH },
917 { "move", 2, 2, c_move, C_AUTH },
918 { "moveafter", 1, INT_MAX, c_moveafter, C_AUTH },
919 { "part", 3, 3, c_part, C_AUTH },
920 { "pause", 0, 0, c_pause, C_AUTH },
921 { "play", 1, 1, c_play, C_AUTH },
922 { "playing", 0, 0, c_playing, C_AUTH },
923 { "prefs", 1, 1, c_prefs, C_AUTH },
924 { "queue", 0, 0, c_queue, C_AUTH },
925 { "random-disable", 0, 0, c_random_disable, C_AUTH },
926 { "random-enable", 0, 0, c_random_enable, C_AUTH },
927 { "random-enabled", 0, 0, c_random_enabled, C_AUTH },
928 { "recent", 0, 0, c_recent, C_AUTH },
929 { "reconfigure", 0, 0, c_reconfigure, C_AUTH|C_TRUSTED },
930 { "remove", 1, 1, c_remove, C_AUTH },
931 { "rescan", 0, 0, c_rescan, C_AUTH|C_TRUSTED },
932 { "resolve", 1, 1, c_resolve, C_AUTH },
933 { "resume", 0, 0, c_resume, C_AUTH },
934 { "scratch", 0, 1, c_scratch, C_AUTH },
935 { "search", 1, 1, c_search, C_AUTH },
936 { "set", 3, 3, c_set, C_AUTH, },
937 { "set-global", 2, 2, c_set_global, C_AUTH },
938 { "shutdown", 0, 0, c_shutdown, C_AUTH|C_TRUSTED },
939 { "stats", 0, 0, c_stats, C_AUTH },
940 { "tags", 0, 0, c_tags, C_AUTH },
941 { "unset", 2, 2, c_set, C_AUTH },
942 { "unset-global", 1, 1, c_set_global, C_AUTH },
943 { "user", 2, 2, c_user, 0 },
944 { "version", 0, 0, c_version, C_AUTH },
945 { "volume", 0, 2, c_volume, C_AUTH }
948 static void command_error(const char *msg, void *u) {
951 sink_printf(ev_writer_sink(c->w), "500 parse error: %s\n", msg);
954 /* process a command. Return 1 if complete, 0 if incomplete. */
955 static int command(struct conn *c, char *line) {
959 D(("server command %s", line));
960 if(!(vec = split(line, &nvec, SPLIT_QUOTES, command_error, c))) {
961 sink_writes(ev_writer_sink(c->w), "500 cannot parse command\n");
965 sink_writes(ev_writer_sink(c->w), "500 do what?\n");
968 if((n = TABLE_FIND(commands, struct command, name, vec[0])) < 0)
969 sink_writes(ev_writer_sink(c->w), "500 unknown command\n");
971 if((commands[n].flags & C_AUTH) && !c->who) {
972 sink_writes(ev_writer_sink(c->w), "530 not authenticated\n");
975 if((commands[n].flags & C_TRUSTED) && !trusted(c)) {
976 sink_writes(ev_writer_sink(c->w), "530 insufficient privilege\n");
981 if(nvec < commands[n].minargs) {
982 sink_writes(ev_writer_sink(c->w), "500 missing argument(s)\n");
985 if(nvec > commands[n].maxargs) {
986 sink_writes(ev_writer_sink(c->w), "500 too many arguments\n");
989 return commands[n].fn(c, vec, nvec);
991 return 1; /* completed */
994 /* redirect to the right reader callback for our current state */
995 static int redirect_reader_callback(ev_source *ev,
1004 return c->reader(ev, reader, fd, ptr, bytes, eof, u);
1007 /* the main command reader */
1008 static int reader_callback(ev_source attribute((unused)) *ev,
1010 int attribute((unused)) fd,
1019 D(("server reader_callback"));
1020 while((eol = memchr(ptr, '\n', bytes))) {
1022 ev_reader_consume(reader, eol - (char *)ptr);
1023 complete = command(c, ptr);
1024 bytes -= (eol - (char *)ptr);
1027 /* the command had better have set a new reader callback */
1029 /* there are further bytes to read, or we are at eof; arrange for the
1030 * command's reader callback to handle them */
1031 return ev_reader_incomplete(reader);
1032 /* nothing's going on right now */
1035 /* command completed, we can go around and handle the next one */
1039 error(0, "S%x unterminated line", c->tag);
1041 return ev_writer_close(c->w);
1046 static int listen_callback(ev_source *ev,
1048 const struct sockaddr attribute((unused)) *remote,
1049 socklen_t attribute((unused)) rlen,
1051 const struct listener *l = u;
1052 struct conn *c = xmalloc(sizeof *c);
1053 static unsigned tags;
1055 D(("server listen_callback fd %d (%s)", fd, l->name));
1060 c->w = ev_writer_new(ev, fd, writer_error, c);
1061 c->r = ev_reader_new(ev, fd, redirect_reader_callback, reader_error, c);
1063 c->reader = reader_callback;
1065 gcry_randomize(c->nonce, sizeof c->nonce, GCRY_STRONG_RANDOM);
1066 sink_printf(ev_writer_sink(c->w), "231 %s\n", hex(c->nonce, sizeof c->nonce));
1070 int server_start(ev_source *ev, int pf,
1071 size_t socklen, const struct sockaddr *sa,
1074 struct listener *l = xmalloc(sizeof *l);
1075 static const int one = 1;
1077 D(("server_init socket %s", name));
1078 fd = xsocket(pf, SOCK_STREAM, 0);
1079 xsetsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one);
1080 if(bind(fd, sa, socklen) < 0) {
1081 error(errno, "error binding to %s", name);
1089 if(ev_listen(ev, fd, listen_callback, l)) exit(EXIT_FAILURE);
1093 int server_stop(ev_source *ev, int fd) {
1095 return ev_listen_cancel(ev, fd);