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