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