chiark / gitweb /
ALSA mixer support.
[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 }
467 c = disorder_new(1);
468 if(disorder_connect_user(c, username, password)) {
469 cgi_set_option("error", "loginfailed");
470 expand_template(ds, output, "login");
471 return;
472 }
473 if(disorder_make_cookie(c, &login_cookie)) {
474 cgi_set_option("error", "cookiefailed");
475 expand_template(ds, output, "login");
476 return;
477 }
478 /* We have a new cookie */
7f66f58b 479 header_cookie(output->sink);
fdf98378 480 if((back = cgi_get("back")) && back)
481 /* Redirect back to somewhere or other */
482 redirect(output->sink);
483 else
484 /* Stick to the login page */
485 expand_template(ds, output, "login");
486}
487
b42ffad6 488static void act_logout(cgi_sink *output,
489 dcgi_state *ds) {
490 disorder_revoke(ds->g->client);
491 login_cookie = 0;
7f66f58b 492 /* Reconnect as guest */
938d8157 493 disorder_cgi_login(ds, output);
b42ffad6 494 /* Back to the login page */
495 expand_template(ds, output, "login");
496}
497
fdf98378 498static void act_register(cgi_sink *output,
499 dcgi_state *ds) {
500 const char *username, *password, *email;
bb6ae3fb 501 char *confirm, *content_type;
502 const char *text, *encoding, *charset;
fdf98378 503
504 username = cgi_get("username");
505 password = cgi_get("password");
506 email = cgi_get("email");
507
508 if(!username || !*username) {
509 cgi_set_option("error", "nousername");
510 expand_template(ds, output, "login");
511 return;
512 }
513 if(!password || !*password) {
514 cgi_set_option("error", "nopassword");
515 expand_template(ds, output, "login");
516 return;
517 }
518 if(!email || !*email) {
519 cgi_set_option("error", "noemail");
520 expand_template(ds, output, "login");
521 return;
522 }
523 /* We could well do better address validation but for now we'll just do the
524 * minimum */
525 if(!strchr(email, '@')) {
526 cgi_set_option("error", "bademail");
527 expand_template(ds, output, "login");
528 return;
529 }
530 if(disorder_register(ds->g->client, username, password, email, &confirm)) {
531 cgi_set_option("error", "cannotregister");
532 expand_template(ds, output, "login");
533 return;
534 }
bb6ae3fb 535 /* Send the user a mail */
536 /* TODO templatize this */
537 byte_xasprintf((char **)&text,
538 "Welcome to DisOrder. To active your login, please visit this URL:\n"
539 "\n"
540 " %s?confirm=%s\n", config->url, confirm);
541 if(!(text = mime_encode_text(text, &charset, &encoding)))
542 fatal(0, "cannot encode email");
543 byte_xasprintf(&content_type, "text/plain;charset=%s",
544 quote822(charset, 0));
545 sendmail("", config->mail_sender, email, "Welcome to DisOrder",
546 encoding, content_type, text); /* TODO error checking */
fdf98378 547 /* We'll go back to the login page with a suitable message */
548 cgi_set_option("registered", "registeredok");
549 expand_template(ds, output, "login");
550}
551
460b9539 552static const struct action {
553 const char *name;
554 void (*handler)(cgi_sink *output, dcgi_state *ds);
555} actions[] = {
556 { "disable", act_disable },
557 { "enable", act_enable },
fdf98378 558 { "login", act_login },
b42ffad6 559 { "logout", act_logout },
460b9539 560 { "move", act_move },
561 { "pause", act_pause },
562 { "play", act_play },
563 { "playing", act_playing },
564 { "prefs", act_prefs },
565 { "random-disable", act_random_disable },
566 { "random-enable", act_random_enable },
fdf98378 567 { "register", act_register },
460b9539 568 { "remove", act_remove },
569 { "resume", act_resume },
570 { "scratch", act_scratch },
571 { "volume", act_volume },
572};
573
574/* expansions *****************************************************************/
575
576static void exp_include(int attribute((unused)) nargs,
577 char **args,
578 cgi_sink *output,
579 void *u) {
580 expand(output, args[0], u);
581}
582
583static void exp_server_version(int attribute((unused)) nargs,
584 char attribute((unused)) **args,
585 cgi_sink *output,
586 void *u) {
587 dcgi_state *ds = u;
588 const char *v;
589
590 if(ds->g->client) {
591 if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)";
592 } else
593 v = "(server not running)";
594 cgi_output(output, "%s", v);
595}
596
597static void exp_version(int attribute((unused)) nargs,
598 char attribute((unused)) **args,
599 cgi_sink *output,
600 void attribute((unused)) *u) {
a05e4467 601 cgi_output(output, "%s", disorder_short_version_string);
460b9539 602}
603
604static void exp_nonce(int attribute((unused)) nargs,
605 char attribute((unused)) **args,
606 cgi_sink *output,
607 void attribute((unused)) *u) {
608 cgi_output(output, "%s", nonce());
609}
610
611static void exp_label(int attribute((unused)) nargs,
612 char **args,
613 cgi_sink *output,
614 void attribute((unused)) *u) {
615 cgi_output(output, "%s", cgi_label(args[0]));
616}
617
618struct trackinfo_state {
619 dcgi_state *ds;
620 const struct queue_entry *q;
621 long length;
622 time_t when;
623};
624
625static void exp_who(int attribute((unused)) nargs,
626 char attribute((unused)) **args,
627 cgi_sink *output,
628 void *u) {
629 dcgi_state *ds = u;
630
631 if(ds->track && ds->track->submitter)
632 cgi_output(output, "%s", ds->track->submitter);
633}
634
635static void exp_length(int attribute((unused)) nargs,
636 char attribute((unused)) **args,
637 cgi_sink *output,
638 void *u) {
639 dcgi_state *ds = u;
78efa64e 640 long length = 0;
460b9539 641
642 if(ds->track
643 && (ds->track->state == playing_started
644 || ds->track->state == playing_paused)
645 && ds->track->sofar >= 0)
646 cgi_output(output, "%ld:%02ld/",
647 ds->track->sofar / 60, ds->track->sofar % 60);
78efa64e
RK
648 length = 0;
649 if(ds->track)
650 disorder_length(ds->g->client, ds->track->track, &length);
651 else if(ds->tracks)
652 disorder_length(ds->g->client, ds->tracks[0], &length);
460b9539 653 if(length)
654 cgi_output(output, "%ld:%02ld", length / 60, length % 60);
655 else
656 sink_printf(output->sink, "%s", "&nbsp;");
657}
658
659static void exp_when(int attribute((unused)) nargs,
660 char attribute((unused)) **args,
661 cgi_sink *output,
662 void *u) {
663 dcgi_state *ds = u;
664 const struct tm *w = 0;
665
666 if(ds->track)
667 switch(ds->track->state) {
668 case playing_isscratch:
669 case playing_unplayed:
670 case playing_random:
671 if(ds->track->expected)
672 w = localtime(&ds->track->expected);
673 break;
674 case playing_failed:
675 case playing_no_player:
676 case playing_ok:
677 case playing_scratched:
678 case playing_started:
679 case playing_paused:
680 case playing_quitting:
681 if(ds->track->played)
682 w = localtime(&ds->track->played);
683 break;
684 }
685 if(w)
686 cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min);
687 else
688 sink_printf(output->sink, "&nbsp;");
689}
690
691static void exp_part(int nargs,
692 char **args,
693 cgi_sink *output,
694 void *u) {
695 dcgi_state *ds = u;
696 const char *s, *track, *part, *context;
697
698 if(nargs == 3)
699 track = args[2];
700 else {
701 if(ds->track)
702 track = ds->track->track;
703 else if(ds->tracks)
704 track = ds->tracks[0];
705 else
706 track = 0;
707 }
708 if(track) {
709 switch(nargs) {
710 case 1:
711 context = "display";
712 part = args[0];
713 break;
714 case 2:
715 case 3:
716 context = args[0];
717 part = args[1];
718 break;
719 default:
720 abort();
721 }
61507e3c
RK
722 if(disorder_part(ds->g->client, (char **)&s, track,
723 !strcmp(context, "short") ? "display" : context, part))
460b9539 724 fatal(0, "disorder_part() failed");
61507e3c
RK
725 if(!strcmp(context, "short"))
726 s = truncate_for_display(s, config->short_display);
460b9539 727 cgi_output(output, "%s", s);
728 } else
729 sink_printf(output->sink, "&nbsp;");
730}
731
732static void exp_playing(int attribute((unused)) nargs,
733 char **args,
734 cgi_sink *output,
735 void *u) {
736 dcgi_state *ds = u;
737 dcgi_state s;
738
739 lookups(ds, DC_PLAYING);
740 memset(&s, 0, sizeof s);
741 s.g = ds->g;
742 if(ds->g->playing) {
743 s.track = ds->g->playing;
744 expandstring(output, args[0], &s);
745 }
746}
747
748static void exp_queue(int attribute((unused)) nargs,
749 char **args,
750 cgi_sink *output,
751 void *u) {
752 dcgi_state *ds = u;
753 dcgi_state s;
754 struct queue_entry *q;
755
756 lookups(ds, DC_QUEUE);
757 memset(&s, 0, sizeof s);
758 s.g = ds->g;
759 s.first = 1;
760 for(q = ds->g->queue; q; q = q->next) {
761 s.last = !q->next;
762 s.track = q;
763 expandstring(output, args[0], &s);
764 s.index++;
765 s.first = 0;
766 }
767}
768
769static void exp_recent(int attribute((unused)) nargs,
770 char **args,
771 cgi_sink *output,
772 void *u) {
773 dcgi_state *ds = u;
774 dcgi_state s;
775 struct queue_entry *q;
776
777 lookups(ds, DC_RECENT);
778 memset(&s, 0, sizeof s);
779 s.g = ds->g;
780 s.first = 1;
781 for(q = ds->g->recent; q; q = q->next) {
782 s.last = !q;
783 s.track = q;
784 expandstring(output, args[0], &s);
785 s.index++;
786 s.first = 0;
787 }
788}
789
78efa64e
RK
790static void exp_new(int attribute((unused)) nargs,
791 char **args,
792 cgi_sink *output,
793 void *u) {
794 dcgi_state *ds = u;
795 dcgi_state s;
796
797 lookups(ds, DC_NEW);
798 memset(&s, 0, sizeof s);
799 s.g = ds->g;
800 s.first = 1;
801 for(s.index = 0; s.index < ds->g->nnew; ++s.index) {
802 s.last = s.index + 1 < ds->g->nnew;
803 s.tracks = &ds->g->new[s.index];
804 expandstring(output, args[0], &s);
805 s.first = 0;
806 }
807}
808
460b9539 809static void exp_url(int attribute((unused)) nargs,
810 char attribute((unused)) **args,
811 cgi_sink *output,
812 void attribute((unused)) *u) {
813 cgi_output(output, "%s", config->url);
814}
815
816struct result {
817 char *track;
818 const char *sort;
819};
820
821static int compare_result(const void *a, const void *b) {
822 const struct result *ra = a, *rb = b;
823 int c;
824
825 if(!(c = strcmp(ra->sort, rb->sort)))
826 c = strcmp(ra->track, rb->track);
827 return c;
828}
829
830static void exp_search(int nargs,
831 char **args,
832 cgi_sink *output,
833 void *u) {
834 dcgi_state *ds = u, substate;
835 char **tracks;
836 const char *q, *context, *part, *template;
837 int ntracks, n, m;
838 struct result *r;
839
840 switch(nargs) {
841 case 2:
842 part = args[0];
843 context = "sort";
844 template = args[1];
845 break;
846 case 3:
847 part = args[0];
848 context = args[1];
849 template = args[2];
850 break;
851 default:
852 assert(!"should never happen");
853 part = context = template = 0; /* quieten compiler */
854 }
855 if(ds->tracks == 0) {
856 /* we are the top level, let's get some search results */
857 if(!(q = cgi_get("query"))) return; /* no results yet */
858 if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return;
859 if(!ntracks) return;
860 } else {
861 tracks = ds->tracks;
862 ntracks = ds->ntracks;
863 }
864 assert(ntracks != 0);
865 /* sort tracks by the appropriate part */
866 r = xmalloc(ntracks * sizeof *r);
867 for(n = 0; n < ntracks; ++n) {
868 r[n].track = tracks[n];
869 if(disorder_part(ds->g->client, (char **)&r[n].sort,
870 tracks[n], context, part))
871 fatal(0, "disorder_part() failed");
872 }
873 qsort(r, ntracks, sizeof (struct result), compare_result);
874 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
875 * array as we know it's guaranteed to be big enough and isn't going to be
876 * used for anything else any more. */
877 memset(&substate, 0, sizeof substate);
878 substate.g = ds->g;
879 substate.first = 1;
880 n = 0;
881 while(n < ntracks) {
882 substate.tracks = tracks;
883 substate.ntracks = 0;
884 m = n;
885 while(m < ntracks
886 && !strcmp(r[m].sort, r[n].sort))
887 tracks[substate.ntracks++] = r[m++].track;
888 substate.last = (m == ntracks);
889 expandstring(output, template, &substate);
890 substate.index++;
891 substate.first = 0;
892 n = m;
893 }
894 assert(substate.last != 0);
895}
896
897static void exp_arg(int attribute((unused)) nargs,
898 char **args,
899 cgi_sink *output,
900 void attribute((unused)) *u) {
901 const char *v;
902
903 if((v = cgi_get(args[0])))
904 cgi_output(output, "%s", v);
905}
906
907static void exp_stats(int attribute((unused)) nargs,
908 char attribute((unused)) **args,
909 cgi_sink *output,
910 void *u) {
911 dcgi_state *ds = u;
912 char **v;
913
914 cgi_opentag(output->sink, "pre", "class", "stats", (char *)0);
915 if(!disorder_stats(ds->g->client, &v, 0)) {
916 while(*v)
917 cgi_output(output, "%s\n", *v++);
918 }
919 cgi_closetag(output->sink, "pre");
920}
921
922static void exp_volume(int attribute((unused)) nargs,
923 char **args,
924 cgi_sink *output,
925 void *u) {
926 dcgi_state *ds = u;
927
928 lookups(ds, DC_VOLUME);
929 if(!strcmp(args[0], "left"))
930 cgi_output(output, "%d", ds->g->volume_left);
931 else
932 cgi_output(output, "%d", ds->g->volume_right);
933}
934
935static void exp_shell(int attribute((unused)) nargs,
936 char **args,
937 cgi_sink *output,
938 void attribute((unused)) *u) {
939 int w, p[2], n;
940 char buffer[4096];
941 pid_t pid;
942
943 xpipe(p);
944 if(!(pid = xfork())) {
945 exitfn = _exit;
946 xclose(p[0]);
947 xdup2(p[1], 1);
948 xclose(p[1]);
949 execlp("sh", "sh", "-c", args[0], (char *)0);
950 fatal(errno, "error executing sh");
951 }
952 xclose(p[1]);
953 while((n = read(p[0], buffer, sizeof buffer))) {
954 if(n < 0) {
955 if(errno == EINTR) continue;
956 else fatal(errno, "error reading from pipe");
957 }
958 output->sink->write(output->sink, buffer, n);
959 }
960 xclose(p[0]);
961 while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR)
962 ;
963 if(n < 0) fatal(errno, "error calling waitpid");
964 if(w)
965 error(0, "shell command '%s' %s", args[0], wstat(w));
966}
967
968static inline int str2bool(const char *s) {
969 return !strcmp(s, "true");
970}
971
972static inline const char *bool2str(int n) {
973 return n ? "true" : "false";
974}
975
976static char *expandarg(const char *arg, dcgi_state *ds) {
977 struct dynstr d;
978 cgi_sink output;
979
980 dynstr_init(&d);
981 output.quote = 0;
982 output.sink = sink_dynstr(&d);
983 expandstring(&output, arg, ds);
984 dynstr_terminate(&d);
985 return d.vec;
986}
987
988static void exp_prefs(int attribute((unused)) nargs,
989 char **args,
990 cgi_sink *output,
991 void *u) {
992 dcgi_state *ds = u;
993 dcgi_state substate;
994 struct kvp *k;
995 const char *file = expandarg(args[0], ds);
996
997 memset(&substate, 0, sizeof substate);
998 substate.g = ds->g;
999 substate.first = 1;
1000 if(disorder_prefs(ds->g->client, file, &k)) return;
1001 while(k) {
1002 substate.last = !k->next;
1003 substate.pref = k;
1004 expandstring(output, args[1], &substate);
1005 ++substate.index;
1006 k = k->next;
1007 substate.first = 0;
1008 }
1009}
1010
1011static void exp_pref(int attribute((unused)) nargs,
1012 char **args,
1013 cgi_sink *output,
1014 void *u) {
1015 char *value;
1016 dcgi_state *ds = u;
1017
1018 if(!disorder_get(ds->g->client, args[0], args[1], &value))
1019 cgi_output(output, "%s", value);
1020}
1021
1022static void exp_if(int nargs,
1023 char **args,
1024 cgi_sink *output,
1025 void *u) {
1026 dcgi_state *ds = u;
1027 int n = str2bool(expandarg(args[0], ds)) ? 1 : 2;
1028
1029 if(n < nargs)
1030 expandstring(output, args[n], ds);
1031}
1032
1033static void exp_and(int nargs,
1034 char **args,
1035 cgi_sink *output,
1036 void *u) {
1037 dcgi_state *ds = u;
1038 int n, result = 1;
1039
1040 for(n = 0; n < nargs; ++n)
1041 if(!str2bool(expandarg(args[n], ds))) {
1042 result = 0;
1043 break;
1044 }
1045 sink_printf(output->sink, "%s", bool2str(result));
1046}
1047
1048static void exp_or(int nargs,
1049 char **args,
1050 cgi_sink *output,
1051 void *u) {
1052 dcgi_state *ds = u;
1053 int n, result = 0;
1054
1055 for(n = 0; n < nargs; ++n)
1056 if(str2bool(expandarg(args[n], ds))) {
1057 result = 1;
1058 break;
1059 }
1060 sink_printf(output->sink, "%s", bool2str(result));
1061}
1062
1063static void exp_not(int attribute((unused)) nargs,
1064 char **args,
1065 cgi_sink *output,
1066 void attribute((unused)) *u) {
1067 sink_printf(output->sink, "%s", bool2str(!str2bool(args[0])));
1068}
1069
1070static void exp_isplaying(int attribute((unused)) nargs,
1071 char attribute((unused)) **args,
1072 cgi_sink *output,
1073 void *u) {
1074 dcgi_state *ds = u;
1075
1076 lookups(ds, DC_PLAYING);
1077 sink_printf(output->sink, "%s", bool2str(!!ds->g->playing));
1078}
1079
1080static void exp_isqueue(int attribute((unused)) nargs,
1081 char attribute((unused)) **args,
1082 cgi_sink *output,
1083 void *u) {
1084 dcgi_state *ds = u;
1085
1086 lookups(ds, DC_QUEUE);
1087 sink_printf(output->sink, "%s", bool2str(!!ds->g->queue));
1088}
1089
1090static void exp_isrecent(int attribute((unused)) nargs,
1091 char attribute((unused)) **args,
1092 cgi_sink *output,
1093 void *u) {
1094 dcgi_state *ds = u;
1095
1096 lookups(ds, DC_RECENT);
1097 sink_printf(output->sink, "%s", bool2str(!!ds->g->recent));
1098}
1099
78efa64e
RK
1100static void exp_isnew(int attribute((unused)) nargs,
1101 char attribute((unused)) **args,
1102 cgi_sink *output,
1103 void *u) {
1104 dcgi_state *ds = u;
1105
1106 lookups(ds, DC_NEW);
1107 sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew));
1108}
1109
460b9539 1110static void exp_id(int attribute((unused)) nargs,
1111 char attribute((unused)) **args,
1112 cgi_sink *output,
1113 void *u) {
1114 dcgi_state *ds = u;
1115
1116 if(ds->track)
1117 cgi_output(output, "%s", ds->track->id);
1118}
1119
1120static void exp_track(int attribute((unused)) nargs,
1121 char attribute((unused)) **args,
1122 cgi_sink *output,
1123 void *u) {
1124 dcgi_state *ds = u;
1125
1126 if(ds->track)
1127 cgi_output(output, "%s", ds->track->track);
1128}
1129
1130static void exp_parity(int attribute((unused)) nargs,
1131 char attribute((unused)) **args,
1132 cgi_sink *output,
1133 void *u) {
1134 dcgi_state *ds = u;
1135
1136 cgi_output(output, "%s", ds->index % 2 ? "odd" : "even");
1137}
1138
1139static void exp_comment(int attribute((unused)) nargs,
1140 char attribute((unused)) **args,
1141 cgi_sink attribute((unused)) *output,
1142 void attribute((unused)) *u) {
1143 /* do nothing */
1144}
1145
1146static void exp_prefname(int attribute((unused)) nargs,
1147 char attribute((unused)) **args,
1148 cgi_sink *output,
1149 void *u) {
1150 dcgi_state *ds = u;
1151
1152 if(ds->pref && ds->pref->name)
1153 cgi_output(output, "%s", ds->pref->name);
1154}
1155
1156static void exp_prefvalue(int attribute((unused)) nargs,
1157 char attribute((unused)) **args,
1158 cgi_sink *output,
1159 void *u) {
1160 dcgi_state *ds = u;
1161
1162 if(ds->pref && ds->pref->value)
1163 cgi_output(output, "%s", ds->pref->value);
1164}
1165
1166static void exp_isfiles(int attribute((unused)) nargs,
1167 char attribute((unused)) **args,
1168 cgi_sink *output,
1169 void *u) {
1170 dcgi_state *ds = u;
1171
1172 lookups(ds, DC_FILES);
1173 sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles));
1174}
1175
1176static void exp_isdirectories(int attribute((unused)) nargs,
1177 char attribute((unused)) **args,
1178 cgi_sink *output,
1179 void *u) {
1180 dcgi_state *ds = u;
1181
1182 lookups(ds, DC_DIRS);
1183 sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs));
1184}
1185
1186static void exp_choose(int attribute((unused)) nargs,
1187 char **args,
1188 cgi_sink *output,
1189 void *u) {
1190 dcgi_state *ds = u;
1191 dcgi_state substate;
1192 int nfiles, n;
1193 char **files;
1194 struct entry *e;
1195 const char *type, *what = expandarg(args[0], ds);
1196
1197 if(!strcmp(what, "files")) {
1198 lookups(ds, DC_FILES);
1199 files = ds->g->files;
1200 nfiles = ds->g->nfiles;
1201 type = "track";
1202 } else if(!strcmp(what, "directories")) {
1203 lookups(ds, DC_DIRS);
1204 files = ds->g->dirs;
1205 nfiles = ds->g->ndirs;
1206 type = "dir";
1207 } else {
1208 error(0, "unknown @choose@ argument '%s'", what);
1209 return;
1210 }
1211 e = xmalloc(nfiles * sizeof (struct entry));
1212 for(n = 0; n < nfiles; ++n) {
1213 e[n].path = files[n];
1214 e[n].sort = trackname_transform(type, files[n], "sort");
1215 e[n].display = trackname_transform(type, files[n], "display");
1216 }
1217 qsort(e, nfiles, sizeof (struct entry), compare_entry);
1218 memset(&substate, 0, sizeof substate);
1219 substate.g = ds->g;
1220 substate.first = 1;
1221 for(n = 0; n < nfiles; ++n) {
1222 substate.last = (n == nfiles - 1);
1223 substate.index = n;
1224 substate.entry = &e[n];
1225 expandstring(output, args[1], &substate);
1226 substate.first = 0;
1227 }
1228}
1229
1230static void exp_file(int attribute((unused)) nargs,
1231 char attribute((unused)) **args,
1232 cgi_sink *output,
1233 void *u) {
1234 dcgi_state *ds = u;
1235
1236 if(ds->entry)
1237 cgi_output(output, "%s", ds->entry->path);
1238 else if(ds->track)
1239 cgi_output(output, "%s", ds->track->track);
1240 else if(ds->tracks)
1241 cgi_output(output, "%s", ds->tracks[0]);
1242}
1243
1244static void exp_transform(int nargs,
1245 char **args,
1246 cgi_sink *output,
1247 void attribute((unused)) *u) {
1248 const char *context = nargs > 2 ? args[2] : "display";
1249
1250 cgi_output(output, "%s", trackname_transform(args[1], args[0], context));
1251}
1252
1253static void exp_urlquote(int attribute((unused)) nargs,
1254 char **args,
1255 cgi_sink *output,
1256 void attribute((unused)) *u) {
1257 cgi_output(output, "%s", urlencodestring(args[0]));
1258}
1259
1260static void exp_scratchable(int attribute((unused)) nargs,
1261 char attribute((unused)) **args,
1262 cgi_sink *output,
1263 void attribute((unused)) *u) {
1264 dcgi_state *ds = u;
938d8157 1265
1266 lookups(ds, DC_PLAYING|DC_RIGHTS);
1267 sink_printf(output->sink, "%s",
1268 bool2str(right_scratchable(ds->g->rights,
1269 disorder_user(ds->g->client),
1270 ds->g->playing)));
460b9539 1271}
1272
1273static void exp_removable(int attribute((unused)) nargs,
1274 char attribute((unused)) **args,
1275 cgi_sink *output,
1276 void attribute((unused)) *u) {
1277 dcgi_state *ds = u;
460b9539 1278
938d8157 1279 lookups(ds, DC_RIGHTS);
1280 sink_printf(output->sink, "%s",
1281 bool2str(right_removable(ds->g->rights,
1282 disorder_user(ds->g->client),
1283 ds->track)));
1284}
1285
1286static void exp_movable(int attribute((unused)) nargs,
1287 char attribute((unused)) **args,
1288 cgi_sink *output,
1289 void attribute((unused)) *u) {
1290 dcgi_state *ds = u;
1291
1292 lookups(ds, DC_RIGHTS);
1293 sink_printf(output->sink, "%s",
1294 bool2str(right_movable(ds->g->rights,
1295 disorder_user(ds->g->client),
1296 ds->track)));
460b9539 1297}
1298
1299static void exp_navigate(int attribute((unused)) nargs,
1300 char **args,
1301 cgi_sink *output,
1302 void *u) {
1303 dcgi_state *ds = u;
1304 dcgi_state substate;
1305 const char *path = expandarg(args[0], ds);
1306 const char *ptr;
1307 int dirlen;
1308
1309 if(*path) {
1310 memset(&substate, 0, sizeof substate);
1311 substate.g = ds->g;
1312 ptr = path + 1; /* skip root */
1313 dirlen = 0;
1314 substate.nav_path = path;
1315 substate.first = 1;
1316 while(*ptr) {
1317 while(*ptr && *ptr != '/')
1318 ++ptr;
1319 substate.last = !*ptr;
1320 substate.nav_len = ptr - path;
1321 substate.nav_dirlen = dirlen;
1322 expandstring(output, args[1], &substate);
1323 dirlen = substate.nav_len;
1324 if(*ptr) ++ptr;
1325 substate.first = 0;
1326 }
1327 }
1328}
1329
1330static void exp_fullname(int attribute((unused)) nargs,
1331 char attribute((unused)) **args,
1332 cgi_sink *output,
1333 void *u) {
1334 dcgi_state *ds = u;
1335 cgi_output(output, "%.*s", ds->nav_len, ds->nav_path);
1336}
1337
1338static void exp_basename(int nargs,
1339 char **args,
1340 cgi_sink *output,
1341 void *u) {
1342 dcgi_state *ds = u;
1343 const char *s;
1344
1345 if(nargs) {
1346 if((s = strrchr(args[0], '/'))) ++s;
1347 else s = args[0];
1348 cgi_output(output, "%s", s);
1349 } else
1350 cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1,
1351 ds->nav_path + ds->nav_dirlen + 1);
1352}
1353
1354static void exp_dirname(int nargs,
1355 char **args,
1356 cgi_sink *output,
1357 void *u) {
1358 dcgi_state *ds = u;
1359 const char *s;
1360
1361 if(nargs) {
1362 if((s = strrchr(args[0], '/')))
1363 cgi_output(output, "%.*s", (int)(s - args[0]), args[0]);
1364 } else
1365 cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path);
1366}
1367
1368static void exp_eq(int attribute((unused)) nargs,
1369 char **args,
1370 cgi_sink *output,
1371 void attribute((unused)) *u) {
1372 cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1])));
1373}
1374
1375static void exp_ne(int attribute((unused)) nargs,
1376 char **args,
1377 cgi_sink *output,
1378 void attribute((unused)) *u) {
1379 cgi_output(output, "%s", bool2str(strcmp(args[0], args[1])));
1380}
1381
1382static void exp_enabled(int attribute((unused)) nargs,
1383 char attribute((unused)) **args,
1384 cgi_sink *output,
1385 void *u) {
1386 dcgi_state *ds = u;
1387 int enabled = 0;
1388
1389 if(ds->g->client)
1390 disorder_enabled(ds->g->client, &enabled);
1391 cgi_output(output, "%s", bool2str(enabled));
1392}
1393
1394static void exp_random_enabled(int attribute((unused)) nargs,
1395 char attribute((unused)) **args,
1396 cgi_sink *output,
1397 void *u) {
1398 dcgi_state *ds = u;
1399 int enabled = 0;
1400
1401 if(ds->g->client)
1402 disorder_random_enabled(ds->g->client, &enabled);
1403 cgi_output(output, "%s", bool2str(enabled));
1404}
1405
1406static void exp_trackstate(int attribute((unused)) nargs,
1407 char **args,
1408 cgi_sink *output,
1409 void *u) {
1410 dcgi_state *ds = u;
1411 struct queue_entry *q;
1412 char *track;
1413
1414 if(disorder_resolve(ds->g->client, &track, args[0])) return;
1415 lookups(ds, DC_QUEUE|DC_PLAYING);
1416 if(ds->g->playing && !strcmp(ds->g->playing->track, track))
1417 cgi_output(output, "playing");
1418 else {
1419 for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next)
1420 ;
1421 if(q)
1422 cgi_output(output, "queued");
1423 }
1424}
1425
1426static void exp_thisurl(int attribute((unused)) nargs,
1427 char attribute((unused)) **args,
1428 cgi_sink *output,
1429 void attribute((unused)) *u) {
1430 kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */
1431 cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0));
1432}
1433
1434static void exp_isfirst(int attribute((unused)) nargs,
1435 char attribute((unused)) **args,
1436 cgi_sink *output,
1437 void *u) {
1438 dcgi_state *ds = u;
1439
1440 sink_printf(output->sink, "%s", bool2str(!!ds->first));
1441}
1442
1443static void exp_islast(int attribute((unused)) nargs,
1444 char attribute((unused)) **args,
1445 cgi_sink *output,
1446 void *u) {
1447 dcgi_state *ds = u;
1448
1449 sink_printf(output->sink, "%s", bool2str(!!ds->last));
1450}
1451
1452static void exp_action(int attribute((unused)) nargs,
1453 char attribute((unused)) **args,
1454 cgi_sink *output,
1455 void attribute((unused)) *u) {
1456 const char *action = cgi_get("action"), *mgmt;
1457
1458 if(!action) action = "playing";
1459 if(!strcmp(action, "playing")
1460 && (mgmt = cgi_get("mgmt"))
1461 && !strcmp(mgmt, "true"))
1462 action = "manage";
1463 sink_printf(output->sink, "%s", action);
1464}
1465
1466static void exp_resolve(int attribute((unused)) nargs,
1467 char **args,
1468 cgi_sink *output,
1469 void attribute((unused)) *u) {
1470 dcgi_state *ds = u;
1471 char *track;
1472
1473 if(!disorder_resolve(ds->g->client, &track, args[0]))
1474 sink_printf(output->sink, "%s", track);
1475}
1476
1477static void exp_paused(int attribute((unused)) nargs,
1478 char attribute((unused)) **args,
1479 cgi_sink *output,
1480 void *u) {
1481 dcgi_state *ds = u;
1482 int paused = 0;
1483
1484 lookups(ds, DC_PLAYING);
1485 if(ds->g->playing && ds->g->playing->state == playing_paused)
1486 paused = 1;
1487 cgi_output(output, "%s", bool2str(paused));
1488}
1489
1490static void exp_state(int attribute((unused)) nargs,
1491 char attribute((unused)) **args,
1492 cgi_sink *output,
1493 void *u) {
1494 dcgi_state *ds = u;
1495
1496 if(ds->track)
1497 cgi_output(output, "%s", playing_states[ds->track->state]);
1498}
1499
1500static void exp_files(int attribute((unused)) nargs,
1501 char **args,
1502 cgi_sink *output,
1503 void *u) {
1504 dcgi_state *ds = u;
1505 dcgi_state substate;
1506 const char *nfiles_arg, *directory;
1507 int nfiles, numfile;
1508 struct kvp *k;
1509
1510 memset(&substate, 0, sizeof substate);
1511 substate.g = ds->g;
1512 if((directory = cgi_get("directory"))) {
1513 /* Prefs for whole directory. */
1514 lookups(ds, DC_FILES);
1515 /* Synthesize args for the file list. */
1516 nfiles = ds->g->nfiles;
1517 for(numfile = 0; numfile < nfiles; ++numfile) {
1518 k = xmalloc(sizeof *k);
1519 byte_xasprintf((char **)&k->name, "%d_file", numfile);
1520 k->value = ds->g->files[numfile];
1521 k->next = cgi_args;
1522 cgi_args = k;
1523 }
1524 } else {
1525 /* Args already present. */
1526 if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg);
1527 else nfiles = 1;
1528 }
1529 for(numfile = 0; numfile < nfiles; ++numfile) {
1530 substate.index = numfile;
1531 expandstring(output, args[0], &substate);
1532 }
1533}
1534
1535static void exp_index(int attribute((unused)) nargs,
1536 char attribute((unused)) **args,
1537 cgi_sink *output,
1538 void *u) {
1539 dcgi_state *ds = u;
1540
1541 cgi_output(output, "%d", ds->index);
1542}
1543
1544static void exp_nfiles(int attribute((unused)) nargs,
1545 char attribute((unused)) **args,
1546 cgi_sink *output,
1547 void *u) {
1548 dcgi_state *ds = u;
1549 const char *files_arg;
1550
1551 if(cgi_get("directory")) {
1552 lookups(ds, DC_FILES);
1553 cgi_output(output, "%d", ds->g->nfiles);
1554 } else if((files_arg = cgi_get("files")))
1555 cgi_output(output, "%s", files_arg);
1556 else
1557 cgi_output(output, "1");
1558}
1559
fdf98378 1560static void exp_user(int attribute((unused)) nargs,
1561 char attribute((unused)) **args,
1562 cgi_sink *output,
1563 void *u) {
1564 dcgi_state *const ds = u;
1565
1566 cgi_output(output, "%s", disorder_user(ds->g->client));
1567}
1568
938d8157 1569static void exp_right(int attribute((unused)) nargs,
1570 char **args,
1571 cgi_sink *output,
1572 void *u) {
1573 dcgi_state *const ds = u;
1574 const char *right = expandarg(args[0], ds);
1575 rights_type r;
1576
1577 lookups(ds, DC_RIGHTS);
1578 if(parse_rights(right, &r, 1/*report*/))
1579 r = 0;
1580 if(args[1] == 0)
1581 cgi_output(output, "%s", bool2str(!!(r & ds->g->rights)));
1582 else if(r & ds->g->rights)
1583 expandstring(output, args[1], ds);
1584 else if(args[2])
1585 expandstring(output, args[2], ds);
1586}
1587
460b9539 1588static const struct cgi_expansion expansions[] = {
1589 { "#", 0, INT_MAX, EXP_MAGIC, exp_comment },
1590 { "action", 0, 0, 0, exp_action },
1591 { "and", 0, INT_MAX, EXP_MAGIC, exp_and },
1592 { "arg", 1, 1, 0, exp_arg },
1593 { "basename", 0, 1, 0, exp_basename },
1594 { "choose", 2, 2, EXP_MAGIC, exp_choose },
1595 { "dirname", 0, 1, 0, exp_dirname },
1596 { "enabled", 0, 0, 0, exp_enabled },
1597 { "eq", 2, 2, 0, exp_eq },
1598 { "file", 0, 0, 0, exp_file },
1599 { "files", 1, 1, EXP_MAGIC, exp_files },
1600 { "fullname", 0, 0, 0, exp_fullname },
1601 { "id", 0, 0, 0, exp_id },
1602 { "if", 2, 3, EXP_MAGIC, exp_if },
1603 { "include", 1, 1, 0, exp_include },
1604 { "index", 0, 0, 0, exp_index },
1605 { "isdirectories", 0, 0, 0, exp_isdirectories },
1606 { "isfiles", 0, 0, 0, exp_isfiles },
1607 { "isfirst", 0, 0, 0, exp_isfirst },
1608 { "islast", 0, 0, 0, exp_islast },
78efa64e 1609 { "isnew", 0, 0, 0, exp_isnew },
460b9539 1610 { "isplaying", 0, 0, 0, exp_isplaying },
1611 { "isqueue", 0, 0, 0, exp_isqueue },
1612 { "isrecent", 0, 0, 0, exp_isrecent },
1613 { "label", 1, 1, 0, exp_label },
1614 { "length", 0, 0, 0, exp_length },
938d8157 1615 { "movable", 0, 0, 0, exp_movable },
460b9539 1616 { "navigate", 2, 2, EXP_MAGIC, exp_navigate },
1617 { "ne", 2, 2, 0, exp_ne },
78efa64e 1618 { "new", 1, 1, EXP_MAGIC, exp_new },
460b9539 1619 { "nfiles", 0, 0, 0, exp_nfiles },
1620 { "nonce", 0, 0, 0, exp_nonce },
1621 { "not", 1, 1, 0, exp_not },
1622 { "or", 0, INT_MAX, EXP_MAGIC, exp_or },
1623 { "parity", 0, 0, 0, exp_parity },
1624 { "part", 1, 3, 0, exp_part },
1625 { "paused", 0, 0, 0, exp_paused },
1626 { "playing", 1, 1, EXP_MAGIC, exp_playing },
1627 { "pref", 2, 2, 0, exp_pref },
1628 { "prefname", 0, 0, 0, exp_prefname },
1629 { "prefs", 2, 2, EXP_MAGIC, exp_prefs },
1630 { "prefvalue", 0, 0, 0, exp_prefvalue },
1631 { "queue", 1, 1, EXP_MAGIC, exp_queue },
1632 { "random-enabled", 0, 0, 0, exp_random_enabled },
1633 { "recent", 1, 1, EXP_MAGIC, exp_recent },
1634 { "removable", 0, 0, 0, exp_removable },
1635 { "resolve", 1, 1, 0, exp_resolve },
938d8157 1636 { "right", 1, 3, EXP_MAGIC, exp_right },
460b9539 1637 { "scratchable", 0, 0, 0, exp_scratchable },
1638 { "search", 2, 3, EXP_MAGIC, exp_search },
1639 { "server-version", 0, 0, 0, exp_server_version },
1640 { "shell", 1, 1, 0, exp_shell },
1641 { "state", 0, 0, 0, exp_state },
1642 { "stats", 0, 0, 0, exp_stats },
1643 { "thisurl", 0, 0, 0, exp_thisurl },
1644 { "track", 0, 0, 0, exp_track },
1645 { "trackstate", 1, 1, 0, exp_trackstate },
1646 { "transform", 2, 3, 0, exp_transform },
1647 { "url", 0, 0, 0, exp_url },
1648 { "urlquote", 1, 1, 0, exp_urlquote },
fdf98378 1649 { "user", 0, 0, 0, exp_user },
460b9539 1650 { "version", 0, 0, 0, exp_version },
1651 { "volume", 1, 1, 0, exp_volume },
1652 { "when", 0, 0, 0, exp_when },
1653 { "who", 0, 0, 0, exp_who }
1654};
1655
1656static void expand(cgi_sink *output,
1657 const char *template,
1658 dcgi_state *ds) {
1659 cgi_expand(template,
1660 expansions, sizeof expansions / sizeof *expansions,
1661 output,
1662 ds);
1663}
1664
1665static void expandstring(cgi_sink *output,
1666 const char *string,
1667 dcgi_state *ds) {
1668 cgi_expand_string("",
1669 string,
1670 expansions, sizeof expansions / sizeof *expansions,
1671 output,
1672 ds);
1673}
1674
1675static void perform_action(cgi_sink *output, dcgi_state *ds,
1676 const char *action) {
1677 int n;
1678
fdf98378 1679 /* We don't ever want anything to be cached */
1680 cgi_header(output->sink, "Cache-Control", "no-cache");
460b9539 1681 if((n = TABLE_FIND(actions, struct action, name, action)) >= 0)
1682 actions[n].handler(output, ds);
fdf98378 1683 else
1684 expand_template(ds, output, action);
460b9539 1685}
1686
1687void disorder_cgi(cgi_sink *output, dcgi_state *ds) {
1688 const char *action = cgi_get("action");
1689
1690 if(!action) action = "playing";
1691 perform_action(output, ds, action);
1692}
1693
1694void disorder_cgi_error(cgi_sink *output, dcgi_state *ds,
1695 const char *msg) {
1696 cgi_set_option("error", msg);
1697 perform_action(output, ds, "error");
1698}
1699
938d8157 1700/** @brief Log in as the current user or guest if none */
1701void disorder_cgi_login(dcgi_state *ds, cgi_sink *output) {
1702 /* Create a new connection */
1703 ds->g->client = disorder_new(0);
1704 /* Forget everything we knew */
1705 ds->g->flags = 0;
1706 /* Reconnect */
1707 if(disorder_connect_cookie(ds->g->client, login_cookie)) {
1708 disorder_cgi_error(output, ds, "connect");
1709 exit(0);
1710 }
1711 /* If there was a cookie but it went bad, we forget it */
1712 if(login_cookie && !strcmp(disorder_user(ds->g->client), "guest"))
1713 login_cookie = 0;
1714}
1715
460b9539 1716/*
1717Local Variables:
1718c-basic-offset:2
1719comment-column:40
1720fill-column:79
1721End:
1722*/