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
24 /** @brief Login cookie */
27 /** @brief Table of actions */
28 static const struct action {
29 /** @brief Action name */
31 /** @brief Action handler */
32 void (*handler)(void);
34 { "confirm", act_confirm },
35 { "disable", act_disable },
36 { "edituser", act_edituser },
37 { "enable", act_enable },
38 { "login", act_login },
39 { "logout", act_logout },
40 { "manage", act_manage },
42 { "pause", act_pause },
44 { "playing", act_playing },
45 { "prefs", act_prefs },
46 { "random-disable", act_random_disable },
47 { "random-enable", act_random_enable },
48 { "register", act_register },
49 { "reminder", act_reminder },
50 { "remove", act_remove },
51 { "resume", act_resume },
52 { "scratch", act_scratch },
53 { "volume", act_volume },
56 /** @brief Expand a template
57 * @param name Base name of template, or NULL to consult CGI args
59 void disorder_cgi_expand(const char *name) {
62 /* For unknown actions check that they aren't evil */
63 for(p = name; *p && isalnum((unsigned char)*p); ++p)
66 fatal(0, "invalid action name '%s'", action);
67 byte_xasprintf((char **)&p, "%s.tmpl", action);
68 if(mx_expand_file(p, sink_stdio(stdout), 0) == -1
69 || fflush(stdout) < 0)
70 fatal(errno, "error writing to stdout");
73 /** @brief Execute a web action
74 * @param action Action to perform, or NULL to consult CGI args
76 * If no recognized action is specified then 'playing' is assumed.
78 void disorder_cgi_action(const char *action) {
82 /* Consult CGI args if caller had no view */
84 action = cgi_get("action");
85 /* Pick a default if nobody cares at all */
87 /* We allow URLs which are just c=... in order to keep confirmation URLs,
88 * which are user-facing, as short as possible. Actually we could lose the
95 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
96 /* Its a known action */
99 /* Just expand the template */
100 disorder_cgi_expand(action);
103 /** @brief Generate an error page */
104 void disorder_cgi_error(const char *msg, ...) {
108 byte_xvasprintf(&error_string, msg, ap);
110 disorder_cgi_expand("error");
113 /** @brief Log in as the current user or guest if none */
114 void disorder_cgi_login(dcgi_state *ds, struct sink *output) {
116 disorder_macros_reset();
118 if(disorder_connect_cookie(client, login_cookie)) {
119 disorder_cgi_error("Cannot connect to server");
122 /* If there was a cookie but it went bad, we forget it */
123 if(login_cookie && !strcmp(disorder_user(>client), "guest"))