chiark / gitweb /
Add a new 'origin' field to queue entries. This records the origin of
[disorder] / cgi / macros-disorder.c
CommitLineData
9faa7a88
RK
1/*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
4 *
e7eb3a27 5 * This program is free software: you can redistribute it and/or modify
9faa7a88 6 * it under the terms of the GNU General Public License as published by
e7eb3a27 7 * the Free Software Foundation, either version 3 of the License, or
9faa7a88
RK
8 * (at your option) any later version.
9 *
e7eb3a27
RK
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
9faa7a88 15 * You should have received a copy of the GNU General Public License
e7eb3a27 16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
9faa7a88 17 */
59cf25c4 18/** @file cgi/macros-disorder.c
9faa7a88 19 * @brief DisOrder-specific expansions
59cf25c4
RK
20 *
21 * See @ref lib/macros-builtin.c for docstring syntax.
9faa7a88
RK
22 */
23
1e97629d 24#include "disorder-cgi.h"
9faa7a88 25
bca4e2b7 26/** @brief For error template */
e7ce7665 27const char *dcgi_error_string;
bca4e2b7 28
343d9c2b 29/** @brief For login template */
e7ce7665 30const char *dcgi_status_string;
343d9c2b 31
9faa7a88
RK
32/** @brief Return @p i as a string */
33static const char *make_index(int i) {
34 char *s;
35
36 byte_xasprintf(&s, "%d", i);
37 return s;
38}
39
59cf25c4 40/*$ @server-version
9faa7a88
RK
41 *
42 * Expands to the server's version string, or a (safe to use) error
43 * value if the server is unavailable or broken.
44 */
45static int exp_server_version(int attribute((unused)) nargs,
46 char attribute((unused)) **args,
47 struct sink *output,
48 void attribute((unused)) *u) {
49 const char *v;
50
1e97629d
RK
51 if(dcgi_client) {
52 if(disorder_version(dcgi_client, (char **)&v))
9faa7a88
RK
53 v = "(cannot get version)";
54 } else
55 v = "(server not running)";
71634563 56 return sink_writes(output, cgi_sgmlquote(v)) < 0 ? -1 : 0;
9faa7a88
RK
57}
58
59cf25c4 59/*$ @version
9faa7a88
RK
60 *
61 * Expands to the local version string.
62 */
63static int exp_version(int attribute((unused)) nargs,
64 char attribute((unused)) **args,
65 struct sink *output,
66 void attribute((unused)) *u) {
71634563
RK
67 return sink_writes(output,
68 cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0;
9faa7a88
RK
69}
70
59cf25c4 71/*$ @url
9faa7a88
RK
72 *
73 * Expands to the base URL of the web interface.
74 */
75static int exp_url(int attribute((unused)) nargs,
76 char attribute((unused)) **args,
77 struct sink *output,
78 void attribute((unused)) *u) {
71634563
RK
79 return sink_writes(output,
80 cgi_sgmlquote(config->url)) < 0 ? -1 : 0;
9faa7a88
RK
81}
82
59cf25c4 83/*$ @arg{NAME}
9faa7a88 84 *
48e472fa
RK
85 * Expands to the UNQUOTED form of CGI argument NAME, or the empty string if
86 * there is no such argument. Use @argq for a quick way to quote the argument.
9faa7a88
RK
87 */
88static int exp_arg(int attribute((unused)) nargs,
89 char **args,
90 struct sink *output,
91 void attribute((unused)) *u) {
92 const char *s = cgi_get(args[0]);
2257512d 93
9faa7a88 94 if(s)
48e472fa
RK
95 return sink_writes(output, s) < 0 ? -1 : 0;
96 else
97 return 0;
98}
99
59cf25c4 100/*$ @argq{NAME}
48e472fa
RK
101 *
102 * Expands to the (quoted) form of CGI argument NAME, or the empty string if
103 * there is no such argument. Use @arg for the unquoted argument.
104 */
105static int exp_argq(int attribute((unused)) nargs,
106 char **args,
107 struct sink *output,
108 void attribute((unused)) *u) {
109 const char *s = cgi_get(args[0]);
110
111 if(s)
112 return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
9faa7a88
RK
113 else
114 return 0;
115}
116
59cf25c4 117/*$ @user
9faa7a88
RK
118 *
119 * Expands to the logged-in username (which might be "guest"), or to
120 * the empty string if not connected.
121 */
122static int exp_user(int attribute((unused)) nargs,
123 char attribute((unused)) **args,
124 struct sink *output,
125 void attribute((unused)) *u) {
71634563 126 const char *user;
9faa7a88 127
1e97629d 128 if(dcgi_client && (user = disorder_user(dcgi_client)))
71634563 129 return sink_writes(output, cgi_sgmlquote(user)) < 0 ? -1 : 0;
9faa7a88
RK
130 return 0;
131}
132
59cf25c4 133/*$ @part{TRACK|ID}{PART}{CONTEXT}
9faa7a88
RK
134 *
135 * Expands to a track name part.
136 *
137 * A track may be identified by name or by queue ID.
138 *
139 * CONTEXT may be omitted. If it is then 'display' is assumed.
140 *
141 * If the CONTEXT is 'short' then the 'display' part is looked up, and the
142 * result truncated according to the length defined by the short_display
143 * configuration directive.
144 */
145static int exp_part(int nargs,
146 char **args,
147 struct sink *output,
148 void attribute((unused)) *u) {
149 const char *track = args[0], *part = args[1];
150 const char *context = nargs > 2 ? args[2] : "display";
2647daf5 151 const char *s;
9faa7a88
RK
152
153 if(track[0] != '/') {
a2c4ad5f 154 struct queue_entry *q = dcgi_findtrack(track);
9faa7a88
RK
155
156 if(q)
157 track = q->track;
158 else
159 return 0;
160 }
1e97629d 161 if(dcgi_client
2647daf5 162 && !disorder_part(dcgi_client, (char **)&s,
71634563
RK
163 track,
164 !strcmp(context, "short") ? "display" : context,
2647daf5
RK
165 part)) {
166 if(!strcmp(context, "short"))
167 s = truncate_for_display(s, config->short_display);
71634563 168 return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
2647daf5 169 }
9faa7a88
RK
170 return 0;
171}
172
59cf25c4 173/*$ @quote{STRING}
9faa7a88
RK
174 *
175 * SGML-quotes STRING. Note that most expansion results are already suitable
176 * quoted, so this expansion is usually not required.
177 */
178static int exp_quote(int attribute((unused)) nargs,
179 char **args,
180 struct sink *output,
181 void attribute((unused)) *u) {
71634563 182 return sink_writes(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0;
9faa7a88
RK
183}
184
59cf25c4 185/*$ @who{ID}
9faa7a88
RK
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 */
190static int exp_who(int attribute((unused)) nargs,
191 char **args,
192 struct sink *output,
193 void attribute((unused)) *u) {
a2c4ad5f 194 struct queue_entry *q = dcgi_findtrack(args[0]);
9faa7a88
RK
195
196 if(q && q->submitter)
71634563 197 return sink_writes(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0;
9faa7a88
RK
198 return 0;
199}
200
59cf25c4 201/*$ @when{ID}
9faa7a88
RK
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 */
206static int exp_when(int attribute((unused)) nargs,
207 char **args,
208 struct sink *output,
209 void attribute((unused)) *u) {
a2c4ad5f 210 struct queue_entry *q = dcgi_findtrack(args[0]);
9faa7a88
RK
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 }
71634563 235 return sink_writes(output, "&nbsp;") < 0 ? -1 : 0;
9faa7a88
RK
236}
237
59cf25c4 238/*$ @length{ID|TRACK}
9faa7a88
RK
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 */
244static 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 */
a2c4ad5f 257 if(!(q = dcgi_findtrack(args[0])))
9faa7a88 258 return 0;
71634563 259 if(q->state == playing_started || q->state == playing_paused)
9faa7a88
RK
260 if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0)
261 return -1;
262 name = q->track;
263 }
8a82274b 264 if(dcgi_client && !disorder_length(dcgi_client, name, &length))
9faa7a88
RK
265 return sink_printf(output, "%ld:%02ld",
266 length / 60, length % 60) < 0 ? -1 : 0;
71634563 267 return sink_writes(output, "&nbsp;") < 0 ? -1 : 0;
9faa7a88
RK
268}
269
59cf25c4 270/*$ @removable{ID}
9faa7a88
RK
271 *
272 * Expands to "true" if track ID is removable (or scratchable, if it is the
273 * playing track) and "false" otherwise.
274 */
275static int exp_removable(int attribute((unused)) nargs,
276 char **args,
277 struct sink *output,
278 void attribute((unused)) *u) {
a2c4ad5f 279 struct queue_entry *q = dcgi_findtrack(args[0]);
9faa7a88
RK
280 /* TODO would be better to reject recent */
281
1e97629d 282 if(!q || !dcgi_client)
9faa7a88 283 return mx_bool_result(output, 0);
1e97629d 284 dcgi_lookup(DCGI_RIGHTS);
9faa7a88 285 return mx_bool_result(output,
1e97629d
RK
286 (q == dcgi_playing ? right_scratchable : right_removable)
287 (dcgi_rights, disorder_user(dcgi_client), q));
9faa7a88
RK
288}
289
59cf25c4 290/*$ @movable{ID}{DIR}
9faa7a88
RK
291 *
292 * Expands to "true" if track ID is movable and "false" otherwise.
af106f2d
RK
293 *
294 * DIR (which is optional) should be a non-zero integer. If it is negative
295 * then the intended move is down (later in the queue), if it is positive then
296 * the intended move is up (earlier in the queue). The first track is not
297 * movable up and the last track not movable down.
9faa7a88 298 */
af106f2d 299static int exp_movable(int nargs,
9faa7a88
RK
300 char **args,
301 struct sink *output,
302 void attribute((unused)) *u) {
a2c4ad5f 303 struct queue_entry *q = dcgi_findtrack(args[0]);
9faa7a88
RK
304 /* TODO would be better to recent playing/recent */
305
1e97629d 306 if(!q || !dcgi_client)
9faa7a88 307 return mx_bool_result(output, 0);
af106f2d
RK
308 if(nargs > 1) {
309 const long dir = atoi(args[1]);
310
311 if(dir > 0 && q == dcgi_queue)
312 return mx_bool_result(output, 0);
313 if(dir < 0 && q->next == 0)
314 return mx_bool_result(output, 0);
315 }
1e97629d 316 dcgi_lookup(DCGI_RIGHTS);
9faa7a88 317 return mx_bool_result(output,
af106f2d
RK
318 right_movable(dcgi_rights,
319 disorder_user(dcgi_client),
320 q));
9faa7a88
RK
321}
322
59cf25c4 323/*$ @playing{TEMPLATE}
9faa7a88 324 *
3e1616b6
RK
325 * Expands to TEMPLATE, with the following expansions:
326 * - @id: the queue ID of the playing track
327 * - @track: the playing track's
328 UNQUOTED name
9faa7a88
RK
329 *
330 * If no track is playing expands to nothing.
331 *
332 * TEMPLATE is optional. If it is left out then instead expands to the queue
333 * ID of the playing track.
334 */
335static int exp_playing(int nargs,
336 const struct mx_node **args,
337 struct sink *output,
71634563 338 void *u) {
1e97629d
RK
339 dcgi_lookup(DCGI_PLAYING);
340 if(!dcgi_playing)
9faa7a88
RK
341 return 0;
342 if(!nargs)
1e97629d 343 return sink_writes(output, dcgi_playing->id) < 0 ? -1 : 0;
71634563 344 return mx_expand(mx_rewritel(args[0],
1e97629d
RK
345 "id", dcgi_playing->id,
346 "track", dcgi_playing->track,
71634563
RK
347 (char *)0),
348 output, u);
9faa7a88
RK
349}
350
59cf25c4 351/*$ @queue{TEMPLATE}
9faa7a88
RK
352 *
353 * For each track in the queue, expands TEMPLATE with the following expansions:
3e1616b6
RK
354 * - @id: the queue ID of the track
355 * - @track: the UNQUOTED track name
356 * - @index: the track number from 0
357 * - @parity: "even" or "odd" alternately
358 * - @first: "true" on the first track and "false" otherwise
359 * - @last: "true" on the last track and "false" otherwise
9faa7a88
RK
360 */
361static int exp_queue(int attribute((unused)) nargs,
362 const struct mx_node **args,
363 struct sink *output,
71634563 364 void *u) {
9faa7a88
RK
365 struct queue_entry *q;
366 int rc, i;
367
1e97629d
RK
368 dcgi_lookup(DCGI_QUEUE);
369 for(q = dcgi_queue, i = 0; q; q = q->next, ++i)
71634563
RK
370 if((rc = mx_expand(mx_rewritel(args[0],
371 "id", q->id,
372 "track", q->track,
373 "index", make_index(i),
374 "parity", i % 2 ? "odd" : "even",
1e97629d 375 "first", q == dcgi_queue ? "true" : "false",
71634563
RK
376 "last", q->next ? "false" : "true",
377 (char *)0),
378 output, u)))
9faa7a88
RK
379 return rc;
380 return 0;
381}
382
59cf25c4 383/*$ @recent{TEMPLATE}
9faa7a88
RK
384 *
385 * For each track in the recently played list, expands TEMPLATE with the
386 * following expansions:
3e1616b6
RK
387 * - @id: the queue ID of the track
388 * - @track: the UNQUOTED track name
389 * - @index: the track number from 0
390 * - @parity: "even" or "odd" alternately
391 * - @first: "true" on the first track and "false" otherwise
392 * - @last: "true" on the last track and "false" otherwise
9faa7a88
RK
393 */
394static int exp_recent(int attribute((unused)) nargs,
395 const struct mx_node **args,
396 struct sink *output,
71634563 397 void *u) {
9faa7a88
RK
398 struct queue_entry *q;
399 int rc, i;
400
1e97629d
RK
401 dcgi_lookup(DCGI_RECENT);
402 for(q = dcgi_recent, i = 0; q; q = q->next, ++i)
71634563
RK
403 if((rc = mx_expand(mx_rewritel(args[0],
404 "id", q->id,
405 "track", q->track,
406 "index", make_index(i),
407 "parity", i % 2 ? "odd" : "even",
1e97629d 408 "first", q == dcgi_recent ? "true" : "false",
71634563
RK
409 "last", q->next ? "false" : "true",
410 (char *)0),
411 output, u)))
9faa7a88
RK
412 return rc;
413 return 0;
414}
415
59cf25c4 416/*$ @new{TEMPLATE}
9faa7a88
RK
417 *
418 * For each track in the newly added list, expands TEMPLATE wit the following
419 * expansions:
3e1616b6
RK
420 * - @track: the UNQUOTED track name
421 * - @index: the track number from 0
422 * - @parity: "even" or "odd" alternately
423 * - @first: "true" on the first track and "false" otherwise
424 * - @last: "true" on the last track and "false" otherwise
9faa7a88 425 *
9bf61b29
RK
426 * Note that unlike @playing, @queue and @recent which are otherwise
427 * superficially similar, there is no @id sub-expansion here.
9faa7a88
RK
428 */
429static int exp_new(int attribute((unused)) nargs,
430 const struct mx_node **args,
431 struct sink *output,
71634563 432 void *u) {
9faa7a88
RK
433 int rc, i;
434
1e97629d 435 dcgi_lookup(DCGI_NEW);
9faa7a88 436 /* TODO perhaps we should generate an ID value for tracks in the new list */
1e97629d 437 for(i = 0; i < dcgi_nnew; ++i)
71634563 438 if((rc = mx_expand(mx_rewritel(args[0],
1e97629d 439 "track", dcgi_new[i],
71634563
RK
440 "index", make_index(i),
441 "parity", i % 2 ? "odd" : "even",
442 "first", i == 0 ? "true" : "false",
1e97629d 443 "last", i == dcgi_nnew - 1 ? "false" : "true",
71634563
RK
444 (char *)0),
445 output, u)))
9faa7a88
RK
446 return rc;
447 return 0;
448}
449
59cf25c4 450/*$ @volume{CHANNEL}
9faa7a88
RK
451 *
452 * Expands to the volume in a given channel. CHANNEL must be "left" or
453 * "right".
454 */
455static int exp_volume(int attribute((unused)) nargs,
456 char **args,
457 struct sink *output,
458 void attribute((unused)) *u) {
1e97629d 459 dcgi_lookup(DCGI_VOLUME);
71634563
RK
460 return sink_printf(output, "%d",
461 !strcmp(args[0], "left")
1e97629d 462 ? dcgi_volume_left : dcgi_volume_right) < 0 ? -1 : 0;
9faa7a88
RK
463}
464
59cf25c4 465/*$ @isplaying
9faa7a88
RK
466 *
467 * Expands to "true" if there is a playing track, otherwise "false".
468 */
469static int exp_isplaying(int attribute((unused)) nargs,
470 char attribute((unused)) **args,
471 struct sink *output,
472 void attribute((unused)) *u) {
1e97629d
RK
473 dcgi_lookup(DCGI_PLAYING);
474 return mx_bool_result(output, !!dcgi_playing);
9faa7a88
RK
475}
476
59cf25c4 477/*$ @isqueue
9faa7a88
RK
478 *
479 * Expands to "true" if there the queue is nonempty, otherwise "false".
480 */
481static int exp_isqueue(int attribute((unused)) nargs,
482 char attribute((unused)) **args,
483 struct sink *output,
484 void attribute((unused)) *u) {
1e97629d
RK
485 dcgi_lookup(DCGI_QUEUE);
486 return mx_bool_result(output, !!dcgi_queue);
9faa7a88
RK
487}
488
2dc2f478 489/*$ @isrecent
9faa7a88
RK
490 *
491 * Expands to "true" if there the recently played list is nonempty, otherwise
492 * "false".
493 */
494static int exp_isrecent(int attribute((unused)) nargs,
495 char attribute((unused)) **args,
496 struct sink *output,
497 void attribute((unused)) *u) {
1e97629d
RK
498 dcgi_lookup(DCGI_RECENT);
499 return mx_bool_result(output, !!dcgi_recent);
9faa7a88
RK
500}
501
59cf25c4 502/*$ @isnew
9faa7a88
RK
503 *
504 * Expands to "true" if there the newly added track list is nonempty, otherwise
505 * "false".
506 */
507static int exp_isnew(int attribute((unused)) nargs,
508 char attribute((unused)) **args,
509 struct sink *output,
510 void attribute((unused)) *u) {
1e97629d
RK
511 dcgi_lookup(DCGI_NEW);
512 return mx_bool_result(output, !!dcgi_nnew);
9faa7a88
RK
513}
514
59cf25c4 515/*$ @pref{TRACK}{KEY}
9faa7a88
RK
516 *
517 * Expands to a track preference.
518 */
519static int exp_pref(int attribute((unused)) nargs,
520 char **args,
521 struct sink *output,
522 void attribute((unused)) *u) {
523 char *value;
524
1e97629d 525 if(dcgi_client && !disorder_get(dcgi_client, args[0], args[1], &value))
71634563
RK
526 return sink_writes(output, cgi_sgmlquote(value)) < 0 ? -1 : 0;
527 return 0;
9faa7a88
RK
528}
529
59cf25c4 530/*$ @prefs{TRACK}{TEMPLATE}
9faa7a88
RK
531 *
532 * For each track preference of track TRACK, expands TEMPLATE with the
533 * following expansions:
3e1616b6
RK
534 * - @name: the UNQUOTED preference name
535 * - @index: the preference number from 0
536 * - @value: the UNQUOTED preference value
537 * - @parity: "even" or "odd" alternately
538 * - @first: "true" on the first preference and "false" otherwise
539 * - @last: "true" on the last preference and "false" otherwise
9faa7a88 540 *
9bf61b29 541 * Use @quote to quote preference names and values where necessary; see below.
9faa7a88
RK
542 */
543static int exp_prefs(int attribute((unused)) nargs,
544 const struct mx_node **args,
545 struct sink *output,
71634563 546 void *u) {
9faa7a88
RK
547 int rc, i;
548 struct kvp *k, *head;
549 char *track;
550
551 if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)")))
552 return rc;
1e97629d 553 if(!dcgi_client || disorder_prefs(dcgi_client, track, &head))
9faa7a88
RK
554 return 0;
555 for(k = head, i = 0; k; k = k->next, ++i)
71634563
RK
556 if((rc = mx_expand(mx_rewritel(args[1],
557 "index", make_index(i),
558 "parity", i % 2 ? "odd" : "even",
559 "name", k->name,
560 "value", k->value,
561 "first", k == head ? "true" : "false",
562 "last", k->next ? "false" : "true",
563 (char *)0),
564 output, u)))
9faa7a88
RK
565 return rc;
566 return 0;
567}
568
59cf25c4 569/*$ @transform{TRACK}{TYPE}{CONTEXT}
9faa7a88
RK
570 *
571 * Transforms a track name (if TYPE is "track") or directory name (if type is
572 * "dir"). CONTEXT should be the context, if it is left out then "display" is
573 * assumed.
574 */
575static int exp_transform(int nargs,
576 char **args,
577 struct sink *output,
578 void attribute((unused)) *u) {
579 const char *t = trackname_transform(args[1], args[0],
71634563
RK
580 (nargs > 2 ? args[2] : "display"));
581 return sink_writes(output, cgi_sgmlquote(t)) < 0 ? -1 : 0;
9faa7a88
RK
582}
583
2dc2f478 584/*$ @enabled
9faa7a88
RK
585 *
586 * Expands to "true" if playing is enabled, otherwise "false".
587 */
588static int exp_enabled(int attribute((unused)) nargs,
589 char attribute((unused)) **args,
590 struct sink *output,
591 void attribute((unused)) *u) {
1e97629d 592 int e = 0;
9faa7a88 593
1e97629d
RK
594 if(dcgi_client)
595 disorder_enabled(dcgi_client, &e);
596 return mx_bool_result(output, e);
9faa7a88
RK
597}
598
59cf25c4 599/*$ @random-enabled
9faa7a88
RK
600 *
601 * Expands to "true" if random play is enabled, otherwise "false".
602 */
71634563
RK
603static int exp_random_enabled(int attribute((unused)) nargs,
604 char attribute((unused)) **args,
605 struct sink *output,
606 void attribute((unused)) *u) {
1e97629d 607 int e = 0;
9faa7a88 608
1e97629d
RK
609 if(dcgi_client)
610 disorder_random_enabled(dcgi_client, &e);
611 return mx_bool_result(output, e);
9faa7a88
RK
612}
613
59cf25c4 614/*$ @trackstate{TRACK}
9faa7a88
RK
615 *
616 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
617 * the queue, otherwise to nothing.
618 */
619static int exp_trackstate(int attribute((unused)) nargs,
620 char **args,
621 struct sink *output,
622 void attribute((unused)) *u) {
623 char *track;
624 struct queue_entry *q;
625
1e97629d 626 if(!dcgi_client)
9faa7a88 627 return 0;
1e97629d 628 if(disorder_resolve(dcgi_client, &track, args[0]))
9faa7a88 629 return 0;
1e97629d
RK
630 dcgi_lookup(DCGI_PLAYING);
631 if(dcgi_playing && !strcmp(track, dcgi_playing->track))
71634563 632 return sink_writes(output, "playing") < 0 ? -1 : 0;
1e97629d
RK
633 dcgi_lookup(DCGI_QUEUE);
634 for(q = dcgi_queue; q; q = q->next)
9faa7a88 635 if(!strcmp(track, q->track))
71634563 636 return sink_writes(output, "queued") < 0 ? -1 : 0;
9faa7a88
RK
637 return 0;
638}
639
59cf25c4 640/*$ @thisurl
9faa7a88
RK
641 *
642 * Expands to an UNQUOTED URL which points back to the current page. (NB it
643 * might not be byte for byte identical - for instance, CGI arguments might be
644 * re-ordered.)
645 */
646static int exp_thisurl(int attribute((unused)) nargs,
647 char attribute((unused)) **args,
648 struct sink *output,
649 void attribute((unused)) *u) {
71634563 650 return sink_writes(output, cgi_thisurl(config->url)) < 0 ? -1 : 0;
9faa7a88
RK
651}
652
59cf25c4 653/*$ @resolve{TRACK}
9faa7a88
RK
654 *
655 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
656 * nothing if it is not a valid track.
657 */
658static int exp_resolve(int attribute((unused)) nargs,
659 char **args,
660 struct sink *output,
661 void attribute((unused)) *u) {
662 char *r;
663
1e97629d 664 if(dcgi_client && !disorder_resolve(dcgi_client, &r, args[0]))
71634563 665 return sink_writes(output, r) < 0 ? -1 : 0;
9faa7a88
RK
666 return 0;
667}
668
59cf25c4 669/*$ @paused
9faa7a88
RK
670 *
671 * Expands to "true" if the playing track is paused, to "false" if it is
672 * playing (or if there is no playing track at all).
673 */
674static int exp_paused(int attribute((unused)) nargs,
675 char attribute((unused)) **args,
676 struct sink *output,
48e472fa 677 void attribute((unused)) *u) {
1e97629d
RK
678 dcgi_lookup(DCGI_PLAYING);
679 return mx_bool_result(output, (dcgi_playing
680 && dcgi_playing->state == playing_paused));
9faa7a88
RK
681}
682
2dc2f478 683/*$ @state{ID}
9faa7a88
RK
684 *
685 * Expands to the current state of track ID.
686 */
687static int exp_state(int attribute((unused)) nargs,
688 char **args,
689 struct sink *output,
690 void attribute((unused)) *u) {
a2c4ad5f 691 struct queue_entry *q = dcgi_findtrack(args[0]);
9faa7a88
RK
692
693 if(q)
71634563 694 return sink_writes(output, playing_states[q->state]) < 0 ? -1 : 0;
9faa7a88
RK
695 return 0;
696}
697
2dc2f478
RK
698/*$ @origin{ID}
699 *
700 * Expands to the current origin of track ID.
701 */
702static int exp_origin(int attribute((unused)) nargs,
703 char **args,
704 struct sink *output,
705 void attribute((unused)) *u) {
706 struct queue_entry *q = dcgi_findtrack(args[0]);
707
708 if(q)
709 return sink_writes(output, track_origins[q->origin]) < 0 ? -1 : 0;
710 return 0;
711}
712
713/*$ @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}
9faa7a88
RK
714 *
715 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
716 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
717 *
718 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
719 * the user has the right and "false" otherwise.
720 *
721 * If there is no connection to the server then expands to nothing (in all
722 * cases).
723 */
724static int exp_right(int nargs,
725 const struct mx_node **args,
726 struct sink *output,
71634563 727 void *u) {
9faa7a88
RK
728 char *right;
729 rights_type r;
71634563 730 int rc;
9faa7a88 731
1e97629d 732 if(!dcgi_client)
9faa7a88 733 return 0;
1e97629d 734 dcgi_lookup(DCGI_RIGHTS);
71634563 735 if((rc = mx_expandstr(args[0], &right, u, "argument #0 (RIGHT)")))
9faa7a88
RK
736 return rc;
737 if(parse_rights(right, &r, 1/*report*/))
738 return 0;
739 /* Single-argument form */
740 if(nargs == 1)
1e97629d 741 return mx_bool_result(output, !!(r & dcgi_rights));
9faa7a88 742 /* Multiple argument form */
1e97629d 743 if(r & dcgi_rights)
71634563 744 return mx_expand(args[1], output, u);
9faa7a88 745 if(nargs == 3)
71634563 746 return mx_expand(args[2], output, u);
9faa7a88
RK
747 return 0;
748}
749
59cf25c4 750/*$ @userinfo{PROPERTY}
9faa7a88
RK
751 *
752 * Expands to the named property of the current user.
753 */
754static int exp_userinfo(int attribute((unused)) nargs,
755 char **args,
756 struct sink *output,
757 void attribute((unused)) *u) {
758 char *v;
759
1e97629d
RK
760 if(dcgi_client
761 && !disorder_userinfo(dcgi_client, disorder_user(dcgi_client),
762 args[0], &v))
71634563 763 return sink_writes(output, v) < 0 ? -1 : 0;
9faa7a88
RK
764 return 0;
765}
766
59cf25c4 767/*$ @error
bca4e2b7
RK
768 *
769 * Expands to the latest error string.
770 */
771static int exp_error(int attribute((unused)) nargs,
772 char attribute((unused)) **args,
773 struct sink *output,
774 void attribute((unused)) *u) {
343d9c2b
RK
775 return sink_writes(output, dcgi_error_string ? dcgi_error_string : "")
776 < 0 ? -1 : 0;
777}
778
59cf25c4 779/*$ @status
343d9c2b
RK
780 *
781 * Expands to the latest status string.
782 */
783static int exp_status(int attribute((unused)) nargs,
784 char attribute((unused)) **args,
785 struct sink *output,
786 void attribute((unused)) *u) {
787 return sink_writes(output, dcgi_status_string ? dcgi_status_string : "")
788 < 0 ? -1 : 0;
bca4e2b7
RK
789}
790
59cf25c4 791/*$ @image{NAME}
30d85588
RK
792 *
793 * Expands to the URL of the image called NAME.
794 *
795 * Firstly if there is a label called images.NAME then the image stem will be
796 * the value of that label. Otherwise the stem will be NAME.png.
797 *
798 * If the label url.static exists then it will give the base URL for images.
799 * Otherwise the base url will be /disorder/.
800 */
801static int exp_image(int attribute((unused)) nargs,
802 char **args,
803 struct sink *output,
804 void attribute((unused)) *u) {
805 const char *url, *stem;
806 char *labelname;
807
808 /* Compute the stem */
809 byte_xasprintf(&labelname, "images.%s", args[0]);
810 if(option_label_exists(labelname))
811 stem = option_label(labelname);
812 else
813 byte_xasprintf((char **)&stem, "%s.png", args[0]);
814 /* If the stem looks like it's reasonalby complete, use that */
815 if(stem[0] == '/'
816 || !strncmp(stem, "http:", 5)
817 || !strncmp(stem, "https:", 6))
818 url = stem;
819 else {
820 /* Compute the URL */
821 if(option_label_exists("url.static"))
822 byte_xasprintf((char **)&url, "%s/%s",
823 option_label("url.static"), stem);
824 else
825 /* Default base is /disorder */
826 byte_xasprintf((char **)&url, "/disorder/%s", stem);
827 }
828 return sink_writes(output, cgi_sgmlquote(url)) < 0 ? -1 : 0;
829}
830
9bf61b29
RK
831/** @brief Implementation of exp_tracks() and exp_dirs() */
832static int exp__files_dirs(int nargs,
833 const struct mx_node **args,
834 struct sink *output,
835 void *u,
836 const char *type,
837 int (*fn)(disorder_client *client,
838 const char *name,
839 const char *re,
840 char ***vecp,
841 int *nvecp)) {
842 char **tracks, *dir, *re;
843 int n, ntracks, rc;
844 const struct mx_node *m;
e13809e4 845 struct tracksort_data *tsd;
9bf61b29
RK
846
847 if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)")))
848 return rc;
849 if(nargs == 3) {
850 if((rc = mx_expandstr(args[1], &re, u, "argument #1 (RE)")))
851 return rc;
852 m = args[2];
853 } else {
854 re = 0;
855 m = args[1];
856 }
857 if(!dcgi_client)
858 return 0;
859 /* Get the list */
860 if(fn(dcgi_client, dir, re, &tracks, &ntracks))
861 return 0;
862 /* Sort it. NB trackname_transform() does not go to the server. */
e13809e4 863 tsd = tracksort_init(ntracks, tracks, type);
48e472fa
RK
864 /* Expand the subsiduary templates. We chuck in @sort and @display because
865 * it is particularly easy to do so. */
9bf61b29
RK
866 for(n = 0; n < ntracks; ++n)
867 if((rc = mx_expand(mx_rewritel(m,
868 "index", make_index(n),
869 "parity", n % 2 ? "odd" : "even",
e13809e4 870 "track", tsd[n].track,
9bf61b29
RK
871 "first", n == 0 ? "true" : "false",
872 "last", n + 1 == ntracks ? "false" : "true",
e13809e4
RK
873 "sort", tsd[n].sort,
874 "display", tsd[n].display,
9bf61b29
RK
875 (char *)0),
876 output, u)))
877 return rc;
878 return 0;
879
880}
881
59cf25c4 882/*$ @tracks{DIR}{RE}{TEMPLATE}
9bf61b29
RK
883 *
884 * For each track below DIR, expands TEMPLATE with the
885 * following expansions:
3e1616b6
RK
886 * - @track: the UNQUOTED track name
887 * - @index: the track number from 0
888 * - @parity: "even" or "odd" alternately
889 * - @first: "true" on the first track and "false" otherwise
890 * - @last: "true" on the last track and "false" otherwise
891 * - @sort: the sort key for this track
892 * - @display: the UNQUOTED display string for this track
9bf61b29
RK
893 *
894 * RE is optional and if present is the regexp to match against.
895 */
896static int exp_tracks(int nargs,
897 const struct mx_node **args,
898 struct sink *output,
899 void *u) {
900 return exp__files_dirs(nargs, args, output, u, "track", disorder_files);
901}
902
59cf25c4 903/*$ @dirs{DIR}{RE}{TEMPLATE}
9bf61b29
RK
904 *
905 * For each directory below DIR, expands TEMPLATE with the
906 * following expansions:
3e1616b6
RK
907 * - @track: the UNQUOTED directory name
908 * - @index: the directory number from 0
909 * - @parity: "even" or "odd" alternately
910 * - @first: "true" on the first directory and "false" otherwise
911 * - @last: "true" on the last directory and "false" otherwise
912 * - @sort: the sort key for this directory
913 * - @display: the UNQUOTED display string for this directory
9bf61b29
RK
914 *
915 * RE is optional and if present is the regexp to match against.
916 */
917static int exp_dirs(int nargs,
918 const struct mx_node **args,
919 struct sink *output,
920 void *u) {
921 return exp__files_dirs(nargs, args, output, u, "dir", disorder_directories);
922}
923
b43dc0cc
RK
924static int exp__search_shim(disorder_client *c, const char *terms,
925 const char attribute((unused)) *re,
926 char ***vecp, int *nvecp) {
927 return disorder_search(c, terms, vecp, nvecp);
928}
929
59cf25c4 930/*$ @search{KEYWORDS}{TEMPLATE}
b43dc0cc
RK
931 *
932 * For each track matching KEYWORDS, expands TEMPLATE with the
933 * following expansions:
3e1616b6
RK
934 * - @track: the UNQUOTED directory name
935 * - @index: the directory number from 0
936 * - @parity: "even" or "odd" alternately
937 * - @first: "true" on the first directory and "false" otherwise
938 * - @last: "true" on the last directory and "false" otherwise
939 * - @sort: the sort key for this track
940 * - @display: the UNQUOTED display string for this track
b43dc0cc
RK
941 */
942static int exp_search(int nargs,
99955407
RK
943 const struct mx_node **args,
944 struct sink *output,
945 void *u) {
b43dc0cc
RK
946 return exp__files_dirs(nargs, args, output, u, "track", exp__search_shim);
947}
948
59cf25c4 949/*$ @label{NAME}
f4a26919
RK
950 *
951 * Expands to label NAME from options.labels. Undefined lables expand to the
952 * last dot-separated component, e.g. X.Y.Z to Z.
953 */
99955407
RK
954static int exp_label(int attribute((unused)) nargs,
955 char **args,
956 struct sink *output,
957 void attribute((unused)) *u) {
958 return sink_writes(output, option_label(args[0])) < 0 ? -1 : 0;
959}
960
59cf25c4 961/*$ @breadcrumbs{DIR}{TEMPLATE}
f4a26919
RK
962 *
963 * Expands TEMPLATE for each directory in the path up to DIR, excluding the root
964 * but including DIR itself, with the following expansions:
965 * - @dir: the directory
966 * - @last: "true" if this is the last directory, otherwise "false"
967 *
968 * DIR must be an absolute path.
969 */
970static int exp_breadcrumbs(int attribute((unused)) nargs,
971 const struct mx_node **args,
972 struct sink *output,
973 void attribute((unused)) *u) {
974 int rc;
975 char *dir, *parent, *ptr;
976
977 if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)")))
978 return rc;
979 /* Reject relative paths */
980 if(dir[0] != '/') {
981 error(0, "breadcrumbs: '%s' is a relative path", dir);
982 return 0;
983 }
984 /* Skip the root */
985 ptr = dir + 1;
986 while(*ptr) {
987 /* Find the end of this directory */
988 while(*ptr && *ptr != '/')
989 ++ptr;
990 parent = xstrndup(dir, ptr - dir);
991 if((rc = mx_expand(mx_rewritel(args[1],
992 "dir", parent,
993 "last", *ptr ? "false" : "true",
994 (char *)0),
995 output, u)))
996 return rc;
997 if(*ptr)
998 ++ptr;
999 }
1000 return 0;
1001}
1002
9faa7a88 1003/** @brief Register DisOrder-specific expansions */
1e97629d 1004void dcgi_expansions(void) {
71634563 1005 mx_register("arg", 1, 1, exp_arg);
48e472fa 1006 mx_register("argq", 1, 1, exp_argq);
71634563
RK
1007 mx_register("enabled", 0, 0, exp_enabled);
1008 mx_register("error", 0, 0, exp_error);
30d85588 1009 mx_register("image", 1, 1, exp_image);
71634563
RK
1010 mx_register("isnew", 0, 0, exp_isnew);
1011 mx_register("isplaying", 0, 0, exp_isplaying);
99955407 1012 mx_register("isqueue", 0, 0, exp_isqueue);
71634563 1013 mx_register("isrecent", 0, 0, exp_isrecent);
99955407 1014 mx_register("label", 1, 1, exp_label);
71634563 1015 mx_register("length", 1, 1, exp_length);
99955407 1016 mx_register("movable", 1, 2, exp_movable);
2dc2f478 1017 mx_register("origin", 1, 1, exp_origin);
71634563
RK
1018 mx_register("part", 2, 3, exp_part);
1019 mx_register("paused", 0, 0, exp_paused);
1020 mx_register("pref", 2, 2, exp_pref);
1021 mx_register("quote", 1, 1, exp_quote);
1022 mx_register("random-enabled", 0, 0, exp_random_enabled);
1023 mx_register("removable", 1, 1, exp_removable);
1024 mx_register("resolve", 1, 1, exp_resolve);
1025 mx_register("server-version", 0, 0, exp_server_version);
1026 mx_register("state", 1, 1, exp_state);
343d9c2b 1027 mx_register("status", 0, 0, exp_status);
71634563
RK
1028 mx_register("thisurl", 0, 0, exp_thisurl);
1029 mx_register("trackstate", 1, 1, exp_trackstate);
1030 mx_register("transform", 2, 3, exp_transform);
1031 mx_register("url", 0, 0, exp_url);
1032 mx_register("user", 0, 0, exp_user);
1033 mx_register("userinfo", 1, 1, exp_userinfo);
1034 mx_register("version", 0, 0, exp_version);
1035 mx_register("volume", 1, 1, exp_volume);
1036 mx_register("when", 1, 1, exp_when);
1037 mx_register("who", 1, 1, exp_who);
f4a26919 1038 mx_register_magic("breadcrumbs", 2, 2, exp_breadcrumbs);
9bf61b29 1039 mx_register_magic("dirs", 2, 3, exp_dirs);
71634563
RK
1040 mx_register_magic("new", 1, 1, exp_new);
1041 mx_register_magic("playing", 0, 1, exp_playing);
1042 mx_register_magic("prefs", 2, 2, exp_prefs);
1043 mx_register_magic("queue", 1, 1, exp_queue);
1044 mx_register_magic("recent", 1, 1, exp_recent);
1045 mx_register_magic("right", 1, 3, exp_right);
b43dc0cc 1046 mx_register_magic("search", 2, 2, exp_search);
9bf61b29 1047 mx_register_magic("tracks", 2, 3, exp_tracks);
bca4e2b7
RK
1048}
1049
9faa7a88
RK
1050/*
1051Local Variables:
1052c-basic-offset:2
1053comment-column:40
1054fill-column:79
1055indent-tabs-mode:nil
1056End:
1057*/