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"
55 #include "trackname.h"
60 static void expand(cgi_sink *output,
63 static void expandstring(cgi_sink *output,
73 static const char *nonce(void) {
74 static unsigned long count;
77 byte_xasprintf(&s, "%lx%lx%lx",
78 (unsigned long)time(0),
79 (unsigned long)getpid(),
84 static int compare_entry(const void *a, const void *b) {
85 const struct entry *ea = a, *eb = b;
87 return compare_tracks(ea->sort, eb->sort,
88 ea->display, eb->display,
92 static const char *front_url(void) {
96 /* preserve management interface visibility */
97 if((mgmt = cgi_get("mgmt")) && !strcmp(mgmt, "true")) {
98 byte_xasprintf(&url, "%s?mgmt=true", config->url);
104 static void redirect(struct sink *output) {
107 back = cgi_get("back");
108 cgi_header(output, "Location", back && *back ? back : front_url());
112 static void header_cookie(cgi_sink *output) {
118 for(s = login_cookie; *s; ++s) {
120 dynstr_append(d, '\\');
121 dynstr_append(d, *s);
124 byte_xasprintf(&s, "disorder=\"%s\"", d->vec); /* TODO domain, path, expiry */
125 cgi_header(output->sink, "Set-Cookie", s);
129 static void expand_template(dcgi_state *ds, cgi_sink *output,
130 const char *action) {
131 cgi_header(output->sink, "Content-Type", "text/html");
132 cgi_body(output->sink);
133 expand(output, action, ds);
136 static void lookups(dcgi_state *ds, unsigned want) {
138 struct queue_entry *r, *rnext;
139 const char *dir, *re;
141 if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) {
143 disorder_queue(ds->g->client, &ds->g->queue);
144 if(need & DC_PLAYING)
145 disorder_playing(ds->g->client, &ds->g->playing);
147 disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0);
148 if(need & DC_RECENT) {
149 /* we need to reverse the order of the list */
150 disorder_recent(ds->g->client, &r);
153 r->next = ds->g->recent;
159 disorder_get_volume(ds->g->client,
160 &ds->g->volume_left, &ds->g->volume_right);
161 if(need & (DC_FILES|DC_DIRS)) {
162 if(!(dir = cgi_get("directory")))
164 re = cgi_get("regexp");
166 if(disorder_directories(ds->g->client, dir, re,
167 &ds->g->dirs, &ds->g->ndirs))
170 if(disorder_files(ds->g->client, dir, re,
171 &ds->g->files, &ds->g->nfiles))
174 ds->g->flags |= need;
178 /* actions ********************************************************************/
180 static void act_disable(cgi_sink *output,
183 disorder_disable(ds->g->client);
184 redirect(output->sink);
187 static void act_enable(cgi_sink *output,
190 disorder_enable(ds->g->client);
191 redirect(output->sink);
194 static void act_random_disable(cgi_sink *output,
197 disorder_random_disable(ds->g->client);
198 redirect(output->sink);
201 static void act_random_enable(cgi_sink *output,
204 disorder_random_enable(ds->g->client);
205 redirect(output->sink);
208 static void act_remove(cgi_sink *output,
212 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
214 disorder_remove(ds->g->client, id);
215 redirect(output->sink);
218 static void act_move(cgi_sink *output,
220 const char *id, *delta;
222 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
223 if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument");
225 disorder_move(ds->g->client, id, atoi(delta));
226 redirect(output->sink);
229 static void act_scratch(cgi_sink *output,
232 disorder_scratch(ds->g->client, cgi_get("id"));
233 redirect(output->sink);
236 static void act_playing(cgi_sink *output, dcgi_state *ds) {
238 long refresh = config->refresh, length;
240 int random_enabled = 0;
243 lookups(ds, DC_PLAYING|DC_QUEUE);
244 cgi_header(output->sink, "Content-Type", "text/html");
245 disorder_random_enabled(ds->g->client, &random_enabled);
246 disorder_enabled(ds->g->client, &enabled);
248 && ds->g->playing->state == playing_started /* i.e. not paused */
249 && !disorder_length(ds->g->client, ds->g->playing->track, &length)
251 && ds->g->playing->sofar >= 0) {
252 /* Try to put the next refresh at the start of the next track. */
254 fin = now + length - ds->g->playing->sofar + config->gap;
255 if(now + refresh > fin)
258 if(ds->g->queue && ds->g->queue->state == playing_isscratch) {
259 /* next track is a scratch, don't leave more than the inter-track gap */
260 if(refresh > config->gap)
261 refresh = config->gap;
263 if(!ds->g->playing && ((ds->g->queue
264 && ds->g->queue->state != playing_random)
265 || random_enabled) && enabled) {
266 /* no track playing but playing is enabled and there is something coming
267 * up, must be in a gap */
268 if(refresh > config->gap)
269 refresh = config->gap;
271 byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1,
273 cgi_header(output->sink, "Refresh", r);
274 cgi_body(output->sink);
275 expand(output, "playing", ds);
278 static void act_play(cgi_sink *output,
280 const char *track, *dir;
285 if((track = cgi_get("file"))) {
286 disorder_play(ds->g->client, track);
287 } else if((dir = cgi_get("directory"))) {
288 if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0;
290 e = xmalloc(ntracks * sizeof (struct entry));
291 for(n = 0; n < ntracks; ++n) {
292 e[n].path = tracks[n];
293 e[n].sort = trackname_transform("track", tracks[n], "sort");
294 e[n].display = trackname_transform("track", tracks[n], "display");
296 qsort(e, ntracks, sizeof (struct entry), compare_entry);
297 for(n = 0; n < ntracks; ++n)
298 disorder_play(ds->g->client, e[n].path);
301 /* XXX error handling */
302 redirect(output->sink);
305 static int clamp(int n, int min, int max) {
313 static const char *volume_url(void) {
316 byte_xasprintf(&url, "%s?action=volume", config->url);
320 static void act_volume(cgi_sink *output, dcgi_state *ds) {
321 const char *l, *r, *d, *back;
322 int nd, changed = 0;;
324 if((d = cgi_get("delta"))) {
325 lookups(ds, DC_VOLUME);
326 nd = clamp(atoi(d), -255, 255);
327 disorder_set_volume(ds->g->client,
328 clamp(ds->g->volume_left + nd, 0, 255),
329 clamp(ds->g->volume_right + nd, 0, 255));
331 } else if((l = cgi_get("left")) && (r = cgi_get("right"))) {
332 disorder_set_volume(ds->g->client, atoi(l), atoi(r));
336 /* redirect back to ourselves (but without the volume-changing bits in the
338 cgi_header(output->sink, "Location",
339 (back = cgi_get("back")) ? back : volume_url());
340 cgi_body(output->sink);
342 cgi_header(output->sink, "Content-Type", "text/html");
343 cgi_body(output->sink);
344 expand(output, "volume", ds);
348 static void act_prefs_errors(const char *msg,
349 void attribute((unused)) *u) {
350 fatal(0, "error splitting parts list: %s", msg);
353 static const char *numbered_arg(const char *argname, int numfile) {
356 byte_xasprintf(&fullname, "%d_%s", numfile, argname);
357 return cgi_get(fullname);
360 static void process_prefs(dcgi_state *ds, int numfile) {
361 const char *file, *name, *value, *part, *parts, *current, *context;
364 if(!(file = numbered_arg("file", numfile)))
365 /* The first file doesn't need numbering. */
366 if(numfile > 0 || !(file = cgi_get("file")))
368 if((parts = numbered_arg("parts", numfile))
369 || (parts = cgi_get("parts"))) {
370 /* Default context is display. Other contexts not actually tested. */
371 if(!(context = numbered_arg("context", numfile))) context = "display";
372 partslist = split(parts, 0, 0, act_prefs_errors, 0);
373 while((part = *partslist++)) {
374 if(!(value = numbered_arg(part, numfile)))
376 /* If it's already right (whether regexps or db) don't change anything,
377 * so we don't fill the database up with rubbish. */
378 if(disorder_part(ds->g->client, (char **)¤t,
379 file, context, part))
380 fatal(0, "disorder_part() failed");
381 if(!strcmp(current, value))
383 byte_xasprintf((char **)&name, "trackname_%s_%s", context, part);
384 disorder_set(ds->g->client, file, name, value);
386 if((value = numbered_arg("random", numfile)))
387 disorder_unset(ds->g->client, file, "pick_at_random");
389 disorder_set(ds->g->client, file, "pick_at_random", "0");
390 if((value = numbered_arg("tags", numfile)))
391 disorder_set(ds->g->client, file, "tags", value);
392 } else if((name = cgi_get("name"))) {
393 /* Raw preferences. Not well supported in the templates at the moment. */
394 value = cgi_get("value");
396 disorder_set(ds->g->client, file, name, value);
398 disorder_unset(ds->g->client, file, name);
402 static void act_prefs(cgi_sink *output, dcgi_state *ds) {
406 if((files = cgi_get("files"))) nfiles = atoi(files);
408 for(numfile = 0; numfile < nfiles; ++numfile)
409 process_prefs(ds, numfile);
410 cgi_header(output->sink, "Content-Type", "text/html");
411 cgi_body(output->sink);
412 expand(output, "prefs", ds);
415 static void act_pause(cgi_sink *output,
418 disorder_pause(ds->g->client);
419 redirect(output->sink);
422 static void act_resume(cgi_sink *output,
425 disorder_resume(ds->g->client);
426 redirect(output->sink);
429 static void act_login(cgi_sink *output,
431 const char *username, *password, *back;
434 username = cgi_get("username");
435 password = cgi_get("password");
436 if(!username || !password
437 || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) {
438 /* We're just visiting the login page */
439 expand_template(ds, output, "login");
443 if(disorder_connect_user(c, username, password)) {
444 cgi_set_option("error", "loginfailed");
445 expand_template(ds, output, "login");
448 if(disorder_make_cookie(c, &login_cookie)) {
449 cgi_set_option("error", "cookiefailed");
450 expand_template(ds, output, "login");
453 /* We have a new cookie */
454 header_cookie(output);
455 if((back = cgi_get("back")) && back)
456 /* Redirect back to somewhere or other */
457 redirect(output->sink);
459 /* Stick to the login page */
460 expand_template(ds, output, "login");
463 static void act_logout(cgi_sink *output,
465 disorder_revoke(ds->g->client);
467 /* Back to the login page */
468 expand_template(ds, output, "login");
471 static void act_register(cgi_sink *output,
473 const char *username, *password, *email;
476 username = cgi_get("username");
477 password = cgi_get("password");
478 email = cgi_get("email");
480 if(!username || !*username) {
481 cgi_set_option("error", "nousername");
482 expand_template(ds, output, "login");
485 if(!password || !*password) {
486 cgi_set_option("error", "nopassword");
487 expand_template(ds, output, "login");
490 if(!email || !*email) {
491 cgi_set_option("error", "noemail");
492 expand_template(ds, output, "login");
495 /* We could well do better address validation but for now we'll just do the
497 if(!strchr(email, '@')) {
498 cgi_set_option("error", "bademail");
499 expand_template(ds, output, "login");
502 if(disorder_register(ds->g->client, username, password, email, &confirm)) {
503 cgi_set_option("error", "cannotregister");
504 expand_template(ds, output, "login");
507 /* We'll go back to the login page with a suitable message */
508 cgi_set_option("registered", "registeredok");
509 expand_template(ds, output, "login");
512 static const struct action {
514 void (*handler)(cgi_sink *output, dcgi_state *ds);
516 { "disable", act_disable },
517 { "enable", act_enable },
518 { "login", act_login },
519 { "logout", act_logout },
520 { "move", act_move },
521 { "pause", act_pause },
522 { "play", act_play },
523 { "playing", act_playing },
524 { "prefs", act_prefs },
525 { "random-disable", act_random_disable },
526 { "random-enable", act_random_enable },
527 { "register", act_register },
528 { "remove", act_remove },
529 { "resume", act_resume },
530 { "scratch", act_scratch },
531 { "volume", act_volume },
534 /* expansions *****************************************************************/
536 static void exp_include(int attribute((unused)) nargs,
540 expand(output, args[0], u);
543 static void exp_server_version(int attribute((unused)) nargs,
544 char attribute((unused)) **args,
551 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
553 v = "(server not running)";
554 cgi_output(output, "%s", v);
557 static void exp_version(int attribute((unused)) nargs,
558 char attribute((unused)) **args,
560 void attribute((unused)) *u) {
561 cgi_output(output, "%s", disorder_short_version_string);
564 static void exp_nonce(int attribute((unused)) nargs,
565 char attribute((unused)) **args,
567 void attribute((unused)) *u) {
568 cgi_output(output, "%s", nonce());
571 static void exp_label(int attribute((unused)) nargs,
574 void attribute((unused)) *u) {
575 cgi_output(output, "%s", cgi_label(args[0]));
578 struct trackinfo_state {
580 const struct queue_entry *q;
585 static void exp_who(int attribute((unused)) nargs,
586 char attribute((unused)) **args,
591 if(ds->track && ds->track->submitter)
592 cgi_output(output, "%s", ds->track->submitter);
595 static void exp_length(int attribute((unused)) nargs,
596 char attribute((unused)) **args,
603 && (ds->track->state == playing_started
604 || ds->track->state == playing_paused)
605 && ds->track->sofar >= 0)
606 cgi_output(output, "%ld:%02ld/",
607 ds->track->sofar / 60, ds->track->sofar % 60);
610 disorder_length(ds->g->client, ds->track->track, &length);
612 disorder_length(ds->g->client, ds->tracks[0], &length);
614 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
616 sink_printf(output->sink, "%s", " ");
619 static void exp_when(int attribute((unused)) nargs,
620 char attribute((unused)) **args,
624 const struct tm *w = 0;
627 switch(ds->track->state) {
628 case playing_isscratch:
629 case playing_unplayed:
631 if(ds->track->expected)
632 w = localtime(&ds->track->expected);
635 case playing_no_player:
637 case playing_scratched:
638 case playing_started:
640 case playing_quitting:
641 if(ds->track->played)
642 w = localtime(&ds->track->played);
646 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
648 sink_printf(output->sink, " ");
651 static void exp_part(int nargs,
656 const char *s, *track, *part, *context;
662 track = ds->track->track;
664 track = ds->tracks[0];
682 if(disorder_part(ds->g->client, (char **)&s, track,
683 !strcmp(context, "short") ? "display" : context, part))
684 fatal(0, "disorder_part() failed");
685 if(!strcmp(context, "short"))
686 s = truncate_for_display(s, config->short_display);
687 cgi_output(output, "%s", s);
689 sink_printf(output->sink, " ");
692 static void exp_playing(int attribute((unused)) nargs,
699 lookups(ds, DC_PLAYING);
700 memset(&s, 0, sizeof s);
703 s.track = ds->g->playing;
704 expandstring(output, args[0], &s);
708 static void exp_queue(int attribute((unused)) nargs,
714 struct queue_entry *q;
716 lookups(ds, DC_QUEUE);
717 memset(&s, 0, sizeof s);
720 for(q = ds->g->queue; q; q = q->next) {
723 expandstring(output, args[0], &s);
729 static void exp_recent(int attribute((unused)) nargs,
735 struct queue_entry *q;
737 lookups(ds, DC_RECENT);
738 memset(&s, 0, sizeof s);
741 for(q = ds->g->recent; q; q = q->next) {
744 expandstring(output, args[0], &s);
750 static void exp_new(int attribute((unused)) nargs,
758 memset(&s, 0, sizeof s);
761 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
762 s.last = s.index + 1 < ds->g->nnew;
763 s.tracks = &ds->g->new[s.index];
764 expandstring(output, args[0], &s);
769 static void exp_url(int attribute((unused)) nargs,
770 char attribute((unused)) **args,
772 void attribute((unused)) *u) {
773 cgi_output(output, "%s", config->url);
781 static int compare_result(const void *a, const void *b) {
782 const struct result *ra = a, *rb = b;
785 if(!(c = strcmp(ra->sort, rb->sort)))
786 c = strcmp(ra->track, rb->track);
790 static void exp_search(int nargs,
794 dcgi_state *ds = u, substate;
796 const char *q, *context, *part, *template;
812 assert(!"should never happen");
813 part = context = template = 0; /* quieten compiler */
815 if(ds->tracks == 0) {
816 /* we are the top level, let's get some search results */
817 if(!(q = cgi_get("query"))) return; /* no results yet */
818 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
822 ntracks = ds->ntracks;
824 assert(ntracks != 0);
825 /* sort tracks by the appropriate part */
826 r = xmalloc(ntracks * sizeof *r);
827 for(n = 0; n < ntracks; ++n) {
828 r[n].track = tracks[n];
829 if(disorder_part(ds->g->client, (char **)&r[n].sort,
830 tracks[n], context, part))
831 fatal(0, "disorder_part() failed");
833 qsort(r, ntracks, sizeof (struct result), compare_result);
834 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
835 * array as we know it's guaranteed to be big enough and isn't going to be
836 * used for anything else any more. */
837 memset(&substate, 0, sizeof substate);
842 substate.tracks = tracks;
843 substate.ntracks = 0;
846 && !strcmp(r[m].sort, r[n].sort))
847 tracks[substate.ntracks++] = r[m++].track;
848 substate.last = (m == ntracks);
849 expandstring(output, template, &substate);
854 assert(substate.last != 0);
857 static void exp_arg(int attribute((unused)) nargs,
860 void attribute((unused)) *u) {
863 if((v = cgi_get(args[0])))
864 cgi_output(output, "%s", v);
867 static void exp_stats(int attribute((unused)) nargs,
868 char attribute((unused)) **args,
874 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
875 if(!disorder_stats(ds->g->client, &v, 0)) {
877 cgi_output(output, "%s\n", *v++);
879 cgi_closetag(output->sink, "pre");
882 static void exp_volume(int attribute((unused)) nargs,
888 lookups(ds, DC_VOLUME);
889 if(!strcmp(args[0], "left"))
890 cgi_output(output, "%d", ds->g->volume_left);
892 cgi_output(output, "%d", ds->g->volume_right);
895 static void exp_shell(int attribute((unused)) nargs,
898 void attribute((unused)) *u) {
904 if(!(pid = xfork())) {
909 execlp("sh", "sh", "-c", args[0], (char *)0);
910 fatal(errno, "error executing sh");
913 while((n = read(p[0], buffer, sizeof buffer))) {
915 if(errno == EINTR) continue;
916 else fatal(errno, "error reading from pipe");
918 output->sink->write(output->sink, buffer, n);
921 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
923 if(n < 0) fatal(errno, "error calling waitpid");
925 error(0, "shell command '%s' %s", args[0], wstat(w));
928 static inline int str2bool(const char *s) {
929 return !strcmp(s, "true");
932 static inline const char *bool2str(int n) {
933 return n ? "true" : "false";
936 static char *expandarg(const char *arg, dcgi_state *ds) {
942 output.sink = sink_dynstr(&d);
943 expandstring(&output, arg, ds);
944 dynstr_terminate(&d);
948 static void exp_prefs(int attribute((unused)) nargs,
955 const char *file = expandarg(args[0], ds);
957 memset(&substate, 0, sizeof substate);
960 if(disorder_prefs(ds->g->client, file, &k)) return;
962 substate.last = !k->next;
964 expandstring(output, args[1], &substate);
971 static void exp_pref(int attribute((unused)) nargs,
978 if(!disorder_get(ds->g->client, args[0], args[1], &value))
979 cgi_output(output, "%s", value);
982 static void exp_if(int nargs,
987 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
990 expandstring(output, args[n], ds);
993 static void exp_and(int nargs,
1000 for(n = 0; n < nargs; ++n)
1001 if(!str2bool(expandarg(args[n], ds))) {
1005 sink_printf(output->sink, "%s", bool2str(result));
1008 static void exp_or(int nargs,
1015 for(n = 0; n < nargs; ++n)
1016 if(str2bool(expandarg(args[n], ds))) {
1020 sink_printf(output->sink, "%s", bool2str(result));
1023 static void exp_not(int attribute((unused)) nargs,
1026 void attribute((unused)) *u) {
1027 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
1030 static void exp_isplaying(int attribute((unused)) nargs,
1031 char attribute((unused)) **args,
1036 lookups(ds, DC_PLAYING);
1037 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
1040 static void exp_isqueue(int attribute((unused)) nargs,
1041 char attribute((unused)) **args,
1046 lookups(ds, DC_QUEUE);
1047 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
1050 static void exp_isrecent(int attribute((unused)) nargs,
1051 char attribute((unused)) **args,
1056 lookups(ds, DC_RECENT);
1057 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
1060 static void exp_isnew(int attribute((unused)) nargs,
1061 char attribute((unused)) **args,
1066 lookups(ds, DC_NEW);
1067 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
1070 static void exp_id(int attribute((unused)) nargs,
1071 char attribute((unused)) **args,
1077 cgi_output(output, "%s", ds->track->id);
1080 static void exp_track(int attribute((unused)) nargs,
1081 char attribute((unused)) **args,
1087 cgi_output(output, "%s", ds->track->track);
1090 static void exp_parity(int attribute((unused)) nargs,
1091 char attribute((unused)) **args,
1096 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
1099 static void exp_comment(int attribute((unused)) nargs,
1100 char attribute((unused)) **args,
1101 cgi_sink attribute((unused)) *output,
1102 void attribute((unused)) *u) {
1106 static void exp_prefname(int attribute((unused)) nargs,
1107 char attribute((unused)) **args,
1112 if(ds->pref && ds->pref->name)
1113 cgi_output(output, "%s", ds->pref->name);
1116 static void exp_prefvalue(int attribute((unused)) nargs,
1117 char attribute((unused)) **args,
1122 if(ds->pref && ds->pref->value)
1123 cgi_output(output, "%s", ds->pref->value);
1126 static void exp_isfiles(int attribute((unused)) nargs,
1127 char attribute((unused)) **args,
1132 lookups(ds, DC_FILES);
1133 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1136 static void exp_isdirectories(int attribute((unused)) nargs,
1137 char attribute((unused)) **args,
1142 lookups(ds, DC_DIRS);
1143 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1146 static void exp_choose(int attribute((unused)) nargs,
1151 dcgi_state substate;
1155 const char *type, *what = expandarg(args[0], ds);
1157 if(!strcmp(what, "files")) {
1158 lookups(ds, DC_FILES);
1159 files = ds->g->files;
1160 nfiles = ds->g->nfiles;
1162 } else if(!strcmp(what, "directories")) {
1163 lookups(ds, DC_DIRS);
1164 files = ds->g->dirs;
1165 nfiles = ds->g->ndirs;
1168 error(0, "unknown @choose@ argument '%s'", what);
1171 e = xmalloc(nfiles * sizeof (struct entry));
1172 for(n = 0; n < nfiles; ++n) {
1173 e[n].path = files[n];
1174 e[n].sort = trackname_transform(type, files[n], "sort");
1175 e[n].display = trackname_transform(type, files[n], "display");
1177 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1178 memset(&substate, 0, sizeof substate);
1181 for(n = 0; n < nfiles; ++n) {
1182 substate.last = (n == nfiles - 1);
1184 substate.entry = &e[n];
1185 expandstring(output, args[1], &substate);
1190 static void exp_file(int attribute((unused)) nargs,
1191 char attribute((unused)) **args,
1197 cgi_output(output, "%s", ds->entry->path);
1199 cgi_output(output, "%s", ds->track->track);
1201 cgi_output(output, "%s", ds->tracks[0]);
1204 static void exp_transform(int nargs,
1207 void attribute((unused)) *u) {
1208 const char *context = nargs > 2 ? args[2] : "display";
1210 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1213 static void exp_urlquote(int attribute((unused)) nargs,
1216 void attribute((unused)) *u) {
1217 cgi_output(output, "%s", urlencodestring(args[0]));
1220 static void exp_scratchable(int attribute((unused)) nargs,
1221 char attribute((unused)) **args,
1223 void attribute((unused)) *u) {
1227 if(config->restrictions & RESTRICT_SCRATCH) {
1228 lookups(ds, DC_PLAYING);
1229 result = (ds->g->playing
1230 && (!ds->g->playing->submitter
1231 || !strcmp(ds->g->playing->submitter,
1232 disorder_user(ds->g->client))));
1235 sink_printf(output->sink, "%s", bool2str(result));
1238 static void exp_removable(int attribute((unused)) nargs,
1239 char attribute((unused)) **args,
1241 void attribute((unused)) *u) {
1245 if(config->restrictions & RESTRICT_REMOVE)
1247 && ds->track->submitter
1248 && !strcmp(ds->track->submitter,
1249 disorder_user(ds->g->client)));
1252 sink_printf(output->sink, "%s", bool2str(result));
1255 static void exp_navigate(int attribute((unused)) nargs,
1260 dcgi_state substate;
1261 const char *path = expandarg(args[0], ds);
1266 memset(&substate, 0, sizeof substate);
1268 ptr = path + 1; /* skip root */
1270 substate.nav_path = path;
1273 while(*ptr && *ptr != '/')
1275 substate.last = !*ptr;
1276 substate.nav_len = ptr - path;
1277 substate.nav_dirlen = dirlen;
1278 expandstring(output, args[1], &substate);
1279 dirlen = substate.nav_len;
1286 static void exp_fullname(int attribute((unused)) nargs,
1287 char attribute((unused)) **args,
1291 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1294 static void exp_basename(int nargs,
1302 if((s = strrchr(args[0], '/'))) ++s;
1304 cgi_output(output, "%s", s);
1306 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1307 ds->nav_path + ds->nav_dirlen + 1);
1310 static void exp_dirname(int nargs,
1318 if((s = strrchr(args[0], '/')))
1319 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1321 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1324 static void exp_eq(int attribute((unused)) nargs,
1327 void attribute((unused)) *u) {
1328 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1331 static void exp_ne(int attribute((unused)) nargs,
1334 void attribute((unused)) *u) {
1335 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1338 static void exp_enabled(int attribute((unused)) nargs,
1339 char attribute((unused)) **args,
1346 disorder_enabled(ds->g->client, &enabled);
1347 cgi_output(output, "%s", bool2str(enabled));
1350 static void exp_random_enabled(int attribute((unused)) nargs,
1351 char attribute((unused)) **args,
1358 disorder_random_enabled(ds->g->client, &enabled);
1359 cgi_output(output, "%s", bool2str(enabled));
1362 static void exp_trackstate(int attribute((unused)) nargs,
1367 struct queue_entry *q;
1370 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1371 lookups(ds, DC_QUEUE|DC_PLAYING);
1372 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1373 cgi_output(output, "playing");
1375 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1378 cgi_output(output, "queued");
1382 static void exp_thisurl(int attribute((unused)) nargs,
1383 char attribute((unused)) **args,
1385 void attribute((unused)) *u) {
1386 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1387 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1390 static void exp_isfirst(int attribute((unused)) nargs,
1391 char attribute((unused)) **args,
1396 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1399 static void exp_islast(int attribute((unused)) nargs,
1400 char attribute((unused)) **args,
1405 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1408 static void exp_action(int attribute((unused)) nargs,
1409 char attribute((unused)) **args,
1411 void attribute((unused)) *u) {
1412 const char *action = cgi_get("action"), *mgmt;
1414 if(!action) action = "playing";
1415 if(!strcmp(action, "playing")
1416 && (mgmt = cgi_get("mgmt"))
1417 && !strcmp(mgmt, "true"))
1419 sink_printf(output->sink, "%s", action);
1422 static void exp_resolve(int attribute((unused)) nargs,
1425 void attribute((unused)) *u) {
1429 if(!disorder_resolve(ds->g->client, &track, args[0]))
1430 sink_printf(output->sink, "%s", track);
1433 static void exp_paused(int attribute((unused)) nargs,
1434 char attribute((unused)) **args,
1440 lookups(ds, DC_PLAYING);
1441 if(ds->g->playing && ds->g->playing->state == playing_paused)
1443 cgi_output(output, "%s", bool2str(paused));
1446 static void exp_state(int attribute((unused)) nargs,
1447 char attribute((unused)) **args,
1453 cgi_output(output, "%s", playing_states[ds->track->state]);
1456 static void exp_files(int attribute((unused)) nargs,
1461 dcgi_state substate;
1462 const char *nfiles_arg, *directory;
1463 int nfiles, numfile;
1466 memset(&substate, 0, sizeof substate);
1468 if((directory = cgi_get("directory"))) {
1469 /* Prefs for whole directory. */
1470 lookups(ds, DC_FILES);
1471 /* Synthesize args for the file list. */
1472 nfiles = ds->g->nfiles;
1473 for(numfile = 0; numfile < nfiles; ++numfile) {
1474 k = xmalloc(sizeof *k);
1475 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1476 k->value = ds->g->files[numfile];
1481 /* Args already present. */
1482 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1485 for(numfile = 0; numfile < nfiles; ++numfile) {
1486 substate.index = numfile;
1487 expandstring(output, args[0], &substate);
1491 static void exp_index(int attribute((unused)) nargs,
1492 char attribute((unused)) **args,
1497 cgi_output(output, "%d", ds->index);
1500 static void exp_nfiles(int attribute((unused)) nargs,
1501 char attribute((unused)) **args,
1505 const char *files_arg;
1507 if(cgi_get("directory")) {
1508 lookups(ds, DC_FILES);
1509 cgi_output(output, "%d", ds->g->nfiles);
1510 } else if((files_arg = cgi_get("files")))
1511 cgi_output(output, "%s", files_arg);
1513 cgi_output(output, "1");
1516 static void exp_user(int attribute((unused)) nargs,
1517 char attribute((unused)) **args,
1520 dcgi_state *const ds = u;
1522 cgi_output(output, "%s", disorder_user(ds->g->client));
1525 static const struct cgi_expansion expansions[] = {
1526 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1527 { "action", 0, 0, 0, exp_action },
1528 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1529 { "arg", 1, 1, 0, exp_arg },
1530 { "basename", 0, 1, 0, exp_basename },
1531 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1532 { "dirname", 0, 1, 0, exp_dirname },
1533 { "enabled", 0, 0, 0, exp_enabled },
1534 { "eq", 2, 2, 0, exp_eq },
1535 { "file", 0, 0, 0, exp_file },
1536 { "files", 1, 1, EXP_MAGIC, exp_files },
1537 { "fullname", 0, 0, 0, exp_fullname },
1538 { "id", 0, 0, 0, exp_id },
1539 { "if", 2, 3, EXP_MAGIC, exp_if },
1540 { "include", 1, 1, 0, exp_include },
1541 { "index", 0, 0, 0, exp_index },
1542 { "isdirectories", 0, 0, 0, exp_isdirectories },
1543 { "isfiles", 0, 0, 0, exp_isfiles },
1544 { "isfirst", 0, 0, 0, exp_isfirst },
1545 { "islast", 0, 0, 0, exp_islast },
1546 { "isnew", 0, 0, 0, exp_isnew },
1547 { "isplaying", 0, 0, 0, exp_isplaying },
1548 { "isqueue", 0, 0, 0, exp_isqueue },
1549 { "isrecent", 0, 0, 0, exp_isrecent },
1550 { "label", 1, 1, 0, exp_label },
1551 { "length", 0, 0, 0, exp_length },
1552 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1553 { "ne", 2, 2, 0, exp_ne },
1554 { "new", 1, 1, EXP_MAGIC, exp_new },
1555 { "nfiles", 0, 0, 0, exp_nfiles },
1556 { "nonce", 0, 0, 0, exp_nonce },
1557 { "not", 1, 1, 0, exp_not },
1558 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1559 { "parity", 0, 0, 0, exp_parity },
1560 { "part", 1, 3, 0, exp_part },
1561 { "paused", 0, 0, 0, exp_paused },
1562 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1563 { "pref", 2, 2, 0, exp_pref },
1564 { "prefname", 0, 0, 0, exp_prefname },
1565 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1566 { "prefvalue", 0, 0, 0, exp_prefvalue },
1567 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1568 { "random-enabled", 0, 0, 0, exp_random_enabled },
1569 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1570 { "removable", 0, 0, 0, exp_removable },
1571 { "resolve", 1, 1, 0, exp_resolve },
1572 { "scratchable", 0, 0, 0, exp_scratchable },
1573 { "search", 2, 3, EXP_MAGIC, exp_search },
1574 { "server-version", 0, 0, 0, exp_server_version },
1575 { "shell", 1, 1, 0, exp_shell },
1576 { "state", 0, 0, 0, exp_state },
1577 { "stats", 0, 0, 0, exp_stats },
1578 { "thisurl", 0, 0, 0, exp_thisurl },
1579 { "track", 0, 0, 0, exp_track },
1580 { "trackstate", 1, 1, 0, exp_trackstate },
1581 { "transform", 2, 3, 0, exp_transform },
1582 { "url", 0, 0, 0, exp_url },
1583 { "urlquote", 1, 1, 0, exp_urlquote },
1584 { "user", 0, 0, 0, exp_user },
1585 { "version", 0, 0, 0, exp_version },
1586 { "volume", 1, 1, 0, exp_volume },
1587 { "when", 0, 0, 0, exp_when },
1588 { "who", 0, 0, 0, exp_who }
1591 static void expand(cgi_sink *output,
1592 const char *template,
1594 cgi_expand(template,
1595 expansions, sizeof expansions / sizeof *expansions,
1600 static void expandstring(cgi_sink *output,
1603 cgi_expand_string("",
1605 expansions, sizeof expansions / sizeof *expansions,
1610 static void perform_action(cgi_sink *output, dcgi_state *ds,
1611 const char *action) {
1614 /* If we have a login cookie it'd better appear in all responses */
1615 header_cookie(output);
1616 /* We don't ever want anything to be cached */
1617 cgi_header(output->sink, "Cache-Control", "no-cache");
1618 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1619 actions[n].handler(output, ds);
1621 expand_template(ds, output, action);
1624 void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1625 const char *action = cgi_get("action");
1627 if(!action) action = "playing";
1628 perform_action(output, ds, action);
1631 void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1633 cgi_set_option("error", msg);
1634 perform_action(output, ds, "error");