Commit | Line | Data |
---|---|---|
460b9539 | 1 | /* |
2 | * This file is part of DisOrder. | |
78efa64e | 3 | * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell |
460b9539 | 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 | #include <stdio.h> | |
25 | #include <errno.h> | |
26 | #include <sys/types.h> | |
27 | #include <sys/socket.h> | |
28 | #include <stddef.h> | |
29 | #include <stdlib.h> | |
30 | #include <time.h> | |
31 | #include <unistd.h> | |
32 | #include <string.h> | |
33 | #include <sys/wait.h> | |
34 | #include <pcre.h> | |
35 | #include <assert.h> | |
36 | ||
37 | #include "client.h" | |
38 | #include "mem.h" | |
39 | #include "vector.h" | |
40 | #include "sink.h" | |
41 | #include "cgi.h" | |
460b9539 | 42 | #include "log.h" |
43 | #include "configuration.h" | |
44 | #include "table.h" | |
45 | #include "queue.h" | |
46 | #include "plugin.h" | |
47 | #include "split.h" | |
460b9539 | 48 | #include "wstat.h" |
49 | #include "kvp.h" | |
50 | #include "syscalls.h" | |
51 | #include "printf.h" | |
52 | #include "regsub.h" | |
53 | #include "defs.h" | |
54 | #include "trackname.h" | |
61507e3c | 55 | #include "charset.h" |
938d8157 | 56 | #include "dcgi.h" |
36bde473 | 57 | #include "url.h" |
58 | #include "mime.h" | |
bb6ae3fb | 59 | #include "sendmail.h" |
460b9539 | 60 | |
fdf98378 | 61 | char *login_cookie; |
62 | ||
460b9539 | 63 | static void expand(cgi_sink *output, |
64 | const char *template, | |
65 | dcgi_state *ds); | |
66 | static void expandstring(cgi_sink *output, | |
67 | const char *string, | |
68 | dcgi_state *ds); | |
69 | ||
70 | struct entry { | |
71 | const char *path; | |
72 | const char *sort; | |
73 | const char *display; | |
74 | }; | |
75 | ||
76 | static const char *nonce(void) { | |
77 | static unsigned long count; | |
78 | char *s; | |
79 | ||
80 | byte_xasprintf(&s, "%lx%lx%lx", | |
81 | (unsigned long)time(0), | |
82 | (unsigned long)getpid(), | |
83 | count++); | |
84 | return s; | |
85 | } | |
86 | ||
87 | static int compare_entry(const void *a, const void *b) { | |
88 | const struct entry *ea = a, *eb = b; | |
89 | ||
90 | return compare_tracks(ea->sort, eb->sort, | |
91 | ea->display, eb->display, | |
92 | ea->path, eb->path); | |
93 | } | |
94 | ||
95 | static const char *front_url(void) { | |
96 | char *url; | |
97 | const char *mgmt; | |
98 | ||
99 | /* preserve management interface visibility */ | |
100 | if((mgmt = cgi_get("mgmt")) && !strcmp(mgmt, "true")) { | |
101 | byte_xasprintf(&url, "%s?mgmt=true", config->url); | |
102 | return url; | |
103 | } | |
104 | return config->url; | |
105 | } | |
106 | ||
7f66f58b | 107 | static void header_cookie(struct sink *output) { |
fdf98378 | 108 | struct dynstr d[1]; |
36bde473 | 109 | struct url u; |
fdf98378 | 110 | |
36bde473 | 111 | memset(&u, 0, sizeof u); |
112 | dynstr_init(d); | |
113 | parse_url(config->url, &u); | |
fdf98378 | 114 | if(login_cookie) { |
36bde473 | 115 | dynstr_append_string(d, "disorder="); |
116 | dynstr_append_string(d, quote822(login_cookie, 0)); | |
117 | } else { | |
7f66f58b | 118 | /* Force browser to discard cookie */ |
36bde473 | 119 | dynstr_append_string(d, "disorder=none;Max-Age=0"); |
120 | } | |
121 | if(u.path) { | |
122 | /* The default domain matches the request host, so we need not override | |
123 | * that. But the default path only goes up to the rightmost /, which would | |
124 | * cause the browser to expose the cookie to other CGI programs on the same | |
125 | * web server. */ | |
126 | dynstr_append_string(d, ";Path="); | |
127 | dynstr_append_string(d, quote822(u.path, 0)); | |
128 | } | |
129 | dynstr_terminate(d); | |
130 | cgi_header(output, "Set-Cookie", d->vec); | |
7f66f58b | 131 | } |
132 | ||
133 | static void redirect(struct sink *output) { | |
134 | const char *back; | |
135 | ||
136 | back = cgi_get("back"); | |
137 | cgi_header(output, "Location", back && *back ? back : front_url()); | |
138 | header_cookie(output); | |
139 | cgi_body(output); | |
fdf98378 | 140 | } |
141 | ||
142 | static void expand_template(dcgi_state *ds, cgi_sink *output, | |
143 | const char *action) { | |
144 | cgi_header(output->sink, "Content-Type", "text/html"); | |
7f66f58b | 145 | header_cookie(output->sink); |
fdf98378 | 146 | cgi_body(output->sink); |
147 | expand(output, action, ds); | |
148 | } | |
149 | ||
460b9539 | 150 | static void lookups(dcgi_state *ds, unsigned want) { |
151 | unsigned need; | |
152 | struct queue_entry *r, *rnext; | |
153 | const char *dir, *re; | |
938d8157 | 154 | char *rights; |
460b9539 | 155 | |
156 | if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) { | |
157 | if(need & DC_QUEUE) | |
158 | disorder_queue(ds->g->client, &ds->g->queue); | |
159 | if(need & DC_PLAYING) | |
160 | disorder_playing(ds->g->client, &ds->g->playing); | |
78efa64e RK |
161 | if(need & DC_NEW) |
162 | disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0); | |
460b9539 | 163 | if(need & DC_RECENT) { |
164 | /* we need to reverse the order of the list */ | |
165 | disorder_recent(ds->g->client, &r); | |
166 | while(r) { | |
167 | rnext = r->next; | |
168 | r->next = ds->g->recent; | |
169 | ds->g->recent = r; | |
170 | r = rnext; | |
171 | } | |
172 | } | |
173 | if(need & DC_VOLUME) | |
174 | disorder_get_volume(ds->g->client, | |
175 | &ds->g->volume_left, &ds->g->volume_right); | |
176 | if(need & (DC_FILES|DC_DIRS)) { | |
177 | if(!(dir = cgi_get("directory"))) | |
178 | dir = ""; | |
179 | re = cgi_get("regexp"); | |
180 | if(need & DC_DIRS) | |
181 | if(disorder_directories(ds->g->client, dir, re, | |
182 | &ds->g->dirs, &ds->g->ndirs)) | |
183 | ds->g->ndirs = 0; | |
184 | if(need & DC_FILES) | |
185 | if(disorder_files(ds->g->client, dir, re, | |
186 | &ds->g->files, &ds->g->nfiles)) | |
187 | ds->g->nfiles = 0; | |
188 | } | |
938d8157 | 189 | if(need & DC_RIGHTS) { |
190 | ds->g->rights = RIGHT_READ; /* fail-safe */ | |
191 | if(!disorder_userinfo(ds->g->client, disorder_user(ds->g->client), | |
192 | "rights", &rights)) | |
193 | parse_rights(rights, &ds->g->rights, 1); | |
194 | } | |
460b9539 | 195 | ds->g->flags |= need; |
196 | } | |
197 | } | |
198 | ||
199 | /* actions ********************************************************************/ | |
200 | ||
201 | static void act_disable(cgi_sink *output, | |
202 | dcgi_state *ds) { | |
203 | if(ds->g->client) | |
204 | disorder_disable(ds->g->client); | |
205 | redirect(output->sink); | |
206 | } | |
207 | ||
208 | static void act_enable(cgi_sink *output, | |
209 | dcgi_state *ds) { | |
210 | if(ds->g->client) | |
211 | disorder_enable(ds->g->client); | |
212 | redirect(output->sink); | |
213 | } | |
214 | ||
215 | static void act_random_disable(cgi_sink *output, | |
216 | dcgi_state *ds) { | |
217 | if(ds->g->client) | |
218 | disorder_random_disable(ds->g->client); | |
219 | redirect(output->sink); | |
220 | } | |
221 | ||
222 | static void act_random_enable(cgi_sink *output, | |
223 | dcgi_state *ds) { | |
224 | if(ds->g->client) | |
225 | disorder_random_enable(ds->g->client); | |
226 | redirect(output->sink); | |
227 | } | |
228 | ||
229 | static void act_remove(cgi_sink *output, | |
230 | dcgi_state *ds) { | |
231 | const char *id; | |
232 | ||
233 | if(!(id = cgi_get("id"))) fatal(0, "missing id argument"); | |
234 | if(ds->g->client) | |
235 | disorder_remove(ds->g->client, id); | |
236 | redirect(output->sink); | |
237 | } | |
238 | ||
239 | static void act_move(cgi_sink *output, | |
240 | dcgi_state *ds) { | |
241 | const char *id, *delta; | |
242 | ||
243 | if(!(id = cgi_get("id"))) fatal(0, "missing id argument"); | |
244 | if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument"); | |
245 | if(ds->g->client) | |
246 | disorder_move(ds->g->client, id, atoi(delta)); | |
247 | redirect(output->sink); | |
248 | } | |
249 | ||
250 | static void act_scratch(cgi_sink *output, | |
251 | dcgi_state *ds) { | |
252 | if(ds->g->client) | |
253 | disorder_scratch(ds->g->client, cgi_get("id")); | |
254 | redirect(output->sink); | |
255 | } | |
256 | ||
257 | static void act_playing(cgi_sink *output, dcgi_state *ds) { | |
258 | char r[1024]; | |
259 | long refresh = config->refresh, length; | |
260 | time_t now, fin; | |
261 | int random_enabled = 0; | |
262 | int enabled = 0; | |
263 | ||
264 | lookups(ds, DC_PLAYING|DC_QUEUE); | |
265 | cgi_header(output->sink, "Content-Type", "text/html"); | |
266 | disorder_random_enabled(ds->g->client, &random_enabled); | |
267 | disorder_enabled(ds->g->client, &enabled); | |
268 | if(ds->g->playing | |
269 | && ds->g->playing->state == playing_started /* i.e. not paused */ | |
270 | && !disorder_length(ds->g->client, ds->g->playing->track, &length) | |
271 | && length | |
272 | && ds->g->playing->sofar >= 0) { | |
273 | /* Try to put the next refresh at the start of the next track. */ | |
274 | time(&now); | |
275 | fin = now + length - ds->g->playing->sofar + config->gap; | |
276 | if(now + refresh > fin) | |
277 | refresh = fin - now; | |
278 | } | |
279 | if(ds->g->queue && ds->g->queue->state == playing_isscratch) { | |
280 | /* next track is a scratch, don't leave more than the inter-track gap */ | |
281 | if(refresh > config->gap) | |
282 | refresh = config->gap; | |
283 | } | |
284 | if(!ds->g->playing && ((ds->g->queue | |
285 | && ds->g->queue->state != playing_random) | |
286 | || random_enabled) && enabled) { | |
287 | /* no track playing but playing is enabled and there is something coming | |
288 | * up, must be in a gap */ | |
289 | if(refresh > config->gap) | |
290 | refresh = config->gap; | |
291 | } | |
292 | byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1, | |
293 | front_url()); | |
294 | cgi_header(output->sink, "Refresh", r); | |
7f66f58b | 295 | header_cookie(output->sink); |
460b9539 | 296 | cgi_body(output->sink); |
297 | expand(output, "playing", ds); | |
298 | } | |
299 | ||
300 | static void act_play(cgi_sink *output, | |
301 | dcgi_state *ds) { | |
302 | const char *track, *dir; | |
303 | char **tracks; | |
304 | int ntracks, n; | |
305 | struct entry *e; | |
306 | ||
307 | if((track = cgi_get("file"))) { | |
308 | disorder_play(ds->g->client, track); | |
309 | } else if((dir = cgi_get("directory"))) { | |
310 | if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0; | |
311 | if(ntracks) { | |
312 | e = xmalloc(ntracks * sizeof (struct entry)); | |
313 | for(n = 0; n < ntracks; ++n) { | |
314 | e[n].path = tracks[n]; | |
315 | e[n].sort = trackname_transform("track", tracks[n], "sort"); | |
316 | e[n].display = trackname_transform("track", tracks[n], "display"); | |
317 | } | |
318 | qsort(e, ntracks, sizeof (struct entry), compare_entry); | |
319 | for(n = 0; n < ntracks; ++n) | |
320 | disorder_play(ds->g->client, e[n].path); | |
321 | } | |
322 | } | |
323 | /* XXX error handling */ | |
324 | redirect(output->sink); | |
325 | } | |
326 | ||
327 | static int clamp(int n, int min, int max) { | |
328 | if(n < min) | |
329 | return min; | |
330 | if(n > max) | |
331 | return max; | |
332 | return n; | |
333 | } | |
334 | ||
335 | static const char *volume_url(void) { | |
336 | char *url; | |
337 | ||
338 | byte_xasprintf(&url, "%s?action=volume", config->url); | |
339 | return url; | |
340 | } | |
341 | ||
342 | static void act_volume(cgi_sink *output, dcgi_state *ds) { | |
343 | const char *l, *r, *d, *back; | |
344 | int nd, changed = 0;; | |
345 | ||
346 | if((d = cgi_get("delta"))) { | |
347 | lookups(ds, DC_VOLUME); | |
348 | nd = clamp(atoi(d), -255, 255); | |
349 | disorder_set_volume(ds->g->client, | |
350 | clamp(ds->g->volume_left + nd, 0, 255), | |
351 | clamp(ds->g->volume_right + nd, 0, 255)); | |
352 | changed = 1; | |
353 | } else if((l = cgi_get("left")) && (r = cgi_get("right"))) { | |
354 | disorder_set_volume(ds->g->client, atoi(l), atoi(r)); | |
355 | changed = 1; | |
356 | } | |
357 | if(changed) { | |
358 | /* redirect back to ourselves (but without the volume-changing bits in the | |
359 | * URL) */ | |
360 | cgi_header(output->sink, "Location", | |
361 | (back = cgi_get("back")) ? back : volume_url()); | |
7f66f58b | 362 | header_cookie(output->sink); |
460b9539 | 363 | cgi_body(output->sink); |
364 | } else { | |
365 | cgi_header(output->sink, "Content-Type", "text/html"); | |
7f66f58b | 366 | header_cookie(output->sink); |
460b9539 | 367 | cgi_body(output->sink); |
368 | expand(output, "volume", ds); | |
369 | } | |
370 | } | |
371 | ||
372 | static void act_prefs_errors(const char *msg, | |
373 | void attribute((unused)) *u) { | |
374 | fatal(0, "error splitting parts list: %s", msg); | |
375 | } | |
376 | ||
377 | static const char *numbered_arg(const char *argname, int numfile) { | |
378 | char *fullname; | |
379 | ||
380 | byte_xasprintf(&fullname, "%d_%s", numfile, argname); | |
381 | return cgi_get(fullname); | |
382 | } | |
383 | ||
384 | static void process_prefs(dcgi_state *ds, int numfile) { | |
385 | const char *file, *name, *value, *part, *parts, *current, *context; | |
386 | char **partslist; | |
387 | ||
388 | if(!(file = numbered_arg("file", numfile))) | |
389 | /* The first file doesn't need numbering. */ | |
390 | if(numfile > 0 || !(file = cgi_get("file"))) | |
391 | return; | |
392 | if((parts = numbered_arg("parts", numfile)) | |
393 | || (parts = cgi_get("parts"))) { | |
394 | /* Default context is display. Other contexts not actually tested. */ | |
395 | if(!(context = numbered_arg("context", numfile))) context = "display"; | |
396 | partslist = split(parts, 0, 0, act_prefs_errors, 0); | |
397 | while((part = *partslist++)) { | |
398 | if(!(value = numbered_arg(part, numfile))) | |
399 | continue; | |
400 | /* If it's already right (whether regexps or db) don't change anything, | |
401 | * so we don't fill the database up with rubbish. */ | |
402 | if(disorder_part(ds->g->client, (char **)¤t, | |
403 | file, context, part)) | |
404 | fatal(0, "disorder_part() failed"); | |
405 | if(!strcmp(current, value)) | |
406 | continue; | |
407 | byte_xasprintf((char **)&name, "trackname_%s_%s", context, part); | |
408 | disorder_set(ds->g->client, file, name, value); | |
409 | } | |
410 | if((value = numbered_arg("random", numfile))) | |
411 | disorder_unset(ds->g->client, file, "pick_at_random"); | |
412 | else | |
413 | disorder_set(ds->g->client, file, "pick_at_random", "0"); | |
414 | if((value = numbered_arg("tags", numfile))) | |
415 | disorder_set(ds->g->client, file, "tags", value); | |
416 | } else if((name = cgi_get("name"))) { | |
417 | /* Raw preferences. Not well supported in the templates at the moment. */ | |
418 | value = cgi_get("value"); | |
419 | if(value) | |
420 | disorder_set(ds->g->client, file, name, value); | |
421 | else | |
422 | disorder_unset(ds->g->client, file, name); | |
423 | } | |
424 | } | |
425 | ||
426 | static void act_prefs(cgi_sink *output, dcgi_state *ds) { | |
427 | const char *files; | |
428 | int nfiles, numfile; | |
429 | ||
430 | if((files = cgi_get("files"))) nfiles = atoi(files); | |
431 | else nfiles = 1; | |
432 | for(numfile = 0; numfile < nfiles; ++numfile) | |
433 | process_prefs(ds, numfile); | |
434 | cgi_header(output->sink, "Content-Type", "text/html"); | |
7f66f58b | 435 | header_cookie(output->sink); |
460b9539 | 436 | cgi_body(output->sink); |
437 | expand(output, "prefs", ds); | |
438 | } | |
439 | ||
440 | static void act_pause(cgi_sink *output, | |
441 | dcgi_state *ds) { | |
442 | if(ds->g->client) | |
443 | disorder_pause(ds->g->client); | |
444 | redirect(output->sink); | |
445 | } | |
446 | ||
447 | static void act_resume(cgi_sink *output, | |
448 | dcgi_state *ds) { | |
449 | if(ds->g->client) | |
450 | disorder_resume(ds->g->client); | |
451 | redirect(output->sink); | |
452 | } | |
453 | ||
fdf98378 | 454 | static void act_login(cgi_sink *output, |
455 | dcgi_state *ds) { | |
456 | const char *username, *password, *back; | |
457 | disorder_client *c; | |
458 | ||
459 | username = cgi_get("username"); | |
460 | password = cgi_get("password"); | |
461 | if(!username || !password | |
462 | || !strcmp(username, "guest")/*bodge to avoid guest cookies*/) { | |
463 | /* We're just visiting the login page */ | |
464 | expand_template(ds, output, "login"); | |
465 | return; | |
466 | } | |
fb6aa8d1 | 467 | /* We'll need a new connection as we are going to stop being guest */ |
468 | c = disorder_new(0); | |
fdf98378 | 469 | if(disorder_connect_user(c, username, password)) { |
470 | cgi_set_option("error", "loginfailed"); | |
471 | expand_template(ds, output, "login"); | |
472 | return; | |
473 | } | |
474 | if(disorder_make_cookie(c, &login_cookie)) { | |
475 | cgi_set_option("error", "cookiefailed"); | |
476 | expand_template(ds, output, "login"); | |
477 | return; | |
478 | } | |
fb6aa8d1 | 479 | /* Use the new connection henceforth */ |
480 | ds->g->client = c; | |
481 | ds->g->flags = 0; | |
fdf98378 | 482 | /* We have a new cookie */ |
7f66f58b | 483 | header_cookie(output->sink); |
ac152d06 | 484 | cgi_set_option("status", "loginok"); |
485 | if((back = cgi_get("back")) && *back) | |
fdf98378 | 486 | /* Redirect back to somewhere or other */ |
487 | redirect(output->sink); | |
488 | else | |
489 | /* Stick to the login page */ | |
490 | expand_template(ds, output, "login"); | |
491 | } | |
492 | ||
b42ffad6 | 493 | static void act_logout(cgi_sink *output, |
494 | dcgi_state *ds) { | |
495 | disorder_revoke(ds->g->client); | |
496 | login_cookie = 0; | |
7f66f58b | 497 | /* Reconnect as guest */ |
938d8157 | 498 | disorder_cgi_login(ds, output); |
b42ffad6 | 499 | /* Back to the login page */ |
ac152d06 | 500 | cgi_set_option("status", "logoutok"); |
b42ffad6 | 501 | expand_template(ds, output, "login"); |
502 | } | |
503 | ||
fdf98378 | 504 | static void act_register(cgi_sink *output, |
505 | dcgi_state *ds) { | |
968f044a | 506 | const char *username, *password, *password2, *email; |
bb6ae3fb | 507 | char *confirm, *content_type; |
508 | const char *text, *encoding, *charset; | |
fdf98378 | 509 | |
510 | username = cgi_get("username"); | |
968f044a | 511 | password = cgi_get("password1"); |
512 | password2 = cgi_get("password2"); | |
fdf98378 | 513 | email = cgi_get("email"); |
514 | ||
515 | if(!username || !*username) { | |
516 | cgi_set_option("error", "nousername"); | |
517 | expand_template(ds, output, "login"); | |
518 | return; | |
519 | } | |
520 | if(!password || !*password) { | |
521 | cgi_set_option("error", "nopassword"); | |
522 | expand_template(ds, output, "login"); | |
523 | return; | |
524 | } | |
968f044a | 525 | if(!password2 || !*password2 || strcmp(password, password2)) { |
526 | cgi_set_option("error", "passwordmismatch"); | |
527 | expand_template(ds, output, "login"); | |
528 | return; | |
529 | } | |
fdf98378 | 530 | if(!email || !*email) { |
531 | cgi_set_option("error", "noemail"); | |
532 | expand_template(ds, output, "login"); | |
533 | return; | |
534 | } | |
535 | /* We could well do better address validation but for now we'll just do the | |
536 | * minimum */ | |
537 | if(!strchr(email, '@')) { | |
538 | cgi_set_option("error", "bademail"); | |
539 | expand_template(ds, output, "login"); | |
540 | return; | |
541 | } | |
542 | if(disorder_register(ds->g->client, username, password, email, &confirm)) { | |
543 | cgi_set_option("error", "cannotregister"); | |
544 | expand_template(ds, output, "login"); | |
545 | return; | |
546 | } | |
bb6ae3fb | 547 | /* Send the user a mail */ |
548 | /* TODO templatize this */ | |
549 | byte_xasprintf((char **)&text, | |
550 | "Welcome to DisOrder. To active your login, please visit this URL:\n" | |
551 | "\n" | |
10114017 | 552 | "%s?c=%s\n", config->url, urlencodestring(confirm)); |
bb6ae3fb | 553 | if(!(text = mime_encode_text(text, &charset, &encoding))) |
554 | fatal(0, "cannot encode email"); | |
555 | byte_xasprintf(&content_type, "text/plain;charset=%s", | |
556 | quote822(charset, 0)); | |
557 | sendmail("", config->mail_sender, email, "Welcome to DisOrder", | |
558 | encoding, content_type, text); /* TODO error checking */ | |
fdf98378 | 559 | /* We'll go back to the login page with a suitable message */ |
ac152d06 | 560 | cgi_set_option("status", "registered"); |
fdf98378 | 561 | expand_template(ds, output, "login"); |
562 | } | |
563 | ||
230209f7 | 564 | static void act_confirm(cgi_sink *output, |
565 | dcgi_state *ds) { | |
566 | const char *confirmation; | |
567 | ||
10114017 | 568 | if(!(confirmation = cgi_get("c"))) { |
230209f7 | 569 | cgi_set_option("error", "noconfirm"); |
570 | expand_template(ds, output, "login"); | |
571 | } | |
30365519 | 572 | /* Confirm our registration */ |
230209f7 | 573 | if(disorder_confirm(ds->g->client, confirmation)) { |
574 | cgi_set_option("error", "badconfirm"); | |
575 | expand_template(ds, output, "login"); | |
576 | } | |
30365519 | 577 | /* Get a cookie */ |
578 | if(disorder_make_cookie(ds->g->client, &login_cookie)) { | |
579 | cgi_set_option("error", "cookiefailed"); | |
580 | expand_template(ds, output, "login"); | |
581 | return; | |
582 | } | |
583 | /* Discard any cached data JIC */ | |
584 | ds->g->flags = 0; | |
585 | /* We have a new cookie */ | |
586 | header_cookie(output->sink); | |
ac152d06 | 587 | cgi_set_option("status", "confirmed"); |
230209f7 | 588 | expand_template(ds, output, "login"); |
589 | } | |
590 | ||
968f044a | 591 | static void act_edituser(cgi_sink *output, |
592 | dcgi_state *ds) { | |
593 | const char *email = cgi_get("email"), *password = cgi_get("changepassword1"); | |
594 | const char *password2 = cgi_get("changepassword2"); | |
595 | int newpassword = 0; | |
596 | disorder_client *c; | |
597 | ||
598 | if((password && *password) || (password && *password2)) { | |
599 | if(!password || !password2 || strcmp(password, password2)) { | |
600 | cgi_set_option("error", "passwordmismatch"); | |
601 | expand_template(ds, output, "login"); | |
602 | return; | |
603 | } | |
604 | } else | |
605 | password = password2 = 0; | |
606 | ||
607 | if(email) { | |
608 | if(disorder_edituser(ds->g->client, disorder_user(ds->g->client), | |
609 | "email", email)) { | |
610 | cgi_set_option("error", "badedit"); | |
611 | expand_template(ds, output, "login"); | |
612 | return; | |
613 | } | |
614 | } | |
615 | if(password) { | |
616 | if(disorder_edituser(ds->g->client, disorder_user(ds->g->client), | |
617 | "password", password)) { | |
618 | cgi_set_option("error", "badedit"); | |
619 | expand_template(ds, output, "login"); | |
620 | return; | |
621 | } | |
622 | newpassword = 1; | |
623 | } | |
624 | if(newpassword) { | |
625 | login_cookie = 0; /* it'll be invalid now */ | |
626 | /* This is a bit duplicative of act_login() */ | |
627 | c = disorder_new(0); | |
628 | if(disorder_connect_user(c, disorder_user(ds->g->client), password)) { | |
629 | cgi_set_option("error", "loginfailed"); | |
630 | expand_template(ds, output, "login"); | |
631 | return; | |
632 | } | |
633 | if(disorder_make_cookie(c, &login_cookie)) { | |
634 | cgi_set_option("error", "cookiefailed"); | |
635 | expand_template(ds, output, "login"); | |
636 | return; | |
637 | } | |
638 | /* Use the new connection henceforth */ | |
639 | ds->g->client = c; | |
640 | ds->g->flags = 0; | |
641 | /* We have a new cookie */ | |
642 | header_cookie(output->sink); | |
643 | } | |
644 | cgi_set_option("status", "edited"); | |
645 | expand_template(ds, output, "login"); | |
646 | } | |
647 | ||
460b9539 | 648 | static const struct action { |
649 | const char *name; | |
650 | void (*handler)(cgi_sink *output, dcgi_state *ds); | |
651 | } actions[] = { | |
230209f7 | 652 | { "confirm", act_confirm }, |
460b9539 | 653 | { "disable", act_disable }, |
968f044a | 654 | { "edituser", act_edituser }, |
460b9539 | 655 | { "enable", act_enable }, |
fdf98378 | 656 | { "login", act_login }, |
b42ffad6 | 657 | { "logout", act_logout }, |
460b9539 | 658 | { "move", act_move }, |
659 | { "pause", act_pause }, | |
660 | { "play", act_play }, | |
661 | { "playing", act_playing }, | |
662 | { "prefs", act_prefs }, | |
663 | { "random-disable", act_random_disable }, | |
664 | { "random-enable", act_random_enable }, | |
fdf98378 | 665 | { "register", act_register }, |
460b9539 | 666 | { "remove", act_remove }, |
667 | { "resume", act_resume }, | |
668 | { "scratch", act_scratch }, | |
669 | { "volume", act_volume }, | |
670 | }; | |
671 | ||
672 | /* expansions *****************************************************************/ | |
673 | ||
674 | static void exp_include(int attribute((unused)) nargs, | |
675 | char **args, | |
676 | cgi_sink *output, | |
677 | void *u) { | |
678 | expand(output, args[0], u); | |
679 | } | |
680 | ||
681 | static void exp_server_version(int attribute((unused)) nargs, | |
682 | char attribute((unused)) **args, | |
683 | cgi_sink *output, | |
684 | void *u) { | |
685 | dcgi_state *ds = u; | |
686 | const char *v; | |
687 | ||
688 | if(ds->g->client) { | |
689 | if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)"; | |
690 | } else | |
691 | v = "(server not running)"; | |
692 | cgi_output(output, "%s", v); | |
693 | } | |
694 | ||
695 | static void exp_version(int attribute((unused)) nargs, | |
696 | char attribute((unused)) **args, | |
697 | cgi_sink *output, | |
698 | void attribute((unused)) *u) { | |
a05e4467 | 699 | cgi_output(output, "%s", disorder_short_version_string); |
460b9539 | 700 | } |
701 | ||
702 | static void exp_nonce(int attribute((unused)) nargs, | |
703 | char attribute((unused)) **args, | |
704 | cgi_sink *output, | |
705 | void attribute((unused)) *u) { | |
706 | cgi_output(output, "%s", nonce()); | |
707 | } | |
708 | ||
709 | static void exp_label(int attribute((unused)) nargs, | |
710 | char **args, | |
711 | cgi_sink *output, | |
712 | void attribute((unused)) *u) { | |
713 | cgi_output(output, "%s", cgi_label(args[0])); | |
714 | } | |
715 | ||
716 | struct trackinfo_state { | |
717 | dcgi_state *ds; | |
718 | const struct queue_entry *q; | |
719 | long length; | |
720 | time_t when; | |
721 | }; | |
722 | ||
723 | static void exp_who(int attribute((unused)) nargs, | |
724 | char attribute((unused)) **args, | |
725 | cgi_sink *output, | |
726 | void *u) { | |
727 | dcgi_state *ds = u; | |
728 | ||
729 | if(ds->track && ds->track->submitter) | |
730 | cgi_output(output, "%s", ds->track->submitter); | |
731 | } | |
732 | ||
733 | static void exp_length(int attribute((unused)) nargs, | |
734 | char attribute((unused)) **args, | |
735 | cgi_sink *output, | |
736 | void *u) { | |
737 | dcgi_state *ds = u; | |
78efa64e | 738 | long length = 0; |
460b9539 | 739 | |
740 | if(ds->track | |
741 | && (ds->track->state == playing_started | |
742 | || ds->track->state == playing_paused) | |
743 | && ds->track->sofar >= 0) | |
744 | cgi_output(output, "%ld:%02ld/", | |
745 | ds->track->sofar / 60, ds->track->sofar % 60); | |
78efa64e RK |
746 | length = 0; |
747 | if(ds->track) | |
748 | disorder_length(ds->g->client, ds->track->track, &length); | |
749 | else if(ds->tracks) | |
750 | disorder_length(ds->g->client, ds->tracks[0], &length); | |
460b9539 | 751 | if(length) |
752 | cgi_output(output, "%ld:%02ld", length / 60, length % 60); | |
753 | else | |
754 | sink_printf(output->sink, "%s", " "); | |
755 | } | |
756 | ||
757 | static void exp_when(int attribute((unused)) nargs, | |
758 | char attribute((unused)) **args, | |
759 | cgi_sink *output, | |
760 | void *u) { | |
761 | dcgi_state *ds = u; | |
762 | const struct tm *w = 0; | |
763 | ||
764 | if(ds->track) | |
765 | switch(ds->track->state) { | |
766 | case playing_isscratch: | |
767 | case playing_unplayed: | |
768 | case playing_random: | |
769 | if(ds->track->expected) | |
770 | w = localtime(&ds->track->expected); | |
771 | break; | |
772 | case playing_failed: | |
773 | case playing_no_player: | |
774 | case playing_ok: | |
775 | case playing_scratched: | |
776 | case playing_started: | |
777 | case playing_paused: | |
778 | case playing_quitting: | |
779 | if(ds->track->played) | |
780 | w = localtime(&ds->track->played); | |
781 | break; | |
782 | } | |
783 | if(w) | |
784 | cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min); | |
785 | else | |
786 | sink_printf(output->sink, " "); | |
787 | } | |
788 | ||
789 | static void exp_part(int nargs, | |
790 | char **args, | |
791 | cgi_sink *output, | |
792 | void *u) { | |
793 | dcgi_state *ds = u; | |
794 | const char *s, *track, *part, *context; | |
795 | ||
796 | if(nargs == 3) | |
797 | track = args[2]; | |
798 | else { | |
799 | if(ds->track) | |
800 | track = ds->track->track; | |
801 | else if(ds->tracks) | |
802 | track = ds->tracks[0]; | |
803 | else | |
804 | track = 0; | |
805 | } | |
806 | if(track) { | |
807 | switch(nargs) { | |
808 | case 1: | |
809 | context = "display"; | |
810 | part = args[0]; | |
811 | break; | |
812 | case 2: | |
813 | case 3: | |
814 | context = args[0]; | |
815 | part = args[1]; | |
816 | break; | |
817 | default: | |
818 | abort(); | |
819 | } | |
61507e3c RK |
820 | if(disorder_part(ds->g->client, (char **)&s, track, |
821 | !strcmp(context, "short") ? "display" : context, part)) | |
460b9539 | 822 | fatal(0, "disorder_part() failed"); |
61507e3c RK |
823 | if(!strcmp(context, "short")) |
824 | s = truncate_for_display(s, config->short_display); | |
460b9539 | 825 | cgi_output(output, "%s", s); |
826 | } else | |
827 | sink_printf(output->sink, " "); | |
828 | } | |
829 | ||
830 | static void exp_playing(int attribute((unused)) nargs, | |
831 | char **args, | |
832 | cgi_sink *output, | |
833 | void *u) { | |
834 | dcgi_state *ds = u; | |
835 | dcgi_state s; | |
836 | ||
837 | lookups(ds, DC_PLAYING); | |
838 | memset(&s, 0, sizeof s); | |
839 | s.g = ds->g; | |
840 | if(ds->g->playing) { | |
841 | s.track = ds->g->playing; | |
842 | expandstring(output, args[0], &s); | |
843 | } | |
844 | } | |
845 | ||
846 | static void exp_queue(int attribute((unused)) nargs, | |
847 | char **args, | |
848 | cgi_sink *output, | |
849 | void *u) { | |
850 | dcgi_state *ds = u; | |
851 | dcgi_state s; | |
852 | struct queue_entry *q; | |
853 | ||
854 | lookups(ds, DC_QUEUE); | |
855 | memset(&s, 0, sizeof s); | |
856 | s.g = ds->g; | |
857 | s.first = 1; | |
858 | for(q = ds->g->queue; q; q = q->next) { | |
859 | s.last = !q->next; | |
860 | s.track = q; | |
861 | expandstring(output, args[0], &s); | |
862 | s.index++; | |
863 | s.first = 0; | |
864 | } | |
865 | } | |
866 | ||
867 | static void exp_recent(int attribute((unused)) nargs, | |
868 | char **args, | |
869 | cgi_sink *output, | |
870 | void *u) { | |
871 | dcgi_state *ds = u; | |
872 | dcgi_state s; | |
873 | struct queue_entry *q; | |
874 | ||
875 | lookups(ds, DC_RECENT); | |
876 | memset(&s, 0, sizeof s); | |
877 | s.g = ds->g; | |
878 | s.first = 1; | |
879 | for(q = ds->g->recent; q; q = q->next) { | |
880 | s.last = !q; | |
881 | s.track = q; | |
882 | expandstring(output, args[0], &s); | |
883 | s.index++; | |
884 | s.first = 0; | |
885 | } | |
886 | } | |
887 | ||
78efa64e RK |
888 | static void exp_new(int attribute((unused)) nargs, |
889 | char **args, | |
890 | cgi_sink *output, | |
891 | void *u) { | |
892 | dcgi_state *ds = u; | |
893 | dcgi_state s; | |
894 | ||
895 | lookups(ds, DC_NEW); | |
896 | memset(&s, 0, sizeof s); | |
897 | s.g = ds->g; | |
898 | s.first = 1; | |
899 | for(s.index = 0; s.index < ds->g->nnew; ++s.index) { | |
900 | s.last = s.index + 1 < ds->g->nnew; | |
901 | s.tracks = &ds->g->new[s.index]; | |
902 | expandstring(output, args[0], &s); | |
903 | s.first = 0; | |
904 | } | |
905 | } | |
906 | ||
460b9539 | 907 | static void exp_url(int attribute((unused)) nargs, |
908 | char attribute((unused)) **args, | |
909 | cgi_sink *output, | |
910 | void attribute((unused)) *u) { | |
911 | cgi_output(output, "%s", config->url); | |
912 | } | |
913 | ||
914 | struct result { | |
915 | char *track; | |
916 | const char *sort; | |
917 | }; | |
918 | ||
919 | static int compare_result(const void *a, const void *b) { | |
920 | const struct result *ra = a, *rb = b; | |
921 | int c; | |
922 | ||
923 | if(!(c = strcmp(ra->sort, rb->sort))) | |
924 | c = strcmp(ra->track, rb->track); | |
925 | return c; | |
926 | } | |
927 | ||
928 | static void exp_search(int nargs, | |
929 | char **args, | |
930 | cgi_sink *output, | |
931 | void *u) { | |
932 | dcgi_state *ds = u, substate; | |
933 | char **tracks; | |
934 | const char *q, *context, *part, *template; | |
935 | int ntracks, n, m; | |
936 | struct result *r; | |
937 | ||
938 | switch(nargs) { | |
939 | case 2: | |
940 | part = args[0]; | |
941 | context = "sort"; | |
942 | template = args[1]; | |
943 | break; | |
944 | case 3: | |
945 | part = args[0]; | |
946 | context = args[1]; | |
947 | template = args[2]; | |
948 | break; | |
949 | default: | |
950 | assert(!"should never happen"); | |
951 | part = context = template = 0; /* quieten compiler */ | |
952 | } | |
953 | if(ds->tracks == 0) { | |
954 | /* we are the top level, let's get some search results */ | |
955 | if(!(q = cgi_get("query"))) return; /* no results yet */ | |
956 | if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return; | |
957 | if(!ntracks) return; | |
958 | } else { | |
959 | tracks = ds->tracks; | |
960 | ntracks = ds->ntracks; | |
961 | } | |
962 | assert(ntracks != 0); | |
963 | /* sort tracks by the appropriate part */ | |
964 | r = xmalloc(ntracks * sizeof *r); | |
965 | for(n = 0; n < ntracks; ++n) { | |
966 | r[n].track = tracks[n]; | |
967 | if(disorder_part(ds->g->client, (char **)&r[n].sort, | |
968 | tracks[n], context, part)) | |
969 | fatal(0, "disorder_part() failed"); | |
970 | } | |
971 | qsort(r, ntracks, sizeof (struct result), compare_result); | |
972 | /* expand the 2nd arg once for each group. We re-use the passed-in tracks | |
973 | * array as we know it's guaranteed to be big enough and isn't going to be | |
974 | * used for anything else any more. */ | |
975 | memset(&substate, 0, sizeof substate); | |
976 | substate.g = ds->g; | |
977 | substate.first = 1; | |
978 | n = 0; | |
979 | while(n < ntracks) { | |
980 | substate.tracks = tracks; | |
981 | substate.ntracks = 0; | |
982 | m = n; | |
983 | while(m < ntracks | |
984 | && !strcmp(r[m].sort, r[n].sort)) | |
985 | tracks[substate.ntracks++] = r[m++].track; | |
986 | substate.last = (m == ntracks); | |
987 | expandstring(output, template, &substate); | |
988 | substate.index++; | |
989 | substate.first = 0; | |
990 | n = m; | |
991 | } | |
992 | assert(substate.last != 0); | |
993 | } | |
994 | ||
995 | static void exp_arg(int attribute((unused)) nargs, | |
996 | char **args, | |
997 | cgi_sink *output, | |
998 | void attribute((unused)) *u) { | |
999 | const char *v; | |
1000 | ||
1001 | if((v = cgi_get(args[0]))) | |
1002 | cgi_output(output, "%s", v); | |
1003 | } | |
1004 | ||
1005 | static void exp_stats(int attribute((unused)) nargs, | |
1006 | char attribute((unused)) **args, | |
1007 | cgi_sink *output, | |
1008 | void *u) { | |
1009 | dcgi_state *ds = u; | |
1010 | char **v; | |
1011 | ||
1012 | cgi_opentag(output->sink, "pre", "class", "stats", (char *)0); | |
1013 | if(!disorder_stats(ds->g->client, &v, 0)) { | |
1014 | while(*v) | |
1015 | cgi_output(output, "%s\n", *v++); | |
1016 | } | |
1017 | cgi_closetag(output->sink, "pre"); | |
1018 | } | |
1019 | ||
1020 | static void exp_volume(int attribute((unused)) nargs, | |
1021 | char **args, | |
1022 | cgi_sink *output, | |
1023 | void *u) { | |
1024 | dcgi_state *ds = u; | |
1025 | ||
1026 | lookups(ds, DC_VOLUME); | |
1027 | if(!strcmp(args[0], "left")) | |
1028 | cgi_output(output, "%d", ds->g->volume_left); | |
1029 | else | |
1030 | cgi_output(output, "%d", ds->g->volume_right); | |
1031 | } | |
1032 | ||
1033 | static void exp_shell(int attribute((unused)) nargs, | |
1034 | char **args, | |
1035 | cgi_sink *output, | |
1036 | void attribute((unused)) *u) { | |
1037 | int w, p[2], n; | |
1038 | char buffer[4096]; | |
1039 | pid_t pid; | |
1040 | ||
1041 | xpipe(p); | |
1042 | if(!(pid = xfork())) { | |
1043 | exitfn = _exit; | |
1044 | xclose(p[0]); | |
1045 | xdup2(p[1], 1); | |
1046 | xclose(p[1]); | |
1047 | execlp("sh", "sh", "-c", args[0], (char *)0); | |
1048 | fatal(errno, "error executing sh"); | |
1049 | } | |
1050 | xclose(p[1]); | |
1051 | while((n = read(p[0], buffer, sizeof buffer))) { | |
1052 | if(n < 0) { | |
1053 | if(errno == EINTR) continue; | |
1054 | else fatal(errno, "error reading from pipe"); | |
1055 | } | |
1056 | output->sink->write(output->sink, buffer, n); | |
1057 | } | |
1058 | xclose(p[0]); | |
1059 | while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR) | |
1060 | ; | |
1061 | if(n < 0) fatal(errno, "error calling waitpid"); | |
1062 | if(w) | |
1063 | error(0, "shell command '%s' %s", args[0], wstat(w)); | |
1064 | } | |
1065 | ||
1066 | static inline int str2bool(const char *s) { | |
1067 | return !strcmp(s, "true"); | |
1068 | } | |
1069 | ||
1070 | static inline const char *bool2str(int n) { | |
1071 | return n ? "true" : "false"; | |
1072 | } | |
1073 | ||
1074 | static char *expandarg(const char *arg, dcgi_state *ds) { | |
1075 | struct dynstr d; | |
1076 | cgi_sink output; | |
1077 | ||
1078 | dynstr_init(&d); | |
1079 | output.quote = 0; | |
1080 | output.sink = sink_dynstr(&d); | |
1081 | expandstring(&output, arg, ds); | |
1082 | dynstr_terminate(&d); | |
1083 | return d.vec; | |
1084 | } | |
1085 | ||
1086 | static void exp_prefs(int attribute((unused)) nargs, | |
1087 | char **args, | |
1088 | cgi_sink *output, | |
1089 | void *u) { | |
1090 | dcgi_state *ds = u; | |
1091 | dcgi_state substate; | |
1092 | struct kvp *k; | |
1093 | const char *file = expandarg(args[0], ds); | |
1094 | ||
1095 | memset(&substate, 0, sizeof substate); | |
1096 | substate.g = ds->g; | |
1097 | substate.first = 1; | |
1098 | if(disorder_prefs(ds->g->client, file, &k)) return; | |
1099 | while(k) { | |
1100 | substate.last = !k->next; | |
1101 | substate.pref = k; | |
1102 | expandstring(output, args[1], &substate); | |
1103 | ++substate.index; | |
1104 | k = k->next; | |
1105 | substate.first = 0; | |
1106 | } | |
1107 | } | |
1108 | ||
1109 | static void exp_pref(int attribute((unused)) nargs, | |
1110 | char **args, | |
1111 | cgi_sink *output, | |
1112 | void *u) { | |
1113 | char *value; | |
1114 | dcgi_state *ds = u; | |
1115 | ||
1116 | if(!disorder_get(ds->g->client, args[0], args[1], &value)) | |
1117 | cgi_output(output, "%s", value); | |
1118 | } | |
1119 | ||
1120 | static void exp_if(int nargs, | |
1121 | char **args, | |
1122 | cgi_sink *output, | |
1123 | void *u) { | |
1124 | dcgi_state *ds = u; | |
1125 | int n = str2bool(expandarg(args[0], ds)) ? 1 : 2; | |
1126 | ||
1127 | if(n < nargs) | |
1128 | expandstring(output, args[n], ds); | |
1129 | } | |
1130 | ||
1131 | static void exp_and(int nargs, | |
1132 | char **args, | |
1133 | cgi_sink *output, | |
1134 | void *u) { | |
1135 | dcgi_state *ds = u; | |
1136 | int n, result = 1; | |
1137 | ||
1138 | for(n = 0; n < nargs; ++n) | |
1139 | if(!str2bool(expandarg(args[n], ds))) { | |
1140 | result = 0; | |
1141 | break; | |
1142 | } | |
1143 | sink_printf(output->sink, "%s", bool2str(result)); | |
1144 | } | |
1145 | ||
1146 | static void exp_or(int nargs, | |
1147 | char **args, | |
1148 | cgi_sink *output, | |
1149 | void *u) { | |
1150 | dcgi_state *ds = u; | |
1151 | int n, result = 0; | |
1152 | ||
1153 | for(n = 0; n < nargs; ++n) | |
1154 | if(str2bool(expandarg(args[n], ds))) { | |
1155 | result = 1; | |
1156 | break; | |
1157 | } | |
1158 | sink_printf(output->sink, "%s", bool2str(result)); | |
1159 | } | |
1160 | ||
1161 | static void exp_not(int attribute((unused)) nargs, | |
1162 | char **args, | |
1163 | cgi_sink *output, | |
1164 | void attribute((unused)) *u) { | |
1165 | sink_printf(output->sink, "%s", bool2str(!str2bool(args[0]))); | |
1166 | } | |
1167 | ||
1168 | static void exp_isplaying(int attribute((unused)) nargs, | |
1169 | char attribute((unused)) **args, | |
1170 | cgi_sink *output, | |
1171 | void *u) { | |
1172 | dcgi_state *ds = u; | |
1173 | ||
1174 | lookups(ds, DC_PLAYING); | |
1175 | sink_printf(output->sink, "%s", bool2str(!!ds->g->playing)); | |
1176 | } | |
1177 | ||
1178 | static void exp_isqueue(int attribute((unused)) nargs, | |
1179 | char attribute((unused)) **args, | |
1180 | cgi_sink *output, | |
1181 | void *u) { | |
1182 | dcgi_state *ds = u; | |
1183 | ||
1184 | lookups(ds, DC_QUEUE); | |
1185 | sink_printf(output->sink, "%s", bool2str(!!ds->g->queue)); | |
1186 | } | |
1187 | ||
1188 | static void exp_isrecent(int attribute((unused)) nargs, | |
1189 | char attribute((unused)) **args, | |
1190 | cgi_sink *output, | |
1191 | void *u) { | |
1192 | dcgi_state *ds = u; | |
1193 | ||
1194 | lookups(ds, DC_RECENT); | |
1195 | sink_printf(output->sink, "%s", bool2str(!!ds->g->recent)); | |
1196 | } | |
1197 | ||
78efa64e RK |
1198 | static void exp_isnew(int attribute((unused)) nargs, |
1199 | char attribute((unused)) **args, | |
1200 | cgi_sink *output, | |
1201 | void *u) { | |
1202 | dcgi_state *ds = u; | |
1203 | ||
1204 | lookups(ds, DC_NEW); | |
1205 | sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew)); | |
1206 | } | |
1207 | ||
460b9539 | 1208 | static void exp_id(int attribute((unused)) nargs, |
1209 | char attribute((unused)) **args, | |
1210 | cgi_sink *output, | |
1211 | void *u) { | |
1212 | dcgi_state *ds = u; | |
1213 | ||
1214 | if(ds->track) | |
1215 | cgi_output(output, "%s", ds->track->id); | |
1216 | } | |
1217 | ||
1218 | static void exp_track(int attribute((unused)) nargs, | |
1219 | char attribute((unused)) **args, | |
1220 | cgi_sink *output, | |
1221 | void *u) { | |
1222 | dcgi_state *ds = u; | |
1223 | ||
1224 | if(ds->track) | |
1225 | cgi_output(output, "%s", ds->track->track); | |
1226 | } | |
1227 | ||
1228 | static void exp_parity(int attribute((unused)) nargs, | |
1229 | char attribute((unused)) **args, | |
1230 | cgi_sink *output, | |
1231 | void *u) { | |
1232 | dcgi_state *ds = u; | |
1233 | ||
1234 | cgi_output(output, "%s", ds->index % 2 ? "odd" : "even"); | |
1235 | } | |
1236 | ||
1237 | static void exp_comment(int attribute((unused)) nargs, | |
1238 | char attribute((unused)) **args, | |
1239 | cgi_sink attribute((unused)) *output, | |
1240 | void attribute((unused)) *u) { | |
1241 | /* do nothing */ | |
1242 | } | |
1243 | ||
1244 | static void exp_prefname(int attribute((unused)) nargs, | |
1245 | char attribute((unused)) **args, | |
1246 | cgi_sink *output, | |
1247 | void *u) { | |
1248 | dcgi_state *ds = u; | |
1249 | ||
1250 | if(ds->pref && ds->pref->name) | |
1251 | cgi_output(output, "%s", ds->pref->name); | |
1252 | } | |
1253 | ||
1254 | static void exp_prefvalue(int attribute((unused)) nargs, | |
1255 | char attribute((unused)) **args, | |
1256 | cgi_sink *output, | |
1257 | void *u) { | |
1258 | dcgi_state *ds = u; | |
1259 | ||
1260 | if(ds->pref && ds->pref->value) | |
1261 | cgi_output(output, "%s", ds->pref->value); | |
1262 | } | |
1263 | ||
1264 | static void exp_isfiles(int attribute((unused)) nargs, | |
1265 | char attribute((unused)) **args, | |
1266 | cgi_sink *output, | |
1267 | void *u) { | |
1268 | dcgi_state *ds = u; | |
1269 | ||
1270 | lookups(ds, DC_FILES); | |
1271 | sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles)); | |
1272 | } | |
1273 | ||
1274 | static void exp_isdirectories(int attribute((unused)) nargs, | |
1275 | char attribute((unused)) **args, | |
1276 | cgi_sink *output, | |
1277 | void *u) { | |
1278 | dcgi_state *ds = u; | |
1279 | ||
1280 | lookups(ds, DC_DIRS); | |
1281 | sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs)); | |
1282 | } | |
1283 | ||
1284 | static void exp_choose(int attribute((unused)) nargs, | |
1285 | char **args, | |
1286 | cgi_sink *output, | |
1287 | void *u) { | |
1288 | dcgi_state *ds = u; | |
1289 | dcgi_state substate; | |
1290 | int nfiles, n; | |
1291 | char **files; | |
1292 | struct entry *e; | |
1293 | const char *type, *what = expandarg(args[0], ds); | |
1294 | ||
1295 | if(!strcmp(what, "files")) { | |
1296 | lookups(ds, DC_FILES); | |
1297 | files = ds->g->files; | |
1298 | nfiles = ds->g->nfiles; | |
1299 | type = "track"; | |
1300 | } else if(!strcmp(what, "directories")) { | |
1301 | lookups(ds, DC_DIRS); | |
1302 | files = ds->g->dirs; | |
1303 | nfiles = ds->g->ndirs; | |
1304 | type = "dir"; | |
1305 | } else { | |
1306 | error(0, "unknown @choose@ argument '%s'", what); | |
1307 | return; | |
1308 | } | |
1309 | e = xmalloc(nfiles * sizeof (struct entry)); | |
1310 | for(n = 0; n < nfiles; ++n) { | |
1311 | e[n].path = files[n]; | |
1312 | e[n].sort = trackname_transform(type, files[n], "sort"); | |
1313 | e[n].display = trackname_transform(type, files[n], "display"); | |
1314 | } | |
1315 | qsort(e, nfiles, sizeof (struct entry), compare_entry); | |
1316 | memset(&substate, 0, sizeof substate); | |
1317 | substate.g = ds->g; | |
1318 | substate.first = 1; | |
1319 | for(n = 0; n < nfiles; ++n) { | |
1320 | substate.last = (n == nfiles - 1); | |
1321 | substate.index = n; | |
1322 | substate.entry = &e[n]; | |
1323 | expandstring(output, args[1], &substate); | |
1324 | substate.first = 0; | |
1325 | } | |
1326 | } | |
1327 | ||
1328 | static void exp_file(int attribute((unused)) nargs, | |
1329 | char attribute((unused)) **args, | |
1330 | cgi_sink *output, | |
1331 | void *u) { | |
1332 | dcgi_state *ds = u; | |
1333 | ||
1334 | if(ds->entry) | |
1335 | cgi_output(output, "%s", ds->entry->path); | |
1336 | else if(ds->track) | |
1337 | cgi_output(output, "%s", ds->track->track); | |
1338 | else if(ds->tracks) | |
1339 | cgi_output(output, "%s", ds->tracks[0]); | |
1340 | } | |
1341 | ||
1342 | static void exp_transform(int nargs, | |
1343 | char **args, | |
1344 | cgi_sink *output, | |
1345 | void attribute((unused)) *u) { | |
1346 | const char *context = nargs > 2 ? args[2] : "display"; | |
1347 | ||
1348 | cgi_output(output, "%s", trackname_transform(args[1], args[0], context)); | |
1349 | } | |
1350 | ||
1351 | static void exp_urlquote(int attribute((unused)) nargs, | |
1352 | char **args, | |
1353 | cgi_sink *output, | |
1354 | void attribute((unused)) *u) { | |
1355 | cgi_output(output, "%s", urlencodestring(args[0])); | |
1356 | } | |
1357 | ||
1358 | static void exp_scratchable(int attribute((unused)) nargs, | |
1359 | char attribute((unused)) **args, | |
1360 | cgi_sink *output, | |
1361 | void attribute((unused)) *u) { | |
1362 | dcgi_state *ds = u; | |
938d8157 | 1363 | |
1364 | lookups(ds, DC_PLAYING|DC_RIGHTS); | |
1365 | sink_printf(output->sink, "%s", | |
1366 | bool2str(right_scratchable(ds->g->rights, | |
1367 | disorder_user(ds->g->client), | |
1368 | ds->g->playing))); | |
460b9539 | 1369 | } |
1370 | ||
1371 | static void exp_removable(int attribute((unused)) nargs, | |
1372 | char attribute((unused)) **args, | |
1373 | cgi_sink *output, | |
1374 | void attribute((unused)) *u) { | |
1375 | dcgi_state *ds = u; | |
460b9539 | 1376 | |
938d8157 | 1377 | lookups(ds, DC_RIGHTS); |
1378 | sink_printf(output->sink, "%s", | |
1379 | bool2str(right_removable(ds->g->rights, | |
1380 | disorder_user(ds->g->client), | |
1381 | ds->track))); | |
1382 | } | |
1383 | ||
1384 | static void exp_movable(int attribute((unused)) nargs, | |
1385 | char attribute((unused)) **args, | |
1386 | cgi_sink *output, | |
1387 | void attribute((unused)) *u) { | |
1388 | dcgi_state *ds = u; | |
1389 | ||
1390 | lookups(ds, DC_RIGHTS); | |
1391 | sink_printf(output->sink, "%s", | |
1392 | bool2str(right_movable(ds->g->rights, | |
1393 | disorder_user(ds->g->client), | |
1394 | ds->track))); | |
460b9539 | 1395 | } |
1396 | ||
1397 | static void exp_navigate(int attribute((unused)) nargs, | |
1398 | char **args, | |
1399 | cgi_sink *output, | |
1400 | void *u) { | |
1401 | dcgi_state *ds = u; | |
1402 | dcgi_state substate; | |
1403 | const char *path = expandarg(args[0], ds); | |
1404 | const char *ptr; | |
1405 | int dirlen; | |
1406 | ||
1407 | if(*path) { | |
1408 | memset(&substate, 0, sizeof substate); | |
1409 | substate.g = ds->g; | |
1410 | ptr = path + 1; /* skip root */ | |
1411 | dirlen = 0; | |
1412 | substate.nav_path = path; | |
1413 | substate.first = 1; | |
1414 | while(*ptr) { | |
1415 | while(*ptr && *ptr != '/') | |
1416 | ++ptr; | |
1417 | substate.last = !*ptr; | |
1418 | substate.nav_len = ptr - path; | |
1419 | substate.nav_dirlen = dirlen; | |
1420 | expandstring(output, args[1], &substate); | |
1421 | dirlen = substate.nav_len; | |
1422 | if(*ptr) ++ptr; | |
1423 | substate.first = 0; | |
1424 | } | |
1425 | } | |
1426 | } | |
1427 | ||
1428 | static void exp_fullname(int attribute((unused)) nargs, | |
1429 | char attribute((unused)) **args, | |
1430 | cgi_sink *output, | |
1431 | void *u) { | |
1432 | dcgi_state *ds = u; | |
1433 | cgi_output(output, "%.*s", ds->nav_len, ds->nav_path); | |
1434 | } | |
1435 | ||
1436 | static void exp_basename(int nargs, | |
1437 | char **args, | |
1438 | cgi_sink *output, | |
1439 | void *u) { | |
1440 | dcgi_state *ds = u; | |
1441 | const char *s; | |
1442 | ||
1443 | if(nargs) { | |
1444 | if((s = strrchr(args[0], '/'))) ++s; | |
1445 | else s = args[0]; | |
1446 | cgi_output(output, "%s", s); | |
1447 | } else | |
1448 | cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1, | |
1449 | ds->nav_path + ds->nav_dirlen + 1); | |
1450 | } | |
1451 | ||
1452 | static void exp_dirname(int nargs, | |
1453 | char **args, | |
1454 | cgi_sink *output, | |
1455 | void *u) { | |
1456 | dcgi_state *ds = u; | |
1457 | const char *s; | |
1458 | ||
1459 | if(nargs) { | |
1460 | if((s = strrchr(args[0], '/'))) | |
1461 | cgi_output(output, "%.*s", (int)(s - args[0]), args[0]); | |
1462 | } else | |
1463 | cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path); | |
1464 | } | |
1465 | ||
1466 | static void exp_eq(int attribute((unused)) nargs, | |
1467 | char **args, | |
1468 | cgi_sink *output, | |
1469 | void attribute((unused)) *u) { | |
1470 | cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1]))); | |
1471 | } | |
1472 | ||
1473 | static void exp_ne(int attribute((unused)) nargs, | |
1474 | char **args, | |
1475 | cgi_sink *output, | |
1476 | void attribute((unused)) *u) { | |
1477 | cgi_output(output, "%s", bool2str(strcmp(args[0], args[1]))); | |
1478 | } | |
1479 | ||
1480 | static void exp_enabled(int attribute((unused)) nargs, | |
1481 | char attribute((unused)) **args, | |
1482 | cgi_sink *output, | |
1483 | void *u) { | |
1484 | dcgi_state *ds = u; | |
1485 | int enabled = 0; | |
1486 | ||
1487 | if(ds->g->client) | |
1488 | disorder_enabled(ds->g->client, &enabled); | |
1489 | cgi_output(output, "%s", bool2str(enabled)); | |
1490 | } | |
1491 | ||
1492 | static void exp_random_enabled(int attribute((unused)) nargs, | |
1493 | char attribute((unused)) **args, | |
1494 | cgi_sink *output, | |
1495 | void *u) { | |
1496 | dcgi_state *ds = u; | |
1497 | int enabled = 0; | |
1498 | ||
1499 | if(ds->g->client) | |
1500 | disorder_random_enabled(ds->g->client, &enabled); | |
1501 | cgi_output(output, "%s", bool2str(enabled)); | |
1502 | } | |
1503 | ||
1504 | static void exp_trackstate(int attribute((unused)) nargs, | |
1505 | char **args, | |
1506 | cgi_sink *output, | |
1507 | void *u) { | |
1508 | dcgi_state *ds = u; | |
1509 | struct queue_entry *q; | |
1510 | char *track; | |
1511 | ||
1512 | if(disorder_resolve(ds->g->client, &track, args[0])) return; | |
1513 | lookups(ds, DC_QUEUE|DC_PLAYING); | |
1514 | if(ds->g->playing && !strcmp(ds->g->playing->track, track)) | |
1515 | cgi_output(output, "playing"); | |
1516 | else { | |
1517 | for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next) | |
1518 | ; | |
1519 | if(q) | |
1520 | cgi_output(output, "queued"); | |
1521 | } | |
1522 | } | |
1523 | ||
1524 | static void exp_thisurl(int attribute((unused)) nargs, | |
1525 | char attribute((unused)) **args, | |
1526 | cgi_sink *output, | |
1527 | void attribute((unused)) *u) { | |
1528 | kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */ | |
1529 | cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0)); | |
1530 | } | |
1531 | ||
1532 | static void exp_isfirst(int attribute((unused)) nargs, | |
1533 | char attribute((unused)) **args, | |
1534 | cgi_sink *output, | |
1535 | void *u) { | |
1536 | dcgi_state *ds = u; | |
1537 | ||
1538 | sink_printf(output->sink, "%s", bool2str(!!ds->first)); | |
1539 | } | |
1540 | ||
1541 | static void exp_islast(int attribute((unused)) nargs, | |
1542 | char attribute((unused)) **args, | |
1543 | cgi_sink *output, | |
1544 | void *u) { | |
1545 | dcgi_state *ds = u; | |
1546 | ||
1547 | sink_printf(output->sink, "%s", bool2str(!!ds->last)); | |
1548 | } | |
1549 | ||
1550 | static void exp_action(int attribute((unused)) nargs, | |
1551 | char attribute((unused)) **args, | |
1552 | cgi_sink *output, | |
1553 | void attribute((unused)) *u) { | |
1554 | const char *action = cgi_get("action"), *mgmt; | |
1555 | ||
1556 | if(!action) action = "playing"; | |
1557 | if(!strcmp(action, "playing") | |
1558 | && (mgmt = cgi_get("mgmt")) | |
1559 | && !strcmp(mgmt, "true")) | |
1560 | action = "manage"; | |
1561 | sink_printf(output->sink, "%s", action); | |
1562 | } | |
1563 | ||
1564 | static void exp_resolve(int attribute((unused)) nargs, | |
1565 | char **args, | |
1566 | cgi_sink *output, | |
1567 | void attribute((unused)) *u) { | |
1568 | dcgi_state *ds = u; | |
1569 | char *track; | |
1570 | ||
1571 | if(!disorder_resolve(ds->g->client, &track, args[0])) | |
1572 | sink_printf(output->sink, "%s", track); | |
1573 | } | |
1574 | ||
1575 | static void exp_paused(int attribute((unused)) nargs, | |
1576 | char attribute((unused)) **args, | |
1577 | cgi_sink *output, | |
1578 | void *u) { | |
1579 | dcgi_state *ds = u; | |
1580 | int paused = 0; | |
1581 | ||
1582 | lookups(ds, DC_PLAYING); | |
1583 | if(ds->g->playing && ds->g->playing->state == playing_paused) | |
1584 | paused = 1; | |
1585 | cgi_output(output, "%s", bool2str(paused)); | |
1586 | } | |
1587 | ||
1588 | static void exp_state(int attribute((unused)) nargs, | |
1589 | char attribute((unused)) **args, | |
1590 | cgi_sink *output, | |
1591 | void *u) { | |
1592 | dcgi_state *ds = u; | |
1593 | ||
1594 | if(ds->track) | |
1595 | cgi_output(output, "%s", playing_states[ds->track->state]); | |
1596 | } | |
1597 | ||
1598 | static void exp_files(int attribute((unused)) nargs, | |
1599 | char **args, | |
1600 | cgi_sink *output, | |
1601 | void *u) { | |
1602 | dcgi_state *ds = u; | |
1603 | dcgi_state substate; | |
1604 | const char *nfiles_arg, *directory; | |
1605 | int nfiles, numfile; | |
1606 | struct kvp *k; | |
1607 | ||
1608 | memset(&substate, 0, sizeof substate); | |
1609 | substate.g = ds->g; | |
1610 | if((directory = cgi_get("directory"))) { | |
1611 | /* Prefs for whole directory. */ | |
1612 | lookups(ds, DC_FILES); | |
1613 | /* Synthesize args for the file list. */ | |
1614 | nfiles = ds->g->nfiles; | |
1615 | for(numfile = 0; numfile < nfiles; ++numfile) { | |
1616 | k = xmalloc(sizeof *k); | |
1617 | byte_xasprintf((char **)&k->name, "%d_file", numfile); | |
1618 | k->value = ds->g->files[numfile]; | |
1619 | k->next = cgi_args; | |
1620 | cgi_args = k; | |
1621 | } | |
1622 | } else { | |
1623 | /* Args already present. */ | |
1624 | if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg); | |
1625 | else nfiles = 1; | |
1626 | } | |
1627 | for(numfile = 0; numfile < nfiles; ++numfile) { | |
1628 | substate.index = numfile; | |
1629 | expandstring(output, args[0], &substate); | |
1630 | } | |
1631 | } | |
1632 | ||
1633 | static void exp_index(int attribute((unused)) nargs, | |
1634 | char attribute((unused)) **args, | |
1635 | cgi_sink *output, | |
1636 | void *u) { | |
1637 | dcgi_state *ds = u; | |
1638 | ||
1639 | cgi_output(output, "%d", ds->index); | |
1640 | } | |
1641 | ||
1642 | static void exp_nfiles(int attribute((unused)) nargs, | |
1643 | char attribute((unused)) **args, | |
1644 | cgi_sink *output, | |
1645 | void *u) { | |
1646 | dcgi_state *ds = u; | |
1647 | const char *files_arg; | |
1648 | ||
1649 | if(cgi_get("directory")) { | |
1650 | lookups(ds, DC_FILES); | |
1651 | cgi_output(output, "%d", ds->g->nfiles); | |
1652 | } else if((files_arg = cgi_get("files"))) | |
1653 | cgi_output(output, "%s", files_arg); | |
1654 | else | |
1655 | cgi_output(output, "1"); | |
1656 | } | |
1657 | ||
fdf98378 | 1658 | static void exp_user(int attribute((unused)) nargs, |
1659 | char attribute((unused)) **args, | |
1660 | cgi_sink *output, | |
1661 | void *u) { | |
1662 | dcgi_state *const ds = u; | |
1663 | ||
1664 | cgi_output(output, "%s", disorder_user(ds->g->client)); | |
1665 | } | |
1666 | ||
938d8157 | 1667 | static void exp_right(int attribute((unused)) nargs, |
1668 | char **args, | |
1669 | cgi_sink *output, | |
1670 | void *u) { | |
1671 | dcgi_state *const ds = u; | |
1672 | const char *right = expandarg(args[0], ds); | |
1673 | rights_type r; | |
1674 | ||
1675 | lookups(ds, DC_RIGHTS); | |
1676 | if(parse_rights(right, &r, 1/*report*/)) | |
1677 | r = 0; | |
1678 | if(args[1] == 0) | |
1679 | cgi_output(output, "%s", bool2str(!!(r & ds->g->rights))); | |
1680 | else if(r & ds->g->rights) | |
1681 | expandstring(output, args[1], ds); | |
1682 | else if(args[2]) | |
1683 | expandstring(output, args[2], ds); | |
1684 | } | |
1685 | ||
968f044a | 1686 | static void exp_userinfo(int attribute((unused)) nargs, |
1687 | char **args, | |
1688 | cgi_sink *output, | |
1689 | void *u) { | |
1690 | dcgi_state *const ds = u; | |
1691 | const char *value; | |
1692 | ||
1693 | if(disorder_userinfo(ds->g->client, disorder_user(ds->g->client), args[0], | |
1694 | (char **)&value)) | |
1695 | value = ""; | |
1696 | cgi_output(output, "%s", value); | |
1697 | } | |
1698 | ||
460b9539 | 1699 | static const struct cgi_expansion expansions[] = { |
1700 | { "#", 0, INT_MAX, EXP_MAGIC, exp_comment }, | |
1701 | { "action", 0, 0, 0, exp_action }, | |
1702 | { "and", 0, INT_MAX, EXP_MAGIC, exp_and }, | |
1703 | { "arg", 1, 1, 0, exp_arg }, | |
1704 | { "basename", 0, 1, 0, exp_basename }, | |
1705 | { "choose", 2, 2, EXP_MAGIC, exp_choose }, | |
1706 | { "dirname", 0, 1, 0, exp_dirname }, | |
1707 | { "enabled", 0, 0, 0, exp_enabled }, | |
1708 | { "eq", 2, 2, 0, exp_eq }, | |
1709 | { "file", 0, 0, 0, exp_file }, | |
1710 | { "files", 1, 1, EXP_MAGIC, exp_files }, | |
1711 | { "fullname", 0, 0, 0, exp_fullname }, | |
1712 | { "id", 0, 0, 0, exp_id }, | |
1713 | { "if", 2, 3, EXP_MAGIC, exp_if }, | |
1714 | { "include", 1, 1, 0, exp_include }, | |
1715 | { "index", 0, 0, 0, exp_index }, | |
1716 | { "isdirectories", 0, 0, 0, exp_isdirectories }, | |
1717 | { "isfiles", 0, 0, 0, exp_isfiles }, | |
1718 | { "isfirst", 0, 0, 0, exp_isfirst }, | |
1719 | { "islast", 0, 0, 0, exp_islast }, | |
78efa64e | 1720 | { "isnew", 0, 0, 0, exp_isnew }, |
460b9539 | 1721 | { "isplaying", 0, 0, 0, exp_isplaying }, |
1722 | { "isqueue", 0, 0, 0, exp_isqueue }, | |
1723 | { "isrecent", 0, 0, 0, exp_isrecent }, | |
1724 | { "label", 1, 1, 0, exp_label }, | |
1725 | { "length", 0, 0, 0, exp_length }, | |
938d8157 | 1726 | { "movable", 0, 0, 0, exp_movable }, |
460b9539 | 1727 | { "navigate", 2, 2, EXP_MAGIC, exp_navigate }, |
1728 | { "ne", 2, 2, 0, exp_ne }, | |
78efa64e | 1729 | { "new", 1, 1, EXP_MAGIC, exp_new }, |
460b9539 | 1730 | { "nfiles", 0, 0, 0, exp_nfiles }, |
1731 | { "nonce", 0, 0, 0, exp_nonce }, | |
1732 | { "not", 1, 1, 0, exp_not }, | |
1733 | { "or", 0, INT_MAX, EXP_MAGIC, exp_or }, | |
1734 | { "parity", 0, 0, 0, exp_parity }, | |
1735 | { "part", 1, 3, 0, exp_part }, | |
1736 | { "paused", 0, 0, 0, exp_paused }, | |
1737 | { "playing", 1, 1, EXP_MAGIC, exp_playing }, | |
1738 | { "pref", 2, 2, 0, exp_pref }, | |
1739 | { "prefname", 0, 0, 0, exp_prefname }, | |
1740 | { "prefs", 2, 2, EXP_MAGIC, exp_prefs }, | |
1741 | { "prefvalue", 0, 0, 0, exp_prefvalue }, | |
1742 | { "queue", 1, 1, EXP_MAGIC, exp_queue }, | |
1743 | { "random-enabled", 0, 0, 0, exp_random_enabled }, | |
1744 | { "recent", 1, 1, EXP_MAGIC, exp_recent }, | |
1745 | { "removable", 0, 0, 0, exp_removable }, | |
1746 | { "resolve", 1, 1, 0, exp_resolve }, | |
938d8157 | 1747 | { "right", 1, 3, EXP_MAGIC, exp_right }, |
460b9539 | 1748 | { "scratchable", 0, 0, 0, exp_scratchable }, |
1749 | { "search", 2, 3, EXP_MAGIC, exp_search }, | |
1750 | { "server-version", 0, 0, 0, exp_server_version }, | |
1751 | { "shell", 1, 1, 0, exp_shell }, | |
1752 | { "state", 0, 0, 0, exp_state }, | |
1753 | { "stats", 0, 0, 0, exp_stats }, | |
1754 | { "thisurl", 0, 0, 0, exp_thisurl }, | |
1755 | { "track", 0, 0, 0, exp_track }, | |
1756 | { "trackstate", 1, 1, 0, exp_trackstate }, | |
1757 | { "transform", 2, 3, 0, exp_transform }, | |
1758 | { "url", 0, 0, 0, exp_url }, | |
1759 | { "urlquote", 1, 1, 0, exp_urlquote }, | |
fdf98378 | 1760 | { "user", 0, 0, 0, exp_user }, |
968f044a | 1761 | { "userinfo", 1, 1, 0, exp_userinfo }, |
460b9539 | 1762 | { "version", 0, 0, 0, exp_version }, |
1763 | { "volume", 1, 1, 0, exp_volume }, | |
1764 | { "when", 0, 0, 0, exp_when }, | |
1765 | { "who", 0, 0, 0, exp_who } | |
1766 | }; | |
1767 | ||
1768 | static void expand(cgi_sink *output, | |
1769 | const char *template, | |
1770 | dcgi_state *ds) { | |
1771 | cgi_expand(template, | |
1772 | expansions, sizeof expansions / sizeof *expansions, | |
1773 | output, | |
1774 | ds); | |
1775 | } | |
1776 | ||
1777 | static void expandstring(cgi_sink *output, | |
1778 | const char *string, | |
1779 | dcgi_state *ds) { | |
1780 | cgi_expand_string("", | |
1781 | string, | |
1782 | expansions, sizeof expansions / sizeof *expansions, | |
1783 | output, | |
1784 | ds); | |
1785 | } | |
1786 | ||
1787 | static void perform_action(cgi_sink *output, dcgi_state *ds, | |
1788 | const char *action) { | |
1789 | int n; | |
1790 | ||
fdf98378 | 1791 | /* We don't ever want anything to be cached */ |
1792 | cgi_header(output->sink, "Cache-Control", "no-cache"); | |
460b9539 | 1793 | if((n = TABLE_FIND(actions, struct action, name, action)) >= 0) |
1794 | actions[n].handler(output, ds); | |
fdf98378 | 1795 | else |
1796 | expand_template(ds, output, action); | |
460b9539 | 1797 | } |
1798 | ||
1799 | void disorder_cgi(cgi_sink *output, dcgi_state *ds) { | |
1800 | const char *action = cgi_get("action"); | |
1801 | ||
230209f7 | 1802 | if(!action) { |
1803 | /* We allow URLs which are just confirm=... in order to keep confirmation | |
1804 | * URLs, which are user-facing, as short as possible. */ | |
10114017 | 1805 | if(cgi_get("c")) |
230209f7 | 1806 | action = "confirm"; |
1807 | else | |
1808 | action = "playing"; | |
1809 | } | |
460b9539 | 1810 | perform_action(output, ds, action); |
1811 | } | |
1812 | ||
1813 | void disorder_cgi_error(cgi_sink *output, dcgi_state *ds, | |
1814 | const char *msg) { | |
1815 | cgi_set_option("error", msg); | |
1816 | perform_action(output, ds, "error"); | |
1817 | } | |
1818 | ||
938d8157 | 1819 | /** @brief Log in as the current user or guest if none */ |
1820 | void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) { | |
1821 | /* Create a new connection */ | |
1822 | ds->g->client = disorder_new(0); | |
1823 | /* Forget everything we knew */ | |
1824 | ds->g->flags = 0; | |
1825 | /* Reconnect */ | |
1826 | if(disorder_connect_cookie(ds->g->client, login_cookie)) { | |
1827 | disorder_cgi_error(output, ds, "connect"); | |
1828 | exit(0); | |
1829 | } | |
1830 | /* If there was a cookie but it went bad, we forget it */ | |
1831 | if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest")) | |
1832 | login_cookie = 0; | |
1833 | } | |
1834 | ||
460b9539 | 1835 | /* |
1836 | Local Variables: | |
1837 | c-basic-offset:2 | |
1838 | comment-column:40 | |
1839 | fill-column:79 | |
1840 | End: | |
1841 | */ |