| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell |
| 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" |
| 42 | #include "dcgi.h" |
| 43 | #include "log.h" |
| 44 | #include "configuration.h" |
| 45 | #include "table.h" |
| 46 | #include "queue.h" |
| 47 | #include "plugin.h" |
| 48 | #include "split.h" |
| 49 | #include "words.h" |
| 50 | #include "wstat.h" |
| 51 | #include "kvp.h" |
| 52 | #include "syscalls.h" |
| 53 | #include "printf.h" |
| 54 | #include "regsub.h" |
| 55 | #include "defs.h" |
| 56 | #include "trackname.h" |
| 57 | #include "charset.h" |
| 58 | |
| 59 | static void expand(cgi_sink *output, |
| 60 | const char *template, |
| 61 | dcgi_state *ds); |
| 62 | static void expandstring(cgi_sink *output, |
| 63 | const char *string, |
| 64 | dcgi_state *ds); |
| 65 | |
| 66 | struct entry { |
| 67 | const char *path; |
| 68 | const char *sort; |
| 69 | const char *display; |
| 70 | }; |
| 71 | |
| 72 | static const char *nonce(void) { |
| 73 | static unsigned long count; |
| 74 | char *s; |
| 75 | |
| 76 | byte_xasprintf(&s, "%lx%lx%lx", |
| 77 | (unsigned long)time(0), |
| 78 | (unsigned long)getpid(), |
| 79 | count++); |
| 80 | return s; |
| 81 | } |
| 82 | |
| 83 | static int compare_entry(const void *a, const void *b) { |
| 84 | const struct entry *ea = a, *eb = b; |
| 85 | |
| 86 | return compare_tracks(ea->sort, eb->sort, |
| 87 | ea->display, eb->display, |
| 88 | ea->path, eb->path); |
| 89 | } |
| 90 | |
| 91 | static const char *front_url(void) { |
| 92 | char *url; |
| 93 | const char *mgmt; |
| 94 | |
| 95 | /* preserve management interface visibility */ |
| 96 | if((mgmt = cgi_get("mgmt")) && !strcmp(mgmt, "true")) { |
| 97 | byte_xasprintf(&url, "%s?mgmt=true", config->url); |
| 98 | return url; |
| 99 | } |
| 100 | return config->url; |
| 101 | } |
| 102 | |
| 103 | static void redirect(struct sink *output) { |
| 104 | const char *back; |
| 105 | |
| 106 | cgi_header(output, "Location", |
| 107 | (back = cgi_get("back")) ? back : front_url()); |
| 108 | cgi_body(output); |
| 109 | } |
| 110 | |
| 111 | static void lookups(dcgi_state *ds, unsigned want) { |
| 112 | unsigned need; |
| 113 | struct queue_entry *r, *rnext; |
| 114 | const char *dir, *re; |
| 115 | |
| 116 | if(ds->g->client && (need = want ^ (ds->g->flags & want)) != 0) { |
| 117 | if(need & DC_QUEUE) |
| 118 | disorder_queue(ds->g->client, &ds->g->queue); |
| 119 | if(need & DC_PLAYING) |
| 120 | disorder_playing(ds->g->client, &ds->g->playing); |
| 121 | if(need & DC_NEW) |
| 122 | disorder_new_tracks(ds->g->client, &ds->g->new, &ds->g->nnew, 0); |
| 123 | if(need & DC_RECENT) { |
| 124 | /* we need to reverse the order of the list */ |
| 125 | disorder_recent(ds->g->client, &r); |
| 126 | while(r) { |
| 127 | rnext = r->next; |
| 128 | r->next = ds->g->recent; |
| 129 | ds->g->recent = r; |
| 130 | r = rnext; |
| 131 | } |
| 132 | } |
| 133 | if(need & DC_VOLUME) |
| 134 | disorder_get_volume(ds->g->client, |
| 135 | &ds->g->volume_left, &ds->g->volume_right); |
| 136 | if(need & (DC_FILES|DC_DIRS)) { |
| 137 | if(!(dir = cgi_get("directory"))) |
| 138 | dir = ""; |
| 139 | re = cgi_get("regexp"); |
| 140 | if(need & DC_DIRS) |
| 141 | if(disorder_directories(ds->g->client, dir, re, |
| 142 | &ds->g->dirs, &ds->g->ndirs)) |
| 143 | ds->g->ndirs = 0; |
| 144 | if(need & DC_FILES) |
| 145 | if(disorder_files(ds->g->client, dir, re, |
| 146 | &ds->g->files, &ds->g->nfiles)) |
| 147 | ds->g->nfiles = 0; |
| 148 | } |
| 149 | ds->g->flags |= need; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | /* actions ********************************************************************/ |
| 154 | |
| 155 | static void act_disable(cgi_sink *output, |
| 156 | dcgi_state *ds) { |
| 157 | if(ds->g->client) |
| 158 | disorder_disable(ds->g->client); |
| 159 | redirect(output->sink); |
| 160 | } |
| 161 | |
| 162 | static void act_enable(cgi_sink *output, |
| 163 | dcgi_state *ds) { |
| 164 | if(ds->g->client) |
| 165 | disorder_enable(ds->g->client); |
| 166 | redirect(output->sink); |
| 167 | } |
| 168 | |
| 169 | static void act_random_disable(cgi_sink *output, |
| 170 | dcgi_state *ds) { |
| 171 | if(ds->g->client) |
| 172 | disorder_random_disable(ds->g->client); |
| 173 | redirect(output->sink); |
| 174 | } |
| 175 | |
| 176 | static void act_random_enable(cgi_sink *output, |
| 177 | dcgi_state *ds) { |
| 178 | if(ds->g->client) |
| 179 | disorder_random_enable(ds->g->client); |
| 180 | redirect(output->sink); |
| 181 | } |
| 182 | |
| 183 | static void act_remove(cgi_sink *output, |
| 184 | dcgi_state *ds) { |
| 185 | const char *id; |
| 186 | |
| 187 | if(!(id = cgi_get("id"))) fatal(0, "missing id argument"); |
| 188 | if(ds->g->client) |
| 189 | disorder_remove(ds->g->client, id); |
| 190 | redirect(output->sink); |
| 191 | } |
| 192 | |
| 193 | static void act_move(cgi_sink *output, |
| 194 | dcgi_state *ds) { |
| 195 | const char *id, *delta; |
| 196 | |
| 197 | if(!(id = cgi_get("id"))) fatal(0, "missing id argument"); |
| 198 | if(!(delta = cgi_get("delta"))) fatal(0, "missing delta argument"); |
| 199 | if(ds->g->client) |
| 200 | disorder_move(ds->g->client, id, atoi(delta)); |
| 201 | redirect(output->sink); |
| 202 | } |
| 203 | |
| 204 | static void act_scratch(cgi_sink *output, |
| 205 | dcgi_state *ds) { |
| 206 | if(ds->g->client) |
| 207 | disorder_scratch(ds->g->client, cgi_get("id")); |
| 208 | redirect(output->sink); |
| 209 | } |
| 210 | |
| 211 | static void act_playing(cgi_sink *output, dcgi_state *ds) { |
| 212 | char r[1024]; |
| 213 | long refresh = config->refresh, length; |
| 214 | time_t now, fin; |
| 215 | int random_enabled = 0; |
| 216 | int enabled = 0; |
| 217 | |
| 218 | lookups(ds, DC_PLAYING|DC_QUEUE); |
| 219 | cgi_header(output->sink, "Content-Type", "text/html"); |
| 220 | disorder_random_enabled(ds->g->client, &random_enabled); |
| 221 | disorder_enabled(ds->g->client, &enabled); |
| 222 | if(ds->g->playing |
| 223 | && ds->g->playing->state == playing_started /* i.e. not paused */ |
| 224 | && !disorder_length(ds->g->client, ds->g->playing->track, &length) |
| 225 | && length |
| 226 | && ds->g->playing->sofar >= 0) { |
| 227 | /* Try to put the next refresh at the start of the next track. */ |
| 228 | time(&now); |
| 229 | fin = now + length - ds->g->playing->sofar + config->gap; |
| 230 | if(now + refresh > fin) |
| 231 | refresh = fin - now; |
| 232 | } |
| 233 | if(ds->g->queue && ds->g->queue->state == playing_isscratch) { |
| 234 | /* next track is a scratch, don't leave more than the inter-track gap */ |
| 235 | if(refresh > config->gap) |
| 236 | refresh = config->gap; |
| 237 | } |
| 238 | if(!ds->g->playing && ((ds->g->queue |
| 239 | && ds->g->queue->state != playing_random) |
| 240 | || random_enabled) && enabled) { |
| 241 | /* no track playing but playing is enabled and there is something coming |
| 242 | * up, must be in a gap */ |
| 243 | if(refresh > config->gap) |
| 244 | refresh = config->gap; |
| 245 | } |
| 246 | byte_snprintf(r, sizeof r, "%ld;url=%s", refresh > 0 ? refresh : 1, |
| 247 | front_url()); |
| 248 | cgi_header(output->sink, "Refresh", r); |
| 249 | cgi_body(output->sink); |
| 250 | expand(output, "playing", ds); |
| 251 | } |
| 252 | |
| 253 | static void act_play(cgi_sink *output, |
| 254 | dcgi_state *ds) { |
| 255 | const char *track, *dir; |
| 256 | char **tracks; |
| 257 | int ntracks, n; |
| 258 | struct entry *e; |
| 259 | |
| 260 | if((track = cgi_get("file"))) { |
| 261 | disorder_play(ds->g->client, track); |
| 262 | } else if((dir = cgi_get("directory"))) { |
| 263 | if(disorder_files(ds->g->client, dir, 0, &tracks, &ntracks)) ntracks = 0; |
| 264 | if(ntracks) { |
| 265 | e = xmalloc(ntracks * sizeof (struct entry)); |
| 266 | for(n = 0; n < ntracks; ++n) { |
| 267 | e[n].path = tracks[n]; |
| 268 | e[n].sort = trackname_transform("track", tracks[n], "sort"); |
| 269 | e[n].display = trackname_transform("track", tracks[n], "display"); |
| 270 | } |
| 271 | qsort(e, ntracks, sizeof (struct entry), compare_entry); |
| 272 | for(n = 0; n < ntracks; ++n) |
| 273 | disorder_play(ds->g->client, e[n].path); |
| 274 | } |
| 275 | } |
| 276 | /* XXX error handling */ |
| 277 | redirect(output->sink); |
| 278 | } |
| 279 | |
| 280 | static int clamp(int n, int min, int max) { |
| 281 | if(n < min) |
| 282 | return min; |
| 283 | if(n > max) |
| 284 | return max; |
| 285 | return n; |
| 286 | } |
| 287 | |
| 288 | static const char *volume_url(void) { |
| 289 | char *url; |
| 290 | |
| 291 | byte_xasprintf(&url, "%s?action=volume", config->url); |
| 292 | return url; |
| 293 | } |
| 294 | |
| 295 | static void act_volume(cgi_sink *output, dcgi_state *ds) { |
| 296 | const char *l, *r, *d, *back; |
| 297 | int nd, changed = 0;; |
| 298 | |
| 299 | if((d = cgi_get("delta"))) { |
| 300 | lookups(ds, DC_VOLUME); |
| 301 | nd = clamp(atoi(d), -255, 255); |
| 302 | disorder_set_volume(ds->g->client, |
| 303 | clamp(ds->g->volume_left + nd, 0, 255), |
| 304 | clamp(ds->g->volume_right + nd, 0, 255)); |
| 305 | changed = 1; |
| 306 | } else if((l = cgi_get("left")) && (r = cgi_get("right"))) { |
| 307 | disorder_set_volume(ds->g->client, atoi(l), atoi(r)); |
| 308 | changed = 1; |
| 309 | } |
| 310 | if(changed) { |
| 311 | /* redirect back to ourselves (but without the volume-changing bits in the |
| 312 | * URL) */ |
| 313 | cgi_header(output->sink, "Location", |
| 314 | (back = cgi_get("back")) ? back : volume_url()); |
| 315 | cgi_body(output->sink); |
| 316 | } else { |
| 317 | cgi_header(output->sink, "Content-Type", "text/html"); |
| 318 | cgi_body(output->sink); |
| 319 | expand(output, "volume", ds); |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | static void act_prefs_errors(const char *msg, |
| 324 | void attribute((unused)) *u) { |
| 325 | fatal(0, "error splitting parts list: %s", msg); |
| 326 | } |
| 327 | |
| 328 | static const char *numbered_arg(const char *argname, int numfile) { |
| 329 | char *fullname; |
| 330 | |
| 331 | byte_xasprintf(&fullname, "%d_%s", numfile, argname); |
| 332 | return cgi_get(fullname); |
| 333 | } |
| 334 | |
| 335 | static void process_prefs(dcgi_state *ds, int numfile) { |
| 336 | const char *file, *name, *value, *part, *parts, *current, *context; |
| 337 | char **partslist; |
| 338 | |
| 339 | if(!(file = numbered_arg("file", numfile))) |
| 340 | /* The first file doesn't need numbering. */ |
| 341 | if(numfile > 0 || !(file = cgi_get("file"))) |
| 342 | return; |
| 343 | if((parts = numbered_arg("parts", numfile)) |
| 344 | || (parts = cgi_get("parts"))) { |
| 345 | /* Default context is display. Other contexts not actually tested. */ |
| 346 | if(!(context = numbered_arg("context", numfile))) context = "display"; |
| 347 | partslist = split(parts, 0, 0, act_prefs_errors, 0); |
| 348 | while((part = *partslist++)) { |
| 349 | if(!(value = numbered_arg(part, numfile))) |
| 350 | continue; |
| 351 | /* If it's already right (whether regexps or db) don't change anything, |
| 352 | * so we don't fill the database up with rubbish. */ |
| 353 | if(disorder_part(ds->g->client, (char **)¤t, |
| 354 | file, context, part)) |
| 355 | fatal(0, "disorder_part() failed"); |
| 356 | if(!strcmp(current, value)) |
| 357 | continue; |
| 358 | byte_xasprintf((char **)&name, "trackname_%s_%s", context, part); |
| 359 | disorder_set(ds->g->client, file, name, value); |
| 360 | } |
| 361 | if((value = numbered_arg("random", numfile))) |
| 362 | disorder_unset(ds->g->client, file, "pick_at_random"); |
| 363 | else |
| 364 | disorder_set(ds->g->client, file, "pick_at_random", "0"); |
| 365 | if((value = numbered_arg("tags", numfile))) |
| 366 | disorder_set(ds->g->client, file, "tags", value); |
| 367 | } else if((name = cgi_get("name"))) { |
| 368 | /* Raw preferences. Not well supported in the templates at the moment. */ |
| 369 | value = cgi_get("value"); |
| 370 | if(value) |
| 371 | disorder_set(ds->g->client, file, name, value); |
| 372 | else |
| 373 | disorder_unset(ds->g->client, file, name); |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | static void act_prefs(cgi_sink *output, dcgi_state *ds) { |
| 378 | const char *files; |
| 379 | int nfiles, numfile; |
| 380 | |
| 381 | if((files = cgi_get("files"))) nfiles = atoi(files); |
| 382 | else nfiles = 1; |
| 383 | for(numfile = 0; numfile < nfiles; ++numfile) |
| 384 | process_prefs(ds, numfile); |
| 385 | cgi_header(output->sink, "Content-Type", "text/html"); |
| 386 | cgi_body(output->sink); |
| 387 | expand(output, "prefs", ds); |
| 388 | } |
| 389 | |
| 390 | static void act_pause(cgi_sink *output, |
| 391 | dcgi_state *ds) { |
| 392 | if(ds->g->client) |
| 393 | disorder_pause(ds->g->client); |
| 394 | redirect(output->sink); |
| 395 | } |
| 396 | |
| 397 | static void act_resume(cgi_sink *output, |
| 398 | dcgi_state *ds) { |
| 399 | if(ds->g->client) |
| 400 | disorder_resume(ds->g->client); |
| 401 | redirect(output->sink); |
| 402 | } |
| 403 | |
| 404 | static const struct action { |
| 405 | const char *name; |
| 406 | void (*handler)(cgi_sink *output, dcgi_state *ds); |
| 407 | } actions[] = { |
| 408 | { "disable", act_disable }, |
| 409 | { "enable", act_enable }, |
| 410 | { "move", act_move }, |
| 411 | { "pause", act_pause }, |
| 412 | { "play", act_play }, |
| 413 | { "playing", act_playing }, |
| 414 | { "prefs", act_prefs }, |
| 415 | { "random-disable", act_random_disable }, |
| 416 | { "random-enable", act_random_enable }, |
| 417 | { "remove", act_remove }, |
| 418 | { "resume", act_resume }, |
| 419 | { "scratch", act_scratch }, |
| 420 | { "volume", act_volume }, |
| 421 | }; |
| 422 | |
| 423 | /* expansions *****************************************************************/ |
| 424 | |
| 425 | static void exp_include(int attribute((unused)) nargs, |
| 426 | char **args, |
| 427 | cgi_sink *output, |
| 428 | void *u) { |
| 429 | expand(output, args[0], u); |
| 430 | } |
| 431 | |
| 432 | static void exp_server_version(int attribute((unused)) nargs, |
| 433 | char attribute((unused)) **args, |
| 434 | cgi_sink *output, |
| 435 | void *u) { |
| 436 | dcgi_state *ds = u; |
| 437 | const char *v; |
| 438 | |
| 439 | if(ds->g->client) { |
| 440 | if(disorder_version(ds->g->client, (char **)&v)) v = "(cannot get version)"; |
| 441 | } else |
| 442 | v = "(server not running)"; |
| 443 | cgi_output(output, "%s", v); |
| 444 | } |
| 445 | |
| 446 | static void exp_version(int attribute((unused)) nargs, |
| 447 | char attribute((unused)) **args, |
| 448 | cgi_sink *output, |
| 449 | void attribute((unused)) *u) { |
| 450 | cgi_output(output, "%s", disorder_version_string); |
| 451 | } |
| 452 | |
| 453 | static void exp_nonce(int attribute((unused)) nargs, |
| 454 | char attribute((unused)) **args, |
| 455 | cgi_sink *output, |
| 456 | void attribute((unused)) *u) { |
| 457 | cgi_output(output, "%s", nonce()); |
| 458 | } |
| 459 | |
| 460 | static void exp_label(int attribute((unused)) nargs, |
| 461 | char **args, |
| 462 | cgi_sink *output, |
| 463 | void attribute((unused)) *u) { |
| 464 | cgi_output(output, "%s", cgi_label(args[0])); |
| 465 | } |
| 466 | |
| 467 | struct trackinfo_state { |
| 468 | dcgi_state *ds; |
| 469 | const struct queue_entry *q; |
| 470 | long length; |
| 471 | time_t when; |
| 472 | }; |
| 473 | |
| 474 | static void exp_who(int attribute((unused)) nargs, |
| 475 | char attribute((unused)) **args, |
| 476 | cgi_sink *output, |
| 477 | void *u) { |
| 478 | dcgi_state *ds = u; |
| 479 | |
| 480 | if(ds->track && ds->track->submitter) |
| 481 | cgi_output(output, "%s", ds->track->submitter); |
| 482 | } |
| 483 | |
| 484 | static void exp_length(int attribute((unused)) nargs, |
| 485 | char attribute((unused)) **args, |
| 486 | cgi_sink *output, |
| 487 | void *u) { |
| 488 | dcgi_state *ds = u; |
| 489 | long length = 0; |
| 490 | |
| 491 | if(ds->track |
| 492 | && (ds->track->state == playing_started |
| 493 | || ds->track->state == playing_paused) |
| 494 | && ds->track->sofar >= 0) |
| 495 | cgi_output(output, "%ld:%02ld/", |
| 496 | ds->track->sofar / 60, ds->track->sofar % 60); |
| 497 | length = 0; |
| 498 | if(ds->track) |
| 499 | disorder_length(ds->g->client, ds->track->track, &length); |
| 500 | else if(ds->tracks) |
| 501 | disorder_length(ds->g->client, ds->tracks[0], &length); |
| 502 | if(length) |
| 503 | cgi_output(output, "%ld:%02ld", length / 60, length % 60); |
| 504 | else |
| 505 | sink_printf(output->sink, "%s", " "); |
| 506 | } |
| 507 | |
| 508 | static void exp_when(int attribute((unused)) nargs, |
| 509 | char attribute((unused)) **args, |
| 510 | cgi_sink *output, |
| 511 | void *u) { |
| 512 | dcgi_state *ds = u; |
| 513 | const struct tm *w = 0; |
| 514 | |
| 515 | if(ds->track) |
| 516 | switch(ds->track->state) { |
| 517 | case playing_isscratch: |
| 518 | case playing_unplayed: |
| 519 | case playing_random: |
| 520 | if(ds->track->expected) |
| 521 | w = localtime(&ds->track->expected); |
| 522 | break; |
| 523 | case playing_failed: |
| 524 | case playing_no_player: |
| 525 | case playing_ok: |
| 526 | case playing_scratched: |
| 527 | case playing_started: |
| 528 | case playing_paused: |
| 529 | case playing_quitting: |
| 530 | if(ds->track->played) |
| 531 | w = localtime(&ds->track->played); |
| 532 | break; |
| 533 | } |
| 534 | if(w) |
| 535 | cgi_output(output, "%d:%02d", w->tm_hour, w->tm_min); |
| 536 | else |
| 537 | sink_printf(output->sink, " "); |
| 538 | } |
| 539 | |
| 540 | static void exp_part(int nargs, |
| 541 | char **args, |
| 542 | cgi_sink *output, |
| 543 | void *u) { |
| 544 | dcgi_state *ds = u; |
| 545 | const char *s, *track, *part, *context; |
| 546 | |
| 547 | if(nargs == 3) |
| 548 | track = args[2]; |
| 549 | else { |
| 550 | if(ds->track) |
| 551 | track = ds->track->track; |
| 552 | else if(ds->tracks) |
| 553 | track = ds->tracks[0]; |
| 554 | else |
| 555 | track = 0; |
| 556 | } |
| 557 | if(track) { |
| 558 | switch(nargs) { |
| 559 | case 1: |
| 560 | context = "display"; |
| 561 | part = args[0]; |
| 562 | break; |
| 563 | case 2: |
| 564 | case 3: |
| 565 | context = args[0]; |
| 566 | part = args[1]; |
| 567 | break; |
| 568 | default: |
| 569 | abort(); |
| 570 | } |
| 571 | if(disorder_part(ds->g->client, (char **)&s, track, |
| 572 | !strcmp(context, "short") ? "display" : context, part)) |
| 573 | fatal(0, "disorder_part() failed"); |
| 574 | if(!strcmp(context, "short")) |
| 575 | s = truncate_for_display(s, config->short_display); |
| 576 | cgi_output(output, "%s", s); |
| 577 | } else |
| 578 | sink_printf(output->sink, " "); |
| 579 | } |
| 580 | |
| 581 | static void exp_playing(int attribute((unused)) nargs, |
| 582 | char **args, |
| 583 | cgi_sink *output, |
| 584 | void *u) { |
| 585 | dcgi_state *ds = u; |
| 586 | dcgi_state s; |
| 587 | |
| 588 | lookups(ds, DC_PLAYING); |
| 589 | memset(&s, 0, sizeof s); |
| 590 | s.g = ds->g; |
| 591 | if(ds->g->playing) { |
| 592 | s.track = ds->g->playing; |
| 593 | expandstring(output, args[0], &s); |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | static void exp_queue(int attribute((unused)) nargs, |
| 598 | char **args, |
| 599 | cgi_sink *output, |
| 600 | void *u) { |
| 601 | dcgi_state *ds = u; |
| 602 | dcgi_state s; |
| 603 | struct queue_entry *q; |
| 604 | |
| 605 | lookups(ds, DC_QUEUE); |
| 606 | memset(&s, 0, sizeof s); |
| 607 | s.g = ds->g; |
| 608 | s.first = 1; |
| 609 | for(q = ds->g->queue; q; q = q->next) { |
| 610 | s.last = !q->next; |
| 611 | s.track = q; |
| 612 | expandstring(output, args[0], &s); |
| 613 | s.index++; |
| 614 | s.first = 0; |
| 615 | } |
| 616 | } |
| 617 | |
| 618 | static void exp_recent(int attribute((unused)) nargs, |
| 619 | char **args, |
| 620 | cgi_sink *output, |
| 621 | void *u) { |
| 622 | dcgi_state *ds = u; |
| 623 | dcgi_state s; |
| 624 | struct queue_entry *q; |
| 625 | |
| 626 | lookups(ds, DC_RECENT); |
| 627 | memset(&s, 0, sizeof s); |
| 628 | s.g = ds->g; |
| 629 | s.first = 1; |
| 630 | for(q = ds->g->recent; q; q = q->next) { |
| 631 | s.last = !q; |
| 632 | s.track = q; |
| 633 | expandstring(output, args[0], &s); |
| 634 | s.index++; |
| 635 | s.first = 0; |
| 636 | } |
| 637 | } |
| 638 | |
| 639 | static void exp_new(int attribute((unused)) nargs, |
| 640 | char **args, |
| 641 | cgi_sink *output, |
| 642 | void *u) { |
| 643 | dcgi_state *ds = u; |
| 644 | dcgi_state s; |
| 645 | |
| 646 | lookups(ds, DC_NEW); |
| 647 | memset(&s, 0, sizeof s); |
| 648 | s.g = ds->g; |
| 649 | s.first = 1; |
| 650 | for(s.index = 0; s.index < ds->g->nnew; ++s.index) { |
| 651 | s.last = s.index + 1 < ds->g->nnew; |
| 652 | s.tracks = &ds->g->new[s.index]; |
| 653 | expandstring(output, args[0], &s); |
| 654 | s.first = 0; |
| 655 | } |
| 656 | } |
| 657 | |
| 658 | static void exp_url(int attribute((unused)) nargs, |
| 659 | char attribute((unused)) **args, |
| 660 | cgi_sink *output, |
| 661 | void attribute((unused)) *u) { |
| 662 | cgi_output(output, "%s", config->url); |
| 663 | } |
| 664 | |
| 665 | struct result { |
| 666 | char *track; |
| 667 | const char *sort; |
| 668 | }; |
| 669 | |
| 670 | static int compare_result(const void *a, const void *b) { |
| 671 | const struct result *ra = a, *rb = b; |
| 672 | int c; |
| 673 | |
| 674 | if(!(c = strcmp(ra->sort, rb->sort))) |
| 675 | c = strcmp(ra->track, rb->track); |
| 676 | return c; |
| 677 | } |
| 678 | |
| 679 | static void exp_search(int nargs, |
| 680 | char **args, |
| 681 | cgi_sink *output, |
| 682 | void *u) { |
| 683 | dcgi_state *ds = u, substate; |
| 684 | char **tracks; |
| 685 | const char *q, *context, *part, *template; |
| 686 | int ntracks, n, m; |
| 687 | struct result *r; |
| 688 | |
| 689 | switch(nargs) { |
| 690 | case 2: |
| 691 | part = args[0]; |
| 692 | context = "sort"; |
| 693 | template = args[1]; |
| 694 | break; |
| 695 | case 3: |
| 696 | part = args[0]; |
| 697 | context = args[1]; |
| 698 | template = args[2]; |
| 699 | break; |
| 700 | default: |
| 701 | assert(!"should never happen"); |
| 702 | part = context = template = 0; /* quieten compiler */ |
| 703 | } |
| 704 | if(ds->tracks == 0) { |
| 705 | /* we are the top level, let's get some search results */ |
| 706 | if(!(q = cgi_get("query"))) return; /* no results yet */ |
| 707 | if(disorder_search(ds->g->client, q, &tracks, &ntracks)) return; |
| 708 | if(!ntracks) return; |
| 709 | } else { |
| 710 | tracks = ds->tracks; |
| 711 | ntracks = ds->ntracks; |
| 712 | } |
| 713 | assert(ntracks != 0); |
| 714 | /* sort tracks by the appropriate part */ |
| 715 | r = xmalloc(ntracks * sizeof *r); |
| 716 | for(n = 0; n < ntracks; ++n) { |
| 717 | r[n].track = tracks[n]; |
| 718 | if(disorder_part(ds->g->client, (char **)&r[n].sort, |
| 719 | tracks[n], context, part)) |
| 720 | fatal(0, "disorder_part() failed"); |
| 721 | } |
| 722 | qsort(r, ntracks, sizeof (struct result), compare_result); |
| 723 | /* expand the 2nd arg once for each group. We re-use the passed-in tracks |
| 724 | * array as we know it's guaranteed to be big enough and isn't going to be |
| 725 | * used for anything else any more. */ |
| 726 | memset(&substate, 0, sizeof substate); |
| 727 | substate.g = ds->g; |
| 728 | substate.first = 1; |
| 729 | n = 0; |
| 730 | while(n < ntracks) { |
| 731 | substate.tracks = tracks; |
| 732 | substate.ntracks = 0; |
| 733 | m = n; |
| 734 | while(m < ntracks |
| 735 | && !strcmp(r[m].sort, r[n].sort)) |
| 736 | tracks[substate.ntracks++] = r[m++].track; |
| 737 | substate.last = (m == ntracks); |
| 738 | expandstring(output, template, &substate); |
| 739 | substate.index++; |
| 740 | substate.first = 0; |
| 741 | n = m; |
| 742 | } |
| 743 | assert(substate.last != 0); |
| 744 | } |
| 745 | |
| 746 | static void exp_arg(int attribute((unused)) nargs, |
| 747 | char **args, |
| 748 | cgi_sink *output, |
| 749 | void attribute((unused)) *u) { |
| 750 | const char *v; |
| 751 | |
| 752 | if((v = cgi_get(args[0]))) |
| 753 | cgi_output(output, "%s", v); |
| 754 | } |
| 755 | |
| 756 | static void exp_stats(int attribute((unused)) nargs, |
| 757 | char attribute((unused)) **args, |
| 758 | cgi_sink *output, |
| 759 | void *u) { |
| 760 | dcgi_state *ds = u; |
| 761 | char **v; |
| 762 | |
| 763 | cgi_opentag(output->sink, "pre", "class", "stats", (char *)0); |
| 764 | if(!disorder_stats(ds->g->client, &v, 0)) { |
| 765 | while(*v) |
| 766 | cgi_output(output, "%s\n", *v++); |
| 767 | } |
| 768 | cgi_closetag(output->sink, "pre"); |
| 769 | } |
| 770 | |
| 771 | static void exp_volume(int attribute((unused)) nargs, |
| 772 | char **args, |
| 773 | cgi_sink *output, |
| 774 | void *u) { |
| 775 | dcgi_state *ds = u; |
| 776 | |
| 777 | lookups(ds, DC_VOLUME); |
| 778 | if(!strcmp(args[0], "left")) |
| 779 | cgi_output(output, "%d", ds->g->volume_left); |
| 780 | else |
| 781 | cgi_output(output, "%d", ds->g->volume_right); |
| 782 | } |
| 783 | |
| 784 | static void exp_shell(int attribute((unused)) nargs, |
| 785 | char **args, |
| 786 | cgi_sink *output, |
| 787 | void attribute((unused)) *u) { |
| 788 | int w, p[2], n; |
| 789 | char buffer[4096]; |
| 790 | pid_t pid; |
| 791 | |
| 792 | xpipe(p); |
| 793 | if(!(pid = xfork())) { |
| 794 | exitfn = _exit; |
| 795 | xclose(p[0]); |
| 796 | xdup2(p[1], 1); |
| 797 | xclose(p[1]); |
| 798 | execlp("sh", "sh", "-c", args[0], (char *)0); |
| 799 | fatal(errno, "error executing sh"); |
| 800 | } |
| 801 | xclose(p[1]); |
| 802 | while((n = read(p[0], buffer, sizeof buffer))) { |
| 803 | if(n < 0) { |
| 804 | if(errno == EINTR) continue; |
| 805 | else fatal(errno, "error reading from pipe"); |
| 806 | } |
| 807 | output->sink->write(output->sink, buffer, n); |
| 808 | } |
| 809 | xclose(p[0]); |
| 810 | while((n = waitpid(pid, &w, 0)) < 0 && errno == EINTR) |
| 811 | ; |
| 812 | if(n < 0) fatal(errno, "error calling waitpid"); |
| 813 | if(w) |
| 814 | error(0, "shell command '%s' %s", args[0], wstat(w)); |
| 815 | } |
| 816 | |
| 817 | static inline int str2bool(const char *s) { |
| 818 | return !strcmp(s, "true"); |
| 819 | } |
| 820 | |
| 821 | static inline const char *bool2str(int n) { |
| 822 | return n ? "true" : "false"; |
| 823 | } |
| 824 | |
| 825 | static char *expandarg(const char *arg, dcgi_state *ds) { |
| 826 | struct dynstr d; |
| 827 | cgi_sink output; |
| 828 | |
| 829 | dynstr_init(&d); |
| 830 | output.quote = 0; |
| 831 | output.sink = sink_dynstr(&d); |
| 832 | expandstring(&output, arg, ds); |
| 833 | dynstr_terminate(&d); |
| 834 | return d.vec; |
| 835 | } |
| 836 | |
| 837 | static void exp_prefs(int attribute((unused)) nargs, |
| 838 | char **args, |
| 839 | cgi_sink *output, |
| 840 | void *u) { |
| 841 | dcgi_state *ds = u; |
| 842 | dcgi_state substate; |
| 843 | struct kvp *k; |
| 844 | const char *file = expandarg(args[0], ds); |
| 845 | |
| 846 | memset(&substate, 0, sizeof substate); |
| 847 | substate.g = ds->g; |
| 848 | substate.first = 1; |
| 849 | if(disorder_prefs(ds->g->client, file, &k)) return; |
| 850 | while(k) { |
| 851 | substate.last = !k->next; |
| 852 | substate.pref = k; |
| 853 | expandstring(output, args[1], &substate); |
| 854 | ++substate.index; |
| 855 | k = k->next; |
| 856 | substate.first = 0; |
| 857 | } |
| 858 | } |
| 859 | |
| 860 | static void exp_pref(int attribute((unused)) nargs, |
| 861 | char **args, |
| 862 | cgi_sink *output, |
| 863 | void *u) { |
| 864 | char *value; |
| 865 | dcgi_state *ds = u; |
| 866 | |
| 867 | if(!disorder_get(ds->g->client, args[0], args[1], &value)) |
| 868 | cgi_output(output, "%s", value); |
| 869 | } |
| 870 | |
| 871 | static void exp_if(int nargs, |
| 872 | char **args, |
| 873 | cgi_sink *output, |
| 874 | void *u) { |
| 875 | dcgi_state *ds = u; |
| 876 | int n = str2bool(expandarg(args[0], ds)) ? 1 : 2; |
| 877 | |
| 878 | if(n < nargs) |
| 879 | expandstring(output, args[n], ds); |
| 880 | } |
| 881 | |
| 882 | static void exp_and(int nargs, |
| 883 | char **args, |
| 884 | cgi_sink *output, |
| 885 | void *u) { |
| 886 | dcgi_state *ds = u; |
| 887 | int n, result = 1; |
| 888 | |
| 889 | for(n = 0; n < nargs; ++n) |
| 890 | if(!str2bool(expandarg(args[n], ds))) { |
| 891 | result = 0; |
| 892 | break; |
| 893 | } |
| 894 | sink_printf(output->sink, "%s", bool2str(result)); |
| 895 | } |
| 896 | |
| 897 | static void exp_or(int nargs, |
| 898 | char **args, |
| 899 | cgi_sink *output, |
| 900 | void *u) { |
| 901 | dcgi_state *ds = u; |
| 902 | int n, result = 0; |
| 903 | |
| 904 | for(n = 0; n < nargs; ++n) |
| 905 | if(str2bool(expandarg(args[n], ds))) { |
| 906 | result = 1; |
| 907 | break; |
| 908 | } |
| 909 | sink_printf(output->sink, "%s", bool2str(result)); |
| 910 | } |
| 911 | |
| 912 | static void exp_not(int attribute((unused)) nargs, |
| 913 | char **args, |
| 914 | cgi_sink *output, |
| 915 | void attribute((unused)) *u) { |
| 916 | sink_printf(output->sink, "%s", bool2str(!str2bool(args[0]))); |
| 917 | } |
| 918 | |
| 919 | static void exp_isplaying(int attribute((unused)) nargs, |
| 920 | char attribute((unused)) **args, |
| 921 | cgi_sink *output, |
| 922 | void *u) { |
| 923 | dcgi_state *ds = u; |
| 924 | |
| 925 | lookups(ds, DC_PLAYING); |
| 926 | sink_printf(output->sink, "%s", bool2str(!!ds->g->playing)); |
| 927 | } |
| 928 | |
| 929 | static void exp_isqueue(int attribute((unused)) nargs, |
| 930 | char attribute((unused)) **args, |
| 931 | cgi_sink *output, |
| 932 | void *u) { |
| 933 | dcgi_state *ds = u; |
| 934 | |
| 935 | lookups(ds, DC_QUEUE); |
| 936 | sink_printf(output->sink, "%s", bool2str(!!ds->g->queue)); |
| 937 | } |
| 938 | |
| 939 | static void exp_isrecent(int attribute((unused)) nargs, |
| 940 | char attribute((unused)) **args, |
| 941 | cgi_sink *output, |
| 942 | void *u) { |
| 943 | dcgi_state *ds = u; |
| 944 | |
| 945 | lookups(ds, DC_RECENT); |
| 946 | sink_printf(output->sink, "%s", bool2str(!!ds->g->recent)); |
| 947 | } |
| 948 | |
| 949 | static void exp_isnew(int attribute((unused)) nargs, |
| 950 | char attribute((unused)) **args, |
| 951 | cgi_sink *output, |
| 952 | void *u) { |
| 953 | dcgi_state *ds = u; |
| 954 | |
| 955 | lookups(ds, DC_NEW); |
| 956 | sink_printf(output->sink, "%s", bool2str(!!ds->g->nnew)); |
| 957 | } |
| 958 | |
| 959 | static void exp_id(int attribute((unused)) nargs, |
| 960 | char attribute((unused)) **args, |
| 961 | cgi_sink *output, |
| 962 | void *u) { |
| 963 | dcgi_state *ds = u; |
| 964 | |
| 965 | if(ds->track) |
| 966 | cgi_output(output, "%s", ds->track->id); |
| 967 | } |
| 968 | |
| 969 | static void exp_track(int attribute((unused)) nargs, |
| 970 | char attribute((unused)) **args, |
| 971 | cgi_sink *output, |
| 972 | void *u) { |
| 973 | dcgi_state *ds = u; |
| 974 | |
| 975 | if(ds->track) |
| 976 | cgi_output(output, "%s", ds->track->track); |
| 977 | } |
| 978 | |
| 979 | static void exp_parity(int attribute((unused)) nargs, |
| 980 | char attribute((unused)) **args, |
| 981 | cgi_sink *output, |
| 982 | void *u) { |
| 983 | dcgi_state *ds = u; |
| 984 | |
| 985 | cgi_output(output, "%s", ds->index % 2 ? "odd" : "even"); |
| 986 | } |
| 987 | |
| 988 | static void exp_comment(int attribute((unused)) nargs, |
| 989 | char attribute((unused)) **args, |
| 990 | cgi_sink attribute((unused)) *output, |
| 991 | void attribute((unused)) *u) { |
| 992 | /* do nothing */ |
| 993 | } |
| 994 | |
| 995 | static void exp_prefname(int attribute((unused)) nargs, |
| 996 | char attribute((unused)) **args, |
| 997 | cgi_sink *output, |
| 998 | void *u) { |
| 999 | dcgi_state *ds = u; |
| 1000 | |
| 1001 | if(ds->pref && ds->pref->name) |
| 1002 | cgi_output(output, "%s", ds->pref->name); |
| 1003 | } |
| 1004 | |
| 1005 | static void exp_prefvalue(int attribute((unused)) nargs, |
| 1006 | char attribute((unused)) **args, |
| 1007 | cgi_sink *output, |
| 1008 | void *u) { |
| 1009 | dcgi_state *ds = u; |
| 1010 | |
| 1011 | if(ds->pref && ds->pref->value) |
| 1012 | cgi_output(output, "%s", ds->pref->value); |
| 1013 | } |
| 1014 | |
| 1015 | static void exp_isfiles(int attribute((unused)) nargs, |
| 1016 | char attribute((unused)) **args, |
| 1017 | cgi_sink *output, |
| 1018 | void *u) { |
| 1019 | dcgi_state *ds = u; |
| 1020 | |
| 1021 | lookups(ds, DC_FILES); |
| 1022 | sink_printf(output->sink, "%s", bool2str(!!ds->g->nfiles)); |
| 1023 | } |
| 1024 | |
| 1025 | static void exp_isdirectories(int attribute((unused)) nargs, |
| 1026 | char attribute((unused)) **args, |
| 1027 | cgi_sink *output, |
| 1028 | void *u) { |
| 1029 | dcgi_state *ds = u; |
| 1030 | |
| 1031 | lookups(ds, DC_DIRS); |
| 1032 | sink_printf(output->sink, "%s", bool2str(!!ds->g->ndirs)); |
| 1033 | } |
| 1034 | |
| 1035 | static void exp_choose(int attribute((unused)) nargs, |
| 1036 | char **args, |
| 1037 | cgi_sink *output, |
| 1038 | void *u) { |
| 1039 | dcgi_state *ds = u; |
| 1040 | dcgi_state substate; |
| 1041 | int nfiles, n; |
| 1042 | char **files; |
| 1043 | struct entry *e; |
| 1044 | const char *type, *what = expandarg(args[0], ds); |
| 1045 | |
| 1046 | if(!strcmp(what, "files")) { |
| 1047 | lookups(ds, DC_FILES); |
| 1048 | files = ds->g->files; |
| 1049 | nfiles = ds->g->nfiles; |
| 1050 | type = "track"; |
| 1051 | } else if(!strcmp(what, "directories")) { |
| 1052 | lookups(ds, DC_DIRS); |
| 1053 | files = ds->g->dirs; |
| 1054 | nfiles = ds->g->ndirs; |
| 1055 | type = "dir"; |
| 1056 | } else { |
| 1057 | error(0, "unknown @choose@ argument '%s'", what); |
| 1058 | return; |
| 1059 | } |
| 1060 | e = xmalloc(nfiles * sizeof (struct entry)); |
| 1061 | for(n = 0; n < nfiles; ++n) { |
| 1062 | e[n].path = files[n]; |
| 1063 | e[n].sort = trackname_transform(type, files[n], "sort"); |
| 1064 | e[n].display = trackname_transform(type, files[n], "display"); |
| 1065 | } |
| 1066 | qsort(e, nfiles, sizeof (struct entry), compare_entry); |
| 1067 | memset(&substate, 0, sizeof substate); |
| 1068 | substate.g = ds->g; |
| 1069 | substate.first = 1; |
| 1070 | for(n = 0; n < nfiles; ++n) { |
| 1071 | substate.last = (n == nfiles - 1); |
| 1072 | substate.index = n; |
| 1073 | substate.entry = &e[n]; |
| 1074 | expandstring(output, args[1], &substate); |
| 1075 | substate.first = 0; |
| 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | static void exp_file(int attribute((unused)) nargs, |
| 1080 | char attribute((unused)) **args, |
| 1081 | cgi_sink *output, |
| 1082 | void *u) { |
| 1083 | dcgi_state *ds = u; |
| 1084 | |
| 1085 | if(ds->entry) |
| 1086 | cgi_output(output, "%s", ds->entry->path); |
| 1087 | else if(ds->track) |
| 1088 | cgi_output(output, "%s", ds->track->track); |
| 1089 | else if(ds->tracks) |
| 1090 | cgi_output(output, "%s", ds->tracks[0]); |
| 1091 | } |
| 1092 | |
| 1093 | static void exp_transform(int nargs, |
| 1094 | char **args, |
| 1095 | cgi_sink *output, |
| 1096 | void attribute((unused)) *u) { |
| 1097 | const char *context = nargs > 2 ? args[2] : "display"; |
| 1098 | |
| 1099 | cgi_output(output, "%s", trackname_transform(args[1], args[0], context)); |
| 1100 | } |
| 1101 | |
| 1102 | static void exp_urlquote(int attribute((unused)) nargs, |
| 1103 | char **args, |
| 1104 | cgi_sink *output, |
| 1105 | void attribute((unused)) *u) { |
| 1106 | cgi_output(output, "%s", urlencodestring(args[0])); |
| 1107 | } |
| 1108 | |
| 1109 | static void exp_scratchable(int attribute((unused)) nargs, |
| 1110 | char attribute((unused)) **args, |
| 1111 | cgi_sink *output, |
| 1112 | void attribute((unused)) *u) { |
| 1113 | dcgi_state *ds = u; |
| 1114 | int result; |
| 1115 | |
| 1116 | if(config->restrictions & RESTRICT_SCRATCH) { |
| 1117 | lookups(ds, DC_PLAYING); |
| 1118 | result = (ds->g->playing |
| 1119 | && (!ds->g->playing->submitter |
| 1120 | || !strcmp(ds->g->playing->submitter, |
| 1121 | disorder_user(ds->g->client)))); |
| 1122 | } else |
| 1123 | result = 1; |
| 1124 | sink_printf(output->sink, "%s", bool2str(result)); |
| 1125 | } |
| 1126 | |
| 1127 | static void exp_removable(int attribute((unused)) nargs, |
| 1128 | char attribute((unused)) **args, |
| 1129 | cgi_sink *output, |
| 1130 | void attribute((unused)) *u) { |
| 1131 | dcgi_state *ds = u; |
| 1132 | int result; |
| 1133 | |
| 1134 | if(config->restrictions & RESTRICT_REMOVE) |
| 1135 | result = (ds->track |
| 1136 | && ds->track->submitter |
| 1137 | && !strcmp(ds->track->submitter, |
| 1138 | disorder_user(ds->g->client))); |
| 1139 | else |
| 1140 | result = 1; |
| 1141 | sink_printf(output->sink, "%s", bool2str(result)); |
| 1142 | } |
| 1143 | |
| 1144 | static void exp_navigate(int attribute((unused)) nargs, |
| 1145 | char **args, |
| 1146 | cgi_sink *output, |
| 1147 | void *u) { |
| 1148 | dcgi_state *ds = u; |
| 1149 | dcgi_state substate; |
| 1150 | const char *path = expandarg(args[0], ds); |
| 1151 | const char *ptr; |
| 1152 | int dirlen; |
| 1153 | |
| 1154 | if(*path) { |
| 1155 | memset(&substate, 0, sizeof substate); |
| 1156 | substate.g = ds->g; |
| 1157 | ptr = path + 1; /* skip root */ |
| 1158 | dirlen = 0; |
| 1159 | substate.nav_path = path; |
| 1160 | substate.first = 1; |
| 1161 | while(*ptr) { |
| 1162 | while(*ptr && *ptr != '/') |
| 1163 | ++ptr; |
| 1164 | substate.last = !*ptr; |
| 1165 | substate.nav_len = ptr - path; |
| 1166 | substate.nav_dirlen = dirlen; |
| 1167 | expandstring(output, args[1], &substate); |
| 1168 | dirlen = substate.nav_len; |
| 1169 | if(*ptr) ++ptr; |
| 1170 | substate.first = 0; |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | static void exp_fullname(int attribute((unused)) nargs, |
| 1176 | char attribute((unused)) **args, |
| 1177 | cgi_sink *output, |
| 1178 | void *u) { |
| 1179 | dcgi_state *ds = u; |
| 1180 | cgi_output(output, "%.*s", ds->nav_len, ds->nav_path); |
| 1181 | } |
| 1182 | |
| 1183 | static void exp_basename(int nargs, |
| 1184 | char **args, |
| 1185 | cgi_sink *output, |
| 1186 | void *u) { |
| 1187 | dcgi_state *ds = u; |
| 1188 | const char *s; |
| 1189 | |
| 1190 | if(nargs) { |
| 1191 | if((s = strrchr(args[0], '/'))) ++s; |
| 1192 | else s = args[0]; |
| 1193 | cgi_output(output, "%s", s); |
| 1194 | } else |
| 1195 | cgi_output(output, "%.*s", ds->nav_len - ds->nav_dirlen - 1, |
| 1196 | ds->nav_path + ds->nav_dirlen + 1); |
| 1197 | } |
| 1198 | |
| 1199 | static void exp_dirname(int nargs, |
| 1200 | char **args, |
| 1201 | cgi_sink *output, |
| 1202 | void *u) { |
| 1203 | dcgi_state *ds = u; |
| 1204 | const char *s; |
| 1205 | |
| 1206 | if(nargs) { |
| 1207 | if((s = strrchr(args[0], '/'))) |
| 1208 | cgi_output(output, "%.*s", (int)(s - args[0]), args[0]); |
| 1209 | } else |
| 1210 | cgi_output(output, "%.*s", ds->nav_dirlen, ds->nav_path); |
| 1211 | } |
| 1212 | |
| 1213 | static void exp_eq(int attribute((unused)) nargs, |
| 1214 | char **args, |
| 1215 | cgi_sink *output, |
| 1216 | void attribute((unused)) *u) { |
| 1217 | cgi_output(output, "%s", bool2str(!strcmp(args[0], args[1]))); |
| 1218 | } |
| 1219 | |
| 1220 | static void exp_ne(int attribute((unused)) nargs, |
| 1221 | char **args, |
| 1222 | cgi_sink *output, |
| 1223 | void attribute((unused)) *u) { |
| 1224 | cgi_output(output, "%s", bool2str(strcmp(args[0], args[1]))); |
| 1225 | } |
| 1226 | |
| 1227 | static void exp_enabled(int attribute((unused)) nargs, |
| 1228 | char attribute((unused)) **args, |
| 1229 | cgi_sink *output, |
| 1230 | void *u) { |
| 1231 | dcgi_state *ds = u; |
| 1232 | int enabled = 0; |
| 1233 | |
| 1234 | if(ds->g->client) |
| 1235 | disorder_enabled(ds->g->client, &enabled); |
| 1236 | cgi_output(output, "%s", bool2str(enabled)); |
| 1237 | } |
| 1238 | |
| 1239 | static void exp_random_enabled(int attribute((unused)) nargs, |
| 1240 | char attribute((unused)) **args, |
| 1241 | cgi_sink *output, |
| 1242 | void *u) { |
| 1243 | dcgi_state *ds = u; |
| 1244 | int enabled = 0; |
| 1245 | |
| 1246 | if(ds->g->client) |
| 1247 | disorder_random_enabled(ds->g->client, &enabled); |
| 1248 | cgi_output(output, "%s", bool2str(enabled)); |
| 1249 | } |
| 1250 | |
| 1251 | static void exp_trackstate(int attribute((unused)) nargs, |
| 1252 | char **args, |
| 1253 | cgi_sink *output, |
| 1254 | void *u) { |
| 1255 | dcgi_state *ds = u; |
| 1256 | struct queue_entry *q; |
| 1257 | char *track; |
| 1258 | |
| 1259 | if(disorder_resolve(ds->g->client, &track, args[0])) return; |
| 1260 | lookups(ds, DC_QUEUE|DC_PLAYING); |
| 1261 | if(ds->g->playing && !strcmp(ds->g->playing->track, track)) |
| 1262 | cgi_output(output, "playing"); |
| 1263 | else { |
| 1264 | for(q = ds->g->queue; q && strcmp(q->track, track); q = q->next) |
| 1265 | ; |
| 1266 | if(q) |
| 1267 | cgi_output(output, "queued"); |
| 1268 | } |
| 1269 | } |
| 1270 | |
| 1271 | static void exp_thisurl(int attribute((unused)) nargs, |
| 1272 | char attribute((unused)) **args, |
| 1273 | cgi_sink *output, |
| 1274 | void attribute((unused)) *u) { |
| 1275 | kvp_set(&cgi_args, "nonce", nonce()); /* nonces had better differ! */ |
| 1276 | cgi_output(output, "%s?%s", config->url, kvp_urlencode(cgi_args, 0)); |
| 1277 | } |
| 1278 | |
| 1279 | static void exp_isfirst(int attribute((unused)) nargs, |
| 1280 | char attribute((unused)) **args, |
| 1281 | cgi_sink *output, |
| 1282 | void *u) { |
| 1283 | dcgi_state *ds = u; |
| 1284 | |
| 1285 | sink_printf(output->sink, "%s", bool2str(!!ds->first)); |
| 1286 | } |
| 1287 | |
| 1288 | static void exp_islast(int attribute((unused)) nargs, |
| 1289 | char attribute((unused)) **args, |
| 1290 | cgi_sink *output, |
| 1291 | void *u) { |
| 1292 | dcgi_state *ds = u; |
| 1293 | |
| 1294 | sink_printf(output->sink, "%s", bool2str(!!ds->last)); |
| 1295 | } |
| 1296 | |
| 1297 | static void exp_action(int attribute((unused)) nargs, |
| 1298 | char attribute((unused)) **args, |
| 1299 | cgi_sink *output, |
| 1300 | void attribute((unused)) *u) { |
| 1301 | const char *action = cgi_get("action"), *mgmt; |
| 1302 | |
| 1303 | if(!action) action = "playing"; |
| 1304 | if(!strcmp(action, "playing") |
| 1305 | && (mgmt = cgi_get("mgmt")) |
| 1306 | && !strcmp(mgmt, "true")) |
| 1307 | action = "manage"; |
| 1308 | sink_printf(output->sink, "%s", action); |
| 1309 | } |
| 1310 | |
| 1311 | static void exp_resolve(int attribute((unused)) nargs, |
| 1312 | char **args, |
| 1313 | cgi_sink *output, |
| 1314 | void attribute((unused)) *u) { |
| 1315 | dcgi_state *ds = u; |
| 1316 | char *track; |
| 1317 | |
| 1318 | if(!disorder_resolve(ds->g->client, &track, args[0])) |
| 1319 | sink_printf(output->sink, "%s", track); |
| 1320 | } |
| 1321 | |
| 1322 | static void exp_paused(int attribute((unused)) nargs, |
| 1323 | char attribute((unused)) **args, |
| 1324 | cgi_sink *output, |
| 1325 | void *u) { |
| 1326 | dcgi_state *ds = u; |
| 1327 | int paused = 0; |
| 1328 | |
| 1329 | lookups(ds, DC_PLAYING); |
| 1330 | if(ds->g->playing && ds->g->playing->state == playing_paused) |
| 1331 | paused = 1; |
| 1332 | cgi_output(output, "%s", bool2str(paused)); |
| 1333 | } |
| 1334 | |
| 1335 | static void exp_state(int attribute((unused)) nargs, |
| 1336 | char attribute((unused)) **args, |
| 1337 | cgi_sink *output, |
| 1338 | void *u) { |
| 1339 | dcgi_state *ds = u; |
| 1340 | |
| 1341 | if(ds->track) |
| 1342 | cgi_output(output, "%s", playing_states[ds->track->state]); |
| 1343 | } |
| 1344 | |
| 1345 | static void exp_files(int attribute((unused)) nargs, |
| 1346 | char **args, |
| 1347 | cgi_sink *output, |
| 1348 | void *u) { |
| 1349 | dcgi_state *ds = u; |
| 1350 | dcgi_state substate; |
| 1351 | const char *nfiles_arg, *directory; |
| 1352 | int nfiles, numfile; |
| 1353 | struct kvp *k; |
| 1354 | |
| 1355 | memset(&substate, 0, sizeof substate); |
| 1356 | substate.g = ds->g; |
| 1357 | if((directory = cgi_get("directory"))) { |
| 1358 | /* Prefs for whole directory. */ |
| 1359 | lookups(ds, DC_FILES); |
| 1360 | /* Synthesize args for the file list. */ |
| 1361 | nfiles = ds->g->nfiles; |
| 1362 | for(numfile = 0; numfile < nfiles; ++numfile) { |
| 1363 | k = xmalloc(sizeof *k); |
| 1364 | byte_xasprintf((char **)&k->name, "%d_file", numfile); |
| 1365 | k->value = ds->g->files[numfile]; |
| 1366 | k->next = cgi_args; |
| 1367 | cgi_args = k; |
| 1368 | } |
| 1369 | } else { |
| 1370 | /* Args already present. */ |
| 1371 | if((nfiles_arg = cgi_get("files"))) nfiles = atoi(nfiles_arg); |
| 1372 | else nfiles = 1; |
| 1373 | } |
| 1374 | for(numfile = 0; numfile < nfiles; ++numfile) { |
| 1375 | substate.index = numfile; |
| 1376 | expandstring(output, args[0], &substate); |
| 1377 | } |
| 1378 | } |
| 1379 | |
| 1380 | static void exp_index(int attribute((unused)) nargs, |
| 1381 | char attribute((unused)) **args, |
| 1382 | cgi_sink *output, |
| 1383 | void *u) { |
| 1384 | dcgi_state *ds = u; |
| 1385 | |
| 1386 | cgi_output(output, "%d", ds->index); |
| 1387 | } |
| 1388 | |
| 1389 | static void exp_nfiles(int attribute((unused)) nargs, |
| 1390 | char attribute((unused)) **args, |
| 1391 | cgi_sink *output, |
| 1392 | void *u) { |
| 1393 | dcgi_state *ds = u; |
| 1394 | const char *files_arg; |
| 1395 | |
| 1396 | if(cgi_get("directory")) { |
| 1397 | lookups(ds, DC_FILES); |
| 1398 | cgi_output(output, "%d", ds->g->nfiles); |
| 1399 | } else if((files_arg = cgi_get("files"))) |
| 1400 | cgi_output(output, "%s", files_arg); |
| 1401 | else |
| 1402 | cgi_output(output, "1"); |
| 1403 | } |
| 1404 | |
| 1405 | static const struct cgi_expansion expansions[] = { |
| 1406 | { "#", 0, INT_MAX, EXP_MAGIC, exp_comment }, |
| 1407 | { "action", 0, 0, 0, exp_action }, |
| 1408 | { "and", 0, INT_MAX, EXP_MAGIC, exp_and }, |
| 1409 | { "arg", 1, 1, 0, exp_arg }, |
| 1410 | { "basename", 0, 1, 0, exp_basename }, |
| 1411 | { "choose", 2, 2, EXP_MAGIC, exp_choose }, |
| 1412 | { "dirname", 0, 1, 0, exp_dirname }, |
| 1413 | { "enabled", 0, 0, 0, exp_enabled }, |
| 1414 | { "eq", 2, 2, 0, exp_eq }, |
| 1415 | { "file", 0, 0, 0, exp_file }, |
| 1416 | { "files", 1, 1, EXP_MAGIC, exp_files }, |
| 1417 | { "fullname", 0, 0, 0, exp_fullname }, |
| 1418 | { "id", 0, 0, 0, exp_id }, |
| 1419 | { "if", 2, 3, EXP_MAGIC, exp_if }, |
| 1420 | { "include", 1, 1, 0, exp_include }, |
| 1421 | { "index", 0, 0, 0, exp_index }, |
| 1422 | { "isdirectories", 0, 0, 0, exp_isdirectories }, |
| 1423 | { "isfiles", 0, 0, 0, exp_isfiles }, |
| 1424 | { "isfirst", 0, 0, 0, exp_isfirst }, |
| 1425 | { "islast", 0, 0, 0, exp_islast }, |
| 1426 | { "isnew", 0, 0, 0, exp_isnew }, |
| 1427 | { "isplaying", 0, 0, 0, exp_isplaying }, |
| 1428 | { "isqueue", 0, 0, 0, exp_isqueue }, |
| 1429 | { "isrecent", 0, 0, 0, exp_isrecent }, |
| 1430 | { "label", 1, 1, 0, exp_label }, |
| 1431 | { "length", 0, 0, 0, exp_length }, |
| 1432 | { "navigate", 2, 2, EXP_MAGIC, exp_navigate }, |
| 1433 | { "ne", 2, 2, 0, exp_ne }, |
| 1434 | { "new", 1, 1, EXP_MAGIC, exp_new }, |
| 1435 | { "nfiles", 0, 0, 0, exp_nfiles }, |
| 1436 | { "nonce", 0, 0, 0, exp_nonce }, |
| 1437 | { "not", 1, 1, 0, exp_not }, |
| 1438 | { "or", 0, INT_MAX, EXP_MAGIC, exp_or }, |
| 1439 | { "parity", 0, 0, 0, exp_parity }, |
| 1440 | { "part", 1, 3, 0, exp_part }, |
| 1441 | { "paused", 0, 0, 0, exp_paused }, |
| 1442 | { "playing", 1, 1, EXP_MAGIC, exp_playing }, |
| 1443 | { "pref", 2, 2, 0, exp_pref }, |
| 1444 | { "prefname", 0, 0, 0, exp_prefname }, |
| 1445 | { "prefs", 2, 2, EXP_MAGIC, exp_prefs }, |
| 1446 | { "prefvalue", 0, 0, 0, exp_prefvalue }, |
| 1447 | { "queue", 1, 1, EXP_MAGIC, exp_queue }, |
| 1448 | { "random-enabled", 0, 0, 0, exp_random_enabled }, |
| 1449 | { "recent", 1, 1, EXP_MAGIC, exp_recent }, |
| 1450 | { "removable", 0, 0, 0, exp_removable }, |
| 1451 | { "resolve", 1, 1, 0, exp_resolve }, |
| 1452 | { "scratchable", 0, 0, 0, exp_scratchable }, |
| 1453 | { "search", 2, 3, EXP_MAGIC, exp_search }, |
| 1454 | { "server-version", 0, 0, 0, exp_server_version }, |
| 1455 | { "shell", 1, 1, 0, exp_shell }, |
| 1456 | { "state", 0, 0, 0, exp_state }, |
| 1457 | { "stats", 0, 0, 0, exp_stats }, |
| 1458 | { "thisurl", 0, 0, 0, exp_thisurl }, |
| 1459 | { "track", 0, 0, 0, exp_track }, |
| 1460 | { "trackstate", 1, 1, 0, exp_trackstate }, |
| 1461 | { "transform", 2, 3, 0, exp_transform }, |
| 1462 | { "url", 0, 0, 0, exp_url }, |
| 1463 | { "urlquote", 1, 1, 0, exp_urlquote }, |
| 1464 | { "version", 0, 0, 0, exp_version }, |
| 1465 | { "volume", 1, 1, 0, exp_volume }, |
| 1466 | { "when", 0, 0, 0, exp_when }, |
| 1467 | { "who", 0, 0, 0, exp_who } |
| 1468 | }; |
| 1469 | |
| 1470 | static void expand(cgi_sink *output, |
| 1471 | const char *template, |
| 1472 | dcgi_state *ds) { |
| 1473 | cgi_expand(template, |
| 1474 | expansions, sizeof expansions / sizeof *expansions, |
| 1475 | output, |
| 1476 | ds); |
| 1477 | } |
| 1478 | |
| 1479 | static void expandstring(cgi_sink *output, |
| 1480 | const char *string, |
| 1481 | dcgi_state *ds) { |
| 1482 | cgi_expand_string("", |
| 1483 | string, |
| 1484 | expansions, sizeof expansions / sizeof *expansions, |
| 1485 | output, |
| 1486 | ds); |
| 1487 | } |
| 1488 | |
| 1489 | static void perform_action(cgi_sink *output, dcgi_state *ds, |
| 1490 | const char *action) { |
| 1491 | int n; |
| 1492 | |
| 1493 | if((n = TABLE_FIND(actions, struct action, name, action)) >= 0) |
| 1494 | actions[n].handler(output, ds); |
| 1495 | else { |
| 1496 | cgi_header(output->sink, "Content-Type", "text/html"); |
| 1497 | cgi_body(output->sink); |
| 1498 | expand(output, action, ds); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | void disorder_cgi(cgi_sink *output, dcgi_state *ds) { |
| 1503 | const char *action = cgi_get("action"); |
| 1504 | |
| 1505 | if(!action) action = "playing"; |
| 1506 | perform_action(output, ds, action); |
| 1507 | } |
| 1508 | |
| 1509 | void disorder_cgi_error(cgi_sink *output, dcgi_state *ds, |
| 1510 | const char *msg) { |
| 1511 | cgi_set_option("error", msg); |
| 1512 | perform_action(output, ds, "error"); |
| 1513 | } |
| 1514 | |
| 1515 | /* |
| 1516 | Local Variables: |
| 1517 | c-basic-offset:2 |
| 1518 | comment-column:40 |
| 1519 | fill-column:79 |
| 1520 | End: |
| 1521 | */ |