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