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