2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 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
26 #include <sys/types.h>
27 #include <sys/socket.h>
43 #include "configuration.h"
54 #include "trackname.h"
63 static void expand(cgi_sink *output,
66 static void expandstring(cgi_sink *output,
76 static const char *nonce(void) {
77 static unsigned long count;
80 byte_xasprintf(&s, "%lx%lx%lx",
81 (unsigned long)time(0),
82 (unsigned long)getpid(),
87 static int compare_entry(const void *a, const void *b) {
88 const struct entry *ea = a, *eb = b;
90 return compare_tracks(ea->sort, eb->sort,
91 ea->display, eb->display,
95 static const char *front_url(void) {
99 /* preserve management interface visibility */
100 if((mgmt = cgi_get("mgmt")) && !strcmp(mgmt, "true")) {
101 byte_xasprintf(&url, "%s?mgmt=true", config->url);
107 static void header_cookie(struct sink *output) {
111 memset(&u, 0, sizeof u);
113 parse_url(config->url, &u);
115 dynstr_append_string(d, "disorder=");
116 dynstr_append_string(d, quote822(login_cookie, 0));
118 /* Force browser to discard cookie */
119 dynstr_append_string(d, "disorder=none;Max-Age=0");
122 /* The default domain matches the request host, so we need not override
123 * that. But the default path only goes up to the rightmost /, which would
124 * cause the browser to expose the cookie to other CGI programs on the same
126 dynstr_append_string(d, ";Path=");
127 dynstr_append_string(d, quote822(u.path, 0));
130 cgi_header(output, "Set-Cookie", d->vec);
133 static void redirect(struct sink *output) {
136 back = cgi_get("back");
137 cgi_header(output, "Location", back && *back ? back : front_url());
138 header_cookie(output);
142 static void expand_template(dcgi_state *ds, cgi_sink *output,
143 const char *action) {
144 cgi_header(output->sink, "Content-Type", "text/html");
145 header_cookie(output->sink);
146 cgi_body(output->sink);
147 expand(output, action, ds);
150 static void lookups(dcgi_state *ds, unsigned want) {
152 struct queue_entry *r, *rnext;
153 const char *dir, *re;
156 if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) {
158 disorder_queue(ds->g->client, &ds->g->queue);
159 if(need & DC_PLAYING)
160 disorder_playing(ds->g->client, &ds->g->playing);
162 disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0);
163 if(need & DC_RECENT) {
164 /* we need to reverse the order of the list */
165 disorder_recent(ds->g->client, &r);
168 r->next = ds->g->recent;
174 disorder_get_volume(ds->g->client,
175 &ds->g->volume_left, &ds->g->volume_right);
176 if(need & (DC_FILES|DC_DIRS)) {
177 if(!(dir = cgi_get("directory")))
179 re = cgi_get("regexp");
181 if(disorder_directories(ds->g->client, dir, re,
182 &ds->g->dirs, &ds->g->ndirs))
185 if(disorder_files(ds->g->client, dir, re,
186 &ds->g->files, &ds->g->nfiles))
189 if(need & DC_RIGHTS) {
190 ds->g->rights = RIGHT_READ; /* fail-safe */
191 if(!disorder_userinfo(ds->g->client, disorder_user(ds->g->client),
193 parse_rights(rights, &ds->g->rights, 1);
195 ds->g->flags |= need;
199 /* actions ********************************************************************/
201 static void act_disable(cgi_sink *output,
204 disorder_disable(ds->g->client);
205 redirect(output->sink);
208 static void act_enable(cgi_sink *output,
211 disorder_enable(ds->g->client);
212 redirect(output->sink);
215 static void act_random_disable(cgi_sink *output,
218 disorder_random_disable(ds->g->client);
219 redirect(output->sink);
222 static void act_random_enable(cgi_sink *output,
225 disorder_random_enable(ds->g->client);
226 redirect(output->sink);
229 static void act_remove(cgi_sink *output,
233 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
235 disorder_remove(ds->g->client, id);
236 redirect(output->sink);
239 static void act_move(cgi_sink *output,
241 const char *id, *delta;
243 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
244 if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument");
246 disorder_move(ds->g->client, id, atoi(delta));
247 redirect(output->sink);
250 static void act_scratch(cgi_sink *output,
253 disorder_scratch(ds->g->client, cgi_get("id"));
254 redirect(output->sink);
257 static void act_playing(cgi_sink *output, dcgi_state *ds) {
259 long refresh = config->refresh, length;
261 int random_enabled = 0;
264 lookups(ds, DC_PLAYING|DC_QUEUE);
265 cgi_header(output->sink, "Content-Type", "text/html");
266 disorder_random_enabled(ds->g->client, &random_enabled);
267 disorder_enabled(ds->g->client, &enabled);
269 && ds->g->playing->state == playing_started /* i.e. not paused */
270 && !disorder_length(ds->g->client, ds->g->playing->track, &length)
272 && ds->g->playing->sofar >= 0) {
273 /* Try to put the next refresh at the start of the next track. */
275 fin = now + length - ds->g->playing->sofar + config->gap;
276 if(now + refresh > fin)
279 if(ds->g->queue && ds->g->queue->state == playing_isscratch) {
280 /* next track is a scratch, don't leave more than the inter-track gap */
281 if(refresh > config->gap)
282 refresh = config->gap;
284 if(!ds->g->playing && ((ds->g->queue
285 && ds->g->queue->state != playing_random)
286 || random_enabled) && enabled) {
287 /* no track playing but playing is enabled and there is something coming
288 * up, must be in a gap */
289 if(refresh > config->gap)
290 refresh = config->gap;
292 byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1,
294 cgi_header(output->sink, "Refresh", r);
295 header_cookie(output->sink);
296 cgi_body(output->sink);
297 expand(output, "playing", ds);
300 static void act_play(cgi_sink *output,
302 const char *track, *dir;
307 if((track = cgi_get("file"))) {
308 disorder_play(ds->g->client, track);
309 } else if((dir = cgi_get("directory"))) {
310 if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0;
312 e = xmalloc(ntracks * sizeof (struct entry));
313 for(n = 0; n < ntracks; ++n) {
314 e[n].path = tracks[n];
315 e[n].sort = trackname_transform("track", tracks[n], "sort");
316 e[n].display = trackname_transform("track", tracks[n], "display");
318 qsort(e, ntracks, sizeof (struct entry), compare_entry);
319 for(n = 0; n < ntracks; ++n)
320 disorder_play(ds->g->client, e[n].path);
323 /* XXX error handling */
324 redirect(output->sink);
327 static int clamp(int n, int min, int max) {
335 static const char *volume_url(void) {
338 byte_xasprintf(&url, "%s?action=volume", config->url);
342 static void act_volume(cgi_sink *output, dcgi_state *ds) {
343 const char *l, *r, *d, *back;
344 int nd, changed = 0;;
346 if((d = cgi_get("delta"))) {
347 lookups(ds, DC_VOLUME);
348 nd = clamp(atoi(d), -255, 255);
349 disorder_set_volume(ds->g->client,
350 clamp(ds->g->volume_left + nd, 0, 255),
351 clamp(ds->g->volume_right + nd, 0, 255));
353 } else if((l = cgi_get("left")) && (r = cgi_get("right"))) {
354 disorder_set_volume(ds->g->client, atoi(l), atoi(r));
358 /* redirect back to ourselves (but without the volume-changing bits in the
360 cgi_header(output->sink, "Location",
361 (back = cgi_get("back")) ? back : volume_url());
362 header_cookie(output->sink);
363 cgi_body(output->sink);
365 cgi_header(output->sink, "Content-Type", "text/html");
366 header_cookie(output->sink);
367 cgi_body(output->sink);
368 expand(output, "volume", ds);
372 static void act_prefs_errors(const char *msg,
373 void attribute((unused)) *u) {
374 fatal(0, "error splitting parts list: %s", msg);
377 static const char *numbered_arg(const char *argname, int numfile) {
380 byte_xasprintf(&fullname, "%d_%s", numfile, argname);
381 return cgi_get(fullname);
384 static void process_prefs(dcgi_state *ds, int numfile) {
385 const char *file, *name, *value, *part, *parts, *current, *context;
388 if(!(file = numbered_arg("file", numfile)))
389 /* The first file doesn't need numbering. */
390 if(numfile > 0 || !(file = cgi_get("file")))
392 if((parts = numbered_arg("parts", numfile))
393 || (parts = cgi_get("parts"))) {
394 /* Default context is display. Other contexts not actually tested. */
395 if(!(context = numbered_arg("context", numfile))) context = "display";
396 partslist = split(parts, 0, 0, act_prefs_errors, 0);
397 while((part = *partslist++)) {
398 if(!(value = numbered_arg(part, numfile)))
400 /* If it's already right (whether regexps or db) don't change anything,
401 * so we don't fill the database up with rubbish. */
402 if(disorder_part(ds->g->client, (char **)¤t,
403 file, context, part))
404 fatal(0, "disorder_part() failed");
405 if(!strcmp(current, value))
407 byte_xasprintf((char **)&name, "trackname_%s_%s", context, part);
408 disorder_set(ds->g->client, file, name, value);
410 if((value = numbered_arg("random", numfile)))
411 disorder_unset(ds->g->client, file, "pick_at_random");
413 disorder_set(ds->g->client, file, "pick_at_random", "0");
414 if((value = numbered_arg("tags", numfile)))
415 disorder_set(ds->g->client, file, "tags", value);
416 } else if((name = cgi_get("name"))) {
417 /* Raw preferences. Not well supported in the templates at the moment. */
418 value = cgi_get("value");
420 disorder_set(ds->g->client, file, name, value);
422 disorder_unset(ds->g->client, file, name);
426 static void act_prefs(cgi_sink *output, dcgi_state *ds) {
430 if((files = cgi_get("files"))) nfiles = atoi(files);
432 for(numfile = 0; numfile < nfiles; ++numfile)
433 process_prefs(ds, numfile);
434 cgi_header(output->sink, "Content-Type", "text/html");
435 header_cookie(output->sink);
436 cgi_body(output->sink);
437 expand(output, "prefs", ds);
440 static void act_pause(cgi_sink *output,
443 disorder_pause(ds->g->client);
444 redirect(output->sink);
447 static void act_resume(cgi_sink *output,
450 disorder_resume(ds->g->client);
451 redirect(output->sink);
454 static void act_login(cgi_sink *output,
456 const char *username, *password, *back;
459 username = cgi_get("username");
460 password = cgi_get("password");
461 if(!username || !password
462 || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) {
463 /* We're just visiting the login page */
464 expand_template(ds, output, "login");
467 /* We'll need a new connection as we are going to stop being guest */
469 if(disorder_connect_user(c, username, password)) {
470 cgi_set_option("error", "loginfailed");
471 expand_template(ds, output, "login");
474 if(disorder_make_cookie(c, &login_cookie)) {
475 cgi_set_option("error", "cookiefailed");
476 expand_template(ds, output, "login");
479 /* Use the new connection henceforth */
482 /* We have a new cookie */
483 header_cookie(output->sink);
484 cgi_set_option("status", "loginok");
485 if((back = cgi_get("back")) && *back)
486 /* Redirect back to somewhere or other */
487 redirect(output->sink);
489 /* Stick to the login page */
490 expand_template(ds, output, "login");
493 static void act_logout(cgi_sink *output,
495 disorder_revoke(ds->g->client);
497 /* Reconnect as guest */
498 disorder_cgi_login(ds, output);
499 /* Back to the login page */
500 cgi_set_option("status", "logoutok");
501 expand_template(ds, output, "login");
504 static void act_register(cgi_sink *output,
506 const char *username, *password, *email;
507 char *confirm, *content_type;
508 const char *text, *encoding, *charset;
510 username = cgi_get("username");
511 password = cgi_get("password");
512 email = cgi_get("email");
514 if(!username || !*username) {
515 cgi_set_option("error", "nousername");
516 expand_template(ds, output, "login");
519 if(!password || !*password) {
520 cgi_set_option("error", "nopassword");
521 expand_template(ds, output, "login");
524 if(!email || !*email) {
525 cgi_set_option("error", "noemail");
526 expand_template(ds, output, "login");
529 /* We could well do better address validation but for now we'll just do the
531 if(!strchr(email, '@')) {
532 cgi_set_option("error", "bademail");
533 expand_template(ds, output, "login");
536 if(disorder_register(ds->g->client, username, password, email, &confirm)) {
537 cgi_set_option("error", "cannotregister");
538 expand_template(ds, output, "login");
541 /* Send the user a mail */
542 /* TODO templatize this */
543 byte_xasprintf((char **)&text,
544 "Welcome to DisOrder. To active your login, please visit this URL:\n"
546 "%s?c=%s\n", config->url, urlencodestring(confirm));
547 if(!(text = mime_encode_text(text, &charset, &encoding)))
548 fatal(0, "cannot encode email");
549 byte_xasprintf(&content_type, "text/plain;charset=%s",
550 quote822(charset, 0));
551 sendmail("", config->mail_sender, email, "Welcome to DisOrder",
552 encoding, content_type, text); /* TODO error checking */
553 /* We'll go back to the login page with a suitable message */
554 cgi_set_option("status", "registered");
555 expand_template(ds, output, "login");
558 static void act_confirm(cgi_sink *output,
560 const char *confirmation;
562 if(!(confirmation = cgi_get("c"))) {
563 cgi_set_option("error", "noconfirm");
564 expand_template(ds, output, "login");
566 if(disorder_confirm(ds->g->client, confirmation)) {
567 cgi_set_option("error", "badconfirm");
568 expand_template(ds, output, "login");
570 cgi_set_option("status", "confirmed");
571 expand_template(ds, output, "login");
574 static const struct action {
576 void (*handler)(cgi_sink *output, dcgi_state *ds);
578 { "confirm", act_confirm },
579 { "disable", act_disable },
580 { "enable", act_enable },
581 { "login", act_login },
582 { "logout", act_logout },
583 { "move", act_move },
584 { "pause", act_pause },
585 { "play", act_play },
586 { "playing", act_playing },
587 { "prefs", act_prefs },
588 { "random-disable", act_random_disable },
589 { "random-enable", act_random_enable },
590 { "register", act_register },
591 { "remove", act_remove },
592 { "resume", act_resume },
593 { "scratch", act_scratch },
594 { "volume", act_volume },
597 /* expansions *****************************************************************/
599 static void exp_include(int attribute((unused)) nargs,
603 expand(output, args[0], u);
606 static void exp_server_version(int attribute((unused)) nargs,
607 char attribute((unused)) **args,
614 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
616 v = "(server not running)";
617 cgi_output(output, "%s", v);
620 static void exp_version(int attribute((unused)) nargs,
621 char attribute((unused)) **args,
623 void attribute((unused)) *u) {
624 cgi_output(output, "%s", disorder_short_version_string);
627 static void exp_nonce(int attribute((unused)) nargs,
628 char attribute((unused)) **args,
630 void attribute((unused)) *u) {
631 cgi_output(output, "%s", nonce());
634 static void exp_label(int attribute((unused)) nargs,
637 void attribute((unused)) *u) {
638 cgi_output(output, "%s", cgi_label(args[0]));
641 struct trackinfo_state {
643 const struct queue_entry *q;
648 static void exp_who(int attribute((unused)) nargs,
649 char attribute((unused)) **args,
654 if(ds->track && ds->track->submitter)
655 cgi_output(output, "%s", ds->track->submitter);
658 static void exp_length(int attribute((unused)) nargs,
659 char attribute((unused)) **args,
666 && (ds->track->state == playing_started
667 || ds->track->state == playing_paused)
668 && ds->track->sofar >= 0)
669 cgi_output(output, "%ld:%02ld/",
670 ds->track->sofar / 60, ds->track->sofar % 60);
673 disorder_length(ds->g->client, ds->track->track, &length);
675 disorder_length(ds->g->client, ds->tracks[0], &length);
677 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
679 sink_printf(output->sink, "%s", " ");
682 static void exp_when(int attribute((unused)) nargs,
683 char attribute((unused)) **args,
687 const struct tm *w = 0;
690 switch(ds->track->state) {
691 case playing_isscratch:
692 case playing_unplayed:
694 if(ds->track->expected)
695 w = localtime(&ds->track->expected);
698 case playing_no_player:
700 case playing_scratched:
701 case playing_started:
703 case playing_quitting:
704 if(ds->track->played)
705 w = localtime(&ds->track->played);
709 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
711 sink_printf(output->sink, " ");
714 static void exp_part(int nargs,
719 const char *s, *track, *part, *context;
725 track = ds->track->track;
727 track = ds->tracks[0];
745 if(disorder_part(ds->g->client, (char **)&s, track,
746 !strcmp(context, "short") ? "display" : context, part))
747 fatal(0, "disorder_part() failed");
748 if(!strcmp(context, "short"))
749 s = truncate_for_display(s, config->short_display);
750 cgi_output(output, "%s", s);
752 sink_printf(output->sink, " ");
755 static void exp_playing(int attribute((unused)) nargs,
762 lookups(ds, DC_PLAYING);
763 memset(&s, 0, sizeof s);
766 s.track = ds->g->playing;
767 expandstring(output, args[0], &s);
771 static void exp_queue(int attribute((unused)) nargs,
777 struct queue_entry *q;
779 lookups(ds, DC_QUEUE);
780 memset(&s, 0, sizeof s);
783 for(q = ds->g->queue; q; q = q->next) {
786 expandstring(output, args[0], &s);
792 static void exp_recent(int attribute((unused)) nargs,
798 struct queue_entry *q;
800 lookups(ds, DC_RECENT);
801 memset(&s, 0, sizeof s);
804 for(q = ds->g->recent; q; q = q->next) {
807 expandstring(output, args[0], &s);
813 static void exp_new(int attribute((unused)) nargs,
821 memset(&s, 0, sizeof s);
824 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
825 s.last = s.index + 1 < ds->g->nnew;
826 s.tracks = &ds->g->new[s.index];
827 expandstring(output, args[0], &s);
832 static void exp_url(int attribute((unused)) nargs,
833 char attribute((unused)) **args,
835 void attribute((unused)) *u) {
836 cgi_output(output, "%s", config->url);
844 static int compare_result(const void *a, const void *b) {
845 const struct result *ra = a, *rb = b;
848 if(!(c = strcmp(ra->sort, rb->sort)))
849 c = strcmp(ra->track, rb->track);
853 static void exp_search(int nargs,
857 dcgi_state *ds = u, substate;
859 const char *q, *context, *part, *template;
875 assert(!"should never happen");
876 part = context = template = 0; /* quieten compiler */
878 if(ds->tracks == 0) {
879 /* we are the top level, let's get some search results */
880 if(!(q = cgi_get("query"))) return; /* no results yet */
881 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
885 ntracks = ds->ntracks;
887 assert(ntracks != 0);
888 /* sort tracks by the appropriate part */
889 r = xmalloc(ntracks * sizeof *r);
890 for(n = 0; n < ntracks; ++n) {
891 r[n].track = tracks[n];
892 if(disorder_part(ds->g->client, (char **)&r[n].sort,
893 tracks[n], context, part))
894 fatal(0, "disorder_part() failed");
896 qsort(r, ntracks, sizeof (struct result), compare_result);
897 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
898 * array as we know it's guaranteed to be big enough and isn't going to be
899 * used for anything else any more. */
900 memset(&substate, 0, sizeof substate);
905 substate.tracks = tracks;
906 substate.ntracks = 0;
909 && !strcmp(r[m].sort, r[n].sort))
910 tracks[substate.ntracks++] = r[m++].track;
911 substate.last = (m == ntracks);
912 expandstring(output, template, &substate);
917 assert(substate.last != 0);
920 static void exp_arg(int attribute((unused)) nargs,
923 void attribute((unused)) *u) {
926 if((v = cgi_get(args[0])))
927 cgi_output(output, "%s", v);
930 static void exp_stats(int attribute((unused)) nargs,
931 char attribute((unused)) **args,
937 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
938 if(!disorder_stats(ds->g->client, &v, 0)) {
940 cgi_output(output, "%s\n", *v++);
942 cgi_closetag(output->sink, "pre");
945 static void exp_volume(int attribute((unused)) nargs,
951 lookups(ds, DC_VOLUME);
952 if(!strcmp(args[0], "left"))
953 cgi_output(output, "%d", ds->g->volume_left);
955 cgi_output(output, "%d", ds->g->volume_right);
958 static void exp_shell(int attribute((unused)) nargs,
961 void attribute((unused)) *u) {
967 if(!(pid = xfork())) {
972 execlp("sh", "sh", "-c", args[0], (char *)0);
973 fatal(errno, "error executing sh");
976 while((n = read(p[0], buffer, sizeof buffer))) {
978 if(errno == EINTR) continue;
979 else fatal(errno, "error reading from pipe");
981 output->sink->write(output->sink, buffer, n);
984 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
986 if(n < 0) fatal(errno, "error calling waitpid");
988 error(0, "shell command '%s' %s", args[0], wstat(w));
991 static inline int str2bool(const char *s) {
992 return !strcmp(s, "true");
995 static inline const char *bool2str(int n) {
996 return n ? "true" : "false";
999 static char *expandarg(const char *arg, dcgi_state *ds) {
1005 output.sink = sink_dynstr(&d);
1006 expandstring(&output, arg, ds);
1007 dynstr_terminate(&d);
1011 static void exp_prefs(int attribute((unused)) nargs,
1016 dcgi_state substate;
1018 const char *file = expandarg(args[0], ds);
1020 memset(&substate, 0, sizeof substate);
1023 if(disorder_prefs(ds->g->client, file, &k)) return;
1025 substate.last = !k->next;
1027 expandstring(output, args[1], &substate);
1034 static void exp_pref(int attribute((unused)) nargs,
1041 if(!disorder_get(ds->g->client, args[0], args[1], &value))
1042 cgi_output(output, "%s", value);
1045 static void exp_if(int nargs,
1050 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
1053 expandstring(output, args[n], ds);
1056 static void exp_and(int nargs,
1063 for(n = 0; n < nargs; ++n)
1064 if(!str2bool(expandarg(args[n], ds))) {
1068 sink_printf(output->sink, "%s", bool2str(result));
1071 static void exp_or(int nargs,
1078 for(n = 0; n < nargs; ++n)
1079 if(str2bool(expandarg(args[n], ds))) {
1083 sink_printf(output->sink, "%s", bool2str(result));
1086 static void exp_not(int attribute((unused)) nargs,
1089 void attribute((unused)) *u) {
1090 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
1093 static void exp_isplaying(int attribute((unused)) nargs,
1094 char attribute((unused)) **args,
1099 lookups(ds, DC_PLAYING);
1100 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
1103 static void exp_isqueue(int attribute((unused)) nargs,
1104 char attribute((unused)) **args,
1109 lookups(ds, DC_QUEUE);
1110 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
1113 static void exp_isrecent(int attribute((unused)) nargs,
1114 char attribute((unused)) **args,
1119 lookups(ds, DC_RECENT);
1120 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
1123 static void exp_isnew(int attribute((unused)) nargs,
1124 char attribute((unused)) **args,
1129 lookups(ds, DC_NEW);
1130 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
1133 static void exp_id(int attribute((unused)) nargs,
1134 char attribute((unused)) **args,
1140 cgi_output(output, "%s", ds->track->id);
1143 static void exp_track(int attribute((unused)) nargs,
1144 char attribute((unused)) **args,
1150 cgi_output(output, "%s", ds->track->track);
1153 static void exp_parity(int attribute((unused)) nargs,
1154 char attribute((unused)) **args,
1159 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
1162 static void exp_comment(int attribute((unused)) nargs,
1163 char attribute((unused)) **args,
1164 cgi_sink attribute((unused)) *output,
1165 void attribute((unused)) *u) {
1169 static void exp_prefname(int attribute((unused)) nargs,
1170 char attribute((unused)) **args,
1175 if(ds->pref && ds->pref->name)
1176 cgi_output(output, "%s", ds->pref->name);
1179 static void exp_prefvalue(int attribute((unused)) nargs,
1180 char attribute((unused)) **args,
1185 if(ds->pref && ds->pref->value)
1186 cgi_output(output, "%s", ds->pref->value);
1189 static void exp_isfiles(int attribute((unused)) nargs,
1190 char attribute((unused)) **args,
1195 lookups(ds, DC_FILES);
1196 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1199 static void exp_isdirectories(int attribute((unused)) nargs,
1200 char attribute((unused)) **args,
1205 lookups(ds, DC_DIRS);
1206 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1209 static void exp_choose(int attribute((unused)) nargs,
1214 dcgi_state substate;
1218 const char *type, *what = expandarg(args[0], ds);
1220 if(!strcmp(what, "files")) {
1221 lookups(ds, DC_FILES);
1222 files = ds->g->files;
1223 nfiles = ds->g->nfiles;
1225 } else if(!strcmp(what, "directories")) {
1226 lookups(ds, DC_DIRS);
1227 files = ds->g->dirs;
1228 nfiles = ds->g->ndirs;
1231 error(0, "unknown @choose@ argument '%s'", what);
1234 e = xmalloc(nfiles * sizeof (struct entry));
1235 for(n = 0; n < nfiles; ++n) {
1236 e[n].path = files[n];
1237 e[n].sort = trackname_transform(type, files[n], "sort");
1238 e[n].display = trackname_transform(type, files[n], "display");
1240 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1241 memset(&substate, 0, sizeof substate);
1244 for(n = 0; n < nfiles; ++n) {
1245 substate.last = (n == nfiles - 1);
1247 substate.entry = &e[n];
1248 expandstring(output, args[1], &substate);
1253 static void exp_file(int attribute((unused)) nargs,
1254 char attribute((unused)) **args,
1260 cgi_output(output, "%s", ds->entry->path);
1262 cgi_output(output, "%s", ds->track->track);
1264 cgi_output(output, "%s", ds->tracks[0]);
1267 static void exp_transform(int nargs,
1270 void attribute((unused)) *u) {
1271 const char *context = nargs > 2 ? args[2] : "display";
1273 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1276 static void exp_urlquote(int attribute((unused)) nargs,
1279 void attribute((unused)) *u) {
1280 cgi_output(output, "%s", urlencodestring(args[0]));
1283 static void exp_scratchable(int attribute((unused)) nargs,
1284 char attribute((unused)) **args,
1286 void attribute((unused)) *u) {
1289 lookups(ds, DC_PLAYING|DC_RIGHTS);
1290 sink_printf(output->sink, "%s",
1291 bool2str(right_scratchable(ds->g->rights,
1292 disorder_user(ds->g->client),
1296 static void exp_removable(int attribute((unused)) nargs,
1297 char attribute((unused)) **args,
1299 void attribute((unused)) *u) {
1302 lookups(ds, DC_RIGHTS);
1303 sink_printf(output->sink, "%s",
1304 bool2str(right_removable(ds->g->rights,
1305 disorder_user(ds->g->client),
1309 static void exp_movable(int attribute((unused)) nargs,
1310 char attribute((unused)) **args,
1312 void attribute((unused)) *u) {
1315 lookups(ds, DC_RIGHTS);
1316 sink_printf(output->sink, "%s",
1317 bool2str(right_movable(ds->g->rights,
1318 disorder_user(ds->g->client),
1322 static void exp_navigate(int attribute((unused)) nargs,
1327 dcgi_state substate;
1328 const char *path = expandarg(args[0], ds);
1333 memset(&substate, 0, sizeof substate);
1335 ptr = path + 1; /* skip root */
1337 substate.nav_path = path;
1340 while(*ptr && *ptr != '/')
1342 substate.last = !*ptr;
1343 substate.nav_len = ptr - path;
1344 substate.nav_dirlen = dirlen;
1345 expandstring(output, args[1], &substate);
1346 dirlen = substate.nav_len;
1353 static void exp_fullname(int attribute((unused)) nargs,
1354 char attribute((unused)) **args,
1358 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1361 static void exp_basename(int nargs,
1369 if((s = strrchr(args[0], '/'))) ++s;
1371 cgi_output(output, "%s", s);
1373 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1374 ds->nav_path + ds->nav_dirlen + 1);
1377 static void exp_dirname(int nargs,
1385 if((s = strrchr(args[0], '/')))
1386 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1388 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1391 static void exp_eq(int attribute((unused)) nargs,
1394 void attribute((unused)) *u) {
1395 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1398 static void exp_ne(int attribute((unused)) nargs,
1401 void attribute((unused)) *u) {
1402 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1405 static void exp_enabled(int attribute((unused)) nargs,
1406 char attribute((unused)) **args,
1413 disorder_enabled(ds->g->client, &enabled);
1414 cgi_output(output, "%s", bool2str(enabled));
1417 static void exp_random_enabled(int attribute((unused)) nargs,
1418 char attribute((unused)) **args,
1425 disorder_random_enabled(ds->g->client, &enabled);
1426 cgi_output(output, "%s", bool2str(enabled));
1429 static void exp_trackstate(int attribute((unused)) nargs,
1434 struct queue_entry *q;
1437 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1438 lookups(ds, DC_QUEUE|DC_PLAYING);
1439 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1440 cgi_output(output, "playing");
1442 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1445 cgi_output(output, "queued");
1449 static void exp_thisurl(int attribute((unused)) nargs,
1450 char attribute((unused)) **args,
1452 void attribute((unused)) *u) {
1453 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1454 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1457 static void exp_isfirst(int attribute((unused)) nargs,
1458 char attribute((unused)) **args,
1463 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1466 static void exp_islast(int attribute((unused)) nargs,
1467 char attribute((unused)) **args,
1472 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1475 static void exp_action(int attribute((unused)) nargs,
1476 char attribute((unused)) **args,
1478 void attribute((unused)) *u) {
1479 const char *action = cgi_get("action"), *mgmt;
1481 if(!action) action = "playing";
1482 if(!strcmp(action, "playing")
1483 && (mgmt = cgi_get("mgmt"))
1484 && !strcmp(mgmt, "true"))
1486 sink_printf(output->sink, "%s", action);
1489 static void exp_resolve(int attribute((unused)) nargs,
1492 void attribute((unused)) *u) {
1496 if(!disorder_resolve(ds->g->client, &track, args[0]))
1497 sink_printf(output->sink, "%s", track);
1500 static void exp_paused(int attribute((unused)) nargs,
1501 char attribute((unused)) **args,
1507 lookups(ds, DC_PLAYING);
1508 if(ds->g->playing && ds->g->playing->state == playing_paused)
1510 cgi_output(output, "%s", bool2str(paused));
1513 static void exp_state(int attribute((unused)) nargs,
1514 char attribute((unused)) **args,
1520 cgi_output(output, "%s", playing_states[ds->track->state]);
1523 static void exp_files(int attribute((unused)) nargs,
1528 dcgi_state substate;
1529 const char *nfiles_arg, *directory;
1530 int nfiles, numfile;
1533 memset(&substate, 0, sizeof substate);
1535 if((directory = cgi_get("directory"))) {
1536 /* Prefs for whole directory. */
1537 lookups(ds, DC_FILES);
1538 /* Synthesize args for the file list. */
1539 nfiles = ds->g->nfiles;
1540 for(numfile = 0; numfile < nfiles; ++numfile) {
1541 k = xmalloc(sizeof *k);
1542 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1543 k->value = ds->g->files[numfile];
1548 /* Args already present. */
1549 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1552 for(numfile = 0; numfile < nfiles; ++numfile) {
1553 substate.index = numfile;
1554 expandstring(output, args[0], &substate);
1558 static void exp_index(int attribute((unused)) nargs,
1559 char attribute((unused)) **args,
1564 cgi_output(output, "%d", ds->index);
1567 static void exp_nfiles(int attribute((unused)) nargs,
1568 char attribute((unused)) **args,
1572 const char *files_arg;
1574 if(cgi_get("directory")) {
1575 lookups(ds, DC_FILES);
1576 cgi_output(output, "%d", ds->g->nfiles);
1577 } else if((files_arg = cgi_get("files")))
1578 cgi_output(output, "%s", files_arg);
1580 cgi_output(output, "1");
1583 static void exp_user(int attribute((unused)) nargs,
1584 char attribute((unused)) **args,
1587 dcgi_state *const ds = u;
1589 cgi_output(output, "%s", disorder_user(ds->g->client));
1592 static void exp_right(int attribute((unused)) nargs,
1596 dcgi_state *const ds = u;
1597 const char *right = expandarg(args[0], ds);
1600 lookups(ds, DC_RIGHTS);
1601 if(parse_rights(right, &r, 1/*report*/))
1604 cgi_output(output, "%s", bool2str(!!(r & ds->g->rights)));
1605 else if(r & ds->g->rights)
1606 expandstring(output, args[1], ds);
1608 expandstring(output, args[2], ds);
1611 static const struct cgi_expansion expansions[] = {
1612 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1613 { "action", 0, 0, 0, exp_action },
1614 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1615 { "arg", 1, 1, 0, exp_arg },
1616 { "basename", 0, 1, 0, exp_basename },
1617 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1618 { "dirname", 0, 1, 0, exp_dirname },
1619 { "enabled", 0, 0, 0, exp_enabled },
1620 { "eq", 2, 2, 0, exp_eq },
1621 { "file", 0, 0, 0, exp_file },
1622 { "files", 1, 1, EXP_MAGIC, exp_files },
1623 { "fullname", 0, 0, 0, exp_fullname },
1624 { "id", 0, 0, 0, exp_id },
1625 { "if", 2, 3, EXP_MAGIC, exp_if },
1626 { "include", 1, 1, 0, exp_include },
1627 { "index", 0, 0, 0, exp_index },
1628 { "isdirectories", 0, 0, 0, exp_isdirectories },
1629 { "isfiles", 0, 0, 0, exp_isfiles },
1630 { "isfirst", 0, 0, 0, exp_isfirst },
1631 { "islast", 0, 0, 0, exp_islast },
1632 { "isnew", 0, 0, 0, exp_isnew },
1633 { "isplaying", 0, 0, 0, exp_isplaying },
1634 { "isqueue", 0, 0, 0, exp_isqueue },
1635 { "isrecent", 0, 0, 0, exp_isrecent },
1636 { "label", 1, 1, 0, exp_label },
1637 { "length", 0, 0, 0, exp_length },
1638 { "movable", 0, 0, 0, exp_movable },
1639 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1640 { "ne", 2, 2, 0, exp_ne },
1641 { "new", 1, 1, EXP_MAGIC, exp_new },
1642 { "nfiles", 0, 0, 0, exp_nfiles },
1643 { "nonce", 0, 0, 0, exp_nonce },
1644 { "not", 1, 1, 0, exp_not },
1645 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1646 { "parity", 0, 0, 0, exp_parity },
1647 { "part", 1, 3, 0, exp_part },
1648 { "paused", 0, 0, 0, exp_paused },
1649 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1650 { "pref", 2, 2, 0, exp_pref },
1651 { "prefname", 0, 0, 0, exp_prefname },
1652 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1653 { "prefvalue", 0, 0, 0, exp_prefvalue },
1654 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1655 { "random-enabled", 0, 0, 0, exp_random_enabled },
1656 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1657 { "removable", 0, 0, 0, exp_removable },
1658 { "resolve", 1, 1, 0, exp_resolve },
1659 { "right", 1, 3, EXP_MAGIC, exp_right },
1660 { "scratchable", 0, 0, 0, exp_scratchable },
1661 { "search", 2, 3, EXP_MAGIC, exp_search },
1662 { "server-version", 0, 0, 0, exp_server_version },
1663 { "shell", 1, 1, 0, exp_shell },
1664 { "state", 0, 0, 0, exp_state },
1665 { "stats", 0, 0, 0, exp_stats },
1666 { "thisurl", 0, 0, 0, exp_thisurl },
1667 { "track", 0, 0, 0, exp_track },
1668 { "trackstate", 1, 1, 0, exp_trackstate },
1669 { "transform", 2, 3, 0, exp_transform },
1670 { "url", 0, 0, 0, exp_url },
1671 { "urlquote", 1, 1, 0, exp_urlquote },
1672 { "user", 0, 0, 0, exp_user },
1673 { "version", 0, 0, 0, exp_version },
1674 { "volume", 1, 1, 0, exp_volume },
1675 { "when", 0, 0, 0, exp_when },
1676 { "who", 0, 0, 0, exp_who }
1679 static void expand(cgi_sink *output,
1680 const char *template,
1682 cgi_expand(template,
1683 expansions, sizeof expansions / sizeof *expansions,
1688 static void expandstring(cgi_sink *output,
1691 cgi_expand_string("",
1693 expansions, sizeof expansions / sizeof *expansions,
1698 static void perform_action(cgi_sink *output, dcgi_state *ds,
1699 const char *action) {
1702 /* We don't ever want anything to be cached */
1703 cgi_header(output->sink, "Cache-Control", "no-cache");
1704 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1705 actions[n].handler(output, ds);
1707 expand_template(ds, output, action);
1710 void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1711 const char *action = cgi_get("action");
1714 /* We allow URLs which are just confirm=... in order to keep confirmation
1715 * URLs, which are user-facing, as short as possible. */
1721 perform_action(output, ds, action);
1724 void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1726 cgi_set_option("error", msg);
1727 perform_action(output, ds, "error");
1730 /** @brief Log in as the current user or guest if none */
1731 void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) {
1732 /* Create a new connection */
1733 ds->g->client = disorder_new(0);
1734 /* Forget everything we knew */
1737 if(disorder_connect_cookie(ds->g->client, login_cookie)) {
1738 disorder_cgi_error(output, ds, "connect");
1741 /* If there was a cookie but it went bad, we forget it */
1742 if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest"))