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