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