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>
44 #include "configuration.h"
56 #include "trackname.h"
59 static void expand(cgi_sink *output,
62 static void expandstring(cgi_sink *output,
72 static const char *nonce(void) {
73 static unsigned long count;
76 byte_xasprintf(&s, "%lx%lx%lx",
77 (unsigned long)time(0),
78 (unsigned long)getpid(),
83 static int compare_entry(const void *a, const void *b) {
84 const struct entry *ea = a, *eb = b;
86 return compare_tracks(ea->sort, eb->sort,
87 ea->display, eb->display,
91 static const char *front_url(void) {
95 /* preserve management interface visibility */
96 if((mgmt = cgi_get("mgmt")) && !strcmp(mgmt, "true")) {
97 byte_xasprintf(&url, "%s?mgmt=true", config->url);
103 static void redirect(struct sink *output) {
106 cgi_header(output, "Location",
107 (back = cgi_get("back")) ? back : front_url());
111 static void lookups(dcgi_state *ds, unsigned want) {
113 struct queue_entry *r, *rnext;
114 const char *dir, *re;
116 if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) {
118 disorder_queue(ds->g->client, &ds->g->queue);
119 if(need & DC_PLAYING)
120 disorder_playing(ds->g->client, &ds->g->playing);
122 disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0);
123 if(need & DC_RECENT) {
124 /* we need to reverse the order of the list */
125 disorder_recent(ds->g->client, &r);
128 r->next = ds->g->recent;
134 disorder_get_volume(ds->g->client,
135 &ds->g->volume_left, &ds->g->volume_right);
136 if(need & (DC_FILES|DC_DIRS)) {
137 if(!(dir = cgi_get("directory")))
139 re = cgi_get("regexp");
141 if(disorder_directories(ds->g->client, dir, re,
142 &ds->g->dirs, &ds->g->ndirs))
145 if(disorder_files(ds->g->client, dir, re,
146 &ds->g->files, &ds->g->nfiles))
149 ds->g->flags |= need;
153 /* actions ********************************************************************/
155 static void act_disable(cgi_sink *output,
158 disorder_disable(ds->g->client);
159 redirect(output->sink);
162 static void act_enable(cgi_sink *output,
165 disorder_enable(ds->g->client);
166 redirect(output->sink);
169 static void act_random_disable(cgi_sink *output,
172 disorder_random_disable(ds->g->client);
173 redirect(output->sink);
176 static void act_random_enable(cgi_sink *output,
179 disorder_random_enable(ds->g->client);
180 redirect(output->sink);
183 static void act_remove(cgi_sink *output,
187 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
189 disorder_remove(ds->g->client, id);
190 redirect(output->sink);
193 static void act_move(cgi_sink *output,
195 const char *id, *delta;
197 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
198 if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument");
200 disorder_move(ds->g->client, id, atoi(delta));
201 redirect(output->sink);
204 static void act_scratch(cgi_sink *output,
207 disorder_scratch(ds->g->client, cgi_get("id"));
208 redirect(output->sink);
211 static void act_playing(cgi_sink *output, dcgi_state *ds) {
213 long refresh = config->refresh, length;
215 int random_enabled = 0;
218 lookups(ds, DC_PLAYING|DC_QUEUE);
219 cgi_header(output->sink, "Content-Type", "text/html");
220 disorder_random_enabled(ds->g->client, &random_enabled);
221 disorder_enabled(ds->g->client, &enabled);
223 && ds->g->playing->state == playing_started /* i.e. not paused */
224 && !disorder_length(ds->g->client, ds->g->playing->track, &length)
226 && ds->g->playing->sofar >= 0) {
227 /* Try to put the next refresh at the start of the next track. */
229 fin = now + length - ds->g->playing->sofar + config->gap;
230 if(now + refresh > fin)
233 if(ds->g->queue && ds->g->queue->state == playing_isscratch) {
234 /* next track is a scratch, don't leave more than the inter-track gap */
235 if(refresh > config->gap)
236 refresh = config->gap;
238 if(!ds->g->playing && ((ds->g->queue
239 && ds->g->queue->state != playing_random)
240 || random_enabled) && enabled) {
241 /* no track playing but playing is enabled and there is something coming
242 * up, must be in a gap */
243 if(refresh > config->gap)
244 refresh = config->gap;
246 byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1,
248 cgi_header(output->sink, "Refresh", r);
249 cgi_body(output->sink);
250 expand(output, "playing", ds);
253 static void act_play(cgi_sink *output,
255 const char *track, *dir;
260 if((track = cgi_get("file"))) {
261 disorder_play(ds->g->client, track);
262 } else if((dir = cgi_get("directory"))) {
263 if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0;
265 e = xmalloc(ntracks * sizeof (struct entry));
266 for(n = 0; n < ntracks; ++n) {
267 e[n].path = tracks[n];
268 e[n].sort = trackname_transform("track", tracks[n], "sort");
269 e[n].display = trackname_transform("track", tracks[n], "display");
271 qsort(e, ntracks, sizeof (struct entry), compare_entry);
272 for(n = 0; n < ntracks; ++n)
273 disorder_play(ds->g->client, e[n].path);
276 /* XXX error handling */
277 redirect(output->sink);
280 static int clamp(int n, int min, int max) {
288 static const char *volume_url(void) {
291 byte_xasprintf(&url, "%s?action=volume", config->url);
295 static void act_volume(cgi_sink *output, dcgi_state *ds) {
296 const char *l, *r, *d, *back;
297 int nd, changed = 0;;
299 if((d = cgi_get("delta"))) {
300 lookups(ds, DC_VOLUME);
301 nd = clamp(atoi(d), -255, 255);
302 disorder_set_volume(ds->g->client,
303 clamp(ds->g->volume_left + nd, 0, 255),
304 clamp(ds->g->volume_right + nd, 0, 255));
306 } else if((l = cgi_get("left")) && (r = cgi_get("right"))) {
307 disorder_set_volume(ds->g->client, atoi(l), atoi(r));
311 /* redirect back to ourselves (but without the volume-changing bits in the
313 cgi_header(output->sink, "Location",
314 (back = cgi_get("back")) ? back : volume_url());
315 cgi_body(output->sink);
317 cgi_header(output->sink, "Content-Type", "text/html");
318 cgi_body(output->sink);
319 expand(output, "volume", ds);
323 static void act_prefs_errors(const char *msg,
324 void attribute((unused)) *u) {
325 fatal(0, "error splitting parts list: %s", msg);
328 static const char *numbered_arg(const char *argname, int numfile) {
331 byte_xasprintf(&fullname, "%d_%s", numfile, argname);
332 return cgi_get(fullname);
335 static void process_prefs(dcgi_state *ds, int numfile) {
336 const char *file, *name, *value, *part, *parts, *current, *context;
339 if(!(file = numbered_arg("file", numfile)))
340 /* The first file doesn't need numbering. */
341 if(numfile > 0 || !(file = cgi_get("file")))
343 if((parts = numbered_arg("parts", numfile))
344 || (parts = cgi_get("parts"))) {
345 /* Default context is display. Other contexts not actually tested. */
346 if(!(context = numbered_arg("context", numfile))) context = "display";
347 partslist = split(parts, 0, 0, act_prefs_errors, 0);
348 while((part = *partslist++)) {
349 if(!(value = numbered_arg(part, numfile)))
351 /* If it's already right (whether regexps or db) don't change anything,
352 * so we don't fill the database up with rubbish. */
353 if(disorder_part(ds->g->client, (char **)¤t,
354 file, context, part))
355 fatal(0, "disorder_part() failed");
356 if(!strcmp(current, value))
358 byte_xasprintf((char **)&name, "trackname_%s_%s", context, part);
359 disorder_set(ds->g->client, file, name, value);
361 if((value = numbered_arg("random", numfile)))
362 disorder_unset(ds->g->client, file, "pick_at_random");
364 disorder_set(ds->g->client, file, "pick_at_random", "0");
365 if((value = numbered_arg("tags", numfile)))
366 disorder_set(ds->g->client, file, "tags", value);
367 } else if((name = cgi_get("name"))) {
368 /* Raw preferences. Not well supported in the templates at the moment. */
369 value = cgi_get("value");
371 disorder_set(ds->g->client, file, name, value);
373 disorder_unset(ds->g->client, file, name);
377 static void act_prefs(cgi_sink *output, dcgi_state *ds) {
381 if((files = cgi_get("files"))) nfiles = atoi(files);
383 for(numfile = 0; numfile < nfiles; ++numfile)
384 process_prefs(ds, numfile);
385 cgi_header(output->sink, "Content-Type", "text/html");
386 cgi_body(output->sink);
387 expand(output, "prefs", ds);
390 static void act_pause(cgi_sink *output,
393 disorder_pause(ds->g->client);
394 redirect(output->sink);
397 static void act_resume(cgi_sink *output,
400 disorder_resume(ds->g->client);
401 redirect(output->sink);
404 static const struct action {
406 void (*handler)(cgi_sink *output, dcgi_state *ds);
408 { "disable", act_disable },
409 { "enable", act_enable },
410 { "move", act_move },
411 { "pause", act_pause },
412 { "play", act_play },
413 { "playing", act_playing },
414 { "prefs", act_prefs },
415 { "random-disable", act_random_disable },
416 { "random-enable", act_random_enable },
417 { "remove", act_remove },
418 { "resume", act_resume },
419 { "scratch", act_scratch },
420 { "volume", act_volume },
423 /* expansions *****************************************************************/
425 static void exp_include(int attribute((unused)) nargs,
429 expand(output, args[0], u);
432 static void exp_server_version(int attribute((unused)) nargs,
433 char attribute((unused)) **args,
440 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
442 v = "(server not running)";
443 cgi_output(output, "%s", v);
446 static void exp_version(int attribute((unused)) nargs,
447 char attribute((unused)) **args,
449 void attribute((unused)) *u) {
450 cgi_output(output, "%s", disorder_version_string);
453 static void exp_nonce(int attribute((unused)) nargs,
454 char attribute((unused)) **args,
456 void attribute((unused)) *u) {
457 cgi_output(output, "%s", nonce());
460 static void exp_label(int attribute((unused)) nargs,
463 void attribute((unused)) *u) {
464 cgi_output(output, "%s", cgi_label(args[0]));
467 struct trackinfo_state {
469 const struct queue_entry *q;
474 static void exp_who(int attribute((unused)) nargs,
475 char attribute((unused)) **args,
480 if(ds->track && ds->track->submitter)
481 cgi_output(output, "%s", ds->track->submitter);
484 static void exp_length(int attribute((unused)) nargs,
485 char attribute((unused)) **args,
492 && (ds->track->state == playing_started
493 || ds->track->state == playing_paused)
494 && ds->track->sofar >= 0)
495 cgi_output(output, "%ld:%02ld/",
496 ds->track->sofar / 60, ds->track->sofar % 60);
499 disorder_length(ds->g->client, ds->track->track, &length);
501 disorder_length(ds->g->client, ds->tracks[0], &length);
503 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
505 sink_printf(output->sink, "%s", " ");
508 static void exp_when(int attribute((unused)) nargs,
509 char attribute((unused)) **args,
513 const struct tm *w = 0;
516 switch(ds->track->state) {
517 case playing_isscratch:
518 case playing_unplayed:
520 if(ds->track->expected)
521 w = localtime(&ds->track->expected);
524 case playing_no_player:
526 case playing_scratched:
527 case playing_started:
529 case playing_quitting:
530 if(ds->track->played)
531 w = localtime(&ds->track->played);
535 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
537 sink_printf(output->sink, " ");
540 static void exp_part(int nargs,
545 const char *s, *track, *part, *context;
551 track = ds->track->track;
553 track = ds->tracks[0];
571 if(disorder_part(ds->g->client, (char **)&s, track,
572 !strcmp(context, "short") ? "display" : context, part))
573 fatal(0, "disorder_part() failed");
574 if(!strcmp(context, "short"))
575 s = truncate_for_display(s, config->short_display);
576 cgi_output(output, "%s", s);
578 sink_printf(output->sink, " ");
581 static void exp_playing(int attribute((unused)) nargs,
588 lookups(ds, DC_PLAYING);
589 memset(&s, 0, sizeof s);
592 s.track = ds->g->playing;
593 expandstring(output, args[0], &s);
597 static void exp_queue(int attribute((unused)) nargs,
603 struct queue_entry *q;
605 lookups(ds, DC_QUEUE);
606 memset(&s, 0, sizeof s);
609 for(q = ds->g->queue; q; q = q->next) {
612 expandstring(output, args[0], &s);
618 static void exp_recent(int attribute((unused)) nargs,
624 struct queue_entry *q;
626 lookups(ds, DC_RECENT);
627 memset(&s, 0, sizeof s);
630 for(q = ds->g->recent; q; q = q->next) {
633 expandstring(output, args[0], &s);
639 static void exp_new(int attribute((unused)) nargs,
647 memset(&s, 0, sizeof s);
650 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
651 s.last = s.index + 1 < ds->g->nnew;
652 s.tracks = &ds->g->new[s.index];
653 expandstring(output, args[0], &s);
658 static void exp_url(int attribute((unused)) nargs,
659 char attribute((unused)) **args,
661 void attribute((unused)) *u) {
662 cgi_output(output, "%s", config->url);
670 static int compare_result(const void *a, const void *b) {
671 const struct result *ra = a, *rb = b;
674 if(!(c = strcmp(ra->sort, rb->sort)))
675 c = strcmp(ra->track, rb->track);
679 static void exp_search(int nargs,
683 dcgi_state *ds = u, substate;
685 const char *q, *context, *part, *template;
701 assert(!"should never happen");
702 part = context = template = 0; /* quieten compiler */
704 if(ds->tracks == 0) {
705 /* we are the top level, let's get some search results */
706 if(!(q = cgi_get("query"))) return; /* no results yet */
707 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
711 ntracks = ds->ntracks;
713 assert(ntracks != 0);
714 /* sort tracks by the appropriate part */
715 r = xmalloc(ntracks * sizeof *r);
716 for(n = 0; n < ntracks; ++n) {
717 r[n].track = tracks[n];
718 if(disorder_part(ds->g->client, (char **)&r[n].sort,
719 tracks[n], context, part))
720 fatal(0, "disorder_part() failed");
722 qsort(r, ntracks, sizeof (struct result), compare_result);
723 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
724 * array as we know it's guaranteed to be big enough and isn't going to be
725 * used for anything else any more. */
726 memset(&substate, 0, sizeof substate);
731 substate.tracks = tracks;
732 substate.ntracks = 0;
735 && !strcmp(r[m].sort, r[n].sort))
736 tracks[substate.ntracks++] = r[m++].track;
737 substate.last = (m == ntracks);
738 expandstring(output, template, &substate);
743 assert(substate.last != 0);
746 static void exp_arg(int attribute((unused)) nargs,
749 void attribute((unused)) *u) {
752 if((v = cgi_get(args[0])))
753 cgi_output(output, "%s", v);
756 static void exp_stats(int attribute((unused)) nargs,
757 char attribute((unused)) **args,
763 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
764 if(!disorder_stats(ds->g->client, &v, 0)) {
766 cgi_output(output, "%s\n", *v++);
768 cgi_closetag(output->sink, "pre");
771 static void exp_volume(int attribute((unused)) nargs,
777 lookups(ds, DC_VOLUME);
778 if(!strcmp(args[0], "left"))
779 cgi_output(output, "%d", ds->g->volume_left);
781 cgi_output(output, "%d", ds->g->volume_right);
784 static void exp_shell(int attribute((unused)) nargs,
787 void attribute((unused)) *u) {
793 if(!(pid = xfork())) {
798 execlp("sh", "sh", "-c", args[0], (char *)0);
799 fatal(errno, "error executing sh");
802 while((n = read(p[0], buffer, sizeof buffer))) {
804 if(errno == EINTR) continue;
805 else fatal(errno, "error reading from pipe");
807 output->sink->write(output->sink, buffer, n);
810 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
812 if(n < 0) fatal(errno, "error calling waitpid");
814 error(0, "shell command '%s' %s", args[0], wstat(w));
817 static inline int str2bool(const char *s) {
818 return !strcmp(s, "true");
821 static inline const char *bool2str(int n) {
822 return n ? "true" : "false";
825 static char *expandarg(const char *arg, dcgi_state *ds) {
831 output.sink = sink_dynstr(&d);
832 expandstring(&output, arg, ds);
833 dynstr_terminate(&d);
837 static void exp_prefs(int attribute((unused)) nargs,
844 const char *file = expandarg(args[0], ds);
846 memset(&substate, 0, sizeof substate);
849 if(disorder_prefs(ds->g->client, file, &k)) return;
851 substate.last = !k->next;
853 expandstring(output, args[1], &substate);
860 static void exp_pref(int attribute((unused)) nargs,
867 if(!disorder_get(ds->g->client, args[0], args[1], &value))
868 cgi_output(output, "%s", value);
871 static void exp_if(int nargs,
876 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
879 expandstring(output, args[n], ds);
882 static void exp_and(int nargs,
889 for(n = 0; n < nargs; ++n)
890 if(!str2bool(expandarg(args[n], ds))) {
894 sink_printf(output->sink, "%s", bool2str(result));
897 static void exp_or(int nargs,
904 for(n = 0; n < nargs; ++n)
905 if(str2bool(expandarg(args[n], ds))) {
909 sink_printf(output->sink, "%s", bool2str(result));
912 static void exp_not(int attribute((unused)) nargs,
915 void attribute((unused)) *u) {
916 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
919 static void exp_isplaying(int attribute((unused)) nargs,
920 char attribute((unused)) **args,
925 lookups(ds, DC_PLAYING);
926 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
929 static void exp_isqueue(int attribute((unused)) nargs,
930 char attribute((unused)) **args,
935 lookups(ds, DC_QUEUE);
936 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
939 static void exp_isrecent(int attribute((unused)) nargs,
940 char attribute((unused)) **args,
945 lookups(ds, DC_RECENT);
946 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
949 static void exp_isnew(int attribute((unused)) nargs,
950 char attribute((unused)) **args,
956 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
959 static void exp_id(int attribute((unused)) nargs,
960 char attribute((unused)) **args,
966 cgi_output(output, "%s", ds->track->id);
969 static void exp_track(int attribute((unused)) nargs,
970 char attribute((unused)) **args,
976 cgi_output(output, "%s", ds->track->track);
979 static void exp_parity(int attribute((unused)) nargs,
980 char attribute((unused)) **args,
985 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
988 static void exp_comment(int attribute((unused)) nargs,
989 char attribute((unused)) **args,
990 cgi_sink attribute((unused)) *output,
991 void attribute((unused)) *u) {
995 static void exp_prefname(int attribute((unused)) nargs,
996 char attribute((unused)) **args,
1001 if(ds->pref && ds->pref->name)
1002 cgi_output(output, "%s", ds->pref->name);
1005 static void exp_prefvalue(int attribute((unused)) nargs,
1006 char attribute((unused)) **args,
1011 if(ds->pref && ds->pref->value)
1012 cgi_output(output, "%s", ds->pref->value);
1015 static void exp_isfiles(int attribute((unused)) nargs,
1016 char attribute((unused)) **args,
1021 lookups(ds, DC_FILES);
1022 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1025 static void exp_isdirectories(int attribute((unused)) nargs,
1026 char attribute((unused)) **args,
1031 lookups(ds, DC_DIRS);
1032 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1035 static void exp_choose(int attribute((unused)) nargs,
1040 dcgi_state substate;
1044 const char *type, *what = expandarg(args[0], ds);
1046 if(!strcmp(what, "files")) {
1047 lookups(ds, DC_FILES);
1048 files = ds->g->files;
1049 nfiles = ds->g->nfiles;
1051 } else if(!strcmp(what, "directories")) {
1052 lookups(ds, DC_DIRS);
1053 files = ds->g->dirs;
1054 nfiles = ds->g->ndirs;
1057 error(0, "unknown @choose@ argument '%s'", what);
1060 e = xmalloc(nfiles * sizeof (struct entry));
1061 for(n = 0; n < nfiles; ++n) {
1062 e[n].path = files[n];
1063 e[n].sort = trackname_transform(type, files[n], "sort");
1064 e[n].display = trackname_transform(type, files[n], "display");
1066 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1067 memset(&substate, 0, sizeof substate);
1070 for(n = 0; n < nfiles; ++n) {
1071 substate.last = (n == nfiles - 1);
1073 substate.entry = &e[n];
1074 expandstring(output, args[1], &substate);
1079 static void exp_file(int attribute((unused)) nargs,
1080 char attribute((unused)) **args,
1086 cgi_output(output, "%s", ds->entry->path);
1088 cgi_output(output, "%s", ds->track->track);
1090 cgi_output(output, "%s", ds->tracks[0]);
1093 static void exp_transform(int nargs,
1096 void attribute((unused)) *u) {
1097 const char *context = nargs > 2 ? args[2] : "display";
1099 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1102 static void exp_urlquote(int attribute((unused)) nargs,
1105 void attribute((unused)) *u) {
1106 cgi_output(output, "%s", urlencodestring(args[0]));
1109 static void exp_scratchable(int attribute((unused)) nargs,
1110 char attribute((unused)) **args,
1112 void attribute((unused)) *u) {
1116 if(config->restrictions & RESTRICT_SCRATCH) {
1117 lookups(ds, DC_PLAYING);
1118 result = (ds->g->playing
1119 && (!ds->g->playing->submitter
1120 || !strcmp(ds->g->playing->submitter,
1121 disorder_user(ds->g->client))));
1124 sink_printf(output->sink, "%s", bool2str(result));
1127 static void exp_removable(int attribute((unused)) nargs,
1128 char attribute((unused)) **args,
1130 void attribute((unused)) *u) {
1134 if(config->restrictions & RESTRICT_REMOVE)
1136 && ds->track->submitter
1137 && !strcmp(ds->track->submitter,
1138 disorder_user(ds->g->client)));
1141 sink_printf(output->sink, "%s", bool2str(result));
1144 static void exp_navigate(int attribute((unused)) nargs,
1149 dcgi_state substate;
1150 const char *path = expandarg(args[0], ds);
1155 memset(&substate, 0, sizeof substate);
1157 ptr = path + 1; /* skip root */
1159 substate.nav_path = path;
1162 while(*ptr && *ptr != '/')
1164 substate.last = !*ptr;
1165 substate.nav_len = ptr - path;
1166 substate.nav_dirlen = dirlen;
1167 expandstring(output, args[1], &substate);
1168 dirlen = substate.nav_len;
1175 static void exp_fullname(int attribute((unused)) nargs,
1176 char attribute((unused)) **args,
1180 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1183 static void exp_basename(int nargs,
1191 if((s = strrchr(args[0], '/'))) ++s;
1193 cgi_output(output, "%s", s);
1195 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1196 ds->nav_path + ds->nav_dirlen + 1);
1199 static void exp_dirname(int nargs,
1207 if((s = strrchr(args[0], '/')))
1208 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1210 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1213 static void exp_eq(int attribute((unused)) nargs,
1216 void attribute((unused)) *u) {
1217 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1220 static void exp_ne(int attribute((unused)) nargs,
1223 void attribute((unused)) *u) {
1224 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1227 static void exp_enabled(int attribute((unused)) nargs,
1228 char attribute((unused)) **args,
1235 disorder_enabled(ds->g->client, &enabled);
1236 cgi_output(output, "%s", bool2str(enabled));
1239 static void exp_random_enabled(int attribute((unused)) nargs,
1240 char attribute((unused)) **args,
1247 disorder_random_enabled(ds->g->client, &enabled);
1248 cgi_output(output, "%s", bool2str(enabled));
1251 static void exp_trackstate(int attribute((unused)) nargs,
1256 struct queue_entry *q;
1259 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1260 lookups(ds, DC_QUEUE|DC_PLAYING);
1261 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1262 cgi_output(output, "playing");
1264 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1267 cgi_output(output, "queued");
1271 static void exp_thisurl(int attribute((unused)) nargs,
1272 char attribute((unused)) **args,
1274 void attribute((unused)) *u) {
1275 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1276 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1279 static void exp_isfirst(int attribute((unused)) nargs,
1280 char attribute((unused)) **args,
1285 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1288 static void exp_islast(int attribute((unused)) nargs,
1289 char attribute((unused)) **args,
1294 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1297 static void exp_action(int attribute((unused)) nargs,
1298 char attribute((unused)) **args,
1300 void attribute((unused)) *u) {
1301 const char *action = cgi_get("action"), *mgmt;
1303 if(!action) action = "playing";
1304 if(!strcmp(action, "playing")
1305 && (mgmt = cgi_get("mgmt"))
1306 && !strcmp(mgmt, "true"))
1308 sink_printf(output->sink, "%s", action);
1311 static void exp_resolve(int attribute((unused)) nargs,
1314 void attribute((unused)) *u) {
1318 if(!disorder_resolve(ds->g->client, &track, args[0]))
1319 sink_printf(output->sink, "%s", track);
1322 static void exp_paused(int attribute((unused)) nargs,
1323 char attribute((unused)) **args,
1329 lookups(ds, DC_PLAYING);
1330 if(ds->g->playing && ds->g->playing->state == playing_paused)
1332 cgi_output(output, "%s", bool2str(paused));
1335 static void exp_state(int attribute((unused)) nargs,
1336 char attribute((unused)) **args,
1342 cgi_output(output, "%s", playing_states[ds->track->state]);
1345 static void exp_files(int attribute((unused)) nargs,
1350 dcgi_state substate;
1351 const char *nfiles_arg, *directory;
1352 int nfiles, numfile;
1355 memset(&substate, 0, sizeof substate);
1357 if((directory = cgi_get("directory"))) {
1358 /* Prefs for whole directory. */
1359 lookups(ds, DC_FILES);
1360 /* Synthesize args for the file list. */
1361 nfiles = ds->g->nfiles;
1362 for(numfile = 0; numfile < nfiles; ++numfile) {
1363 k = xmalloc(sizeof *k);
1364 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1365 k->value = ds->g->files[numfile];
1370 /* Args already present. */
1371 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1374 for(numfile = 0; numfile < nfiles; ++numfile) {
1375 substate.index = numfile;
1376 expandstring(output, args[0], &substate);
1380 static void exp_index(int attribute((unused)) nargs,
1381 char attribute((unused)) **args,
1386 cgi_output(output, "%d", ds->index);
1389 static void exp_nfiles(int attribute((unused)) nargs,
1390 char attribute((unused)) **args,
1394 const char *files_arg;
1396 if(cgi_get("directory")) {
1397 lookups(ds, DC_FILES);
1398 cgi_output(output, "%d", ds->g->nfiles);
1399 } else if((files_arg = cgi_get("files")))
1400 cgi_output(output, "%s", files_arg);
1402 cgi_output(output, "1");
1405 static const struct cgi_expansion expansions[] = {
1406 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1407 { "action", 0, 0, 0, exp_action },
1408 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1409 { "arg", 1, 1, 0, exp_arg },
1410 { "basename", 0, 1, 0, exp_basename },
1411 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1412 { "dirname", 0, 1, 0, exp_dirname },
1413 { "enabled", 0, 0, 0, exp_enabled },
1414 { "eq", 2, 2, 0, exp_eq },
1415 { "file", 0, 0, 0, exp_file },
1416 { "files", 1, 1, EXP_MAGIC, exp_files },
1417 { "fullname", 0, 0, 0, exp_fullname },
1418 { "id", 0, 0, 0, exp_id },
1419 { "if", 2, 3, EXP_MAGIC, exp_if },
1420 { "include", 1, 1, 0, exp_include },
1421 { "index", 0, 0, 0, exp_index },
1422 { "isdirectories", 0, 0, 0, exp_isdirectories },
1423 { "isfiles", 0, 0, 0, exp_isfiles },
1424 { "isfirst", 0, 0, 0, exp_isfirst },
1425 { "islast", 0, 0, 0, exp_islast },
1426 { "isnew", 0, 0, 0, exp_isnew },
1427 { "isplaying", 0, 0, 0, exp_isplaying },
1428 { "isqueue", 0, 0, 0, exp_isqueue },
1429 { "isrecent", 0, 0, 0, exp_isrecent },
1430 { "label", 1, 1, 0, exp_label },
1431 { "length", 0, 0, 0, exp_length },
1432 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1433 { "ne", 2, 2, 0, exp_ne },
1434 { "new", 1, 1, EXP_MAGIC, exp_new },
1435 { "nfiles", 0, 0, 0, exp_nfiles },
1436 { "nonce", 0, 0, 0, exp_nonce },
1437 { "not", 1, 1, 0, exp_not },
1438 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1439 { "parity", 0, 0, 0, exp_parity },
1440 { "part", 1, 3, 0, exp_part },
1441 { "paused", 0, 0, 0, exp_paused },
1442 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1443 { "pref", 2, 2, 0, exp_pref },
1444 { "prefname", 0, 0, 0, exp_prefname },
1445 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1446 { "prefvalue", 0, 0, 0, exp_prefvalue },
1447 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1448 { "random-enabled", 0, 0, 0, exp_random_enabled },
1449 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1450 { "removable", 0, 0, 0, exp_removable },
1451 { "resolve", 1, 1, 0, exp_resolve },
1452 { "scratchable", 0, 0, 0, exp_scratchable },
1453 { "search", 2, 3, EXP_MAGIC, exp_search },
1454 { "server-version", 0, 0, 0, exp_server_version },
1455 { "shell", 1, 1, 0, exp_shell },
1456 { "state", 0, 0, 0, exp_state },
1457 { "stats", 0, 0, 0, exp_stats },
1458 { "thisurl", 0, 0, 0, exp_thisurl },
1459 { "track", 0, 0, 0, exp_track },
1460 { "trackstate", 1, 1, 0, exp_trackstate },
1461 { "transform", 2, 3, 0, exp_transform },
1462 { "url", 0, 0, 0, exp_url },
1463 { "urlquote", 1, 1, 0, exp_urlquote },
1464 { "version", 0, 0, 0, exp_version },
1465 { "volume", 1, 1, 0, exp_volume },
1466 { "when", 0, 0, 0, exp_when },
1467 { "who", 0, 0, 0, exp_who }
1470 static void expand(cgi_sink *output,
1471 const char *template,
1473 cgi_expand(template,
1474 expansions, sizeof expansions / sizeof *expansions,
1479 static void expandstring(cgi_sink *output,
1482 cgi_expand_string("",
1484 expansions, sizeof expansions / sizeof *expansions,
1489 static void perform_action(cgi_sink *output, dcgi_state *ds,
1490 const char *action) {
1493 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1494 actions[n].handler(output, ds);
1496 cgi_header(output->sink, "Content-Type", "text/html");
1497 cgi_body(output->sink);
1498 expand(output, action, ds);
1502 void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1503 const char *action = cgi_get("action");
1505 if(!action) action = "playing";
1506 perform_action(output, ds, action);
1509 void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1511 cgi_set_option("error", msg);
1512 perform_action(output, ds, "error");