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