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