-/*
- * This file is part of DisOrder.
- * Copyright (C) 2004-2008 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>
#include "sendmail.h"
#include "base64.h"
-char *login_cookie;
-
-static void expand(cgi_sink *output,
- const char *template,
- dcgi_state *ds);
-static void expandstring(cgi_sink *output,
- const char *string,
- dcgi_state *ds);
-
struct entry {
const char *path;
const char *sort;
const char *display;
};
-static const char nonce_base64_table[] =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-/*";
-
-static const char *nonce(void) {
- static uint32_t count;
-
- struct ndata {
- uint16_t count;
- uint16_t pid;
- uint32_t when;
- } nd;
-
- nd.count = count++;
- nd.pid = (uint32_t)getpid();
- nd.when = (uint32_t)time(0);
- return generic_to_base64((void *)&nd, sizeof nd,
- nonce_base64_table);
-}
-
static int compare_entry(const void *a, const void *b) {
const struct entry *ea = a, *eb = b;
return config->url;
}
-static void header_cookie(struct sink *output) {
- struct dynstr d[1];
- struct url u;
-
- memset(&u, 0, sizeof u);
- dynstr_init(d);
- parse_url(config->url, &u);
- if(login_cookie) {
- dynstr_append_string(d, "disorder=");
- dynstr_append_string(d, login_cookie);
- } else {
- /* Force browser to discard cookie */
- dynstr_append_string(d, "disorder=none;Max-Age=0");
- }
- if(u.path) {
- /* The default domain matches the request host, so we need not override
- * that. But the default path only goes up to the rightmost /, which would
- * cause the browser to expose the cookie to other CGI programs on the same
- * web server. */
- dynstr_append_string(d, ";Version=1;Path=");
- /* Formally we are supposed to quote the path, since it invariably has a
- * slash in it. However Safari does not parse quoted paths correctly, so
- * this won't work. Fortunately nothing else seems to care about proper
- * quoting of paths, so in practice we get with it. (See also
- * parse_cookie() where we are liberal about cookie paths on the way back
- * in.) */
- dynstr_append_string(d, u.path);
- }
- dynstr_terminate(d);
- cgi_header(output, "Set-Cookie", d->vec);
-}
-
static void redirect(struct sink *output) {
const char *back;
/* actions ********************************************************************/
-static void act_disable(cgi_sink *output,
- dcgi_state *ds) {
- if(ds->g->client)
- disorder_disable(ds->g->client);
- redirect(output->sink);
-}
-
-static void act_enable(cgi_sink *output,
- dcgi_state *ds) {
- if(ds->g->client)
- disorder_enable(ds->g->client);
- redirect(output->sink);
-}
-
-static void act_random_disable(cgi_sink *output,
- dcgi_state *ds) {
- if(ds->g->client)
- disorder_random_disable(ds->g->client);
- redirect(output->sink);
-}
-
-static void act_random_enable(cgi_sink *output,
- dcgi_state *ds) {
- if(ds->g->client)
- disorder_random_enable(ds->g->client);
- redirect(output->sink);
-}
-
-static void act_remove(cgi_sink *output,
- dcgi_state *ds) {
- const char *id;
-
- if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
- if(ds->g->client)
- disorder_remove(ds->g->client, id);
- redirect(output->sink);
-}
-
-static void act_move(cgi_sink *output,
- dcgi_state *ds) {
- const char *id, *delta;
-
- if(!(id = cgi_get("id"))) fatal(0, "missing id argument");
- if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument");
- if(ds->g->client)
- disorder_move(ds->g->client, id, atoi(delta));
- redirect(output->sink);
-}
-
-static void act_scratch(cgi_sink *output,
- dcgi_state *ds) {
- if(ds->g->client)
- disorder_scratch(ds->g->client, cgi_get("id"));
- 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);
- header_cookie(output->sink);
- cgi_body(output->sink);
- expand(output, "playing", ds);
-}
-
-static void act_play(cgi_sink *output,
- dcgi_state *ds) {
- const char *track, *dir;
- char **tracks;
- int ntracks, n;
- struct entry *e;
-
- if((track = cgi_get("file"))) {
- disorder_play(ds->g->client, track);
- } else if((dir = cgi_get("directory"))) {
- if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0;
- if(ntracks) {
- e = xmalloc(ntracks * sizeof (struct entry));
- for(n = 0; n < ntracks; ++n) {
- e[n].path = tracks[n];
- e[n].sort = trackname_transform("track", tracks[n], "sort");
- e[n].display = trackname_transform("track", tracks[n], "display");
- }
- qsort(e, ntracks, sizeof (struct entry), compare_entry);
- for(n = 0; n < ntracks; ++n)
- disorder_play(ds->g->client, e[n].path);
- }
- }
- /* XXX error handling */
- redirect(output->sink);
-}
-
-static int clamp(int n, int min, int max) {
- if(n < min)
- return min;
- if(n > max)
- return max;
- return n;
-}
-
-static const char *volume_url(void) {
- char *url;
-
- byte_xasprintf(&url, "%s?action=volume", config->url);
- return url;
-}
-
-static void act_volume(cgi_sink *output, dcgi_state *ds) {
- const char *l, *r, *d, *back;
- int nd, changed = 0;;
-
- if((d = cgi_get("delta"))) {
- lookups(ds, DC_VOLUME);
- nd = clamp(atoi(d), -255, 255);
- disorder_set_volume(ds->g->client,
- clamp(ds->g->volume_left + nd, 0, 255),
- clamp(ds->g->volume_right + nd, 0, 255));
- changed = 1;
- } else if((l = cgi_get("left")) && (r = cgi_get("right"))) {
- disorder_set_volume(ds->g->client, atoi(l), atoi(r));
- changed = 1;
- }
- if(changed) {
- /* redirect back to ourselves (but without the volume-changing bits in the
- * 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);
- }
-}
-
static void act_prefs_errors(const char *msg,
void attribute((unused)) *u) {
fatal(0, "error splitting parts list: %s", msg);
expand(output, "prefs", ds);
}
-static void act_pause(cgi_sink *output,
- dcgi_state *ds) {
- if(ds->g->client)
- disorder_pause(ds->g->client);
- redirect(output->sink);
-}
-
-static void act_resume(cgi_sink *output,
- dcgi_state *ds) {
- if(ds->g->client)
- disorder_resume(ds->g->client);
- redirect(output->sink);
-}
-
static void act_login(cgi_sink *output,
dcgi_state *ds) {
const char *username, *password, *back;
expand_template(ds, output, "login");
}
-static const struct action {
- const char *name;
- void (*handler)(cgi_sink *output, dcgi_state *ds);
-} actions[] = {
- { "confirm", act_confirm },
- { "disable", act_disable },
- { "edituser", act_edituser },
- { "enable", act_enable },
- { "login", act_login },
- { "logout", act_logout },
- { "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 },
- { "register", act_register },
- { "reminder", act_reminder },
- { "remove", act_remove },
- { "resume", act_resume },
- { "scratch", act_scratch },
- { "volume", act_volume },
-};
-
/* expansions *****************************************************************/
static void exp_label(int attribute((unused)) nargs,
return c;
}
-static void exp_search(int nargs,
- char **args,
- cgi_sink *output,
- void *u) {
- dcgi_state *ds = u, substate;
- char **tracks;
- const char *q, *context, *part, *template;
- int ntracks, n, m;
- struct result *r;
-
- switch(nargs) {
- case 2:
- part = args[0];
- context = "sort";
- template = args[1];
- break;
- case 3:
- part = args[0];
- context = args[1];
- template = args[2];
- break;
- default:
- assert(!"should never happen");
- part = context = template = 0; /* quieten compiler */
- }
- if(ds->tracks == 0) {
- /* we are the top level, let's get some search results */
- if(!(q = cgi_get("query"))) return; /* no results yet */
- if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
- if(!ntracks) return;
- } else {
- tracks = ds->tracks;
- ntracks = ds->ntracks;
- }
- assert(ntracks != 0);
- /* sort tracks by the appropriate part */
- r = xmalloc(ntracks * sizeof *r);
- for(n = 0; n < ntracks; ++n) {
- r[n].track = tracks[n];
- if(disorder_part(ds->g->client, (char **)&r[n].sort,
- tracks[n], context, part))
- fatal(0, "disorder_part() failed");
- }
- qsort(r, ntracks, sizeof (struct result), compare_result);
- /* expand the 2nd arg once for each group. We re-use the passed-in tracks
- * array as we know it's guaranteed to be big enough and isn't going to be
- * used for anything else any more. */
- memset(&substate, 0, sizeof substate);
- substate.g = ds->g;
- substate.first = 1;
- n = 0;
- while(n < ntracks) {
- substate.tracks = tracks;
- substate.ntracks = 0;
- m = n;
- while(m < ntracks
- && !strcmp(r[m].sort, r[n].sort))
- tracks[substate.ntracks++] = r[m++].track;
- substate.last = (m == ntracks);
- expandstring(output, template, &substate);
- substate.index++;
- substate.first = 0;
- n = m;
- }
- assert(substate.last != 0);
-}
-
static void exp_stats(int attribute((unused)) nargs,
char attribute((unused)) **args,
cgi_sink *output,
return d.vec;
}
-static void exp_isfiles(int attribute((unused)) nargs,
- char attribute((unused)) **args,
- cgi_sink *output,
- void *u) {
- dcgi_state *ds = u;
-
- lookups(ds, DC_FILES);
- sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
-}
-
-static void exp_isdirectories(int attribute((unused)) nargs,
- char attribute((unused)) **args,
- cgi_sink *output,
- void *u) {
- dcgi_state *ds = u;
-
- lookups(ds, DC_DIRS);
- sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
-}
-
-static void exp_choose(int attribute((unused)) nargs,
- char **args,
- cgi_sink *output,
- void *u) {
- dcgi_state *ds = u;
- dcgi_state substate;
- int nfiles, n;
- char **files;
- struct entry *e;
- const char *type, *what = expandarg(args[0], ds);
-
- if(!strcmp(what, "files")) {
- lookups(ds, DC_FILES);
- files = ds->g->files;
- nfiles = ds->g->nfiles;
- type = "track";
- } else if(!strcmp(what, "directories")) {
- lookups(ds, DC_DIRS);
- files = ds->g->dirs;
- nfiles = ds->g->ndirs;
- type = "dir";
- } else {
- error(0, "unknown @choose@ argument '%s'", what);
- return;
- }
- e = xmalloc(nfiles * sizeof (struct entry));
- for(n = 0; n < nfiles; ++n) {
- e[n].path = files[n];
- e[n].sort = trackname_transform(type, files[n], "sort");
- e[n].display = trackname_transform(type, files[n], "display");
- }
- qsort(e, nfiles, sizeof (struct entry), compare_entry);
- memset(&substate, 0, sizeof substate);
- substate.g = ds->g;
- substate.first = 1;
- for(n = 0; n < nfiles; ++n) {
- substate.last = (n == nfiles - 1);
- substate.index = n;
- substate.entry = &e[n];
- expandstring(output, args[1], &substate);
- substate.first = 0;
- }
-}
-
-static void exp_file(int attribute((unused)) nargs,
- char attribute((unused)) **args,
- cgi_sink *output,
- void *u) {
- dcgi_state *ds = u;
-
- if(ds->entry)
- cgi_output(output, "%s", ds->entry->path);
- else if(ds->track)
- cgi_output(output, "%s", ds->track->track);
- else if(ds->tracks)
- cgi_output(output, "%s", ds->tracks[0]);
-}
-
static void exp_navigate(int attribute((unused)) nargs,
char **args,
cgi_sink *output,
cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
}
-static void exp_basename(int nargs,
- char **args,
- cgi_sink *output,
- void *u) {
- dcgi_state *ds = u;
- const char *s;
-
- if(nargs) {
- if((s = strrchr(args[0], '/'))) ++s;
- else s = args[0];
- cgi_output(output, "%s", s);
- } else
- cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
- ds->nav_path + ds->nav_dirlen + 1);
-}
-
-static void exp_dirname(int nargs,
- char **args,
- cgi_sink *output,
- void *u) {
- dcgi_state *ds = u;
- const char *s;
-
- if(nargs) {
- if((s = strrchr(args[0], '/')))
- cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
- } else
- cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
-}
-
-static void exp_files(int attribute((unused)) nargs,
- char **args,
- cgi_sink *output,
- void *u) {
- dcgi_state *ds = u;
- dcgi_state substate;
- const char *nfiles_arg, *directory;
- int nfiles, numfile;
- struct kvp *k;
-
- memset(&substate, 0, sizeof substate);
- substate.g = ds->g;
- if((directory = cgi_get("directory"))) {
- /* Prefs for whole directory. */
- lookups(ds, DC_FILES);
- /* Synthesize args for the file list. */
- nfiles = ds->g->nfiles;
- for(numfile = 0; numfile < nfiles; ++numfile) {
- k = xmalloc(sizeof *k);
- byte_xasprintf((char **)&k->name, "%d_file", numfile);
- k->value = ds->g->files[numfile];
- k->next = cgi_args;
- cgi_args = k;
- }
- } else {
- /* Args already present. */
- if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
- else nfiles = 1;
- }
- for(numfile = 0; numfile < nfiles; ++numfile) {
- substate.index = numfile;
- expandstring(output, args[0], &substate);
- }
-}
-
-static void exp_nfiles(int attribute((unused)) nargs,
- char attribute((unused)) **args,
- cgi_sink *output,
- void *u) {
- dcgi_state *ds = u;
- const char *files_arg;
-
- if(cgi_get("directory")) {
- lookups(ds, DC_FILES);
- cgi_output(output, "%d", ds->g->nfiles);
- } else if((files_arg = cgi_get("files")))
- cgi_output(output, "%s", files_arg);
- else
- cgi_output(output, "1");
-}
-
-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);
-}
-
-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 },
- { "define", 3, 3, EXP_MAGIC, exp_define },
- { "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 },
- { "image", 1, 1, 0, exp_image },
- { "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 },
- { "movable", 0, 0, 0, exp_movable },
- { "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 },
- { "right", 1, 3, EXP_MAGIC, exp_right },
- { "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 },
- { "user", 0, 0, 0, exp_user },
- { "userinfo", 1, 1, 0, exp_userinfo },
- { "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;
-
- /* We don't ever want anything to be cached */
- cgi_header(output->sink, "Cache-Control", "no-cache");
- if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
- actions[n].handler(output, ds);
- else
- expand_template(ds, output, action);
-}
-
-void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
- const char *action = cgi_get("action");
-
- if(!action) {
- /* We allow URLs which are just confirm=... in order to keep confirmation
- * URLs, which are user-facing, as short as possible. */
- if(cgi_get("c"))
- action = "confirm";
- else
- 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");
-}
-
-/** @brief Log in as the current user or guest if none */
-void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) {
- /* Create a new connection */
- ds->g->client = disorder_new(0);
- /* Forget everything we knew */
- ds->g->flags = 0;
- /* Reconnect */
- if(disorder_connect_cookie(ds->g->client, login_cookie)) {
- disorder_cgi_error(output, ds, "connect");
- exit(0);
- }
- /* If there was a cookie but it went bad, we forget it */
- if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest"))
- login_cookie = 0;
-}
-
/*
Local Variables:
c-basic-offset:2