Split template docs out to disorder_templates(5)
[disorder] / server / macros-disorder.c
CommitLineData
9faa7a88
RK
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
1e97629d 24#include "disorder-cgi.h"
9faa7a88 25
bca4e2b7 26/** @brief For error template */
1e97629d 27char *dcgi_error_string;
bca4e2b7 28
343d9c2b
RK
29/** @brief For login template */
30char *dcgi_status_string;
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
bca4e2b7 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
bca4e2b7 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
bca4e2b7 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
bca4e2b7 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
100/* @argq{NAME}
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
bca4e2b7 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
133/* @part{TRACK|ID}{PART}{CONTEXT}
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";
151 char *s;
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 }
99955407
RK
161 fprintf(stderr, "track=[%s] part=[%s] context=[%s] dcgi_client=[%p]\n",
162 track,
163 part,
164 !strcmp(context, "short") ? "display" : context,
165 dcgi_client);
1e97629d
RK
166 if(dcgi_client
167 && !disorder_part(dcgi_client, &s,
71634563
RK
168 track,
169 !strcmp(context, "short") ? "display" : context,
170 part))
171 return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
9faa7a88
RK
172 return 0;
173}
174
175/* @quote{STRING}
176 *
177 * SGML-quotes STRING. Note that most expansion results are already suitable
178 * quoted, so this expansion is usually not required.
179 */
180static int exp_quote(int attribute((unused)) nargs,
181 char **args,
182 struct sink *output,
183 void attribute((unused)) *u) {
71634563 184 return sink_writes(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0;
9faa7a88
RK
185}
186
187/* @who{ID}
188 *
189 * Expands to the name of the submitter of track ID, which must be a playing
190 * track, in the queue, or in the recent list.
191 */
192static int exp_who(int attribute((unused)) nargs,
193 char **args,
194 struct sink *output,
195 void attribute((unused)) *u) {
a2c4ad5f 196 struct queue_entry *q = dcgi_findtrack(args[0]);
9faa7a88
RK
197
198 if(q && q->submitter)
71634563 199 return sink_writes(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0;
9faa7a88
RK
200 return 0;
201}
202
203/* @when{ID}
204 *
205 * Expands to the time a track started or is expected to start. The track must
206 * be a playing track, in the queue, or in the recent list.
207 */
208static int exp_when(int attribute((unused)) nargs,
209 char **args,
210 struct sink *output,
211 void attribute((unused)) *u) {
a2c4ad5f 212 struct queue_entry *q = dcgi_findtrack(args[0]);
9faa7a88
RK
213 const struct tm *w = 0;
214
215 if(q) {
216 switch(q->state) {
217 case playing_isscratch:
218 case playing_unplayed:
219 case playing_random:
220 if(q->expected)
221 w = localtime(&q->expected);
222 break;
223 case playing_failed:
224 case playing_no_player:
225 case playing_ok:
226 case playing_scratched:
227 case playing_started:
228 case playing_paused:
229 case playing_quitting:
230 if(q->played)
231 w = localtime(&q->played);
232 break;
233 }
234 if(w)
235 return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0;
236 }
71634563 237 return sink_writes(output, "&nbsp;") < 0 ? -1 : 0;
9faa7a88
RK
238}
239
240/* @length{ID|TRACK}
241 *
242 * Expands to the length of a track, identified by its queue ID or its name.
243 * If it is the playing track (identified by ID) then the amount played so far
244 * is included.
245 */
246static int exp_length(int attribute((unused)) nargs,
247 char **args,
248 struct sink *output,
249 void attribute((unused)) *u) {
250 struct queue_entry *q;
251 long length = 0;
252 const char *name;
253
254 if(args[0][0] == '/')
255 /* Track identified by name */
256 name = args[0];
257 else {
258 /* Track identified by queue ID */
a2c4ad5f 259 if(!(q = dcgi_findtrack(args[0])))
9faa7a88 260 return 0;
71634563 261 if(q->state == playing_started || q->state == playing_paused)
9faa7a88
RK
262 if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0)
263 return -1;
264 name = q->track;
265 }
1e97629d 266 if(dcgi_client && disorder_length(dcgi_client, name, &length))
9faa7a88
RK
267 return sink_printf(output, "%ld:%02ld",
268 length / 60, length % 60) < 0 ? -1 : 0;
71634563 269 return sink_writes(output, "&nbsp;") < 0 ? -1 : 0;
9faa7a88
RK
270}
271
bca4e2b7 272/* @removable{ID}
9faa7a88
RK
273 *
274 * Expands to "true" if track ID is removable (or scratchable, if it is the
275 * playing track) and "false" otherwise.
276 */
277static int exp_removable(int attribute((unused)) nargs,
278 char **args,
279 struct sink *output,
280 void attribute((unused)) *u) {
a2c4ad5f 281 struct queue_entry *q = dcgi_findtrack(args[0]);
9faa7a88
RK
282 /* TODO would be better to reject recent */
283
1e97629d 284 if(!q || !dcgi_client)
9faa7a88 285 return mx_bool_result(output, 0);
1e97629d 286 dcgi_lookup(DCGI_RIGHTS);
9faa7a88 287 return mx_bool_result(output,
1e97629d
RK
288 (q == dcgi_playing ? right_scratchable : right_removable)
289 (dcgi_rights, disorder_user(dcgi_client), q));
9faa7a88
RK
290}
291
af106f2d 292/* @movable{ID}{DIR}
9faa7a88
RK
293 *
294 * Expands to "true" if track ID is movable and "false" otherwise.
af106f2d
RK
295 *
296 * DIR (which is optional) should be a non-zero integer. If it is negative
297 * then the intended move is down (later in the queue), if it is positive then
298 * the intended move is up (earlier in the queue). The first track is not
299 * movable up and the last track not movable down.
9faa7a88 300 */
af106f2d 301static int exp_movable(int nargs,
9faa7a88
RK
302 char **args,
303 struct sink *output,
304 void attribute((unused)) *u) {
a2c4ad5f 305 struct queue_entry *q = dcgi_findtrack(args[0]);
9faa7a88
RK
306 /* TODO would be better to recent playing/recent */
307
1e97629d 308 if(!q || !dcgi_client)
9faa7a88 309 return mx_bool_result(output, 0);
af106f2d
RK
310 if(nargs > 1) {
311 const long dir = atoi(args[1]);
312
313 if(dir > 0 && q == dcgi_queue)
314 return mx_bool_result(output, 0);
315 if(dir < 0 && q->next == 0)
316 return mx_bool_result(output, 0);
317 }
1e97629d 318 dcgi_lookup(DCGI_RIGHTS);
9faa7a88 319 return mx_bool_result(output,
af106f2d
RK
320 right_movable(dcgi_rights,
321 disorder_user(dcgi_client),
322 q));
9faa7a88
RK
323}
324
325/* @playing{TEMPLATE}
326 *
3e1616b6
RK
327 * Expands to TEMPLATE, with the following expansions:
328 * - @id: the queue ID of the playing track
329 * - @track: the playing track's
330 UNQUOTED name
9faa7a88
RK
331 *
332 * If no track is playing expands to nothing.
333 *
334 * TEMPLATE is optional. If it is left out then instead expands to the queue
335 * ID of the playing track.
336 */
337static int exp_playing(int nargs,
338 const struct mx_node **args,
339 struct sink *output,
71634563 340 void *u) {
1e97629d
RK
341 dcgi_lookup(DCGI_PLAYING);
342 if(!dcgi_playing)
9faa7a88
RK
343 return 0;
344 if(!nargs)
1e97629d 345 return sink_writes(output, dcgi_playing->id) < 0 ? -1 : 0;
71634563 346 return mx_expand(mx_rewritel(args[0],
1e97629d
RK
347 "id", dcgi_playing->id,
348 "track", dcgi_playing->track,
71634563
RK
349 (char *)0),
350 output, u);
9faa7a88
RK
351}
352
353/* @queue{TEMPLATE}
354 *
355 * For each track in the queue, expands TEMPLATE with the following expansions:
3e1616b6
RK
356 * - @id: the queue ID of the track
357 * - @track: the UNQUOTED track name
358 * - @index: the track number from 0
359 * - @parity: "even" or "odd" alternately
360 * - @first: "true" on the first track and "false" otherwise
361 * - @last: "true" on the last track and "false" otherwise
9faa7a88
RK
362 */
363static int exp_queue(int attribute((unused)) nargs,
364 const struct mx_node **args,
365 struct sink *output,
71634563 366 void *u) {
9faa7a88
RK
367 struct queue_entry *q;
368 int rc, i;
369
1e97629d
RK
370 dcgi_lookup(DCGI_QUEUE);
371 for(q = dcgi_queue, i = 0; q; q = q->next, ++i)
71634563
RK
372 if((rc = mx_expand(mx_rewritel(args[0],
373 "id", q->id,
374 "track", q->track,
375 "index", make_index(i),
376 "parity", i % 2 ? "odd" : "even",
1e97629d 377 "first", q == dcgi_queue ? "true" : "false",
71634563
RK
378 "last", q->next ? "false" : "true",
379 (char *)0),
380 output, u)))
9faa7a88
RK
381 return rc;
382 return 0;
383}
384
385/* @recent{TEMPLATE}
386 *
387 * For each track in the recently played list, expands TEMPLATE with the
388 * following expansions:
3e1616b6
RK
389 * - @id: the queue ID of the track
390 * - @track: the UNQUOTED track name
391 * - @index: the track number from 0
392 * - @parity: "even" or "odd" alternately
393 * - @first: "true" on the first track and "false" otherwise
394 * - @last: "true" on the last track and "false" otherwise
9faa7a88
RK
395 */
396static int exp_recent(int attribute((unused)) nargs,
397 const struct mx_node **args,
398 struct sink *output,
71634563 399 void *u) {
9faa7a88
RK
400 struct queue_entry *q;
401 int rc, i;
402
1e97629d
RK
403 dcgi_lookup(DCGI_RECENT);
404 for(q = dcgi_recent, i = 0; q; q = q->next, ++i)
71634563
RK
405 if((rc = mx_expand(mx_rewritel(args[0],
406 "id", q->id,
407 "track", q->track,
408 "index", make_index(i),
409 "parity", i % 2 ? "odd" : "even",
1e97629d 410 "first", q == dcgi_recent ? "true" : "false",
71634563
RK
411 "last", q->next ? "false" : "true",
412 (char *)0),
413 output, u)))
9faa7a88
RK
414 return rc;
415 return 0;
416}
417
418/* @new{TEMPLATE}
419 *
420 * For each track in the newly added list, expands TEMPLATE wit the following
421 * expansions:
3e1616b6
RK
422 * - @track: the UNQUOTED track name
423 * - @index: the track number from 0
424 * - @parity: "even" or "odd" alternately
425 * - @first: "true" on the first track and "false" otherwise
426 * - @last: "true" on the last track and "false" otherwise
9faa7a88 427 *
9bf61b29
RK
428 * Note that unlike @playing, @queue and @recent which are otherwise
429 * superficially similar, there is no @id sub-expansion here.
9faa7a88
RK
430 */
431static int exp_new(int attribute((unused)) nargs,
432 const struct mx_node **args,
433 struct sink *output,
71634563 434 void *u) {
9faa7a88
RK
435 int rc, i;
436
1e97629d 437 dcgi_lookup(DCGI_NEW);
9faa7a88 438 /* TODO perhaps we should generate an ID value for tracks in the new list */
1e97629d 439 for(i = 0; i < dcgi_nnew; ++i)
71634563 440 if((rc = mx_expand(mx_rewritel(args[0],
1e97629d 441 "track", dcgi_new[i],
71634563
RK
442 "index", make_index(i),
443 "parity", i % 2 ? "odd" : "even",
444 "first", i == 0 ? "true" : "false",
1e97629d 445 "last", i == dcgi_nnew - 1 ? "false" : "true",
71634563
RK
446 (char *)0),
447 output, u)))
9faa7a88
RK
448 return rc;
449 return 0;
450}
451
bca4e2b7 452/* @volume{CHANNEL}
9faa7a88
RK
453 *
454 * Expands to the volume in a given channel. CHANNEL must be "left" or
455 * "right".
456 */
457static int exp_volume(int attribute((unused)) nargs,
458 char **args,
459 struct sink *output,
460 void attribute((unused)) *u) {
1e97629d 461 dcgi_lookup(DCGI_VOLUME);
71634563
RK
462 return sink_printf(output, "%d",
463 !strcmp(args[0], "left")
1e97629d 464 ? dcgi_volume_left : dcgi_volume_right) < 0 ? -1 : 0;
9faa7a88
RK
465}
466
bca4e2b7 467/* @isplaying
9faa7a88
RK
468 *
469 * Expands to "true" if there is a playing track, otherwise "false".
470 */
471static int exp_isplaying(int attribute((unused)) nargs,
472 char attribute((unused)) **args,
473 struct sink *output,
474 void attribute((unused)) *u) {
1e97629d
RK
475 dcgi_lookup(DCGI_PLAYING);
476 return mx_bool_result(output, !!dcgi_playing);
9faa7a88
RK
477}
478
bca4e2b7 479/* @isqueue
9faa7a88
RK
480 *
481 * Expands to "true" if there the queue is nonempty, otherwise "false".
482 */
483static int exp_isqueue(int attribute((unused)) nargs,
484 char attribute((unused)) **args,
485 struct sink *output,
486 void attribute((unused)) *u) {
1e97629d
RK
487 dcgi_lookup(DCGI_QUEUE);
488 return mx_bool_result(output, !!dcgi_queue);
9faa7a88
RK
489}
490
491/* @isrecent@
492 *
493 * Expands to "true" if there the recently played list is nonempty, otherwise
494 * "false".
495 */
496static int exp_isrecent(int attribute((unused)) nargs,
497 char attribute((unused)) **args,
498 struct sink *output,
499 void attribute((unused)) *u) {
1e97629d
RK
500 dcgi_lookup(DCGI_RECENT);
501 return mx_bool_result(output, !!dcgi_recent);
9faa7a88
RK
502}
503
bca4e2b7 504/* @isnew
9faa7a88
RK
505 *
506 * Expands to "true" if there the newly added track list is nonempty, otherwise
507 * "false".
508 */
509static int exp_isnew(int attribute((unused)) nargs,
510 char attribute((unused)) **args,
511 struct sink *output,
512 void attribute((unused)) *u) {
1e97629d
RK
513 dcgi_lookup(DCGI_NEW);
514 return mx_bool_result(output, !!dcgi_nnew);
9faa7a88
RK
515}
516
bca4e2b7 517/* @pref{TRACK}{KEY}
9faa7a88
RK
518 *
519 * Expands to a track preference.
520 */
521static int exp_pref(int attribute((unused)) nargs,
522 char **args,
523 struct sink *output,
524 void attribute((unused)) *u) {
525 char *value;
526
1e97629d 527 if(dcgi_client && !disorder_get(dcgi_client, args[0], args[1], &value))
71634563
RK
528 return sink_writes(output, cgi_sgmlquote(value)) < 0 ? -1 : 0;
529 return 0;
9faa7a88
RK
530}
531
bca4e2b7 532/* @prefs{TRACK}{TEMPLATE}
9faa7a88
RK
533 *
534 * For each track preference of track TRACK, expands TEMPLATE with the
535 * following expansions:
3e1616b6
RK
536 * - @name: the UNQUOTED preference name
537 * - @index: the preference number from 0
538 * - @value: the UNQUOTED preference value
539 * - @parity: "even" or "odd" alternately
540 * - @first: "true" on the first preference and "false" otherwise
541 * - @last: "true" on the last preference and "false" otherwise
9faa7a88 542 *
9bf61b29 543 * Use @quote to quote preference names and values where necessary; see below.
9faa7a88
RK
544 */
545static int exp_prefs(int attribute((unused)) nargs,
546 const struct mx_node **args,
547 struct sink *output,
71634563 548 void *u) {
9faa7a88
RK
549 int rc, i;
550 struct kvp *k, *head;
551 char *track;
552
553 if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)")))
554 return rc;
1e97629d 555 if(!dcgi_client || disorder_prefs(dcgi_client, track, &head))
9faa7a88
RK
556 return 0;
557 for(k = head, i = 0; k; k = k->next, ++i)
71634563
RK
558 if((rc = mx_expand(mx_rewritel(args[1],
559 "index", make_index(i),
560 "parity", i % 2 ? "odd" : "even",
561 "name", k->name,
562 "value", k->value,
563 "first", k == head ? "true" : "false",
564 "last", k->next ? "false" : "true",
565 (char *)0),
566 output, u)))
9faa7a88
RK
567 return rc;
568 return 0;
569}
570
bca4e2b7 571/* @transform{TRACK}{TYPE}{CONTEXT}
9faa7a88
RK
572 *
573 * Transforms a track name (if TYPE is "track") or directory name (if type is
574 * "dir"). CONTEXT should be the context, if it is left out then "display" is
575 * assumed.
576 */
577static int exp_transform(int nargs,
578 char **args,
579 struct sink *output,
580 void attribute((unused)) *u) {
581 const char *t = trackname_transform(args[1], args[0],
71634563
RK
582 (nargs > 2 ? args[2] : "display"));
583 return sink_writes(output, cgi_sgmlquote(t)) < 0 ? -1 : 0;
9faa7a88
RK
584}
585
586/* @enabled@
587 *
588 * Expands to "true" if playing is enabled, otherwise "false".
589 */
590static int exp_enabled(int attribute((unused)) nargs,
591 char attribute((unused)) **args,
592 struct sink *output,
593 void attribute((unused)) *u) {
1e97629d 594 int e = 0;
9faa7a88 595
1e97629d
RK
596 if(dcgi_client)
597 disorder_enabled(dcgi_client, &e);
598 return mx_bool_result(output, e);
9faa7a88
RK
599}
600
bca4e2b7 601/* @random-enabled
9faa7a88
RK
602 *
603 * Expands to "true" if random play is enabled, otherwise "false".
604 */
71634563
RK
605static int exp_random_enabled(int attribute((unused)) nargs,
606 char attribute((unused)) **args,
607 struct sink *output,
608 void attribute((unused)) *u) {
1e97629d 609 int e = 0;
9faa7a88 610
1e97629d
RK
611 if(dcgi_client)
612 disorder_random_enabled(dcgi_client, &e);
613 return mx_bool_result(output, e);
9faa7a88
RK
614}
615
04024c2c 616/* @trackstate{TRACK}
9faa7a88
RK
617 *
618 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
619 * the queue, otherwise to nothing.
620 */
621static int exp_trackstate(int attribute((unused)) nargs,
622 char **args,
623 struct sink *output,
624 void attribute((unused)) *u) {
625 char *track;
626 struct queue_entry *q;
627
1e97629d 628 if(!dcgi_client)
9faa7a88 629 return 0;
1e97629d 630 if(disorder_resolve(dcgi_client, &track, args[0]))
9faa7a88 631 return 0;
1e97629d
RK
632 dcgi_lookup(DCGI_PLAYING);
633 if(dcgi_playing && !strcmp(track, dcgi_playing->track))
71634563 634 return sink_writes(output, "playing") < 0 ? -1 : 0;
1e97629d
RK
635 dcgi_lookup(DCGI_QUEUE);
636 for(q = dcgi_queue; q; q = q->next)
9faa7a88 637 if(!strcmp(track, q->track))
71634563 638 return sink_writes(output, "queued") < 0 ? -1 : 0;
9faa7a88
RK
639 return 0;
640}
641
bca4e2b7 642/* @thisurl
9faa7a88
RK
643 *
644 * Expands to an UNQUOTED URL which points back to the current page. (NB it
645 * might not be byte for byte identical - for instance, CGI arguments might be
646 * re-ordered.)
647 */
648static int exp_thisurl(int attribute((unused)) nargs,
649 char attribute((unused)) **args,
650 struct sink *output,
651 void attribute((unused)) *u) {
71634563 652 return sink_writes(output, cgi_thisurl(config->url)) < 0 ? -1 : 0;
9faa7a88
RK
653}
654
bca4e2b7 655/* @resolve{TRACK}
9faa7a88
RK
656 *
657 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
658 * nothing if it is not a valid track.
659 */
660static int exp_resolve(int attribute((unused)) nargs,
661 char **args,
662 struct sink *output,
663 void attribute((unused)) *u) {
664 char *r;
665
1e97629d 666 if(dcgi_client && !disorder_resolve(dcgi_client, &r, args[0]))
71634563 667 return sink_writes(output, r) < 0 ? -1 : 0;
9faa7a88
RK
668 return 0;
669}
670
bca4e2b7 671/* @paused
9faa7a88
RK
672 *
673 * Expands to "true" if the playing track is paused, to "false" if it is
674 * playing (or if there is no playing track at all).
675 */
676static int exp_paused(int attribute((unused)) nargs,
677 char attribute((unused)) **args,
678 struct sink *output,
48e472fa 679 void attribute((unused)) *u) {
1e97629d
RK
680 dcgi_lookup(DCGI_PLAYING);
681 return mx_bool_result(output, (dcgi_playing
682 && dcgi_playing->state == playing_paused));
9faa7a88
RK
683}
684
685/* @state{ID}@
686 *
687 * Expands to the current state of track ID.
688 */
689static int exp_state(int attribute((unused)) nargs,
690 char **args,
691 struct sink *output,
692 void attribute((unused)) *u) {
a2c4ad5f 693 struct queue_entry *q = dcgi_findtrack(args[0]);
9faa7a88
RK
694
695 if(q)
71634563 696 return sink_writes(output, playing_states[q->state]) < 0 ? -1 : 0;
9faa7a88
RK
697 return 0;
698}
699
700/* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
701 *
702 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
703 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
704 *
705 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
706 * the user has the right and "false" otherwise.
707 *
708 * If there is no connection to the server then expands to nothing (in all
709 * cases).
710 */
711static int exp_right(int nargs,
712 const struct mx_node **args,
713 struct sink *output,
71634563 714 void *u) {
9faa7a88
RK
715 char *right;
716 rights_type r;
71634563 717 int rc;
9faa7a88 718
1e97629d 719 if(!dcgi_client)
9faa7a88 720 return 0;
1e97629d 721 dcgi_lookup(DCGI_RIGHTS);
71634563 722 if((rc = mx_expandstr(args[0], &right, u, "argument #0 (RIGHT)")))
9faa7a88
RK
723 return rc;
724 if(parse_rights(right, &r, 1/*report*/))
725 return 0;
726 /* Single-argument form */
727 if(nargs == 1)
1e97629d 728 return mx_bool_result(output, !!(r & dcgi_rights));
9faa7a88 729 /* Multiple argument form */
1e97629d 730 if(r & dcgi_rights)
71634563 731 return mx_expand(args[1], output, u);
9faa7a88 732 if(nargs == 3)
71634563 733 return mx_expand(args[2], output, u);
9faa7a88
RK
734 return 0;
735}
736
bca4e2b7 737/* @userinfo{PROPERTY}
9faa7a88
RK
738 *
739 * Expands to the named property of the current user.
740 */
741static int exp_userinfo(int attribute((unused)) nargs,
742 char **args,
743 struct sink *output,
744 void attribute((unused)) *u) {
745 char *v;
746
1e97629d
RK
747 if(dcgi_client
748 && !disorder_userinfo(dcgi_client, disorder_user(dcgi_client),
749 args[0], &v))
71634563 750 return sink_writes(output, v) < 0 ? -1 : 0;
9faa7a88
RK
751 return 0;
752}
753
bca4e2b7
RK
754/* @error
755 *
756 * Expands to the latest error string.
757 */
758static int exp_error(int attribute((unused)) nargs,
759 char attribute((unused)) **args,
760 struct sink *output,
761 void attribute((unused)) *u) {
343d9c2b
RK
762 return sink_writes(output, dcgi_error_string ? dcgi_error_string : "")
763 < 0 ? -1 : 0;
764}
765
3e1616b6 766/* @status
343d9c2b
RK
767 *
768 * Expands to the latest status string.
769 */
770static int exp_status(int attribute((unused)) nargs,
771 char attribute((unused)) **args,
772 struct sink *output,
773 void attribute((unused)) *u) {
774 return sink_writes(output, dcgi_status_string ? dcgi_status_string : "")
775 < 0 ? -1 : 0;
bca4e2b7
RK
776}
777
30d85588
RK
778/* @image{NAME}
779 *
780 * Expands to the URL of the image called NAME.
781 *
782 * Firstly if there is a label called images.NAME then the image stem will be
783 * the value of that label. Otherwise the stem will be NAME.png.
784 *
785 * If the label url.static exists then it will give the base URL for images.
786 * Otherwise the base url will be /disorder/.
787 */
788static int exp_image(int attribute((unused)) nargs,
789 char **args,
790 struct sink *output,
791 void attribute((unused)) *u) {
792 const char *url, *stem;
793 char *labelname;
794
795 /* Compute the stem */
796 byte_xasprintf(&labelname, "images.%s", args[0]);
797 if(option_label_exists(labelname))
798 stem = option_label(labelname);
799 else
800 byte_xasprintf((char **)&stem, "%s.png", args[0]);
801 /* If the stem looks like it's reasonalby complete, use that */
802 if(stem[0] == '/'
803 || !strncmp(stem, "http:", 5)
804 || !strncmp(stem, "https:", 6))
805 url = stem;
806 else {
807 /* Compute the URL */
808 if(option_label_exists("url.static"))
809 byte_xasprintf((char **)&url, "%s/%s",
810 option_label("url.static"), stem);
811 else
812 /* Default base is /disorder */
813 byte_xasprintf((char **)&url, "/disorder/%s", stem);
814 }
815 return sink_writes(output, cgi_sgmlquote(url)) < 0 ? -1 : 0;
816}
817
9bf61b29
RK
818/** @brief Entry in a list of tracks or directories */
819struct entry {
820 /** @brief Track name */
821 const char *track;
822 /** @brief Sort key */
823 const char *sort;
824 /** @brief Display key */
825 const char *display;
826};
827
828/** @brief Compare two @ref entry objects */
829static int compare_entry(const void *a, const void *b) {
830 const struct entry *ea = a, *eb = b;
831
832 return compare_tracks(ea->sort, eb->sort,
833 ea->display, eb->display,
834 ea->track, eb->track);
835}
836
837/** @brief Implementation of exp_tracks() and exp_dirs() */
838static int exp__files_dirs(int nargs,
839 const struct mx_node **args,
840 struct sink *output,
841 void *u,
842 const char *type,
843 int (*fn)(disorder_client *client,
844 const char *name,
845 const char *re,
846 char ***vecp,
847 int *nvecp)) {
848 char **tracks, *dir, *re;
849 int n, ntracks, rc;
850 const struct mx_node *m;
851 struct entry *e;
852
853 if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)")))
854 return rc;
855 if(nargs == 3) {
856 if((rc = mx_expandstr(args[1], &re, u, "argument #1 (RE)")))
857 return rc;
858 m = args[2];
859 } else {
860 re = 0;
861 m = args[1];
862 }
863 if(!dcgi_client)
864 return 0;
865 /* Get the list */
866 if(fn(dcgi_client, dir, re, &tracks, &ntracks))
867 return 0;
868 /* Sort it. NB trackname_transform() does not go to the server. */
869 e = xcalloc(ntracks, sizeof *e);
870 for(n = 0; n < ntracks; ++n) {
871 e->track = tracks[n];
872 e[n].track = tracks[n];
873 e[n].sort = trackname_transform(type, tracks[n], "sort");
874 e[n].display = trackname_transform(type, tracks[n], "display");
875 }
876 qsort(e, ntracks, sizeof (struct entry), compare_entry);
48e472fa
RK
877 /* Expand the subsiduary templates. We chuck in @sort and @display because
878 * it is particularly easy to do so. */
9bf61b29
RK
879 for(n = 0; n < ntracks; ++n)
880 if((rc = mx_expand(mx_rewritel(m,
881 "index", make_index(n),
882 "parity", n % 2 ? "odd" : "even",
883 "track", tracks[n],
884 "first", n == 0 ? "true" : "false",
885 "last", n + 1 == ntracks ? "false" : "true",
48e472fa
RK
886 "sort", e[n].sort,
887 "display", e[n].display,
9bf61b29
RK
888 (char *)0),
889 output, u)))
890 return rc;
891 return 0;
892
893}
894
895/** @tracks{DIR}{RE}{TEMPLATE}
896 *
897 * For each track below DIR, expands TEMPLATE with the
898 * following expansions:
3e1616b6
RK
899 * - @track: the UNQUOTED track name
900 * - @index: the track number from 0
901 * - @parity: "even" or "odd" alternately
902 * - @first: "true" on the first track and "false" otherwise
903 * - @last: "true" on the last track and "false" otherwise
904 * - @sort: the sort key for this track
905 * - @display: the UNQUOTED display string for this track
9bf61b29
RK
906 *
907 * RE is optional and if present is the regexp to match against.
908 */
909static int exp_tracks(int nargs,
910 const struct mx_node **args,
911 struct sink *output,
912 void *u) {
913 return exp__files_dirs(nargs, args, output, u, "track", disorder_files);
914}
915
916/** @dirs{DIR}{RE}{TEMPLATE}
917 *
918 * For each directory below DIR, expands TEMPLATE with the
919 * following expansions:
3e1616b6
RK
920 * - @track: the UNQUOTED directory name
921 * - @index: the directory number from 0
922 * - @parity: "even" or "odd" alternately
923 * - @first: "true" on the first directory and "false" otherwise
924 * - @last: "true" on the last directory and "false" otherwise
925 * - @sort: the sort key for this directory
926 * - @display: the UNQUOTED display string for this directory
9bf61b29
RK
927 *
928 * RE is optional and if present is the regexp to match against.
929 */
930static int exp_dirs(int nargs,
931 const struct mx_node **args,
932 struct sink *output,
933 void *u) {
934 return exp__files_dirs(nargs, args, output, u, "dir", disorder_directories);
935}
936
b43dc0cc
RK
937static int exp__search_shim(disorder_client *c, const char *terms,
938 const char attribute((unused)) *re,
939 char ***vecp, int *nvecp) {
940 return disorder_search(c, terms, vecp, nvecp);
941}
942
943/** @search{KEYWORDS}{TEMPLATE}
944 *
945 * For each track matching KEYWORDS, expands TEMPLATE with the
946 * following expansions:
3e1616b6
RK
947 * - @track: the UNQUOTED directory name
948 * - @index: the directory number from 0
949 * - @parity: "even" or "odd" alternately
950 * - @first: "true" on the first directory and "false" otherwise
951 * - @last: "true" on the last directory and "false" otherwise
952 * - @sort: the sort key for this track
953 * - @display: the UNQUOTED display string for this track
b43dc0cc
RK
954 */
955static int exp_search(int nargs,
99955407
RK
956 const struct mx_node **args,
957 struct sink *output,
958 void *u) {
b43dc0cc
RK
959 return exp__files_dirs(nargs, args, output, u, "track", exp__search_shim);
960}
961
99955407
RK
962static int exp_label(int attribute((unused)) nargs,
963 char **args,
964 struct sink *output,
965 void attribute((unused)) *u) {
966 return sink_writes(output, option_label(args[0])) < 0 ? -1 : 0;
967}
968
9faa7a88 969/** @brief Register DisOrder-specific expansions */
1e97629d 970void dcgi_expansions(void) {
71634563 971 mx_register("arg", 1, 1, exp_arg);
48e472fa 972 mx_register("argq", 1, 1, exp_argq);
71634563
RK
973 mx_register("enabled", 0, 0, exp_enabled);
974 mx_register("error", 0, 0, exp_error);
30d85588 975 mx_register("image", 1, 1, exp_image);
71634563
RK
976 mx_register("isnew", 0, 0, exp_isnew);
977 mx_register("isplaying", 0, 0, exp_isplaying);
99955407 978 mx_register("isqueue", 0, 0, exp_isqueue);
71634563 979 mx_register("isrecent", 0, 0, exp_isrecent);
99955407 980 mx_register("label", 1, 1, exp_label);
71634563 981 mx_register("length", 1, 1, exp_length);
99955407 982 mx_register("movable", 1, 2, exp_movable);
71634563
RK
983 mx_register("part", 2, 3, exp_part);
984 mx_register("paused", 0, 0, exp_paused);
985 mx_register("pref", 2, 2, exp_pref);
986 mx_register("quote", 1, 1, exp_quote);
987 mx_register("random-enabled", 0, 0, exp_random_enabled);
988 mx_register("removable", 1, 1, exp_removable);
989 mx_register("resolve", 1, 1, exp_resolve);
990 mx_register("server-version", 0, 0, exp_server_version);
991 mx_register("state", 1, 1, exp_state);
343d9c2b 992 mx_register("status", 0, 0, exp_status);
71634563
RK
993 mx_register("thisurl", 0, 0, exp_thisurl);
994 mx_register("trackstate", 1, 1, exp_trackstate);
995 mx_register("transform", 2, 3, exp_transform);
996 mx_register("url", 0, 0, exp_url);
997 mx_register("user", 0, 0, exp_user);
998 mx_register("userinfo", 1, 1, exp_userinfo);
999 mx_register("version", 0, 0, exp_version);
1000 mx_register("volume", 1, 1, exp_volume);
1001 mx_register("when", 1, 1, exp_when);
1002 mx_register("who", 1, 1, exp_who);
9bf61b29 1003 mx_register_magic("dirs", 2, 3, exp_dirs);
71634563
RK
1004 mx_register_magic("new", 1, 1, exp_new);
1005 mx_register_magic("playing", 0, 1, exp_playing);
1006 mx_register_magic("prefs", 2, 2, exp_prefs);
1007 mx_register_magic("queue", 1, 1, exp_queue);
1008 mx_register_magic("recent", 1, 1, exp_recent);
1009 mx_register_magic("right", 1, 3, exp_right);
b43dc0cc 1010 mx_register_magic("search", 2, 2, exp_search);
9bf61b29 1011 mx_register_magic("tracks", 2, 3, exp_tracks);
bca4e2b7
RK
1012}
1013
9faa7a88
RK
1014/*
1015Local Variables:
1016c-basic-offset:2
1017comment-column:40
1018fill-column:79
1019indent-tabs-mode:nil
1020End:
1021*/