2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
26 #include <sys/types.h>
27 #include <sys/socket.h>
43 #include "configuration.h"
54 #include "trackname.h"
63 static void expand(cgi_sink *output,
66 static void expandstring(cgi_sink *output,
76 static const char *nonce(void) {
77 static unsigned long count;
80 byte_xasprintf(&s, "%lx%lx%lx",
81 (unsigned long)time(0),
82 (unsigned long)getpid(),
87 static int compare_entry(const void *a, const void *b) {
88 const struct entry *ea = a, *eb = b;
90 return compare_tracks(ea->sort, eb->sort,
91 ea->display, eb->display,
95 static const char *front_url(void) {
99 /* preserve management interface visibility */
100 if((mgmt = cgi_get("mgmt")) && !strcmp(mgmt, "true")) {
101 byte_xasprintf(&url, "%s?mgmt=true", config->url);
107 static void header_cookie(struct sink *output) {
111 memset(&u, 0, sizeof u);
113 parse_url(config->url, &u);
115 dynstr_append_string(d, "disorder=");
116 dynstr_append_string(d, quote822(login_cookie, 0));
118 /* Force browser to discard cookie */
119 dynstr_append_string(d, "disorder=none;Max-Age=0");
122 /* The default domain matches the request host, so we need not override
123 * that. But the default path only goes up to the rightmost /, which would
124 * cause the browser to expose the cookie to other CGI programs on the same
126 dynstr_append_string(d, ";Path=");
127 dynstr_append_string(d, quote822(u.path, 0));
130 cgi_header(output, "Set-Cookie", d->vec);
133 static void redirect(struct sink *output) {
136 back = cgi_get("back");
137 cgi_header(output, "Location", back && *back ? back : front_url());
138 header_cookie(output);
142 static void expand_template(dcgi_state *ds, cgi_sink *output,
143 const char *action) {
144 cgi_header(output->sink, "Content-Type", "text/html");
145 header_cookie(output->sink);
146 cgi_body(output->sink);
147 expand(output, action, ds);
150 static void lookups(dcgi_state *ds, unsigned want) {
152 struct queue_entry *r, *rnext;
153 const char *dir, *re;
156 if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) {
158 disorder_queue(ds->g->client, &ds->g->queue);
159 if(need & DC_PLAYING)
160 disorder_playing(ds->g->client, &ds->g->playing);
162 disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0);
163 if(need & DC_RECENT) {
164 /* we need to reverse the order of the list */
165 disorder_recent(ds->g->client, &r);
168 r->next = ds->g->recent;
174 disorder_get_volume(ds->g->client,
175 &ds->g->volume_left, &ds->g->volume_right);
176 if(need & (DC_FILES|DC_DIRS)) {
177 if(!(dir = cgi_get("directory")))
179 re = cgi_get("regexp");
181 if(disorder_directories(ds->g->client, dir, re,
182 &ds->g->dirs, &ds->g->ndirs))
185 if(disorder_files(ds->g->client, dir, re,
186 &ds->g->files, &ds->g->nfiles))
189 if(need & DC_RIGHTS) {
190 ds->g->rights = RIGHT_READ; /* fail-safe */
191 if(!disorder_userinfo(ds->g->client, disorder_user(ds->g->client),
193 parse_rights(rights, &ds->g->rights, 1);
195 ds->g->flags |= need;
199 /* actions ********************************************************************/
201 static void act_disable(cgi_sink *output,
204 disorder_disable(ds->g->client);
205 redirect(output->sink);
208 static void act_enable(cgi_sink *output,
211 disorder_enable(ds->g->client);
212 redirect(output->sink);
215 static void act_random_disable(cgi_sink *output,
218 disorder_random_disable(ds->g->client);
219 redirect(output->sink);
222 static void act_random_enable(cgi_sink *output,
225 disorder_random_enable(ds->g->client);
226 redirect(output->sink);
229 static void act_remove(cgi_sink *output,
233 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
235 disorder_remove(ds->g->client, id);
236 redirect(output->sink);
239 static void act_move(cgi_sink *output,
241 const char *id, *delta;
243 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
244 if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument");
246 disorder_move(ds->g->client, id, atoi(delta));
247 redirect(output->sink);
250 static void act_scratch(cgi_sink *output,
253 disorder_scratch(ds->g->client, cgi_get("id"));
254 redirect(output->sink);
257 static void act_playing(cgi_sink *output, dcgi_state *ds) {
259 long refresh = config->refresh, length;
261 int random_enabled = 0;
264 lookups(ds, DC_PLAYING|DC_QUEUE);
265 cgi_header(output->sink, "Content-Type", "text/html");
266 disorder_random_enabled(ds->g->client, &random_enabled);
267 disorder_enabled(ds->g->client, &enabled);
269 && ds->g->playing->state == playing_started /* i.e. not paused */
270 && !disorder_length(ds->g->client, ds->g->playing->track, &length)
272 && ds->g->playing->sofar >= 0) {
273 /* Try to put the next refresh at the start of the next track. */
275 fin = now + length - ds->g->playing->sofar + config->gap;
276 if(now + refresh > fin)
279 if(ds->g->queue && ds->g->queue->state == playing_isscratch) {
280 /* next track is a scratch, don't leave more than the inter-track gap */
281 if(refresh > config->gap)
282 refresh = config->gap;
284 if(!ds->g->playing && ((ds->g->queue
285 && ds->g->queue->state != playing_random)
286 || random_enabled) && enabled) {
287 /* no track playing but playing is enabled and there is something coming
288 * up, must be in a gap */
289 if(refresh > config->gap)
290 refresh = config->gap;
292 byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1,
294 cgi_header(output->sink, "Refresh", r);
295 header_cookie(output->sink);
296 cgi_body(output->sink);
297 expand(output, "playing", ds);
300 static void act_play(cgi_sink *output,
302 const char *track, *dir;
307 if((track = cgi_get("file"))) {
308 disorder_play(ds->g->client, track);
309 } else if((dir = cgi_get("directory"))) {
310 if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0;
312 e = xmalloc(ntracks * sizeof (struct entry));
313 for(n = 0; n < ntracks; ++n) {
314 e[n].path = tracks[n];
315 e[n].sort = trackname_transform("track", tracks[n], "sort");
316 e[n].display = trackname_transform("track", tracks[n], "display");
318 qsort(e, ntracks, sizeof (struct entry), compare_entry);
319 for(n = 0; n < ntracks; ++n)
320 disorder_play(ds->g->client, e[n].path);
323 /* XXX error handling */
324 redirect(output->sink);
327 static int clamp(int n, int min, int max) {
335 static const char *volume_url(void) {
338 byte_xasprintf(&url, "%s?action=volume", config->url);
342 static void act_volume(cgi_sink *output, dcgi_state *ds) {
343 const char *l, *r, *d, *back;
344 int nd, changed = 0;;
346 if((d = cgi_get("delta"))) {
347 lookups(ds, DC_VOLUME);
348 nd = clamp(atoi(d), -255, 255);
349 disorder_set_volume(ds->g->client,
350 clamp(ds->g->volume_left + nd, 0, 255),
351 clamp(ds->g->volume_right + nd, 0, 255));
353 } else if((l = cgi_get("left")) && (r = cgi_get("right"))) {
354 disorder_set_volume(ds->g->client, atoi(l), atoi(r));
358 /* redirect back to ourselves (but without the volume-changing bits in the
360 cgi_header(output->sink, "Location",
361 (back = cgi_get("back")) ? back : volume_url());
362 header_cookie(output->sink);
363 cgi_body(output->sink);
365 cgi_header(output->sink, "Content-Type", "text/html");
366 header_cookie(output->sink);
367 cgi_body(output->sink);
368 expand(output, "volume", ds);
372 static void act_prefs_errors(const char *msg,
373 void attribute((unused)) *u) {
374 fatal(0, "error splitting parts list: %s", msg);
377 static const char *numbered_arg(const char *argname, int numfile) {
380 byte_xasprintf(&fullname, "%d_%s", numfile, argname);
381 return cgi_get(fullname);
384 static void process_prefs(dcgi_state *ds, int numfile) {
385 const char *file, *name, *value, *part, *parts, *current, *context;
388 if(!(file = numbered_arg("file", numfile)))
389 /* The first file doesn't need numbering. */
390 if(numfile > 0 || !(file = cgi_get("file")))
392 if((parts = numbered_arg("parts", numfile))
393 || (parts = cgi_get("parts"))) {
394 /* Default context is display. Other contexts not actually tested. */
395 if(!(context = numbered_arg("context", numfile))) context = "display";
396 partslist = split(parts, 0, 0, act_prefs_errors, 0);
397 while((part = *partslist++)) {
398 if(!(value = numbered_arg(part, numfile)))
400 /* If it's already right (whether regexps or db) don't change anything,
401 * so we don't fill the database up with rubbish. */
402 if(disorder_part(ds->g->client, (char **)¤t,
403 file, context, part))
404 fatal(0, "disorder_part() failed");
405 if(!strcmp(current, value))
407 byte_xasprintf((char **)&name, "trackname_%s_%s", context, part);
408 disorder_set(ds->g->client, file, name, value);
410 if((value = numbered_arg("random", numfile)))
411 disorder_unset(ds->g->client, file, "pick_at_random");
413 disorder_set(ds->g->client, file, "pick_at_random", "0");
414 if((value = numbered_arg("tags", numfile)))
415 disorder_set(ds->g->client, file, "tags", value);
416 } else if((name = cgi_get("name"))) {
417 /* Raw preferences. Not well supported in the templates at the moment. */
418 value = cgi_get("value");
420 disorder_set(ds->g->client, file, name, value);
422 disorder_unset(ds->g->client, file, name);
426 static void act_prefs(cgi_sink *output, dcgi_state *ds) {
430 if((files = cgi_get("files"))) nfiles = atoi(files);
432 for(numfile = 0; numfile < nfiles; ++numfile)
433 process_prefs(ds, numfile);
434 cgi_header(output->sink, "Content-Type", "text/html");
435 header_cookie(output->sink);
436 cgi_body(output->sink);
437 expand(output, "prefs", ds);
440 static void act_pause(cgi_sink *output,
443 disorder_pause(ds->g->client);
444 redirect(output->sink);
447 static void act_resume(cgi_sink *output,
450 disorder_resume(ds->g->client);
451 redirect(output->sink);
454 static void act_login(cgi_sink *output,
456 const char *username, *password, *back;
459 username = cgi_get("username");
460 password = cgi_get("password");
461 if(!username || !password
462 || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) {
463 /* We're just visiting the login page */
464 expand_template(ds, output, "login");
468 if(disorder_connect_user(c, username, password)) {
469 cgi_set_option("error", "loginfailed");
470 expand_template(ds, output, "login");
473 if(disorder_make_cookie(c, &login_cookie)) {
474 cgi_set_option("error", "cookiefailed");
475 expand_template(ds, output, "login");
478 /* We have a new cookie */
479 header_cookie(output->sink);
480 cgi_set_option("status", "loginok");
481 if((back = cgi_get("back")) && *back)
482 /* Redirect back to somewhere or other */
483 redirect(output->sink);
485 /* Stick to the login page */
486 expand_template(ds, output, "login");
489 static void act_logout(cgi_sink *output,
491 disorder_revoke(ds->g->client);
493 /* Reconnect as guest */
494 disorder_cgi_login(ds, output);
495 /* Back to the login page */
496 cgi_set_option("status", "logoutok");
497 expand_template(ds, output, "login");
500 static void act_register(cgi_sink *output,
502 const char *username, *password, *email;
503 char *confirm, *content_type;
504 const char *text, *encoding, *charset;
506 username = cgi_get("username");
507 password = cgi_get("password");
508 email = cgi_get("email");
510 if(!username || !*username) {
511 cgi_set_option("error", "nousername");
512 expand_template(ds, output, "login");
515 if(!password || !*password) {
516 cgi_set_option("error", "nopassword");
517 expand_template(ds, output, "login");
520 if(!email || !*email) {
521 cgi_set_option("error", "noemail");
522 expand_template(ds, output, "login");
525 /* We could well do better address validation but for now we'll just do the
527 if(!strchr(email, '@')) {
528 cgi_set_option("error", "bademail");
529 expand_template(ds, output, "login");
532 if(disorder_register(ds->g->client, username, password, email, &confirm)) {
533 cgi_set_option("error", "cannotregister");
534 expand_template(ds, output, "login");
537 /* Send the user a mail */
538 /* TODO templatize this */
539 byte_xasprintf((char **)&text,
540 "Welcome to DisOrder. To active your login, please visit this URL:\n"
542 "%s?c=%s\n", config->url, urlencodestring(confirm));
543 if(!(text = mime_encode_text(text, &charset, &encoding)))
544 fatal(0, "cannot encode email");
545 byte_xasprintf(&content_type, "text/plain;charset=%s",
546 quote822(charset, 0));
547 sendmail("", config->mail_sender, email, "Welcome to DisOrder",
548 encoding, content_type, text); /* TODO error checking */
549 /* We'll go back to the login page with a suitable message */
550 cgi_set_option("status", "registered");
551 expand_template(ds, output, "login");
554 static void act_confirm(cgi_sink *output,
556 const char *confirmation;
558 if(!(confirmation = cgi_get("c"))) {
559 cgi_set_option("error", "noconfirm");
560 expand_template(ds, output, "login");
562 if(disorder_confirm(ds->g->client, confirmation)) {
563 cgi_set_option("error", "badconfirm");
564 expand_template(ds, output, "login");
566 cgi_set_option("status", "confirmed");
567 expand_template(ds, output, "login");
570 static const struct action {
572 void (*handler)(cgi_sink *output, dcgi_state *ds);
574 { "confirm", act_confirm },
575 { "disable", act_disable },
576 { "enable", act_enable },
577 { "login", act_login },
578 { "logout", act_logout },
579 { "move", act_move },
580 { "pause", act_pause },
581 { "play", act_play },
582 { "playing", act_playing },
583 { "prefs", act_prefs },
584 { "random-disable", act_random_disable },
585 { "random-enable", act_random_enable },
586 { "register", act_register },
587 { "remove", act_remove },
588 { "resume", act_resume },
589 { "scratch", act_scratch },
590 { "volume", act_volume },
593 /* expansions *****************************************************************/
595 static void exp_include(int attribute((unused)) nargs,
599 expand(output, args[0], u);
602 static void exp_server_version(int attribute((unused)) nargs,
603 char attribute((unused)) **args,
610 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
612 v = "(server not running)";
613 cgi_output(output, "%s", v);
616 static void exp_version(int attribute((unused)) nargs,
617 char attribute((unused)) **args,
619 void attribute((unused)) *u) {
620 cgi_output(output, "%s", disorder_short_version_string);
623 static void exp_nonce(int attribute((unused)) nargs,
624 char attribute((unused)) **args,
626 void attribute((unused)) *u) {
627 cgi_output(output, "%s", nonce());
630 static void exp_label(int attribute((unused)) nargs,
633 void attribute((unused)) *u) {
634 cgi_output(output, "%s", cgi_label(args[0]));
637 struct trackinfo_state {
639 const struct queue_entry *q;
644 static void exp_who(int attribute((unused)) nargs,
645 char attribute((unused)) **args,
650 if(ds->track && ds->track->submitter)
651 cgi_output(output, "%s", ds->track->submitter);
654 static void exp_length(int attribute((unused)) nargs,
655 char attribute((unused)) **args,
662 && (ds->track->state == playing_started
663 || ds->track->state == playing_paused)
664 && ds->track->sofar >= 0)
665 cgi_output(output, "%ld:%02ld/",
666 ds->track->sofar / 60, ds->track->sofar % 60);
669 disorder_length(ds->g->client, ds->track->track, &length);
671 disorder_length(ds->g->client, ds->tracks[0], &length);
673 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
675 sink_printf(output->sink, "%s", " ");
678 static void exp_when(int attribute((unused)) nargs,
679 char attribute((unused)) **args,
683 const struct tm *w = 0;
686 switch(ds->track->state) {
687 case playing_isscratch:
688 case playing_unplayed:
690 if(ds->track->expected)
691 w = localtime(&ds->track->expected);
694 case playing_no_player:
696 case playing_scratched:
697 case playing_started:
699 case playing_quitting:
700 if(ds->track->played)
701 w = localtime(&ds->track->played);
705 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
707 sink_printf(output->sink, " ");
710 static void exp_part(int nargs,
715 const char *s, *track, *part, *context;
721 track = ds->track->track;
723 track = ds->tracks[0];
741 if(disorder_part(ds->g->client, (char **)&s, track,
742 !strcmp(context, "short") ? "display" : context, part))
743 fatal(0, "disorder_part() failed");
744 if(!strcmp(context, "short"))
745 s = truncate_for_display(s, config->short_display);
746 cgi_output(output, "%s", s);
748 sink_printf(output->sink, " ");
751 static void exp_playing(int attribute((unused)) nargs,
758 lookups(ds, DC_PLAYING);
759 memset(&s, 0, sizeof s);
762 s.track = ds->g->playing;
763 expandstring(output, args[0], &s);
767 static void exp_queue(int attribute((unused)) nargs,
773 struct queue_entry *q;
775 lookups(ds, DC_QUEUE);
776 memset(&s, 0, sizeof s);
779 for(q = ds->g->queue; q; q = q->next) {
782 expandstring(output, args[0], &s);
788 static void exp_recent(int attribute((unused)) nargs,
794 struct queue_entry *q;
796 lookups(ds, DC_RECENT);
797 memset(&s, 0, sizeof s);
800 for(q = ds->g->recent; q; q = q->next) {
803 expandstring(output, args[0], &s);
809 static void exp_new(int attribute((unused)) nargs,
817 memset(&s, 0, sizeof s);
820 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
821 s.last = s.index + 1 < ds->g->nnew;
822 s.tracks = &ds->g->new[s.index];
823 expandstring(output, args[0], &s);
828 static void exp_url(int attribute((unused)) nargs,
829 char attribute((unused)) **args,
831 void attribute((unused)) *u) {
832 cgi_output(output, "%s", config->url);
840 static int compare_result(const void *a, const void *b) {
841 const struct result *ra = a, *rb = b;
844 if(!(c = strcmp(ra->sort, rb->sort)))
845 c = strcmp(ra->track, rb->track);
849 static void exp_search(int nargs,
853 dcgi_state *ds = u, substate;
855 const char *q, *context, *part, *template;
871 assert(!"should never happen");
872 part = context = template = 0; /* quieten compiler */
874 if(ds->tracks == 0) {
875 /* we are the top level, let's get some search results */
876 if(!(q = cgi_get("query"))) return; /* no results yet */
877 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
881 ntracks = ds->ntracks;
883 assert(ntracks != 0);
884 /* sort tracks by the appropriate part */
885 r = xmalloc(ntracks * sizeof *r);
886 for(n = 0; n < ntracks; ++n) {
887 r[n].track = tracks[n];
888 if(disorder_part(ds->g->client, (char **)&r[n].sort,
889 tracks[n], context, part))
890 fatal(0, "disorder_part() failed");
892 qsort(r, ntracks, sizeof (struct result), compare_result);
893 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
894 * array as we know it's guaranteed to be big enough and isn't going to be
895 * used for anything else any more. */
896 memset(&substate, 0, sizeof substate);
901 substate.tracks = tracks;
902 substate.ntracks = 0;
905 && !strcmp(r[m].sort, r[n].sort))
906 tracks[substate.ntracks++] = r[m++].track;
907 substate.last = (m == ntracks);
908 expandstring(output, template, &substate);
913 assert(substate.last != 0);
916 static void exp_arg(int attribute((unused)) nargs,
919 void attribute((unused)) *u) {
922 if((v = cgi_get(args[0])))
923 cgi_output(output, "%s", v);
926 static void exp_stats(int attribute((unused)) nargs,
927 char attribute((unused)) **args,
933 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
934 if(!disorder_stats(ds->g->client, &v, 0)) {
936 cgi_output(output, "%s\n", *v++);
938 cgi_closetag(output->sink, "pre");
941 static void exp_volume(int attribute((unused)) nargs,
947 lookups(ds, DC_VOLUME);
948 if(!strcmp(args[0], "left"))
949 cgi_output(output, "%d", ds->g->volume_left);
951 cgi_output(output, "%d", ds->g->volume_right);
954 static void exp_shell(int attribute((unused)) nargs,
957 void attribute((unused)) *u) {
963 if(!(pid = xfork())) {
968 execlp("sh", "sh", "-c", args[0], (char *)0);
969 fatal(errno, "error executing sh");
972 while((n = read(p[0], buffer, sizeof buffer))) {
974 if(errno == EINTR) continue;
975 else fatal(errno, "error reading from pipe");
977 output->sink->write(output->sink, buffer, n);
980 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
982 if(n < 0) fatal(errno, "error calling waitpid");
984 error(0, "shell command '%s' %s", args[0], wstat(w));
987 static inline int str2bool(const char *s) {
988 return !strcmp(s, "true");
991 static inline const char *bool2str(int n) {
992 return n ? "true" : "false";
995 static char *expandarg(const char *arg, dcgi_state *ds) {
1001 output.sink = sink_dynstr(&d);
1002 expandstring(&output, arg, ds);
1003 dynstr_terminate(&d);
1007 static void exp_prefs(int attribute((unused)) nargs,
1012 dcgi_state substate;
1014 const char *file = expandarg(args[0], ds);
1016 memset(&substate, 0, sizeof substate);
1019 if(disorder_prefs(ds->g->client, file, &k)) return;
1021 substate.last = !k->next;
1023 expandstring(output, args[1], &substate);
1030 static void exp_pref(int attribute((unused)) nargs,
1037 if(!disorder_get(ds->g->client, args[0], args[1], &value))
1038 cgi_output(output, "%s", value);
1041 static void exp_if(int nargs,
1046 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
1049 expandstring(output, args[n], ds);
1052 static void exp_and(int nargs,
1059 for(n = 0; n < nargs; ++n)
1060 if(!str2bool(expandarg(args[n], ds))) {
1064 sink_printf(output->sink, "%s", bool2str(result));
1067 static void exp_or(int nargs,
1074 for(n = 0; n < nargs; ++n)
1075 if(str2bool(expandarg(args[n], ds))) {
1079 sink_printf(output->sink, "%s", bool2str(result));
1082 static void exp_not(int attribute((unused)) nargs,
1085 void attribute((unused)) *u) {
1086 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
1089 static void exp_isplaying(int attribute((unused)) nargs,
1090 char attribute((unused)) **args,
1095 lookups(ds, DC_PLAYING);
1096 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
1099 static void exp_isqueue(int attribute((unused)) nargs,
1100 char attribute((unused)) **args,
1105 lookups(ds, DC_QUEUE);
1106 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
1109 static void exp_isrecent(int attribute((unused)) nargs,
1110 char attribute((unused)) **args,
1115 lookups(ds, DC_RECENT);
1116 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
1119 static void exp_isnew(int attribute((unused)) nargs,
1120 char attribute((unused)) **args,
1125 lookups(ds, DC_NEW);
1126 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
1129 static void exp_id(int attribute((unused)) nargs,
1130 char attribute((unused)) **args,
1136 cgi_output(output, "%s", ds->track->id);
1139 static void exp_track(int attribute((unused)) nargs,
1140 char attribute((unused)) **args,
1146 cgi_output(output, "%s", ds->track->track);
1149 static void exp_parity(int attribute((unused)) nargs,
1150 char attribute((unused)) **args,
1155 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
1158 static void exp_comment(int attribute((unused)) nargs,
1159 char attribute((unused)) **args,
1160 cgi_sink attribute((unused)) *output,
1161 void attribute((unused)) *u) {
1165 static void exp_prefname(int attribute((unused)) nargs,
1166 char attribute((unused)) **args,
1171 if(ds->pref && ds->pref->name)
1172 cgi_output(output, "%s", ds->pref->name);
1175 static void exp_prefvalue(int attribute((unused)) nargs,
1176 char attribute((unused)) **args,
1181 if(ds->pref && ds->pref->value)
1182 cgi_output(output, "%s", ds->pref->value);
1185 static void exp_isfiles(int attribute((unused)) nargs,
1186 char attribute((unused)) **args,
1191 lookups(ds, DC_FILES);
1192 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1195 static void exp_isdirectories(int attribute((unused)) nargs,
1196 char attribute((unused)) **args,
1201 lookups(ds, DC_DIRS);
1202 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1205 static void exp_choose(int attribute((unused)) nargs,
1210 dcgi_state substate;
1214 const char *type, *what = expandarg(args[0], ds);
1216 if(!strcmp(what, "files")) {
1217 lookups(ds, DC_FILES);
1218 files = ds->g->files;
1219 nfiles = ds->g->nfiles;
1221 } else if(!strcmp(what, "directories")) {
1222 lookups(ds, DC_DIRS);
1223 files = ds->g->dirs;
1224 nfiles = ds->g->ndirs;
1227 error(0, "unknown @choose@ argument '%s'", what);
1230 e = xmalloc(nfiles * sizeof (struct entry));
1231 for(n = 0; n < nfiles; ++n) {
1232 e[n].path = files[n];
1233 e[n].sort = trackname_transform(type, files[n], "sort");
1234 e[n].display = trackname_transform(type, files[n], "display");
1236 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1237 memset(&substate, 0, sizeof substate);
1240 for(n = 0; n < nfiles; ++n) {
1241 substate.last = (n == nfiles - 1);
1243 substate.entry = &e[n];
1244 expandstring(output, args[1], &substate);
1249 static void exp_file(int attribute((unused)) nargs,
1250 char attribute((unused)) **args,
1256 cgi_output(output, "%s", ds->entry->path);
1258 cgi_output(output, "%s", ds->track->track);
1260 cgi_output(output, "%s", ds->tracks[0]);
1263 static void exp_transform(int nargs,
1266 void attribute((unused)) *u) {
1267 const char *context = nargs > 2 ? args[2] : "display";
1269 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1272 static void exp_urlquote(int attribute((unused)) nargs,
1275 void attribute((unused)) *u) {
1276 cgi_output(output, "%s", urlencodestring(args[0]));
1279 static void exp_scratchable(int attribute((unused)) nargs,
1280 char attribute((unused)) **args,
1282 void attribute((unused)) *u) {
1285 lookups(ds, DC_PLAYING|DC_RIGHTS);
1286 sink_printf(output->sink, "%s",
1287 bool2str(right_scratchable(ds->g->rights,
1288 disorder_user(ds->g->client),
1292 static void exp_removable(int attribute((unused)) nargs,
1293 char attribute((unused)) **args,
1295 void attribute((unused)) *u) {
1298 lookups(ds, DC_RIGHTS);
1299 sink_printf(output->sink, "%s",
1300 bool2str(right_removable(ds->g->rights,
1301 disorder_user(ds->g->client),
1305 static void exp_movable(int attribute((unused)) nargs,
1306 char attribute((unused)) **args,
1308 void attribute((unused)) *u) {
1311 lookups(ds, DC_RIGHTS);
1312 sink_printf(output->sink, "%s",
1313 bool2str(right_movable(ds->g->rights,
1314 disorder_user(ds->g->client),
1318 static void exp_navigate(int attribute((unused)) nargs,
1323 dcgi_state substate;
1324 const char *path = expandarg(args[0], ds);
1329 memset(&substate, 0, sizeof substate);
1331 ptr = path + 1; /* skip root */
1333 substate.nav_path = path;
1336 while(*ptr && *ptr != '/')
1338 substate.last = !*ptr;
1339 substate.nav_len = ptr - path;
1340 substate.nav_dirlen = dirlen;
1341 expandstring(output, args[1], &substate);
1342 dirlen = substate.nav_len;
1349 static void exp_fullname(int attribute((unused)) nargs,
1350 char attribute((unused)) **args,
1354 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1357 static void exp_basename(int nargs,
1365 if((s = strrchr(args[0], '/'))) ++s;
1367 cgi_output(output, "%s", s);
1369 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1370 ds->nav_path + ds->nav_dirlen + 1);
1373 static void exp_dirname(int nargs,
1381 if((s = strrchr(args[0], '/')))
1382 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1384 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1387 static void exp_eq(int attribute((unused)) nargs,
1390 void attribute((unused)) *u) {
1391 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1394 static void exp_ne(int attribute((unused)) nargs,
1397 void attribute((unused)) *u) {
1398 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1401 static void exp_enabled(int attribute((unused)) nargs,
1402 char attribute((unused)) **args,
1409 disorder_enabled(ds->g->client, &enabled);
1410 cgi_output(output, "%s", bool2str(enabled));
1413 static void exp_random_enabled(int attribute((unused)) nargs,
1414 char attribute((unused)) **args,
1421 disorder_random_enabled(ds->g->client, &enabled);
1422 cgi_output(output, "%s", bool2str(enabled));
1425 static void exp_trackstate(int attribute((unused)) nargs,
1430 struct queue_entry *q;
1433 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1434 lookups(ds, DC_QUEUE|DC_PLAYING);
1435 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1436 cgi_output(output, "playing");
1438 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1441 cgi_output(output, "queued");
1445 static void exp_thisurl(int attribute((unused)) nargs,
1446 char attribute((unused)) **args,
1448 void attribute((unused)) *u) {
1449 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1450 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1453 static void exp_isfirst(int attribute((unused)) nargs,
1454 char attribute((unused)) **args,
1459 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1462 static void exp_islast(int attribute((unused)) nargs,
1463 char attribute((unused)) **args,
1468 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1471 static void exp_action(int attribute((unused)) nargs,
1472 char attribute((unused)) **args,
1474 void attribute((unused)) *u) {
1475 const char *action = cgi_get("action"), *mgmt;
1477 if(!action) action = "playing";
1478 if(!strcmp(action, "playing")
1479 && (mgmt = cgi_get("mgmt"))
1480 && !strcmp(mgmt, "true"))
1482 sink_printf(output->sink, "%s", action);
1485 static void exp_resolve(int attribute((unused)) nargs,
1488 void attribute((unused)) *u) {
1492 if(!disorder_resolve(ds->g->client, &track, args[0]))
1493 sink_printf(output->sink, "%s", track);
1496 static void exp_paused(int attribute((unused)) nargs,
1497 char attribute((unused)) **args,
1503 lookups(ds, DC_PLAYING);
1504 if(ds->g->playing && ds->g->playing->state == playing_paused)
1506 cgi_output(output, "%s", bool2str(paused));
1509 static void exp_state(int attribute((unused)) nargs,
1510 char attribute((unused)) **args,
1516 cgi_output(output, "%s", playing_states[ds->track->state]);
1519 static void exp_files(int attribute((unused)) nargs,
1524 dcgi_state substate;
1525 const char *nfiles_arg, *directory;
1526 int nfiles, numfile;
1529 memset(&substate, 0, sizeof substate);
1531 if((directory = cgi_get("directory"))) {
1532 /* Prefs for whole directory. */
1533 lookups(ds, DC_FILES);
1534 /* Synthesize args for the file list. */
1535 nfiles = ds->g->nfiles;
1536 for(numfile = 0; numfile < nfiles; ++numfile) {
1537 k = xmalloc(sizeof *k);
1538 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1539 k->value = ds->g->files[numfile];
1544 /* Args already present. */
1545 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1548 for(numfile = 0; numfile < nfiles; ++numfile) {
1549 substate.index = numfile;
1550 expandstring(output, args[0], &substate);
1554 static void exp_index(int attribute((unused)) nargs,
1555 char attribute((unused)) **args,
1560 cgi_output(output, "%d", ds->index);
1563 static void exp_nfiles(int attribute((unused)) nargs,
1564 char attribute((unused)) **args,
1568 const char *files_arg;
1570 if(cgi_get("directory")) {
1571 lookups(ds, DC_FILES);
1572 cgi_output(output, "%d", ds->g->nfiles);
1573 } else if((files_arg = cgi_get("files")))
1574 cgi_output(output, "%s", files_arg);
1576 cgi_output(output, "1");
1579 static void exp_user(int attribute((unused)) nargs,
1580 char attribute((unused)) **args,
1583 dcgi_state *const ds = u;
1585 cgi_output(output, "%s", disorder_user(ds->g->client));
1588 static void exp_right(int attribute((unused)) nargs,
1592 dcgi_state *const ds = u;
1593 const char *right = expandarg(args[0], ds);
1596 lookups(ds, DC_RIGHTS);
1597 if(parse_rights(right, &r, 1/*report*/))
1600 cgi_output(output, "%s", bool2str(!!(r & ds->g->rights)));
1601 else if(r & ds->g->rights)
1602 expandstring(output, args[1], ds);
1604 expandstring(output, args[2], ds);
1607 static const struct cgi_expansion expansions[] = {
1608 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1609 { "action", 0, 0, 0, exp_action },
1610 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1611 { "arg", 1, 1, 0, exp_arg },
1612 { "basename", 0, 1, 0, exp_basename },
1613 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1614 { "dirname", 0, 1, 0, exp_dirname },
1615 { "enabled", 0, 0, 0, exp_enabled },
1616 { "eq", 2, 2, 0, exp_eq },
1617 { "file", 0, 0, 0, exp_file },
1618 { "files", 1, 1, EXP_MAGIC, exp_files },
1619 { "fullname", 0, 0, 0, exp_fullname },
1620 { "id", 0, 0, 0, exp_id },
1621 { "if", 2, 3, EXP_MAGIC, exp_if },
1622 { "include", 1, 1, 0, exp_include },
1623 { "index", 0, 0, 0, exp_index },
1624 { "isdirectories", 0, 0, 0, exp_isdirectories },
1625 { "isfiles", 0, 0, 0, exp_isfiles },
1626 { "isfirst", 0, 0, 0, exp_isfirst },
1627 { "islast", 0, 0, 0, exp_islast },
1628 { "isnew", 0, 0, 0, exp_isnew },
1629 { "isplaying", 0, 0, 0, exp_isplaying },
1630 { "isqueue", 0, 0, 0, exp_isqueue },
1631 { "isrecent", 0, 0, 0, exp_isrecent },
1632 { "label", 1, 1, 0, exp_label },
1633 { "length", 0, 0, 0, exp_length },
1634 { "movable", 0, 0, 0, exp_movable },
1635 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1636 { "ne", 2, 2, 0, exp_ne },
1637 { "new", 1, 1, EXP_MAGIC, exp_new },
1638 { "nfiles", 0, 0, 0, exp_nfiles },
1639 { "nonce", 0, 0, 0, exp_nonce },
1640 { "not", 1, 1, 0, exp_not },
1641 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1642 { "parity", 0, 0, 0, exp_parity },
1643 { "part", 1, 3, 0, exp_part },
1644 { "paused", 0, 0, 0, exp_paused },
1645 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1646 { "pref", 2, 2, 0, exp_pref },
1647 { "prefname", 0, 0, 0, exp_prefname },
1648 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1649 { "prefvalue", 0, 0, 0, exp_prefvalue },
1650 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1651 { "random-enabled", 0, 0, 0, exp_random_enabled },
1652 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1653 { "removable", 0, 0, 0, exp_removable },
1654 { "resolve", 1, 1, 0, exp_resolve },
1655 { "right", 1, 3, EXP_MAGIC, exp_right },
1656 { "scratchable", 0, 0, 0, exp_scratchable },
1657 { "search", 2, 3, EXP_MAGIC, exp_search },
1658 { "server-version", 0, 0, 0, exp_server_version },
1659 { "shell", 1, 1, 0, exp_shell },
1660 { "state", 0, 0, 0, exp_state },
1661 { "stats", 0, 0, 0, exp_stats },
1662 { "thisurl", 0, 0, 0, exp_thisurl },
1663 { "track", 0, 0, 0, exp_track },
1664 { "trackstate", 1, 1, 0, exp_trackstate },
1665 { "transform", 2, 3, 0, exp_transform },
1666 { "url", 0, 0, 0, exp_url },
1667 { "urlquote", 1, 1, 0, exp_urlquote },
1668 { "user", 0, 0, 0, exp_user },
1669 { "version", 0, 0, 0, exp_version },
1670 { "volume", 1, 1, 0, exp_volume },
1671 { "when", 0, 0, 0, exp_when },
1672 { "who", 0, 0, 0, exp_who }
1675 static void expand(cgi_sink *output,
1676 const char *template,
1678 cgi_expand(template,
1679 expansions, sizeof expansions / sizeof *expansions,
1684 static void expandstring(cgi_sink *output,
1687 cgi_expand_string("",
1689 expansions, sizeof expansions / sizeof *expansions,
1694 static void perform_action(cgi_sink *output, dcgi_state *ds,
1695 const char *action) {
1698 /* We don't ever want anything to be cached */
1699 cgi_header(output->sink, "Cache-Control", "no-cache");
1700 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1701 actions[n].handler(output, ds);
1703 expand_template(ds, output, action);
1706 void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1707 const char *action = cgi_get("action");
1710 /* We allow URLs which are just confirm=... in order to keep confirmation
1711 * URLs, which are user-facing, as short as possible. */
1717 perform_action(output, ds, action);
1720 void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1722 cgi_set_option("error", msg);
1723 perform_action(output, ds, "error");
1726 /** @brief Log in as the current user or guest if none */
1727 void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) {
1728 /* Create a new connection */
1729 ds->g->client = disorder_new(0);
1730 /* Forget everything we knew */
1733 if(disorder_connect_cookie(ds->g->client, login_cookie)) {
1734 disorder_cgi_error(output, ds, "connect");
1737 /* If there was a cookie but it went bad, we forget it */
1738 if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest"))