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