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"
62 static void expand(cgi_sink *output,
65 static void expandstring(cgi_sink *output,
75 static const char *nonce(void) {
76 static unsigned long count;
79 byte_xasprintf(&s, "%lx%lx%lx",
80 (unsigned long)time(0),
81 (unsigned long)getpid(),
86 static int compare_entry(const void *a, const void *b) {
87 const struct entry *ea = a, *eb = b;
89 return compare_tracks(ea->sort, eb->sort,
90 ea->display, eb->display,
94 static const char *front_url(void) {
98 /* preserve management interface visibility */
99 if((mgmt = cgi_get("mgmt")) && !strcmp(mgmt, "true")) {
100 byte_xasprintf(&url, "%s?mgmt=true", config->url);
106 static void header_cookie(struct sink *output) {
110 memset(&u, 0, sizeof u);
112 parse_url(config->url, &u);
114 dynstr_append_string(d, "disorder=");
115 dynstr_append_string(d, quote822(login_cookie, 0));
117 /* Force browser to discard cookie */
118 dynstr_append_string(d, "disorder=none;Max-Age=0");
121 /* The default domain matches the request host, so we need not override
122 * that. But the default path only goes up to the rightmost /, which would
123 * cause the browser to expose the cookie to other CGI programs on the same
125 dynstr_append_string(d, ";Path=");
126 dynstr_append_string(d, quote822(u.path, 0));
129 cgi_header(output, "Set-Cookie", d->vec);
132 static void redirect(struct sink *output) {
135 back = cgi_get("back");
136 cgi_header(output, "Location", back && *back ? back : front_url());
137 header_cookie(output);
141 static void expand_template(dcgi_state *ds, cgi_sink *output,
142 const char *action) {
143 cgi_header(output->sink, "Content-Type", "text/html");
144 header_cookie(output->sink);
145 cgi_body(output->sink);
146 expand(output, action, ds);
149 static void lookups(dcgi_state *ds, unsigned want) {
151 struct queue_entry *r, *rnext;
152 const char *dir, *re;
155 if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) {
157 disorder_queue(ds->g->client, &ds->g->queue);
158 if(need & DC_PLAYING)
159 disorder_playing(ds->g->client, &ds->g->playing);
161 disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0);
162 if(need & DC_RECENT) {
163 /* we need to reverse the order of the list */
164 disorder_recent(ds->g->client, &r);
167 r->next = ds->g->recent;
173 disorder_get_volume(ds->g->client,
174 &ds->g->volume_left, &ds->g->volume_right);
175 if(need & (DC_FILES|DC_DIRS)) {
176 if(!(dir = cgi_get("directory")))
178 re = cgi_get("regexp");
180 if(disorder_directories(ds->g->client, dir, re,
181 &ds->g->dirs, &ds->g->ndirs))
184 if(disorder_files(ds->g->client, dir, re,
185 &ds->g->files, &ds->g->nfiles))
188 if(need & DC_RIGHTS) {
189 ds->g->rights = RIGHT_READ; /* fail-safe */
190 if(!disorder_userinfo(ds->g->client, disorder_user(ds->g->client),
192 parse_rights(rights, &ds->g->rights, 1);
194 ds->g->flags |= need;
198 /* actions ********************************************************************/
200 static void act_disable(cgi_sink *output,
203 disorder_disable(ds->g->client);
204 redirect(output->sink);
207 static void act_enable(cgi_sink *output,
210 disorder_enable(ds->g->client);
211 redirect(output->sink);
214 static void act_random_disable(cgi_sink *output,
217 disorder_random_disable(ds->g->client);
218 redirect(output->sink);
221 static void act_random_enable(cgi_sink *output,
224 disorder_random_enable(ds->g->client);
225 redirect(output->sink);
228 static void act_remove(cgi_sink *output,
232 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
234 disorder_remove(ds->g->client, id);
235 redirect(output->sink);
238 static void act_move(cgi_sink *output,
240 const char *id, *delta;
242 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
243 if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument");
245 disorder_move(ds->g->client, id, atoi(delta));
246 redirect(output->sink);
249 static void act_scratch(cgi_sink *output,
252 disorder_scratch(ds->g->client, cgi_get("id"));
253 redirect(output->sink);
256 static void act_playing(cgi_sink *output, dcgi_state *ds) {
258 long refresh = config->refresh, length;
260 int random_enabled = 0;
263 lookups(ds, DC_PLAYING|DC_QUEUE);
264 cgi_header(output->sink, "Content-Type", "text/html");
265 disorder_random_enabled(ds->g->client, &random_enabled);
266 disorder_enabled(ds->g->client, &enabled);
268 && ds->g->playing->state == playing_started /* i.e. not paused */
269 && !disorder_length(ds->g->client, ds->g->playing->track, &length)
271 && ds->g->playing->sofar >= 0) {
272 /* Try to put the next refresh at the start of the next track. */
274 fin = now + length - ds->g->playing->sofar + config->gap;
275 if(now + refresh > fin)
278 if(ds->g->queue && ds->g->queue->state == playing_isscratch) {
279 /* next track is a scratch, don't leave more than the inter-track gap */
280 if(refresh > config->gap)
281 refresh = config->gap;
283 if(!ds->g->playing && ((ds->g->queue
284 && ds->g->queue->state != playing_random)
285 || random_enabled) && enabled) {
286 /* no track playing but playing is enabled and there is something coming
287 * up, must be in a gap */
288 if(refresh > config->gap)
289 refresh = config->gap;
291 byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1,
293 cgi_header(output->sink, "Refresh", r);
294 header_cookie(output->sink);
295 cgi_body(output->sink);
296 expand(output, "playing", ds);
299 static void act_play(cgi_sink *output,
301 const char *track, *dir;
306 if((track = cgi_get("file"))) {
307 disorder_play(ds->g->client, track);
308 } else if((dir = cgi_get("directory"))) {
309 if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0;
311 e = xmalloc(ntracks * sizeof (struct entry));
312 for(n = 0; n < ntracks; ++n) {
313 e[n].path = tracks[n];
314 e[n].sort = trackname_transform("track", tracks[n], "sort");
315 e[n].display = trackname_transform("track", tracks[n], "display");
317 qsort(e, ntracks, sizeof (struct entry), compare_entry);
318 for(n = 0; n < ntracks; ++n)
319 disorder_play(ds->g->client, e[n].path);
322 /* XXX error handling */
323 redirect(output->sink);
326 static int clamp(int n, int min, int max) {
334 static const char *volume_url(void) {
337 byte_xasprintf(&url, "%s?action=volume", config->url);
341 static void act_volume(cgi_sink *output, dcgi_state *ds) {
342 const char *l, *r, *d, *back;
343 int nd, changed = 0;;
345 if((d = cgi_get("delta"))) {
346 lookups(ds, DC_VOLUME);
347 nd = clamp(atoi(d), -255, 255);
348 disorder_set_volume(ds->g->client,
349 clamp(ds->g->volume_left + nd, 0, 255),
350 clamp(ds->g->volume_right + nd, 0, 255));
352 } else if((l = cgi_get("left")) && (r = cgi_get("right"))) {
353 disorder_set_volume(ds->g->client, atoi(l), atoi(r));
357 /* redirect back to ourselves (but without the volume-changing bits in the
359 cgi_header(output->sink, "Location",
360 (back = cgi_get("back")) ? back : volume_url());
361 header_cookie(output->sink);
362 cgi_body(output->sink);
364 cgi_header(output->sink, "Content-Type", "text/html");
365 header_cookie(output->sink);
366 cgi_body(output->sink);
367 expand(output, "volume", ds);
371 static void act_prefs_errors(const char *msg,
372 void attribute((unused)) *u) {
373 fatal(0, "error splitting parts list: %s", msg);
376 static const char *numbered_arg(const char *argname, int numfile) {
379 byte_xasprintf(&fullname, "%d_%s", numfile, argname);
380 return cgi_get(fullname);
383 static void process_prefs(dcgi_state *ds, int numfile) {
384 const char *file, *name, *value, *part, *parts, *current, *context;
387 if(!(file = numbered_arg("file", numfile)))
388 /* The first file doesn't need numbering. */
389 if(numfile > 0 || !(file = cgi_get("file")))
391 if((parts = numbered_arg("parts", numfile))
392 || (parts = cgi_get("parts"))) {
393 /* Default context is display. Other contexts not actually tested. */
394 if(!(context = numbered_arg("context", numfile))) context = "display";
395 partslist = split(parts, 0, 0, act_prefs_errors, 0);
396 while((part = *partslist++)) {
397 if(!(value = numbered_arg(part, numfile)))
399 /* If it's already right (whether regexps or db) don't change anything,
400 * so we don't fill the database up with rubbish. */
401 if(disorder_part(ds->g->client, (char **)¤t,
402 file, context, part))
403 fatal(0, "disorder_part() failed");
404 if(!strcmp(current, value))
406 byte_xasprintf((char **)&name, "trackname_%s_%s", context, part);
407 disorder_set(ds->g->client, file, name, value);
409 if((value = numbered_arg("random", numfile)))
410 disorder_unset(ds->g->client, file, "pick_at_random");
412 disorder_set(ds->g->client, file, "pick_at_random", "0");
413 if((value = numbered_arg("tags", numfile)))
414 disorder_set(ds->g->client, file, "tags", value);
415 } else if((name = cgi_get("name"))) {
416 /* Raw preferences. Not well supported in the templates at the moment. */
417 value = cgi_get("value");
419 disorder_set(ds->g->client, file, name, value);
421 disorder_unset(ds->g->client, file, name);
425 static void act_prefs(cgi_sink *output, dcgi_state *ds) {
429 if((files = cgi_get("files"))) nfiles = atoi(files);
431 for(numfile = 0; numfile < nfiles; ++numfile)
432 process_prefs(ds, numfile);
433 cgi_header(output->sink, "Content-Type", "text/html");
434 header_cookie(output->sink);
435 cgi_body(output->sink);
436 expand(output, "prefs", ds);
439 static void act_pause(cgi_sink *output,
442 disorder_pause(ds->g->client);
443 redirect(output->sink);
446 static void act_resume(cgi_sink *output,
449 disorder_resume(ds->g->client);
450 redirect(output->sink);
453 static void act_login(cgi_sink *output,
455 const char *username, *password, *back;
458 username = cgi_get("username");
459 password = cgi_get("password");
460 if(!username || !password
461 || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) {
462 /* We're just visiting the login page */
463 expand_template(ds, output, "login");
467 if(disorder_connect_user(c, username, password)) {
468 cgi_set_option("error", "loginfailed");
469 expand_template(ds, output, "login");
472 if(disorder_make_cookie(c, &login_cookie)) {
473 cgi_set_option("error", "cookiefailed");
474 expand_template(ds, output, "login");
477 /* We have a new cookie */
478 header_cookie(output->sink);
479 if((back = cgi_get("back")) && back)
480 /* Redirect back to somewhere or other */
481 redirect(output->sink);
483 /* Stick to the login page */
484 expand_template(ds, output, "login");
487 static void act_logout(cgi_sink *output,
489 disorder_revoke(ds->g->client);
491 /* Reconnect as guest */
492 disorder_cgi_login(ds, output);
493 /* Back to the login page */
494 expand_template(ds, output, "login");
497 static void act_register(cgi_sink *output,
499 const char *username, *password, *email;
502 username = cgi_get("username");
503 password = cgi_get("password");
504 email = cgi_get("email");
506 if(!username || !*username) {
507 cgi_set_option("error", "nousername");
508 expand_template(ds, output, "login");
511 if(!password || !*password) {
512 cgi_set_option("error", "nopassword");
513 expand_template(ds, output, "login");
516 if(!email || !*email) {
517 cgi_set_option("error", "noemail");
518 expand_template(ds, output, "login");
521 /* We could well do better address validation but for now we'll just do the
523 if(!strchr(email, '@')) {
524 cgi_set_option("error", "bademail");
525 expand_template(ds, output, "login");
528 if(disorder_register(ds->g->client, username, password, email, &confirm)) {
529 cgi_set_option("error", "cannotregister");
530 expand_template(ds, output, "login");
533 /* We'll go back to the login page with a suitable message */
534 cgi_set_option("registered", "registeredok");
535 expand_template(ds, output, "login");
538 static const struct action {
540 void (*handler)(cgi_sink *output, dcgi_state *ds);
542 { "disable", act_disable },
543 { "enable", act_enable },
544 { "login", act_login },
545 { "logout", act_logout },
546 { "move", act_move },
547 { "pause", act_pause },
548 { "play", act_play },
549 { "playing", act_playing },
550 { "prefs", act_prefs },
551 { "random-disable", act_random_disable },
552 { "random-enable", act_random_enable },
553 { "register", act_register },
554 { "remove", act_remove },
555 { "resume", act_resume },
556 { "scratch", act_scratch },
557 { "volume", act_volume },
560 /* expansions *****************************************************************/
562 static void exp_include(int attribute((unused)) nargs,
566 expand(output, args[0], u);
569 static void exp_server_version(int attribute((unused)) nargs,
570 char attribute((unused)) **args,
577 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
579 v = "(server not running)";
580 cgi_output(output, "%s", v);
583 static void exp_version(int attribute((unused)) nargs,
584 char attribute((unused)) **args,
586 void attribute((unused)) *u) {
587 cgi_output(output, "%s", disorder_short_version_string);
590 static void exp_nonce(int attribute((unused)) nargs,
591 char attribute((unused)) **args,
593 void attribute((unused)) *u) {
594 cgi_output(output, "%s", nonce());
597 static void exp_label(int attribute((unused)) nargs,
600 void attribute((unused)) *u) {
601 cgi_output(output, "%s", cgi_label(args[0]));
604 struct trackinfo_state {
606 const struct queue_entry *q;
611 static void exp_who(int attribute((unused)) nargs,
612 char attribute((unused)) **args,
617 if(ds->track && ds->track->submitter)
618 cgi_output(output, "%s", ds->track->submitter);
621 static void exp_length(int attribute((unused)) nargs,
622 char attribute((unused)) **args,
629 && (ds->track->state == playing_started
630 || ds->track->state == playing_paused)
631 && ds->track->sofar >= 0)
632 cgi_output(output, "%ld:%02ld/",
633 ds->track->sofar / 60, ds->track->sofar % 60);
636 disorder_length(ds->g->client, ds->track->track, &length);
638 disorder_length(ds->g->client, ds->tracks[0], &length);
640 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
642 sink_printf(output->sink, "%s", " ");
645 static void exp_when(int attribute((unused)) nargs,
646 char attribute((unused)) **args,
650 const struct tm *w = 0;
653 switch(ds->track->state) {
654 case playing_isscratch:
655 case playing_unplayed:
657 if(ds->track->expected)
658 w = localtime(&ds->track->expected);
661 case playing_no_player:
663 case playing_scratched:
664 case playing_started:
666 case playing_quitting:
667 if(ds->track->played)
668 w = localtime(&ds->track->played);
672 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
674 sink_printf(output->sink, " ");
677 static void exp_part(int nargs,
682 const char *s, *track, *part, *context;
688 track = ds->track->track;
690 track = ds->tracks[0];
708 if(disorder_part(ds->g->client, (char **)&s, track,
709 !strcmp(context, "short") ? "display" : context, part))
710 fatal(0, "disorder_part() failed");
711 if(!strcmp(context, "short"))
712 s = truncate_for_display(s, config->short_display);
713 cgi_output(output, "%s", s);
715 sink_printf(output->sink, " ");
718 static void exp_playing(int attribute((unused)) nargs,
725 lookups(ds, DC_PLAYING);
726 memset(&s, 0, sizeof s);
729 s.track = ds->g->playing;
730 expandstring(output, args[0], &s);
734 static void exp_queue(int attribute((unused)) nargs,
740 struct queue_entry *q;
742 lookups(ds, DC_QUEUE);
743 memset(&s, 0, sizeof s);
746 for(q = ds->g->queue; q; q = q->next) {
749 expandstring(output, args[0], &s);
755 static void exp_recent(int attribute((unused)) nargs,
761 struct queue_entry *q;
763 lookups(ds, DC_RECENT);
764 memset(&s, 0, sizeof s);
767 for(q = ds->g->recent; q; q = q->next) {
770 expandstring(output, args[0], &s);
776 static void exp_new(int attribute((unused)) nargs,
784 memset(&s, 0, sizeof s);
787 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
788 s.last = s.index + 1 < ds->g->nnew;
789 s.tracks = &ds->g->new[s.index];
790 expandstring(output, args[0], &s);
795 static void exp_url(int attribute((unused)) nargs,
796 char attribute((unused)) **args,
798 void attribute((unused)) *u) {
799 cgi_output(output, "%s", config->url);
807 static int compare_result(const void *a, const void *b) {
808 const struct result *ra = a, *rb = b;
811 if(!(c = strcmp(ra->sort, rb->sort)))
812 c = strcmp(ra->track, rb->track);
816 static void exp_search(int nargs,
820 dcgi_state *ds = u, substate;
822 const char *q, *context, *part, *template;
838 assert(!"should never happen");
839 part = context = template = 0; /* quieten compiler */
841 if(ds->tracks == 0) {
842 /* we are the top level, let's get some search results */
843 if(!(q = cgi_get("query"))) return; /* no results yet */
844 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
848 ntracks = ds->ntracks;
850 assert(ntracks != 0);
851 /* sort tracks by the appropriate part */
852 r = xmalloc(ntracks * sizeof *r);
853 for(n = 0; n < ntracks; ++n) {
854 r[n].track = tracks[n];
855 if(disorder_part(ds->g->client, (char **)&r[n].sort,
856 tracks[n], context, part))
857 fatal(0, "disorder_part() failed");
859 qsort(r, ntracks, sizeof (struct result), compare_result);
860 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
861 * array as we know it's guaranteed to be big enough and isn't going to be
862 * used for anything else any more. */
863 memset(&substate, 0, sizeof substate);
868 substate.tracks = tracks;
869 substate.ntracks = 0;
872 && !strcmp(r[m].sort, r[n].sort))
873 tracks[substate.ntracks++] = r[m++].track;
874 substate.last = (m == ntracks);
875 expandstring(output, template, &substate);
880 assert(substate.last != 0);
883 static void exp_arg(int attribute((unused)) nargs,
886 void attribute((unused)) *u) {
889 if((v = cgi_get(args[0])))
890 cgi_output(output, "%s", v);
893 static void exp_stats(int attribute((unused)) nargs,
894 char attribute((unused)) **args,
900 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
901 if(!disorder_stats(ds->g->client, &v, 0)) {
903 cgi_output(output, "%s\n", *v++);
905 cgi_closetag(output->sink, "pre");
908 static void exp_volume(int attribute((unused)) nargs,
914 lookups(ds, DC_VOLUME);
915 if(!strcmp(args[0], "left"))
916 cgi_output(output, "%d", ds->g->volume_left);
918 cgi_output(output, "%d", ds->g->volume_right);
921 static void exp_shell(int attribute((unused)) nargs,
924 void attribute((unused)) *u) {
930 if(!(pid = xfork())) {
935 execlp("sh", "sh", "-c", args[0], (char *)0);
936 fatal(errno, "error executing sh");
939 while((n = read(p[0], buffer, sizeof buffer))) {
941 if(errno == EINTR) continue;
942 else fatal(errno, "error reading from pipe");
944 output->sink->write(output->sink, buffer, n);
947 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
949 if(n < 0) fatal(errno, "error calling waitpid");
951 error(0, "shell command '%s' %s", args[0], wstat(w));
954 static inline int str2bool(const char *s) {
955 return !strcmp(s, "true");
958 static inline const char *bool2str(int n) {
959 return n ? "true" : "false";
962 static char *expandarg(const char *arg, dcgi_state *ds) {
968 output.sink = sink_dynstr(&d);
969 expandstring(&output, arg, ds);
970 dynstr_terminate(&d);
974 static void exp_prefs(int attribute((unused)) nargs,
981 const char *file = expandarg(args[0], ds);
983 memset(&substate, 0, sizeof substate);
986 if(disorder_prefs(ds->g->client, file, &k)) return;
988 substate.last = !k->next;
990 expandstring(output, args[1], &substate);
997 static void exp_pref(int attribute((unused)) nargs,
1004 if(!disorder_get(ds->g->client, args[0], args[1], &value))
1005 cgi_output(output, "%s", value);
1008 static void exp_if(int nargs,
1013 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
1016 expandstring(output, args[n], ds);
1019 static void exp_and(int nargs,
1026 for(n = 0; n < nargs; ++n)
1027 if(!str2bool(expandarg(args[n], ds))) {
1031 sink_printf(output->sink, "%s", bool2str(result));
1034 static void exp_or(int nargs,
1041 for(n = 0; n < nargs; ++n)
1042 if(str2bool(expandarg(args[n], ds))) {
1046 sink_printf(output->sink, "%s", bool2str(result));
1049 static void exp_not(int attribute((unused)) nargs,
1052 void attribute((unused)) *u) {
1053 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
1056 static void exp_isplaying(int attribute((unused)) nargs,
1057 char attribute((unused)) **args,
1062 lookups(ds, DC_PLAYING);
1063 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
1066 static void exp_isqueue(int attribute((unused)) nargs,
1067 char attribute((unused)) **args,
1072 lookups(ds, DC_QUEUE);
1073 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
1076 static void exp_isrecent(int attribute((unused)) nargs,
1077 char attribute((unused)) **args,
1082 lookups(ds, DC_RECENT);
1083 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
1086 static void exp_isnew(int attribute((unused)) nargs,
1087 char attribute((unused)) **args,
1092 lookups(ds, DC_NEW);
1093 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
1096 static void exp_id(int attribute((unused)) nargs,
1097 char attribute((unused)) **args,
1103 cgi_output(output, "%s", ds->track->id);
1106 static void exp_track(int attribute((unused)) nargs,
1107 char attribute((unused)) **args,
1113 cgi_output(output, "%s", ds->track->track);
1116 static void exp_parity(int attribute((unused)) nargs,
1117 char attribute((unused)) **args,
1122 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
1125 static void exp_comment(int attribute((unused)) nargs,
1126 char attribute((unused)) **args,
1127 cgi_sink attribute((unused)) *output,
1128 void attribute((unused)) *u) {
1132 static void exp_prefname(int attribute((unused)) nargs,
1133 char attribute((unused)) **args,
1138 if(ds->pref && ds->pref->name)
1139 cgi_output(output, "%s", ds->pref->name);
1142 static void exp_prefvalue(int attribute((unused)) nargs,
1143 char attribute((unused)) **args,
1148 if(ds->pref && ds->pref->value)
1149 cgi_output(output, "%s", ds->pref->value);
1152 static void exp_isfiles(int attribute((unused)) nargs,
1153 char attribute((unused)) **args,
1158 lookups(ds, DC_FILES);
1159 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1162 static void exp_isdirectories(int attribute((unused)) nargs,
1163 char attribute((unused)) **args,
1168 lookups(ds, DC_DIRS);
1169 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1172 static void exp_choose(int attribute((unused)) nargs,
1177 dcgi_state substate;
1181 const char *type, *what = expandarg(args[0], ds);
1183 if(!strcmp(what, "files")) {
1184 lookups(ds, DC_FILES);
1185 files = ds->g->files;
1186 nfiles = ds->g->nfiles;
1188 } else if(!strcmp(what, "directories")) {
1189 lookups(ds, DC_DIRS);
1190 files = ds->g->dirs;
1191 nfiles = ds->g->ndirs;
1194 error(0, "unknown @choose@ argument '%s'", what);
1197 e = xmalloc(nfiles * sizeof (struct entry));
1198 for(n = 0; n < nfiles; ++n) {
1199 e[n].path = files[n];
1200 e[n].sort = trackname_transform(type, files[n], "sort");
1201 e[n].display = trackname_transform(type, files[n], "display");
1203 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1204 memset(&substate, 0, sizeof substate);
1207 for(n = 0; n < nfiles; ++n) {
1208 substate.last = (n == nfiles - 1);
1210 substate.entry = &e[n];
1211 expandstring(output, args[1], &substate);
1216 static void exp_file(int attribute((unused)) nargs,
1217 char attribute((unused)) **args,
1223 cgi_output(output, "%s", ds->entry->path);
1225 cgi_output(output, "%s", ds->track->track);
1227 cgi_output(output, "%s", ds->tracks[0]);
1230 static void exp_transform(int nargs,
1233 void attribute((unused)) *u) {
1234 const char *context = nargs > 2 ? args[2] : "display";
1236 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1239 static void exp_urlquote(int attribute((unused)) nargs,
1242 void attribute((unused)) *u) {
1243 cgi_output(output, "%s", urlencodestring(args[0]));
1246 static void exp_scratchable(int attribute((unused)) nargs,
1247 char attribute((unused)) **args,
1249 void attribute((unused)) *u) {
1252 lookups(ds, DC_PLAYING|DC_RIGHTS);
1253 sink_printf(output->sink, "%s",
1254 bool2str(right_scratchable(ds->g->rights,
1255 disorder_user(ds->g->client),
1259 static void exp_removable(int attribute((unused)) nargs,
1260 char attribute((unused)) **args,
1262 void attribute((unused)) *u) {
1265 lookups(ds, DC_RIGHTS);
1266 sink_printf(output->sink, "%s",
1267 bool2str(right_removable(ds->g->rights,
1268 disorder_user(ds->g->client),
1272 static void exp_movable(int attribute((unused)) nargs,
1273 char attribute((unused)) **args,
1275 void attribute((unused)) *u) {
1278 lookups(ds, DC_RIGHTS);
1279 sink_printf(output->sink, "%s",
1280 bool2str(right_movable(ds->g->rights,
1281 disorder_user(ds->g->client),
1285 static void exp_navigate(int attribute((unused)) nargs,
1290 dcgi_state substate;
1291 const char *path = expandarg(args[0], ds);
1296 memset(&substate, 0, sizeof substate);
1298 ptr = path + 1; /* skip root */
1300 substate.nav_path = path;
1303 while(*ptr && *ptr != '/')
1305 substate.last = !*ptr;
1306 substate.nav_len = ptr - path;
1307 substate.nav_dirlen = dirlen;
1308 expandstring(output, args[1], &substate);
1309 dirlen = substate.nav_len;
1316 static void exp_fullname(int attribute((unused)) nargs,
1317 char attribute((unused)) **args,
1321 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1324 static void exp_basename(int nargs,
1332 if((s = strrchr(args[0], '/'))) ++s;
1334 cgi_output(output, "%s", s);
1336 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1337 ds->nav_path + ds->nav_dirlen + 1);
1340 static void exp_dirname(int nargs,
1348 if((s = strrchr(args[0], '/')))
1349 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1351 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1354 static void exp_eq(int attribute((unused)) nargs,
1357 void attribute((unused)) *u) {
1358 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1361 static void exp_ne(int attribute((unused)) nargs,
1364 void attribute((unused)) *u) {
1365 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1368 static void exp_enabled(int attribute((unused)) nargs,
1369 char attribute((unused)) **args,
1376 disorder_enabled(ds->g->client, &enabled);
1377 cgi_output(output, "%s", bool2str(enabled));
1380 static void exp_random_enabled(int attribute((unused)) nargs,
1381 char attribute((unused)) **args,
1388 disorder_random_enabled(ds->g->client, &enabled);
1389 cgi_output(output, "%s", bool2str(enabled));
1392 static void exp_trackstate(int attribute((unused)) nargs,
1397 struct queue_entry *q;
1400 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1401 lookups(ds, DC_QUEUE|DC_PLAYING);
1402 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1403 cgi_output(output, "playing");
1405 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1408 cgi_output(output, "queued");
1412 static void exp_thisurl(int attribute((unused)) nargs,
1413 char attribute((unused)) **args,
1415 void attribute((unused)) *u) {
1416 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1417 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1420 static void exp_isfirst(int attribute((unused)) nargs,
1421 char attribute((unused)) **args,
1426 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1429 static void exp_islast(int attribute((unused)) nargs,
1430 char attribute((unused)) **args,
1435 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1438 static void exp_action(int attribute((unused)) nargs,
1439 char attribute((unused)) **args,
1441 void attribute((unused)) *u) {
1442 const char *action = cgi_get("action"), *mgmt;
1444 if(!action) action = "playing";
1445 if(!strcmp(action, "playing")
1446 && (mgmt = cgi_get("mgmt"))
1447 && !strcmp(mgmt, "true"))
1449 sink_printf(output->sink, "%s", action);
1452 static void exp_resolve(int attribute((unused)) nargs,
1455 void attribute((unused)) *u) {
1459 if(!disorder_resolve(ds->g->client, &track, args[0]))
1460 sink_printf(output->sink, "%s", track);
1463 static void exp_paused(int attribute((unused)) nargs,
1464 char attribute((unused)) **args,
1470 lookups(ds, DC_PLAYING);
1471 if(ds->g->playing && ds->g->playing->state == playing_paused)
1473 cgi_output(output, "%s", bool2str(paused));
1476 static void exp_state(int attribute((unused)) nargs,
1477 char attribute((unused)) **args,
1483 cgi_output(output, "%s", playing_states[ds->track->state]);
1486 static void exp_files(int attribute((unused)) nargs,
1491 dcgi_state substate;
1492 const char *nfiles_arg, *directory;
1493 int nfiles, numfile;
1496 memset(&substate, 0, sizeof substate);
1498 if((directory = cgi_get("directory"))) {
1499 /* Prefs for whole directory. */
1500 lookups(ds, DC_FILES);
1501 /* Synthesize args for the file list. */
1502 nfiles = ds->g->nfiles;
1503 for(numfile = 0; numfile < nfiles; ++numfile) {
1504 k = xmalloc(sizeof *k);
1505 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1506 k->value = ds->g->files[numfile];
1511 /* Args already present. */
1512 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1515 for(numfile = 0; numfile < nfiles; ++numfile) {
1516 substate.index = numfile;
1517 expandstring(output, args[0], &substate);
1521 static void exp_index(int attribute((unused)) nargs,
1522 char attribute((unused)) **args,
1527 cgi_output(output, "%d", ds->index);
1530 static void exp_nfiles(int attribute((unused)) nargs,
1531 char attribute((unused)) **args,
1535 const char *files_arg;
1537 if(cgi_get("directory")) {
1538 lookups(ds, DC_FILES);
1539 cgi_output(output, "%d", ds->g->nfiles);
1540 } else if((files_arg = cgi_get("files")))
1541 cgi_output(output, "%s", files_arg);
1543 cgi_output(output, "1");
1546 static void exp_user(int attribute((unused)) nargs,
1547 char attribute((unused)) **args,
1550 dcgi_state *const ds = u;
1552 cgi_output(output, "%s", disorder_user(ds->g->client));
1555 static void exp_right(int attribute((unused)) nargs,
1559 dcgi_state *const ds = u;
1560 const char *right = expandarg(args[0], ds);
1563 lookups(ds, DC_RIGHTS);
1564 if(parse_rights(right, &r, 1/*report*/))
1567 cgi_output(output, "%s", bool2str(!!(r & ds->g->rights)));
1568 else if(r & ds->g->rights)
1569 expandstring(output, args[1], ds);
1571 expandstring(output, args[2], ds);
1574 static const struct cgi_expansion expansions[] = {
1575 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1576 { "action", 0, 0, 0, exp_action },
1577 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1578 { "arg", 1, 1, 0, exp_arg },
1579 { "basename", 0, 1, 0, exp_basename },
1580 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1581 { "dirname", 0, 1, 0, exp_dirname },
1582 { "enabled", 0, 0, 0, exp_enabled },
1583 { "eq", 2, 2, 0, exp_eq },
1584 { "file", 0, 0, 0, exp_file },
1585 { "files", 1, 1, EXP_MAGIC, exp_files },
1586 { "fullname", 0, 0, 0, exp_fullname },
1587 { "id", 0, 0, 0, exp_id },
1588 { "if", 2, 3, EXP_MAGIC, exp_if },
1589 { "include", 1, 1, 0, exp_include },
1590 { "index", 0, 0, 0, exp_index },
1591 { "isdirectories", 0, 0, 0, exp_isdirectories },
1592 { "isfiles", 0, 0, 0, exp_isfiles },
1593 { "isfirst", 0, 0, 0, exp_isfirst },
1594 { "islast", 0, 0, 0, exp_islast },
1595 { "isnew", 0, 0, 0, exp_isnew },
1596 { "isplaying", 0, 0, 0, exp_isplaying },
1597 { "isqueue", 0, 0, 0, exp_isqueue },
1598 { "isrecent", 0, 0, 0, exp_isrecent },
1599 { "label", 1, 1, 0, exp_label },
1600 { "length", 0, 0, 0, exp_length },
1601 { "movable", 0, 0, 0, exp_movable },
1602 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1603 { "ne", 2, 2, 0, exp_ne },
1604 { "new", 1, 1, EXP_MAGIC, exp_new },
1605 { "nfiles", 0, 0, 0, exp_nfiles },
1606 { "nonce", 0, 0, 0, exp_nonce },
1607 { "not", 1, 1, 0, exp_not },
1608 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1609 { "parity", 0, 0, 0, exp_parity },
1610 { "part", 1, 3, 0, exp_part },
1611 { "paused", 0, 0, 0, exp_paused },
1612 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1613 { "pref", 2, 2, 0, exp_pref },
1614 { "prefname", 0, 0, 0, exp_prefname },
1615 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1616 { "prefvalue", 0, 0, 0, exp_prefvalue },
1617 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1618 { "random-enabled", 0, 0, 0, exp_random_enabled },
1619 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1620 { "removable", 0, 0, 0, exp_removable },
1621 { "resolve", 1, 1, 0, exp_resolve },
1622 { "right", 1, 3, EXP_MAGIC, exp_right },
1623 { "scratchable", 0, 0, 0, exp_scratchable },
1624 { "search", 2, 3, EXP_MAGIC, exp_search },
1625 { "server-version", 0, 0, 0, exp_server_version },
1626 { "shell", 1, 1, 0, exp_shell },
1627 { "state", 0, 0, 0, exp_state },
1628 { "stats", 0, 0, 0, exp_stats },
1629 { "thisurl", 0, 0, 0, exp_thisurl },
1630 { "track", 0, 0, 0, exp_track },
1631 { "trackstate", 1, 1, 0, exp_trackstate },
1632 { "transform", 2, 3, 0, exp_transform },
1633 { "url", 0, 0, 0, exp_url },
1634 { "urlquote", 1, 1, 0, exp_urlquote },
1635 { "user", 0, 0, 0, exp_user },
1636 { "version", 0, 0, 0, exp_version },
1637 { "volume", 1, 1, 0, exp_volume },
1638 { "when", 0, 0, 0, exp_when },
1639 { "who", 0, 0, 0, exp_who }
1642 static void expand(cgi_sink *output,
1643 const char *template,
1645 cgi_expand(template,
1646 expansions, sizeof expansions / sizeof *expansions,
1651 static void expandstring(cgi_sink *output,
1654 cgi_expand_string("",
1656 expansions, sizeof expansions / sizeof *expansions,
1661 static void perform_action(cgi_sink *output, dcgi_state *ds,
1662 const char *action) {
1665 /* We don't ever want anything to be cached */
1666 cgi_header(output->sink, "Cache-Control", "no-cache");
1667 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1668 actions[n].handler(output, ds);
1670 expand_template(ds, output, action);
1673 void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1674 const char *action = cgi_get("action");
1676 if(!action) action = "playing";
1677 perform_action(output, ds, action);
1680 void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1682 cgi_set_option("error", msg);
1683 perform_action(output, ds, "error");
1686 /** @brief Log in as the current user or guest if none */
1687 void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) {
1688 /* Create a new connection */
1689 ds->g->client = disorder_new(0);
1690 /* Forget everything we knew */
1693 if(disorder_connect_cookie(ds->g->client, login_cookie)) {
1694 disorder_cgi_error(output, ds, "connect");
1697 /* If there was a cookie but it went bad, we forget it */
1698 if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest"))