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>
41 #include "server-cgi.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 /* actions ********************************************************************/
167 static void act_disable(cgi_sink *output,
170 disorder_disable(ds->g->client);
171 redirect(output->sink);
174 static void act_enable(cgi_sink *output,
177 disorder_enable(ds->g->client);
178 redirect(output->sink);
181 static void act_random_disable(cgi_sink *output,
184 disorder_random_disable(ds->g->client);
185 redirect(output->sink);
188 static void act_random_enable(cgi_sink *output,
191 disorder_random_enable(ds->g->client);
192 redirect(output->sink);
195 static void act_remove(cgi_sink *output,
199 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
201 disorder_remove(ds->g->client, id);
202 redirect(output->sink);
205 static void act_move(cgi_sink *output,
207 const char *id, *delta;
209 if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
210 if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument");
212 disorder_move(ds->g->client, id, atoi(delta));
213 redirect(output->sink);
216 static void act_scratch(cgi_sink *output,
219 disorder_scratch(ds->g->client, cgi_get("id"));
220 redirect(output->sink);
223 static void act_playing(cgi_sink *output, dcgi_state *ds) {
225 long refresh = config->refresh, length;
227 int random_enabled = 0;
230 lookups(ds, DC_PLAYING|DC_QUEUE);
231 cgi_header(output->sink, "Content-Type", "text/html");
232 disorder_random_enabled(ds->g->client, &random_enabled);
233 disorder_enabled(ds->g->client, &enabled);
235 && ds->g->playing->state == playing_started /* i.e. not paused */
236 && !disorder_length(ds->g->client, ds->g->playing->track, &length)
238 && ds->g->playing->sofar >= 0) {
239 /* Try to put the next refresh at the start of the next track. */
241 fin = now + length - ds->g->playing->sofar + config->gap;
242 if(now + refresh > fin)
245 if(ds->g->queue && ds->g->queue->state == playing_isscratch) {
246 /* next track is a scratch, don't leave more than the inter-track gap */
247 if(refresh > config->gap)
248 refresh = config->gap;
250 if(!ds->g->playing && ((ds->g->queue
251 && ds->g->queue->state != playing_random)
252 || random_enabled) && enabled) {
253 /* no track playing but playing is enabled and there is something coming
254 * up, must be in a gap */
255 if(refresh > config->gap)
256 refresh = config->gap;
258 byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1,
260 cgi_header(output->sink, "Refresh", r);
261 header_cookie(output->sink);
262 cgi_body(output->sink);
263 expand(output, "playing", ds);
266 static void act_play(cgi_sink *output,
268 const char *track, *dir;
273 if((track = cgi_get("file"))) {
274 disorder_play(ds->g->client, track);
275 } else if((dir = cgi_get("directory"))) {
276 if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0;
278 e = xmalloc(ntracks * sizeof (struct entry));
279 for(n = 0; n < ntracks; ++n) {
280 e[n].path = tracks[n];
281 e[n].sort = trackname_transform("track", tracks[n], "sort");
282 e[n].display = trackname_transform("track", tracks[n], "display");
284 qsort(e, ntracks, sizeof (struct entry), compare_entry);
285 for(n = 0; n < ntracks; ++n)
286 disorder_play(ds->g->client, e[n].path);
289 /* XXX error handling */
290 redirect(output->sink);
293 static int clamp(int n, int min, int max) {
301 static const char *volume_url(void) {
304 byte_xasprintf(&url, "%s?action=volume", config->url);
308 static void act_volume(cgi_sink *output, dcgi_state *ds) {
309 const char *l, *r, *d, *back;
310 int nd, changed = 0;;
312 if((d = cgi_get("delta"))) {
313 lookups(ds, DC_VOLUME);
314 nd = clamp(atoi(d), -255, 255);
315 disorder_set_volume(ds->g->client,
316 clamp(ds->g->volume_left + nd, 0, 255),
317 clamp(ds->g->volume_right + nd, 0, 255));
319 } else if((l = cgi_get("left")) && (r = cgi_get("right"))) {
320 disorder_set_volume(ds->g->client, atoi(l), atoi(r));
324 /* redirect back to ourselves (but without the volume-changing bits in the
326 cgi_header(output->sink, "Location",
327 (back = cgi_get("back")) ? back : volume_url());
328 header_cookie(output->sink);
329 cgi_body(output->sink);
331 cgi_header(output->sink, "Content-Type", "text/html");
332 header_cookie(output->sink);
333 cgi_body(output->sink);
334 expand(output, "volume", ds);
338 static void act_prefs_errors(const char *msg,
339 void attribute((unused)) *u) {
340 fatal(0, "error splitting parts list: %s", msg);
343 static const char *numbered_arg(const char *argname, int numfile) {
346 byte_xasprintf(&fullname, "%d_%s", numfile, argname);
347 return cgi_get(fullname);
350 static void process_prefs(dcgi_state *ds, int numfile) {
351 const char *file, *name, *value, *part, *parts, *current, *context;
354 if(!(file = numbered_arg("file", numfile)))
355 /* The first file doesn't need numbering. */
356 if(numfile > 0 || !(file = cgi_get("file")))
358 if((parts = numbered_arg("parts", numfile))
359 || (parts = cgi_get("parts"))) {
360 /* Default context is display. Other contexts not actually tested. */
361 if(!(context = numbered_arg("context", numfile))) context = "display";
362 partslist = split(parts, 0, 0, act_prefs_errors, 0);
363 while((part = *partslist++)) {
364 if(!(value = numbered_arg(part, numfile)))
366 /* If it's already right (whether regexps or db) don't change anything,
367 * so we don't fill the database up with rubbish. */
368 if(disorder_part(ds->g->client, (char **)¤t,
369 file, context, part))
370 fatal(0, "disorder_part() failed");
371 if(!strcmp(current, value))
373 byte_xasprintf((char **)&name, "trackname_%s_%s", context, part);
374 disorder_set(ds->g->client, file, name, value);
376 if((value = numbered_arg("random", numfile)))
377 disorder_unset(ds->g->client, file, "pick_at_random");
379 disorder_set(ds->g->client, file, "pick_at_random", "0");
380 if((value = numbered_arg("tags", numfile))) {
382 disorder_unset(ds->g->client, file, "tags");
384 disorder_set(ds->g->client, file, "tags", value);
386 if((value = numbered_arg("weight", numfile))) {
387 if(!*value || !strcmp(value, "90000"))
388 disorder_unset(ds->g->client, file, "weight");
390 disorder_set(ds->g->client, file, "weight", value);
392 } else if((name = cgi_get("name"))) {
393 /* Raw preferences. Not well supported in the templates at the moment. */
394 value = cgi_get("value");
396 disorder_set(ds->g->client, file, name, value);
398 disorder_unset(ds->g->client, file, name);
402 static void act_prefs(cgi_sink *output, dcgi_state *ds) {
406 if((files = cgi_get("files"))) nfiles = atoi(files);
408 for(numfile = 0; numfile < nfiles; ++numfile)
409 process_prefs(ds, numfile);
410 cgi_header(output->sink, "Content-Type", "text/html");
411 header_cookie(output->sink);
412 cgi_body(output->sink);
413 expand(output, "prefs", ds);
416 static void act_pause(cgi_sink *output,
419 disorder_pause(ds->g->client);
420 redirect(output->sink);
423 static void act_resume(cgi_sink *output,
426 disorder_resume(ds->g->client);
427 redirect(output->sink);
430 static void act_login(cgi_sink *output,
432 const char *username, *password, *back;
435 username = cgi_get("username");
436 password = cgi_get("password");
437 if(!username || !password
438 || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) {
439 /* We're just visiting the login page */
440 expand_template(ds, output, "login");
443 /* We'll need a new connection as we are going to stop being guest */
445 if(disorder_connect_user(c, username, password)) {
446 cgi_set_option("error", "loginfailed");
447 expand_template(ds, output, "login");
450 if(disorder_make_cookie(c, &login_cookie)) {
451 cgi_set_option("error", "cookiefailed");
452 expand_template(ds, output, "login");
455 /* Use the new connection henceforth */
458 /* We have a new cookie */
459 header_cookie(output->sink);
460 cgi_set_option("status", "loginok");
461 if((back = cgi_get("back")) && *back)
462 /* Redirect back to somewhere or other */
463 redirect(output->sink);
465 /* Stick to the login page */
466 expand_template(ds, output, "login");
469 static void act_logout(cgi_sink *output,
471 disorder_revoke(ds->g->client);
473 /* Reconnect as guest */
474 disorder_cgi_login(ds, output);
475 /* Back to the login page */
476 cgi_set_option("status", "logoutok");
477 expand_template(ds, output, "login");
480 static void act_register(cgi_sink *output,
482 const char *username, *password, *password2, *email;
483 char *confirm, *content_type;
484 const char *text, *encoding, *charset;
486 username = cgi_get("username");
487 password = cgi_get("password1");
488 password2 = cgi_get("password2");
489 email = cgi_get("email");
491 if(!username || !*username) {
492 cgi_set_option("error", "nousername");
493 expand_template(ds, output, "login");
496 if(!password || !*password) {
497 cgi_set_option("error", "nopassword");
498 expand_template(ds, output, "login");
501 if(!password2 || !*password2 || strcmp(password, password2)) {
502 cgi_set_option("error", "passwordmismatch");
503 expand_template(ds, output, "login");
506 if(!email || !*email) {
507 cgi_set_option("error", "noemail");
508 expand_template(ds, output, "login");
511 /* We could well do better address validation but for now we'll just do the
513 if(!strchr(email, '@')) {
514 cgi_set_option("error", "bademail");
515 expand_template(ds, output, "login");
518 if(disorder_register(ds->g->client, username, password, email, &confirm)) {
519 cgi_set_option("error", "cannotregister");
520 expand_template(ds, output, "login");
523 /* Send the user a mail */
524 /* TODO templatize this */
525 byte_xasprintf((char **)&text,
526 "Welcome to DisOrder. To active your login, please visit this URL:\n"
528 "%s?c=%s\n", config->url, urlencodestring(confirm));
529 if(!(text = mime_encode_text(text, &charset, &encoding)))
530 fatal(0, "cannot encode email");
531 byte_xasprintf(&content_type, "text/plain;charset=%s",
532 quote822(charset, 0));
533 sendmail("", config->mail_sender, email, "Welcome to DisOrder",
534 encoding, content_type, text); /* TODO error checking */
535 /* We'll go back to the login page with a suitable message */
536 cgi_set_option("status", "registered");
537 expand_template(ds, output, "login");
540 static void act_confirm(cgi_sink *output,
542 const char *confirmation;
544 if(!(confirmation = cgi_get("c"))) {
545 cgi_set_option("error", "noconfirm");
546 expand_template(ds, output, "login");
548 /* Confirm our registration */
549 if(disorder_confirm(ds->g->client, confirmation)) {
550 cgi_set_option("error", "badconfirm");
551 expand_template(ds, output, "login");
554 if(disorder_make_cookie(ds->g->client, &login_cookie)) {
555 cgi_set_option("error", "cookiefailed");
556 expand_template(ds, output, "login");
559 /* Discard any cached data JIC */
561 /* We have a new cookie */
562 header_cookie(output->sink);
563 cgi_set_option("status", "confirmed");
564 expand_template(ds, output, "login");
567 static void act_edituser(cgi_sink *output,
569 const char *email = cgi_get("email"), *password = cgi_get("changepassword1");
570 const char *password2 = cgi_get("changepassword2");
574 if((password && *password) || (password && *password2)) {
575 if(!password || !password2 || strcmp(password, password2)) {
576 cgi_set_option("error", "passwordmismatch");
577 expand_template(ds, output, "login");
581 password = password2 = 0;
584 if(disorder_edituser(ds->g->client, disorder_user(ds->g->client),
586 cgi_set_option("error", "badedit");
587 expand_template(ds, output, "login");
592 if(disorder_edituser(ds->g->client, disorder_user(ds->g->client),
593 "password", password)) {
594 cgi_set_option("error", "badedit");
595 expand_template(ds, output, "login");
601 login_cookie = 0; /* it'll be invalid now */
602 /* This is a bit duplicative of act_login() */
604 if(disorder_connect_user(c, disorder_user(ds->g->client), password)) {
605 cgi_set_option("error", "loginfailed");
606 expand_template(ds, output, "login");
609 if(disorder_make_cookie(c, &login_cookie)) {
610 cgi_set_option("error", "cookiefailed");
611 expand_template(ds, output, "login");
614 /* Use the new connection henceforth */
617 /* We have a new cookie */
618 header_cookie(output->sink);
620 cgi_set_option("status", "edited");
621 expand_template(ds, output, "login");
624 static void act_reminder(cgi_sink *output,
626 const char *const username = cgi_get("username");
628 if(!username || !*username) {
629 cgi_set_option("error", "nousername");
630 expand_template(ds, output, "login");
633 if(disorder_reminder(ds->g->client, username)) {
634 cgi_set_option("error", "reminderfailed");
635 expand_template(ds, output, "login");
638 cgi_set_option("status", "reminded");
639 expand_template(ds, output, "login");
642 static const struct action {
644 void (*handler)(cgi_sink *output, dcgi_state *ds);
646 { "confirm", act_confirm },
647 { "disable", act_disable },
648 { "edituser", act_edituser },
649 { "enable", act_enable },
650 { "login", act_login },
651 { "logout", act_logout },
652 { "move", act_move },
653 { "pause", act_pause },
654 { "play", act_play },
655 { "playing", act_playing },
656 { "prefs", act_prefs },
657 { "random-disable", act_random_disable },
658 { "random-enable", act_random_enable },
659 { "register", act_register },
660 { "reminder", act_reminder },
661 { "remove", act_remove },
662 { "resume", act_resume },
663 { "scratch", act_scratch },
664 { "volume", act_volume },
667 /* expansions *****************************************************************/
669 static void exp_label(int attribute((unused)) nargs,
672 void attribute((unused)) *u) {
673 cgi_output(output, "%s", cgi_label(args[0]));
676 struct trackinfo_state {
678 const struct queue_entry *q;
688 static int compare_result(const void *a, const void *b) {
689 const struct result *ra = a, *rb = b;
692 if(!(c = strcmp(ra->sort, rb->sort)))
693 c = strcmp(ra->track, rb->track);
697 static void exp_search(int nargs,
701 dcgi_state *ds = u, substate;
703 const char *q, *context, *part, *template;
719 assert(!"should never happen");
720 part = context = template = 0; /* quieten compiler */
722 if(ds->tracks == 0) {
723 /* we are the top level, let's get some search results */
724 if(!(q = cgi_get("query"))) return; /* no results yet */
725 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
729 ntracks = ds->ntracks;
731 assert(ntracks != 0);
732 /* sort tracks by the appropriate part */
733 r = xmalloc(ntracks * sizeof *r);
734 for(n = 0; n < ntracks; ++n) {
735 r[n].track = tracks[n];
736 if(disorder_part(ds->g->client, (char **)&r[n].sort,
737 tracks[n], context, part))
738 fatal(0, "disorder_part() failed");
740 qsort(r, ntracks, sizeof (struct result), compare_result);
741 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
742 * array as we know it's guaranteed to be big enough and isn't going to be
743 * used for anything else any more. */
744 memset(&substate, 0, sizeof substate);
749 substate.tracks = tracks;
750 substate.ntracks = 0;
753 && !strcmp(r[m].sort, r[n].sort))
754 tracks[substate.ntracks++] = r[m++].track;
755 substate.last = (m == ntracks);
756 expandstring(output, template, &substate);
761 assert(substate.last != 0);
764 static void exp_stats(int attribute((unused)) nargs,
765 char attribute((unused)) **args,
771 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
772 if(!disorder_stats(ds->g->client, &v, 0)) {
774 cgi_output(output, "%s\n", *v++);
776 cgi_closetag(output->sink, "pre");
779 static char *expandarg(const char *arg, dcgi_state *ds) {
785 output.sink = sink_dynstr(&d);
786 expandstring(&output, arg, ds);
787 dynstr_terminate(&d);
791 static void exp_isfiles(int attribute((unused)) nargs,
792 char attribute((unused)) **args,
797 lookups(ds, DC_FILES);
798 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
801 static void exp_isdirectories(int attribute((unused)) nargs,
802 char attribute((unused)) **args,
807 lookups(ds, DC_DIRS);
808 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
811 static void exp_choose(int attribute((unused)) nargs,
820 const char *type, *what = expandarg(args[0], ds);
822 if(!strcmp(what, "files")) {
823 lookups(ds, DC_FILES);
824 files = ds->g->files;
825 nfiles = ds->g->nfiles;
827 } else if(!strcmp(what, "directories")) {
828 lookups(ds, DC_DIRS);
830 nfiles = ds->g->ndirs;
833 error(0, "unknown @choose@ argument '%s'", what);
836 e = xmalloc(nfiles * sizeof (struct entry));
837 for(n = 0; n < nfiles; ++n) {
838 e[n].path = files[n];
839 e[n].sort = trackname_transform(type, files[n], "sort");
840 e[n].display = trackname_transform(type, files[n], "display");
842 qsort(e, nfiles, sizeof (struct entry), compare_entry);
843 memset(&substate, 0, sizeof substate);
846 for(n = 0; n < nfiles; ++n) {
847 substate.last = (n == nfiles - 1);
849 substate.entry = &e[n];
850 expandstring(output, args[1], &substate);
855 static void exp_file(int attribute((unused)) nargs,
856 char attribute((unused)) **args,
862 cgi_output(output, "%s", ds->entry->path);
864 cgi_output(output, "%s", ds->track->track);
866 cgi_output(output, "%s", ds->tracks[0]);
869 static void exp_navigate(int attribute((unused)) nargs,
875 const char *path = expandarg(args[0], ds);
880 memset(&substate, 0, sizeof substate);
882 ptr = path + 1; /* skip root */
884 substate.nav_path = path;
887 while(*ptr && *ptr != '/')
889 substate.last = !*ptr;
890 substate.nav_len = ptr - path;
891 substate.nav_dirlen = dirlen;
892 expandstring(output, args[1], &substate);
893 dirlen = substate.nav_len;
900 static void exp_fullname(int attribute((unused)) nargs,
901 char attribute((unused)) **args,
905 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
908 static void exp_basename(int nargs,
916 if((s = strrchr(args[0], '/'))) ++s;
918 cgi_output(output, "%s", s);
920 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
921 ds->nav_path + ds->nav_dirlen + 1);
924 static void exp_dirname(int nargs,
932 if((s = strrchr(args[0], '/')))
933 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
935 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
938 static void exp_files(int attribute((unused)) nargs,
944 const char *nfiles_arg, *directory;
948 memset(&substate, 0, sizeof substate);
950 if((directory = cgi_get("directory"))) {
951 /* Prefs for whole directory. */
952 lookups(ds, DC_FILES);
953 /* Synthesize args for the file list. */
954 nfiles = ds->g->nfiles;
955 for(numfile = 0; numfile < nfiles; ++numfile) {
956 k = xmalloc(sizeof *k);
957 byte_xasprintf((char **)&k->name, "%d_file", numfile);
958 k->value = ds->g->files[numfile];
963 /* Args already present. */
964 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
967 for(numfile = 0; numfile < nfiles; ++numfile) {
968 substate.index = numfile;
969 expandstring(output, args[0], &substate);
973 static void exp_nfiles(int attribute((unused)) nargs,
974 char attribute((unused)) **args,
978 const char *files_arg;
980 if(cgi_get("directory")) {
981 lookups(ds, DC_FILES);
982 cgi_output(output, "%d", ds->g->nfiles);
983 } else if((files_arg = cgi_get("files")))
984 cgi_output(output, "%s", files_arg);
986 cgi_output(output, "1");
989 static void exp_image(int attribute((unused)) nargs,
992 void attribute((unused)) *u) {
994 const char *imagestem;
996 byte_xasprintf(&labelname, "images.%s", args[0]);
997 if(cgi_label_exists(labelname))
998 imagestem = cgi_label(labelname);
999 else if(strchr(args[0], '.'))
1000 imagestem = args[0];
1002 byte_xasprintf((char **)&imagestem, "%s.png", args[0]);
1003 if(cgi_label_exists("url.static"))
1004 cgi_output(output, "%s/%s", cgi_label("url.static"), imagestem);
1006 cgi_output(output, "/disorder/%s", imagestem);
1009 static const struct cgi_expansion expansions[] = {
1010 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1011 { "action", 0, 0, 0, exp_action },
1012 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1013 { "arg", 1, 1, 0, exp_arg },
1014 { "basename", 0, 1, 0, exp_basename },
1015 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1016 { "define", 3, 3, EXP_MAGIC, exp_define },
1017 { "dirname", 0, 1, 0, exp_dirname },
1018 { "enabled", 0, 0, 0, exp_enabled },
1019 { "eq", 2, 2, 0, exp_eq },
1020 { "file", 0, 0, 0, exp_file },
1021 { "files", 1, 1, EXP_MAGIC, exp_files },
1022 { "fullname", 0, 0, 0, exp_fullname },
1023 { "id", 0, 0, 0, exp_id },
1024 { "if", 2, 3, EXP_MAGIC, exp_if },
1025 { "image", 1, 1, 0, exp_image },
1026 { "include", 1, 1, 0, exp_include },
1027 { "index", 0, 0, 0, exp_index },
1028 { "isdirectories", 0, 0, 0, exp_isdirectories },
1029 { "isfiles", 0, 0, 0, exp_isfiles },
1030 { "isfirst", 0, 0, 0, exp_isfirst },
1031 { "islast", 0, 0, 0, exp_islast },
1032 { "isnew", 0, 0, 0, exp_isnew },
1033 { "isplaying", 0, 0, 0, exp_isplaying },
1034 { "isqueue", 0, 0, 0, exp_isqueue },
1035 { "isrecent", 0, 0, 0, exp_isrecent },
1036 { "label", 1, 1, 0, exp_label },
1037 { "length", 0, 0, 0, exp_length },
1038 { "movable", 0, 0, 0, exp_movable },
1039 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1040 { "ne", 2, 2, 0, exp_ne },
1041 { "new", 1, 1, EXP_MAGIC, exp_new },
1042 { "nfiles", 0, 0, 0, exp_nfiles },
1043 { "nonce", 0, 0, 0, exp_nonce },
1044 { "not", 1, 1, 0, exp_not },
1045 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1046 { "parity", 0, 0, 0, exp_parity },
1047 { "part", 1, 3, 0, exp_part },
1048 { "paused", 0, 0, 0, exp_paused },
1049 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1050 { "pref", 2, 2, 0, exp_pref },
1051 { "prefname", 0, 0, 0, exp_prefname },
1052 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1053 { "prefvalue", 0, 0, 0, exp_prefvalue },
1054 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1055 { "random-enabled", 0, 0, 0, exp_random_enabled },
1056 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1057 { "removable", 0, 0, 0, exp_removable },
1058 { "resolve", 1, 1, 0, exp_resolve },
1059 { "right", 1, 3, EXP_MAGIC, exp_right },
1060 { "scratchable", 0, 0, 0, exp_scratchable },
1061 { "search", 2, 3, EXP_MAGIC, exp_search },
1062 { "server-version", 0, 0, 0, exp_server_version },
1063 { "shell", 1, 1, 0, exp_shell },
1064 { "state", 0, 0, 0, exp_state },
1065 { "stats", 0, 0, 0, exp_stats },
1066 { "thisurl", 0, 0, 0, exp_thisurl },
1067 { "track", 0, 0, 0, exp_track },
1068 { "trackstate", 1, 1, 0, exp_trackstate },
1069 { "transform", 2, 3, 0, exp_transform },
1070 { "url", 0, 0, 0, exp_url },
1071 { "urlquote", 1, 1, 0, exp_urlquote },
1072 { "user", 0, 0, 0, exp_user },
1073 { "userinfo", 1, 1, 0, exp_userinfo },
1074 { "version", 0, 0, 0, exp_version },
1075 { "volume", 1, 1, 0, exp_volume },
1076 { "when", 0, 0, 0, exp_when },
1077 { "who", 0, 0, 0, exp_who }
1080 static void expand(cgi_sink *output,
1081 const char *template,
1083 cgi_expand(template,
1084 expansions, sizeof expansions / sizeof *expansions,
1089 static void expandstring(cgi_sink *output,
1092 cgi_expand_string("",
1094 expansions, sizeof expansions / sizeof *expansions,
1099 static void perform_action(cgi_sink *output, dcgi_state *ds,
1100 const char *action) {
1103 /* We don't ever want anything to be cached */
1104 cgi_header(output->sink, "Cache-Control", "no-cache");
1105 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1106 actions[n].handler(output, ds);
1108 expand_template(ds, output, action);
1111 void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1112 const char *action = cgi_get("action");
1115 /* We allow URLs which are just confirm=... in order to keep confirmation
1116 * URLs, which are user-facing, as short as possible. */
1122 perform_action(output, ds, action);
1125 void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1127 cgi_set_option("error", msg);
1128 perform_action(output, ds, "error");
1131 /** @brief Log in as the current user or guest if none */
1132 void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) {
1133 /* Create a new connection */
1134 ds->g->client = disorder_new(0);
1135 /* Forget everything we knew */
1138 if(disorder_connect_cookie(ds->g->client, login_cookie)) {
1139 disorder_cgi_error(output, ds, "connect");
1142 /* If there was a cookie but it went bad, we forget it */
1143 if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest"))