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 /* Formally we are supposed to quote the path, since it invariably has a
137 * slash in it. However Safari does not parse quoted paths correctly, so
138 * this won't work. Fortunately nothing else seems to care about proper
139 * quoting of paths, so in practice we get with it. (See also
140 * parse_cookie() where we are liberal about cookie paths on the way back
142 dynstr_append_string(d, u.path);
145 cgi_header(output, "Set-Cookie", d->vec);
148 static void redirect(struct sink *output) {
151 back = cgi_get("back");
152 cgi_header(output, "Location", back && *back ? back : front_url());
153 header_cookie(output);
157 static void expand_template(dcgi_state *ds, cgi_sink *output,
158 const char *action) {
159 cgi_header(output->sink, "Content-Type", "text/html");
160 header_cookie(output->sink);
161 cgi_body(output->sink);
162 expand(output, action, ds);
165 static void lookups(dcgi_state *ds, unsigned want) {
167 struct queue_entry *r, *rnext;
168 const char *dir, *re;
171 if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) {
173 disorder_queue(ds->g->client, &ds->g->queue);
174 if(need & DC_PLAYING)
175 disorder_playing(ds->g->client, &ds->g->playing);
177 disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0);
178 if(need & DC_RECENT) {
179 /* we need to reverse the order of the list */
180 disorder_recent(ds->g->client, &r);
183 r->next = ds->g->recent;
189 disorder_get_volume(ds->g->client,
190 &ds->g->volume_left, &ds->g->volume_right);
191 if(need & (DC_FILES|DC_DIRS)) {
192 if(!(dir = cgi_get("directory")))
194 re = cgi_get("regexp");
196 if(disorder_directories(ds->g->client, dir, re,
197 &ds->g->dirs, &ds->g->ndirs))
200 if(disorder_files(ds->g->client, dir, re,
201 &ds->g->files, &ds->g->nfiles))
204 if(need & DC_RIGHTS) {
205 ds->g->rights = RIGHT_READ; /* fail-safe */
206 if(!disorder_userinfo(ds->g->client, disorder_user(ds->g->client),
208 parse_rights(rights, &ds->g->rights, 1);
210 ds->g->flags |= need;
214 /* actions ********************************************************************/
216 static void act_disable(cgi_sink *output,
219 disorder_disable(ds->g->client);
220 redirect(output->sink);
223 static void act_enable(cgi_sink *output,
226 disorder_enable(ds->g->client);
227 redirect(output->sink);
230 static void act_random_disable(cgi_sink *output,
233 disorder_random_disable(ds->g->client);
234 redirect(output->sink);
237 static void act_random_enable(cgi_sink *output,
240 disorder_random_enable(ds->g->client);
241 redirect(output->sink);
244 static void act_remove(cgi_sink *output,
248 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
250 disorder_remove(ds->g->client, id);
251 redirect(output->sink);
254 static void act_move(cgi_sink *output,
256 const char *id, *delta;
258 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
259 if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument");
261 disorder_move(ds->g->client, id, atoi(delta));
262 redirect(output->sink);
265 static void act_scratch(cgi_sink *output,
268 disorder_scratch(ds->g->client, cgi_get("id"));
269 redirect(output->sink);
272 static void act_playing(cgi_sink *output, dcgi_state *ds) {
274 long refresh = config->refresh, length;
276 int random_enabled = 0;
279 lookups(ds, DC_PLAYING|DC_QUEUE);
280 cgi_header(output->sink, "Content-Type", "text/html");
281 disorder_random_enabled(ds->g->client, &random_enabled);
282 disorder_enabled(ds->g->client, &enabled);
284 && ds->g->playing->state == playing_started /* i.e. not paused */
285 && !disorder_length(ds->g->client, ds->g->playing->track, &length)
287 && ds->g->playing->sofar >= 0) {
288 /* Try to put the next refresh at the start of the next track. */
290 fin = now + length - ds->g->playing->sofar + config->gap;
291 if(now + refresh > fin)
294 if(ds->g->queue && ds->g->queue->state == playing_isscratch) {
295 /* next track is a scratch, don't leave more than the inter-track gap */
296 if(refresh > config->gap)
297 refresh = config->gap;
299 if(!ds->g->playing && ((ds->g->queue
300 && ds->g->queue->state != playing_random)
301 || random_enabled) && enabled) {
302 /* no track playing but playing is enabled and there is something coming
303 * up, must be in a gap */
304 if(refresh > config->gap)
305 refresh = config->gap;
307 byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1,
309 cgi_header(output->sink, "Refresh", r);
310 header_cookie(output->sink);
311 cgi_body(output->sink);
312 expand(output, "playing", ds);
315 static void act_play(cgi_sink *output,
317 const char *track, *dir;
322 if((track = cgi_get("file"))) {
323 disorder_play(ds->g->client, track);
324 } else if((dir = cgi_get("directory"))) {
325 if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0;
327 e = xmalloc(ntracks * sizeof (struct entry));
328 for(n = 0; n < ntracks; ++n) {
329 e[n].path = tracks[n];
330 e[n].sort = trackname_transform("track", tracks[n], "sort");
331 e[n].display = trackname_transform("track", tracks[n], "display");
333 qsort(e, ntracks, sizeof (struct entry), compare_entry);
334 for(n = 0; n < ntracks; ++n)
335 disorder_play(ds->g->client, e[n].path);
338 /* XXX error handling */
339 redirect(output->sink);
342 static int clamp(int n, int min, int max) {
350 static const char *volume_url(void) {
353 byte_xasprintf(&url, "%s?action=volume", config->url);
357 static void act_volume(cgi_sink *output, dcgi_state *ds) {
358 const char *l, *r, *d, *back;
359 int nd, changed = 0;;
361 if((d = cgi_get("delta"))) {
362 lookups(ds, DC_VOLUME);
363 nd = clamp(atoi(d), -255, 255);
364 disorder_set_volume(ds->g->client,
365 clamp(ds->g->volume_left + nd, 0, 255),
366 clamp(ds->g->volume_right + nd, 0, 255));
368 } else if((l = cgi_get("left")) && (r = cgi_get("right"))) {
369 disorder_set_volume(ds->g->client, atoi(l), atoi(r));
373 /* redirect back to ourselves (but without the volume-changing bits in the
375 cgi_header(output->sink, "Location",
376 (back = cgi_get("back")) ? back : volume_url());
377 header_cookie(output->sink);
378 cgi_body(output->sink);
380 cgi_header(output->sink, "Content-Type", "text/html");
381 header_cookie(output->sink);
382 cgi_body(output->sink);
383 expand(output, "volume", ds);
387 static void act_prefs_errors(const char *msg,
388 void attribute((unused)) *u) {
389 fatal(0, "error splitting parts list: %s", msg);
392 static const char *numbered_arg(const char *argname, int numfile) {
395 byte_xasprintf(&fullname, "%d_%s", numfile, argname);
396 return cgi_get(fullname);
399 static void process_prefs(dcgi_state *ds, int numfile) {
400 const char *file, *name, *value, *part, *parts, *current, *context;
403 if(!(file = numbered_arg("file", numfile)))
404 /* The first file doesn't need numbering. */
405 if(numfile > 0 || !(file = cgi_get("file")))
407 if((parts = numbered_arg("parts", numfile))
408 || (parts = cgi_get("parts"))) {
409 /* Default context is display. Other contexts not actually tested. */
410 if(!(context = numbered_arg("context", numfile))) context = "display";
411 partslist = split(parts, 0, 0, act_prefs_errors, 0);
412 while((part = *partslist++)) {
413 if(!(value = numbered_arg(part, numfile)))
415 /* If it's already right (whether regexps or db) don't change anything,
416 * so we don't fill the database up with rubbish. */
417 if(disorder_part(ds->g->client, (char **)¤t,
418 file, context, part))
419 fatal(0, "disorder_part() failed");
420 if(!strcmp(current, value))
422 byte_xasprintf((char **)&name, "trackname_%s_%s", context, part);
423 disorder_set(ds->g->client, file, name, value);
425 if((value = numbered_arg("random", numfile)))
426 disorder_unset(ds->g->client, file, "pick_at_random");
428 disorder_set(ds->g->client, file, "pick_at_random", "0");
429 if((value = numbered_arg("tags", numfile))) {
431 disorder_unset(ds->g->client, file, "tags");
433 disorder_set(ds->g->client, file, "tags", value);
435 if((value = numbered_arg("weight", numfile))) {
436 if(!*value || !strcmp(value, "90000"))
437 disorder_unset(ds->g->client, file, "weight");
439 disorder_set(ds->g->client, file, "weight", value);
441 } else if((name = cgi_get("name"))) {
442 /* Raw preferences. Not well supported in the templates at the moment. */
443 value = cgi_get("value");
445 disorder_set(ds->g->client, file, name, value);
447 disorder_unset(ds->g->client, file, name);
451 static void act_prefs(cgi_sink *output, dcgi_state *ds) {
455 if((files = cgi_get("files"))) nfiles = atoi(files);
457 for(numfile = 0; numfile < nfiles; ++numfile)
458 process_prefs(ds, numfile);
459 cgi_header(output->sink, "Content-Type", "text/html");
460 header_cookie(output->sink);
461 cgi_body(output->sink);
462 expand(output, "prefs", ds);
465 static void act_pause(cgi_sink *output,
468 disorder_pause(ds->g->client);
469 redirect(output->sink);
472 static void act_resume(cgi_sink *output,
475 disorder_resume(ds->g->client);
476 redirect(output->sink);
479 static void act_login(cgi_sink *output,
481 const char *username, *password, *back;
484 username = cgi_get("username");
485 password = cgi_get("password");
486 if(!username || !password
487 || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) {
488 /* We're just visiting the login page */
489 expand_template(ds, output, "login");
492 /* We'll need a new connection as we are going to stop being guest */
494 if(disorder_connect_user(c, username, password)) {
495 cgi_set_option("error", "loginfailed");
496 expand_template(ds, output, "login");
499 if(disorder_make_cookie(c, &login_cookie)) {
500 cgi_set_option("error", "cookiefailed");
501 expand_template(ds, output, "login");
504 /* Use the new connection henceforth */
507 /* We have a new cookie */
508 header_cookie(output->sink);
509 cgi_set_option("status", "loginok");
510 if((back = cgi_get("back")) && *back)
511 /* Redirect back to somewhere or other */
512 redirect(output->sink);
514 /* Stick to the login page */
515 expand_template(ds, output, "login");
518 static void act_logout(cgi_sink *output,
520 disorder_revoke(ds->g->client);
522 /* Reconnect as guest */
523 disorder_cgi_login(ds, output);
524 /* Back to the login page */
525 cgi_set_option("status", "logoutok");
526 expand_template(ds, output, "login");
529 static void act_register(cgi_sink *output,
531 const char *username, *password, *password2, *email;
532 char *confirm, *content_type;
533 const char *text, *encoding, *charset;
535 username = cgi_get("username");
536 password = cgi_get("password1");
537 password2 = cgi_get("password2");
538 email = cgi_get("email");
540 if(!username || !*username) {
541 cgi_set_option("error", "nousername");
542 expand_template(ds, output, "login");
545 if(!password || !*password) {
546 cgi_set_option("error", "nopassword");
547 expand_template(ds, output, "login");
550 if(!password2 || !*password2 || strcmp(password, password2)) {
551 cgi_set_option("error", "passwordmismatch");
552 expand_template(ds, output, "login");
555 if(!email || !*email) {
556 cgi_set_option("error", "noemail");
557 expand_template(ds, output, "login");
560 /* We could well do better address validation but for now we'll just do the
562 if(!strchr(email, '@')) {
563 cgi_set_option("error", "bademail");
564 expand_template(ds, output, "login");
567 if(disorder_register(ds->g->client, username, password, email, &confirm)) {
568 cgi_set_option("error", "cannotregister");
569 expand_template(ds, output, "login");
572 /* Send the user a mail */
573 /* TODO templatize this */
574 byte_xasprintf((char **)&text,
575 "Welcome to DisOrder. To active your login, please visit this URL:\n"
577 "%s?c=%s\n", config->url, urlencodestring(confirm));
578 if(!(text = mime_encode_text(text, &charset, &encoding)))
579 fatal(0, "cannot encode email");
580 byte_xasprintf(&content_type, "text/plain;charset=%s",
581 quote822(charset, 0));
582 sendmail("", config->mail_sender, email, "Welcome to DisOrder",
583 encoding, content_type, text); /* TODO error checking */
584 /* We'll go back to the login page with a suitable message */
585 cgi_set_option("status", "registered");
586 expand_template(ds, output, "login");
589 static void act_confirm(cgi_sink *output,
591 const char *confirmation;
593 if(!(confirmation = cgi_get("c"))) {
594 cgi_set_option("error", "noconfirm");
595 expand_template(ds, output, "login");
597 /* Confirm our registration */
598 if(disorder_confirm(ds->g->client, confirmation)) {
599 cgi_set_option("error", "badconfirm");
600 expand_template(ds, output, "login");
603 if(disorder_make_cookie(ds->g->client, &login_cookie)) {
604 cgi_set_option("error", "cookiefailed");
605 expand_template(ds, output, "login");
608 /* Discard any cached data JIC */
610 /* We have a new cookie */
611 header_cookie(output->sink);
612 cgi_set_option("status", "confirmed");
613 expand_template(ds, output, "login");
616 static void act_edituser(cgi_sink *output,
618 const char *email = cgi_get("email"), *password = cgi_get("changepassword1");
619 const char *password2 = cgi_get("changepassword2");
623 if((password && *password) || (password && *password2)) {
624 if(!password || !password2 || strcmp(password, password2)) {
625 cgi_set_option("error", "passwordmismatch");
626 expand_template(ds, output, "login");
630 password = password2 = 0;
633 if(disorder_edituser(ds->g->client, disorder_user(ds->g->client),
635 cgi_set_option("error", "badedit");
636 expand_template(ds, output, "login");
641 if(disorder_edituser(ds->g->client, disorder_user(ds->g->client),
642 "password", password)) {
643 cgi_set_option("error", "badedit");
644 expand_template(ds, output, "login");
650 login_cookie = 0; /* it'll be invalid now */
651 /* This is a bit duplicative of act_login() */
653 if(disorder_connect_user(c, disorder_user(ds->g->client), password)) {
654 cgi_set_option("error", "loginfailed");
655 expand_template(ds, output, "login");
658 if(disorder_make_cookie(c, &login_cookie)) {
659 cgi_set_option("error", "cookiefailed");
660 expand_template(ds, output, "login");
663 /* Use the new connection henceforth */
666 /* We have a new cookie */
667 header_cookie(output->sink);
669 cgi_set_option("status", "edited");
670 expand_template(ds, output, "login");
673 static void act_reminder(cgi_sink *output,
675 const char *const username = cgi_get("username");
677 if(!username || !*username) {
678 cgi_set_option("error", "nousername");
679 expand_template(ds, output, "login");
682 if(disorder_reminder(ds->g->client, username)) {
683 cgi_set_option("error", "reminderfailed");
684 expand_template(ds, output, "login");
687 cgi_set_option("status", "reminded");
688 expand_template(ds, output, "login");
691 static const struct action {
693 void (*handler)(cgi_sink *output, dcgi_state *ds);
695 { "confirm", act_confirm },
696 { "disable", act_disable },
697 { "edituser", act_edituser },
698 { "enable", act_enable },
699 { "login", act_login },
700 { "logout", act_logout },
701 { "move", act_move },
702 { "pause", act_pause },
703 { "play", act_play },
704 { "playing", act_playing },
705 { "prefs", act_prefs },
706 { "random-disable", act_random_disable },
707 { "random-enable", act_random_enable },
708 { "register", act_register },
709 { "reminder", act_reminder },
710 { "remove", act_remove },
711 { "resume", act_resume },
712 { "scratch", act_scratch },
713 { "volume", act_volume },
716 /* expansions *****************************************************************/
718 static void exp_include(int attribute((unused)) nargs,
722 expand(output, args[0], u);
725 static void exp_server_version(int attribute((unused)) nargs,
726 char attribute((unused)) **args,
733 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
735 v = "(server not running)";
736 cgi_output(output, "%s", v);
739 static void exp_version(int attribute((unused)) nargs,
740 char attribute((unused)) **args,
742 void attribute((unused)) *u) {
743 cgi_output(output, "%s", disorder_short_version_string);
746 static void exp_nonce(int attribute((unused)) nargs,
747 char attribute((unused)) **args,
749 void attribute((unused)) *u) {
750 cgi_output(output, "%s", nonce());
753 static void exp_label(int attribute((unused)) nargs,
756 void attribute((unused)) *u) {
757 cgi_output(output, "%s", cgi_label(args[0]));
760 struct trackinfo_state {
762 const struct queue_entry *q;
767 static void exp_who(int attribute((unused)) nargs,
768 char attribute((unused)) **args,
773 if(ds->track && ds->track->submitter)
774 cgi_output(output, "%s", ds->track->submitter);
777 static void exp_length(int attribute((unused)) nargs,
778 char attribute((unused)) **args,
785 && (ds->track->state == playing_started
786 || ds->track->state == playing_paused)
787 && ds->track->sofar >= 0)
788 cgi_output(output, "%ld:%02ld/",
789 ds->track->sofar / 60, ds->track->sofar % 60);
792 disorder_length(ds->g->client, ds->track->track, &length);
794 disorder_length(ds->g->client, ds->tracks[0], &length);
796 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
798 sink_printf(output->sink, "%s", " ");
801 static void exp_when(int attribute((unused)) nargs,
802 char attribute((unused)) **args,
806 const struct tm *w = 0;
809 switch(ds->track->state) {
810 case playing_isscratch:
811 case playing_unplayed:
813 if(ds->track->expected)
814 w = localtime(&ds->track->expected);
817 case playing_no_player:
819 case playing_scratched:
820 case playing_started:
822 case playing_quitting:
823 if(ds->track->played)
824 w = localtime(&ds->track->played);
828 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
830 sink_printf(output->sink, " ");
833 static void exp_part(int nargs,
838 const char *s, *track, *part, *context;
844 track = ds->track->track;
846 track = ds->tracks[0];
864 if(disorder_part(ds->g->client, (char **)&s, track,
865 !strcmp(context, "short") ? "display" : context, part))
866 fatal(0, "disorder_part() failed");
867 if(!strcmp(context, "short"))
868 s = truncate_for_display(s, config->short_display);
869 cgi_output(output, "%s", s);
871 sink_printf(output->sink, " ");
874 static void exp_playing(int attribute((unused)) nargs,
881 lookups(ds, DC_PLAYING);
882 memset(&s, 0, sizeof s);
885 s.track = ds->g->playing;
886 expandstring(output, args[0], &s);
890 static void exp_queue(int attribute((unused)) nargs,
896 struct queue_entry *q;
898 lookups(ds, DC_QUEUE);
899 memset(&s, 0, sizeof s);
902 for(q = ds->g->queue; q; q = q->next) {
905 expandstring(output, args[0], &s);
911 static void exp_recent(int attribute((unused)) nargs,
917 struct queue_entry *q;
919 lookups(ds, DC_RECENT);
920 memset(&s, 0, sizeof s);
923 for(q = ds->g->recent; q; q = q->next) {
926 expandstring(output, args[0], &s);
932 static void exp_new(int attribute((unused)) nargs,
940 memset(&s, 0, sizeof s);
943 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
944 s.last = s.index + 1 < ds->g->nnew;
945 s.tracks = &ds->g->new[s.index];
946 expandstring(output, args[0], &s);
951 static void exp_url(int attribute((unused)) nargs,
952 char attribute((unused)) **args,
954 void attribute((unused)) *u) {
955 cgi_output(output, "%s", config->url);
963 static int compare_result(const void *a, const void *b) {
964 const struct result *ra = a, *rb = b;
967 if(!(c = strcmp(ra->sort, rb->sort)))
968 c = strcmp(ra->track, rb->track);
972 static void exp_search(int nargs,
976 dcgi_state *ds = u, substate;
978 const char *q, *context, *part, *template;
994 assert(!"should never happen");
995 part = context = template = 0; /* quieten compiler */
997 if(ds->tracks == 0) {
998 /* we are the top level, let's get some search results */
999 if(!(q = cgi_get("query"))) return; /* no results yet */
1000 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
1001 if(!ntracks) return;
1003 tracks = ds->tracks;
1004 ntracks = ds->ntracks;
1006 assert(ntracks != 0);
1007 /* sort tracks by the appropriate part */
1008 r = xmalloc(ntracks * sizeof *r);
1009 for(n = 0; n < ntracks; ++n) {
1010 r[n].track = tracks[n];
1011 if(disorder_part(ds->g->client, (char **)&r[n].sort,
1012 tracks[n], context, part))
1013 fatal(0, "disorder_part() failed");
1015 qsort(r, ntracks, sizeof (struct result), compare_result);
1016 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
1017 * array as we know it's guaranteed to be big enough and isn't going to be
1018 * used for anything else any more. */
1019 memset(&substate, 0, sizeof substate);
1023 while(n < ntracks) {
1024 substate.tracks = tracks;
1025 substate.ntracks = 0;
1028 && !strcmp(r[m].sort, r[n].sort))
1029 tracks[substate.ntracks++] = r[m++].track;
1030 substate.last = (m == ntracks);
1031 expandstring(output, template, &substate);
1036 assert(substate.last != 0);
1039 static void exp_arg(int attribute((unused)) nargs,
1042 void attribute((unused)) *u) {
1045 if((v = cgi_get(args[0])))
1046 cgi_output(output, "%s", v);
1049 static void exp_stats(int attribute((unused)) nargs,
1050 char attribute((unused)) **args,
1056 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
1057 if(!disorder_stats(ds->g->client, &v, 0)) {
1059 cgi_output(output, "%s\n", *v++);
1061 cgi_closetag(output->sink, "pre");
1064 static void exp_volume(int attribute((unused)) nargs,
1070 lookups(ds, DC_VOLUME);
1071 if(!strcmp(args[0], "left"))
1072 cgi_output(output, "%d", ds->g->volume_left);
1074 cgi_output(output, "%d", ds->g->volume_right);
1077 static void exp_shell(int attribute((unused)) nargs,
1080 void attribute((unused)) *u) {
1086 if(!(pid = xfork())) {
1091 execlp("sh", "sh", "-c", args[0], (char *)0);
1092 fatal(errno, "error executing sh");
1095 while((n = read(p[0], buffer, sizeof buffer))) {
1097 if(errno == EINTR) continue;
1098 else fatal(errno, "error reading from pipe");
1100 output->sink->write(output->sink, buffer, n);
1103 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
1105 if(n < 0) fatal(errno, "error calling waitpid");
1107 error(0, "shell command '%s' %s", args[0], wstat(w));
1110 static inline int str2bool(const char *s) {
1111 return !strcmp(s, "true");
1114 static inline const char *bool2str(int n) {
1115 return n ? "true" : "false";
1118 static char *expandarg(const char *arg, dcgi_state *ds) {
1124 output.sink = sink_dynstr(&d);
1125 expandstring(&output, arg, ds);
1126 dynstr_terminate(&d);
1130 static void exp_prefs(int attribute((unused)) nargs,
1135 dcgi_state substate;
1137 const char *file = expandarg(args[0], ds);
1139 memset(&substate, 0, sizeof substate);
1142 if(disorder_prefs(ds->g->client, file, &k)) return;
1144 substate.last = !k->next;
1146 expandstring(output, args[1], &substate);
1153 static void exp_pref(int attribute((unused)) nargs,
1160 if(!disorder_get(ds->g->client, args[0], args[1], &value))
1161 cgi_output(output, "%s", value);
1164 static void exp_if(int nargs,
1169 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
1172 expandstring(output, args[n], ds);
1175 static void exp_and(int nargs,
1182 for(n = 0; n < nargs; ++n)
1183 if(!str2bool(expandarg(args[n], ds))) {
1187 sink_printf(output->sink, "%s", bool2str(result));
1190 static void exp_or(int nargs,
1197 for(n = 0; n < nargs; ++n)
1198 if(str2bool(expandarg(args[n], ds))) {
1202 sink_printf(output->sink, "%s", bool2str(result));
1205 static void exp_not(int attribute((unused)) nargs,
1208 void attribute((unused)) *u) {
1209 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
1212 static void exp_isplaying(int attribute((unused)) nargs,
1213 char attribute((unused)) **args,
1218 lookups(ds, DC_PLAYING);
1219 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
1222 static void exp_isqueue(int attribute((unused)) nargs,
1223 char attribute((unused)) **args,
1228 lookups(ds, DC_QUEUE);
1229 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
1232 static void exp_isrecent(int attribute((unused)) nargs,
1233 char attribute((unused)) **args,
1238 lookups(ds, DC_RECENT);
1239 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
1242 static void exp_isnew(int attribute((unused)) nargs,
1243 char attribute((unused)) **args,
1248 lookups(ds, DC_NEW);
1249 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
1252 static void exp_id(int attribute((unused)) nargs,
1253 char attribute((unused)) **args,
1259 cgi_output(output, "%s", ds->track->id);
1262 static void exp_track(int attribute((unused)) nargs,
1263 char attribute((unused)) **args,
1269 cgi_output(output, "%s", ds->track->track);
1272 static void exp_parity(int attribute((unused)) nargs,
1273 char attribute((unused)) **args,
1278 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
1281 static void exp_comment(int attribute((unused)) nargs,
1282 char attribute((unused)) **args,
1283 cgi_sink attribute((unused)) *output,
1284 void attribute((unused)) *u) {
1288 static void exp_prefname(int attribute((unused)) nargs,
1289 char attribute((unused)) **args,
1294 if(ds->pref && ds->pref->name)
1295 cgi_output(output, "%s", ds->pref->name);
1298 static void exp_prefvalue(int attribute((unused)) nargs,
1299 char attribute((unused)) **args,
1304 if(ds->pref && ds->pref->value)
1305 cgi_output(output, "%s", ds->pref->value);
1308 static void exp_isfiles(int attribute((unused)) nargs,
1309 char attribute((unused)) **args,
1314 lookups(ds, DC_FILES);
1315 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1318 static void exp_isdirectories(int attribute((unused)) nargs,
1319 char attribute((unused)) **args,
1324 lookups(ds, DC_DIRS);
1325 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1328 static void exp_choose(int attribute((unused)) nargs,
1333 dcgi_state substate;
1337 const char *type, *what = expandarg(args[0], ds);
1339 if(!strcmp(what, "files")) {
1340 lookups(ds, DC_FILES);
1341 files = ds->g->files;
1342 nfiles = ds->g->nfiles;
1344 } else if(!strcmp(what, "directories")) {
1345 lookups(ds, DC_DIRS);
1346 files = ds->g->dirs;
1347 nfiles = ds->g->ndirs;
1350 error(0, "unknown @choose@ argument '%s'", what);
1353 e = xmalloc(nfiles * sizeof (struct entry));
1354 for(n = 0; n < nfiles; ++n) {
1355 e[n].path = files[n];
1356 e[n].sort = trackname_transform(type, files[n], "sort");
1357 e[n].display = trackname_transform(type, files[n], "display");
1359 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1360 memset(&substate, 0, sizeof substate);
1363 for(n = 0; n < nfiles; ++n) {
1364 substate.last = (n == nfiles - 1);
1366 substate.entry = &e[n];
1367 expandstring(output, args[1], &substate);
1372 static void exp_file(int attribute((unused)) nargs,
1373 char attribute((unused)) **args,
1379 cgi_output(output, "%s", ds->entry->path);
1381 cgi_output(output, "%s", ds->track->track);
1383 cgi_output(output, "%s", ds->tracks[0]);
1386 static void exp_transform(int nargs,
1389 void attribute((unused)) *u) {
1390 const char *context = nargs > 2 ? args[2] : "display";
1392 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1395 static void exp_urlquote(int attribute((unused)) nargs,
1398 void attribute((unused)) *u) {
1399 cgi_output(output, "%s", urlencodestring(args[0]));
1402 static void exp_scratchable(int attribute((unused)) nargs,
1403 char attribute((unused)) **args,
1405 void attribute((unused)) *u) {
1408 lookups(ds, DC_PLAYING|DC_RIGHTS);
1409 sink_printf(output->sink, "%s",
1410 bool2str(right_scratchable(ds->g->rights,
1411 disorder_user(ds->g->client),
1415 static void exp_removable(int attribute((unused)) nargs,
1416 char attribute((unused)) **args,
1418 void attribute((unused)) *u) {
1421 lookups(ds, DC_RIGHTS);
1422 sink_printf(output->sink, "%s",
1423 bool2str(right_removable(ds->g->rights,
1424 disorder_user(ds->g->client),
1428 static void exp_movable(int attribute((unused)) nargs,
1429 char attribute((unused)) **args,
1431 void attribute((unused)) *u) {
1434 lookups(ds, DC_RIGHTS);
1435 sink_printf(output->sink, "%s",
1436 bool2str(right_movable(ds->g->rights,
1437 disorder_user(ds->g->client),
1441 static void exp_navigate(int attribute((unused)) nargs,
1446 dcgi_state substate;
1447 const char *path = expandarg(args[0], ds);
1452 memset(&substate, 0, sizeof substate);
1454 ptr = path + 1; /* skip root */
1456 substate.nav_path = path;
1459 while(*ptr && *ptr != '/')
1461 substate.last = !*ptr;
1462 substate.nav_len = ptr - path;
1463 substate.nav_dirlen = dirlen;
1464 expandstring(output, args[1], &substate);
1465 dirlen = substate.nav_len;
1472 static void exp_fullname(int attribute((unused)) nargs,
1473 char attribute((unused)) **args,
1477 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1480 static void exp_basename(int nargs,
1488 if((s = strrchr(args[0], '/'))) ++s;
1490 cgi_output(output, "%s", s);
1492 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1493 ds->nav_path + ds->nav_dirlen + 1);
1496 static void exp_dirname(int nargs,
1504 if((s = strrchr(args[0], '/')))
1505 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1507 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1510 static void exp_eq(int attribute((unused)) nargs,
1513 void attribute((unused)) *u) {
1514 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1517 static void exp_ne(int attribute((unused)) nargs,
1520 void attribute((unused)) *u) {
1521 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1524 static void exp_enabled(int attribute((unused)) nargs,
1525 char attribute((unused)) **args,
1532 disorder_enabled(ds->g->client, &enabled);
1533 cgi_output(output, "%s", bool2str(enabled));
1536 static void exp_random_enabled(int attribute((unused)) nargs,
1537 char attribute((unused)) **args,
1544 disorder_random_enabled(ds->g->client, &enabled);
1545 cgi_output(output, "%s", bool2str(enabled));
1548 static void exp_trackstate(int attribute((unused)) nargs,
1553 struct queue_entry *q;
1556 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1557 lookups(ds, DC_QUEUE|DC_PLAYING);
1558 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1559 cgi_output(output, "playing");
1561 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1564 cgi_output(output, "queued");
1568 static void exp_thisurl(int attribute((unused)) nargs,
1569 char attribute((unused)) **args,
1571 void attribute((unused)) *u) {
1572 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1573 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1576 static void exp_isfirst(int attribute((unused)) nargs,
1577 char attribute((unused)) **args,
1582 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1585 static void exp_islast(int attribute((unused)) nargs,
1586 char attribute((unused)) **args,
1591 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1594 static void exp_action(int attribute((unused)) nargs,
1595 char attribute((unused)) **args,
1597 void attribute((unused)) *u) {
1598 const char *action = cgi_get("action"), *mgmt;
1600 if(!action) action = "playing";
1601 if(!strcmp(action, "playing")
1602 && (mgmt = cgi_get("mgmt"))
1603 && !strcmp(mgmt, "true"))
1605 sink_printf(output->sink, "%s", action);
1608 static void exp_resolve(int attribute((unused)) nargs,
1611 void attribute((unused)) *u) {
1615 if(!disorder_resolve(ds->g->client, &track, args[0]))
1616 sink_printf(output->sink, "%s", track);
1619 static void exp_paused(int attribute((unused)) nargs,
1620 char attribute((unused)) **args,
1626 lookups(ds, DC_PLAYING);
1627 if(ds->g->playing && ds->g->playing->state == playing_paused)
1629 cgi_output(output, "%s", bool2str(paused));
1632 static void exp_state(int attribute((unused)) nargs,
1633 char attribute((unused)) **args,
1639 cgi_output(output, "%s", playing_states[ds->track->state]);
1642 static void exp_files(int attribute((unused)) nargs,
1647 dcgi_state substate;
1648 const char *nfiles_arg, *directory;
1649 int nfiles, numfile;
1652 memset(&substate, 0, sizeof substate);
1654 if((directory = cgi_get("directory"))) {
1655 /* Prefs for whole directory. */
1656 lookups(ds, DC_FILES);
1657 /* Synthesize args for the file list. */
1658 nfiles = ds->g->nfiles;
1659 for(numfile = 0; numfile < nfiles; ++numfile) {
1660 k = xmalloc(sizeof *k);
1661 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1662 k->value = ds->g->files[numfile];
1667 /* Args already present. */
1668 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1671 for(numfile = 0; numfile < nfiles; ++numfile) {
1672 substate.index = numfile;
1673 expandstring(output, args[0], &substate);
1677 static void exp_index(int attribute((unused)) nargs,
1678 char attribute((unused)) **args,
1683 cgi_output(output, "%d", ds->index);
1686 static void exp_nfiles(int attribute((unused)) nargs,
1687 char attribute((unused)) **args,
1691 const char *files_arg;
1693 if(cgi_get("directory")) {
1694 lookups(ds, DC_FILES);
1695 cgi_output(output, "%d", ds->g->nfiles);
1696 } else if((files_arg = cgi_get("files")))
1697 cgi_output(output, "%s", files_arg);
1699 cgi_output(output, "1");
1702 static void exp_user(int attribute((unused)) nargs,
1703 char attribute((unused)) **args,
1706 dcgi_state *const ds = u;
1708 cgi_output(output, "%s", disorder_user(ds->g->client));
1711 static void exp_right(int attribute((unused)) nargs,
1715 dcgi_state *const ds = u;
1716 const char *right = expandarg(args[0], ds);
1719 lookups(ds, DC_RIGHTS);
1720 if(parse_rights(right, &r, 1/*report*/))
1723 cgi_output(output, "%s", bool2str(!!(r & ds->g->rights)));
1724 else if(r & ds->g->rights)
1725 expandstring(output, args[1], ds);
1727 expandstring(output, args[2], ds);
1730 static void exp_userinfo(int attribute((unused)) nargs,
1734 dcgi_state *const ds = u;
1737 if(disorder_userinfo(ds->g->client, disorder_user(ds->g->client), args[0],
1740 cgi_output(output, "%s", value);
1743 static void exp_image(int attribute((unused)) nargs,
1746 void attribute((unused)) *u) {
1748 const char *imagestem;
1750 byte_xasprintf(&labelname, "images.%s", args[0]);
1751 if(cgi_label_exists(labelname))
1752 imagestem = cgi_label(labelname);
1753 else if(strchr(args[0], '.'))
1754 imagestem = args[0];
1756 byte_xasprintf((char **)&imagestem, "%s.png", args[0]);
1757 if(cgi_label_exists("url.static"))
1758 cgi_output(output, "%s/%s", cgi_label("url.static"), imagestem);
1760 cgi_output(output, "/disorder/%s", imagestem);
1763 static const struct cgi_expansion expansions[] = {
1764 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1765 { "action", 0, 0, 0, exp_action },
1766 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1767 { "arg", 1, 1, 0, exp_arg },
1768 { "basename", 0, 1, 0, exp_basename },
1769 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1770 { "dirname", 0, 1, 0, exp_dirname },
1771 { "enabled", 0, 0, 0, exp_enabled },
1772 { "eq", 2, 2, 0, exp_eq },
1773 { "file", 0, 0, 0, exp_file },
1774 { "files", 1, 1, EXP_MAGIC, exp_files },
1775 { "fullname", 0, 0, 0, exp_fullname },
1776 { "id", 0, 0, 0, exp_id },
1777 { "if", 2, 3, EXP_MAGIC, exp_if },
1778 { "image", 1, 1, 0, exp_image },
1779 { "include", 1, 1, 0, exp_include },
1780 { "index", 0, 0, 0, exp_index },
1781 { "isdirectories", 0, 0, 0, exp_isdirectories },
1782 { "isfiles", 0, 0, 0, exp_isfiles },
1783 { "isfirst", 0, 0, 0, exp_isfirst },
1784 { "islast", 0, 0, 0, exp_islast },
1785 { "isnew", 0, 0, 0, exp_isnew },
1786 { "isplaying", 0, 0, 0, exp_isplaying },
1787 { "isqueue", 0, 0, 0, exp_isqueue },
1788 { "isrecent", 0, 0, 0, exp_isrecent },
1789 { "label", 1, 1, 0, exp_label },
1790 { "length", 0, 0, 0, exp_length },
1791 { "movable", 0, 0, 0, exp_movable },
1792 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1793 { "ne", 2, 2, 0, exp_ne },
1794 { "new", 1, 1, EXP_MAGIC, exp_new },
1795 { "nfiles", 0, 0, 0, exp_nfiles },
1796 { "nonce", 0, 0, 0, exp_nonce },
1797 { "not", 1, 1, 0, exp_not },
1798 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1799 { "parity", 0, 0, 0, exp_parity },
1800 { "part", 1, 3, 0, exp_part },
1801 { "paused", 0, 0, 0, exp_paused },
1802 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1803 { "pref", 2, 2, 0, exp_pref },
1804 { "prefname", 0, 0, 0, exp_prefname },
1805 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1806 { "prefvalue", 0, 0, 0, exp_prefvalue },
1807 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1808 { "random-enabled", 0, 0, 0, exp_random_enabled },
1809 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1810 { "removable", 0, 0, 0, exp_removable },
1811 { "resolve", 1, 1, 0, exp_resolve },
1812 { "right", 1, 3, EXP_MAGIC, exp_right },
1813 { "scratchable", 0, 0, 0, exp_scratchable },
1814 { "search", 2, 3, EXP_MAGIC, exp_search },
1815 { "server-version", 0, 0, 0, exp_server_version },
1816 { "shell", 1, 1, 0, exp_shell },
1817 { "state", 0, 0, 0, exp_state },
1818 { "stats", 0, 0, 0, exp_stats },
1819 { "thisurl", 0, 0, 0, exp_thisurl },
1820 { "track", 0, 0, 0, exp_track },
1821 { "trackstate", 1, 1, 0, exp_trackstate },
1822 { "transform", 2, 3, 0, exp_transform },
1823 { "url", 0, 0, 0, exp_url },
1824 { "urlquote", 1, 1, 0, exp_urlquote },
1825 { "user", 0, 0, 0, exp_user },
1826 { "userinfo", 1, 1, 0, exp_userinfo },
1827 { "version", 0, 0, 0, exp_version },
1828 { "volume", 1, 1, 0, exp_volume },
1829 { "when", 0, 0, 0, exp_when },
1830 { "who", 0, 0, 0, exp_who }
1833 static void expand(cgi_sink *output,
1834 const char *template,
1836 cgi_expand(template,
1837 expansions, sizeof expansions / sizeof *expansions,
1842 static void expandstring(cgi_sink *output,
1845 cgi_expand_string("",
1847 expansions, sizeof expansions / sizeof *expansions,
1852 static void perform_action(cgi_sink *output, dcgi_state *ds,
1853 const char *action) {
1856 /* We don't ever want anything to be cached */
1857 cgi_header(output->sink, "Cache-Control", "no-cache");
1858 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1859 actions[n].handler(output, ds);
1861 expand_template(ds, output, action);
1864 void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1865 const char *action = cgi_get("action");
1868 /* We allow URLs which are just confirm=... in order to keep confirmation
1869 * URLs, which are user-facing, as short as possible. */
1875 perform_action(output, ds, action);
1878 void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1880 cgi_set_option("error", msg);
1881 perform_action(output, ds, "error");
1884 /** @brief Log in as the current user or guest if none */
1885 void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) {
1886 /* Create a new connection */
1887 ds->g->client = disorder_new(0);
1888 /* Forget everything we knew */
1891 if(disorder_connect_cookie(ds->g->client, login_cookie)) {
1892 disorder_cgi_error(output, ds, "connect");
1895 /* If there was a cookie but it went bad, we forget it */
1896 if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest"))