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 header_cookie(struct sink *output) {
110 for(s = login_cookie; *s; ++s) {
112 dynstr_append(d, '\\');
113 dynstr_append(d, *s);
116 byte_xasprintf(&s, "disorder=\"%s\"", d->vec); /* TODO domain, path, expiry */
117 cgi_header(output, "Set-Cookie", s);
119 /* Force browser to discard cookie */
120 cgi_header(output, "Set-Cookie", "disorder=none;Max-Age=0");
123 static void redirect(struct sink *output) {
126 back = cgi_get("back");
127 cgi_header(output, "Location", back && *back ? back : front_url());
128 header_cookie(output);
132 static void expand_template(dcgi_state *ds, cgi_sink *output,
133 const char *action) {
134 cgi_header(output->sink, "Content-Type", "text/html");
135 header_cookie(output->sink);
136 cgi_body(output->sink);
137 expand(output, action, ds);
140 static void lookups(dcgi_state *ds, unsigned want) {
142 struct queue_entry *r, *rnext;
143 const char *dir, *re;
145 if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) {
147 disorder_queue(ds->g->client, &ds->g->queue);
148 if(need & DC_PLAYING)
149 disorder_playing(ds->g->client, &ds->g->playing);
151 disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0);
152 if(need & DC_RECENT) {
153 /* we need to reverse the order of the list */
154 disorder_recent(ds->g->client, &r);
157 r->next = ds->g->recent;
163 disorder_get_volume(ds->g->client,
164 &ds->g->volume_left, &ds->g->volume_right);
165 if(need & (DC_FILES|DC_DIRS)) {
166 if(!(dir = cgi_get("directory")))
168 re = cgi_get("regexp");
170 if(disorder_directories(ds->g->client, dir, re,
171 &ds->g->dirs, &ds->g->ndirs))
174 if(disorder_files(ds->g->client, dir, re,
175 &ds->g->files, &ds->g->nfiles))
178 ds->g->flags |= need;
182 /* actions ********************************************************************/
184 static void act_disable(cgi_sink *output,
187 disorder_disable(ds->g->client);
188 redirect(output->sink);
191 static void act_enable(cgi_sink *output,
194 disorder_enable(ds->g->client);
195 redirect(output->sink);
198 static void act_random_disable(cgi_sink *output,
201 disorder_random_disable(ds->g->client);
202 redirect(output->sink);
205 static void act_random_enable(cgi_sink *output,
208 disorder_random_enable(ds->g->client);
209 redirect(output->sink);
212 static void act_remove(cgi_sink *output,
216 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
218 disorder_remove(ds->g->client, id);
219 redirect(output->sink);
222 static void act_move(cgi_sink *output,
224 const char *id, *delta;
226 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
227 if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument");
229 disorder_move(ds->g->client, id, atoi(delta));
230 redirect(output->sink);
233 static void act_scratch(cgi_sink *output,
236 disorder_scratch(ds->g->client, cgi_get("id"));
237 redirect(output->sink);
240 static void act_playing(cgi_sink *output, dcgi_state *ds) {
242 long refresh = config->refresh, length;
244 int random_enabled = 0;
247 lookups(ds, DC_PLAYING|DC_QUEUE);
248 cgi_header(output->sink, "Content-Type", "text/html");
249 disorder_random_enabled(ds->g->client, &random_enabled);
250 disorder_enabled(ds->g->client, &enabled);
252 && ds->g->playing->state == playing_started /* i.e. not paused */
253 && !disorder_length(ds->g->client, ds->g->playing->track, &length)
255 && ds->g->playing->sofar >= 0) {
256 /* Try to put the next refresh at the start of the next track. */
258 fin = now + length - ds->g->playing->sofar + config->gap;
259 if(now + refresh > fin)
262 if(ds->g->queue && ds->g->queue->state == playing_isscratch) {
263 /* next track is a scratch, don't leave more than the inter-track gap */
264 if(refresh > config->gap)
265 refresh = config->gap;
267 if(!ds->g->playing && ((ds->g->queue
268 && ds->g->queue->state != playing_random)
269 || random_enabled) && enabled) {
270 /* no track playing but playing is enabled and there is something coming
271 * up, must be in a gap */
272 if(refresh > config->gap)
273 refresh = config->gap;
275 byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1,
277 cgi_header(output->sink, "Refresh", r);
278 header_cookie(output->sink);
279 cgi_body(output->sink);
280 expand(output, "playing", ds);
283 static void act_play(cgi_sink *output,
285 const char *track, *dir;
290 if((track = cgi_get("file"))) {
291 disorder_play(ds->g->client, track);
292 } else if((dir = cgi_get("directory"))) {
293 if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0;
295 e = xmalloc(ntracks * sizeof (struct entry));
296 for(n = 0; n < ntracks; ++n) {
297 e[n].path = tracks[n];
298 e[n].sort = trackname_transform("track", tracks[n], "sort");
299 e[n].display = trackname_transform("track", tracks[n], "display");
301 qsort(e, ntracks, sizeof (struct entry), compare_entry);
302 for(n = 0; n < ntracks; ++n)
303 disorder_play(ds->g->client, e[n].path);
306 /* XXX error handling */
307 redirect(output->sink);
310 static int clamp(int n, int min, int max) {
318 static const char *volume_url(void) {
321 byte_xasprintf(&url, "%s?action=volume", config->url);
325 static void act_volume(cgi_sink *output, dcgi_state *ds) {
326 const char *l, *r, *d, *back;
327 int nd, changed = 0;;
329 if((d = cgi_get("delta"))) {
330 lookups(ds, DC_VOLUME);
331 nd = clamp(atoi(d), -255, 255);
332 disorder_set_volume(ds->g->client,
333 clamp(ds->g->volume_left + nd, 0, 255),
334 clamp(ds->g->volume_right + nd, 0, 255));
336 } else if((l = cgi_get("left")) && (r = cgi_get("right"))) {
337 disorder_set_volume(ds->g->client, atoi(l), atoi(r));
341 /* redirect back to ourselves (but without the volume-changing bits in the
343 cgi_header(output->sink, "Location",
344 (back = cgi_get("back")) ? back : volume_url());
345 header_cookie(output->sink);
346 cgi_body(output->sink);
348 cgi_header(output->sink, "Content-Type", "text/html");
349 header_cookie(output->sink);
350 cgi_body(output->sink);
351 expand(output, "volume", ds);
355 static void act_prefs_errors(const char *msg,
356 void attribute((unused)) *u) {
357 fatal(0, "error splitting parts list: %s", msg);
360 static const char *numbered_arg(const char *argname, int numfile) {
363 byte_xasprintf(&fullname, "%d_%s", numfile, argname);
364 return cgi_get(fullname);
367 static void process_prefs(dcgi_state *ds, int numfile) {
368 const char *file, *name, *value, *part, *parts, *current, *context;
371 if(!(file = numbered_arg("file", numfile)))
372 /* The first file doesn't need numbering. */
373 if(numfile > 0 || !(file = cgi_get("file")))
375 if((parts = numbered_arg("parts", numfile))
376 || (parts = cgi_get("parts"))) {
377 /* Default context is display. Other contexts not actually tested. */
378 if(!(context = numbered_arg("context", numfile))) context = "display";
379 partslist = split(parts, 0, 0, act_prefs_errors, 0);
380 while((part = *partslist++)) {
381 if(!(value = numbered_arg(part, numfile)))
383 /* If it's already right (whether regexps or db) don't change anything,
384 * so we don't fill the database up with rubbish. */
385 if(disorder_part(ds->g->client, (char **)¤t,
386 file, context, part))
387 fatal(0, "disorder_part() failed");
388 if(!strcmp(current, value))
390 byte_xasprintf((char **)&name, "trackname_%s_%s", context, part);
391 disorder_set(ds->g->client, file, name, value);
393 if((value = numbered_arg("random", numfile)))
394 disorder_unset(ds->g->client, file, "pick_at_random");
396 disorder_set(ds->g->client, file, "pick_at_random", "0");
397 if((value = numbered_arg("tags", numfile)))
398 disorder_set(ds->g->client, file, "tags", value);
399 } else if((name = cgi_get("name"))) {
400 /* Raw preferences. Not well supported in the templates at the moment. */
401 value = cgi_get("value");
403 disorder_set(ds->g->client, file, name, value);
405 disorder_unset(ds->g->client, file, name);
409 static void act_prefs(cgi_sink *output, dcgi_state *ds) {
413 if((files = cgi_get("files"))) nfiles = atoi(files);
415 for(numfile = 0; numfile < nfiles; ++numfile)
416 process_prefs(ds, numfile);
417 cgi_header(output->sink, "Content-Type", "text/html");
418 header_cookie(output->sink);
419 cgi_body(output->sink);
420 expand(output, "prefs", ds);
423 static void act_pause(cgi_sink *output,
426 disorder_pause(ds->g->client);
427 redirect(output->sink);
430 static void act_resume(cgi_sink *output,
433 disorder_resume(ds->g->client);
434 redirect(output->sink);
437 static void act_login(cgi_sink *output,
439 const char *username, *password, *back;
442 username = cgi_get("username");
443 password = cgi_get("password");
444 if(!username || !password
445 || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) {
446 /* We're just visiting the login page */
447 expand_template(ds, output, "login");
451 if(disorder_connect_user(c, username, password)) {
452 cgi_set_option("error", "loginfailed");
453 expand_template(ds, output, "login");
456 if(disorder_make_cookie(c, &login_cookie)) {
457 cgi_set_option("error", "cookiefailed");
458 expand_template(ds, output, "login");
461 /* We have a new cookie */
462 header_cookie(output->sink);
463 if((back = cgi_get("back")) && back)
464 /* Redirect back to somewhere or other */
465 redirect(output->sink);
467 /* Stick to the login page */
468 expand_template(ds, output, "login");
471 static void act_logout(cgi_sink *output,
473 disorder_revoke(ds->g->client);
475 /* Reconnect as guest */
476 ds->g->client = disorder_new(0);
477 if(disorder_connect_cookie(ds->g->client, 0)) {
478 disorder_cgi_error(output, ds, "connect");
481 /* Back to the login page */
482 expand_template(ds, output, "login");
485 static void act_register(cgi_sink *output,
487 const char *username, *password, *email;
490 username = cgi_get("username");
491 password = cgi_get("password");
492 email = cgi_get("email");
494 if(!username || !*username) {
495 cgi_set_option("error", "nousername");
496 expand_template(ds, output, "login");
499 if(!password || !*password) {
500 cgi_set_option("error", "nopassword");
501 expand_template(ds, output, "login");
504 if(!email || !*email) {
505 cgi_set_option("error", "noemail");
506 expand_template(ds, output, "login");
509 /* We could well do better address validation but for now we'll just do the
511 if(!strchr(email, '@')) {
512 cgi_set_option("error", "bademail");
513 expand_template(ds, output, "login");
516 if(disorder_register(ds->g->client, username, password, email, &confirm)) {
517 cgi_set_option("error", "cannotregister");
518 expand_template(ds, output, "login");
521 /* We'll go back to the login page with a suitable message */
522 cgi_set_option("registered", "registeredok");
523 expand_template(ds, output, "login");
526 static const struct action {
528 void (*handler)(cgi_sink *output, dcgi_state *ds);
530 { "disable", act_disable },
531 { "enable", act_enable },
532 { "login", act_login },
533 { "logout", act_logout },
534 { "move", act_move },
535 { "pause", act_pause },
536 { "play", act_play },
537 { "playing", act_playing },
538 { "prefs", act_prefs },
539 { "random-disable", act_random_disable },
540 { "random-enable", act_random_enable },
541 { "register", act_register },
542 { "remove", act_remove },
543 { "resume", act_resume },
544 { "scratch", act_scratch },
545 { "volume", act_volume },
548 /* expansions *****************************************************************/
550 static void exp_include(int attribute((unused)) nargs,
554 expand(output, args[0], u);
557 static void exp_server_version(int attribute((unused)) nargs,
558 char attribute((unused)) **args,
565 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
567 v = "(server not running)";
568 cgi_output(output, "%s", v);
571 static void exp_version(int attribute((unused)) nargs,
572 char attribute((unused)) **args,
574 void attribute((unused)) *u) {
575 cgi_output(output, "%s", disorder_short_version_string);
578 static void exp_nonce(int attribute((unused)) nargs,
579 char attribute((unused)) **args,
581 void attribute((unused)) *u) {
582 cgi_output(output, "%s", nonce());
585 static void exp_label(int attribute((unused)) nargs,
588 void attribute((unused)) *u) {
589 cgi_output(output, "%s", cgi_label(args[0]));
592 struct trackinfo_state {
594 const struct queue_entry *q;
599 static void exp_who(int attribute((unused)) nargs,
600 char attribute((unused)) **args,
605 if(ds->track && ds->track->submitter)
606 cgi_output(output, "%s", ds->track->submitter);
609 static void exp_length(int attribute((unused)) nargs,
610 char attribute((unused)) **args,
617 && (ds->track->state == playing_started
618 || ds->track->state == playing_paused)
619 && ds->track->sofar >= 0)
620 cgi_output(output, "%ld:%02ld/",
621 ds->track->sofar / 60, ds->track->sofar % 60);
624 disorder_length(ds->g->client, ds->track->track, &length);
626 disorder_length(ds->g->client, ds->tracks[0], &length);
628 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
630 sink_printf(output->sink, "%s", " ");
633 static void exp_when(int attribute((unused)) nargs,
634 char attribute((unused)) **args,
638 const struct tm *w = 0;
641 switch(ds->track->state) {
642 case playing_isscratch:
643 case playing_unplayed:
645 if(ds->track->expected)
646 w = localtime(&ds->track->expected);
649 case playing_no_player:
651 case playing_scratched:
652 case playing_started:
654 case playing_quitting:
655 if(ds->track->played)
656 w = localtime(&ds->track->played);
660 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
662 sink_printf(output->sink, " ");
665 static void exp_part(int nargs,
670 const char *s, *track, *part, *context;
676 track = ds->track->track;
678 track = ds->tracks[0];
696 if(disorder_part(ds->g->client, (char **)&s, track,
697 !strcmp(context, "short") ? "display" : context, part))
698 fatal(0, "disorder_part() failed");
699 if(!strcmp(context, "short"))
700 s = truncate_for_display(s, config->short_display);
701 cgi_output(output, "%s", s);
703 sink_printf(output->sink, " ");
706 static void exp_playing(int attribute((unused)) nargs,
713 lookups(ds, DC_PLAYING);
714 memset(&s, 0, sizeof s);
717 s.track = ds->g->playing;
718 expandstring(output, args[0], &s);
722 static void exp_queue(int attribute((unused)) nargs,
728 struct queue_entry *q;
730 lookups(ds, DC_QUEUE);
731 memset(&s, 0, sizeof s);
734 for(q = ds->g->queue; q; q = q->next) {
737 expandstring(output, args[0], &s);
743 static void exp_recent(int attribute((unused)) nargs,
749 struct queue_entry *q;
751 lookups(ds, DC_RECENT);
752 memset(&s, 0, sizeof s);
755 for(q = ds->g->recent; q; q = q->next) {
758 expandstring(output, args[0], &s);
764 static void exp_new(int attribute((unused)) nargs,
772 memset(&s, 0, sizeof s);
775 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
776 s.last = s.index + 1 < ds->g->nnew;
777 s.tracks = &ds->g->new[s.index];
778 expandstring(output, args[0], &s);
783 static void exp_url(int attribute((unused)) nargs,
784 char attribute((unused)) **args,
786 void attribute((unused)) *u) {
787 cgi_output(output, "%s", config->url);
795 static int compare_result(const void *a, const void *b) {
796 const struct result *ra = a, *rb = b;
799 if(!(c = strcmp(ra->sort, rb->sort)))
800 c = strcmp(ra->track, rb->track);
804 static void exp_search(int nargs,
808 dcgi_state *ds = u, substate;
810 const char *q, *context, *part, *template;
826 assert(!"should never happen");
827 part = context = template = 0; /* quieten compiler */
829 if(ds->tracks == 0) {
830 /* we are the top level, let's get some search results */
831 if(!(q = cgi_get("query"))) return; /* no results yet */
832 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
836 ntracks = ds->ntracks;
838 assert(ntracks != 0);
839 /* sort tracks by the appropriate part */
840 r = xmalloc(ntracks * sizeof *r);
841 for(n = 0; n < ntracks; ++n) {
842 r[n].track = tracks[n];
843 if(disorder_part(ds->g->client, (char **)&r[n].sort,
844 tracks[n], context, part))
845 fatal(0, "disorder_part() failed");
847 qsort(r, ntracks, sizeof (struct result), compare_result);
848 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
849 * array as we know it's guaranteed to be big enough and isn't going to be
850 * used for anything else any more. */
851 memset(&substate, 0, sizeof substate);
856 substate.tracks = tracks;
857 substate.ntracks = 0;
860 && !strcmp(r[m].sort, r[n].sort))
861 tracks[substate.ntracks++] = r[m++].track;
862 substate.last = (m == ntracks);
863 expandstring(output, template, &substate);
868 assert(substate.last != 0);
871 static void exp_arg(int attribute((unused)) nargs,
874 void attribute((unused)) *u) {
877 if((v = cgi_get(args[0])))
878 cgi_output(output, "%s", v);
881 static void exp_stats(int attribute((unused)) nargs,
882 char attribute((unused)) **args,
888 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
889 if(!disorder_stats(ds->g->client, &v, 0)) {
891 cgi_output(output, "%s\n", *v++);
893 cgi_closetag(output->sink, "pre");
896 static void exp_volume(int attribute((unused)) nargs,
902 lookups(ds, DC_VOLUME);
903 if(!strcmp(args[0], "left"))
904 cgi_output(output, "%d", ds->g->volume_left);
906 cgi_output(output, "%d", ds->g->volume_right);
909 static void exp_shell(int attribute((unused)) nargs,
912 void attribute((unused)) *u) {
918 if(!(pid = xfork())) {
923 execlp("sh", "sh", "-c", args[0], (char *)0);
924 fatal(errno, "error executing sh");
927 while((n = read(p[0], buffer, sizeof buffer))) {
929 if(errno == EINTR) continue;
930 else fatal(errno, "error reading from pipe");
932 output->sink->write(output->sink, buffer, n);
935 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
937 if(n < 0) fatal(errno, "error calling waitpid");
939 error(0, "shell command '%s' %s", args[0], wstat(w));
942 static inline int str2bool(const char *s) {
943 return !strcmp(s, "true");
946 static inline const char *bool2str(int n) {
947 return n ? "true" : "false";
950 static char *expandarg(const char *arg, dcgi_state *ds) {
956 output.sink = sink_dynstr(&d);
957 expandstring(&output, arg, ds);
958 dynstr_terminate(&d);
962 static void exp_prefs(int attribute((unused)) nargs,
969 const char *file = expandarg(args[0], ds);
971 memset(&substate, 0, sizeof substate);
974 if(disorder_prefs(ds->g->client, file, &k)) return;
976 substate.last = !k->next;
978 expandstring(output, args[1], &substate);
985 static void exp_pref(int attribute((unused)) nargs,
992 if(!disorder_get(ds->g->client, args[0], args[1], &value))
993 cgi_output(output, "%s", value);
996 static void exp_if(int nargs,
1001 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
1004 expandstring(output, args[n], ds);
1007 static void exp_and(int nargs,
1014 for(n = 0; n < nargs; ++n)
1015 if(!str2bool(expandarg(args[n], ds))) {
1019 sink_printf(output->sink, "%s", bool2str(result));
1022 static void exp_or(int nargs,
1029 for(n = 0; n < nargs; ++n)
1030 if(str2bool(expandarg(args[n], ds))) {
1034 sink_printf(output->sink, "%s", bool2str(result));
1037 static void exp_not(int attribute((unused)) nargs,
1040 void attribute((unused)) *u) {
1041 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
1044 static void exp_isplaying(int attribute((unused)) nargs,
1045 char attribute((unused)) **args,
1050 lookups(ds, DC_PLAYING);
1051 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
1054 static void exp_isqueue(int attribute((unused)) nargs,
1055 char attribute((unused)) **args,
1060 lookups(ds, DC_QUEUE);
1061 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
1064 static void exp_isrecent(int attribute((unused)) nargs,
1065 char attribute((unused)) **args,
1070 lookups(ds, DC_RECENT);
1071 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
1074 static void exp_isnew(int attribute((unused)) nargs,
1075 char attribute((unused)) **args,
1080 lookups(ds, DC_NEW);
1081 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
1084 static void exp_id(int attribute((unused)) nargs,
1085 char attribute((unused)) **args,
1091 cgi_output(output, "%s", ds->track->id);
1094 static void exp_track(int attribute((unused)) nargs,
1095 char attribute((unused)) **args,
1101 cgi_output(output, "%s", ds->track->track);
1104 static void exp_parity(int attribute((unused)) nargs,
1105 char attribute((unused)) **args,
1110 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
1113 static void exp_comment(int attribute((unused)) nargs,
1114 char attribute((unused)) **args,
1115 cgi_sink attribute((unused)) *output,
1116 void attribute((unused)) *u) {
1120 static void exp_prefname(int attribute((unused)) nargs,
1121 char attribute((unused)) **args,
1126 if(ds->pref && ds->pref->name)
1127 cgi_output(output, "%s", ds->pref->name);
1130 static void exp_prefvalue(int attribute((unused)) nargs,
1131 char attribute((unused)) **args,
1136 if(ds->pref && ds->pref->value)
1137 cgi_output(output, "%s", ds->pref->value);
1140 static void exp_isfiles(int attribute((unused)) nargs,
1141 char attribute((unused)) **args,
1146 lookups(ds, DC_FILES);
1147 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1150 static void exp_isdirectories(int attribute((unused)) nargs,
1151 char attribute((unused)) **args,
1156 lookups(ds, DC_DIRS);
1157 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1160 static void exp_choose(int attribute((unused)) nargs,
1165 dcgi_state substate;
1169 const char *type, *what = expandarg(args[0], ds);
1171 if(!strcmp(what, "files")) {
1172 lookups(ds, DC_FILES);
1173 files = ds->g->files;
1174 nfiles = ds->g->nfiles;
1176 } else if(!strcmp(what, "directories")) {
1177 lookups(ds, DC_DIRS);
1178 files = ds->g->dirs;
1179 nfiles = ds->g->ndirs;
1182 error(0, "unknown @choose@ argument '%s'", what);
1185 e = xmalloc(nfiles * sizeof (struct entry));
1186 for(n = 0; n < nfiles; ++n) {
1187 e[n].path = files[n];
1188 e[n].sort = trackname_transform(type, files[n], "sort");
1189 e[n].display = trackname_transform(type, files[n], "display");
1191 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1192 memset(&substate, 0, sizeof substate);
1195 for(n = 0; n < nfiles; ++n) {
1196 substate.last = (n == nfiles - 1);
1198 substate.entry = &e[n];
1199 expandstring(output, args[1], &substate);
1204 static void exp_file(int attribute((unused)) nargs,
1205 char attribute((unused)) **args,
1211 cgi_output(output, "%s", ds->entry->path);
1213 cgi_output(output, "%s", ds->track->track);
1215 cgi_output(output, "%s", ds->tracks[0]);
1218 static void exp_transform(int nargs,
1221 void attribute((unused)) *u) {
1222 const char *context = nargs > 2 ? args[2] : "display";
1224 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1227 static void exp_urlquote(int attribute((unused)) nargs,
1230 void attribute((unused)) *u) {
1231 cgi_output(output, "%s", urlencodestring(args[0]));
1234 static void exp_scratchable(int attribute((unused)) nargs,
1235 char attribute((unused)) **args,
1237 void attribute((unused)) *u) {
1241 if(config->restrictions & RESTRICT_SCRATCH) {
1242 lookups(ds, DC_PLAYING);
1243 result = (ds->g->playing
1244 && (!ds->g->playing->submitter
1245 || !strcmp(ds->g->playing->submitter,
1246 disorder_user(ds->g->client))));
1249 sink_printf(output->sink, "%s", bool2str(result));
1252 static void exp_removable(int attribute((unused)) nargs,
1253 char attribute((unused)) **args,
1255 void attribute((unused)) *u) {
1259 if(config->restrictions & RESTRICT_REMOVE)
1261 && ds->track->submitter
1262 && !strcmp(ds->track->submitter,
1263 disorder_user(ds->g->client)));
1266 sink_printf(output->sink, "%s", bool2str(result));
1269 static void exp_navigate(int attribute((unused)) nargs,
1274 dcgi_state substate;
1275 const char *path = expandarg(args[0], ds);
1280 memset(&substate, 0, sizeof substate);
1282 ptr = path + 1; /* skip root */
1284 substate.nav_path = path;
1287 while(*ptr && *ptr != '/')
1289 substate.last = !*ptr;
1290 substate.nav_len = ptr - path;
1291 substate.nav_dirlen = dirlen;
1292 expandstring(output, args[1], &substate);
1293 dirlen = substate.nav_len;
1300 static void exp_fullname(int attribute((unused)) nargs,
1301 char attribute((unused)) **args,
1305 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1308 static void exp_basename(int nargs,
1316 if((s = strrchr(args[0], '/'))) ++s;
1318 cgi_output(output, "%s", s);
1320 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1321 ds->nav_path + ds->nav_dirlen + 1);
1324 static void exp_dirname(int nargs,
1332 if((s = strrchr(args[0], '/')))
1333 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1335 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1338 static void exp_eq(int attribute((unused)) nargs,
1341 void attribute((unused)) *u) {
1342 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1345 static void exp_ne(int attribute((unused)) nargs,
1348 void attribute((unused)) *u) {
1349 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1352 static void exp_enabled(int attribute((unused)) nargs,
1353 char attribute((unused)) **args,
1360 disorder_enabled(ds->g->client, &enabled);
1361 cgi_output(output, "%s", bool2str(enabled));
1364 static void exp_random_enabled(int attribute((unused)) nargs,
1365 char attribute((unused)) **args,
1372 disorder_random_enabled(ds->g->client, &enabled);
1373 cgi_output(output, "%s", bool2str(enabled));
1376 static void exp_trackstate(int attribute((unused)) nargs,
1381 struct queue_entry *q;
1384 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1385 lookups(ds, DC_QUEUE|DC_PLAYING);
1386 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1387 cgi_output(output, "playing");
1389 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1392 cgi_output(output, "queued");
1396 static void exp_thisurl(int attribute((unused)) nargs,
1397 char attribute((unused)) **args,
1399 void attribute((unused)) *u) {
1400 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1401 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1404 static void exp_isfirst(int attribute((unused)) nargs,
1405 char attribute((unused)) **args,
1410 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1413 static void exp_islast(int attribute((unused)) nargs,
1414 char attribute((unused)) **args,
1419 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1422 static void exp_action(int attribute((unused)) nargs,
1423 char attribute((unused)) **args,
1425 void attribute((unused)) *u) {
1426 const char *action = cgi_get("action"), *mgmt;
1428 if(!action) action = "playing";
1429 if(!strcmp(action, "playing")
1430 && (mgmt = cgi_get("mgmt"))
1431 && !strcmp(mgmt, "true"))
1433 sink_printf(output->sink, "%s", action);
1436 static void exp_resolve(int attribute((unused)) nargs,
1439 void attribute((unused)) *u) {
1443 if(!disorder_resolve(ds->g->client, &track, args[0]))
1444 sink_printf(output->sink, "%s", track);
1447 static void exp_paused(int attribute((unused)) nargs,
1448 char attribute((unused)) **args,
1454 lookups(ds, DC_PLAYING);
1455 if(ds->g->playing && ds->g->playing->state == playing_paused)
1457 cgi_output(output, "%s", bool2str(paused));
1460 static void exp_state(int attribute((unused)) nargs,
1461 char attribute((unused)) **args,
1467 cgi_output(output, "%s", playing_states[ds->track->state]);
1470 static void exp_files(int attribute((unused)) nargs,
1475 dcgi_state substate;
1476 const char *nfiles_arg, *directory;
1477 int nfiles, numfile;
1480 memset(&substate, 0, sizeof substate);
1482 if((directory = cgi_get("directory"))) {
1483 /* Prefs for whole directory. */
1484 lookups(ds, DC_FILES);
1485 /* Synthesize args for the file list. */
1486 nfiles = ds->g->nfiles;
1487 for(numfile = 0; numfile < nfiles; ++numfile) {
1488 k = xmalloc(sizeof *k);
1489 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1490 k->value = ds->g->files[numfile];
1495 /* Args already present. */
1496 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1499 for(numfile = 0; numfile < nfiles; ++numfile) {
1500 substate.index = numfile;
1501 expandstring(output, args[0], &substate);
1505 static void exp_index(int attribute((unused)) nargs,
1506 char attribute((unused)) **args,
1511 cgi_output(output, "%d", ds->index);
1514 static void exp_nfiles(int attribute((unused)) nargs,
1515 char attribute((unused)) **args,
1519 const char *files_arg;
1521 if(cgi_get("directory")) {
1522 lookups(ds, DC_FILES);
1523 cgi_output(output, "%d", ds->g->nfiles);
1524 } else if((files_arg = cgi_get("files")))
1525 cgi_output(output, "%s", files_arg);
1527 cgi_output(output, "1");
1530 static void exp_user(int attribute((unused)) nargs,
1531 char attribute((unused)) **args,
1534 dcgi_state *const ds = u;
1536 cgi_output(output, "%s", disorder_user(ds->g->client));
1539 static const struct cgi_expansion expansions[] = {
1540 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1541 { "action", 0, 0, 0, exp_action },
1542 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1543 { "arg", 1, 1, 0, exp_arg },
1544 { "basename", 0, 1, 0, exp_basename },
1545 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1546 { "dirname", 0, 1, 0, exp_dirname },
1547 { "enabled", 0, 0, 0, exp_enabled },
1548 { "eq", 2, 2, 0, exp_eq },
1549 { "file", 0, 0, 0, exp_file },
1550 { "files", 1, 1, EXP_MAGIC, exp_files },
1551 { "fullname", 0, 0, 0, exp_fullname },
1552 { "id", 0, 0, 0, exp_id },
1553 { "if", 2, 3, EXP_MAGIC, exp_if },
1554 { "include", 1, 1, 0, exp_include },
1555 { "index", 0, 0, 0, exp_index },
1556 { "isdirectories", 0, 0, 0, exp_isdirectories },
1557 { "isfiles", 0, 0, 0, exp_isfiles },
1558 { "isfirst", 0, 0, 0, exp_isfirst },
1559 { "islast", 0, 0, 0, exp_islast },
1560 { "isnew", 0, 0, 0, exp_isnew },
1561 { "isplaying", 0, 0, 0, exp_isplaying },
1562 { "isqueue", 0, 0, 0, exp_isqueue },
1563 { "isrecent", 0, 0, 0, exp_isrecent },
1564 { "label", 1, 1, 0, exp_label },
1565 { "length", 0, 0, 0, exp_length },
1566 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1567 { "ne", 2, 2, 0, exp_ne },
1568 { "new", 1, 1, EXP_MAGIC, exp_new },
1569 { "nfiles", 0, 0, 0, exp_nfiles },
1570 { "nonce", 0, 0, 0, exp_nonce },
1571 { "not", 1, 1, 0, exp_not },
1572 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1573 { "parity", 0, 0, 0, exp_parity },
1574 { "part", 1, 3, 0, exp_part },
1575 { "paused", 0, 0, 0, exp_paused },
1576 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1577 { "pref", 2, 2, 0, exp_pref },
1578 { "prefname", 0, 0, 0, exp_prefname },
1579 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1580 { "prefvalue", 0, 0, 0, exp_prefvalue },
1581 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1582 { "random-enabled", 0, 0, 0, exp_random_enabled },
1583 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1584 { "removable", 0, 0, 0, exp_removable },
1585 { "resolve", 1, 1, 0, exp_resolve },
1586 { "scratchable", 0, 0, 0, exp_scratchable },
1587 { "search", 2, 3, EXP_MAGIC, exp_search },
1588 { "server-version", 0, 0, 0, exp_server_version },
1589 { "shell", 1, 1, 0, exp_shell },
1590 { "state", 0, 0, 0, exp_state },
1591 { "stats", 0, 0, 0, exp_stats },
1592 { "thisurl", 0, 0, 0, exp_thisurl },
1593 { "track", 0, 0, 0, exp_track },
1594 { "trackstate", 1, 1, 0, exp_trackstate },
1595 { "transform", 2, 3, 0, exp_transform },
1596 { "url", 0, 0, 0, exp_url },
1597 { "urlquote", 1, 1, 0, exp_urlquote },
1598 { "user", 0, 0, 0, exp_user },
1599 { "version", 0, 0, 0, exp_version },
1600 { "volume", 1, 1, 0, exp_volume },
1601 { "when", 0, 0, 0, exp_when },
1602 { "who", 0, 0, 0, exp_who }
1605 static void expand(cgi_sink *output,
1606 const char *template,
1608 cgi_expand(template,
1609 expansions, sizeof expansions / sizeof *expansions,
1614 static void expandstring(cgi_sink *output,
1617 cgi_expand_string("",
1619 expansions, sizeof expansions / sizeof *expansions,
1624 static void perform_action(cgi_sink *output, dcgi_state *ds,
1625 const char *action) {
1628 /* We don't ever want anything to be cached */
1629 cgi_header(output->sink, "Cache-Control", "no-cache");
1630 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1631 actions[n].handler(output, ds);
1633 expand_template(ds, output, action);
1636 void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1637 const char *action = cgi_get("action");
1639 if(!action) action = "playing";
1640 perform_action(output, ds, action);
1643 void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1645 cgi_set_option("error", msg);
1646 perform_action(output, ds, "error");