chiark / gitweb /
split out dcgi_get_cookie
[disorder] / server / dcgi.c
index 07e1825e705f409e9cb1f211c856a010c9725219..1ba638fe6a0db2e7a0e502b900986226798ac656 100644 (file)
@@ -1,25 +1,3 @@
-/*
- * This file is part of DisOrder.
- * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful, but
- * WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
- * USA
- */
-
-#include <config.h>
-#include "types.h"
 
 #include <stdio.h>
 #include <errno.h>
@@ -38,8 +16,7 @@
 #include "mem.h"
 #include "vector.h"
 #include "sink.h"
-#include "cgi.h"
-#include "dcgi.h"
+#include "server-cgi.h"
 #include "log.h"
 #include "configuration.h"
 #include "table.h"
 #include "defs.h"
 #include "trackname.h"
 #include "charset.h"
-
-static void expand(cgi_sink *output,
-                  const char *template,
-                  dcgi_state *ds);
-static void expandstring(cgi_sink *output,
-                        const char *string,
-                        dcgi_state *ds);
+#include "dcgi.h"
+#include "url.h"
+#include "mime.h"
+#include "sendmail.h"
+#include "base64.h"
 
 struct entry {
   const char *path;
@@ -68,17 +43,6 @@ struct entry {
   const char *display;
 };
 
-static const char *nonce(void) {
-  static unsigned long count;
-  char *s;
-
-  byte_xasprintf(&s, "%lx%lx%lx",
-          (unsigned long)time(0),
-          (unsigned long)getpid(),
-          count++);
-  return s;
-}
-
 static int compare_entry(const void *a, const void *b) {
   const struct entry *ea = a, *eb = b;
 
@@ -102,51 +66,18 @@ static const char *front_url(void) {
 static void redirect(struct sink *output) {
   const char *back;
 
-  cgi_header(output, "Location",
-            (back = cgi_get("back")) ? back : front_url());
+  back = cgi_get("back");
+  cgi_header(output, "Location", back && *back ? back : front_url());
+  header_cookie(output);
   cgi_body(output);
 }
 
-static void lookups(dcgi_state *ds, unsigned want) {
-  unsigned need;
-  struct queue_entry *r, *rnext;
-  const char *dir, *re;
-
-  if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) {
-    if(need & DC_QUEUE)
-      disorder_queue(ds->g->client, &ds->g->queue);
-    if(need & DC_PLAYING)
-      disorder_playing(ds->g->client, &ds->g->playing);
-    if(need & DC_NEW)
-      disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0);
-    if(need & DC_RECENT) {
-      /* we need to reverse the order of the list */
-      disorder_recent(ds->g->client, &r);
-      while(r) {
-       rnext = r->next;
-       r->next = ds->g->recent;
-       ds->g->recent = r;
-       r = rnext;
-      }
-    }
-    if(need & DC_VOLUME)
-      disorder_get_volume(ds->g->client,
-                         &ds->g->volume_left, &ds->g->volume_right);
-    if(need & (DC_FILES|DC_DIRS)) {
-      if(!(dir = cgi_get("directory")))
-       dir = "";
-      re = cgi_get("regexp");
-      if(need & DC_DIRS)
-       if(disorder_directories(ds->g->client, dir, re,
-                               &ds->g->dirs, &ds->g->ndirs))
-         ds->g->ndirs = 0;
-      if(need & DC_FILES)
-       if(disorder_files(ds->g->client, dir, re,
-                         &ds->g->files, &ds->g->nfiles))
-         ds->g->nfiles = 0;
-    }
-    ds->g->flags |= need;
-  }
+static void expand_template(dcgi_state *ds, cgi_sink *output,
+                           const char *action) {
+  cgi_header(output->sink, "Content-Type", "text/html");
+  header_cookie(output->sink);
+  cgi_body(output->sink);
+  expand(output, action, ds);
 }
 
 /* actions ********************************************************************/
@@ -207,48 +138,6 @@ static void act_scratch(cgi_sink *output,
   redirect(output->sink);
 }
 
-static void act_playing(cgi_sink *output, dcgi_state *ds) {
-  char r[1024];
-  long refresh = config->refresh, length;
-  time_t now, fin;
-  int random_enabled = 0;
-  int enabled = 0;
-
-  lookups(ds, DC_PLAYING|DC_QUEUE);
-  cgi_header(output->sink, "Content-Type", "text/html");
-  disorder_random_enabled(ds->g->client, &random_enabled);
-  disorder_enabled(ds->g->client, &enabled);
-  if(ds->g->playing
-     && ds->g->playing->state == playing_started /* i.e. not paused */
-     && !disorder_length(ds->g->client, ds->g->playing->track, &length)
-     && length
-     && ds->g->playing->sofar >= 0) {
-    /* Try to put the next refresh at the start of the next track. */
-    time(&now);
-    fin = now + length - ds->g->playing->sofar + config->gap;
-    if(now + refresh > fin)
-      refresh = fin - now;
-  }
-  if(ds->g->queue && ds->g->queue->state == playing_isscratch) {
-    /* next track is a scratch, don't leave more than the inter-track gap */
-    if(refresh > config->gap)
-      refresh = config->gap;
-  }
-  if(!ds->g->playing && ((ds->g->queue
-                         && ds->g->queue->state != playing_random)
-                        || random_enabled) && enabled) {
-    /* no track playing but playing is enabled and there is something coming
-     * up, must be in a gap */
-    if(refresh > config->gap)
-      refresh = config->gap;
-  }
-  byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1,
-               front_url());
-  cgi_header(output->sink, "Refresh", r);
-  cgi_body(output->sink);
-  expand(output, "playing", ds);
-}
-
 static void act_play(cgi_sink *output,
                     dcgi_state *ds) {
   const char *track, *dir;
@@ -311,9 +200,11 @@ static void act_volume(cgi_sink *output, dcgi_state *ds) {
      * URL) */
     cgi_header(output->sink, "Location",
               (back = cgi_get("back")) ? back : volume_url());
+    header_cookie(output->sink);
     cgi_body(output->sink);
   } else {
     cgi_header(output->sink, "Content-Type", "text/html");
+    header_cookie(output->sink);
     cgi_body(output->sink);
     expand(output, "volume", ds);
   }
@@ -361,8 +252,18 @@ static void process_prefs(dcgi_state *ds, int numfile) {
       disorder_unset(ds->g->client, file, "pick_at_random");
     else
       disorder_set(ds->g->client, file, "pick_at_random", "0");
-    if((value = numbered_arg("tags", numfile)))
-      disorder_set(ds->g->client, file, "tags", value);
+    if((value = numbered_arg("tags", numfile))) {
+      if(!*value)
+       disorder_unset(ds->g->client, file, "tags");
+      else
+       disorder_set(ds->g->client, file, "tags", value);
+    }
+    if((value = numbered_arg("weight", numfile))) {
+      if(!*value || !strcmp(value, "90000"))
+       disorder_unset(ds->g->client, file, "weight");
+      else
+       disorder_set(ds->g->client, file, "weight", value);
+    }
   } else if((name = cgi_get("name"))) {
     /* Raw preferences.  Not well supported in the templates at the moment. */
     value = cgi_get("value");
@@ -382,6 +283,7 @@ static void act_prefs(cgi_sink *output, dcgi_state *ds) {
   for(numfile = 0; numfile < nfiles; ++numfile)
     process_prefs(ds, numfile);
   cgi_header(output->sink, "Content-Type", "text/html");
+  header_cookie(output->sink);
   cgi_body(output->sink);
   expand(output, "prefs", ds);
 }
@@ -400,62 +302,220 @@ static void act_resume(cgi_sink *output,
   redirect(output->sink);
 }
 
-static const struct action {
-  const char *name;
-  void (*handler)(cgi_sink *output, dcgi_state *ds);
-} actions[] = {
-  { "disable", act_disable },
-  { "enable", act_enable },
-  { "move", act_move },
-  { "pause", act_pause },
-  { "play", act_play },
-  { "playing", act_playing },
-  { "prefs", act_prefs },
-  { "random-disable", act_random_disable },
-  { "random-enable", act_random_enable },
-  { "remove", act_remove },
-  { "resume", act_resume },
-  { "scratch", act_scratch },
-  { "volume", act_volume },
-};
-
-/* expansions *****************************************************************/
+static void act_login(cgi_sink *output,
+                     dcgi_state *ds) {
+  const char *username, *password, *back;
+  disorder_client *c;
+
+  username = cgi_get("username");
+  password = cgi_get("password");
+  if(!username || !password
+     || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) {
+    /* We're just visiting the login page */
+    expand_template(ds, output, "login");
+    return;
+  }
+  /* We'll need a new connection as we are going to stop being guest */
+  c = disorder_new(0);
+  if(disorder_connect_user(c, username, password)) {
+    cgi_set_option("error", "loginfailed");
+    expand_template(ds, output, "login");
+    return;
+  }
+  if(disorder_make_cookie(c, &login_cookie)) {
+    cgi_set_option("error", "cookiefailed");
+    expand_template(ds, output, "login");
+    return;
+  }
+  /* Use the new connection henceforth */
+  ds->g->client = c;
+  ds->g->flags = 0;
+  /* We have a new cookie */
+  header_cookie(output->sink);
+  cgi_set_option("status", "loginok");
+  if((back = cgi_get("back")) && *back)
+    /* Redirect back to somewhere or other */
+    redirect(output->sink);
+  else
+    /* Stick to the login page */
+    expand_template(ds, output, "login");
+}
 
-static void exp_include(int attribute((unused)) nargs,
-                       char **args,
-                       cgi_sink *output,
-                       void *u) {
-  expand(output, args[0], u);
+static void act_logout(cgi_sink *output,
+                      dcgi_state *ds) {
+  disorder_revoke(ds->g->client);
+  login_cookie = 0;
+  /* Reconnect as guest */
+  disorder_cgi_login(ds, output);
+  /* Back to the login page */
+  cgi_set_option("status", "logoutok");
+  expand_template(ds, output, "login");
 }
 
-static void exp_server_version(int attribute((unused)) nargs,
-                              char attribute((unused)) **args,
-                              cgi_sink *output,
-                              void *u) {
-  dcgi_state *ds = u;
-  const char *v;
+static void act_register(cgi_sink *output,
+                        dcgi_state *ds) {
+  const char *username, *password, *password2, *email;
+  char *confirm, *content_type;
+  const char *text, *encoding, *charset;
+
+  username = cgi_get("username");
+  password = cgi_get("password1");
+  password2 = cgi_get("password2");
+  email = cgi_get("email");
+
+  if(!username || !*username) {
+    cgi_set_option("error", "nousername");
+    expand_template(ds, output, "login");
+    return;
+  }
+  if(!password || !*password) {
+    cgi_set_option("error", "nopassword");
+    expand_template(ds, output, "login");
+    return;
+  }
+  if(!password2 || !*password2 || strcmp(password, password2)) {
+    cgi_set_option("error", "passwordmismatch");
+    expand_template(ds, output, "login");
+    return;
+  }
+  if(!email || !*email) {
+    cgi_set_option("error", "noemail");
+    expand_template(ds, output, "login");
+    return;
+  }
+  /* We could well do better address validation but for now we'll just do the
+   * minimum */
+  if(!strchr(email, '@')) {
+    cgi_set_option("error", "bademail");
+    expand_template(ds, output, "login");
+    return;
+  }
+  if(disorder_register(ds->g->client, username, password, email, &confirm)) {
+    cgi_set_option("error", "cannotregister");
+    expand_template(ds, output, "login");
+    return;
+  }
+  /* Send the user a mail */
+  /* TODO templatize this */
+  byte_xasprintf((char **)&text,
+                "Welcome to DisOrder.  To active your login, please visit this URL:\n"
+                "\n"
+                "%s?c=%s\n", config->url, urlencodestring(confirm));
+  if(!(text = mime_encode_text(text, &charset, &encoding)))
+    fatal(0, "cannot encode email");
+  byte_xasprintf(&content_type, "text/plain;charset=%s",
+                quote822(charset, 0));
+  sendmail("", config->mail_sender, email, "Welcome to DisOrder",
+          encoding, content_type, text); /* TODO error checking  */
+  /* We'll go back to the login page with a suitable message */
+  cgi_set_option("status", "registered");
+  expand_template(ds, output, "login");
+}
+
+static void act_confirm(cgi_sink *output,
+                       dcgi_state *ds) {
+  const char *confirmation;
 
-  if(ds->g->client) {
-    if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
-  } else
-    v = "(server not running)";
-  cgi_output(output, "%s", v);
+  if(!(confirmation = cgi_get("c"))) {
+    cgi_set_option("error", "noconfirm");
+    expand_template(ds, output, "login");
+  }
+  /* Confirm our registration */
+  if(disorder_confirm(ds->g->client, confirmation)) {
+    cgi_set_option("error", "badconfirm");
+    expand_template(ds, output, "login");
+  }
+  /* Get a cookie */
+  if(disorder_make_cookie(ds->g->client, &login_cookie)) {
+    cgi_set_option("error", "cookiefailed");
+    expand_template(ds, output, "login");
+    return;
+  }
+  /* Discard any cached data JIC */
+  ds->g->flags = 0;
+  /* We have a new cookie */
+  header_cookie(output->sink);
+  cgi_set_option("status", "confirmed");
+  expand_template(ds, output, "login");
 }
 
-static void exp_version(int attribute((unused)) nargs,
-                       char attribute((unused)) **args,
-                       cgi_sink *output,
-                       void attribute((unused)) *u) {
-  cgi_output(output, "%s", disorder_version_string);
+static void act_edituser(cgi_sink *output,
+                        dcgi_state *ds) {
+  const char *email = cgi_get("email"), *password = cgi_get("changepassword1");
+  const char *password2 = cgi_get("changepassword2");
+  int newpassword = 0;
+  disorder_client *c;
+
+  if((password && *password) || (password && *password2)) {
+    if(!password || !password2 || strcmp(password, password2)) {
+      cgi_set_option("error", "passwordmismatch");
+      expand_template(ds, output, "login");
+      return;
+    }
+  } else
+    password = password2 = 0;
+  
+  if(email) {
+    if(disorder_edituser(ds->g->client, disorder_user(ds->g->client),
+                        "email", email)) {
+      cgi_set_option("error", "badedit");
+      expand_template(ds, output, "login");
+      return;
+    }
+  }
+  if(password) {
+    if(disorder_edituser(ds->g->client, disorder_user(ds->g->client),
+                        "password", password)) {
+      cgi_set_option("error", "badedit");
+      expand_template(ds, output, "login");
+      return;
+    }
+    newpassword = 1;
+  }
+  if(newpassword) {
+    login_cookie = 0;                  /* it'll be invalid now */
+    /* This is a bit duplicative of act_login() */
+    c = disorder_new(0);
+    if(disorder_connect_user(c, disorder_user(ds->g->client), password)) {
+      cgi_set_option("error", "loginfailed");
+      expand_template(ds, output, "login");
+      return;
+    }
+    if(disorder_make_cookie(c, &login_cookie)) {
+      cgi_set_option("error", "cookiefailed");
+      expand_template(ds, output, "login");
+      return;
+    }
+    /* Use the new connection henceforth */
+    ds->g->client = c;
+    ds->g->flags = 0;
+    /* We have a new cookie */
+    header_cookie(output->sink);
+  }
+  cgi_set_option("status", "edited");
+  expand_template(ds, output, "login");  
 }
 
-static void exp_nonce(int attribute((unused)) nargs,
-                     char attribute((unused)) **args,
-                     cgi_sink *output,
-                     void attribute((unused)) *u) {
-  cgi_output(output, "%s", nonce());
+static void act_reminder(cgi_sink *output,
+                        dcgi_state *ds) {
+  const char *const username = cgi_get("username");
+
+  if(!username || !*username) {
+    cgi_set_option("error", "nousername");
+    expand_template(ds, output, "login");
+    return;
+  }
+  if(disorder_reminder(ds->g->client, username)) {
+    cgi_set_option("error", "reminderfailed");
+    expand_template(ds, output, "login");
+    return;
+  }
+  cgi_set_option("status", "reminded");
+  expand_template(ds, output, "login");  
 }
 
+/* expansions *****************************************************************/
+
 static void exp_label(int attribute((unused)) nargs,
                      char **args,
                      cgi_sink *output,
@@ -470,197 +530,6 @@ struct trackinfo_state {
   time_t when;
 };
 
-static void exp_who(int attribute((unused)) nargs,
-                   char attribute((unused)) **args,
-                   cgi_sink *output,
-                   void *u) {
-  dcgi_state *ds = u;
-  
-  if(ds->track && ds->track->submitter)
-    cgi_output(output, "%s", ds->track->submitter);
-}
-
-static void exp_length(int attribute((unused)) nargs,
-                      char attribute((unused)) **args,
-                      cgi_sink *output,
-                      void *u) {
-  dcgi_state *ds = u;
-  long length = 0;
-
-  if(ds->track
-     && (ds->track->state == playing_started
-        || ds->track->state == playing_paused)
-     && ds->track->sofar >= 0)
-    cgi_output(output, "%ld:%02ld/",
-              ds->track->sofar / 60, ds->track->sofar % 60);
-  length = 0;
-  if(ds->track)
-    disorder_length(ds->g->client, ds->track->track, &length);
-  else if(ds->tracks)
-    disorder_length(ds->g->client, ds->tracks[0], &length);
-  if(length)
-    cgi_output(output, "%ld:%02ld", length / 60, length % 60);
-  else
-    sink_printf(output->sink, "%s", "&nbsp;");
-}
-
-static void exp_when(int attribute((unused)) nargs,
-                    char attribute((unused)) **args,
-                    cgi_sink *output,
-                    void *u) {
-  dcgi_state *ds = u;
-  const struct tm *w = 0;
-
-  if(ds->track)
-    switch(ds->track->state) {
-    case playing_isscratch:
-    case playing_unplayed:
-    case playing_random:
-      if(ds->track->expected)
-       w = localtime(&ds->track->expected);
-      break;
-    case playing_failed:
-    case playing_no_player:
-    case playing_ok:
-    case playing_scratched:
-    case playing_started:
-    case playing_paused:
-    case playing_quitting:
-      if(ds->track->played)
-       w = localtime(&ds->track->played);
-      break;
-    }
-  if(w)
-    cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
-  else
-    sink_printf(output->sink, "&nbsp;");
-}
-
-static void exp_part(int nargs,
-                    char **args,
-                    cgi_sink *output,
-                    void *u) {
-  dcgi_state *ds = u;
-  const char *s, *track, *part, *context;
-
-  if(nargs == 3)
-    track = args[2];
-  else {
-    if(ds->track)
-      track = ds->track->track;
-    else if(ds->tracks)
-      track = ds->tracks[0];
-    else
-      track = 0;
-  }
-  if(track) {
-    switch(nargs) {
-    case 1:
-      context = "display";
-      part = args[0];
-      break;
-    case 2:
-    case 3:
-      context = args[0];
-      part = args[1];
-      break;
-    default:
-      abort();
-    }
-    if(disorder_part(ds->g->client, (char **)&s, track,
-                    !strcmp(context, "short") ? "display" : context, part))
-      fatal(0, "disorder_part() failed");
-    if(!strcmp(context, "short"))
-      s = truncate_for_display(s, config->short_display);
-    cgi_output(output, "%s", s);
-  } else
-    sink_printf(output->sink, "&nbsp;");
-}
-
-static void exp_playing(int attribute((unused)) nargs,
-                       char **args,
-                       cgi_sink *output,
-                       void  *u) {
-  dcgi_state *ds = u;
-  dcgi_state s;
-
-  lookups(ds, DC_PLAYING);
-  memset(&s, 0, sizeof s);
-  s.g = ds->g;
-  if(ds->g->playing) {
-    s.track = ds->g->playing;
-    expandstring(output, args[0], &s);
-  }
-}
-
-static void exp_queue(int attribute((unused)) nargs,
-                     char **args,
-                     cgi_sink *output,
-                     void  *u) {
-  dcgi_state *ds = u;
-  dcgi_state s;
-  struct queue_entry *q;
-
-  lookups(ds, DC_QUEUE);
-  memset(&s, 0, sizeof s);
-  s.g = ds->g;
-  s.first = 1;
-  for(q = ds->g->queue; q; q = q->next) {
-    s.last = !q->next;
-    s.track = q;
-    expandstring(output, args[0], &s);
-    s.index++;
-    s.first = 0;
-  }
-}
-
-static void exp_recent(int attribute((unused)) nargs,
-                      char **args,
-                      cgi_sink *output,
-                      void  *u) {
-  dcgi_state *ds = u;
-  dcgi_state s;
-  struct queue_entry *q;
-
-  lookups(ds, DC_RECENT);
-  memset(&s, 0, sizeof s);
-  s.g = ds->g;
-  s.first = 1;
-  for(q = ds->g->recent; q; q = q->next) {
-    s.last = !q;
-    s.track = q;
-    expandstring(output, args[0], &s);
-    s.index++;
-    s.first = 0;
-  }
-}
-
-static void exp_new(int attribute((unused)) nargs,
-                   char **args,
-                   cgi_sink *output,
-                   void  *u) {
-  dcgi_state *ds = u;
-  dcgi_state s;
-
-  lookups(ds, DC_NEW);
-  memset(&s, 0, sizeof s);
-  s.g = ds->g;
-  s.first = 1;
-  for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
-    s.last = s.index + 1 < ds->g->nnew;
-    s.tracks = &ds->g->new[s.index];
-    expandstring(output, args[0], &s);
-    s.first = 0;
-  }
-}
-
-static void exp_url(int attribute((unused)) nargs,
-                   char attribute((unused)) **args,
-                   cgi_sink *output,
-                   void attribute((unused)) *u) {
-  cgi_output(output, "%s", config->url);
-}
-
 struct result {
   char *track;
   const char *sort;
@@ -742,16 +611,6 @@ static void exp_search(int nargs,
   assert(substate.last != 0);
 }
 
-static void exp_arg(int attribute((unused)) nargs,
-                   char **args,
-                   cgi_sink *output,
-                   void attribute((unused)) *u) {
-  const char *v;
-
-  if((v = cgi_get(args[0])))
-    cgi_output(output, "%s", v);
-}
-
 static void exp_stats(int attribute((unused)) nargs,
                      char attribute((unused)) **args,
                      cgi_sink *output,
@@ -767,60 +626,6 @@ static void exp_stats(int attribute((unused)) nargs,
   cgi_closetag(output->sink, "pre");
 }
 
-static void exp_volume(int attribute((unused)) nargs,
-                      char **args,
-                      cgi_sink *output,
-                      void *u) {
-  dcgi_state *ds = u;
-
-  lookups(ds, DC_VOLUME);
-  if(!strcmp(args[0], "left"))
-    cgi_output(output, "%d", ds->g->volume_left);
-  else
-    cgi_output(output, "%d", ds->g->volume_right);
-}
-
-static void exp_shell(int attribute((unused)) nargs,
-                     char **args,
-                     cgi_sink *output,
-                     void attribute((unused)) *u) {
-  int w, p[2], n;
-  char buffer[4096];
-  pid_t pid;
-  
-  xpipe(p);
-  if(!(pid = xfork())) {
-    exitfn = _exit;
-    xclose(p[0]);
-    xdup2(p[1], 1);
-    xclose(p[1]);
-    execlp("sh", "sh", "-c", args[0], (char *)0);
-    fatal(errno, "error executing sh");
-  }
-  xclose(p[1]);
-  while((n = read(p[0], buffer, sizeof buffer))) {
-    if(n < 0) {
-      if(errno == EINTR) continue;
-      else fatal(errno, "error reading from pipe");
-    }
-    output->sink->write(output->sink, buffer, n);
-  }
-  xclose(p[0]);
-  while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
-    ;
-  if(n < 0) fatal(errno, "error calling waitpid");
-  if(w)
-    error(0, "shell command '%s' %s", args[0], wstat(w));
-}
-
-static inline int str2bool(const char *s) {
-  return !strcmp(s, "true");
-}
-
-static inline const char *bool2str(int n) {
-  return n ? "true" : "false";
-}
-
 static char *expandarg(const char *arg, dcgi_state *ds) {
   struct dynstr d;
   cgi_sink output;
@@ -833,184 +638,6 @@ static char *expandarg(const char *arg, dcgi_state *ds) {
   return d.vec;
 }
 
-static void exp_prefs(int attribute((unused)) nargs,
-                     char **args,
-                     cgi_sink *output,
-                     void *u) {
-  dcgi_state *ds = u;
-  dcgi_state substate;
-  struct kvp *k;
-  const char *file = expandarg(args[0], ds);
-  
-  memset(&substate, 0, sizeof substate);
-  substate.g = ds->g;
-  substate.first = 1;
-  if(disorder_prefs(ds->g->client, file, &k)) return;
-  while(k) {
-    substate.last = !k->next;
-    substate.pref = k;
-    expandstring(output, args[1], &substate);
-    ++substate.index;
-    k = k->next;
-    substate.first = 0;
-  }
-}
-
-static void exp_pref(int attribute((unused)) nargs,
-                    char **args,
-                    cgi_sink *output,
-                    void *u) {
-  char *value;
-  dcgi_state *ds = u;
-
-  if(!disorder_get(ds->g->client, args[0], args[1], &value))
-    cgi_output(output, "%s", value);
-}
-
-static void exp_if(int nargs,
-                  char **args,
-                  cgi_sink *output,
-                  void *u) {
-  dcgi_state *ds = u;
-  int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
-  
-  if(n < nargs)
-    expandstring(output, args[n], ds);
-}
-
-static void exp_and(int nargs,
-                   char **args,
-                   cgi_sink *output,
-                   void *u) {
-  dcgi_state *ds = u;
-  int n, result = 1;
-
-  for(n = 0; n < nargs; ++n)
-    if(!str2bool(expandarg(args[n], ds))) {
-      result = 0;
-      break;
-    }
-  sink_printf(output->sink, "%s", bool2str(result));
-}
-
-static void exp_or(int nargs,
-                  char **args,
-                  cgi_sink *output,
-                  void *u) {
-  dcgi_state *ds = u;
-  int n, result = 0;
-
-  for(n = 0; n < nargs; ++n)
-    if(str2bool(expandarg(args[n], ds))) {
-      result = 1;
-      break;
-    }
-  sink_printf(output->sink, "%s", bool2str(result));
-}
-
-static void exp_not(int attribute((unused)) nargs,
-                   char **args,
-                   cgi_sink *output,
-                   void attribute((unused)) *u) {
-  sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
-}
-
-static void exp_isplaying(int attribute((unused)) nargs,
-                         char attribute((unused)) **args,
-                         cgi_sink *output,
-                         void *u) {
-  dcgi_state *ds = u;
-
-  lookups(ds, DC_PLAYING);
-  sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
-}
-
-static void exp_isqueue(int attribute((unused)) nargs,
-                       char attribute((unused)) **args,
-                       cgi_sink *output,
-                       void *u) {
-  dcgi_state *ds = u;
-
-  lookups(ds, DC_QUEUE);
-  sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
-}
-
-static void exp_isrecent(int attribute((unused)) nargs,
-                        char attribute((unused)) **args,
-                        cgi_sink *output,
-                        void *u) {
-  dcgi_state *ds = u;
-
-  lookups(ds, DC_RECENT);
-  sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
-}
-
-static void exp_isnew(int attribute((unused)) nargs,
-                     char attribute((unused)) **args,
-                     cgi_sink *output,
-                     void *u) {
-  dcgi_state *ds = u;
-
-  lookups(ds, DC_NEW);
-  sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
-}
-
-static void exp_id(int attribute((unused)) nargs,
-                  char attribute((unused)) **args,
-                  cgi_sink *output,
-                  void *u) {
-  dcgi_state *ds = u;
-
-  if(ds->track)
-    cgi_output(output, "%s", ds->track->id);
-}
-
-static void exp_track(int attribute((unused)) nargs,
-                     char attribute((unused)) **args,
-                     cgi_sink *output,
-                     void *u) {
-  dcgi_state *ds = u;
-
-  if(ds->track)
-    cgi_output(output, "%s", ds->track->track);
-}
-
-static void exp_parity(int attribute((unused)) nargs,
-                      char attribute((unused)) **args,
-                      cgi_sink *output,
-                      void *u) {
-  dcgi_state *ds = u;
-
-  cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
-}
-
-static void exp_comment(int attribute((unused)) nargs,
-                       char attribute((unused)) **args,
-                       cgi_sink attribute((unused)) *output,
-                       void attribute((unused)) *u) {
-  /* do nothing */
-}
-
-static void exp_prefname(int attribute((unused)) nargs,
-                        char attribute((unused)) **args,
-                        cgi_sink *output,
-                        void *u) {
-  dcgi_state *ds = u;
-
-  if(ds->pref && ds->pref->name)
-    cgi_output(output, "%s", ds->pref->name);
-}
-
-static void exp_prefvalue(int attribute((unused)) nargs,
-                         char attribute((unused)) **args,
-                         cgi_sink *output,
-                         void *u) {
-  dcgi_state *ds = u;
-
-  if(ds->pref && ds->pref->value)
-    cgi_output(output, "%s", ds->pref->value);
-}
-
 static void exp_isfiles(int attribute((unused)) nargs,
                        char attribute((unused)) **args,
                        cgi_sink *output,
@@ -1089,57 +716,6 @@ static void exp_file(int attribute((unused)) nargs,
     cgi_output(output, "%s", ds->tracks[0]);
 }
 
-static void exp_transform(int nargs,
-                         char **args,
-                         cgi_sink *output,
-                         void attribute((unused)) *u) {
-  const char *context = nargs > 2 ? args[2] : "display";
-
-  cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
-}
-
-static void exp_urlquote(int attribute((unused)) nargs,
-                        char **args,
-                        cgi_sink *output,
-                        void attribute((unused)) *u) {
-  cgi_output(output, "%s", urlencodestring(args[0]));
-}
-
-static void exp_scratchable(int attribute((unused)) nargs,
-                           char attribute((unused)) **args,
-                           cgi_sink *output,
-                           void attribute((unused)) *u) {
-  dcgi_state *ds = u;
-  int result;
-
-  if(config->restrictions & RESTRICT_SCRATCH) {
-    lookups(ds, DC_PLAYING);
-    result = (ds->g->playing
-             && (!ds->g->playing->submitter
-                 || !strcmp(ds->g->playing->submitter,
-                            disorder_user(ds->g->client))));
-  } else
-    result = 1;
-  sink_printf(output->sink, "%s", bool2str(result));
-}
-
-static void exp_removable(int attribute((unused)) nargs,
-                         char attribute((unused)) **args,
-                         cgi_sink *output,
-                         void attribute((unused)) *u) {
-  dcgi_state *ds = u;
-  int result;
-
-  if(config->restrictions & RESTRICT_REMOVE)
-    result = (ds->track
-             && ds->track->submitter
-             && !strcmp(ds->track->submitter,
-                        disorder_user(ds->g->client)));
-  else
-    result = 1;
-  sink_printf(output->sink, "%s", bool2str(result));
-}
-
 static void exp_navigate(int attribute((unused)) nargs,
                         char **args,
                         cgi_sink *output,
@@ -1209,138 +785,6 @@ static void exp_dirname(int nargs,
     cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
 }
 
-static void exp_eq(int attribute((unused)) nargs,
-                  char **args,
-                  cgi_sink *output,
-                  void attribute((unused)) *u) {
-  cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
-}
-
-static void exp_ne(int attribute((unused)) nargs,
-                  char **args,
-                  cgi_sink *output,
-                  void attribute((unused)) *u) {
-  cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
-}
-
-static void exp_enabled(int attribute((unused)) nargs,
-                              char attribute((unused)) **args,
-                              cgi_sink *output,
-                              void *u) {
-  dcgi_state *ds = u;
-  int enabled = 0;
-
-  if(ds->g->client)
-    disorder_enabled(ds->g->client, &enabled);
-  cgi_output(output, "%s", bool2str(enabled));
-}
-
-static void exp_random_enabled(int attribute((unused)) nargs,
-                              char attribute((unused)) **args,
-                              cgi_sink *output,
-                              void *u) {
-  dcgi_state *ds = u;
-  int enabled = 0;
-
-  if(ds->g->client)
-    disorder_random_enabled(ds->g->client, &enabled);
-  cgi_output(output, "%s", bool2str(enabled));
-}
-
-static void exp_trackstate(int attribute((unused)) nargs,
-                          char **args,
-                          cgi_sink *output,
-                          void *u) {
-  dcgi_state *ds = u;
-  struct queue_entry *q;
-  char *track;
-
-  if(disorder_resolve(ds->g->client, &track, args[0])) return;
-  lookups(ds, DC_QUEUE|DC_PLAYING);
-  if(ds->g->playing && !strcmp(ds->g->playing->track, track))
-    cgi_output(output, "playing");
-  else {
-    for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
-      ;
-    if(q)
-      cgi_output(output, "queued");
-  }
-}
-
-static void exp_thisurl(int attribute((unused)) nargs,
-                       char attribute((unused)) **args,
-                       cgi_sink *output,
-                       void attribute((unused)) *u) {
-  kvp_set(&cgi_args, "nonce", nonce());        /* nonces had better differ! */
-  cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
-}
-
-static void exp_isfirst(int attribute((unused)) nargs,
-                       char attribute((unused)) **args,
-                       cgi_sink *output,
-                       void *u) {
-  dcgi_state *ds = u;
-
-  sink_printf(output->sink, "%s", bool2str(!!ds->first));
-}
-
-static void exp_islast(int attribute((unused)) nargs,
-                       char attribute((unused)) **args,
-                       cgi_sink *output,
-                       void *u) {
-  dcgi_state *ds = u;
-
-  sink_printf(output->sink, "%s", bool2str(!!ds->last));
-}
-
-static void exp_action(int attribute((unused)) nargs,
-                      char attribute((unused)) **args,
-                      cgi_sink *output,
-                      void attribute((unused)) *u) {
-  const char *action = cgi_get("action"), *mgmt;
-
-  if(!action) action = "playing";
-  if(!strcmp(action, "playing")
-     && (mgmt = cgi_get("mgmt"))
-     && !strcmp(mgmt, "true"))
-    action = "manage";
-  sink_printf(output->sink, "%s", action);
-}
-
-static void exp_resolve(int attribute((unused)) nargs,
-                      char  **args,
-                      cgi_sink *output,
-                      void attribute((unused)) *u) {
-  dcgi_state *ds = u;
-  char *track;
-  
-  if(!disorder_resolve(ds->g->client, &track, args[0]))
-    sink_printf(output->sink, "%s", track);
-}
-static void exp_paused(int attribute((unused)) nargs,
-                      char attribute((unused)) **args,
-                      cgi_sink *output,
-                      void *u) {
-  dcgi_state *ds = u;
-  int paused = 0;
-
-  lookups(ds, DC_PLAYING);
-  if(ds->g->playing && ds->g->playing->state == playing_paused)
-    paused = 1;
-  cgi_output(output, "%s", bool2str(paused));
-}
-
-static void exp_state(int attribute((unused)) nargs,
-                     char attribute((unused)) **args,
-                     cgi_sink *output,
-                     void *u) {
-  dcgi_state *ds = u;
-
-  if(ds->track)
-    cgi_output(output, "%s", playing_states[ds->track->state]);
-}
-
 static void exp_files(int attribute((unused)) nargs,
                      char **args,
                      cgi_sink *output,
@@ -1376,15 +820,6 @@ static void exp_files(int attribute((unused)) nargs,
   }
 }
 
-static void exp_index(int attribute((unused)) nargs,
-                     char attribute((unused)) **args,
-                     cgi_sink *output,
-                     void *u) {
-  dcgi_state *ds = u;
-
-  cgi_output(output, "%d", ds->index);
-}
-
 static void exp_nfiles(int attribute((unused)) nargs,
                       char attribute((unused)) **args,
                       cgi_sink *output,
@@ -1401,114 +836,24 @@ static void exp_nfiles(int attribute((unused)) nargs,
     cgi_output(output, "1");
 }
 
-static const struct cgi_expansion expansions[] = {
-  { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
-  { "action", 0, 0, 0, exp_action },
-  { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
-  { "arg", 1, 1, 0, exp_arg },
-  { "basename", 0, 1, 0, exp_basename },
-  { "choose", 2, 2, EXP_MAGIC, exp_choose },
-  { "dirname", 0, 1, 0, exp_dirname },
-  { "enabled", 0, 0, 0, exp_enabled },
-  { "eq", 2, 2, 0, exp_eq },
-  { "file", 0, 0, 0, exp_file },
-  { "files", 1, 1, EXP_MAGIC, exp_files },
-  { "fullname", 0, 0, 0, exp_fullname },
-  { "id", 0, 0, 0, exp_id },
-  { "if", 2, 3, EXP_MAGIC, exp_if },
-  { "include", 1, 1, 0, exp_include },
-  { "index", 0, 0, 0, exp_index },
-  { "isdirectories", 0, 0, 0, exp_isdirectories },
-  { "isfiles", 0, 0, 0, exp_isfiles },
-  { "isfirst", 0, 0, 0, exp_isfirst },
-  { "islast", 0, 0, 0, exp_islast },
-  { "isnew", 0, 0, 0, exp_isnew },
-  { "isplaying", 0, 0, 0, exp_isplaying },
-  { "isqueue", 0, 0, 0, exp_isqueue },
-  { "isrecent", 0, 0, 0, exp_isrecent },
-  { "label", 1, 1, 0, exp_label },
-  { "length", 0, 0, 0, exp_length },
-  { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
-  { "ne", 2, 2, 0, exp_ne },
-  { "new", 1, 1, EXP_MAGIC, exp_new },
-  { "nfiles", 0, 0, 0, exp_nfiles },
-  { "nonce", 0, 0, 0, exp_nonce },
-  { "not", 1, 1, 0, exp_not },
-  { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
-  { "parity", 0, 0, 0, exp_parity },
-  { "part", 1, 3, 0, exp_part },
-  { "paused", 0, 0, 0, exp_paused },
-  { "playing", 1, 1, EXP_MAGIC, exp_playing },
-  { "pref", 2, 2, 0, exp_pref },
-  { "prefname", 0, 0, 0, exp_prefname },
-  { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
-  { "prefvalue", 0, 0, 0, exp_prefvalue },
-  { "queue", 1, 1, EXP_MAGIC, exp_queue },
-  { "random-enabled", 0, 0, 0, exp_random_enabled },
-  { "recent", 1, 1, EXP_MAGIC, exp_recent },
-  { "removable", 0, 0, 0, exp_removable },
-  { "resolve", 1, 1, 0, exp_resolve },
-  { "scratchable", 0, 0, 0, exp_scratchable },
-  { "search", 2, 3, EXP_MAGIC, exp_search },
-  { "server-version", 0, 0, 0, exp_server_version },
-  { "shell", 1, 1, 0, exp_shell },
-  { "state", 0, 0, 0, exp_state },
-  { "stats", 0, 0, 0, exp_stats },
-  { "thisurl", 0, 0, 0, exp_thisurl },
-  { "track", 0, 0, 0, exp_track },
-  { "trackstate", 1, 1, 0, exp_trackstate },
-  { "transform", 2, 3, 0, exp_transform },
-  { "url", 0, 0, 0, exp_url },
-  { "urlquote", 1, 1, 0, exp_urlquote },
-  { "version", 0, 0, 0, exp_version },
-  { "volume", 1, 1, 0, exp_volume },
-  { "when", 0, 0, 0, exp_when },
-  { "who", 0, 0, 0, exp_who }
-};
-
-static void expand(cgi_sink *output,
-                  const char *template,
-                  dcgi_state *ds) {
-  cgi_expand(template,
-            expansions, sizeof expansions / sizeof *expansions,
-            output,
-            ds);
-}
-
-static void expandstring(cgi_sink *output,
-                        const char *string,
-                        dcgi_state *ds) {
-  cgi_expand_string("",
-                   string,
-                   expansions, sizeof expansions / sizeof *expansions,
-                   output,
-                   ds);
-}
-
-static void perform_action(cgi_sink *output, dcgi_state *ds,
-                          const char *action) {
-  int n;
-
-  if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
-    actions[n].handler(output, ds);
-  else {
-    cgi_header(output->sink, "Content-Type", "text/html");
-    cgi_body(output->sink);
-    expand(output, action, ds);
-  }
-}
-
-void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
-  const char *action = cgi_get("action");
-
-  if(!action) action = "playing";
-  perform_action(output, ds, action);
-}
-
-void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
-                       const char *msg) {
-  cgi_set_option("error", msg);
-  perform_action(output, ds, "error");
+static void exp_image(int attribute((unused)) nargs,
+                     char **args,
+                     cgi_sink *output,
+                     void attribute((unused)) *u) {
+  char *labelname;
+  const char *imagestem;
+
+  byte_xasprintf(&labelname, "images.%s", args[0]);
+  if(cgi_label_exists(labelname))
+    imagestem = cgi_label(labelname);
+  else if(strchr(args[0], '.'))
+    imagestem = args[0];
+  else
+    byte_xasprintf((char **)&imagestem, "%s.png", args[0]);
+  if(cgi_label_exists("url.static"))
+    cgi_output(output, "%s/%s", cgi_label("url.static"), imagestem);
+  else
+    cgi_output(output, "/disorder/%s", imagestem);
 }
 
 /*