Start conversion of CGI actions.
[disorder] / server / actions.c
CommitLineData
bca4e2b7
RK
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
21#include <config.h>
22#include "types.h"
23
24/** @brief Login cookie */
25char *login_cookie;
26
27/** @brief Table of actions */
28static const struct action {
29 /** @brief Action name */
30 const char *name;
31 /** @brief Action handler */
32 void (*handler)(void);
33} actions[] = {
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 },
41 { "move", act_move },
42 { "pause", act_pause },
43 { "play", act_play },
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 },
54};
55
56/** @brief Expand a template
57 * @param name Base name of template, or NULL to consult CGI args
58 */
59void disorder_cgi_expand(const char *name) {
60 const char *p;
61
62 /* For unknown actions check that they aren't evil */
63 for(p = name; *p && isalnum((unsigned char)*p); ++p)
64 ;
65 if(*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");
71}
72
73/** @brief Execute a web action
74 * @param action Action to perform, or NULL to consult CGI args
75 *
76 * If no recognized action is specified then 'playing' is assumed.
77 */
78void disorder_cgi_action(const char *action) {
79 int n;
80 char *s;
81
82 /* Consult CGI args if caller had no view */
83 if(!action)
84 action = cgi_get("action");
85 /* Pick a default if nobody cares at all */
86 if(!action) {
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
89 * c= for this... */
90 if(cgi_get("c"))
91 action = "confirm";
92 else
93 action = "playing";
94 }
95 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
96 /* Its a known action */
97 actions[n].handler();
98 else
99 /* Just expand the template */
100 disorder_cgi_expand(action);
101}
102
103/** @brief Generate an error page */
104void disorder_cgi_error(const char *msg, ...) {
105 va_list ap;
106
107 va_start(ap, msg);
108 byte_xvasprintf(&error_string, msg, ap);
109 va_end(ap);
110 disorder_cgi_expand("error");
111}
112
113/** @brief Log in as the current user or guest if none */
114void disorder_cgi_login(dcgi_state *ds, struct sink *output) {
115 /* Junk old data */
116 disorder_macros_reset();
117 /* Reconnect */
118 if(disorder_connect_cookie(client, login_cookie)) {
119 disorder_cgi_error("Cannot connect to server");
120 exit(0);
121 }
122 /* If there was a cookie but it went bad, we forget it */
123 if(login_cookie && !strcmp(disorder_user(>client), "guest"))
124 login_cookie = 0;
125}
126
127/*
128Local Variables:
129c-basic-offset:2
130comment-column:40
131fill-column:79
132indent-tabs-mode:nil
133End:
134*/