| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2004-2008 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 | /** @file server/macros-disorder.c |
| 21 | * @brief DisOrder-specific expansions |
| 22 | */ |
| 23 | |
| 24 | #include <config.h> |
| 25 | #include "types.h" |
| 26 | |
| 27 | #include "sink.h" |
| 28 | #include "client.h" |
| 29 | #include "cgi.h" |
| 30 | #include "macros-disorder.h" |
| 31 | #include "lookups.h" |
| 32 | |
| 33 | /** @brief For error template */ |
| 34 | char *error_string; |
| 35 | |
| 36 | /** @brief Locate a track by ID */ |
| 37 | static struct queue_entry *findtrack(const char *id) { |
| 38 | struct queue_entry *q; |
| 39 | |
| 40 | lookups(DC_PLAYING); |
| 41 | if(playing && !strcmp(playing->id, id)) |
| 42 | return playing; |
| 43 | lookups(DC_QUEUE); |
| 44 | for(q = queue; q; q = q->next) |
| 45 | if(!strcmp(q->id, id)) |
| 46 | return q; |
| 47 | lookups(DC_RECENT); |
| 48 | for(q = recent; q; q = q->next) |
| 49 | if(!strcmp(q->id, id)) |
| 50 | return q; |
| 51 | } |
| 52 | |
| 53 | /** @brief Return @p i as a string */ |
| 54 | static const char *make_index(int i) { |
| 55 | char *s; |
| 56 | |
| 57 | byte_xasprintf(&s, "%d", i); |
| 58 | return s; |
| 59 | } |
| 60 | |
| 61 | /* @server-version |
| 62 | * |
| 63 | * Expands to the server's version string, or a (safe to use) error |
| 64 | * value if the server is unavailable or broken. |
| 65 | */ |
| 66 | static int exp_server_version(int attribute((unused)) nargs, |
| 67 | char attribute((unused)) **args, |
| 68 | struct sink *output, |
| 69 | void attribute((unused)) *u) { |
| 70 | const char *v; |
| 71 | |
| 72 | if(client) { |
| 73 | if(disorder_version(client, (char **)&v)) |
| 74 | v = "(cannot get version)"; |
| 75 | } else |
| 76 | v = "(server not running)"; |
| 77 | return sink_write(output, cgi_sgmlquote(v)) < 0 ? -1 : 0; |
| 78 | } |
| 79 | |
| 80 | /* @version |
| 81 | * |
| 82 | * Expands to the local version string. |
| 83 | */ |
| 84 | static int exp_version(int attribute((unused)) nargs, |
| 85 | char attribute((unused)) **args, |
| 86 | struct sink *output, |
| 87 | void attribute((unused)) *u) { |
| 88 | return sink_write(output, |
| 89 | cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0; |
| 90 | } |
| 91 | |
| 92 | /* @url |
| 93 | * |
| 94 | * Expands to the base URL of the web interface. |
| 95 | */ |
| 96 | static int exp_url(int attribute((unused)) nargs, |
| 97 | char attribute((unused)) **args, |
| 98 | struct sink *output, |
| 99 | void attribute((unused)) *u) { |
| 100 | return sink_write(output, |
| 101 | cgi_sgmlquote(config->url)) < 0 ? -1 : 0; |
| 102 | } |
| 103 | |
| 104 | /* @arg{NAME} |
| 105 | * |
| 106 | * Expands to the CGI argument NAME, or the empty string if there is |
| 107 | * no such argument. |
| 108 | */ |
| 109 | static int exp_arg(int attribute((unused)) nargs, |
| 110 | char **args, |
| 111 | struct sink *output, |
| 112 | void attribute((unused)) *u) { |
| 113 | const char *s = cgi_get(args[0]); |
| 114 | if(s) |
| 115 | return sink_write(output, |
| 116 | cgi_sgmlquote(s)) < 0 ? -1 : 0; |
| 117 | else |
| 118 | return 0; |
| 119 | } |
| 120 | |
| 121 | /* @user |
| 122 | * |
| 123 | * Expands to the logged-in username (which might be "guest"), or to |
| 124 | * the empty string if not connected. |
| 125 | */ |
| 126 | static int exp_user(int attribute((unused)) nargs, |
| 127 | char attribute((unused)) **args, |
| 128 | struct sink *output, |
| 129 | void attribute((unused)) *u) { |
| 130 | const char *u; |
| 131 | |
| 132 | if(client && (u = disorder_user(client))) |
| 133 | return sink_write(output, cgi_sgmlquote(u)) < 0 ? -1 : 0; |
| 134 | return 0; |
| 135 | } |
| 136 | |
| 137 | /* @part{TRACK|ID}{PART}{CONTEXT} |
| 138 | * |
| 139 | * Expands to a track name part. |
| 140 | * |
| 141 | * A track may be identified by name or by queue ID. |
| 142 | * |
| 143 | * CONTEXT may be omitted. If it is then 'display' is assumed. |
| 144 | * |
| 145 | * If the CONTEXT is 'short' then the 'display' part is looked up, and the |
| 146 | * result truncated according to the length defined by the short_display |
| 147 | * configuration directive. |
| 148 | */ |
| 149 | static int exp_part(int nargs, |
| 150 | char **args, |
| 151 | struct sink *output, |
| 152 | void attribute((unused)) *u) { |
| 153 | const char *track = args[0], *part = args[1]; |
| 154 | const char *context = nargs > 2 ? args[2] : "display"; |
| 155 | char *s; |
| 156 | |
| 157 | if(track[0] != '/') { |
| 158 | struct queue_entry *q = findtrack(track); |
| 159 | |
| 160 | if(q) |
| 161 | track = q->track; |
| 162 | else |
| 163 | return 0; |
| 164 | } |
| 165 | if(client |
| 166 | && !disorder_get(client, &s, track, |
| 167 | !strcmp(context, "short") ? "display" : context, |
| 168 | part)) |
| 169 | return sink_write(output, cgi_sgmlquote(s)) < 0 ? -1 : 0; |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /* @quote{STRING} |
| 174 | * |
| 175 | * SGML-quotes STRING. Note that most expansion results are already suitable |
| 176 | * quoted, so this expansion is usually not required. |
| 177 | */ |
| 178 | static int exp_quote(int attribute((unused)) nargs, |
| 179 | char **args, |
| 180 | struct sink *output, |
| 181 | void attribute((unused)) *u) { |
| 182 | return sink_write(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0; |
| 183 | } |
| 184 | |
| 185 | /* @who{ID} |
| 186 | * |
| 187 | * Expands to the name of the submitter of track ID, which must be a playing |
| 188 | * track, in the queue, or in the recent list. |
| 189 | */ |
| 190 | static int exp_who(int attribute((unused)) nargs, |
| 191 | char **args, |
| 192 | struct sink *output, |
| 193 | void attribute((unused)) *u) { |
| 194 | struct queue_entry *q = findtrack(args[0]); |
| 195 | |
| 196 | if(q && q->submitter) |
| 197 | return sink_write(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0; |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | /* @when{ID} |
| 202 | * |
| 203 | * Expands to the time a track started or is expected to start. The track must |
| 204 | * be a playing track, in the queue, or in the recent list. |
| 205 | */ |
| 206 | static int exp_when(int attribute((unused)) nargs, |
| 207 | char **args, |
| 208 | struct sink *output, |
| 209 | void attribute((unused)) *u) { |
| 210 | struct queue_entry *q = findtrack(args[0]); |
| 211 | const struct tm *w = 0; |
| 212 | |
| 213 | if(q) { |
| 214 | switch(q->state) { |
| 215 | case playing_isscratch: |
| 216 | case playing_unplayed: |
| 217 | case playing_random: |
| 218 | if(q->expected) |
| 219 | w = localtime(&q->expected); |
| 220 | break; |
| 221 | case playing_failed: |
| 222 | case playing_no_player: |
| 223 | case playing_ok: |
| 224 | case playing_scratched: |
| 225 | case playing_started: |
| 226 | case playing_paused: |
| 227 | case playing_quitting: |
| 228 | if(q->played) |
| 229 | w = localtime(&q->played); |
| 230 | break; |
| 231 | } |
| 232 | if(w) |
| 233 | return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0; |
| 234 | } |
| 235 | return sink_write(output, " ") < 0 ? -1 : 0; |
| 236 | } |
| 237 | |
| 238 | /* @length{ID|TRACK} |
| 239 | * |
| 240 | * Expands to the length of a track, identified by its queue ID or its name. |
| 241 | * If it is the playing track (identified by ID) then the amount played so far |
| 242 | * is included. |
| 243 | */ |
| 244 | static int exp_length(int attribute((unused)) nargs, |
| 245 | char **args, |
| 246 | struct sink *output, |
| 247 | void attribute((unused)) *u) { |
| 248 | struct queue_entry *q; |
| 249 | long length = 0; |
| 250 | const char *name; |
| 251 | |
| 252 | if(args[0][0] == '/') |
| 253 | /* Track identified by name */ |
| 254 | name = args[0]; |
| 255 | else { |
| 256 | /* Track identified by queue ID */ |
| 257 | if(!(q = findtrack(args[0]))) |
| 258 | return 0; |
| 259 | if(q->state == play_start || q->state == playing_paused) |
| 260 | if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0) |
| 261 | return -1; |
| 262 | name = q->track; |
| 263 | } |
| 264 | if(client && disorder_length(client, name, &length)) |
| 265 | return sink_printf(output, "%ld:%02ld", |
| 266 | length / 60, length % 60) < 0 ? -1 : 0; |
| 267 | return sink_write(output, " ") < 0 ? -1 : 0; |
| 268 | } |
| 269 | |
| 270 | /* @removable{ID} |
| 271 | * |
| 272 | * Expands to "true" if track ID is removable (or scratchable, if it is the |
| 273 | * playing track) and "false" otherwise. |
| 274 | */ |
| 275 | static int exp_removable(int attribute((unused)) nargs, |
| 276 | char **args, |
| 277 | struct sink *output, |
| 278 | void attribute((unused)) *u) { |
| 279 | struct queue_entry *q = findtrack(args[0]); |
| 280 | /* TODO would be better to reject recent */ |
| 281 | |
| 282 | if(!q || !client) |
| 283 | return mx_bool_result(output, 0); |
| 284 | lookup(DC_RIGHTS); |
| 285 | return mx_bool_result(output, |
| 286 | (q == playing ? right_scratchable : right_removable) |
| 287 | (rights, disorder_user(client), q)); |
| 288 | } |
| 289 | |
| 290 | /* @movable{ID} |
| 291 | * |
| 292 | * Expands to "true" if track ID is movable and "false" otherwise. |
| 293 | */ |
| 294 | static int exp_movable(int attribute((unused)) nargs, |
| 295 | char **args, |
| 296 | struct sink *output, |
| 297 | void attribute((unused)) *u) { |
| 298 | struct queue_entry *q = findtrack(args[0]); |
| 299 | /* TODO would be better to recent playing/recent */ |
| 300 | |
| 301 | if(!q || !client) |
| 302 | return mx_bool_result(output, 0); |
| 303 | lookup(DC_RIGHTS); |
| 304 | return mx_bool_result(output, |
| 305 | right_movable(rights, disorder_user(client), q)); |
| 306 | } |
| 307 | |
| 308 | /* @playing{TEMPLATE} |
| 309 | * |
| 310 | * Expands to TEMPLATE, with: |
| 311 | * - @id@ expanded to the queue ID of the playing track |
| 312 | * - @track@ expanded to its UNQUOTED name |
| 313 | * |
| 314 | * If no track is playing expands to nothing. |
| 315 | * |
| 316 | * TEMPLATE is optional. If it is left out then instead expands to the queue |
| 317 | * ID of the playing track. |
| 318 | */ |
| 319 | static int exp_playing(int nargs, |
| 320 | const struct mx_node **args, |
| 321 | struct sink *output, |
| 322 | void attribute((unused)) *u) { |
| 323 | lookup(DC_PLAYING); |
| 324 | if(!playing) |
| 325 | return 0; |
| 326 | if(!nargs) |
| 327 | return sink_write(output, playing->id) < 0 ? -1 : 0; |
| 328 | return mx_rewritel(args[0], |
| 329 | "id", playing->id, |
| 330 | "track", playing->track, |
| 331 | (char *)0); |
| 332 | } |
| 333 | |
| 334 | /* @queue{TEMPLATE} |
| 335 | * |
| 336 | * For each track in the queue, expands TEMPLATE with the following expansions: |
| 337 | * - @id@ to the queue ID of the track |
| 338 | * - @track@ to the UNQUOTED track name |
| 339 | * - @index@ to the track number from 0 |
| 340 | * - @parity@ to "even" or "odd" alternately |
| 341 | * - @first@ to "true" on the first track and "false" otherwise |
| 342 | * - @last@ to "true" on the last track and "false" otherwise |
| 343 | */ |
| 344 | static int exp_queue(int attribute((unused)) nargs, |
| 345 | const struct mx_node **args, |
| 346 | struct sink *output, |
| 347 | void attribute((unused)) *u) { |
| 348 | struct queue_entry *q; |
| 349 | int rc, i; |
| 350 | |
| 351 | lookup(DC_QUEUE); |
| 352 | for(q = queue, i = 0; q; q = q->next, ++i) |
| 353 | if((rc = mx_rewritel(args[0], |
| 354 | "id", q->id, |
| 355 | "track", q->track, |
| 356 | "index", make_index(i), |
| 357 | "parity", i % 2 ? "odd" : "even", |
| 358 | "first", q == queue ? "true" : "false", |
| 359 | "last", q->next ? "false" : "true", |
| 360 | (char *)0))) |
| 361 | return rc; |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | /* @recent{TEMPLATE} |
| 366 | * |
| 367 | * For each track in the recently played list, expands TEMPLATE with the |
| 368 | * following expansions: |
| 369 | * - @id@ to the queue ID of the track |
| 370 | * - @track@ to the UNQUOTED track name |
| 371 | * - @index@ to the track number from 0 |
| 372 | * - @parity@ to "even" or "odd" alternately |
| 373 | * - @first@ to "true" on the first track and "false" otherwise |
| 374 | * - @last@ to "true" on the last track and "false" otherwise |
| 375 | */ |
| 376 | static int exp_recent(int attribute((unused)) nargs, |
| 377 | const struct mx_node **args, |
| 378 | struct sink *output, |
| 379 | void attribute((unused)) *u) { |
| 380 | struct queue_entry *q; |
| 381 | int rc, i; |
| 382 | |
| 383 | lookup(DC_RECENT); |
| 384 | for(q = recent, i = 0; q; q = q->next, ++i) |
| 385 | if((rc = mx_rewritel(args[0], |
| 386 | "id", q->id, |
| 387 | "track", q->track, |
| 388 | "index", make_index(i), |
| 389 | "parity", i % 2 ? "odd" : "even", |
| 390 | "first", q == recent ? "true" : "false", |
| 391 | "last", q->next ? "false" : "true", |
| 392 | (char *)0))) |
| 393 | return rc; |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | /* @new{TEMPLATE} |
| 398 | * |
| 399 | * For each track in the newly added list, expands TEMPLATE wit the following |
| 400 | * expansions: |
| 401 | * - @track@ to the UNQUOTED track name |
| 402 | * - @index@ to the track number from 0 |
| 403 | * - @parity@ to "even" or "odd" alternately |
| 404 | * - @first@ to "true" on the first track and "false" otherwise |
| 405 | * - @last@ to "true" on the last track and "false" otherwise |
| 406 | * |
| 407 | * Note that unlike @playing@, @queue@ and @recent@ which are otherwise |
| 408 | * superficially similar, there is no @id@ sub-expansion here. |
| 409 | */ |
| 410 | static int exp_new(int attribute((unused)) nargs, |
| 411 | const struct mx_node **args, |
| 412 | struct sink *output, |
| 413 | void attribute((unused)) *u) { |
| 414 | struct queue_entry *q; |
| 415 | int rc, i; |
| 416 | |
| 417 | lookup(DC_NEW); |
| 418 | /* TODO perhaps we should generate an ID value for tracks in the new list */ |
| 419 | for(i = 0; i < nnew; ++i) |
| 420 | if((rc = mx_rewritel(args[0], |
| 421 | "track", new[i], |
| 422 | "index", make_index(i), |
| 423 | "parity", i % 2 ? "odd" : "even", |
| 424 | "first", i == 0 ? "true" : "false", |
| 425 | "last", i == nnew - 1 ? "false" : "true", |
| 426 | (char *)0))) |
| 427 | return rc; |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | /* @volume{CHANNEL} |
| 432 | * |
| 433 | * Expands to the volume in a given channel. CHANNEL must be "left" or |
| 434 | * "right". |
| 435 | */ |
| 436 | static int exp_volume(int attribute((unused)) nargs, |
| 437 | char **args, |
| 438 | struct sink *output, |
| 439 | void attribute((unused)) *u) { |
| 440 | lookup(DC_VOLUME); |
| 441 | return sink_write(output, "%d", |
| 442 | !strcmp(args[0], "left") |
| 443 | ? volume_left : volume_right) < 0 ? -1 : 0; |
| 444 | } |
| 445 | |
| 446 | /* @isplaying |
| 447 | * |
| 448 | * Expands to "true" if there is a playing track, otherwise "false". |
| 449 | */ |
| 450 | static int exp_isplaying(int attribute((unused)) nargs, |
| 451 | char attribute((unused)) **args, |
| 452 | struct sink *output, |
| 453 | void attribute((unused)) *u) { |
| 454 | lookup(DC_PLAYING); |
| 455 | return mx_bool_result(output, !!playing); |
| 456 | } |
| 457 | |
| 458 | /* @isqueue |
| 459 | * |
| 460 | * Expands to "true" if there the queue is nonempty, otherwise "false". |
| 461 | */ |
| 462 | static int exp_isqueue(int attribute((unused)) nargs, |
| 463 | char attribute((unused)) **args, |
| 464 | struct sink *output, |
| 465 | void attribute((unused)) *u) { |
| 466 | lookup(DC_QUEUE); |
| 467 | return mx_bool_result(output, !!queue); |
| 468 | } |
| 469 | |
| 470 | /* @isrecent@ |
| 471 | * |
| 472 | * Expands to "true" if there the recently played list is nonempty, otherwise |
| 473 | * "false". |
| 474 | */ |
| 475 | static int exp_isrecent(int attribute((unused)) nargs, |
| 476 | char attribute((unused)) **args, |
| 477 | struct sink *output, |
| 478 | void attribute((unused)) *u) { |
| 479 | lookup(DC_RECENT); |
| 480 | return mx_bool_result(output, !!recent); |
| 481 | } |
| 482 | |
| 483 | /* @isnew |
| 484 | * |
| 485 | * Expands to "true" if there the newly added track list is nonempty, otherwise |
| 486 | * "false". |
| 487 | */ |
| 488 | static int exp_isnew(int attribute((unused)) nargs, |
| 489 | char attribute((unused)) **args, |
| 490 | struct sink *output, |
| 491 | void attribute((unused)) *u) { |
| 492 | lookup(DC_NEW); |
| 493 | return mx_bool_result(output, !!nnew); |
| 494 | } |
| 495 | |
| 496 | /* @pref{TRACK}{KEY} |
| 497 | * |
| 498 | * Expands to a track preference. |
| 499 | */ |
| 500 | static int exp_pref(int attribute((unused)) nargs, |
| 501 | char **args, |
| 502 | struct sink *output, |
| 503 | void attribute((unused)) *u) { |
| 504 | char *value; |
| 505 | |
| 506 | if(client && !disorder_get(client, args[0], args[1], &value)) |
| 507 | return sink_write(output, cgi_sgmlquote(value)) < 0 ? -1 : 0; |
| 508 | } |
| 509 | |
| 510 | /* @prefs{TRACK}{TEMPLATE} |
| 511 | * |
| 512 | * For each track preference of track TRACK, expands TEMPLATE with the |
| 513 | * following expansions: |
| 514 | * - @name@ to the UNQUOTED preference name |
| 515 | * - @index@ to the preference number from 0 |
| 516 | * - @value@ to the UNQUOTED preference value |
| 517 | * - @parity@ to "even" or "odd" alternately |
| 518 | * - @first@ to "true" on the first preference and "false" otherwise |
| 519 | * - @last@ to "true" on the last preference and "false" otherwise |
| 520 | * |
| 521 | * Use @quote@ to quote preference names and values where necessary; see below. |
| 522 | */ |
| 523 | static int exp_prefs(int attribute((unused)) nargs, |
| 524 | const struct mx_node **args, |
| 525 | struct sink *output, |
| 526 | void attribute((unused)) *u) { |
| 527 | int rc, i; |
| 528 | struct kvp *k, *head; |
| 529 | char *track; |
| 530 | |
| 531 | if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)"))) |
| 532 | return rc; |
| 533 | if(!client || disorder_prefs(client, track, &head)) |
| 534 | return 0; |
| 535 | for(k = head, i = 0; k; k = k->next, ++i) |
| 536 | if((rc = mx_rewritel(args[1], |
| 537 | "index", make_index(i), |
| 538 | "parity", i % 2 ? "odd" : "even", |
| 539 | "name", k->name, |
| 540 | "value", k->value, |
| 541 | "first", k == head ? "true" : "false", |
| 542 | "last", k->next ? "false" : "true", |
| 543 | (char *)0))) |
| 544 | return rc; |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | /* @transform{TRACK}{TYPE}{CONTEXT} |
| 549 | * |
| 550 | * Transforms a track name (if TYPE is "track") or directory name (if type is |
| 551 | * "dir"). CONTEXT should be the context, if it is left out then "display" is |
| 552 | * assumed. |
| 553 | */ |
| 554 | static int exp_transform(int nargs, |
| 555 | char **args, |
| 556 | struct sink *output, |
| 557 | void attribute((unused)) *u) { |
| 558 | const char *t = trackname_transform(args[1], args[0], |
| 559 | (nargs > 2 ? args[2] : "display"))); |
| 560 | return sink_write(output, cgi_sgmlquote(t)) < 0 ? -1 : 0; |
| 561 | } |
| 562 | |
| 563 | /* @enabled@ |
| 564 | * |
| 565 | * Expands to "true" if playing is enabled, otherwise "false". |
| 566 | */ |
| 567 | static int exp_enabled(int attribute((unused)) nargs, |
| 568 | char attribute((unused)) **args, |
| 569 | struct sink *output, |
| 570 | void attribute((unused)) *u) { |
| 571 | int enabled = 0; |
| 572 | |
| 573 | if(client) |
| 574 | disorder_enabled(client, &enabled); |
| 575 | return mx_bool_result(output, enabled); |
| 576 | } |
| 577 | |
| 578 | /* @random-enabled |
| 579 | * |
| 580 | * Expands to "true" if random play is enabled, otherwise "false". |
| 581 | */ |
| 582 | static int exp_enabled(int attribute((unused)) nargs, |
| 583 | char attribute((unused)) **args, |
| 584 | struct sink *output, |
| 585 | void attribute((unused)) *u) { |
| 586 | int enabled = 0; |
| 587 | |
| 588 | if(client) |
| 589 | disorder_random_enabled(client, &enabled); |
| 590 | return mx_bool_result(output, enabled); |
| 591 | } |
| 592 | |
| 593 | /* @trackstate{TRACK |
| 594 | * |
| 595 | * Expands to "playing" if TRACK is currently playing, or "queue" if it is in |
| 596 | * the queue, otherwise to nothing. |
| 597 | */ |
| 598 | static int exp_trackstate(int attribute((unused)) nargs, |
| 599 | char **args, |
| 600 | struct sink *output, |
| 601 | void attribute((unused)) *u) { |
| 602 | char *track; |
| 603 | struct queue_entry *q; |
| 604 | |
| 605 | if(!client) |
| 606 | return 0; |
| 607 | if(disorder_resolve(client, &track, args[0])) |
| 608 | return 0; |
| 609 | lookup(DC_PLAYING); |
| 610 | if(playing && !strcmp(track, playing->track)) |
| 611 | return sink_write(output, "playing") < 0 ? -1 : 0; |
| 612 | lookup(DC_QUEUE); |
| 613 | for(q = queue; q; q = q->next) |
| 614 | if(!strcmp(track, q->track)) |
| 615 | return sink_write(output, "queued") < 0 ? -1 : 0; |
| 616 | return 0; |
| 617 | } |
| 618 | |
| 619 | /* @thisurl |
| 620 | * |
| 621 | * Expands to an UNQUOTED URL which points back to the current page. (NB it |
| 622 | * might not be byte for byte identical - for instance, CGI arguments might be |
| 623 | * re-ordered.) |
| 624 | */ |
| 625 | static int exp_thisurl(int attribute((unused)) nargs, |
| 626 | char attribute((unused)) **args, |
| 627 | struct sink *output, |
| 628 | void attribute((unused)) *u) { |
| 629 | return cgi_thisurl(config->url); |
| 630 | } |
| 631 | |
| 632 | /* @resolve{TRACK} |
| 633 | * |
| 634 | * Expands to an UNQUOTED name for the TRACK that is not an alias, or to |
| 635 | * nothing if it is not a valid track. |
| 636 | */ |
| 637 | static int exp_resolve(int attribute((unused)) nargs, |
| 638 | char **args, |
| 639 | struct sink *output, |
| 640 | void attribute((unused)) *u) { |
| 641 | char *r; |
| 642 | |
| 643 | if(client && !disorder_resolve(client, &r, args[0])) |
| 644 | return sink_write(output, r) < 0 ? -1 : 0; |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | /* @paused |
| 649 | * |
| 650 | * Expands to "true" if the playing track is paused, to "false" if it is |
| 651 | * playing (or if there is no playing track at all). |
| 652 | */ |
| 653 | static int exp_paused(int attribute((unused)) nargs, |
| 654 | char attribute((unused)) **args, |
| 655 | struct sink *output, |
| 656 | void attribute((unused)) *u) { |
| 657 | lookup(DC_PLAYING); |
| 658 | return mx_bool_result(output, playing && playing->state == playing_paused); |
| 659 | } |
| 660 | |
| 661 | /* @state{ID}@ |
| 662 | * |
| 663 | * Expands to the current state of track ID. |
| 664 | */ |
| 665 | static int exp_state(int attribute((unused)) nargs, |
| 666 | char **args, |
| 667 | struct sink *output, |
| 668 | void attribute((unused)) *u) { |
| 669 | struct queue_entry *q = findtrack(args[0]); |
| 670 | |
| 671 | if(q) |
| 672 | return sink_write(output, playing_states[q->state]) < 0 ? -1 : 0; |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@ |
| 677 | * |
| 678 | * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to |
| 679 | * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out. |
| 680 | * |
| 681 | * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if |
| 682 | * the user has the right and "false" otherwise. |
| 683 | * |
| 684 | * If there is no connection to the server then expands to nothing (in all |
| 685 | * cases). |
| 686 | */ |
| 687 | static int exp_right(int nargs, |
| 688 | const struct mx_node **args, |
| 689 | struct sink *output, |
| 690 | void attribute((unused)) *u) { |
| 691 | char *right; |
| 692 | rights_type r; |
| 693 | |
| 694 | if(!client) |
| 695 | return 0; |
| 696 | lookup(DC_RIGHTS); |
| 697 | if((rc = mx_expandstr(args[0], &rightname, u, "argument #0 (RIGHT)"))) |
| 698 | return rc; |
| 699 | if(parse_rights(right, &r, 1/*report*/)) |
| 700 | return 0; |
| 701 | /* Single-argument form */ |
| 702 | if(nargs == 1) |
| 703 | return mx_bool_result(output, !!(r & rights)); |
| 704 | /* Multiple argument form */ |
| 705 | if(r & rights) |
| 706 | return mx_expandl(args[1], (char *)0); |
| 707 | if(nargs == 3) |
| 708 | return mx_expandl(args[2], (char *)0); |
| 709 | return 0; |
| 710 | } |
| 711 | |
| 712 | /* @userinfo{PROPERTY} |
| 713 | * |
| 714 | * Expands to the named property of the current user. |
| 715 | */ |
| 716 | static int exp_userinfo(int attribute((unused)) nargs, |
| 717 | char **args, |
| 718 | struct sink *output, |
| 719 | void attribute((unused)) *u) { |
| 720 | char *v; |
| 721 | |
| 722 | if(client && !disorder_userinfo(client, disorder_user(client), args[0], &v)) |
| 723 | return sink_write(output, v) < 0 ? -1 : 0; |
| 724 | return 0; |
| 725 | } |
| 726 | |
| 727 | /* @error |
| 728 | * |
| 729 | * Expands to the latest error string. |
| 730 | */ |
| 731 | static int exp_error(int attribute((unused)) nargs, |
| 732 | char attribute((unused)) **args, |
| 733 | struct sink *output, |
| 734 | void attribute((unused)) *u) { |
| 735 | return sink_write(output, cgi_sgmlquote(error_string)) < 0 ? -1 : 0; |
| 736 | } |
| 737 | |
| 738 | /* @userinfo{PROPERTY}@ |
| 739 | * |
| 740 | /** @brief Register DisOrder-specific expansions */ |
| 741 | void register_disorder_expansions(void) { |
| 742 | mx_register(exp_arg, 1, 1, "arg"); |
| 743 | mx_register(exp_enabled, 0, 0, "enabled"); |
| 744 | mx_register(exp_error, 0, 0, "error"); |
| 745 | mx_register(exp_isnew, 0, 0, "isnew"); |
| 746 | mx_register(exp_isplaying, 0, 0, "isplaying"); |
| 747 | mx_register(exp_isqueue, 0, 0, "isplaying"); |
| 748 | mx_register(exp_isrecent, 0, 0, "isrecent"); |
| 749 | mx_register(exp_length, 1, 1, "length"); |
| 750 | mx_register(exp_movable, 1, 1, "movable"); |
| 751 | mx_register(exp_part, 2, 3, "part"); |
| 752 | mx_register(exp_pref, 2, 2, "pref"); |
| 753 | mx_register(exp_quote, 1, 1, "quote"); |
| 754 | mx_register(exp_random_enabled, 0, 0, "random-enabled"); |
| 755 | mx_register(exp_removable, 1, 1, "removable"); |
| 756 | mx_register(exp_resolve, 1, 1, "resolve"); |
| 757 | mx_register(exp_right, 1, 3, "right"); |
| 758 | mx_register(exp_server_version, 0, 0, "server-version"); |
| 759 | mx_register(exp_state, 1, 1, "state"); |
| 760 | mx_register(exp_thisurl, 0, 0, "thisurl"); |
| 761 | mx_register(exp_trackstate, 1, 1, "trackstate"); |
| 762 | mx_register(exp_transform, 2, 3, "transform"); |
| 763 | mx_register(exp_url, 0, 0, "url"); |
| 764 | mx_register(exp_user, 0, 0, "user"); |
| 765 | mx_register(exp_userinfo, 1, 1, "userinfo"); |
| 766 | mx_register(exp_version, 0, 0, "version"); |
| 767 | mx_register(exp_volume, 1, 1, "volume"); |
| 768 | mx_register(exp_when, 1, 1, "when"); |
| 769 | mx_register(exp_who, 1, 1, "who"); |
| 770 | mx_register_magic(exp_new, 1, 1, "new"); |
| 771 | mx_register_magic(exp_playing, 0, 1, "playing"); |
| 772 | mx_register_magic(exp_prefs, 2, 2, "prefs"); |
| 773 | mx_register_magic(exp_queue, 1, 1, "queue"); |
| 774 | mx_register_magic(exp_recent, 1, 1, "recent"); |
| 775 | } |
| 776 | |
| 777 | void disorder_macros_reset(void) { |
| 778 | /* Junk the old connection if there is one */ |
| 779 | if(client) |
| 780 | disorder_close(client); |
| 781 | /* Create a new connection */ |
| 782 | client = disorder_new(0); |
| 783 | /* Forget everything we knew */ |
| 784 | flags = 0; |
| 785 | } |
| 786 | |
| 787 | /* |
| 788 | Local Variables: |
| 789 | c-basic-offset:2 |
| 790 | comment-column:40 |
| 791 | fill-column:79 |
| 792 | indent-tabs-mode:nil |
| 793 | End: |
| 794 | */ |