X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/763d5e6ad88ef3ba1cd1d7742d060e4f1e54c6b8..bb037be206aa8d904c310f722a971ed835588288:/server/dcgi.c diff --git a/server/dcgi.c b/server/dcgi.c index 2892a43..b9e4b60 100644 --- a/server/dcgi.c +++ b/server/dcgi.c @@ -1,6 +1,6 @@ /* * This file is part of DisOrder. - * Copyright (C) 2004, 2005, 2006 Richard Kettlewell + * 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 @@ -46,7 +46,6 @@ #include "queue.h" #include "plugin.h" #include "split.h" -#include "words.h" #include "wstat.h" #include "kvp.h" #include "syscalls.h" @@ -54,6 +53,9 @@ #include "regsub.h" #include "defs.h" #include "trackname.h" +#include "charset.h" + +char *login_cookie; static void expand(cgi_sink *output, const char *template, @@ -107,6 +109,30 @@ static void redirect(struct sink *output) { cgi_body(output); } +static void header_cookie(cgi_sink *output) { + struct dynstr d[1]; + char *s; + + if(login_cookie) { + dynstr_init(d); + for(s = login_cookie; *s; ++s) { + if(*s == '"') + dynstr_append(d, '\\'); + dynstr_append(d, *s); + } + dynstr_terminate(d); + byte_xasprintf(&s, "disorder=\"%s\"", d->vec); /* TODO domain, path, expiry */ + cgi_header(output->sink, "Set-Cookie", s); + } +} + +static void expand_template(dcgi_state *ds, cgi_sink *output, + const char *action) { + cgi_header(output->sink, "Content-Type", "text/html"); + cgi_body(output->sink); + expand(output, action, ds); +} + static void lookups(dcgi_state *ds, unsigned want) { unsigned need; struct queue_entry *r, *rnext; @@ -117,6 +143,8 @@ static void lookups(dcgi_state *ds, unsigned want) { 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); @@ -398,12 +426,88 @@ static void act_resume(cgi_sink *output, redirect(output->sink); } +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; + } + c = disorder_new(1); + 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; + } + /* We have a new cookie */ + header_cookie(output); + 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 act_register(cgi_sink *output, + dcgi_state *ds) { + const char *username, *password, *email; + char *confirm; + + username = cgi_get("username"); + password = cgi_get("password"); + 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(!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; + } + /* We'll go back to the login page with a suitable message */ + cgi_set_option("registered", "registeredok"); + expand_template(ds, output, "login"); +} + static const struct action { const char *name; void (*handler)(cgi_sink *output, dcgi_state *ds); } actions[] = { { "disable", act_disable }, { "enable", act_enable }, + { "login", act_login }, { "move", act_move }, { "pause", act_pause }, { "play", act_play }, @@ -411,6 +515,7 @@ static const struct action { { "prefs", act_prefs }, { "random-disable", act_random_disable }, { "random-enable", act_random_enable }, + { "register", act_register }, { "remove", act_remove }, { "resume", act_resume }, { "scratch", act_scratch }, @@ -444,7 +549,7 @@ 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); + cgi_output(output, "%s", disorder_short_version_string); } static void exp_nonce(int attribute((unused)) nargs, @@ -483,7 +588,7 @@ static void exp_length(int attribute((unused)) nargs, cgi_sink *output, void *u) { dcgi_state *ds = u; - long length; + long length = 0; if(ds->track && (ds->track->state == playing_started @@ -491,8 +596,11 @@ static void exp_length(int attribute((unused)) nargs, && ds->track->sofar >= 0) cgi_output(output, "%ld:%02ld/", ds->track->sofar / 60, ds->track->sofar % 60); - if(!ds->track || disorder_length(ds->g->client, ds->track->track, &length)) - length = 0; + 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 @@ -562,8 +670,11 @@ static void exp_part(int nargs, default: abort(); } - if(disorder_part(ds->g->client, (char **)&s, track, context, part)) + 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, " "); @@ -627,6 +738,25 @@ static void exp_recent(int attribute((unused)) nargs, } } +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, @@ -918,6 +1048,16 @@ static void exp_isrecent(int attribute((unused)) nargs, 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, @@ -1364,6 +1504,15 @@ static void exp_nfiles(int attribute((unused)) nargs, cgi_output(output, "1"); } +static void exp_user(int attribute((unused)) nargs, + char attribute((unused)) **args, + cgi_sink *output, + void *u) { + dcgi_state *const ds = u; + + cgi_output(output, "%s", disorder_user(ds->g->client)); +} + static const struct cgi_expansion expansions[] = { { "#", 0, INT_MAX, EXP_MAGIC, exp_comment }, { "action", 0, 0, 0, exp_action }, @@ -1385,6 +1534,7 @@ static const struct cgi_expansion expansions[] = { { "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 }, @@ -1392,6 +1542,7 @@ static const struct cgi_expansion expansions[] = { { "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 }, @@ -1421,6 +1572,7 @@ static const struct cgi_expansion expansions[] = { { "transform", 2, 3, 0, exp_transform }, { "url", 0, 0, 0, exp_url }, { "urlquote", 1, 1, 0, exp_urlquote }, + { "user", 0, 0, 0, exp_user }, { "version", 0, 0, 0, exp_version }, { "volume", 1, 1, 0, exp_volume }, { "when", 0, 0, 0, exp_when }, @@ -1450,13 +1602,14 @@ static void perform_action(cgi_sink *output, dcgi_state *ds, const char *action) { int n; + /* If we have a login cookie it'd better appear in all responses */ + header_cookie(output); + /* 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 { - cgi_header(output->sink, "Content-Type", "text/html"); - cgi_body(output->sink); - expand(output, action, ds); - } + else + expand_template(ds, output, action); } void disorder_cgi(cgi_sink *output, dcgi_state *ds) {