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