chiark / gitweb /
Add a new 'origin' field to queue entries. This records the origin of
[disorder] / cgi / actions.c
CommitLineData
bca4e2b7
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
bca4e2b7 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
bca4e2b7
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
bca4e2b7 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
bca4e2b7 17 */
59cf25c4 18/** @file cgi/actions.c
1e97629d
RK
19 * @brief DisOrder web actions
20 *
21 * Actions are anything that the web interface does beyond passive template
22 * expansion and inspection of state recieved from the server. This means
23 * playing tracks, editing prefs etc but also setting extra headers e.g. to
24 * auto-refresh the playing list.
59cf25c4
RK
25 *
26 * See @ref lib/macros-builtin.c for docstring syntax.
1e97629d 27 */
bca4e2b7 28
1e97629d 29#include "disorder-cgi.h"
5a7df048
RK
30
31/** @brief Redirect to some other action or URL */
32static void redirect(const char *url) {
33 /* By default use the 'back' argument */
34 if(!url)
71634563 35 url = cgi_get("back");
04aa2c4f 36 if(url && *url) {
e7ce7665 37 if(strncmp(url, "http", 4))
5a7df048
RK
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"
1e97629d 46 "\n", url, dcgi_cookie_header()) < 0)
5a7df048
RK
47 fatal(errno, "error writing to stdout");
48}
49
59cf25c4 50/*$ playing
5c1ae3bc
RK
51 *
52 * Expands \fIplaying.tmpl\fR as if there was no special 'playing' action, but
53 * adds a Refresh: field to the HTTP header. The maximum refresh interval is
54 * defined by \fBrefresh\fR (see \fBdisorder_config\fR(5)) but may be less if
55 * the end of the track is near.
56 */
59cf25c4 57/*$ manage
5c1ae3bc
RK
58 *
59 * Expands \fIplaying.tmpl\fR (NB not \fImanage.tmpl\fR) as if there was no
60 * special 'playing' action, and adds a Refresh: field to the HTTP header. The
61 * maximum refresh interval is defined by \Bfrefresh\fR (see
62 * \fBdisorder_config\fR(5)) but may be less if the end of the track is near.
63 */
448d3570
RK
64static void act_playing(void) {
65 long refresh = config->refresh;
66 long length;
67 time_t now, fin;
68 char *url;
71634563 69 const char *action;
448d3570 70
1e97629d
RK
71 dcgi_lookup(DCGI_PLAYING|DCGI_QUEUE|DCGI_ENABLED|DCGI_RANDOM_ENABLED);
72 if(dcgi_playing
73 && dcgi_playing->state == playing_started /* i.e. not paused */
74 && !disorder_length(dcgi_client, dcgi_playing->track, &length)
448d3570 75 && length
1e97629d 76 && dcgi_playing->sofar >= 0) {
448d3570
RK
77 /* Try to put the next refresh at the start of the next track. */
78 time(&now);
1e97629d 79 fin = now + length - dcgi_playing->sofar + config->gap;
448d3570
RK
80 if(now + refresh > fin)
81 refresh = fin - now;
82 }
1e97629d 83 if(dcgi_queue && dcgi_queue->state == playing_isscratch) {
448d3570
RK
84 /* next track is a scratch, don't leave more than the inter-track gap */
85 if(refresh > config->gap)
86 refresh = config->gap;
87 }
1e97629d
RK
88 if(!dcgi_playing
89 && ((dcgi_queue
2dc2f478 90 && dcgi_queue->origin != origin_random)
1e97629d
RK
91 || dcgi_random_enabled)
92 && dcgi_enabled) {
448d3570
RK
93 /* no track playing but playing is enabled and there is something coming
94 * up, must be in a gap */
95 if(refresh > config->gap)
96 refresh = config->gap;
97 }
98 if((action = cgi_get("action")))
99 url = cgi_makeurl(config->url, "action", action, (char *)0);
100 else
101 url = config->url;
e7ce7665
RK
102 if(printf("Refresh: %ld;url=%s\n",
103 refresh, url) < 0)
448d3570 104 fatal(errno, "error writing to stdout");
e7ce7665 105 dcgi_expand("playing", 1);
1e97629d
RK
106}
107
59cf25c4 108/*$ disable
5c1ae3bc
RK
109 *
110 * Disables play.
111 */
1e97629d
RK
112static void act_disable(void) {
113 if(dcgi_client)
114 disorder_disable(dcgi_client);
115 redirect(0);
116}
117
59cf25c4 118/*$ enable
5c1ae3bc
RK
119 *
120 * Enables play.
121 */
1e97629d
RK
122static void act_enable(void) {
123 if(dcgi_client)
124 disorder_enable(dcgi_client);
125 redirect(0);
126}
127
59cf25c4 128/*$ random-disable
5c1ae3bc
RK
129 *
130 * Disables random play.
131 */
1e97629d
RK
132static void act_random_disable(void) {
133 if(dcgi_client)
134 disorder_random_disable(dcgi_client);
135 redirect(0);
136}
137
59cf25c4 138/*$ random-enable
5c1ae3bc
RK
139 *
140 * Enables random play.
141 */
1e97629d
RK
142static void act_random_enable(void) {
143 if(dcgi_client)
144 disorder_random_enable(dcgi_client);
145 redirect(0);
448d3570
RK
146}
147
59cf25c4 148/*$ pause
5c1ae3bc
RK
149 *
150 * Pauses the current track (if there is one and it's not paused already).
151 */
6d9dd8d9
RK
152static void act_pause(void) {
153 if(dcgi_client)
154 disorder_pause(dcgi_client);
155 redirect(0);
156}
157
59cf25c4 158/*$ resume
5c1ae3bc
RK
159 *
160 * Resumes the current track (if there is one and it's paused).
161 */
6d9dd8d9
RK
162static void act_resume(void) {
163 if(dcgi_client)
164 disorder_resume(dcgi_client);
165 redirect(0);
166}
167
59cf25c4 168/*$ remove
5c1ae3bc
RK
169 *
170 * Removes the track given by the \fBid\fR argument. If this is the currently
171 * playing track then it is scratched.
172 */
a2c4ad5f
RK
173static void act_remove(void) {
174 const char *id;
175 struct queue_entry *q;
176
177 if(dcgi_client) {
178 if(!(id = cgi_get("id")))
179 error(0, "missing 'id' argument");
180 else if(!(q = dcgi_findtrack(id)))
181 error(0, "unknown queue id %s", id);
182 else switch(q->state) {
183 case playing_isscratch:
184 case playing_failed:
185 case playing_no_player:
186 case playing_ok:
187 case playing_quitting:
188 case playing_scratched:
189 error(0, "does not make sense to scratch %s", id);
190 break;
191 case playing_paused: /* started but paused */
192 case playing_started: /* started to play */
193 disorder_scratch(dcgi_client, id);
194 break;
195 case playing_random: /* unplayed randomly chosen track */
196 case playing_unplayed: /* haven't played this track yet */
197 disorder_remove(dcgi_client, id);
198 break;
199 }
200 }
201 redirect(0);
202}
203
59cf25c4 204/*$ move
5c1ae3bc
RK
205 *
206 * Moves the track given by the \fBid\fR argument the distance given by the
207 * \fBdelta\fR argument. If this is positive the track is moved earlier in the
208 * queue and if negative, later.
209 */
6d9dd8d9
RK
210static void act_move(void) {
211 const char *id, *delta;
212 struct queue_entry *q;
213
214 if(dcgi_client) {
215 if(!(id = cgi_get("id")))
216 error(0, "missing 'id' argument");
217 else if(!(delta = cgi_get("delta")))
218 error(0, "missing 'delta' argument");
219 else if(!(q = dcgi_findtrack(id)))
220 error(0, "unknown queue id %s", id);
221 else switch(q->state) {
222 case playing_random: /* unplayed randomly chosen track */
223 case playing_unplayed: /* haven't played this track yet */
224 disorder_move(dcgi_client, id, atol(delta));
225 break;
226 default:
227 error(0, "does not make sense to scratch %s", id);
228 break;
229 }
230 }
231 redirect(0);
232}
233
59cf25c4 234/*$ play
5c1ae3bc
RK
235 *
236 * Play the track given by the \fBtrack\fR argument, or if that is not set all
237 * the tracks in the directory given by the \fBdir\fR argument.
238 */
6d9dd8d9
RK
239static void act_play(void) {
240 const char *track, *dir;
241 char **tracks;
242 int ntracks, n;
e13809e4 243 struct tracksort_data *tsd;
6d9dd8d9
RK
244
245 if(dcgi_client) {
02eaa49d 246 if((track = cgi_get("track"))) {
6d9dd8d9
RK
247 disorder_play(dcgi_client, track);
248 } else if((dir = cgi_get("dir"))) {
249 if(disorder_files(dcgi_client, dir, 0, &tracks, &ntracks))
250 ntracks = 0;
e13809e4 251 tsd = tracksort_init(ntracks, tracks, "track");
6d9dd8d9 252 for(n = 0; n < ntracks; ++n)
e13809e4 253 disorder_play(dcgi_client, tsd[n].track);
6d9dd8d9
RK
254 }
255 }
256 redirect(0);
257}
258
259static int clamp(int n, int min, int max) {
260 if(n < min)
261 return min;
262 if(n > max)
263 return max;
264 return n;
265}
266
59cf25c4 267/*$ volume
5c1ae3bc
RK
268 *
269 * If the \fBdelta\fR argument is set: adjust both channels by that amount (up
270 * if positive, down if negative).
271 *
272 * Otherwise if \fBleft\fR and \fBright\fR are set, set the channels
273 * independently to those values.
274 */
6d9dd8d9
RK
275static void act_volume(void) {
276 const char *l, *r, *d;
277 int nd;
278
279 if(dcgi_client) {
280 if((d = cgi_get("delta"))) {
281 dcgi_lookup(DCGI_VOLUME);
282 nd = clamp(atoi(d), -255, 255);
283 disorder_set_volume(dcgi_client,
284 clamp(dcgi_volume_left + nd, 0, 255),
285 clamp(dcgi_volume_right + nd, 0, 255));
286 } else if((l = cgi_get("left")) && (r = cgi_get("right")))
287 disorder_set_volume(dcgi_client, atoi(l), atoi(r));
288 }
289 redirect(0);
290}
291
e7ce7665 292/** @brief Expand the login template with @b @@error set to @p error
b7e452c7 293 * @param e Error keyword
e7ce7665 294 */
b7e452c7
RK
295static void login_error(const char *e) {
296 dcgi_error_string = e;
e7ce7665
RK
297 dcgi_expand("login", 1);
298}
299
300/** @brief Log in
301 * @param username Login name
302 * @param password Password
303 * @return 0 on success, non-0 on error
304 *
305 * On error, calls login_error() to expand the login template.
306 */
307static int login_as(const char *username, const char *password) {
308 disorder_client *c;
309
310 if(dcgi_cookie && dcgi_client)
311 disorder_revoke(dcgi_client);
312 /* We'll need a new connection as we are going to stop being guest */
313 c = disorder_new(0);
314 if(disorder_connect_user(c, username, password)) {
315 login_error("loginfailed");
316 return -1;
317 }
318 /* Generate a cookie so we can log in again later */
319 if(disorder_make_cookie(c, &dcgi_cookie)) {
320 login_error("cookiefailed");
321 return -1;
322 }
323 /* Use the new connection henceforth */
324 dcgi_client = c;
325 dcgi_lookup_reset();
326 return 0; /* OK */
327}
328
59cf25c4 329/*$ login
5c1ae3bc
RK
330 *
331 * If \fBusername\fR and \fBpassword\fR are set (and the username isn't
332 * "guest") then attempt to log in using those credentials. On success,
333 * redirects to the \fBback\fR argument if that is set, or just expands
334 * \fIlogin.tmpl\fI otherwise, with \fB@status\fR set to \fBloginok\fR.
335 *
336 * If they aren't set then just expands \fIlogin.tmpl\fI.
337 */
e7ce7665
RK
338static void act_login(void) {
339 const char *username, *password;
340
341 /* We try all this even if not connected since the subsequent connection may
342 * succeed. */
343
344 username = cgi_get("username");
345 password = cgi_get("password");
346 if(!username
347 || !password
348 || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) {
349 /* We're just visiting the login page, not performing an action at all. */
350 dcgi_expand("login", 1);
351 return;
352 }
353 if(!login_as(username, password)) {
354 /* Report the succesful login */
355 dcgi_status_string = "loginok";
2cc4c0ef
RK
356 /* Redirect back to where we came from, if necessary */
357 if(cgi_get("back"))
358 redirect(0);
359 else
360 dcgi_expand("login", 1);
e7ce7665
RK
361 }
362}
363
59cf25c4 364/*$ logout
5c1ae3bc
RK
365 *
366 * Logs out the current user and expands \fIlogin.tmpl\fR with \fBstatus\fR or
367 * \fB@error\fR set according to the result.
368 */
e7ce7665
RK
369static void act_logout(void) {
370 if(dcgi_client) {
371 /* Ask the server to revoke the cookie */
372 if(!disorder_revoke(dcgi_client))
373 dcgi_status_string = "logoutok";
374 else
375 dcgi_error_string = "revokefailed";
376 } else {
377 /* We can't guarantee a logout if we can't connect to the server to revoke
378 * the cookie, so we report an error. We'll still ask the browser to
379 * forget the cookie though. */
380 dcgi_error_string = "connect";
381 }
382 /* Attempt to reconnect without the cookie */
383 dcgi_cookie = 0;
384 dcgi_login();
385 /* Back to login page, hopefuly forcing the browser to forget the cookie. */
386 dcgi_expand("login", 1);
387}
388
59cf25c4 389/*$ register
5c1ae3bc
RK
390 *
391 * Register a new user using \fBusername\fR, \fBpassword1\fR, \fBpassword2\fR
392 * and \fBemail\fR and expands \fIlogin.tmpl\fR with \fBstatus\fR or
393 * \fB@error\fR set according to the result.
394 */
e7ce7665
RK
395static void act_register(void) {
396 const char *username, *password, *password2, *email;
397 char *confirm, *content_type;
398 const char *text, *encoding, *charset;
399
400 /* If we're not connected then this is a hopeless exercise */
401 if(!dcgi_client) {
402 login_error("connect");
403 return;
404 }
405
406 /* Collect arguments */
407 username = cgi_get("username");
408 password = cgi_get("password1");
409 password2 = cgi_get("password2");
410 email = cgi_get("email");
411
412 /* Verify arguments */
413 if(!username || !*username) {
414 login_error("nousername");
415 return;
416 }
417 if(!password || !*password) {
418 login_error("nopassword");
419 return;
420 }
421 if(!password2 || !*password2 || strcmp(password, password2)) {
422 login_error("passwordmismatch");
423 return;
424 }
425 if(!email || !*email) {
426 login_error("noemail");
427 return;
428 }
429 /* We could well do better address validation but for now we'll just do the
5f2d537c 430 * minimum */
33e95f03 431 if(!email_valid(email)) {
e7ce7665
RK
432 login_error("bademail");
433 return;
434 }
435 if(disorder_register(dcgi_client, username, password, email, &confirm)) {
436 login_error("cannotregister");
437 return;
438 }
439 /* Send the user a mail */
440 /* TODO templatize this */
441 byte_xasprintf((char **)&text,
442 "Welcome to DisOrder. To active your login, please visit this URL:\n"
443 "\n"
444 "%s?c=%s\n", config->url, urlencodestring(confirm));
445 if(!(text = mime_encode_text(text, &charset, &encoding)))
446 fatal(0, "cannot encode email");
447 byte_xasprintf(&content_type, "text/plain;charset=%s",
448 quote822(charset, 0));
449 sendmail("", config->mail_sender, email, "Welcome to DisOrder",
450 encoding, content_type, text); /* TODO error checking */
451 /* We'll go back to the login page with a suitable message */
452 dcgi_status_string = "registered";
453 dcgi_expand("login", 1);
454}
455
59cf25c4 456/*$ confirm
5c1ae3bc
RK
457 *
458 * Confirm a user registration using the nonce supplied in \fBc\fR and expands
459 * \fIlogin.tmpl\fR with \fBstatus\fR or \fB@error\fR set according to the
460 * result.
461 */
e7ce7665
RK
462static void act_confirm(void) {
463 const char *confirmation;
464
465 /* If we're not connected then this is a hopeless exercise */
466 if(!dcgi_client) {
467 login_error("connect");
468 return;
469 }
470
471 if(!(confirmation = cgi_get("c"))) {
472 login_error("noconfirm");
473 return;
474 }
475 /* Confirm our registration */
476 if(disorder_confirm(dcgi_client, confirmation)) {
477 login_error("badconfirm");
478 return;
479 }
480 /* Get a cookie */
481 if(disorder_make_cookie(dcgi_client, &dcgi_cookie)) {
482 login_error("cookiefailed");
483 return;
484 }
485 /* Junk cached data */
486 dcgi_lookup_reset();
487 /* Report success */
488 dcgi_status_string = "confirmed";
489 dcgi_expand("login", 1);
490}
491
59cf25c4 492/*$ edituser
5c1ae3bc
RK
493 *
494 * Edit user details using \fBusername\fR, \fBchangepassword1\fR,
495 * \fBchangepassword2\fR and \fBemail\fR and expands \fIlogin.tmpl\fR with
496 * \fBstatus\fR or \fB@error\fR set according to the result.
497 */
e7ce7665
RK
498static void act_edituser(void) {
499 const char *email = cgi_get("email"), *password = cgi_get("changepassword1");
500 const char *password2 = cgi_get("changepassword2");
501 int newpassword = 0;
502
503 /* If we're not connected then this is a hopeless exercise */
504 if(!dcgi_client) {
505 login_error("connect");
506 return;
507 }
508
509 /* Verify input */
510
511 /* If either password or password2 is set we insist they match. If they
512 * don't we report an error. */
513 if((password && *password) || (password2 && *password2)) {
514 if(!password || !password2 || strcmp(password, password2)) {
515 login_error("passwordmismatch");
516 return;
517 }
518 } else
519 password = password2 = 0;
33e95f03 520 if(email && !email_valid(email)) {
e7ce7665
RK
521 login_error("bademail");
522 return;
523 }
524
525 /* Commit changes */
526 if(email) {
527 if(disorder_edituser(dcgi_client, disorder_user(dcgi_client),
528 "email", email)) {
529 login_error("badedit");
530 return;
531 }
532 }
533 if(password) {
534 if(disorder_edituser(dcgi_client, disorder_user(dcgi_client),
535 "password", password)) {
536 login_error("badedit");
537 return;
538 }
539 newpassword = 1;
540 }
541
542 if(newpassword) {
543 /* If we changed the password, the cookie is now invalid, so we must log
544 * back in. */
545 if(login_as(disorder_user(dcgi_client), password))
546 return;
547 }
548 /* Report success */
549 dcgi_status_string = "edited";
550 dcgi_expand("login", 1);
551}
552
59cf25c4 553/*$ reminder
5c1ae3bc
RK
554 *
555 * Issue an email password reminder to \fBusername\fR and expands
556 * \fIlogin.tmpl\fR with \fBstatus\fR or \fB@error\fR set according to the
557 * result.
558 */
e7ce7665
RK
559static void act_reminder(void) {
560 const char *const username = cgi_get("username");
561
562 /* If we're not connected then this is a hopeless exercise */
563 if(!dcgi_client) {
564 login_error("connect");
565 return;
566 }
567
568 if(!username || !*username) {
569 login_error("nousername");
570 return;
571 }
572 if(disorder_reminder(dcgi_client, username)) {
573 login_error("reminderfailed");
574 return;
575 }
576 /* Report success */
577 dcgi_status_string = "reminded";
578 dcgi_expand("login", 1);
579}
580
02eaa49d
RK
581/** @brief Get the numbered version of an argument
582 * @param argname Base argument name
583 * @param numfile File number
584 * @return cgi_get(NUMFILE_ARGNAME)
585 */
586static const char *numbered_arg(const char *argname, int numfile) {
587 char *fullname;
588
589 byte_xasprintf(&fullname, "%d_%s", numfile, argname);
590 return cgi_get(fullname);
591}
592
593/** @brief Set preferences for file @p numfile
594 * @return 0 on success, -1 if there is no such track number
595 *
596 * The old @b nfiles parameter has been abolished, we just keep look for more
597 * files until we run out.
598 */
599static int process_prefs(int numfile) {
600 const char *file, *name, *value, *part, *parts, *context;
601 char **partslist;
602
603 if(!(file = numbered_arg("track", numfile)))
604 return -1;
605 if(!(parts = cgi_get("parts")))
606 parts = "artist album title";
607 if(!(context = cgi_get("context")))
608 context = "display";
609 partslist = split(parts, 0, 0, 0, 0);
610 while((part = *partslist++)) {
611 if(!(value = numbered_arg(part, numfile)))
612 continue;
613 byte_xasprintf((char **)&name, "trackname_%s_%s", context, part);
614 disorder_set(dcgi_client, file, name, value);
615 }
616 if((value = numbered_arg("random", numfile)))
617 disorder_unset(dcgi_client, file, "pick_at_random");
618 else
619 disorder_set(dcgi_client, file, "pick_at_random", "0");
620 if((value = numbered_arg("tags", numfile))) {
621 if(!*value)
622 disorder_unset(dcgi_client, file, "tags");
623 else
624 disorder_set(dcgi_client, file, "tags", value);
625 }
626 if((value = numbered_arg("weight", numfile))) {
627 if(!*value)
628 disorder_unset(dcgi_client, file, "weight");
629 else
630 disorder_set(dcgi_client, file, "weight", value);
631 }
632 return 0;
633}
634
59cf25c4 635/*$ prefs
5c1ae3bc
RK
636 *
637 * Set preferences on a number of tracks.
638 *
639 * The tracks to modify are specified in arguments \fB0_track\fR, \fB1_track\fR
640 * etc. The number sequence must be contiguous and start from 0.
641 *
642 * For each track \fIINDEX\fB_track\fR:
643 * - \fIINDEX\fB_\fIPART\fR is used to set the trackname preference for
644 * that part. (See \fBparts\fR below.)
645 * - \fIINDEX\fB_\fIrandom\fR if present enables random play for this track
646 * or disables it if absent.
647 * - \fIINDEX\fB_\fItags\fR sets the list of tags for this track.
648 * - \fIINDEX\fB_\fIweight\fR sets the weight for this track.
649 *
650 * \fBparts\fR can be set to the track name parts to modify. The default is
651 * "artist album title".
652 *
653 * \fBcontext\fR can be set to the context to modify. The default is
654 * "display".
655 *
656 * If the server detects a preference being set to its default, it removes the
657 * preference, thus keeping the database tidy.
658 */
02eaa49d
RK
659static void act_set(void) {
660 int numfile;
661
662 if(dcgi_client) {
663 for(numfile = 0; !process_prefs(numfile); ++numfile)
664 ;
665 }
666 redirect(0);
667}
668
bca4e2b7
RK
669/** @brief Table of actions */
670static const struct action {
671 /** @brief Action name */
672 const char *name;
673 /** @brief Action handler */
674 void (*handler)(void);
02eaa49d
RK
675 /** @brief Union of suitable rights */
676 rights_type rights;
bca4e2b7 677} actions[] = {
02eaa49d
RK
678 { "confirm", act_confirm, 0 },
679 { "disable", act_disable, RIGHT_GLOBAL_PREFS },
680 { "edituser", act_edituser, 0 },
681 { "enable", act_enable, RIGHT_GLOBAL_PREFS },
682 { "login", act_login, 0 },
683 { "logout", act_logout, 0 },
684 { "manage", act_playing, 0 },
685 { "move", act_move, RIGHT_MOVE__MASK },
686 { "pause", act_pause, RIGHT_PAUSE },
687 { "play", act_play, RIGHT_PLAY },
688 { "playing", act_playing, 0 },
689 { "randomdisable", act_random_disable, RIGHT_GLOBAL_PREFS },
690 { "randomenable", act_random_enable, RIGHT_GLOBAL_PREFS },
691 { "register", act_register, 0 },
692 { "reminder", act_reminder, 0 },
693 { "remove", act_remove, RIGHT_MOVE__MASK|RIGHT_SCRATCH__MASK },
694 { "resume", act_resume, RIGHT_PAUSE },
695 { "set", act_set, RIGHT_PREFS },
696 { "volume", act_volume, RIGHT_VOLUME },
bca4e2b7
RK
697};
698
40dcd866
RK
699/** @brief Check that an action name is valid
700 * @param name Action
701 * @return 1 if valid, 0 if not
702 */
703static int dcgi_valid_action(const char *name) {
704 int c;
705
706 /* First character must be letter or digit (this also requires there to _be_
707 * a first character) */
708 if(!isalnum((unsigned char)*name))
709 return 0;
710 /* Only letters, digits, '.' and '-' allowed */
711 while((c = (unsigned char)*name++)) {
712 if(!(isalnum(c)
713 || c == '.'
714 || c == '_'))
715 return 0;
716 }
717 return 1;
718}
719
bca4e2b7
RK
720/** @brief Expand a template
721 * @param name Base name of template, or NULL to consult CGI args
e7ce7665 722 * @param header True to write header
bca4e2b7 723 */
e7ce7665 724void dcgi_expand(const char *name, int header) {
99955407 725 const char *p, *found;
0d0253c9
RK
726
727 /* Parse macros first */
f2d306b4
RK
728 if((found = mx_find("macros.tmpl", 1/*report*/)))
729 mx_expand_file(found, sink_discard(), 0);
730 if((found = mx_find("user.tmpl", 0/*report*/)))
99955407 731 mx_expand_file(found, sink_discard(), 0);
bca4e2b7 732 /* For unknown actions check that they aren't evil */
40dcd866 733 if(!dcgi_valid_action(name))
71634563
RK
734 fatal(0, "invalid action name '%s'", name);
735 byte_xasprintf((char **)&p, "%s.tmpl", name);
f2d306b4 736 if(!(found = mx_find(p, 0/*report*/)))
99955407 737 fatal(errno, "cannot find %s", p);
e7ce7665 738 if(header) {
10921eba 739 if(printf("Content-Type: text/html; charset=UTF-8\n"
e7ce7665
RK
740 "%s\n"
741 "\n", dcgi_cookie_header()) < 0)
742 fatal(errno, "error writing to stdout");
743 }
99955407 744 if(mx_expand_file(found, sink_stdio("stdout", stdout), 0) == -1
bca4e2b7
RK
745 || fflush(stdout) < 0)
746 fatal(errno, "error writing to stdout");
747}
748
749/** @brief Execute a web action
750 * @param action Action to perform, or NULL to consult CGI args
751 *
752 * If no recognized action is specified then 'playing' is assumed.
753 */
1e97629d 754void dcgi_action(const char *action) {
bca4e2b7 755 int n;
bca4e2b7
RK
756
757 /* Consult CGI args if caller had no view */
758 if(!action)
759 action = cgi_get("action");
760 /* Pick a default if nobody cares at all */
761 if(!action) {
762 /* We allow URLs which are just c=... in order to keep confirmation URLs,
763 * which are user-facing, as short as possible. Actually we could lose the
764 * c= for this... */
765 if(cgi_get("c"))
766 action = "confirm";
767 else
768 action = "playing";
5a7df048
RK
769 /* Make sure 'action' is always set */
770 cgi_set("action", action);
bca4e2b7 771 }
ba937f01 772 if((n = TABLE_FIND(actions, name, action)) >= 0) {
02eaa49d
RK
773 if(actions[n].rights) {
774 /* Some right or other is required */
775 dcgi_lookup(DCGI_RIGHTS);
776 if(!(actions[n].rights & dcgi_rights)) {
2cc4c0ef 777 const char *back = cgi_thisurl(config->url);
02eaa49d 778 /* Failed operations jump you to the login screen with an error
2cc4c0ef
RK
779 * message. On success, the user comes back to the page they were
780 * after. */
781 cgi_clear();
782 cgi_set("back", back);
02eaa49d
RK
783 login_error("noright");
784 return;
785 }
786 }
787 /* It's a known action */
bca4e2b7 788 actions[n].handler();
02eaa49d 789 } else {
bca4e2b7 790 /* Just expand the template */
e7ce7665 791 dcgi_expand(action, 1/*header*/);
448d3570 792 }
bca4e2b7
RK
793}
794
795/** @brief Generate an error page */
0d0253c9
RK
796void dcgi_error(const char *key) {
797 dcgi_error_string = xstrdup(key);
e7ce7665 798 dcgi_expand("error", 1);
bca4e2b7
RK
799}
800
801/*
802Local Variables:
803c-basic-offset:2
804comment-column:40
805fill-column:79
806indent-tabs-mode:nil
807End:
808*/