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