2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 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"
64 static void expand(cgi_sink *output,
67 static void expandstring(cgi_sink *output,
77 static const char nonce_base64_table[] =
78 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-/*";
80 static const char *nonce(void) {
81 static uint32_t count;
90 nd.pid = (uint32_t)getpid();
91 nd.when = (uint32_t)time(0);
92 return generic_to_base64((void *)&nd, sizeof nd,
96 static int compare_entry(const void *a, const void *b) {
97 const struct entry *ea = a, *eb = b;
99 return compare_tracks(ea->sort, eb->sort,
100 ea->display, eb->display,
104 static const char *front_url(void) {
108 /* preserve management interface visibility */
109 if((mgmt = cgi_get("mgmt")) && !strcmp(mgmt, "true")) {
110 byte_xasprintf(&url, "%s?mgmt=true", config->url);
116 static void header_cookie(struct sink *output) {
120 memset(&u, 0, sizeof u);
122 parse_url(config->url, &u);
124 dynstr_append_string(d, "disorder=");
125 dynstr_append_string(d, login_cookie);
127 /* Force browser to discard cookie */
128 dynstr_append_string(d, "disorder=none;Max-Age=0");
131 /* The default domain matches the request host, so we need not override
132 * that. But the default path only goes up to the rightmost /, which would
133 * cause the browser to expose the cookie to other CGI programs on the same
135 dynstr_append_string(d, ";Version=1;Path=");
136 dynstr_append_string(d, u.path);
139 cgi_header(output, "Set-Cookie", d->vec);
142 static void redirect(struct sink *output) {
145 back = cgi_get("back");
146 cgi_header(output, "Location", back && *back ? back : front_url());
147 header_cookie(output);
151 static void expand_template(dcgi_state *ds, cgi_sink *output,
152 const char *action) {
153 cgi_header(output->sink, "Content-Type", "text/html");
154 header_cookie(output->sink);
155 cgi_body(output->sink);
156 expand(output, action, ds);
159 static void lookups(dcgi_state *ds, unsigned want) {
161 struct queue_entry *r, *rnext;
162 const char *dir, *re;
165 if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) {
167 disorder_queue(ds->g->client, &ds->g->queue);
168 if(need & DC_PLAYING)
169 disorder_playing(ds->g->client, &ds->g->playing);
171 disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0);
172 if(need & DC_RECENT) {
173 /* we need to reverse the order of the list */
174 disorder_recent(ds->g->client, &r);
177 r->next = ds->g->recent;
183 disorder_get_volume(ds->g->client,
184 &ds->g->volume_left, &ds->g->volume_right);
185 if(need & (DC_FILES|DC_DIRS)) {
186 if(!(dir = cgi_get("directory")))
188 re = cgi_get("regexp");
190 if(disorder_directories(ds->g->client, dir, re,
191 &ds->g->dirs, &ds->g->ndirs))
194 if(disorder_files(ds->g->client, dir, re,
195 &ds->g->files, &ds->g->nfiles))
198 if(need & DC_RIGHTS) {
199 ds->g->rights = RIGHT_READ; /* fail-safe */
200 if(!disorder_userinfo(ds->g->client, disorder_user(ds->g->client),
202 parse_rights(rights, &ds->g->rights, 1);
204 ds->g->flags |= need;
208 /* actions ********************************************************************/
210 static void act_disable(cgi_sink *output,
213 disorder_disable(ds->g->client);
214 redirect(output->sink);
217 static void act_enable(cgi_sink *output,
220 disorder_enable(ds->g->client);
221 redirect(output->sink);
224 static void act_random_disable(cgi_sink *output,
227 disorder_random_disable(ds->g->client);
228 redirect(output->sink);
231 static void act_random_enable(cgi_sink *output,
234 disorder_random_enable(ds->g->client);
235 redirect(output->sink);
238 static void act_remove(cgi_sink *output,
242 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
244 disorder_remove(ds->g->client, id);
245 redirect(output->sink);
248 static void act_move(cgi_sink *output,
250 const char *id, *delta;
252 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
253 if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument");
255 disorder_move(ds->g->client, id, atoi(delta));
256 redirect(output->sink);
259 static void act_scratch(cgi_sink *output,
262 disorder_scratch(ds->g->client, cgi_get("id"));
263 redirect(output->sink);
266 static void act_playing(cgi_sink *output, dcgi_state *ds) {
268 long refresh = config->refresh, length;
270 int random_enabled = 0;
273 lookups(ds, DC_PLAYING|DC_QUEUE);
274 cgi_header(output->sink, "Content-Type", "text/html");
275 disorder_random_enabled(ds->g->client, &random_enabled);
276 disorder_enabled(ds->g->client, &enabled);
278 && ds->g->playing->state == playing_started /* i.e. not paused */
279 && !disorder_length(ds->g->client, ds->g->playing->track, &length)
281 && ds->g->playing->sofar >= 0) {
282 /* Try to put the next refresh at the start of the next track. */
284 fin = now + length - ds->g->playing->sofar + config->gap;
285 if(now + refresh > fin)
288 if(ds->g->queue && ds->g->queue->state == playing_isscratch) {
289 /* next track is a scratch, don't leave more than the inter-track gap */
290 if(refresh > config->gap)
291 refresh = config->gap;
293 if(!ds->g->playing && ((ds->g->queue
294 && ds->g->queue->state != playing_random)
295 || random_enabled) && enabled) {
296 /* no track playing but playing is enabled and there is something coming
297 * up, must be in a gap */
298 if(refresh > config->gap)
299 refresh = config->gap;
301 byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1,
303 cgi_header(output->sink, "Refresh", r);
304 header_cookie(output->sink);
305 cgi_body(output->sink);
306 expand(output, "playing", ds);
309 static void act_play(cgi_sink *output,
311 const char *track, *dir;
316 if((track = cgi_get("file"))) {
317 disorder_play(ds->g->client, track);
318 } else if((dir = cgi_get("directory"))) {
319 if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0;
321 e = xmalloc(ntracks * sizeof (struct entry));
322 for(n = 0; n < ntracks; ++n) {
323 e[n].path = tracks[n];
324 e[n].sort = trackname_transform("track", tracks[n], "sort");
325 e[n].display = trackname_transform("track", tracks[n], "display");
327 qsort(e, ntracks, sizeof (struct entry), compare_entry);
328 for(n = 0; n < ntracks; ++n)
329 disorder_play(ds->g->client, e[n].path);
332 /* XXX error handling */
333 redirect(output->sink);
336 static int clamp(int n, int min, int max) {
344 static const char *volume_url(void) {
347 byte_xasprintf(&url, "%s?action=volume", config->url);
351 static void act_volume(cgi_sink *output, dcgi_state *ds) {
352 const char *l, *r, *d, *back;
353 int nd, changed = 0;;
355 if((d = cgi_get("delta"))) {
356 lookups(ds, DC_VOLUME);
357 nd = clamp(atoi(d), -255, 255);
358 disorder_set_volume(ds->g->client,
359 clamp(ds->g->volume_left + nd, 0, 255),
360 clamp(ds->g->volume_right + nd, 0, 255));
362 } else if((l = cgi_get("left")) && (r = cgi_get("right"))) {
363 disorder_set_volume(ds->g->client, atoi(l), atoi(r));
367 /* redirect back to ourselves (but without the volume-changing bits in the
369 cgi_header(output->sink, "Location",
370 (back = cgi_get("back")) ? back : volume_url());
371 header_cookie(output->sink);
372 cgi_body(output->sink);
374 cgi_header(output->sink, "Content-Type", "text/html");
375 header_cookie(output->sink);
376 cgi_body(output->sink);
377 expand(output, "volume", ds);
381 static void act_prefs_errors(const char *msg,
382 void attribute((unused)) *u) {
383 fatal(0, "error splitting parts list: %s", msg);
386 static const char *numbered_arg(const char *argname, int numfile) {
389 byte_xasprintf(&fullname, "%d_%s", numfile, argname);
390 return cgi_get(fullname);
393 static void process_prefs(dcgi_state *ds, int numfile) {
394 const char *file, *name, *value, *part, *parts, *current, *context;
397 if(!(file = numbered_arg("file", numfile)))
398 /* The first file doesn't need numbering. */
399 if(numfile > 0 || !(file = cgi_get("file")))
401 if((parts = numbered_arg("parts", numfile))
402 || (parts = cgi_get("parts"))) {
403 /* Default context is display. Other contexts not actually tested. */
404 if(!(context = numbered_arg("context", numfile))) context = "display";
405 partslist = split(parts, 0, 0, act_prefs_errors, 0);
406 while((part = *partslist++)) {
407 if(!(value = numbered_arg(part, numfile)))
409 /* If it's already right (whether regexps or db) don't change anything,
410 * so we don't fill the database up with rubbish. */
411 if(disorder_part(ds->g->client, (char **)¤t,
412 file, context, part))
413 fatal(0, "disorder_part() failed");
414 if(!strcmp(current, value))
416 byte_xasprintf((char **)&name, "trackname_%s_%s", context, part);
417 disorder_set(ds->g->client, file, name, value);
419 if((value = numbered_arg("random", numfile)))
420 disorder_unset(ds->g->client, file, "pick_at_random");
422 disorder_set(ds->g->client, file, "pick_at_random", "0");
423 if((value = numbered_arg("tags", numfile)))
424 disorder_set(ds->g->client, file, "tags", value);
425 } else if((name = cgi_get("name"))) {
426 /* Raw preferences. Not well supported in the templates at the moment. */
427 value = cgi_get("value");
429 disorder_set(ds->g->client, file, name, value);
431 disorder_unset(ds->g->client, file, name);
435 static void act_prefs(cgi_sink *output, dcgi_state *ds) {
439 if((files = cgi_get("files"))) nfiles = atoi(files);
441 for(numfile = 0; numfile < nfiles; ++numfile)
442 process_prefs(ds, numfile);
443 cgi_header(output->sink, "Content-Type", "text/html");
444 header_cookie(output->sink);
445 cgi_body(output->sink);
446 expand(output, "prefs", ds);
449 static void act_pause(cgi_sink *output,
452 disorder_pause(ds->g->client);
453 redirect(output->sink);
456 static void act_resume(cgi_sink *output,
459 disorder_resume(ds->g->client);
460 redirect(output->sink);
463 static void act_login(cgi_sink *output,
465 const char *username, *password, *back;
468 username = cgi_get("username");
469 password = cgi_get("password");
470 if(!username || !password
471 || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) {
472 /* We're just visiting the login page */
473 expand_template(ds, output, "login");
476 /* We'll need a new connection as we are going to stop being guest */
478 if(disorder_connect_user(c, username, password)) {
479 cgi_set_option("error", "loginfailed");
480 expand_template(ds, output, "login");
483 if(disorder_make_cookie(c, &login_cookie)) {
484 cgi_set_option("error", "cookiefailed");
485 expand_template(ds, output, "login");
488 /* Use the new connection henceforth */
491 /* We have a new cookie */
492 header_cookie(output->sink);
493 cgi_set_option("status", "loginok");
494 if((back = cgi_get("back")) && *back)
495 /* Redirect back to somewhere or other */
496 redirect(output->sink);
498 /* Stick to the login page */
499 expand_template(ds, output, "login");
502 static void act_logout(cgi_sink *output,
504 disorder_revoke(ds->g->client);
506 /* Reconnect as guest */
507 disorder_cgi_login(ds, output);
508 /* Back to the login page */
509 cgi_set_option("status", "logoutok");
510 expand_template(ds, output, "login");
513 static void act_register(cgi_sink *output,
515 const char *username, *password, *password2, *email;
516 char *confirm, *content_type;
517 const char *text, *encoding, *charset;
519 username = cgi_get("username");
520 password = cgi_get("password1");
521 password2 = cgi_get("password2");
522 email = cgi_get("email");
524 if(!username || !*username) {
525 cgi_set_option("error", "nousername");
526 expand_template(ds, output, "login");
529 if(!password || !*password) {
530 cgi_set_option("error", "nopassword");
531 expand_template(ds, output, "login");
534 if(!password2 || !*password2 || strcmp(password, password2)) {
535 cgi_set_option("error", "passwordmismatch");
536 expand_template(ds, output, "login");
539 if(!email || !*email) {
540 cgi_set_option("error", "noemail");
541 expand_template(ds, output, "login");
544 /* We could well do better address validation but for now we'll just do the
546 if(!strchr(email, '@')) {
547 cgi_set_option("error", "bademail");
548 expand_template(ds, output, "login");
551 if(disorder_register(ds->g->client, username, password, email, &confirm)) {
552 cgi_set_option("error", "cannotregister");
553 expand_template(ds, output, "login");
556 /* Send the user a mail */
557 /* TODO templatize this */
558 byte_xasprintf((char **)&text,
559 "Welcome to DisOrder. To active your login, please visit this URL:\n"
561 "%s?c=%s\n", config->url, urlencodestring(confirm));
562 if(!(text = mime_encode_text(text, &charset, &encoding)))
563 fatal(0, "cannot encode email");
564 byte_xasprintf(&content_type, "text/plain;charset=%s",
565 quote822(charset, 0));
566 sendmail("", config->mail_sender, email, "Welcome to DisOrder",
567 encoding, content_type, text); /* TODO error checking */
568 /* We'll go back to the login page with a suitable message */
569 cgi_set_option("status", "registered");
570 expand_template(ds, output, "login");
573 static void act_confirm(cgi_sink *output,
575 const char *confirmation;
577 if(!(confirmation = cgi_get("c"))) {
578 cgi_set_option("error", "noconfirm");
579 expand_template(ds, output, "login");
581 /* Confirm our registration */
582 if(disorder_confirm(ds->g->client, confirmation)) {
583 cgi_set_option("error", "badconfirm");
584 expand_template(ds, output, "login");
587 if(disorder_make_cookie(ds->g->client, &login_cookie)) {
588 cgi_set_option("error", "cookiefailed");
589 expand_template(ds, output, "login");
592 /* Discard any cached data JIC */
594 /* We have a new cookie */
595 header_cookie(output->sink);
596 cgi_set_option("status", "confirmed");
597 expand_template(ds, output, "login");
600 static void act_edituser(cgi_sink *output,
602 const char *email = cgi_get("email"), *password = cgi_get("changepassword1");
603 const char *password2 = cgi_get("changepassword2");
607 if((password && *password) || (password && *password2)) {
608 if(!password || !password2 || strcmp(password, password2)) {
609 cgi_set_option("error", "passwordmismatch");
610 expand_template(ds, output, "login");
614 password = password2 = 0;
617 if(disorder_edituser(ds->g->client, disorder_user(ds->g->client),
619 cgi_set_option("error", "badedit");
620 expand_template(ds, output, "login");
625 if(disorder_edituser(ds->g->client, disorder_user(ds->g->client),
626 "password", password)) {
627 cgi_set_option("error", "badedit");
628 expand_template(ds, output, "login");
634 login_cookie = 0; /* it'll be invalid now */
635 /* This is a bit duplicative of act_login() */
637 if(disorder_connect_user(c, disorder_user(ds->g->client), password)) {
638 cgi_set_option("error", "loginfailed");
639 expand_template(ds, output, "login");
642 if(disorder_make_cookie(c, &login_cookie)) {
643 cgi_set_option("error", "cookiefailed");
644 expand_template(ds, output, "login");
647 /* Use the new connection henceforth */
650 /* We have a new cookie */
651 header_cookie(output->sink);
653 cgi_set_option("status", "edited");
654 expand_template(ds, output, "login");
657 static void act_reminder(cgi_sink *output,
659 const char *const username = cgi_get("username");
661 if(!username || !*username) {
662 cgi_set_option("error", "nousername");
663 expand_template(ds, output, "login");
666 if(disorder_reminder(ds->g->client, username)) {
667 cgi_set_option("error", "reminderfailed");
668 expand_template(ds, output, "login");
671 cgi_set_option("status", "reminded");
672 expand_template(ds, output, "login");
675 static const struct action {
677 void (*handler)(cgi_sink *output, dcgi_state *ds);
679 { "confirm", act_confirm },
680 { "disable", act_disable },
681 { "edituser", act_edituser },
682 { "enable", act_enable },
683 { "login", act_login },
684 { "logout", act_logout },
685 { "move", act_move },
686 { "pause", act_pause },
687 { "play", act_play },
688 { "playing", act_playing },
689 { "prefs", act_prefs },
690 { "random-disable", act_random_disable },
691 { "random-enable", act_random_enable },
692 { "register", act_register },
693 { "reminder", act_reminder },
694 { "remove", act_remove },
695 { "resume", act_resume },
696 { "scratch", act_scratch },
697 { "volume", act_volume },
700 /* expansions *****************************************************************/
702 static void exp_include(int attribute((unused)) nargs,
706 expand(output, args[0], u);
709 static void exp_server_version(int attribute((unused)) nargs,
710 char attribute((unused)) **args,
717 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
719 v = "(server not running)";
720 cgi_output(output, "%s", v);
723 static void exp_version(int attribute((unused)) nargs,
724 char attribute((unused)) **args,
726 void attribute((unused)) *u) {
727 cgi_output(output, "%s", disorder_short_version_string);
730 static void exp_nonce(int attribute((unused)) nargs,
731 char attribute((unused)) **args,
733 void attribute((unused)) *u) {
734 cgi_output(output, "%s", nonce());
737 static void exp_label(int attribute((unused)) nargs,
740 void attribute((unused)) *u) {
741 cgi_output(output, "%s", cgi_label(args[0]));
744 struct trackinfo_state {
746 const struct queue_entry *q;
751 static void exp_who(int attribute((unused)) nargs,
752 char attribute((unused)) **args,
757 if(ds->track && ds->track->submitter)
758 cgi_output(output, "%s", ds->track->submitter);
761 static void exp_length(int attribute((unused)) nargs,
762 char attribute((unused)) **args,
769 && (ds->track->state == playing_started
770 || ds->track->state == playing_paused)
771 && ds->track->sofar >= 0)
772 cgi_output(output, "%ld:%02ld/",
773 ds->track->sofar / 60, ds->track->sofar % 60);
776 disorder_length(ds->g->client, ds->track->track, &length);
778 disorder_length(ds->g->client, ds->tracks[0], &length);
780 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
782 sink_printf(output->sink, "%s", " ");
785 static void exp_when(int attribute((unused)) nargs,
786 char attribute((unused)) **args,
790 const struct tm *w = 0;
793 switch(ds->track->state) {
794 case playing_isscratch:
795 case playing_unplayed:
797 if(ds->track->expected)
798 w = localtime(&ds->track->expected);
801 case playing_no_player:
803 case playing_scratched:
804 case playing_started:
806 case playing_quitting:
807 if(ds->track->played)
808 w = localtime(&ds->track->played);
812 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
814 sink_printf(output->sink, " ");
817 static void exp_part(int nargs,
822 const char *s, *track, *part, *context;
828 track = ds->track->track;
830 track = ds->tracks[0];
848 if(disorder_part(ds->g->client, (char **)&s, track,
849 !strcmp(context, "short") ? "display" : context, part))
850 fatal(0, "disorder_part() failed");
851 if(!strcmp(context, "short"))
852 s = truncate_for_display(s, config->short_display);
853 cgi_output(output, "%s", s);
855 sink_printf(output->sink, " ");
858 static void exp_playing(int attribute((unused)) nargs,
865 lookups(ds, DC_PLAYING);
866 memset(&s, 0, sizeof s);
869 s.track = ds->g->playing;
870 expandstring(output, args[0], &s);
874 static void exp_queue(int attribute((unused)) nargs,
880 struct queue_entry *q;
882 lookups(ds, DC_QUEUE);
883 memset(&s, 0, sizeof s);
886 for(q = ds->g->queue; q; q = q->next) {
889 expandstring(output, args[0], &s);
895 static void exp_recent(int attribute((unused)) nargs,
901 struct queue_entry *q;
903 lookups(ds, DC_RECENT);
904 memset(&s, 0, sizeof s);
907 for(q = ds->g->recent; q; q = q->next) {
910 expandstring(output, args[0], &s);
916 static void exp_new(int attribute((unused)) nargs,
924 memset(&s, 0, sizeof s);
927 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
928 s.last = s.index + 1 < ds->g->nnew;
929 s.tracks = &ds->g->new[s.index];
930 expandstring(output, args[0], &s);
935 static void exp_url(int attribute((unused)) nargs,
936 char attribute((unused)) **args,
938 void attribute((unused)) *u) {
939 cgi_output(output, "%s", config->url);
947 static int compare_result(const void *a, const void *b) {
948 const struct result *ra = a, *rb = b;
951 if(!(c = strcmp(ra->sort, rb->sort)))
952 c = strcmp(ra->track, rb->track);
956 static void exp_search(int nargs,
960 dcgi_state *ds = u, substate;
962 const char *q, *context, *part, *template;
978 assert(!"should never happen");
979 part = context = template = 0; /* quieten compiler */
981 if(ds->tracks == 0) {
982 /* we are the top level, let's get some search results */
983 if(!(q = cgi_get("query"))) return; /* no results yet */
984 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
988 ntracks = ds->ntracks;
990 assert(ntracks != 0);
991 /* sort tracks by the appropriate part */
992 r = xmalloc(ntracks * sizeof *r);
993 for(n = 0; n < ntracks; ++n) {
994 r[n].track = tracks[n];
995 if(disorder_part(ds->g->client, (char **)&r[n].sort,
996 tracks[n], context, part))
997 fatal(0, "disorder_part() failed");
999 qsort(r, ntracks, sizeof (struct result), compare_result);
1000 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
1001 * array as we know it's guaranteed to be big enough and isn't going to be
1002 * used for anything else any more. */
1003 memset(&substate, 0, sizeof substate);
1007 while(n < ntracks) {
1008 substate.tracks = tracks;
1009 substate.ntracks = 0;
1012 && !strcmp(r[m].sort, r[n].sort))
1013 tracks[substate.ntracks++] = r[m++].track;
1014 substate.last = (m == ntracks);
1015 expandstring(output, template, &substate);
1020 assert(substate.last != 0);
1023 static void exp_arg(int attribute((unused)) nargs,
1026 void attribute((unused)) *u) {
1029 if((v = cgi_get(args[0])))
1030 cgi_output(output, "%s", v);
1033 static void exp_stats(int attribute((unused)) nargs,
1034 char attribute((unused)) **args,
1040 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
1041 if(!disorder_stats(ds->g->client, &v, 0)) {
1043 cgi_output(output, "%s\n", *v++);
1045 cgi_closetag(output->sink, "pre");
1048 static void exp_volume(int attribute((unused)) nargs,
1054 lookups(ds, DC_VOLUME);
1055 if(!strcmp(args[0], "left"))
1056 cgi_output(output, "%d", ds->g->volume_left);
1058 cgi_output(output, "%d", ds->g->volume_right);
1061 static void exp_shell(int attribute((unused)) nargs,
1064 void attribute((unused)) *u) {
1070 if(!(pid = xfork())) {
1075 execlp("sh", "sh", "-c", args[0], (char *)0);
1076 fatal(errno, "error executing sh");
1079 while((n = read(p[0], buffer, sizeof buffer))) {
1081 if(errno == EINTR) continue;
1082 else fatal(errno, "error reading from pipe");
1084 output->sink->write(output->sink, buffer, n);
1087 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
1089 if(n < 0) fatal(errno, "error calling waitpid");
1091 error(0, "shell command '%s' %s", args[0], wstat(w));
1094 static inline int str2bool(const char *s) {
1095 return !strcmp(s, "true");
1098 static inline const char *bool2str(int n) {
1099 return n ? "true" : "false";
1102 static char *expandarg(const char *arg, dcgi_state *ds) {
1108 output.sink = sink_dynstr(&d);
1109 expandstring(&output, arg, ds);
1110 dynstr_terminate(&d);
1114 static void exp_prefs(int attribute((unused)) nargs,
1119 dcgi_state substate;
1121 const char *file = expandarg(args[0], ds);
1123 memset(&substate, 0, sizeof substate);
1126 if(disorder_prefs(ds->g->client, file, &k)) return;
1128 substate.last = !k->next;
1130 expandstring(output, args[1], &substate);
1137 static void exp_pref(int attribute((unused)) nargs,
1144 if(!disorder_get(ds->g->client, args[0], args[1], &value))
1145 cgi_output(output, "%s", value);
1148 static void exp_if(int nargs,
1153 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
1156 expandstring(output, args[n], ds);
1159 static void exp_and(int nargs,
1166 for(n = 0; n < nargs; ++n)
1167 if(!str2bool(expandarg(args[n], ds))) {
1171 sink_printf(output->sink, "%s", bool2str(result));
1174 static void exp_or(int nargs,
1181 for(n = 0; n < nargs; ++n)
1182 if(str2bool(expandarg(args[n], ds))) {
1186 sink_printf(output->sink, "%s", bool2str(result));
1189 static void exp_not(int attribute((unused)) nargs,
1192 void attribute((unused)) *u) {
1193 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
1196 static void exp_isplaying(int attribute((unused)) nargs,
1197 char attribute((unused)) **args,
1202 lookups(ds, DC_PLAYING);
1203 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
1206 static void exp_isqueue(int attribute((unused)) nargs,
1207 char attribute((unused)) **args,
1212 lookups(ds, DC_QUEUE);
1213 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
1216 static void exp_isrecent(int attribute((unused)) nargs,
1217 char attribute((unused)) **args,
1222 lookups(ds, DC_RECENT);
1223 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
1226 static void exp_isnew(int attribute((unused)) nargs,
1227 char attribute((unused)) **args,
1232 lookups(ds, DC_NEW);
1233 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
1236 static void exp_id(int attribute((unused)) nargs,
1237 char attribute((unused)) **args,
1243 cgi_output(output, "%s", ds->track->id);
1246 static void exp_track(int attribute((unused)) nargs,
1247 char attribute((unused)) **args,
1253 cgi_output(output, "%s", ds->track->track);
1256 static void exp_parity(int attribute((unused)) nargs,
1257 char attribute((unused)) **args,
1262 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
1265 static void exp_comment(int attribute((unused)) nargs,
1266 char attribute((unused)) **args,
1267 cgi_sink attribute((unused)) *output,
1268 void attribute((unused)) *u) {
1272 static void exp_prefname(int attribute((unused)) nargs,
1273 char attribute((unused)) **args,
1278 if(ds->pref && ds->pref->name)
1279 cgi_output(output, "%s", ds->pref->name);
1282 static void exp_prefvalue(int attribute((unused)) nargs,
1283 char attribute((unused)) **args,
1288 if(ds->pref && ds->pref->value)
1289 cgi_output(output, "%s", ds->pref->value);
1292 static void exp_isfiles(int attribute((unused)) nargs,
1293 char attribute((unused)) **args,
1298 lookups(ds, DC_FILES);
1299 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1302 static void exp_isdirectories(int attribute((unused)) nargs,
1303 char attribute((unused)) **args,
1308 lookups(ds, DC_DIRS);
1309 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1312 static void exp_choose(int attribute((unused)) nargs,
1317 dcgi_state substate;
1321 const char *type, *what = expandarg(args[0], ds);
1323 if(!strcmp(what, "files")) {
1324 lookups(ds, DC_FILES);
1325 files = ds->g->files;
1326 nfiles = ds->g->nfiles;
1328 } else if(!strcmp(what, "directories")) {
1329 lookups(ds, DC_DIRS);
1330 files = ds->g->dirs;
1331 nfiles = ds->g->ndirs;
1334 error(0, "unknown @choose@ argument '%s'", what);
1337 e = xmalloc(nfiles * sizeof (struct entry));
1338 for(n = 0; n < nfiles; ++n) {
1339 e[n].path = files[n];
1340 e[n].sort = trackname_transform(type, files[n], "sort");
1341 e[n].display = trackname_transform(type, files[n], "display");
1343 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1344 memset(&substate, 0, sizeof substate);
1347 for(n = 0; n < nfiles; ++n) {
1348 substate.last = (n == nfiles - 1);
1350 substate.entry = &e[n];
1351 expandstring(output, args[1], &substate);
1356 static void exp_file(int attribute((unused)) nargs,
1357 char attribute((unused)) **args,
1363 cgi_output(output, "%s", ds->entry->path);
1365 cgi_output(output, "%s", ds->track->track);
1367 cgi_output(output, "%s", ds->tracks[0]);
1370 static void exp_transform(int nargs,
1373 void attribute((unused)) *u) {
1374 const char *context = nargs > 2 ? args[2] : "display";
1376 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1379 static void exp_urlquote(int attribute((unused)) nargs,
1382 void attribute((unused)) *u) {
1383 cgi_output(output, "%s", urlencodestring(args[0]));
1386 static void exp_scratchable(int attribute((unused)) nargs,
1387 char attribute((unused)) **args,
1389 void attribute((unused)) *u) {
1392 lookups(ds, DC_PLAYING|DC_RIGHTS);
1393 sink_printf(output->sink, "%s",
1394 bool2str(right_scratchable(ds->g->rights,
1395 disorder_user(ds->g->client),
1399 static void exp_removable(int attribute((unused)) nargs,
1400 char attribute((unused)) **args,
1402 void attribute((unused)) *u) {
1405 lookups(ds, DC_RIGHTS);
1406 sink_printf(output->sink, "%s",
1407 bool2str(right_removable(ds->g->rights,
1408 disorder_user(ds->g->client),
1412 static void exp_movable(int attribute((unused)) nargs,
1413 char attribute((unused)) **args,
1415 void attribute((unused)) *u) {
1418 lookups(ds, DC_RIGHTS);
1419 sink_printf(output->sink, "%s",
1420 bool2str(right_movable(ds->g->rights,
1421 disorder_user(ds->g->client),
1425 static void exp_navigate(int attribute((unused)) nargs,
1430 dcgi_state substate;
1431 const char *path = expandarg(args[0], ds);
1436 memset(&substate, 0, sizeof substate);
1438 ptr = path + 1; /* skip root */
1440 substate.nav_path = path;
1443 while(*ptr && *ptr != '/')
1445 substate.last = !*ptr;
1446 substate.nav_len = ptr - path;
1447 substate.nav_dirlen = dirlen;
1448 expandstring(output, args[1], &substate);
1449 dirlen = substate.nav_len;
1456 static void exp_fullname(int attribute((unused)) nargs,
1457 char attribute((unused)) **args,
1461 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1464 static void exp_basename(int nargs,
1472 if((s = strrchr(args[0], '/'))) ++s;
1474 cgi_output(output, "%s", s);
1476 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1477 ds->nav_path + ds->nav_dirlen + 1);
1480 static void exp_dirname(int nargs,
1488 if((s = strrchr(args[0], '/')))
1489 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1491 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1494 static void exp_eq(int attribute((unused)) nargs,
1497 void attribute((unused)) *u) {
1498 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1501 static void exp_ne(int attribute((unused)) nargs,
1504 void attribute((unused)) *u) {
1505 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1508 static void exp_enabled(int attribute((unused)) nargs,
1509 char attribute((unused)) **args,
1516 disorder_enabled(ds->g->client, &enabled);
1517 cgi_output(output, "%s", bool2str(enabled));
1520 static void exp_random_enabled(int attribute((unused)) nargs,
1521 char attribute((unused)) **args,
1528 disorder_random_enabled(ds->g->client, &enabled);
1529 cgi_output(output, "%s", bool2str(enabled));
1532 static void exp_trackstate(int attribute((unused)) nargs,
1537 struct queue_entry *q;
1540 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1541 lookups(ds, DC_QUEUE|DC_PLAYING);
1542 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1543 cgi_output(output, "playing");
1545 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1548 cgi_output(output, "queued");
1552 static void exp_thisurl(int attribute((unused)) nargs,
1553 char attribute((unused)) **args,
1555 void attribute((unused)) *u) {
1556 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1557 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1560 static void exp_isfirst(int attribute((unused)) nargs,
1561 char attribute((unused)) **args,
1566 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1569 static void exp_islast(int attribute((unused)) nargs,
1570 char attribute((unused)) **args,
1575 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1578 static void exp_action(int attribute((unused)) nargs,
1579 char attribute((unused)) **args,
1581 void attribute((unused)) *u) {
1582 const char *action = cgi_get("action"), *mgmt;
1584 if(!action) action = "playing";
1585 if(!strcmp(action, "playing")
1586 && (mgmt = cgi_get("mgmt"))
1587 && !strcmp(mgmt, "true"))
1589 sink_printf(output->sink, "%s", action);
1592 static void exp_resolve(int attribute((unused)) nargs,
1595 void attribute((unused)) *u) {
1599 if(!disorder_resolve(ds->g->client, &track, args[0]))
1600 sink_printf(output->sink, "%s", track);
1603 static void exp_paused(int attribute((unused)) nargs,
1604 char attribute((unused)) **args,
1610 lookups(ds, DC_PLAYING);
1611 if(ds->g->playing && ds->g->playing->state == playing_paused)
1613 cgi_output(output, "%s", bool2str(paused));
1616 static void exp_state(int attribute((unused)) nargs,
1617 char attribute((unused)) **args,
1623 cgi_output(output, "%s", playing_states[ds->track->state]);
1626 static void exp_files(int attribute((unused)) nargs,
1631 dcgi_state substate;
1632 const char *nfiles_arg, *directory;
1633 int nfiles, numfile;
1636 memset(&substate, 0, sizeof substate);
1638 if((directory = cgi_get("directory"))) {
1639 /* Prefs for whole directory. */
1640 lookups(ds, DC_FILES);
1641 /* Synthesize args for the file list. */
1642 nfiles = ds->g->nfiles;
1643 for(numfile = 0; numfile < nfiles; ++numfile) {
1644 k = xmalloc(sizeof *k);
1645 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1646 k->value = ds->g->files[numfile];
1651 /* Args already present. */
1652 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1655 for(numfile = 0; numfile < nfiles; ++numfile) {
1656 substate.index = numfile;
1657 expandstring(output, args[0], &substate);
1661 static void exp_index(int attribute((unused)) nargs,
1662 char attribute((unused)) **args,
1667 cgi_output(output, "%d", ds->index);
1670 static void exp_nfiles(int attribute((unused)) nargs,
1671 char attribute((unused)) **args,
1675 const char *files_arg;
1677 if(cgi_get("directory")) {
1678 lookups(ds, DC_FILES);
1679 cgi_output(output, "%d", ds->g->nfiles);
1680 } else if((files_arg = cgi_get("files")))
1681 cgi_output(output, "%s", files_arg);
1683 cgi_output(output, "1");
1686 static void exp_user(int attribute((unused)) nargs,
1687 char attribute((unused)) **args,
1690 dcgi_state *const ds = u;
1692 cgi_output(output, "%s", disorder_user(ds->g->client));
1695 static void exp_right(int attribute((unused)) nargs,
1699 dcgi_state *const ds = u;
1700 const char *right = expandarg(args[0], ds);
1703 lookups(ds, DC_RIGHTS);
1704 if(parse_rights(right, &r, 1/*report*/))
1707 cgi_output(output, "%s", bool2str(!!(r & ds->g->rights)));
1708 else if(r & ds->g->rights)
1709 expandstring(output, args[1], ds);
1711 expandstring(output, args[2], ds);
1714 static void exp_userinfo(int attribute((unused)) nargs,
1718 dcgi_state *const ds = u;
1721 if(disorder_userinfo(ds->g->client, disorder_user(ds->g->client), args[0],
1724 cgi_output(output, "%s", value);
1727 static void exp_image(int attribute((unused)) nargs,
1730 void attribute((unused)) *u) {
1732 const char *imagestem;
1734 byte_xasprintf(&labelname, "images.%s", args[0]);
1735 if(cgi_label_exists(labelname))
1736 imagestem = cgi_label(labelname);
1737 else if(strchr(args[0], '.'))
1738 imagestem = args[0];
1740 byte_xasprintf((char **)&imagestem, "%s.png", args[0]);
1741 if(cgi_label_exists("url.static"))
1742 cgi_output(output, "%s/%s", cgi_label("url.static"), imagestem);
1744 cgi_output(output, "/disorder/%s", imagestem);
1747 static const struct cgi_expansion expansions[] = {
1748 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1749 { "action", 0, 0, 0, exp_action },
1750 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1751 { "arg", 1, 1, 0, exp_arg },
1752 { "basename", 0, 1, 0, exp_basename },
1753 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1754 { "dirname", 0, 1, 0, exp_dirname },
1755 { "enabled", 0, 0, 0, exp_enabled },
1756 { "eq", 2, 2, 0, exp_eq },
1757 { "file", 0, 0, 0, exp_file },
1758 { "files", 1, 1, EXP_MAGIC, exp_files },
1759 { "fullname", 0, 0, 0, exp_fullname },
1760 { "id", 0, 0, 0, exp_id },
1761 { "if", 2, 3, EXP_MAGIC, exp_if },
1762 { "image", 1, 1, 0, exp_image },
1763 { "include", 1, 1, 0, exp_include },
1764 { "index", 0, 0, 0, exp_index },
1765 { "isdirectories", 0, 0, 0, exp_isdirectories },
1766 { "isfiles", 0, 0, 0, exp_isfiles },
1767 { "isfirst", 0, 0, 0, exp_isfirst },
1768 { "islast", 0, 0, 0, exp_islast },
1769 { "isnew", 0, 0, 0, exp_isnew },
1770 { "isplaying", 0, 0, 0, exp_isplaying },
1771 { "isqueue", 0, 0, 0, exp_isqueue },
1772 { "isrecent", 0, 0, 0, exp_isrecent },
1773 { "label", 1, 1, 0, exp_label },
1774 { "length", 0, 0, 0, exp_length },
1775 { "movable", 0, 0, 0, exp_movable },
1776 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1777 { "ne", 2, 2, 0, exp_ne },
1778 { "new", 1, 1, EXP_MAGIC, exp_new },
1779 { "nfiles", 0, 0, 0, exp_nfiles },
1780 { "nonce", 0, 0, 0, exp_nonce },
1781 { "not", 1, 1, 0, exp_not },
1782 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1783 { "parity", 0, 0, 0, exp_parity },
1784 { "part", 1, 3, 0, exp_part },
1785 { "paused", 0, 0, 0, exp_paused },
1786 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1787 { "pref", 2, 2, 0, exp_pref },
1788 { "prefname", 0, 0, 0, exp_prefname },
1789 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1790 { "prefvalue", 0, 0, 0, exp_prefvalue },
1791 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1792 { "random-enabled", 0, 0, 0, exp_random_enabled },
1793 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1794 { "removable", 0, 0, 0, exp_removable },
1795 { "resolve", 1, 1, 0, exp_resolve },
1796 { "right", 1, 3, EXP_MAGIC, exp_right },
1797 { "scratchable", 0, 0, 0, exp_scratchable },
1798 { "search", 2, 3, EXP_MAGIC, exp_search },
1799 { "server-version", 0, 0, 0, exp_server_version },
1800 { "shell", 1, 1, 0, exp_shell },
1801 { "state", 0, 0, 0, exp_state },
1802 { "stats", 0, 0, 0, exp_stats },
1803 { "thisurl", 0, 0, 0, exp_thisurl },
1804 { "track", 0, 0, 0, exp_track },
1805 { "trackstate", 1, 1, 0, exp_trackstate },
1806 { "transform", 2, 3, 0, exp_transform },
1807 { "url", 0, 0, 0, exp_url },
1808 { "urlquote", 1, 1, 0, exp_urlquote },
1809 { "user", 0, 0, 0, exp_user },
1810 { "userinfo", 1, 1, 0, exp_userinfo },
1811 { "version", 0, 0, 0, exp_version },
1812 { "volume", 1, 1, 0, exp_volume },
1813 { "when", 0, 0, 0, exp_when },
1814 { "who", 0, 0, 0, exp_who }
1817 static void expand(cgi_sink *output,
1818 const char *template,
1820 cgi_expand(template,
1821 expansions, sizeof expansions / sizeof *expansions,
1826 static void expandstring(cgi_sink *output,
1829 cgi_expand_string("",
1831 expansions, sizeof expansions / sizeof *expansions,
1836 static void perform_action(cgi_sink *output, dcgi_state *ds,
1837 const char *action) {
1840 /* We don't ever want anything to be cached */
1841 cgi_header(output->sink, "Cache-Control", "no-cache");
1842 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1843 actions[n].handler(output, ds);
1845 expand_template(ds, output, action);
1848 void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1849 const char *action = cgi_get("action");
1852 /* We allow URLs which are just confirm=... in order to keep confirmation
1853 * URLs, which are user-facing, as short as possible. */
1859 perform_action(output, ds, action);
1862 void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1864 cgi_set_option("error", msg);
1865 perform_action(output, ds, "error");
1868 /** @brief Log in as the current user or guest if none */
1869 void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) {
1870 /* Create a new connection */
1871 ds->g->client = disorder_new(0);
1872 /* Forget everything we knew */
1875 if(disorder_connect_cookie(ds->g->client, login_cookie)) {
1876 disorder_cgi_error(output, ds, "connect");
1879 /* If there was a cookie but it went bad, we forget it */
1880 if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest"))