chiark / gitweb /
a504ed54647bf0045d80eb9bdab615574f8fb245
[disorder] / server / actions.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004-2008 Richard Kettlewell
4  *
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.
9  *
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.
14  *
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
18  * USA
19  */
20 /** @file server/actions.c
21  * @brief DisOrder web actions
22  *
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.
27  */
28
29 #include "disorder-cgi.h"
30
31 /** @brief Redirect to some other action or URL */
32 static void redirect(const char *url) {
33   /* By default use the 'back' argument */
34   if(!url)
35     url = cgi_get("back");
36   if(url) {
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);
40   } else {
41     /* If back= is not set just go back to the front page */
42     url = config->url;
43   }
44   if(printf("Location: %s\n"
45             "%s\n"
46             "\n", url, dcgi_cookie_header()) < 0)
47     fatal(errno, "error writing to stdout");
48 }
49
50 /* 'playing' and 'manage' just add a Refresh: header */
51 static void act_playing(void) {
52   long refresh = config->refresh;
53   long length;
54   time_t now, fin;
55   char *url;
56   const char *action;
57
58   dcgi_lookup(DCGI_PLAYING|DCGI_QUEUE|DCGI_ENABLED|DCGI_RANDOM_ENABLED);
59   if(dcgi_playing
60      && dcgi_playing->state == playing_started /* i.e. not paused */
61      && !disorder_length(dcgi_client, dcgi_playing->track, &length)
62      && length
63      && dcgi_playing->sofar >= 0) {
64     /* Try to put the next refresh at the start of the next track. */
65     time(&now);
66     fin = now + length - dcgi_playing->sofar + config->gap;
67     if(now + refresh > fin)
68       refresh = fin - now;
69   }
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;
74   }
75   if(!dcgi_playing
76      && ((dcgi_queue
77           && dcgi_queue->state != playing_random)
78          || dcgi_random_enabled)
79      && dcgi_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;
84   }
85   if((action = cgi_get("action")))
86     url = cgi_makeurl(config->url, "action", action, (char *)0);
87   else
88     url = config->url;
89   if(printf("Content-Type: text/html\n"
90             "Refresh: %ld;url=%s\n"
91             "%s\n"
92             "\n",
93             refresh, url, dcgi_cookie_header()) < 0)
94     fatal(errno, "error writing to stdout");
95   dcgi_expand("playing");
96 }
97
98 static void act_disable(void) {
99   if(dcgi_client)
100     disorder_disable(dcgi_client);
101   redirect(0);
102 }
103
104 static void act_enable(void) {
105   if(dcgi_client)
106     disorder_enable(dcgi_client);
107   redirect(0);
108 }
109
110 static void act_random_disable(void) {
111   if(dcgi_client)
112     disorder_random_disable(dcgi_client);
113   redirect(0);
114 }
115
116 static void act_random_enable(void) {
117   if(dcgi_client)
118     disorder_random_enable(dcgi_client);
119   redirect(0);
120 }
121
122 static void act_remove(void) {
123   const char *id;
124   struct queue_entry *q;
125
126   if(dcgi_client) {
127     if(!(id = cgi_get("id")))
128       error(0, "missing 'id' argument");
129     else if(!(q = dcgi_findtrack(id)))
130       error(0, "unknown queue id %s", id);
131     else switch(q->state) {
132     case playing_isscratch:
133     case playing_failed:
134     case playing_no_player:
135     case playing_ok:
136     case playing_quitting:
137     case playing_scratched:
138       error(0, "does not make sense to scratch %s", id);
139       break;
140     case playing_paused:                /* started but paused */
141     case playing_started:               /* started to play */
142       disorder_scratch(dcgi_client, id);
143       break;
144     case playing_random:                /* unplayed randomly chosen track */
145     case playing_unplayed:              /* haven't played this track yet */
146       disorder_remove(dcgi_client, id);
147       break;
148     }
149   }
150   redirect(0);
151 }
152
153 /** @brief Table of actions */
154 static const struct action {
155   /** @brief Action name */
156   const char *name;
157   /** @brief Action handler */
158   void (*handler)(void);
159 } actions[] = {
160   { "disable", act_disable },
161   { "enable", act_enable },
162   { "manage", act_playing },
163   { "playing", act_playing },
164   { "randomdisable", act_random_disable },
165   { "randomenable", act_random_enable },
166   { "remove", act_remove },
167 };
168
169 /** @brief Check that an action name is valid
170  * @param name Action
171  * @return 1 if valid, 0 if not
172  */
173 static int dcgi_valid_action(const char *name) {
174   int c;
175
176   /* First character must be letter or digit (this also requires there to _be_
177    * a first character) */
178   if(!isalnum((unsigned char)*name))
179     return 0;
180   /* Only letters, digits, '.' and '-' allowed */
181   while((c = (unsigned char)*name++)) {
182     if(!(isalnum(c)
183          || c == '.'
184          || c == '_'))
185       return 0;
186   }
187   return 1;
188 }
189
190 /** @brief Expand a template
191  * @param name Base name of template, or NULL to consult CGI args
192  */
193 void dcgi_expand(const char *name) {
194   const char *p, *found;
195
196   /* Parse macros first */
197   if((found = mx_find("macros.tmpl")))
198     mx_expand_file(found, sink_discard(), 0);
199   /* For unknown actions check that they aren't evil */
200   if(!dcgi_valid_action(name))
201     fatal(0, "invalid action name '%s'", name);
202   byte_xasprintf((char **)&p, "%s.tmpl", name);
203   if(!(found = mx_find(p)))
204     fatal(errno, "cannot find %s", p);
205   if(mx_expand_file(found, sink_stdio("stdout", stdout), 0) == -1
206      || fflush(stdout) < 0)
207     fatal(errno, "error writing to stdout");
208 }
209
210 /** @brief Execute a web action
211  * @param action Action to perform, or NULL to consult CGI args
212  *
213  * If no recognized action is specified then 'playing' is assumed.
214  */
215 void dcgi_action(const char *action) {
216   int n;
217
218   /* Consult CGI args if caller had no view */
219   if(!action)
220     action = cgi_get("action");
221   /* Pick a default if nobody cares at all */
222   if(!action) {
223     /* We allow URLs which are just c=... in order to keep confirmation URLs,
224      * which are user-facing, as short as possible.  Actually we could lose the
225      * c= for this... */
226     if(cgi_get("c"))
227       action = "confirm";
228     else
229       action = "playing";
230     /* Make sure 'action' is always set */
231     cgi_set("action", action);
232   }
233   if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
234     /* Its a known action */
235     actions[n].handler();
236   else {
237     /* Just expand the template */
238     if(printf("Content-Type: text/html\n"
239               "%s\n"
240               "\n", dcgi_cookie_header()) < 0)
241       fatal(errno, "error writing to stdout");
242     dcgi_expand(action);
243   }
244 }
245
246 /** @brief Generate an error page */
247 void dcgi_error(const char *key) {
248   dcgi_error_string = xstrdup(key);
249   dcgi_expand("error");
250 }
251
252 /*
253 Local Variables:
254 c-basic-offset:2
255 comment-column:40
256 fill-column:79
257 indent-tabs-mode:nil
258 End:
259 */