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