2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 /** @file server/actions.c
21 * @brief DisOrder web actions
23 * Actions are anything that the web interface does beyond passive template
24 * expansion and inspection of state recieved from the server. This means
25 * playing tracks, editing prefs etc but also setting extra headers e.g. to
26 * auto-refresh the playing list.
29 #include "disorder-cgi.h"
31 /** @brief Redirect to some other action or URL */
32 static void redirect(const char *url) {
33 /* By default use the 'back' argument */
35 url = cgi_get("back");
37 if(!strncmp(url, "http", 4))
38 /* If the target is not a full URL assume it's the action */
39 url = cgi_makeurl(config->url, "action", url, (char *)0);
41 /* If back= is not set just go back to the front page */
44 if(printf("Location: %s\n"
46 "\n", url, dcgi_cookie_header()) < 0)
47 fatal(errno, "error writing to stdout");
50 /* 'playing' and 'manage' just add a Refresh: header */
51 static void act_playing(void) {
52 long refresh = config->refresh;
58 dcgi_lookup(DCGI_PLAYING|DCGI_QUEUE|DCGI_ENABLED|DCGI_RANDOM_ENABLED);
60 && dcgi_playing->state == playing_started /* i.e. not paused */
61 && !disorder_length(dcgi_client, dcgi_playing->track, &length)
63 && dcgi_playing->sofar >= 0) {
64 /* Try to put the next refresh at the start of the next track. */
66 fin = now + length - dcgi_playing->sofar + config->gap;
67 if(now + refresh > fin)
70 if(dcgi_queue && dcgi_queue->state == playing_isscratch) {
71 /* next track is a scratch, don't leave more than the inter-track gap */
72 if(refresh > config->gap)
73 refresh = config->gap;
77 && dcgi_queue->state != playing_random)
78 || dcgi_random_enabled)
80 /* no track playing but playing is enabled and there is something coming
81 * up, must be in a gap */
82 if(refresh > config->gap)
83 refresh = config->gap;
85 if((action = cgi_get("action")))
86 url = cgi_makeurl(config->url, "action", action, (char *)0);
89 if(printf("Content-Type: text/html\n"
90 "Refresh: %ld;url=%s\n"
93 refresh, url, dcgi_cookie_header()) < 0)
94 fatal(errno, "error writing to stdout");
95 dcgi_expand(action ? action : "playing");
98 static void act_disable(void) {
100 disorder_disable(dcgi_client);
104 static void act_enable(void) {
106 disorder_enable(dcgi_client);
110 static void act_random_disable(void) {
112 disorder_random_disable(dcgi_client);
116 static void act_random_enable(void) {
118 disorder_random_enable(dcgi_client);
122 /** @brief Table of actions */
123 static const struct action {
124 /** @brief Action name */
126 /** @brief Action handler */
127 void (*handler)(void);
129 { "disable", act_disable },
130 { "enable", act_enable },
131 { "manage", act_playing },
132 { "playing", act_playing },
133 { "random-disable", act_random_disable },
134 { "random-enable", act_random_enable },
137 /** @brief Expand a template
138 * @param name Base name of template, or NULL to consult CGI args
140 void dcgi_expand(const char *name) {
143 /* For unknown actions check that they aren't evil */
144 for(p = name; *p && isalnum((unsigned char)*p); ++p)
147 fatal(0, "invalid action name '%s'", name);
148 byte_xasprintf((char **)&p, "%s.tmpl", name);
149 if(mx_expand_file(p, sink_stdio("stdout", stdout), 0) == -1
150 || fflush(stdout) < 0)
151 fatal(errno, "error writing to stdout");
154 /** @brief Execute a web action
155 * @param action Action to perform, or NULL to consult CGI args
157 * If no recognized action is specified then 'playing' is assumed.
159 void dcgi_action(const char *action) {
162 /* Consult CGI args if caller had no view */
164 action = cgi_get("action");
165 /* Pick a default if nobody cares at all */
167 /* We allow URLs which are just c=... in order to keep confirmation URLs,
168 * which are user-facing, as short as possible. Actually we could lose the
174 /* Make sure 'action' is always set */
175 cgi_set("action", action);
177 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
178 /* Its a known action */
179 actions[n].handler();
181 /* Just expand the template */
182 if(printf("Content-Type: text/html\n"
184 "\n", dcgi_cookie_header()) < 0)
185 fatal(errno, "error writing to stdout");
190 /** @brief Generate an error page */
191 void dcgi_error(const char *msg, ...) {
195 byte_xvasprintf(&dcgi_error_string, msg, ap);
197 dcgi_expand("error");