From 1e97629da2ea6c1ae328912ba82d4595eb08b700 Mon Sep 17 00:00:00 2001 Message-Id: <1e97629da2ea6c1ae328912ba82d4595eb08b700.1715298914.git.mdw@distorted.org.uk> From: Mark Wooding Date: Sat, 10 May 2008 18:30:44 +0100 Subject: [PATCH] reorg cgi code a bit... Organization: Straylight/Edgeware From: Richard Kettlewell --- server/Makefile.am | 3 +- server/actions.c | 174 ++++++++----------------- server/actions.h | 39 ------ server/cgimain.c | 37 +----- server/disorder-cgi.h | 115 ++++++++++++++++ server/login.c | 95 ++++++++++++++ server/lookup.c | 136 +++++++++---------- server/lookup.h | 73 ----------- server/macros-disorder.c | 170 +++++++++++------------- server/macros-disorder.h | 40 ------ server/options.c | 21 +-- server/options.h | 39 ------ templates/{volume.html => volume.tmpl} | 0 13 files changed, 412 insertions(+), 530 deletions(-) delete mode 100644 server/actions.h create mode 100644 server/disorder-cgi.h create mode 100644 server/login.c delete mode 100644 server/lookup.h delete mode 100644 server/macros-disorder.h delete mode 100644 server/options.h rename templates/{volume.html => volume.tmpl} (100%) diff --git a/server/Makefile.am b/server/Makefile.am index 6b2c683..9ad35b2 100644 --- a/server/Makefile.am +++ b/server/Makefile.am @@ -101,7 +101,8 @@ disorder_dbupgrade_LDADD=$(LIBOBJS) ../lib/libdisorder.a \ disorder_dbupgrade_DEPENDENCIES=../lib/libdisorder.a disorder_cgi_SOURCES=macros-disorder.c macros-disorder.h lookup.c \ - lookup.h options.c options.h actions.c actions.h api.c \ + lookup.h options.c options.h actions.c actions.h login.c \ + login.h api.c \ api-client.c api-client.h cgimain.c exports.c disorder_cgi_LDADD=../lib/libdisorder.a \ $(LIBPCRE) $(LIBGCRYPT) $(LIBDL) $(LIBDB) diff --git a/server/actions.c b/server/actions.c index 341f881..7b69932 100644 --- a/server/actions.c +++ b/server/actions.c @@ -17,72 +17,16 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ +/** @file server/actions.c + * @brief DisOrder web actions + * + * Actions are anything that the web interface does beyond passive template + * expansion and inspection of state recieved from the server. This means + * playing tracks, editing prefs etc but also setting extra headers e.g. to + * auto-refresh the playing list. + */ -#include -#include "types.h" - -#include -#include -#include -#include -#include -#include - -#include "hash.h" -#include "table.h" -#include "client.h" -#include "rights.h" -#include "mem.h" -#include "sink.h" -#include "vector.h" -#include "printf.h" -#include "actions.h" -#include "lookup.h" -#include "url.h" -#include "configuration.h" -#include "cgi.h" -#include "log.h" -#include "queue.h" -#include "macros.h" -#include "macros-disorder.h" - -/** @brief Login cookie */ -char *login_cookie; - -/** @brief Return a Cookie: header */ -static char *cookie(void) { - struct dynstr d[1]; - struct url u; - char *s; - - 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); - byte_xasprintf(&s, "Set-Cookie: %s", d->vec); - return s; -} +#include "disorder-cgi.h" /** @brief Redirect to some other action or URL */ static void redirect(const char *url) { @@ -99,7 +43,7 @@ static void redirect(const char *url) { } if(printf("Location: %s\n" "%s\n" - "\n", url, cookie()) < 0) + "\n", url, dcgi_cookie_header()) < 0) fatal(errno, "error writing to stdout"); } @@ -111,28 +55,28 @@ static void act_playing(void) { char *url; const char *action; - lookup(DC_PLAYING|DC_QUEUE|DC_ENABLED|DC_RANDOM_ENABLED); - if(playing - && playing->state == playing_started /* i.e. not paused */ - && !disorder_length(client, playing->track, &length) + dcgi_lookup(DCGI_PLAYING|DCGI_QUEUE|DCGI_ENABLED|DCGI_RANDOM_ENABLED); + if(dcgi_playing + && dcgi_playing->state == playing_started /* i.e. not paused */ + && !disorder_length(dcgi_client, dcgi_playing->track, &length) && length - && playing->sofar >= 0) { + && dcgi_playing->sofar >= 0) { /* Try to put the next refresh at the start of the next track. */ time(&now); - fin = now + length - playing->sofar + config->gap; + fin = now + length - dcgi_playing->sofar + config->gap; if(now + refresh > fin) refresh = fin - now; } - if(queue && queue->state == playing_isscratch) { + if(dcgi_queue && dcgi_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(!playing - && ((queue - && queue->state != playing_random) - || random_enabled) - && enabled) { + if(!dcgi_playing + && ((dcgi_queue + && dcgi_queue->state != playing_random) + || dcgi_random_enabled) + && dcgi_enabled) { /* no track playing but playing is enabled and there is something coming * up, must be in a gap */ if(refresh > config->gap) @@ -146,9 +90,33 @@ static void act_playing(void) { "Refresh: %ld;url=%s\n" "%s\n" "\n", - refresh, url, cookie()) < 0) + refresh, url, dcgi_cookie_header()) < 0) fatal(errno, "error writing to stdout"); - disorder_cgi_expand(action ? action : "playing"); + dcgi_expand(action ? action : "playing"); +} + +static void act_disable(void) { + if(dcgi_client) + disorder_disable(dcgi_client); + redirect(0); +} + +static void act_enable(void) { + if(dcgi_client) + disorder_enable(dcgi_client); + redirect(0); +} + +static void act_random_disable(void) { + if(dcgi_client) + disorder_random_disable(dcgi_client); + redirect(0); +} + +static void act_random_enable(void) { + if(dcgi_client) + disorder_random_enable(dcgi_client); + redirect(0); } /** @brief Table of actions */ @@ -158,36 +126,18 @@ static const struct action { /** @brief Action handler */ void (*handler)(void); } actions[] = { -#if 0 - { "confirm", act_confirm }, { "disable", act_disable }, - { "edituser", act_edituser }, { "enable", act_enable }, - { "login", act_login }, - { "logout", act_logout }, { "manage", act_playing }, - { "move", act_move }, - { "pause", act_pause }, - { "play", act_play }, -#endif { "playing", act_playing }, -#if 0 - { "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 }, -#endif }; /** @brief Expand a template * @param name Base name of template, or NULL to consult CGI args */ -void disorder_cgi_expand(const char *name) { +void dcgi_expand(const char *name) { const char *p; /* For unknown actions check that they aren't evil */ @@ -206,7 +156,7 @@ void disorder_cgi_expand(const char *name) { * * If no recognized action is specified then 'playing' is assumed. */ -void disorder_cgi_action(const char *action) { +void dcgi_action(const char *action) { int n; /* Consult CGI args if caller had no view */ @@ -231,34 +181,20 @@ void disorder_cgi_action(const char *action) { /* Just expand the template */ if(printf("Content-Type: text/html\n" "%s\n" - "\n", cookie()) < 0) + "\n", dcgi_cookie_header()) < 0) fatal(errno, "error writing to stdout"); - disorder_cgi_expand(action); + dcgi_expand(action); } } /** @brief Generate an error page */ -void disorder_cgi_error(const char *msg, ...) { +void dcgi_error(const char *msg, ...) { va_list ap; va_start(ap, msg); - byte_xvasprintf(&error_string, msg, ap); + byte_xvasprintf(&dcgi_error_string, msg, ap); va_end(ap); - disorder_cgi_expand("error"); -} - -/** @brief Log in as the current user or guest if none */ -void disorder_cgi_login(void) { - /* Junk old data */ - lookup_reset(); - /* Reconnect */ - if(disorder_connect_cookie(client, login_cookie)) { - disorder_cgi_error("Cannot connect to server"); - exit(0); - } - /* If there was a cookie but it went bad, we forget it */ - if(login_cookie && !strcmp(disorder_user(client), "guest")) - login_cookie = 0; + dcgi_expand("error"); } /* diff --git a/server/actions.h b/server/actions.h deleted file mode 100644 index 5b58161..0000000 --- a/server/actions.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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 - */ -#ifndef ACTIONS_H -#define ACTIONS_H - -extern char *login_cookie; - -void disorder_cgi_expand(const char *name); -void disorder_cgi_action(const char *action); -void disorder_cgi_error(const char *msg, ...); -void disorder_cgi_login(void); - -#endif /* ACTIONS_H */ - -/* -Local Variables: -c-basic-offset:2 -comment-column:40 -fill-column:79 -indent-tabs-mode:nil -End: -*/ diff --git a/server/cgimain.c b/server/cgimain.c index 695fb70..d4882ab 100644 --- a/server/cgimain.c +++ b/server/cgimain.c @@ -21,34 +21,7 @@ * @brief DisOrder CGI */ -#include -#include "types.h" - -#include -#include -#include -#include -#include -#include -#include -#include - -#include "client.h" -#include "sink.h" -#include "hash.h" -#include "mem.h" -#include "log.h" -#include "configuration.h" -#include "disorder.h" -#include "api-client.h" -#include "mime.h" -#include "printf.h" -#include "url.h" -#include "macros.h" -#include "macros-disorder.h" -#include "cgi.h" -#include "actions.h" -#include "defs.h" +#include "disorder-cgi.h" /** @brief Return true if @p a is better than @p b * @@ -123,13 +96,13 @@ int main(int argc, char **argv) { best_cookie = n; } if(best_cookie != -1) - login_cookie = cd.cookies[best_cookie].value; + dcgi_cookie = cd.cookies[best_cookie].value; } else error(0, "could not parse cookie field '%s'", cookie_env); } /* Register expansions */ mx_register_builtin(); - register_disorder_expansions(); + dcgi_expansions(); /* Update search path. We look in the config directory first and the data * directory second, so that the latter overrides the former. */ mx_search_path(pkgconfdir); @@ -139,9 +112,9 @@ int main(int argc, char **argv) { fatal(errno, "error writing to stdout"); /* Create the initial connection, trying the cookie if we found a suitable * one. */ - disorder_cgi_login(); + dcgi_login(); /* The main program... */ - disorder_cgi_action(NULL); + dcgi_action(NULL); /* In practice if a write fails that probably means the web server went away, * but we log it anyway. */ if(fclose(stdout) < 0) diff --git a/server/disorder-cgi.h b/server/disorder-cgi.h new file mode 100644 index 0000000..44a75df --- /dev/null +++ b/server/disorder-cgi.h @@ -0,0 +1,115 @@ +/* + * 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 + */ +/** @file server/disorder-cgi.h + * @brief Shared header for DisOrder CGI program + */ + +#ifndef DISORDER_CGI_H +#define DISORDER_CGI_H + +#include +#include "types.h" + +#include +#include +#include +#include +#include +#include +#include + +#include "mem.h" +#include "kvp.h" +#include "queue.h" +#include "rights.h" +#include "sink.h" +#include "client.h" +#include "cgi.h" +#include "hash.h" +#include "macros.h" +#include "printf.h" +#include "defs.h" +#include "configuration.h" +#include "trackname.h" +#include "table.h" +#include "vector.h" +#include "url.h" +#include "log.h" +#include "inputline.h" +#include "split.h" +#include "mime.h" + +extern disorder_client *dcgi_client; +extern char *dcgi_cookie; +extern char *dcgi_error_string; + +void dcgi_expand(const char *name); +void dcgi_action(const char *action); +void dcgi_error(const char *msg, ...); +void dcgi_login(void); +void dcgi_lookup(unsigned want); +void dcgi_lookup_reset(void); +void dcgi_expansions(void); +char *dcgi_cookie_header(void); +void dcgi_login(void); + +void option_set(const char *name, const char *value); +const char *option_label(const char *key); +int option_label_exists(const char *key); +char **option_columns(const char *name, int *ncolumns); + +#define DCGI_QUEUE 0x0001 +#define DCGI_PLAYING 0x0002 +#define DCGI_RECENT 0x0004 +#define DCGI_VOLUME 0x0008 +#if 0 +#define DCGI_DIRS 0x0010 +#define DCGI_FILES 0x0020 +#endif +#define DCGI_NEW 0x0040 +#define DCGI_RIGHTS 0x0080 +#define DCGI_ENABLED 0x0100 +#define DCGI_RANDOM_ENABLED 0x0200 + +extern struct queue_entry *dcgi_queue; +extern struct queue_entry *dcgi_playing; +extern struct queue_entry *dcgi_recent; + +extern int dcgi_volume_left; +extern int dcgi_volume_right; + +extern char **dcgi_new; +extern int dcgi_nnew; + +extern rights_type dcgi_rights; + +extern int dcgi_enabled; +extern int dcgi_random_enabled; + +#endif /* DISORDER_CGI_H */ + +/* +Local Variables: +c-basic-offset:2 +comment-column:40 +fill-column:79 +indent-tabs-mode:nil +End: +*/ diff --git a/server/login.c b/server/login.c new file mode 100644 index 0000000..7000e90 --- /dev/null +++ b/server/login.c @@ -0,0 +1,95 @@ +/* + * This file is part of DisOrder. + * Copyright (C) 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 "disorder-cgi.h" + +/** @brief Client used by CGI + * + * The caller should arrange for this to be created before any of + * these expansions are used (if it cannot connect then it's safe to + * leave it as NULL). + */ +disorder_client *dcgi_client; + +/** @brief Login cookie */ +char *dcgi_cookie; + +/** @brief Return a Cookie: header */ +char *dcgi_cookie_header(void) { + struct dynstr d[1]; + struct url u; + char *s; + + memset(&u, 0, sizeof u); + dynstr_init(d); + parse_url(config->url, &u); + if(dcgi_cookie) { + dynstr_append_string(d, "disorder="); + dynstr_append_string(d, dcgi_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); + byte_xasprintf(&s, "Set-Cookie: %s", d->vec); + return s; +} + +/** @brief Log in as the current user or guest if none */ +void dcgi_login(void) { + /* Junk old data */ + dcgi_lookup_reset(); + /* Junk the old connection if there is one */ + if(dcgi_client) + disorder_close(dcgi_client); + /* Create a new connection */ + dcgi_client = disorder_new(0); + /* Reconnect */ + if(disorder_connect_cookie(dcgi_client, dcgi_cookie)) { + dcgi_error("Cannot connect to server"); + exit(0); + } + /* If there was a cookie but it went bad, we forget it */ + if(dcgi_cookie && !strcmp(disorder_user(dcgi_client), "guest")) + dcgi_cookie = 0; +} + +/* +Local Variables: +c-basic-offset:2 +comment-column:40 +fill-column:79 +indent-tabs-mode:nil +End: +*/ diff --git a/server/lookup.c b/server/lookup.c index 4005238..cb28f2c 100644 --- a/server/lookup.c +++ b/server/lookup.c @@ -17,119 +17,103 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA */ -/** @file server/macros-disorder.c - * @brief DisOrder-specific expansions - */ - -#include -#include "types.h" - -#include -#include - -#include "queue.h" -#include "sink.h" -#include "client.h" -#include "rights.h" -#include "lookup.h" -#include "cgi.h" - -/** @brief Client used by CGI +/** @file server/lookup.c + * @brief Server lookups * - * The caller should arrange for this to be created before any of - * these expansions are used (if it cannot connect then it's safe to - * leave it as NULL). + * To improve performance many server lookups are cached. */ -disorder_client *client; + +#include "disorder-cgi.h" /** @brief Cached data */ static unsigned flags; -struct queue_entry *queue; -struct queue_entry *playing; -struct queue_entry *recent; - -int volume_left; -int volume_right; - -char **files; -int nfiles; +struct queue_entry *dcgi_queue; +struct queue_entry *dcgi_playing; +struct queue_entry *dcgi_recent; -char **dirs; -int ndirs; +int dcgi_volume_left; +int dcgi_volume_right; -char **new; -int nnew; +char **dcgi_new; +int dcgi_nnew; -rights_type rights; +rights_type dcgi_rights; -int enabled; -int random_enabled; +int dcgi_enabled; +int dcgi_random_enabled; /** @brief Fetch cachable data */ -void lookup(unsigned want) { +void dcgi_lookup(unsigned want) { unsigned need = want ^ (flags & want); struct queue_entry *r, *rnext; +#if 0 const char *dir, *re; - char *rights_string; +#endif + char *rs; - if(!client || !need) + if(!dcgi_client || !need) return; - if(need & DC_QUEUE) - disorder_queue(client, &queue); - if(need & DC_PLAYING) - disorder_playing(client, &playing); - if(need & DC_NEW) - disorder_new_tracks(client, &new, &nnew, 0); - if(need & DC_RECENT) { + if(need & DCGI_QUEUE) + disorder_queue(dcgi_client, &dcgi_queue); + if(need & DCGI_PLAYING) + disorder_playing(dcgi_client, &dcgi_playing); + if(need & DCGI_NEW) + disorder_new_tracks(dcgi_client, &dcgi_new, &dcgi_nnew, 0); + if(need & DCGI_RECENT) { /* we need to reverse the order of the list */ - disorder_recent(client, &r); + disorder_recent(dcgi_client, &r); while(r) { rnext = r->next; - r->next = recent; - recent = r; + r->next = dcgi_recent; + dcgi_recent = r; r = rnext; } } - if(need & DC_VOLUME) - disorder_get_volume(client, - &volume_left, &volume_right); - /* DC_FILES and DC_DIRS are looking obsolete now */ - if(need & (DC_FILES|DC_DIRS)) { + if(need & DCGI_VOLUME) + disorder_get_volume(dcgi_client, + &dcgi_volume_left, &dcgi_volume_right); +#if 0 + /* DCGI_FILES and DCGI_DIRS are looking obsolete now */ + if(need & (DCGI_FILES|DCGI_DIRS)) { if(!(dir = cgi_get("directory"))) dir = ""; re = cgi_get("regexp"); - if(need & DC_DIRS) - if(disorder_directories(client, dir, re, + if(need & DCGI_DIRS) + if(disorder_directories(dcgi_client, dir, re, &dirs, &ndirs)) ndirs = 0; - if(need & DC_FILES) - if(disorder_files(client, dir, re, + if(need & DCGI_FILES) + if(disorder_files(dcgi_client, dir, re, &files, &nfiles)) nfiles = 0; } - if(need & DC_RIGHTS) { - rights = RIGHT_READ; /* fail-safe */ - if(!disorder_userinfo(client, disorder_user(client), - "rights", &rights_string)) - parse_rights(rights_string, &rights, 1); +#endif + if(need & DCGI_RIGHTS) { + dcgi_rights = RIGHT_READ; /* fail-safe */ + if(!disorder_userinfo(dcgi_client, disorder_user(dcgi_client), + "rights", &rs)) + parse_rights(rs, &dcgi_rights, 1); } - if(need & DC_ENABLED) - disorder_enabled(client, &enabled); - if(need & DC_RANDOM_ENABLED) - disorder_random_enabled(client, &random_enabled); - if(need & DC_RANDOM_ENABLED) + if(need & DCGI_ENABLED) + disorder_enabled(dcgi_client, &dcgi_enabled); + if(need & DCGI_RANDOM_ENABLED) + disorder_random_enabled(dcgi_client, &dcgi_random_enabled); flags |= need; } -void lookup_reset(void) { - /* Junk the old connection if there is one */ - if(client) - disorder_close(client); - /* Create a new connection */ - client = disorder_new(0); +void dcgi_lookup_reset(void) { /* Forget everything we knew */ flags = 0; + dcgi_recent = 0; + dcgi_queue = 0; + dcgi_playing = 0; + dcgi_rights = 0; + dcgi_new = 0; + dcgi_nnew = 0; + dcgi_enabled = 0; + dcgi_random_enabled = 0; + dcgi_volume_left = dcgi_volume_right = 0; } diff --git a/server/lookup.h b/server/lookup.h deleted file mode 100644 index 1e48455..0000000 --- a/server/lookup.h +++ /dev/null @@ -1,73 +0,0 @@ -/* - * This file is part of DisOrder. - * Copyright (C) 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 - */ -/** @file server/lookup.h - * @brief Server lookup code for CGI - */ - -#ifndef LOOKUP_H -#define LOOKUP_H - -extern disorder_client *client; - -#define DC_QUEUE 0x0001 -#define DC_PLAYING 0x0002 -#define DC_RECENT 0x0004 -#define DC_VOLUME 0x0008 -#define DC_DIRS 0x0010 -#define DC_FILES 0x0020 -#define DC_NEW 0x0040 -#define DC_RIGHTS 0x0080 -#define DC_ENABLED 0x0100 -#define DC_RANDOM_ENABLED 0x0200 - -extern struct queue_entry *queue; -extern struct queue_entry *playing; -extern struct queue_entry *recent; - -extern int volume_left; -extern int volume_right; - -extern char **files; -extern int nfiles; - -extern char **dirs; -extern int ndirs; - -extern char **new; -extern int nnew; - -extern rights_type rights; - -extern int enabled; -extern int random_enabled; - -void lookup(unsigned want); -void lookup_reset(void); - -#endif /* LOOKUP_H */ - -/* -Local Variables: -c-basic-offset:2 -comment-column:40 -fill-column:79 -indent-tabs-mode:nil -End: -*/ diff --git a/server/macros-disorder.c b/server/macros-disorder.c index 90c1392..bb920f4 100644 --- a/server/macros-disorder.c +++ b/server/macros-disorder.c @@ -21,45 +21,24 @@ * @brief DisOrder-specific expansions */ -#include -#include "types.h" - -#include -#include -#include -#include - -#include "kvp.h" -#include "queue.h" -#include "rights.h" -#include "sink.h" -#include "client.h" -#include "cgi.h" -#include "hash.h" -#include "macros.h" -#include "macros-disorder.h" -#include "lookup.h" -#include "printf.h" -#include "defs.h" -#include "configuration.h" -#include "trackname.h" +#include "disorder-cgi.h" /** @brief For error template */ -char *error_string; +char *dcgi_error_string; /** @brief Locate a track by ID */ static struct queue_entry *findtrack(const char *id) { struct queue_entry *q; - lookup(DC_PLAYING); - if(playing && !strcmp(playing->id, id)) - return playing; - lookup(DC_QUEUE); - for(q = queue; q; q = q->next) + dcgi_lookup(DCGI_PLAYING); + if(dcgi_playing && !strcmp(dcgi_playing->id, id)) + return dcgi_playing; + dcgi_lookup(DCGI_QUEUE); + for(q = dcgi_queue; q; q = q->next) if(!strcmp(q->id, id)) return q; - lookup(DC_RECENT); - for(q = recent; q; q = q->next) + dcgi_lookup(DCGI_RECENT); + for(q = dcgi_recent; q; q = q->next) if(!strcmp(q->id, id)) return q; return NULL; @@ -84,8 +63,8 @@ static int exp_server_version(int attribute((unused)) nargs, void attribute((unused)) *u) { const char *v; - if(client) { - if(disorder_version(client, (char **)&v)) + if(dcgi_client) { + if(disorder_version(dcgi_client, (char **)&v)) v = "(cannot get version)"; } else v = "(server not running)"; @@ -144,7 +123,7 @@ static int exp_user(int attribute((unused)) nargs, void attribute((unused)) *u) { const char *user; - if(client && (user = disorder_user(client))) + if(dcgi_client && (user = disorder_user(dcgi_client))) return sink_writes(output, cgi_sgmlquote(user)) < 0 ? -1 : 0; return 0; } @@ -177,8 +156,8 @@ static int exp_part(int nargs, else return 0; } - if(client - && !disorder_part(client, &s, + if(dcgi_client + && !disorder_part(dcgi_client, &s, track, !strcmp(context, "short") ? "display" : context, part)) @@ -277,7 +256,7 @@ static int exp_length(int attribute((unused)) nargs, return -1; name = q->track; } - if(client && disorder_length(client, name, &length)) + if(dcgi_client && disorder_length(dcgi_client, name, &length)) return sink_printf(output, "%ld:%02ld", length / 60, length % 60) < 0 ? -1 : 0; return sink_writes(output, " ") < 0 ? -1 : 0; @@ -295,12 +274,12 @@ static int exp_removable(int attribute((unused)) nargs, struct queue_entry *q = findtrack(args[0]); /* TODO would be better to reject recent */ - if(!q || !client) + if(!q || !dcgi_client) return mx_bool_result(output, 0); - lookup(DC_RIGHTS); + dcgi_lookup(DCGI_RIGHTS); return mx_bool_result(output, - (q == playing ? right_scratchable : right_removable) - (rights, disorder_user(client), q)); + (q == dcgi_playing ? right_scratchable : right_removable) + (dcgi_rights, disorder_user(dcgi_client), q)); } /* @movable{ID} @@ -314,11 +293,11 @@ static int exp_movable(int attribute((unused)) nargs, struct queue_entry *q = findtrack(args[0]); /* TODO would be better to recent playing/recent */ - if(!q || !client) + if(!q || !dcgi_client) return mx_bool_result(output, 0); - lookup(DC_RIGHTS); + dcgi_lookup(DCGI_RIGHTS); return mx_bool_result(output, - right_movable(rights, disorder_user(client), q)); + right_movable(dcgi_rights, disorder_user(dcgi_client), q)); } /* @playing{TEMPLATE} @@ -336,14 +315,14 @@ static int exp_playing(int nargs, const struct mx_node **args, struct sink *output, void *u) { - lookup(DC_PLAYING); - if(!playing) + dcgi_lookup(DCGI_PLAYING); + if(!dcgi_playing) return 0; if(!nargs) - return sink_writes(output, playing->id) < 0 ? -1 : 0; + return sink_writes(output, dcgi_playing->id) < 0 ? -1 : 0; return mx_expand(mx_rewritel(args[0], - "id", playing->id, - "track", playing->track, + "id", dcgi_playing->id, + "track", dcgi_playing->track, (char *)0), output, u); } @@ -365,14 +344,14 @@ static int exp_queue(int attribute((unused)) nargs, struct queue_entry *q; int rc, i; - lookup(DC_QUEUE); - for(q = queue, i = 0; q; q = q->next, ++i) + dcgi_lookup(DCGI_QUEUE); + for(q = dcgi_queue, i = 0; q; q = q->next, ++i) if((rc = mx_expand(mx_rewritel(args[0], "id", q->id, "track", q->track, "index", make_index(i), "parity", i % 2 ? "odd" : "even", - "first", q == queue ? "true" : "false", + "first", q == dcgi_queue ? "true" : "false", "last", q->next ? "false" : "true", (char *)0), output, u))) @@ -398,14 +377,14 @@ static int exp_recent(int attribute((unused)) nargs, struct queue_entry *q; int rc, i; - lookup(DC_RECENT); - for(q = recent, i = 0; q; q = q->next, ++i) + dcgi_lookup(DCGI_RECENT); + for(q = dcgi_recent, i = 0; q; q = q->next, ++i) if((rc = mx_expand(mx_rewritel(args[0], "id", q->id, "track", q->track, "index", make_index(i), "parity", i % 2 ? "odd" : "even", - "first", q == recent ? "true" : "false", + "first", q == dcgi_recent ? "true" : "false", "last", q->next ? "false" : "true", (char *)0), output, u))) @@ -432,15 +411,15 @@ static int exp_new(int attribute((unused)) nargs, void *u) { int rc, i; - lookup(DC_NEW); + dcgi_lookup(DCGI_NEW); /* TODO perhaps we should generate an ID value for tracks in the new list */ - for(i = 0; i < nnew; ++i) + for(i = 0; i < dcgi_nnew; ++i) if((rc = mx_expand(mx_rewritel(args[0], - "track", new[i], + "track", dcgi_new[i], "index", make_index(i), "parity", i % 2 ? "odd" : "even", "first", i == 0 ? "true" : "false", - "last", i == nnew - 1 ? "false" : "true", + "last", i == dcgi_nnew - 1 ? "false" : "true", (char *)0), output, u))) return rc; @@ -456,10 +435,10 @@ static int exp_volume(int attribute((unused)) nargs, char **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_VOLUME); + dcgi_lookup(DCGI_VOLUME); return sink_printf(output, "%d", !strcmp(args[0], "left") - ? volume_left : volume_right) < 0 ? -1 : 0; + ? dcgi_volume_left : dcgi_volume_right) < 0 ? -1 : 0; } /* @isplaying @@ -470,8 +449,8 @@ static int exp_isplaying(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_PLAYING); - return mx_bool_result(output, !!playing); + dcgi_lookup(DCGI_PLAYING); + return mx_bool_result(output, !!dcgi_playing); } /* @isqueue @@ -482,8 +461,8 @@ static int exp_isqueue(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_QUEUE); - return mx_bool_result(output, !!queue); + dcgi_lookup(DCGI_QUEUE); + return mx_bool_result(output, !!dcgi_queue); } /* @isrecent@ @@ -495,8 +474,8 @@ static int exp_isrecent(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_RECENT); - return mx_bool_result(output, !!recent); + dcgi_lookup(DCGI_RECENT); + return mx_bool_result(output, !!dcgi_recent); } /* @isnew @@ -508,8 +487,8 @@ static int exp_isnew(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_NEW); - return mx_bool_result(output, !!nnew); + dcgi_lookup(DCGI_NEW); + return mx_bool_result(output, !!dcgi_nnew); } /* @pref{TRACK}{KEY} @@ -522,7 +501,7 @@ static int exp_pref(int attribute((unused)) nargs, void attribute((unused)) *u) { char *value; - if(client && !disorder_get(client, args[0], args[1], &value)) + if(dcgi_client && !disorder_get(dcgi_client, args[0], args[1], &value)) return sink_writes(output, cgi_sgmlquote(value)) < 0 ? -1 : 0; return 0; } @@ -550,7 +529,7 @@ static int exp_prefs(int attribute((unused)) nargs, if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)"))) return rc; - if(!client || disorder_prefs(client, track, &head)) + if(!dcgi_client || disorder_prefs(dcgi_client, track, &head)) return 0; for(k = head, i = 0; k; k = k->next, ++i) if((rc = mx_expand(mx_rewritel(args[1], @@ -589,11 +568,11 @@ static int exp_enabled(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - int enabled = 0; + int e = 0; - if(client) - disorder_enabled(client, &enabled); - return mx_bool_result(output, enabled); + if(dcgi_client) + disorder_enabled(dcgi_client, &e); + return mx_bool_result(output, e); } /* @random-enabled @@ -604,11 +583,11 @@ static int exp_random_enabled(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - int enabled = 0; + int e = 0; - if(client) - disorder_random_enabled(client, &enabled); - return mx_bool_result(output, enabled); + if(dcgi_client) + disorder_random_enabled(dcgi_client, &e); + return mx_bool_result(output, e); } /* @trackstate{TRACK} @@ -623,15 +602,15 @@ static int exp_trackstate(int attribute((unused)) nargs, char *track; struct queue_entry *q; - if(!client) + if(!dcgi_client) return 0; - if(disorder_resolve(client, &track, args[0])) + if(disorder_resolve(dcgi_client, &track, args[0])) return 0; - lookup(DC_PLAYING); - if(playing && !strcmp(track, playing->track)) + dcgi_lookup(DCGI_PLAYING); + if(dcgi_playing && !strcmp(track, dcgi_playing->track)) return sink_writes(output, "playing") < 0 ? -1 : 0; - lookup(DC_QUEUE); - for(q = queue; q; q = q->next) + dcgi_lookup(DCGI_QUEUE); + for(q = dcgi_queue; q; q = q->next) if(!strcmp(track, q->track)) return sink_writes(output, "queued") < 0 ? -1 : 0; return 0; @@ -661,7 +640,7 @@ static int exp_resolve(int attribute((unused)) nargs, void attribute((unused)) *u) { char *r; - if(client && !disorder_resolve(client, &r, args[0])) + if(dcgi_client && !disorder_resolve(dcgi_client, &r, args[0])) return sink_writes(output, r) < 0 ? -1 : 0; return 0; } @@ -675,8 +654,9 @@ static int exp_paused(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - lookup(DC_PLAYING); - return mx_bool_result(output, playing && playing->state == playing_paused); + dcgi_lookup(DCGI_PLAYING); + return mx_bool_result(output, (dcgi_playing + && dcgi_playing->state == playing_paused)); } /* @state{ID}@ @@ -713,18 +693,18 @@ static int exp_right(int nargs, rights_type r; int rc; - if(!client) + if(!dcgi_client) return 0; - lookup(DC_RIGHTS); + dcgi_lookup(DCGI_RIGHTS); if((rc = mx_expandstr(args[0], &right, u, "argument #0 (RIGHT)"))) return rc; if(parse_rights(right, &r, 1/*report*/)) return 0; /* Single-argument form */ if(nargs == 1) - return mx_bool_result(output, !!(r & rights)); + return mx_bool_result(output, !!(r & dcgi_rights)); /* Multiple argument form */ - if(r & rights) + if(r & dcgi_rights) return mx_expand(args[1], output, u); if(nargs == 3) return mx_expand(args[2], output, u); @@ -741,7 +721,9 @@ static int exp_userinfo(int attribute((unused)) nargs, void attribute((unused)) *u) { char *v; - if(client && !disorder_userinfo(client, disorder_user(client), args[0], &v)) + if(dcgi_client + && !disorder_userinfo(dcgi_client, disorder_user(dcgi_client), + args[0], &v)) return sink_writes(output, v) < 0 ? -1 : 0; return 0; } @@ -754,11 +736,11 @@ static int exp_error(int attribute((unused)) nargs, char attribute((unused)) **args, struct sink *output, void attribute((unused)) *u) { - return sink_writes(output, cgi_sgmlquote(error_string)) < 0 ? -1 : 0; + return sink_writes(output, cgi_sgmlquote(dcgi_error_string)) < 0 ? -1 : 0; } /** @brief Register DisOrder-specific expansions */ -void register_disorder_expansions(void) { +void dcgi_expansions(void) { mx_register("arg", 1, 1, exp_arg); mx_register("enabled", 0, 0, exp_enabled); mx_register("error", 0, 0, exp_error); diff --git a/server/macros-disorder.h b/server/macros-disorder.h deleted file mode 100644 index 1dc8ba3..0000000 --- a/server/macros-disorder.h +++ /dev/null @@ -1,40 +0,0 @@ -/* - * This file is part of DisOrder. - * Copyright (C) 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 - */ -/** @file server/macros-disorder.h - * @brief DisOrder-specific expansions - */ - -#ifndef MACROS_DISORDER_H -#define MACROS_DISORDER_H - -extern disorder_client *client; -extern char *error_string; -void register_disorder_expansions(void); - -#endif /* MACROS_DISORDER_H */ - -/* -Local Variables: -c-basic-offset:2 -comment-column:40 -fill-column:79 -indent-tabs-mode:nil -End: -*/ diff --git a/server/options.c b/server/options.c index e3cf7b8..9185610 100644 --- a/server/options.c +++ b/server/options.c @@ -19,25 +19,12 @@ */ /** @file server/options.c * @brief CGI options + * + * Options represent an additional configuration system private to the + * CGI program. */ -#include -#include "types.h" - -#include -#include -#include -#include - -#include "mem.h" -#include "hash.h" -#include "macros.h" -#include "options.h" -#include "split.h" -#include "table.h" -#include "log.h" -#include "inputline.h" -#include "printf.h" +#include "disorder-cgi.h" struct column { int ncolumns; diff --git a/server/options.h b/server/options.h deleted file mode 100644 index 0f16ff5..0000000 --- a/server/options.h +++ /dev/null @@ -1,39 +0,0 @@ -/* - * This file is part of DisOrder. - * Copyright (C) 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 - */ -/** @file server/options.ch - * @brief CGI options - */ - -#ifndef OPTIONS_H -#define OPTIONS_H - -void option_set(const char *name, const char *value); -const char *option_label(const char *key); -int option_label_exists(const char *key); -char **option_columns(const char *name, int *ncolumns); - -#endif /* OPTIONS_H */ - -/* -Local Variables: -c-basic-offset:2 -comment-column:40 -End: -*/ diff --git a/templates/volume.html b/templates/volume.tmpl similarity index 100% rename from templates/volume.html rename to templates/volume.tmpl -- [mdw]