Commit | Line | Data |
---|---|---|
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 | */ | |
1e97629d RK |
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 | */ | |
bca4e2b7 | 28 | |
1e97629d | 29 | #include "disorder-cgi.h" |
5a7df048 RK |
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) | |
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 | ||
5c1ae3bc RK |
50 | /*! playing |
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 | */ | |
57 | /*! manage | |
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 |
64 | static 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 | |
90 | && dcgi_queue->state != playing_random) | |
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 | ||
5c1ae3bc RK |
108 | /*! disable |
109 | * | |
110 | * Disables play. | |
111 | */ | |
1e97629d RK |
112 | static void act_disable(void) { |
113 | if(dcgi_client) | |
114 | disorder_disable(dcgi_client); | |
115 | redirect(0); | |
116 | } | |
117 | ||
5c1ae3bc RK |
118 | /*! enable |
119 | * | |
120 | * Enables play. | |
121 | */ | |
1e97629d RK |
122 | static void act_enable(void) { |
123 | if(dcgi_client) | |
124 | disorder_enable(dcgi_client); | |
125 | redirect(0); | |
126 | } | |
127 | ||
5c1ae3bc RK |
128 | /*! random-disable |
129 | * | |
130 | * Disables random play. | |
131 | */ | |
1e97629d RK |
132 | static void act_random_disable(void) { |
133 | if(dcgi_client) | |
134 | disorder_random_disable(dcgi_client); | |
135 | redirect(0); | |
136 | } | |
137 | ||
5c1ae3bc RK |
138 | /*! random-enable |
139 | * | |
140 | * Enables random play. | |
141 | */ | |
1e97629d RK |
142 | static void act_random_enable(void) { |
143 | if(dcgi_client) | |
144 | disorder_random_enable(dcgi_client); | |
145 | redirect(0); | |
448d3570 RK |
146 | } |
147 | ||
5c1ae3bc RK |
148 | /*! pause |
149 | * | |
150 | * Pauses the current track (if there is one and it's not paused already). | |
151 | */ | |
6d9dd8d9 RK |
152 | static void act_pause(void) { |
153 | if(dcgi_client) | |
154 | disorder_pause(dcgi_client); | |
155 | redirect(0); | |
156 | } | |
157 | ||
5c1ae3bc RK |
158 | /*! resume |
159 | * | |
160 | * Resumes the current track (if there is one and it's paused). | |
161 | */ | |
6d9dd8d9 RK |
162 | static void act_resume(void) { |
163 | if(dcgi_client) | |
164 | disorder_resume(dcgi_client); | |
165 | redirect(0); | |
166 | } | |
167 | ||
5c1ae3bc RK |
168 | /*! remove |
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 |
173 | static 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 | ||
5c1ae3bc RK |
204 | /*! move |
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 |
210 | static 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 | ||
5c1ae3bc RK |
234 | /*! play |
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 |
239 | static void act_play(void) { |
240 | const char *track, *dir; | |
241 | char **tracks; | |
242 | int ntracks, n; | |
243 | struct dcgi_entry *e; | |
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; | |
2fadbbc6 | 251 | /* TODO use tracksort_init */ |
6d9dd8d9 RK |
252 | e = xmalloc(ntracks * sizeof (struct dcgi_entry)); |
253 | for(n = 0; n < ntracks; ++n) { | |
254 | e[n].track = tracks[n]; | |
255 | e[n].sort = trackname_transform("track", tracks[n], "sort"); | |
256 | e[n].display = trackname_transform("track", tracks[n], "display"); | |
257 | } | |
258 | qsort(e, ntracks, sizeof (struct dcgi_entry), dcgi_compare_entry); | |
259 | for(n = 0; n < ntracks; ++n) | |
260 | disorder_play(dcgi_client, e[n].track); | |
261 | } | |
262 | } | |
263 | redirect(0); | |
264 | } | |
265 | ||
266 | static int clamp(int n, int min, int max) { | |
267 | if(n < min) | |
268 | return min; | |
269 | if(n > max) | |
270 | return max; | |
271 | return n; | |
272 | } | |
273 | ||
5c1ae3bc RK |
274 | /*! volume |
275 | * | |
276 | * If the \fBdelta\fR argument is set: adjust both channels by that amount (up | |
277 | * if positive, down if negative). | |
278 | * | |
279 | * Otherwise if \fBleft\fR and \fBright\fR are set, set the channels | |
280 | * independently to those values. | |
281 | */ | |
6d9dd8d9 RK |
282 | static void act_volume(void) { |
283 | const char *l, *r, *d; | |
284 | int nd; | |
285 | ||
286 | if(dcgi_client) { | |
287 | if((d = cgi_get("delta"))) { | |
288 | dcgi_lookup(DCGI_VOLUME); | |
289 | nd = clamp(atoi(d), -255, 255); | |
290 | disorder_set_volume(dcgi_client, | |
291 | clamp(dcgi_volume_left + nd, 0, 255), | |
292 | clamp(dcgi_volume_right + nd, 0, 255)); | |
293 | } else if((l = cgi_get("left")) && (r = cgi_get("right"))) | |
294 | disorder_set_volume(dcgi_client, atoi(l), atoi(r)); | |
295 | } | |
296 | redirect(0); | |
297 | } | |
298 | ||
e7ce7665 | 299 | /** @brief Expand the login template with @b @@error set to @p error |
b7e452c7 | 300 | * @param e Error keyword |
e7ce7665 | 301 | */ |
b7e452c7 RK |
302 | static void login_error(const char *e) { |
303 | dcgi_error_string = e; | |
e7ce7665 RK |
304 | dcgi_expand("login", 1); |
305 | } | |
306 | ||
307 | /** @brief Log in | |
308 | * @param username Login name | |
309 | * @param password Password | |
310 | * @return 0 on success, non-0 on error | |
311 | * | |
312 | * On error, calls login_error() to expand the login template. | |
313 | */ | |
314 | static int login_as(const char *username, const char *password) { | |
315 | disorder_client *c; | |
316 | ||
317 | if(dcgi_cookie && dcgi_client) | |
318 | disorder_revoke(dcgi_client); | |
319 | /* We'll need a new connection as we are going to stop being guest */ | |
320 | c = disorder_new(0); | |
321 | if(disorder_connect_user(c, username, password)) { | |
322 | login_error("loginfailed"); | |
323 | return -1; | |
324 | } | |
325 | /* Generate a cookie so we can log in again later */ | |
326 | if(disorder_make_cookie(c, &dcgi_cookie)) { | |
327 | login_error("cookiefailed"); | |
328 | return -1; | |
329 | } | |
330 | /* Use the new connection henceforth */ | |
331 | dcgi_client = c; | |
332 | dcgi_lookup_reset(); | |
333 | return 0; /* OK */ | |
334 | } | |
335 | ||
5c1ae3bc RK |
336 | /*! login |
337 | * | |
338 | * If \fBusername\fR and \fBpassword\fR are set (and the username isn't | |
339 | * "guest") then attempt to log in using those credentials. On success, | |
340 | * redirects to the \fBback\fR argument if that is set, or just expands | |
341 | * \fIlogin.tmpl\fI otherwise, with \fB@status\fR set to \fBloginok\fR. | |
342 | * | |
343 | * If they aren't set then just expands \fIlogin.tmpl\fI. | |
344 | */ | |
e7ce7665 RK |
345 | static void act_login(void) { |
346 | const char *username, *password; | |
347 | ||
348 | /* We try all this even if not connected since the subsequent connection may | |
349 | * succeed. */ | |
350 | ||
351 | username = cgi_get("username"); | |
352 | password = cgi_get("password"); | |
353 | if(!username | |
354 | || !password | |
355 | || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) { | |
356 | /* We're just visiting the login page, not performing an action at all. */ | |
357 | dcgi_expand("login", 1); | |
358 | return; | |
359 | } | |
360 | if(!login_as(username, password)) { | |
361 | /* Report the succesful login */ | |
362 | dcgi_status_string = "loginok"; | |
2cc4c0ef RK |
363 | /* Redirect back to where we came from, if necessary */ |
364 | if(cgi_get("back")) | |
365 | redirect(0); | |
366 | else | |
367 | dcgi_expand("login", 1); | |
e7ce7665 RK |
368 | } |
369 | } | |
370 | ||
5c1ae3bc RK |
371 | /*! logout |
372 | * | |
373 | * Logs out the current user and expands \fIlogin.tmpl\fR with \fBstatus\fR or | |
374 | * \fB@error\fR set according to the result. | |
375 | */ | |
e7ce7665 RK |
376 | static void act_logout(void) { |
377 | if(dcgi_client) { | |
378 | /* Ask the server to revoke the cookie */ | |
379 | if(!disorder_revoke(dcgi_client)) | |
380 | dcgi_status_string = "logoutok"; | |
381 | else | |
382 | dcgi_error_string = "revokefailed"; | |
383 | } else { | |
384 | /* We can't guarantee a logout if we can't connect to the server to revoke | |
385 | * the cookie, so we report an error. We'll still ask the browser to | |
386 | * forget the cookie though. */ | |
387 | dcgi_error_string = "connect"; | |
388 | } | |
389 | /* Attempt to reconnect without the cookie */ | |
390 | dcgi_cookie = 0; | |
391 | dcgi_login(); | |
392 | /* Back to login page, hopefuly forcing the browser to forget the cookie. */ | |
393 | dcgi_expand("login", 1); | |
394 | } | |
395 | ||
5c1ae3bc RK |
396 | /*! register |
397 | * | |
398 | * Register a new user using \fBusername\fR, \fBpassword1\fR, \fBpassword2\fR | |
399 | * and \fBemail\fR and expands \fIlogin.tmpl\fR with \fBstatus\fR or | |
400 | * \fB@error\fR set according to the result. | |
401 | */ | |
e7ce7665 RK |
402 | static void act_register(void) { |
403 | const char *username, *password, *password2, *email; | |
404 | char *confirm, *content_type; | |
405 | const char *text, *encoding, *charset; | |
406 | ||
407 | /* If we're not connected then this is a hopeless exercise */ | |
408 | if(!dcgi_client) { | |
409 | login_error("connect"); | |
410 | return; | |
411 | } | |
412 | ||
413 | /* Collect arguments */ | |
414 | username = cgi_get("username"); | |
415 | password = cgi_get("password1"); | |
416 | password2 = cgi_get("password2"); | |
417 | email = cgi_get("email"); | |
418 | ||
419 | /* Verify arguments */ | |
420 | if(!username || !*username) { | |
421 | login_error("nousername"); | |
422 | return; | |
423 | } | |
424 | if(!password || !*password) { | |
425 | login_error("nopassword"); | |
426 | return; | |
427 | } | |
428 | if(!password2 || !*password2 || strcmp(password, password2)) { | |
429 | login_error("passwordmismatch"); | |
430 | return; | |
431 | } | |
432 | if(!email || !*email) { | |
433 | login_error("noemail"); | |
434 | return; | |
435 | } | |
436 | /* We could well do better address validation but for now we'll just do the | |
437 | * minimum */ | |
438 | if(!strchr(email, '@')) { | |
439 | login_error("bademail"); | |
440 | return; | |
441 | } | |
442 | if(disorder_register(dcgi_client, username, password, email, &confirm)) { | |
443 | login_error("cannotregister"); | |
444 | return; | |
445 | } | |
446 | /* Send the user a mail */ | |
447 | /* TODO templatize this */ | |
448 | byte_xasprintf((char **)&text, | |
449 | "Welcome to DisOrder. To active your login, please visit this URL:\n" | |
450 | "\n" | |
451 | "%s?c=%s\n", config->url, urlencodestring(confirm)); | |
452 | if(!(text = mime_encode_text(text, &charset, &encoding))) | |
453 | fatal(0, "cannot encode email"); | |
454 | byte_xasprintf(&content_type, "text/plain;charset=%s", | |
455 | quote822(charset, 0)); | |
456 | sendmail("", config->mail_sender, email, "Welcome to DisOrder", | |
457 | encoding, content_type, text); /* TODO error checking */ | |
458 | /* We'll go back to the login page with a suitable message */ | |
459 | dcgi_status_string = "registered"; | |
460 | dcgi_expand("login", 1); | |
461 | } | |
462 | ||
5c1ae3bc RK |
463 | /*! confirm |
464 | * | |
465 | * Confirm a user registration using the nonce supplied in \fBc\fR and expands | |
466 | * \fIlogin.tmpl\fR with \fBstatus\fR or \fB@error\fR set according to the | |
467 | * result. | |
468 | */ | |
e7ce7665 RK |
469 | static void act_confirm(void) { |
470 | const char *confirmation; | |
471 | ||
472 | /* If we're not connected then this is a hopeless exercise */ | |
473 | if(!dcgi_client) { | |
474 | login_error("connect"); | |
475 | return; | |
476 | } | |
477 | ||
478 | if(!(confirmation = cgi_get("c"))) { | |
479 | login_error("noconfirm"); | |
480 | return; | |
481 | } | |
482 | /* Confirm our registration */ | |
483 | if(disorder_confirm(dcgi_client, confirmation)) { | |
484 | login_error("badconfirm"); | |
485 | return; | |
486 | } | |
487 | /* Get a cookie */ | |
488 | if(disorder_make_cookie(dcgi_client, &dcgi_cookie)) { | |
489 | login_error("cookiefailed"); | |
490 | return; | |
491 | } | |
492 | /* Junk cached data */ | |
493 | dcgi_lookup_reset(); | |
494 | /* Report success */ | |
495 | dcgi_status_string = "confirmed"; | |
496 | dcgi_expand("login", 1); | |
497 | } | |
498 | ||
5c1ae3bc RK |
499 | /*! edituser |
500 | * | |
501 | * Edit user details using \fBusername\fR, \fBchangepassword1\fR, | |
502 | * \fBchangepassword2\fR and \fBemail\fR and expands \fIlogin.tmpl\fR with | |
503 | * \fBstatus\fR or \fB@error\fR set according to the result. | |
504 | */ | |
e7ce7665 RK |
505 | static void act_edituser(void) { |
506 | const char *email = cgi_get("email"), *password = cgi_get("changepassword1"); | |
507 | const char *password2 = cgi_get("changepassword2"); | |
508 | int newpassword = 0; | |
509 | ||
510 | /* If we're not connected then this is a hopeless exercise */ | |
511 | if(!dcgi_client) { | |
512 | login_error("connect"); | |
513 | return; | |
514 | } | |
515 | ||
516 | /* Verify input */ | |
517 | ||
518 | /* If either password or password2 is set we insist they match. If they | |
519 | * don't we report an error. */ | |
520 | if((password && *password) || (password2 && *password2)) { | |
521 | if(!password || !password2 || strcmp(password, password2)) { | |
522 | login_error("passwordmismatch"); | |
523 | return; | |
524 | } | |
525 | } else | |
526 | password = password2 = 0; | |
527 | if(email && !strchr(email, '@')) { | |
528 | login_error("bademail"); | |
529 | return; | |
530 | } | |
531 | ||
532 | /* Commit changes */ | |
533 | if(email) { | |
534 | if(disorder_edituser(dcgi_client, disorder_user(dcgi_client), | |
535 | "email", email)) { | |
536 | login_error("badedit"); | |
537 | return; | |
538 | } | |
539 | } | |
540 | if(password) { | |
541 | if(disorder_edituser(dcgi_client, disorder_user(dcgi_client), | |
542 | "password", password)) { | |
543 | login_error("badedit"); | |
544 | return; | |
545 | } | |
546 | newpassword = 1; | |
547 | } | |
548 | ||
549 | if(newpassword) { | |
550 | /* If we changed the password, the cookie is now invalid, so we must log | |
551 | * back in. */ | |
552 | if(login_as(disorder_user(dcgi_client), password)) | |
553 | return; | |
554 | } | |
555 | /* Report success */ | |
556 | dcgi_status_string = "edited"; | |
557 | dcgi_expand("login", 1); | |
558 | } | |
559 | ||
5c1ae3bc RK |
560 | /*! reminder |
561 | * | |
562 | * Issue an email password reminder to \fBusername\fR and expands | |
563 | * \fIlogin.tmpl\fR with \fBstatus\fR or \fB@error\fR set according to the | |
564 | * result. | |
565 | */ | |
e7ce7665 RK |
566 | static void act_reminder(void) { |
567 | const char *const username = cgi_get("username"); | |
568 | ||
569 | /* If we're not connected then this is a hopeless exercise */ | |
570 | if(!dcgi_client) { | |
571 | login_error("connect"); | |
572 | return; | |
573 | } | |
574 | ||
575 | if(!username || !*username) { | |
576 | login_error("nousername"); | |
577 | return; | |
578 | } | |
579 | if(disorder_reminder(dcgi_client, username)) { | |
580 | login_error("reminderfailed"); | |
581 | return; | |
582 | } | |
583 | /* Report success */ | |
584 | dcgi_status_string = "reminded"; | |
585 | dcgi_expand("login", 1); | |
586 | } | |
587 | ||
02eaa49d RK |
588 | /** @brief Get the numbered version of an argument |
589 | * @param argname Base argument name | |
590 | * @param numfile File number | |
591 | * @return cgi_get(NUMFILE_ARGNAME) | |
592 | */ | |
593 | static const char *numbered_arg(const char *argname, int numfile) { | |
594 | char *fullname; | |
595 | ||
596 | byte_xasprintf(&fullname, "%d_%s", numfile, argname); | |
597 | return cgi_get(fullname); | |
598 | } | |
599 | ||
600 | /** @brief Set preferences for file @p numfile | |
601 | * @return 0 on success, -1 if there is no such track number | |
602 | * | |
603 | * The old @b nfiles parameter has been abolished, we just keep look for more | |
604 | * files until we run out. | |
605 | */ | |
606 | static int process_prefs(int numfile) { | |
607 | const char *file, *name, *value, *part, *parts, *context; | |
608 | char **partslist; | |
609 | ||
610 | if(!(file = numbered_arg("track", numfile))) | |
611 | return -1; | |
612 | if(!(parts = cgi_get("parts"))) | |
613 | parts = "artist album title"; | |
614 | if(!(context = cgi_get("context"))) | |
615 | context = "display"; | |
616 | partslist = split(parts, 0, 0, 0, 0); | |
617 | while((part = *partslist++)) { | |
618 | if(!(value = numbered_arg(part, numfile))) | |
619 | continue; | |
620 | byte_xasprintf((char **)&name, "trackname_%s_%s", context, part); | |
621 | disorder_set(dcgi_client, file, name, value); | |
622 | } | |
623 | if((value = numbered_arg("random", numfile))) | |
624 | disorder_unset(dcgi_client, file, "pick_at_random"); | |
625 | else | |
626 | disorder_set(dcgi_client, file, "pick_at_random", "0"); | |
627 | if((value = numbered_arg("tags", numfile))) { | |
628 | if(!*value) | |
629 | disorder_unset(dcgi_client, file, "tags"); | |
630 | else | |
631 | disorder_set(dcgi_client, file, "tags", value); | |
632 | } | |
633 | if((value = numbered_arg("weight", numfile))) { | |
634 | if(!*value) | |
635 | disorder_unset(dcgi_client, file, "weight"); | |
636 | else | |
637 | disorder_set(dcgi_client, file, "weight", value); | |
638 | } | |
639 | return 0; | |
640 | } | |
641 | ||
5c1ae3bc RK |
642 | /*! prefs |
643 | * | |
644 | * Set preferences on a number of tracks. | |
645 | * | |
646 | * The tracks to modify are specified in arguments \fB0_track\fR, \fB1_track\fR | |
647 | * etc. The number sequence must be contiguous and start from 0. | |
648 | * | |
649 | * For each track \fIINDEX\fB_track\fR: | |
650 | * - \fIINDEX\fB_\fIPART\fR is used to set the trackname preference for | |
651 | * that part. (See \fBparts\fR below.) | |
652 | * - \fIINDEX\fB_\fIrandom\fR if present enables random play for this track | |
653 | * or disables it if absent. | |
654 | * - \fIINDEX\fB_\fItags\fR sets the list of tags for this track. | |
655 | * - \fIINDEX\fB_\fIweight\fR sets the weight for this track. | |
656 | * | |
657 | * \fBparts\fR can be set to the track name parts to modify. The default is | |
658 | * "artist album title". | |
659 | * | |
660 | * \fBcontext\fR can be set to the context to modify. The default is | |
661 | * "display". | |
662 | * | |
663 | * If the server detects a preference being set to its default, it removes the | |
664 | * preference, thus keeping the database tidy. | |
665 | */ | |
02eaa49d RK |
666 | static void act_set(void) { |
667 | int numfile; | |
668 | ||
669 | if(dcgi_client) { | |
670 | for(numfile = 0; !process_prefs(numfile); ++numfile) | |
671 | ; | |
672 | } | |
673 | redirect(0); | |
674 | } | |
675 | ||
bca4e2b7 RK |
676 | /** @brief Table of actions */ |
677 | static const struct action { | |
678 | /** @brief Action name */ | |
679 | const char *name; | |
680 | /** @brief Action handler */ | |
681 | void (*handler)(void); | |
02eaa49d RK |
682 | /** @brief Union of suitable rights */ |
683 | rights_type rights; | |
bca4e2b7 | 684 | } actions[] = { |
02eaa49d RK |
685 | { "confirm", act_confirm, 0 }, |
686 | { "disable", act_disable, RIGHT_GLOBAL_PREFS }, | |
687 | { "edituser", act_edituser, 0 }, | |
688 | { "enable", act_enable, RIGHT_GLOBAL_PREFS }, | |
689 | { "login", act_login, 0 }, | |
690 | { "logout", act_logout, 0 }, | |
691 | { "manage", act_playing, 0 }, | |
692 | { "move", act_move, RIGHT_MOVE__MASK }, | |
693 | { "pause", act_pause, RIGHT_PAUSE }, | |
694 | { "play", act_play, RIGHT_PLAY }, | |
695 | { "playing", act_playing, 0 }, | |
696 | { "randomdisable", act_random_disable, RIGHT_GLOBAL_PREFS }, | |
697 | { "randomenable", act_random_enable, RIGHT_GLOBAL_PREFS }, | |
698 | { "register", act_register, 0 }, | |
699 | { "reminder", act_reminder, 0 }, | |
700 | { "remove", act_remove, RIGHT_MOVE__MASK|RIGHT_SCRATCH__MASK }, | |
701 | { "resume", act_resume, RIGHT_PAUSE }, | |
702 | { "set", act_set, RIGHT_PREFS }, | |
703 | { "volume", act_volume, RIGHT_VOLUME }, | |
bca4e2b7 RK |
704 | }; |
705 | ||
40dcd866 RK |
706 | /** @brief Check that an action name is valid |
707 | * @param name Action | |
708 | * @return 1 if valid, 0 if not | |
709 | */ | |
710 | static int dcgi_valid_action(const char *name) { | |
711 | int c; | |
712 | ||
713 | /* First character must be letter or digit (this also requires there to _be_ | |
714 | * a first character) */ | |
715 | if(!isalnum((unsigned char)*name)) | |
716 | return 0; | |
717 | /* Only letters, digits, '.' and '-' allowed */ | |
718 | while((c = (unsigned char)*name++)) { | |
719 | if(!(isalnum(c) | |
720 | || c == '.' | |
721 | || c == '_')) | |
722 | return 0; | |
723 | } | |
724 | return 1; | |
725 | } | |
726 | ||
bca4e2b7 RK |
727 | /** @brief Expand a template |
728 | * @param name Base name of template, or NULL to consult CGI args | |
e7ce7665 | 729 | * @param header True to write header |
bca4e2b7 | 730 | */ |
e7ce7665 | 731 | void dcgi_expand(const char *name, int header) { |
99955407 | 732 | const char *p, *found; |
0d0253c9 RK |
733 | |
734 | /* Parse macros first */ | |
f2d306b4 RK |
735 | if((found = mx_find("macros.tmpl", 1/*report*/))) |
736 | mx_expand_file(found, sink_discard(), 0); | |
737 | if((found = mx_find("user.tmpl", 0/*report*/))) | |
99955407 | 738 | mx_expand_file(found, sink_discard(), 0); |
bca4e2b7 | 739 | /* For unknown actions check that they aren't evil */ |
40dcd866 | 740 | if(!dcgi_valid_action(name)) |
71634563 RK |
741 | fatal(0, "invalid action name '%s'", name); |
742 | byte_xasprintf((char **)&p, "%s.tmpl", name); | |
f2d306b4 | 743 | if(!(found = mx_find(p, 0/*report*/))) |
99955407 | 744 | fatal(errno, "cannot find %s", p); |
e7ce7665 RK |
745 | if(header) { |
746 | if(printf("Content-Type: text/html\n" | |
747 | "%s\n" | |
748 | "\n", dcgi_cookie_header()) < 0) | |
749 | fatal(errno, "error writing to stdout"); | |
750 | } | |
99955407 | 751 | if(mx_expand_file(found, sink_stdio("stdout", stdout), 0) == -1 |
bca4e2b7 RK |
752 | || fflush(stdout) < 0) |
753 | fatal(errno, "error writing to stdout"); | |
754 | } | |
755 | ||
756 | /** @brief Execute a web action | |
757 | * @param action Action to perform, or NULL to consult CGI args | |
758 | * | |
759 | * If no recognized action is specified then 'playing' is assumed. | |
760 | */ | |
1e97629d | 761 | void dcgi_action(const char *action) { |
bca4e2b7 | 762 | int n; |
bca4e2b7 RK |
763 | |
764 | /* Consult CGI args if caller had no view */ | |
765 | if(!action) | |
766 | action = cgi_get("action"); | |
767 | /* Pick a default if nobody cares at all */ | |
768 | if(!action) { | |
769 | /* We allow URLs which are just c=... in order to keep confirmation URLs, | |
770 | * which are user-facing, as short as possible. Actually we could lose the | |
771 | * c= for this... */ | |
772 | if(cgi_get("c")) | |
773 | action = "confirm"; | |
774 | else | |
775 | action = "playing"; | |
5a7df048 RK |
776 | /* Make sure 'action' is always set */ |
777 | cgi_set("action", action); | |
bca4e2b7 | 778 | } |
ba937f01 | 779 | if((n = TABLE_FIND(actions, name, action)) >= 0) { |
02eaa49d RK |
780 | if(actions[n].rights) { |
781 | /* Some right or other is required */ | |
782 | dcgi_lookup(DCGI_RIGHTS); | |
783 | if(!(actions[n].rights & dcgi_rights)) { | |
2cc4c0ef | 784 | const char *back = cgi_thisurl(config->url); |
02eaa49d | 785 | /* Failed operations jump you to the login screen with an error |
2cc4c0ef RK |
786 | * message. On success, the user comes back to the page they were |
787 | * after. */ | |
788 | cgi_clear(); | |
789 | cgi_set("back", back); | |
02eaa49d RK |
790 | login_error("noright"); |
791 | return; | |
792 | } | |
793 | } | |
794 | /* It's a known action */ | |
bca4e2b7 | 795 | actions[n].handler(); |
02eaa49d | 796 | } else { |
bca4e2b7 | 797 | /* Just expand the template */ |
e7ce7665 | 798 | dcgi_expand(action, 1/*header*/); |
448d3570 | 799 | } |
bca4e2b7 RK |
800 | } |
801 | ||
802 | /** @brief Generate an error page */ | |
0d0253c9 RK |
803 | void dcgi_error(const char *key) { |
804 | dcgi_error_string = xstrdup(key); | |
e7ce7665 | 805 | dcgi_expand("error", 1); |
bca4e2b7 RK |
806 | } |
807 | ||
808 | /* | |
809 | Local Variables: | |
810 | c-basic-offset:2 | |
811 | comment-column:40 | |
812 | fill-column:79 | |
813 | indent-tabs-mode:nil | |
814 | End: | |
815 | */ |