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");
467 /* We'll need a new connection as we are going to stop being guest */
469 if(disorder_connect_user(c, username, password)) {
470 cgi_set_option("error", "loginfailed");
471 expand_template(ds, output, "login");
474 if(disorder_make_cookie(c, &login_cookie)) {
475 cgi_set_option("error", "cookiefailed");
476 expand_template(ds, output, "login");
479 /* Use the new connection henceforth */
482 /* We have a new cookie */
483 header_cookie(output->sink);
484 cgi_set_option("status", "loginok");
485 if((back = cgi_get("back")) && *back)
486 /* Redirect back to somewhere or other */
487 redirect(output->sink);
489 /* Stick to the login page */
490 expand_template(ds, output, "login");
493 static void act_logout(cgi_sink *output,
495 disorder_revoke(ds->g->client);
497 /* Reconnect as guest */
498 disorder_cgi_login(ds, output);
499 /* Back to the login page */
500 cgi_set_option("status", "logoutok");
501 expand_template(ds, output, "login");
504 static void act_register(cgi_sink *output,
506 const char *username, *password, *password2, *email;
507 char *confirm, *content_type;
508 const char *text, *encoding, *charset;
510 username = cgi_get("username");
511 password = cgi_get("password1");
512 password2 = cgi_get("password2");
513 email = cgi_get("email");
515 if(!username || !*username) {
516 cgi_set_option("error", "nousername");
517 expand_template(ds, output, "login");
520 if(!password || !*password) {
521 cgi_set_option("error", "nopassword");
522 expand_template(ds, output, "login");
525 if(!password2 || !*password2 || strcmp(password, password2)) {
526 cgi_set_option("error", "passwordmismatch");
527 expand_template(ds, output, "login");
530 if(!email || !*email) {
531 cgi_set_option("error", "noemail");
532 expand_template(ds, output, "login");
535 /* We could well do better address validation but for now we'll just do the
537 if(!strchr(email, '@')) {
538 cgi_set_option("error", "bademail");
539 expand_template(ds, output, "login");
542 if(disorder_register(ds->g->client, username, password, email, &confirm)) {
543 cgi_set_option("error", "cannotregister");
544 expand_template(ds, output, "login");
547 /* Send the user a mail */
548 /* TODO templatize this */
549 byte_xasprintf((char **)&text,
550 "Welcome to DisOrder. To active your login, please visit this URL:\n"
552 "%s?c=%s\n", config->url, urlencodestring(confirm));
553 if(!(text = mime_encode_text(text, &charset, &encoding)))
554 fatal(0, "cannot encode email");
555 byte_xasprintf(&content_type, "text/plain;charset=%s",
556 quote822(charset, 0));
557 sendmail("", config->mail_sender, email, "Welcome to DisOrder",
558 encoding, content_type, text); /* TODO error checking */
559 /* We'll go back to the login page with a suitable message */
560 cgi_set_option("status", "registered");
561 expand_template(ds, output, "login");
564 static void act_confirm(cgi_sink *output,
566 const char *confirmation;
568 if(!(confirmation = cgi_get("c"))) {
569 cgi_set_option("error", "noconfirm");
570 expand_template(ds, output, "login");
572 /* Confirm our registration */
573 if(disorder_confirm(ds->g->client, confirmation)) {
574 cgi_set_option("error", "badconfirm");
575 expand_template(ds, output, "login");
578 if(disorder_make_cookie(ds->g->client, &login_cookie)) {
579 cgi_set_option("error", "cookiefailed");
580 expand_template(ds, output, "login");
583 /* Discard any cached data JIC */
585 /* We have a new cookie */
586 header_cookie(output->sink);
587 cgi_set_option("status", "confirmed");
588 expand_template(ds, output, "login");
591 static void act_edituser(cgi_sink *output,
593 const char *email = cgi_get("email"), *password = cgi_get("changepassword1");
594 const char *password2 = cgi_get("changepassword2");
598 if((password && *password) || (password && *password2)) {
599 if(!password || !password2 || strcmp(password, password2)) {
600 cgi_set_option("error", "passwordmismatch");
601 expand_template(ds, output, "login");
605 password = password2 = 0;
608 if(disorder_edituser(ds->g->client, disorder_user(ds->g->client),
610 cgi_set_option("error", "badedit");
611 expand_template(ds, output, "login");
616 if(disorder_edituser(ds->g->client, disorder_user(ds->g->client),
617 "password", password)) {
618 cgi_set_option("error", "badedit");
619 expand_template(ds, output, "login");
625 login_cookie = 0; /* it'll be invalid now */
626 /* This is a bit duplicative of act_login() */
628 if(disorder_connect_user(c, disorder_user(ds->g->client), password)) {
629 cgi_set_option("error", "loginfailed");
630 expand_template(ds, output, "login");
633 if(disorder_make_cookie(c, &login_cookie)) {
634 cgi_set_option("error", "cookiefailed");
635 expand_template(ds, output, "login");
638 /* Use the new connection henceforth */
641 /* We have a new cookie */
642 header_cookie(output->sink);
644 cgi_set_option("status", "edited");
645 expand_template(ds, output, "login");
648 static const struct action {
650 void (*handler)(cgi_sink *output, dcgi_state *ds);
652 { "confirm", act_confirm },
653 { "disable", act_disable },
654 { "edituser", act_edituser },
655 { "enable", act_enable },
656 { "login", act_login },
657 { "logout", act_logout },
658 { "move", act_move },
659 { "pause", act_pause },
660 { "play", act_play },
661 { "playing", act_playing },
662 { "prefs", act_prefs },
663 { "random-disable", act_random_disable },
664 { "random-enable", act_random_enable },
665 { "register", act_register },
666 { "remove", act_remove },
667 { "resume", act_resume },
668 { "scratch", act_scratch },
669 { "volume", act_volume },
672 /* expansions *****************************************************************/
674 static void exp_include(int attribute((unused)) nargs,
678 expand(output, args[0], u);
681 static void exp_server_version(int attribute((unused)) nargs,
682 char attribute((unused)) **args,
689 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
691 v = "(server not running)";
692 cgi_output(output, "%s", v);
695 static void exp_version(int attribute((unused)) nargs,
696 char attribute((unused)) **args,
698 void attribute((unused)) *u) {
699 cgi_output(output, "%s", disorder_short_version_string);
702 static void exp_nonce(int attribute((unused)) nargs,
703 char attribute((unused)) **args,
705 void attribute((unused)) *u) {
706 cgi_output(output, "%s", nonce());
709 static void exp_label(int attribute((unused)) nargs,
712 void attribute((unused)) *u) {
713 cgi_output(output, "%s", cgi_label(args[0]));
716 struct trackinfo_state {
718 const struct queue_entry *q;
723 static void exp_who(int attribute((unused)) nargs,
724 char attribute((unused)) **args,
729 if(ds->track && ds->track->submitter)
730 cgi_output(output, "%s", ds->track->submitter);
733 static void exp_length(int attribute((unused)) nargs,
734 char attribute((unused)) **args,
741 && (ds->track->state == playing_started
742 || ds->track->state == playing_paused)
743 && ds->track->sofar >= 0)
744 cgi_output(output, "%ld:%02ld/",
745 ds->track->sofar / 60, ds->track->sofar % 60);
748 disorder_length(ds->g->client, ds->track->track, &length);
750 disorder_length(ds->g->client, ds->tracks[0], &length);
752 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
754 sink_printf(output->sink, "%s", " ");
757 static void exp_when(int attribute((unused)) nargs,
758 char attribute((unused)) **args,
762 const struct tm *w = 0;
765 switch(ds->track->state) {
766 case playing_isscratch:
767 case playing_unplayed:
769 if(ds->track->expected)
770 w = localtime(&ds->track->expected);
773 case playing_no_player:
775 case playing_scratched:
776 case playing_started:
778 case playing_quitting:
779 if(ds->track->played)
780 w = localtime(&ds->track->played);
784 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
786 sink_printf(output->sink, " ");
789 static void exp_part(int nargs,
794 const char *s, *track, *part, *context;
800 track = ds->track->track;
802 track = ds->tracks[0];
820 if(disorder_part(ds->g->client, (char **)&s, track,
821 !strcmp(context, "short") ? "display" : context, part))
822 fatal(0, "disorder_part() failed");
823 if(!strcmp(context, "short"))
824 s = truncate_for_display(s, config->short_display);
825 cgi_output(output, "%s", s);
827 sink_printf(output->sink, " ");
830 static void exp_playing(int attribute((unused)) nargs,
837 lookups(ds, DC_PLAYING);
838 memset(&s, 0, sizeof s);
841 s.track = ds->g->playing;
842 expandstring(output, args[0], &s);
846 static void exp_queue(int attribute((unused)) nargs,
852 struct queue_entry *q;
854 lookups(ds, DC_QUEUE);
855 memset(&s, 0, sizeof s);
858 for(q = ds->g->queue; q; q = q->next) {
861 expandstring(output, args[0], &s);
867 static void exp_recent(int attribute((unused)) nargs,
873 struct queue_entry *q;
875 lookups(ds, DC_RECENT);
876 memset(&s, 0, sizeof s);
879 for(q = ds->g->recent; q; q = q->next) {
882 expandstring(output, args[0], &s);
888 static void exp_new(int attribute((unused)) nargs,
896 memset(&s, 0, sizeof s);
899 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
900 s.last = s.index + 1 < ds->g->nnew;
901 s.tracks = &ds->g->new[s.index];
902 expandstring(output, args[0], &s);
907 static void exp_url(int attribute((unused)) nargs,
908 char attribute((unused)) **args,
910 void attribute((unused)) *u) {
911 cgi_output(output, "%s", config->url);
919 static int compare_result(const void *a, const void *b) {
920 const struct result *ra = a, *rb = b;
923 if(!(c = strcmp(ra->sort, rb->sort)))
924 c = strcmp(ra->track, rb->track);
928 static void exp_search(int nargs,
932 dcgi_state *ds = u, substate;
934 const char *q, *context, *part, *template;
950 assert(!"should never happen");
951 part = context = template = 0; /* quieten compiler */
953 if(ds->tracks == 0) {
954 /* we are the top level, let's get some search results */
955 if(!(q = cgi_get("query"))) return; /* no results yet */
956 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
960 ntracks = ds->ntracks;
962 assert(ntracks != 0);
963 /* sort tracks by the appropriate part */
964 r = xmalloc(ntracks * sizeof *r);
965 for(n = 0; n < ntracks; ++n) {
966 r[n].track = tracks[n];
967 if(disorder_part(ds->g->client, (char **)&r[n].sort,
968 tracks[n], context, part))
969 fatal(0, "disorder_part() failed");
971 qsort(r, ntracks, sizeof (struct result), compare_result);
972 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
973 * array as we know it's guaranteed to be big enough and isn't going to be
974 * used for anything else any more. */
975 memset(&substate, 0, sizeof substate);
980 substate.tracks = tracks;
981 substate.ntracks = 0;
984 && !strcmp(r[m].sort, r[n].sort))
985 tracks[substate.ntracks++] = r[m++].track;
986 substate.last = (m == ntracks);
987 expandstring(output, template, &substate);
992 assert(substate.last != 0);
995 static void exp_arg(int attribute((unused)) nargs,
998 void attribute((unused)) *u) {
1001 if((v = cgi_get(args[0])))
1002 cgi_output(output, "%s", v);
1005 static void exp_stats(int attribute((unused)) nargs,
1006 char attribute((unused)) **args,
1012 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
1013 if(!disorder_stats(ds->g->client, &v, 0)) {
1015 cgi_output(output, "%s\n", *v++);
1017 cgi_closetag(output->sink, "pre");
1020 static void exp_volume(int attribute((unused)) nargs,
1026 lookups(ds, DC_VOLUME);
1027 if(!strcmp(args[0], "left"))
1028 cgi_output(output, "%d", ds->g->volume_left);
1030 cgi_output(output, "%d", ds->g->volume_right);
1033 static void exp_shell(int attribute((unused)) nargs,
1036 void attribute((unused)) *u) {
1042 if(!(pid = xfork())) {
1047 execlp("sh", "sh", "-c", args[0], (char *)0);
1048 fatal(errno, "error executing sh");
1051 while((n = read(p[0], buffer, sizeof buffer))) {
1053 if(errno == EINTR) continue;
1054 else fatal(errno, "error reading from pipe");
1056 output->sink->write(output->sink, buffer, n);
1059 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
1061 if(n < 0) fatal(errno, "error calling waitpid");
1063 error(0, "shell command '%s' %s", args[0], wstat(w));
1066 static inline int str2bool(const char *s) {
1067 return !strcmp(s, "true");
1070 static inline const char *bool2str(int n) {
1071 return n ? "true" : "false";
1074 static char *expandarg(const char *arg, dcgi_state *ds) {
1080 output.sink = sink_dynstr(&d);
1081 expandstring(&output, arg, ds);
1082 dynstr_terminate(&d);
1086 static void exp_prefs(int attribute((unused)) nargs,
1091 dcgi_state substate;
1093 const char *file = expandarg(args[0], ds);
1095 memset(&substate, 0, sizeof substate);
1098 if(disorder_prefs(ds->g->client, file, &k)) return;
1100 substate.last = !k->next;
1102 expandstring(output, args[1], &substate);
1109 static void exp_pref(int attribute((unused)) nargs,
1116 if(!disorder_get(ds->g->client, args[0], args[1], &value))
1117 cgi_output(output, "%s", value);
1120 static void exp_if(int nargs,
1125 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
1128 expandstring(output, args[n], ds);
1131 static void exp_and(int nargs,
1138 for(n = 0; n < nargs; ++n)
1139 if(!str2bool(expandarg(args[n], ds))) {
1143 sink_printf(output->sink, "%s", bool2str(result));
1146 static void exp_or(int nargs,
1153 for(n = 0; n < nargs; ++n)
1154 if(str2bool(expandarg(args[n], ds))) {
1158 sink_printf(output->sink, "%s", bool2str(result));
1161 static void exp_not(int attribute((unused)) nargs,
1164 void attribute((unused)) *u) {
1165 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
1168 static void exp_isplaying(int attribute((unused)) nargs,
1169 char attribute((unused)) **args,
1174 lookups(ds, DC_PLAYING);
1175 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
1178 static void exp_isqueue(int attribute((unused)) nargs,
1179 char attribute((unused)) **args,
1184 lookups(ds, DC_QUEUE);
1185 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
1188 static void exp_isrecent(int attribute((unused)) nargs,
1189 char attribute((unused)) **args,
1194 lookups(ds, DC_RECENT);
1195 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
1198 static void exp_isnew(int attribute((unused)) nargs,
1199 char attribute((unused)) **args,
1204 lookups(ds, DC_NEW);
1205 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
1208 static void exp_id(int attribute((unused)) nargs,
1209 char attribute((unused)) **args,
1215 cgi_output(output, "%s", ds->track->id);
1218 static void exp_track(int attribute((unused)) nargs,
1219 char attribute((unused)) **args,
1225 cgi_output(output, "%s", ds->track->track);
1228 static void exp_parity(int attribute((unused)) nargs,
1229 char attribute((unused)) **args,
1234 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
1237 static void exp_comment(int attribute((unused)) nargs,
1238 char attribute((unused)) **args,
1239 cgi_sink attribute((unused)) *output,
1240 void attribute((unused)) *u) {
1244 static void exp_prefname(int attribute((unused)) nargs,
1245 char attribute((unused)) **args,
1250 if(ds->pref && ds->pref->name)
1251 cgi_output(output, "%s", ds->pref->name);
1254 static void exp_prefvalue(int attribute((unused)) nargs,
1255 char attribute((unused)) **args,
1260 if(ds->pref && ds->pref->value)
1261 cgi_output(output, "%s", ds->pref->value);
1264 static void exp_isfiles(int attribute((unused)) nargs,
1265 char attribute((unused)) **args,
1270 lookups(ds, DC_FILES);
1271 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1274 static void exp_isdirectories(int attribute((unused)) nargs,
1275 char attribute((unused)) **args,
1280 lookups(ds, DC_DIRS);
1281 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1284 static void exp_choose(int attribute((unused)) nargs,
1289 dcgi_state substate;
1293 const char *type, *what = expandarg(args[0], ds);
1295 if(!strcmp(what, "files")) {
1296 lookups(ds, DC_FILES);
1297 files = ds->g->files;
1298 nfiles = ds->g->nfiles;
1300 } else if(!strcmp(what, "directories")) {
1301 lookups(ds, DC_DIRS);
1302 files = ds->g->dirs;
1303 nfiles = ds->g->ndirs;
1306 error(0, "unknown @choose@ argument '%s'", what);
1309 e = xmalloc(nfiles * sizeof (struct entry));
1310 for(n = 0; n < nfiles; ++n) {
1311 e[n].path = files[n];
1312 e[n].sort = trackname_transform(type, files[n], "sort");
1313 e[n].display = trackname_transform(type, files[n], "display");
1315 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1316 memset(&substate, 0, sizeof substate);
1319 for(n = 0; n < nfiles; ++n) {
1320 substate.last = (n == nfiles - 1);
1322 substate.entry = &e[n];
1323 expandstring(output, args[1], &substate);
1328 static void exp_file(int attribute((unused)) nargs,
1329 char attribute((unused)) **args,
1335 cgi_output(output, "%s", ds->entry->path);
1337 cgi_output(output, "%s", ds->track->track);
1339 cgi_output(output, "%s", ds->tracks[0]);
1342 static void exp_transform(int nargs,
1345 void attribute((unused)) *u) {
1346 const char *context = nargs > 2 ? args[2] : "display";
1348 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1351 static void exp_urlquote(int attribute((unused)) nargs,
1354 void attribute((unused)) *u) {
1355 cgi_output(output, "%s", urlencodestring(args[0]));
1358 static void exp_scratchable(int attribute((unused)) nargs,
1359 char attribute((unused)) **args,
1361 void attribute((unused)) *u) {
1364 lookups(ds, DC_PLAYING|DC_RIGHTS);
1365 sink_printf(output->sink, "%s",
1366 bool2str(right_scratchable(ds->g->rights,
1367 disorder_user(ds->g->client),
1371 static void exp_removable(int attribute((unused)) nargs,
1372 char attribute((unused)) **args,
1374 void attribute((unused)) *u) {
1377 lookups(ds, DC_RIGHTS);
1378 sink_printf(output->sink, "%s",
1379 bool2str(right_removable(ds->g->rights,
1380 disorder_user(ds->g->client),
1384 static void exp_movable(int attribute((unused)) nargs,
1385 char attribute((unused)) **args,
1387 void attribute((unused)) *u) {
1390 lookups(ds, DC_RIGHTS);
1391 sink_printf(output->sink, "%s",
1392 bool2str(right_movable(ds->g->rights,
1393 disorder_user(ds->g->client),
1397 static void exp_navigate(int attribute((unused)) nargs,
1402 dcgi_state substate;
1403 const char *path = expandarg(args[0], ds);
1408 memset(&substate, 0, sizeof substate);
1410 ptr = path + 1; /* skip root */
1412 substate.nav_path = path;
1415 while(*ptr && *ptr != '/')
1417 substate.last = !*ptr;
1418 substate.nav_len = ptr - path;
1419 substate.nav_dirlen = dirlen;
1420 expandstring(output, args[1], &substate);
1421 dirlen = substate.nav_len;
1428 static void exp_fullname(int attribute((unused)) nargs,
1429 char attribute((unused)) **args,
1433 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1436 static void exp_basename(int nargs,
1444 if((s = strrchr(args[0], '/'))) ++s;
1446 cgi_output(output, "%s", s);
1448 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1449 ds->nav_path + ds->nav_dirlen + 1);
1452 static void exp_dirname(int nargs,
1460 if((s = strrchr(args[0], '/')))
1461 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1463 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1466 static void exp_eq(int attribute((unused)) nargs,
1469 void attribute((unused)) *u) {
1470 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1473 static void exp_ne(int attribute((unused)) nargs,
1476 void attribute((unused)) *u) {
1477 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1480 static void exp_enabled(int attribute((unused)) nargs,
1481 char attribute((unused)) **args,
1488 disorder_enabled(ds->g->client, &enabled);
1489 cgi_output(output, "%s", bool2str(enabled));
1492 static void exp_random_enabled(int attribute((unused)) nargs,
1493 char attribute((unused)) **args,
1500 disorder_random_enabled(ds->g->client, &enabled);
1501 cgi_output(output, "%s", bool2str(enabled));
1504 static void exp_trackstate(int attribute((unused)) nargs,
1509 struct queue_entry *q;
1512 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1513 lookups(ds, DC_QUEUE|DC_PLAYING);
1514 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1515 cgi_output(output, "playing");
1517 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1520 cgi_output(output, "queued");
1524 static void exp_thisurl(int attribute((unused)) nargs,
1525 char attribute((unused)) **args,
1527 void attribute((unused)) *u) {
1528 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1529 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1532 static void exp_isfirst(int attribute((unused)) nargs,
1533 char attribute((unused)) **args,
1538 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1541 static void exp_islast(int attribute((unused)) nargs,
1542 char attribute((unused)) **args,
1547 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1550 static void exp_action(int attribute((unused)) nargs,
1551 char attribute((unused)) **args,
1553 void attribute((unused)) *u) {
1554 const char *action = cgi_get("action"), *mgmt;
1556 if(!action) action = "playing";
1557 if(!strcmp(action, "playing")
1558 && (mgmt = cgi_get("mgmt"))
1559 && !strcmp(mgmt, "true"))
1561 sink_printf(output->sink, "%s", action);
1564 static void exp_resolve(int attribute((unused)) nargs,
1567 void attribute((unused)) *u) {
1571 if(!disorder_resolve(ds->g->client, &track, args[0]))
1572 sink_printf(output->sink, "%s", track);
1575 static void exp_paused(int attribute((unused)) nargs,
1576 char attribute((unused)) **args,
1582 lookups(ds, DC_PLAYING);
1583 if(ds->g->playing && ds->g->playing->state == playing_paused)
1585 cgi_output(output, "%s", bool2str(paused));
1588 static void exp_state(int attribute((unused)) nargs,
1589 char attribute((unused)) **args,
1595 cgi_output(output, "%s", playing_states[ds->track->state]);
1598 static void exp_files(int attribute((unused)) nargs,
1603 dcgi_state substate;
1604 const char *nfiles_arg, *directory;
1605 int nfiles, numfile;
1608 memset(&substate, 0, sizeof substate);
1610 if((directory = cgi_get("directory"))) {
1611 /* Prefs for whole directory. */
1612 lookups(ds, DC_FILES);
1613 /* Synthesize args for the file list. */
1614 nfiles = ds->g->nfiles;
1615 for(numfile = 0; numfile < nfiles; ++numfile) {
1616 k = xmalloc(sizeof *k);
1617 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1618 k->value = ds->g->files[numfile];
1623 /* Args already present. */
1624 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1627 for(numfile = 0; numfile < nfiles; ++numfile) {
1628 substate.index = numfile;
1629 expandstring(output, args[0], &substate);
1633 static void exp_index(int attribute((unused)) nargs,
1634 char attribute((unused)) **args,
1639 cgi_output(output, "%d", ds->index);
1642 static void exp_nfiles(int attribute((unused)) nargs,
1643 char attribute((unused)) **args,
1647 const char *files_arg;
1649 if(cgi_get("directory")) {
1650 lookups(ds, DC_FILES);
1651 cgi_output(output, "%d", ds->g->nfiles);
1652 } else if((files_arg = cgi_get("files")))
1653 cgi_output(output, "%s", files_arg);
1655 cgi_output(output, "1");
1658 static void exp_user(int attribute((unused)) nargs,
1659 char attribute((unused)) **args,
1662 dcgi_state *const ds = u;
1664 cgi_output(output, "%s", disorder_user(ds->g->client));
1667 static void exp_right(int attribute((unused)) nargs,
1671 dcgi_state *const ds = u;
1672 const char *right = expandarg(args[0], ds);
1675 lookups(ds, DC_RIGHTS);
1676 if(parse_rights(right, &r, 1/*report*/))
1679 cgi_output(output, "%s", bool2str(!!(r & ds->g->rights)));
1680 else if(r & ds->g->rights)
1681 expandstring(output, args[1], ds);
1683 expandstring(output, args[2], ds);
1686 static void exp_userinfo(int attribute((unused)) nargs,
1690 dcgi_state *const ds = u;
1693 if(disorder_userinfo(ds->g->client, disorder_user(ds->g->client), args[0],
1696 cgi_output(output, "%s", value);
1699 static const struct cgi_expansion expansions[] = {
1700 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1701 { "action", 0, 0, 0, exp_action },
1702 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1703 { "arg", 1, 1, 0, exp_arg },
1704 { "basename", 0, 1, 0, exp_basename },
1705 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1706 { "dirname", 0, 1, 0, exp_dirname },
1707 { "enabled", 0, 0, 0, exp_enabled },
1708 { "eq", 2, 2, 0, exp_eq },
1709 { "file", 0, 0, 0, exp_file },
1710 { "files", 1, 1, EXP_MAGIC, exp_files },
1711 { "fullname", 0, 0, 0, exp_fullname },
1712 { "id", 0, 0, 0, exp_id },
1713 { "if", 2, 3, EXP_MAGIC, exp_if },
1714 { "include", 1, 1, 0, exp_include },
1715 { "index", 0, 0, 0, exp_index },
1716 { "isdirectories", 0, 0, 0, exp_isdirectories },
1717 { "isfiles", 0, 0, 0, exp_isfiles },
1718 { "isfirst", 0, 0, 0, exp_isfirst },
1719 { "islast", 0, 0, 0, exp_islast },
1720 { "isnew", 0, 0, 0, exp_isnew },
1721 { "isplaying", 0, 0, 0, exp_isplaying },
1722 { "isqueue", 0, 0, 0, exp_isqueue },
1723 { "isrecent", 0, 0, 0, exp_isrecent },
1724 { "label", 1, 1, 0, exp_label },
1725 { "length", 0, 0, 0, exp_length },
1726 { "movable", 0, 0, 0, exp_movable },
1727 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1728 { "ne", 2, 2, 0, exp_ne },
1729 { "new", 1, 1, EXP_MAGIC, exp_new },
1730 { "nfiles", 0, 0, 0, exp_nfiles },
1731 { "nonce", 0, 0, 0, exp_nonce },
1732 { "not", 1, 1, 0, exp_not },
1733 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1734 { "parity", 0, 0, 0, exp_parity },
1735 { "part", 1, 3, 0, exp_part },
1736 { "paused", 0, 0, 0, exp_paused },
1737 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1738 { "pref", 2, 2, 0, exp_pref },
1739 { "prefname", 0, 0, 0, exp_prefname },
1740 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1741 { "prefvalue", 0, 0, 0, exp_prefvalue },
1742 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1743 { "random-enabled", 0, 0, 0, exp_random_enabled },
1744 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1745 { "removable", 0, 0, 0, exp_removable },
1746 { "resolve", 1, 1, 0, exp_resolve },
1747 { "right", 1, 3, EXP_MAGIC, exp_right },
1748 { "scratchable", 0, 0, 0, exp_scratchable },
1749 { "search", 2, 3, EXP_MAGIC, exp_search },
1750 { "server-version", 0, 0, 0, exp_server_version },
1751 { "shell", 1, 1, 0, exp_shell },
1752 { "state", 0, 0, 0, exp_state },
1753 { "stats", 0, 0, 0, exp_stats },
1754 { "thisurl", 0, 0, 0, exp_thisurl },
1755 { "track", 0, 0, 0, exp_track },
1756 { "trackstate", 1, 1, 0, exp_trackstate },
1757 { "transform", 2, 3, 0, exp_transform },
1758 { "url", 0, 0, 0, exp_url },
1759 { "urlquote", 1, 1, 0, exp_urlquote },
1760 { "user", 0, 0, 0, exp_user },
1761 { "userinfo", 1, 1, 0, exp_userinfo },
1762 { "version", 0, 0, 0, exp_version },
1763 { "volume", 1, 1, 0, exp_volume },
1764 { "when", 0, 0, 0, exp_when },
1765 { "who", 0, 0, 0, exp_who }
1768 static void expand(cgi_sink *output,
1769 const char *template,
1771 cgi_expand(template,
1772 expansions, sizeof expansions / sizeof *expansions,
1777 static void expandstring(cgi_sink *output,
1780 cgi_expand_string("",
1782 expansions, sizeof expansions / sizeof *expansions,
1787 static void perform_action(cgi_sink *output, dcgi_state *ds,
1788 const char *action) {
1791 /* We don't ever want anything to be cached */
1792 cgi_header(output->sink, "Cache-Control", "no-cache");
1793 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1794 actions[n].handler(output, ds);
1796 expand_template(ds, output, action);
1799 void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1800 const char *action = cgi_get("action");
1803 /* We allow URLs which are just confirm=... in order to keep confirmation
1804 * URLs, which are user-facing, as short as possible. */
1810 perform_action(output, ds, action);
1813 void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1815 cgi_set_option("error", msg);
1816 perform_action(output, ds, "error");
1819 /** @brief Log in as the current user or guest if none */
1820 void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) {
1821 /* Create a new connection */
1822 ds->g->client = disorder_new(0);
1823 /* Forget everything we knew */
1826 if(disorder_connect_cookie(ds->g->client, login_cookie)) {
1827 disorder_cgi_error(output, ds, "connect");
1830 /* If there was a cookie but it went bad, we forget it */
1831 if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest"))