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 if((back = cgi_get("back")) && back)
481 /* Redirect back to somewhere or other */
482 redirect(output->sink);
484 /* Stick to the login page */
485 expand_template(ds, output, "login");
488 static void act_logout(cgi_sink *output,
490 disorder_revoke(ds->g->client);
492 /* Reconnect as guest */
493 disorder_cgi_login(ds, output);
494 /* Back to the login page */
495 expand_template(ds, output, "login");
498 static void act_register(cgi_sink *output,
500 const char *username, *password, *email;
501 char *confirm, *content_type;
502 const char *text, *encoding, *charset;
504 username = cgi_get("username");
505 password = cgi_get("password");
506 email = cgi_get("email");
508 if(!username || !*username) {
509 cgi_set_option("error", "nousername");
510 expand_template(ds, output, "login");
513 if(!password || !*password) {
514 cgi_set_option("error", "nopassword");
515 expand_template(ds, output, "login");
518 if(!email || !*email) {
519 cgi_set_option("error", "noemail");
520 expand_template(ds, output, "login");
523 /* We could well do better address validation but for now we'll just do the
525 if(!strchr(email, '@')) {
526 cgi_set_option("error", "bademail");
527 expand_template(ds, output, "login");
530 if(disorder_register(ds->g->client, username, password, email, &confirm)) {
531 cgi_set_option("error", "cannotregister");
532 expand_template(ds, output, "login");
535 /* Send the user a mail */
536 /* TODO templatize this */
537 byte_xasprintf((char **)&text,
538 "Welcome to DisOrder. To active your login, please visit this URL:\n"
540 " %s?confirm=%s\n", config->url, confirm);
541 if(!(text = mime_encode_text(text, &charset, &encoding)))
542 fatal(0, "cannot encode email");
543 byte_xasprintf(&content_type, "text/plain;charset=%s",
544 quote822(charset, 0));
545 sendmail("", config->mail_sender, email, "Welcome to DisOrder",
546 encoding, content_type, text); /* TODO error checking */
547 /* We'll go back to the login page with a suitable message */
548 cgi_set_option("registered", "registeredok");
549 expand_template(ds, output, "login");
552 static const struct action {
554 void (*handler)(cgi_sink *output, dcgi_state *ds);
556 { "disable", act_disable },
557 { "enable", act_enable },
558 { "login", act_login },
559 { "logout", act_logout },
560 { "move", act_move },
561 { "pause", act_pause },
562 { "play", act_play },
563 { "playing", act_playing },
564 { "prefs", act_prefs },
565 { "random-disable", act_random_disable },
566 { "random-enable", act_random_enable },
567 { "register", act_register },
568 { "remove", act_remove },
569 { "resume", act_resume },
570 { "scratch", act_scratch },
571 { "volume", act_volume },
574 /* expansions *****************************************************************/
576 static void exp_include(int attribute((unused)) nargs,
580 expand(output, args[0], u);
583 static void exp_server_version(int attribute((unused)) nargs,
584 char attribute((unused)) **args,
591 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
593 v = "(server not running)";
594 cgi_output(output, "%s", v);
597 static void exp_version(int attribute((unused)) nargs,
598 char attribute((unused)) **args,
600 void attribute((unused)) *u) {
601 cgi_output(output, "%s", disorder_short_version_string);
604 static void exp_nonce(int attribute((unused)) nargs,
605 char attribute((unused)) **args,
607 void attribute((unused)) *u) {
608 cgi_output(output, "%s", nonce());
611 static void exp_label(int attribute((unused)) nargs,
614 void attribute((unused)) *u) {
615 cgi_output(output, "%s", cgi_label(args[0]));
618 struct trackinfo_state {
620 const struct queue_entry *q;
625 static void exp_who(int attribute((unused)) nargs,
626 char attribute((unused)) **args,
631 if(ds->track && ds->track->submitter)
632 cgi_output(output, "%s", ds->track->submitter);
635 static void exp_length(int attribute((unused)) nargs,
636 char attribute((unused)) **args,
643 && (ds->track->state == playing_started
644 || ds->track->state == playing_paused)
645 && ds->track->sofar >= 0)
646 cgi_output(output, "%ld:%02ld/",
647 ds->track->sofar / 60, ds->track->sofar % 60);
650 disorder_length(ds->g->client, ds->track->track, &length);
652 disorder_length(ds->g->client, ds->tracks[0], &length);
654 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
656 sink_printf(output->sink, "%s", " ");
659 static void exp_when(int attribute((unused)) nargs,
660 char attribute((unused)) **args,
664 const struct tm *w = 0;
667 switch(ds->track->state) {
668 case playing_isscratch:
669 case playing_unplayed:
671 if(ds->track->expected)
672 w = localtime(&ds->track->expected);
675 case playing_no_player:
677 case playing_scratched:
678 case playing_started:
680 case playing_quitting:
681 if(ds->track->played)
682 w = localtime(&ds->track->played);
686 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
688 sink_printf(output->sink, " ");
691 static void exp_part(int nargs,
696 const char *s, *track, *part, *context;
702 track = ds->track->track;
704 track = ds->tracks[0];
722 if(disorder_part(ds->g->client, (char **)&s, track,
723 !strcmp(context, "short") ? "display" : context, part))
724 fatal(0, "disorder_part() failed");
725 if(!strcmp(context, "short"))
726 s = truncate_for_display(s, config->short_display);
727 cgi_output(output, "%s", s);
729 sink_printf(output->sink, " ");
732 static void exp_playing(int attribute((unused)) nargs,
739 lookups(ds, DC_PLAYING);
740 memset(&s, 0, sizeof s);
743 s.track = ds->g->playing;
744 expandstring(output, args[0], &s);
748 static void exp_queue(int attribute((unused)) nargs,
754 struct queue_entry *q;
756 lookups(ds, DC_QUEUE);
757 memset(&s, 0, sizeof s);
760 for(q = ds->g->queue; q; q = q->next) {
763 expandstring(output, args[0], &s);
769 static void exp_recent(int attribute((unused)) nargs,
775 struct queue_entry *q;
777 lookups(ds, DC_RECENT);
778 memset(&s, 0, sizeof s);
781 for(q = ds->g->recent; q; q = q->next) {
784 expandstring(output, args[0], &s);
790 static void exp_new(int attribute((unused)) nargs,
798 memset(&s, 0, sizeof s);
801 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
802 s.last = s.index + 1 < ds->g->nnew;
803 s.tracks = &ds->g->new[s.index];
804 expandstring(output, args[0], &s);
809 static void exp_url(int attribute((unused)) nargs,
810 char attribute((unused)) **args,
812 void attribute((unused)) *u) {
813 cgi_output(output, "%s", config->url);
821 static int compare_result(const void *a, const void *b) {
822 const struct result *ra = a, *rb = b;
825 if(!(c = strcmp(ra->sort, rb->sort)))
826 c = strcmp(ra->track, rb->track);
830 static void exp_search(int nargs,
834 dcgi_state *ds = u, substate;
836 const char *q, *context, *part, *template;
852 assert(!"should never happen");
853 part = context = template = 0; /* quieten compiler */
855 if(ds->tracks == 0) {
856 /* we are the top level, let's get some search results */
857 if(!(q = cgi_get("query"))) return; /* no results yet */
858 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
862 ntracks = ds->ntracks;
864 assert(ntracks != 0);
865 /* sort tracks by the appropriate part */
866 r = xmalloc(ntracks * sizeof *r);
867 for(n = 0; n < ntracks; ++n) {
868 r[n].track = tracks[n];
869 if(disorder_part(ds->g->client, (char **)&r[n].sort,
870 tracks[n], context, part))
871 fatal(0, "disorder_part() failed");
873 qsort(r, ntracks, sizeof (struct result), compare_result);
874 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
875 * array as we know it's guaranteed to be big enough and isn't going to be
876 * used for anything else any more. */
877 memset(&substate, 0, sizeof substate);
882 substate.tracks = tracks;
883 substate.ntracks = 0;
886 && !strcmp(r[m].sort, r[n].sort))
887 tracks[substate.ntracks++] = r[m++].track;
888 substate.last = (m == ntracks);
889 expandstring(output, template, &substate);
894 assert(substate.last != 0);
897 static void exp_arg(int attribute((unused)) nargs,
900 void attribute((unused)) *u) {
903 if((v = cgi_get(args[0])))
904 cgi_output(output, "%s", v);
907 static void exp_stats(int attribute((unused)) nargs,
908 char attribute((unused)) **args,
914 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
915 if(!disorder_stats(ds->g->client, &v, 0)) {
917 cgi_output(output, "%s\n", *v++);
919 cgi_closetag(output->sink, "pre");
922 static void exp_volume(int attribute((unused)) nargs,
928 lookups(ds, DC_VOLUME);
929 if(!strcmp(args[0], "left"))
930 cgi_output(output, "%d", ds->g->volume_left);
932 cgi_output(output, "%d", ds->g->volume_right);
935 static void exp_shell(int attribute((unused)) nargs,
938 void attribute((unused)) *u) {
944 if(!(pid = xfork())) {
949 execlp("sh", "sh", "-c", args[0], (char *)0);
950 fatal(errno, "error executing sh");
953 while((n = read(p[0], buffer, sizeof buffer))) {
955 if(errno == EINTR) continue;
956 else fatal(errno, "error reading from pipe");
958 output->sink->write(output->sink, buffer, n);
961 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
963 if(n < 0) fatal(errno, "error calling waitpid");
965 error(0, "shell command '%s' %s", args[0], wstat(w));
968 static inline int str2bool(const char *s) {
969 return !strcmp(s, "true");
972 static inline const char *bool2str(int n) {
973 return n ? "true" : "false";
976 static char *expandarg(const char *arg, dcgi_state *ds) {
982 output.sink = sink_dynstr(&d);
983 expandstring(&output, arg, ds);
984 dynstr_terminate(&d);
988 static void exp_prefs(int attribute((unused)) nargs,
995 const char *file = expandarg(args[0], ds);
997 memset(&substate, 0, sizeof substate);
1000 if(disorder_prefs(ds->g->client, file, &k)) return;
1002 substate.last = !k->next;
1004 expandstring(output, args[1], &substate);
1011 static void exp_pref(int attribute((unused)) nargs,
1018 if(!disorder_get(ds->g->client, args[0], args[1], &value))
1019 cgi_output(output, "%s", value);
1022 static void exp_if(int nargs,
1027 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
1030 expandstring(output, args[n], ds);
1033 static void exp_and(int nargs,
1040 for(n = 0; n < nargs; ++n)
1041 if(!str2bool(expandarg(args[n], ds))) {
1045 sink_printf(output->sink, "%s", bool2str(result));
1048 static void exp_or(int nargs,
1055 for(n = 0; n < nargs; ++n)
1056 if(str2bool(expandarg(args[n], ds))) {
1060 sink_printf(output->sink, "%s", bool2str(result));
1063 static void exp_not(int attribute((unused)) nargs,
1066 void attribute((unused)) *u) {
1067 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
1070 static void exp_isplaying(int attribute((unused)) nargs,
1071 char attribute((unused)) **args,
1076 lookups(ds, DC_PLAYING);
1077 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
1080 static void exp_isqueue(int attribute((unused)) nargs,
1081 char attribute((unused)) **args,
1086 lookups(ds, DC_QUEUE);
1087 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
1090 static void exp_isrecent(int attribute((unused)) nargs,
1091 char attribute((unused)) **args,
1096 lookups(ds, DC_RECENT);
1097 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
1100 static void exp_isnew(int attribute((unused)) nargs,
1101 char attribute((unused)) **args,
1106 lookups(ds, DC_NEW);
1107 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
1110 static void exp_id(int attribute((unused)) nargs,
1111 char attribute((unused)) **args,
1117 cgi_output(output, "%s", ds->track->id);
1120 static void exp_track(int attribute((unused)) nargs,
1121 char attribute((unused)) **args,
1127 cgi_output(output, "%s", ds->track->track);
1130 static void exp_parity(int attribute((unused)) nargs,
1131 char attribute((unused)) **args,
1136 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
1139 static void exp_comment(int attribute((unused)) nargs,
1140 char attribute((unused)) **args,
1141 cgi_sink attribute((unused)) *output,
1142 void attribute((unused)) *u) {
1146 static void exp_prefname(int attribute((unused)) nargs,
1147 char attribute((unused)) **args,
1152 if(ds->pref && ds->pref->name)
1153 cgi_output(output, "%s", ds->pref->name);
1156 static void exp_prefvalue(int attribute((unused)) nargs,
1157 char attribute((unused)) **args,
1162 if(ds->pref && ds->pref->value)
1163 cgi_output(output, "%s", ds->pref->value);
1166 static void exp_isfiles(int attribute((unused)) nargs,
1167 char attribute((unused)) **args,
1172 lookups(ds, DC_FILES);
1173 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1176 static void exp_isdirectories(int attribute((unused)) nargs,
1177 char attribute((unused)) **args,
1182 lookups(ds, DC_DIRS);
1183 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1186 static void exp_choose(int attribute((unused)) nargs,
1191 dcgi_state substate;
1195 const char *type, *what = expandarg(args[0], ds);
1197 if(!strcmp(what, "files")) {
1198 lookups(ds, DC_FILES);
1199 files = ds->g->files;
1200 nfiles = ds->g->nfiles;
1202 } else if(!strcmp(what, "directories")) {
1203 lookups(ds, DC_DIRS);
1204 files = ds->g->dirs;
1205 nfiles = ds->g->ndirs;
1208 error(0, "unknown @choose@ argument '%s'", what);
1211 e = xmalloc(nfiles * sizeof (struct entry));
1212 for(n = 0; n < nfiles; ++n) {
1213 e[n].path = files[n];
1214 e[n].sort = trackname_transform(type, files[n], "sort");
1215 e[n].display = trackname_transform(type, files[n], "display");
1217 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1218 memset(&substate, 0, sizeof substate);
1221 for(n = 0; n < nfiles; ++n) {
1222 substate.last = (n == nfiles - 1);
1224 substate.entry = &e[n];
1225 expandstring(output, args[1], &substate);
1230 static void exp_file(int attribute((unused)) nargs,
1231 char attribute((unused)) **args,
1237 cgi_output(output, "%s", ds->entry->path);
1239 cgi_output(output, "%s", ds->track->track);
1241 cgi_output(output, "%s", ds->tracks[0]);
1244 static void exp_transform(int nargs,
1247 void attribute((unused)) *u) {
1248 const char *context = nargs > 2 ? args[2] : "display";
1250 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1253 static void exp_urlquote(int attribute((unused)) nargs,
1256 void attribute((unused)) *u) {
1257 cgi_output(output, "%s", urlencodestring(args[0]));
1260 static void exp_scratchable(int attribute((unused)) nargs,
1261 char attribute((unused)) **args,
1263 void attribute((unused)) *u) {
1266 lookups(ds, DC_PLAYING|DC_RIGHTS);
1267 sink_printf(output->sink, "%s",
1268 bool2str(right_scratchable(ds->g->rights,
1269 disorder_user(ds->g->client),
1273 static void exp_removable(int attribute((unused)) nargs,
1274 char attribute((unused)) **args,
1276 void attribute((unused)) *u) {
1279 lookups(ds, DC_RIGHTS);
1280 sink_printf(output->sink, "%s",
1281 bool2str(right_removable(ds->g->rights,
1282 disorder_user(ds->g->client),
1286 static void exp_movable(int attribute((unused)) nargs,
1287 char attribute((unused)) **args,
1289 void attribute((unused)) *u) {
1292 lookups(ds, DC_RIGHTS);
1293 sink_printf(output->sink, "%s",
1294 bool2str(right_movable(ds->g->rights,
1295 disorder_user(ds->g->client),
1299 static void exp_navigate(int attribute((unused)) nargs,
1304 dcgi_state substate;
1305 const char *path = expandarg(args[0], ds);
1310 memset(&substate, 0, sizeof substate);
1312 ptr = path + 1; /* skip root */
1314 substate.nav_path = path;
1317 while(*ptr && *ptr != '/')
1319 substate.last = !*ptr;
1320 substate.nav_len = ptr - path;
1321 substate.nav_dirlen = dirlen;
1322 expandstring(output, args[1], &substate);
1323 dirlen = substate.nav_len;
1330 static void exp_fullname(int attribute((unused)) nargs,
1331 char attribute((unused)) **args,
1335 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1338 static void exp_basename(int nargs,
1346 if((s = strrchr(args[0], '/'))) ++s;
1348 cgi_output(output, "%s", s);
1350 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1351 ds->nav_path + ds->nav_dirlen + 1);
1354 static void exp_dirname(int nargs,
1362 if((s = strrchr(args[0], '/')))
1363 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1365 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1368 static void exp_eq(int attribute((unused)) nargs,
1371 void attribute((unused)) *u) {
1372 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1375 static void exp_ne(int attribute((unused)) nargs,
1378 void attribute((unused)) *u) {
1379 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1382 static void exp_enabled(int attribute((unused)) nargs,
1383 char attribute((unused)) **args,
1390 disorder_enabled(ds->g->client, &enabled);
1391 cgi_output(output, "%s", bool2str(enabled));
1394 static void exp_random_enabled(int attribute((unused)) nargs,
1395 char attribute((unused)) **args,
1402 disorder_random_enabled(ds->g->client, &enabled);
1403 cgi_output(output, "%s", bool2str(enabled));
1406 static void exp_trackstate(int attribute((unused)) nargs,
1411 struct queue_entry *q;
1414 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1415 lookups(ds, DC_QUEUE|DC_PLAYING);
1416 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1417 cgi_output(output, "playing");
1419 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1422 cgi_output(output, "queued");
1426 static void exp_thisurl(int attribute((unused)) nargs,
1427 char attribute((unused)) **args,
1429 void attribute((unused)) *u) {
1430 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1431 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1434 static void exp_isfirst(int attribute((unused)) nargs,
1435 char attribute((unused)) **args,
1440 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1443 static void exp_islast(int attribute((unused)) nargs,
1444 char attribute((unused)) **args,
1449 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1452 static void exp_action(int attribute((unused)) nargs,
1453 char attribute((unused)) **args,
1455 void attribute((unused)) *u) {
1456 const char *action = cgi_get("action"), *mgmt;
1458 if(!action) action = "playing";
1459 if(!strcmp(action, "playing")
1460 && (mgmt = cgi_get("mgmt"))
1461 && !strcmp(mgmt, "true"))
1463 sink_printf(output->sink, "%s", action);
1466 static void exp_resolve(int attribute((unused)) nargs,
1469 void attribute((unused)) *u) {
1473 if(!disorder_resolve(ds->g->client, &track, args[0]))
1474 sink_printf(output->sink, "%s", track);
1477 static void exp_paused(int attribute((unused)) nargs,
1478 char attribute((unused)) **args,
1484 lookups(ds, DC_PLAYING);
1485 if(ds->g->playing && ds->g->playing->state == playing_paused)
1487 cgi_output(output, "%s", bool2str(paused));
1490 static void exp_state(int attribute((unused)) nargs,
1491 char attribute((unused)) **args,
1497 cgi_output(output, "%s", playing_states[ds->track->state]);
1500 static void exp_files(int attribute((unused)) nargs,
1505 dcgi_state substate;
1506 const char *nfiles_arg, *directory;
1507 int nfiles, numfile;
1510 memset(&substate, 0, sizeof substate);
1512 if((directory = cgi_get("directory"))) {
1513 /* Prefs for whole directory. */
1514 lookups(ds, DC_FILES);
1515 /* Synthesize args for the file list. */
1516 nfiles = ds->g->nfiles;
1517 for(numfile = 0; numfile < nfiles; ++numfile) {
1518 k = xmalloc(sizeof *k);
1519 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1520 k->value = ds->g->files[numfile];
1525 /* Args already present. */
1526 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1529 for(numfile = 0; numfile < nfiles; ++numfile) {
1530 substate.index = numfile;
1531 expandstring(output, args[0], &substate);
1535 static void exp_index(int attribute((unused)) nargs,
1536 char attribute((unused)) **args,
1541 cgi_output(output, "%d", ds->index);
1544 static void exp_nfiles(int attribute((unused)) nargs,
1545 char attribute((unused)) **args,
1549 const char *files_arg;
1551 if(cgi_get("directory")) {
1552 lookups(ds, DC_FILES);
1553 cgi_output(output, "%d", ds->g->nfiles);
1554 } else if((files_arg = cgi_get("files")))
1555 cgi_output(output, "%s", files_arg);
1557 cgi_output(output, "1");
1560 static void exp_user(int attribute((unused)) nargs,
1561 char attribute((unused)) **args,
1564 dcgi_state *const ds = u;
1566 cgi_output(output, "%s", disorder_user(ds->g->client));
1569 static void exp_right(int attribute((unused)) nargs,
1573 dcgi_state *const ds = u;
1574 const char *right = expandarg(args[0], ds);
1577 lookups(ds, DC_RIGHTS);
1578 if(parse_rights(right, &r, 1/*report*/))
1581 cgi_output(output, "%s", bool2str(!!(r & ds->g->rights)));
1582 else if(r & ds->g->rights)
1583 expandstring(output, args[1], ds);
1585 expandstring(output, args[2], ds);
1588 static const struct cgi_expansion expansions[] = {
1589 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1590 { "action", 0, 0, 0, exp_action },
1591 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1592 { "arg", 1, 1, 0, exp_arg },
1593 { "basename", 0, 1, 0, exp_basename },
1594 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1595 { "dirname", 0, 1, 0, exp_dirname },
1596 { "enabled", 0, 0, 0, exp_enabled },
1597 { "eq", 2, 2, 0, exp_eq },
1598 { "file", 0, 0, 0, exp_file },
1599 { "files", 1, 1, EXP_MAGIC, exp_files },
1600 { "fullname", 0, 0, 0, exp_fullname },
1601 { "id", 0, 0, 0, exp_id },
1602 { "if", 2, 3, EXP_MAGIC, exp_if },
1603 { "include", 1, 1, 0, exp_include },
1604 { "index", 0, 0, 0, exp_index },
1605 { "isdirectories", 0, 0, 0, exp_isdirectories },
1606 { "isfiles", 0, 0, 0, exp_isfiles },
1607 { "isfirst", 0, 0, 0, exp_isfirst },
1608 { "islast", 0, 0, 0, exp_islast },
1609 { "isnew", 0, 0, 0, exp_isnew },
1610 { "isplaying", 0, 0, 0, exp_isplaying },
1611 { "isqueue", 0, 0, 0, exp_isqueue },
1612 { "isrecent", 0, 0, 0, exp_isrecent },
1613 { "label", 1, 1, 0, exp_label },
1614 { "length", 0, 0, 0, exp_length },
1615 { "movable", 0, 0, 0, exp_movable },
1616 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1617 { "ne", 2, 2, 0, exp_ne },
1618 { "new", 1, 1, EXP_MAGIC, exp_new },
1619 { "nfiles", 0, 0, 0, exp_nfiles },
1620 { "nonce", 0, 0, 0, exp_nonce },
1621 { "not", 1, 1, 0, exp_not },
1622 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1623 { "parity", 0, 0, 0, exp_parity },
1624 { "part", 1, 3, 0, exp_part },
1625 { "paused", 0, 0, 0, exp_paused },
1626 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1627 { "pref", 2, 2, 0, exp_pref },
1628 { "prefname", 0, 0, 0, exp_prefname },
1629 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1630 { "prefvalue", 0, 0, 0, exp_prefvalue },
1631 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1632 { "random-enabled", 0, 0, 0, exp_random_enabled },
1633 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1634 { "removable", 0, 0, 0, exp_removable },
1635 { "resolve", 1, 1, 0, exp_resolve },
1636 { "right", 1, 3, EXP_MAGIC, exp_right },
1637 { "scratchable", 0, 0, 0, exp_scratchable },
1638 { "search", 2, 3, EXP_MAGIC, exp_search },
1639 { "server-version", 0, 0, 0, exp_server_version },
1640 { "shell", 1, 1, 0, exp_shell },
1641 { "state", 0, 0, 0, exp_state },
1642 { "stats", 0, 0, 0, exp_stats },
1643 { "thisurl", 0, 0, 0, exp_thisurl },
1644 { "track", 0, 0, 0, exp_track },
1645 { "trackstate", 1, 1, 0, exp_trackstate },
1646 { "transform", 2, 3, 0, exp_transform },
1647 { "url", 0, 0, 0, exp_url },
1648 { "urlquote", 1, 1, 0, exp_urlquote },
1649 { "user", 0, 0, 0, exp_user },
1650 { "version", 0, 0, 0, exp_version },
1651 { "volume", 1, 1, 0, exp_volume },
1652 { "when", 0, 0, 0, exp_when },
1653 { "who", 0, 0, 0, exp_who }
1656 static void expand(cgi_sink *output,
1657 const char *template,
1659 cgi_expand(template,
1660 expansions, sizeof expansions / sizeof *expansions,
1665 static void expandstring(cgi_sink *output,
1668 cgi_expand_string("",
1670 expansions, sizeof expansions / sizeof *expansions,
1675 static void perform_action(cgi_sink *output, dcgi_state *ds,
1676 const char *action) {
1679 /* We don't ever want anything to be cached */
1680 cgi_header(output->sink, "Cache-Control", "no-cache");
1681 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1682 actions[n].handler(output, ds);
1684 expand_template(ds, output, action);
1687 void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1688 const char *action = cgi_get("action");
1690 if(!action) action = "playing";
1691 perform_action(output, ds, action);
1694 void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1696 cgi_set_option("error", msg);
1697 perform_action(output, ds, "error");
1700 /** @brief Log in as the current user or guest if none */
1701 void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) {
1702 /* Create a new connection */
1703 ds->g->client = disorder_new(0);
1704 /* Forget everything we knew */
1707 if(disorder_connect_cookie(ds->g->client, login_cookie)) {
1708 disorder_cgi_error(output, ds, "connect");
1711 /* If there was a cookie but it went bad, we forget it */
1712 if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest"))