2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
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.
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.
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
20 /** @file server/macros-disorder.c
21 * @brief DisOrder-specific expansions
24 #include "disorder-cgi.h"
26 /** @brief For error template */
27 char *dcgi_error_string
;
29 /** @brief Return @p i as a string */
30 static const char *make_index(int i
) {
33 byte_xasprintf(&s
, "%d", i
);
39 * Expands to the server's version string, or a (safe to use) error
40 * value if the server is unavailable or broken.
42 static int exp_server_version(int attribute((unused
)) nargs
,
43 char attribute((unused
)) **args
,
45 void attribute((unused
)) *u
) {
49 if(disorder_version(dcgi_client
, (char **)&v
))
50 v
= "(cannot get version)";
52 v
= "(server not running)";
53 return sink_writes(output
, cgi_sgmlquote(v
)) < 0 ?
-1 : 0;
58 * Expands to the local version string.
60 static int exp_version(int attribute((unused
)) nargs
,
61 char attribute((unused
)) **args
,
63 void attribute((unused
)) *u
) {
64 return sink_writes(output
,
65 cgi_sgmlquote(disorder_short_version_string
)) < 0 ?
-1 : 0;
70 * Expands to the base URL of the web interface.
72 static int exp_url(int attribute((unused
)) nargs
,
73 char attribute((unused
)) **args
,
75 void attribute((unused
)) *u
) {
76 return sink_writes(output
,
77 cgi_sgmlquote(config
->url
)) < 0 ?
-1 : 0;
82 * Expands to the CGI argument NAME, or the empty string if there is
85 static int exp_arg(int attribute((unused
)) nargs
,
88 void attribute((unused
)) *u
) {
89 const char *s
= cgi_get(args
[0]);
92 return sink_writes(output
,
93 cgi_sgmlquote(s
)) < 0 ?
-1 : 0;
100 * Expands to the logged-in username (which might be "guest"), or to
101 * the empty string if not connected.
103 static int exp_user(int attribute((unused
)) nargs
,
104 char attribute((unused
)) **args
,
106 void attribute((unused
)) *u
) {
109 if(dcgi_client
&& (user
= disorder_user(dcgi_client
)))
110 return sink_writes(output
, cgi_sgmlquote(user
)) < 0 ?
-1 : 0;
114 /* @part{TRACK|ID}{PART}{CONTEXT}
116 * Expands to a track name part.
118 * A track may be identified by name or by queue ID.
120 * CONTEXT may be omitted. If it is then 'display' is assumed.
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.
126 static int exp_part(int nargs
,
129 void attribute((unused
)) *u
) {
130 const char *track
= args
[0], *part
= args
[1];
131 const char *context
= nargs
> 2 ? args
[2] : "display";
134 if(track
[0] != '/') {
135 struct queue_entry
*q
= dcgi_findtrack(track
);
143 && !disorder_part(dcgi_client
, &s
,
145 !strcmp(context
, "short") ?
"display" : context
,
147 return sink_writes(output
, cgi_sgmlquote(s
)) < 0 ?
-1 : 0;
153 * SGML-quotes STRING. Note that most expansion results are already suitable
154 * quoted, so this expansion is usually not required.
156 static int exp_quote(int attribute((unused
)) nargs
,
159 void attribute((unused
)) *u
) {
160 return sink_writes(output
, cgi_sgmlquote(args
[0])) < 0 ?
-1 : 0;
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.
168 static int exp_who(int attribute((unused
)) nargs
,
171 void attribute((unused
)) *u
) {
172 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
174 if(q
&& q
->submitter
)
175 return sink_writes(output
, cgi_sgmlquote(q
->submitter
)) < 0 ?
-1 : 0;
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.
184 static int exp_when(int attribute((unused
)) nargs
,
187 void attribute((unused
)) *u
) {
188 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
189 const struct tm
*w
= 0;
193 case playing_isscratch
:
194 case playing_unplayed
:
197 w
= localtime(&q
->expected
);
200 case playing_no_player
:
202 case playing_scratched
:
203 case playing_started
:
205 case playing_quitting
:
207 w
= localtime(&q
->played
);
211 return sink_printf(output
, "%d:%02d", w
->tm_hour
, w
->tm_min
) < 0 ?
-1 : 0;
213 return sink_writes(output
, " ") < 0 ?
-1 : 0;
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
222 static int exp_length(int attribute((unused
)) nargs
,
225 void attribute((unused
)) *u
) {
226 struct queue_entry
*q
;
230 if(args
[0][0] == '/')
231 /* Track identified by name */
234 /* Track identified by queue ID */
235 if(!(q
= dcgi_findtrack(args
[0])))
237 if(q
->state
== playing_started
|| q
->state
== playing_paused
)
238 if(sink_printf(output
, "%ld:%02ld/", q
->sofar
/ 60, q
->sofar
% 60) < 0)
242 if(dcgi_client
&& disorder_length(dcgi_client
, name
, &length
))
243 return sink_printf(output
, "%ld:%02ld",
244 length
/ 60, length
% 60) < 0 ?
-1 : 0;
245 return sink_writes(output
, " ") < 0 ?
-1 : 0;
250 * Expands to "true" if track ID is removable (or scratchable, if it is the
251 * playing track) and "false" otherwise.
253 static int exp_removable(int attribute((unused
)) nargs
,
256 void attribute((unused
)) *u
) {
257 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
258 /* TODO would be better to reject recent */
260 if(!q
|| !dcgi_client
)
261 return mx_bool_result(output
, 0);
262 dcgi_lookup(DCGI_RIGHTS
);
263 return mx_bool_result(output
,
264 (q
== dcgi_playing ? right_scratchable
: right_removable
)
265 (dcgi_rights
, disorder_user(dcgi_client
), q
));
270 * Expands to "true" if track ID is movable and "false" otherwise.
272 * DIR (which is optional) should be a non-zero integer. If it is negative
273 * then the intended move is down (later in the queue), if it is positive then
274 * the intended move is up (earlier in the queue). The first track is not
275 * movable up and the last track not movable down.
277 static int exp_movable(int nargs
,
280 void attribute((unused
)) *u
) {
281 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
282 /* TODO would be better to recent playing/recent */
284 if(!q
|| !dcgi_client
)
285 return mx_bool_result(output
, 0);
287 const long dir
= atoi(args
[1]);
289 if(dir
> 0 && q
== dcgi_queue
)
290 return mx_bool_result(output
, 0);
291 if(dir
< 0 && q
->next
== 0)
292 return mx_bool_result(output
, 0);
294 dcgi_lookup(DCGI_RIGHTS
);
295 return mx_bool_result(output
,
296 right_movable(dcgi_rights
,
297 disorder_user(dcgi_client
),
301 /* @playing{TEMPLATE}
303 * Expands to TEMPLATE, with:
304 * - @id@ expanded to the queue ID of the playing track
305 * - @track@ expanded to its UNQUOTED name
307 * If no track is playing expands to nothing.
309 * TEMPLATE is optional. If it is left out then instead expands to the queue
310 * ID of the playing track.
312 static int exp_playing(int nargs
,
313 const struct mx_node
**args
,
316 dcgi_lookup(DCGI_PLAYING
);
320 return sink_writes(output
, dcgi_playing
->id
) < 0 ?
-1 : 0;
321 return mx_expand(mx_rewritel(args
[0],
322 "id", dcgi_playing
->id
,
323 "track", dcgi_playing
->track
,
330 * For each track in the queue, expands TEMPLATE with the following expansions:
331 * - @id@ to the queue ID of the track
332 * - @track@ to the UNQUOTED track name
333 * - @index@ to the track number from 0
334 * - @parity@ to "even" or "odd" alternately
335 * - @first@ to "true" on the first track and "false" otherwise
336 * - @last@ to "true" on the last track and "false" otherwise
338 static int exp_queue(int attribute((unused
)) nargs
,
339 const struct mx_node
**args
,
342 struct queue_entry
*q
;
345 dcgi_lookup(DCGI_QUEUE
);
346 for(q
= dcgi_queue
, i
= 0; q
; q
= q
->next
, ++i
)
347 if((rc
= mx_expand(mx_rewritel(args
[0],
350 "index", make_index(i
),
351 "parity", i
% 2 ?
"odd" : "even",
352 "first", q
== dcgi_queue ?
"true" : "false",
353 "last", q
->next ?
"false" : "true",
362 * For each track in the recently played list, expands TEMPLATE with the
363 * following expansions:
364 * - @id@ to the queue ID of the track
365 * - @track@ to the UNQUOTED track name
366 * - @index@ to the track number from 0
367 * - @parity@ to "even" or "odd" alternately
368 * - @first@ to "true" on the first track and "false" otherwise
369 * - @last@ to "true" on the last track and "false" otherwise
371 static int exp_recent(int attribute((unused
)) nargs
,
372 const struct mx_node
**args
,
375 struct queue_entry
*q
;
378 dcgi_lookup(DCGI_RECENT
);
379 for(q
= dcgi_recent
, i
= 0; q
; q
= q
->next
, ++i
)
380 if((rc
= mx_expand(mx_rewritel(args
[0],
383 "index", make_index(i
),
384 "parity", i
% 2 ?
"odd" : "even",
385 "first", q
== dcgi_recent ?
"true" : "false",
386 "last", q
->next ?
"false" : "true",
395 * For each track in the newly added list, expands TEMPLATE wit the following
397 * - @track@ to the UNQUOTED track name
398 * - @index@ to the track number from 0
399 * - @parity@ to "even" or "odd" alternately
400 * - @first@ to "true" on the first track and "false" otherwise
401 * - @last@ to "true" on the last track and "false" otherwise
403 * Note that unlike @playing@, @queue@ and @recent@ which are otherwise
404 * superficially similar, there is no @id@ sub-expansion here.
406 static int exp_new(int attribute((unused
)) nargs
,
407 const struct mx_node
**args
,
412 dcgi_lookup(DCGI_NEW
);
413 /* TODO perhaps we should generate an ID value for tracks in the new list */
414 for(i
= 0; i
< dcgi_nnew
; ++i
)
415 if((rc
= mx_expand(mx_rewritel(args
[0],
416 "track", dcgi_new
[i
],
417 "index", make_index(i
),
418 "parity", i
% 2 ?
"odd" : "even",
419 "first", i
== 0 ?
"true" : "false",
420 "last", i
== dcgi_nnew
- 1 ?
"false" : "true",
429 * Expands to the volume in a given channel. CHANNEL must be "left" or
432 static int exp_volume(int attribute((unused
)) nargs
,
435 void attribute((unused
)) *u
) {
436 dcgi_lookup(DCGI_VOLUME
);
437 return sink_printf(output
, "%d",
438 !strcmp(args
[0], "left")
439 ? dcgi_volume_left
: dcgi_volume_right
) < 0 ?
-1 : 0;
444 * Expands to "true" if there is a playing track, otherwise "false".
446 static int exp_isplaying(int attribute((unused
)) nargs
,
447 char attribute((unused
)) **args
,
449 void attribute((unused
)) *u
) {
450 dcgi_lookup(DCGI_PLAYING
);
451 return mx_bool_result(output
, !!dcgi_playing
);
456 * Expands to "true" if there the queue is nonempty, otherwise "false".
458 static int exp_isqueue(int attribute((unused
)) nargs
,
459 char attribute((unused
)) **args
,
461 void attribute((unused
)) *u
) {
462 dcgi_lookup(DCGI_QUEUE
);
463 return mx_bool_result(output
, !!dcgi_queue
);
468 * Expands to "true" if there the recently played list is nonempty, otherwise
471 static int exp_isrecent(int attribute((unused
)) nargs
,
472 char attribute((unused
)) **args
,
474 void attribute((unused
)) *u
) {
475 dcgi_lookup(DCGI_RECENT
);
476 return mx_bool_result(output
, !!dcgi_recent
);
481 * Expands to "true" if there the newly added track list is nonempty, otherwise
484 static int exp_isnew(int attribute((unused
)) nargs
,
485 char attribute((unused
)) **args
,
487 void attribute((unused
)) *u
) {
488 dcgi_lookup(DCGI_NEW
);
489 return mx_bool_result(output
, !!dcgi_nnew
);
494 * Expands to a track preference.
496 static int exp_pref(int attribute((unused
)) nargs
,
499 void attribute((unused
)) *u
) {
502 if(dcgi_client
&& !disorder_get(dcgi_client
, args
[0], args
[1], &value
))
503 return sink_writes(output
, cgi_sgmlquote(value
)) < 0 ?
-1 : 0;
507 /* @prefs{TRACK}{TEMPLATE}
509 * For each track preference of track TRACK, expands TEMPLATE with the
510 * following expansions:
511 * - @name@ to the UNQUOTED preference name
512 * - @index@ to the preference number from 0
513 * - @value@ to the UNQUOTED preference value
514 * - @parity@ to "even" or "odd" alternately
515 * - @first@ to "true" on the first preference and "false" otherwise
516 * - @last@ to "true" on the last preference and "false" otherwise
518 * Use @quote@ to quote preference names and values where necessary; see below.
520 static int exp_prefs(int attribute((unused
)) nargs
,
521 const struct mx_node
**args
,
525 struct kvp
*k
, *head
;
528 if((rc
= mx_expandstr(args
[0], &track
, u
, "argument #0 (TRACK)")))
530 if(!dcgi_client
|| disorder_prefs(dcgi_client
, track
, &head
))
532 for(k
= head
, i
= 0; k
; k
= k
->next
, ++i
)
533 if((rc
= mx_expand(mx_rewritel(args
[1],
534 "index", make_index(i
),
535 "parity", i
% 2 ?
"odd" : "even",
538 "first", k
== head ?
"true" : "false",
539 "last", k
->next ?
"false" : "true",
546 /* @transform{TRACK}{TYPE}{CONTEXT}
548 * Transforms a track name (if TYPE is "track") or directory name (if type is
549 * "dir"). CONTEXT should be the context, if it is left out then "display" is
552 static int exp_transform(int nargs
,
555 void attribute((unused
)) *u
) {
556 const char *t
= trackname_transform(args
[1], args
[0],
557 (nargs
> 2 ? args
[2] : "display"));
558 return sink_writes(output
, cgi_sgmlquote(t
)) < 0 ?
-1 : 0;
563 * Expands to "true" if playing is enabled, otherwise "false".
565 static int exp_enabled(int attribute((unused
)) nargs
,
566 char attribute((unused
)) **args
,
568 void attribute((unused
)) *u
) {
572 disorder_enabled(dcgi_client
, &e
);
573 return mx_bool_result(output
, e
);
578 * Expands to "true" if random play is enabled, otherwise "false".
580 static int exp_random_enabled(int attribute((unused
)) nargs
,
581 char attribute((unused
)) **args
,
583 void attribute((unused
)) *u
) {
587 disorder_random_enabled(dcgi_client
, &e
);
588 return mx_bool_result(output
, e
);
591 /* @trackstate{TRACK}
593 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
594 * the queue, otherwise to nothing.
596 static int exp_trackstate(int attribute((unused
)) nargs
,
599 void attribute((unused
)) *u
) {
601 struct queue_entry
*q
;
605 if(disorder_resolve(dcgi_client
, &track
, args
[0]))
607 dcgi_lookup(DCGI_PLAYING
);
608 if(dcgi_playing
&& !strcmp(track
, dcgi_playing
->track
))
609 return sink_writes(output
, "playing") < 0 ?
-1 : 0;
610 dcgi_lookup(DCGI_QUEUE
);
611 for(q
= dcgi_queue
; q
; q
= q
->next
)
612 if(!strcmp(track
, q
->track
))
613 return sink_writes(output
, "queued") < 0 ?
-1 : 0;
619 * Expands to an UNQUOTED URL which points back to the current page. (NB it
620 * might not be byte for byte identical - for instance, CGI arguments might be
623 static int exp_thisurl(int attribute((unused
)) nargs
,
624 char attribute((unused
)) **args
,
626 void attribute((unused
)) *u
) {
627 return sink_writes(output
, cgi_thisurl(config
->url
)) < 0 ?
-1 : 0;
632 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
633 * nothing if it is not a valid track.
635 static int exp_resolve(int attribute((unused
)) nargs
,
638 void attribute((unused
)) *u
) {
641 if(dcgi_client
&& !disorder_resolve(dcgi_client
, &r
, args
[0]))
642 return sink_writes(output
, r
) < 0 ?
-1 : 0;
648 * Expands to "true" if the playing track is paused, to "false" if it is
649 * playing (or if there is no playing track at all).
651 static int exp_paused(int attribute((unused
)) nargs
,
652 char attribute((unused
)) **args
,
654 void attribute((unused
)) *u
) {
655 dcgi_lookup(DCGI_PLAYING
);
656 return mx_bool_result(output
, (dcgi_playing
657 && dcgi_playing
->state
== playing_paused
));
662 * Expands to the current state of track ID.
664 static int exp_state(int attribute((unused
)) nargs
,
667 void attribute((unused
)) *u
) {
668 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
671 return sink_writes(output
, playing_states
[q
->state
]) < 0 ?
-1 : 0;
675 /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
677 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
678 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
680 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
681 * the user has the right and "false" otherwise.
683 * If there is no connection to the server then expands to nothing (in all
686 static int exp_right(int nargs
,
687 const struct mx_node
**args
,
696 dcgi_lookup(DCGI_RIGHTS
);
697 if((rc
= mx_expandstr(args
[0], &right
, u
, "argument #0 (RIGHT)")))
699 if(parse_rights(right
, &r
, 1/*report*/))
701 /* Single-argument form */
703 return mx_bool_result(output
, !!(r
& dcgi_rights
));
704 /* Multiple argument form */
706 return mx_expand(args
[1], output
, u
);
708 return mx_expand(args
[2], output
, u
);
712 /* @userinfo{PROPERTY}
714 * Expands to the named property of the current user.
716 static int exp_userinfo(int attribute((unused
)) nargs
,
719 void attribute((unused
)) *u
) {
723 && !disorder_userinfo(dcgi_client
, disorder_user(dcgi_client
),
725 return sink_writes(output
, v
) < 0 ?
-1 : 0;
731 * Expands to the latest error string.
733 static int exp_error(int attribute((unused
)) nargs
,
734 char attribute((unused
)) **args
,
736 void attribute((unused
)) *u
) {
737 return sink_writes(output
, cgi_sgmlquote(dcgi_error_string
)) < 0 ?
-1 : 0;
742 * Expands to the URL of the image called NAME.
744 * Firstly if there is a label called images.NAME then the image stem will be
745 * the value of that label. Otherwise the stem will be NAME.png.
747 * If the label url.static exists then it will give the base URL for images.
748 * Otherwise the base url will be /disorder/.
750 static int exp_image(int attribute((unused
)) nargs
,
753 void attribute((unused
)) *u
) {
754 const char *url
, *stem
;
757 /* Compute the stem */
758 byte_xasprintf(&labelname
, "images.%s", args
[0]);
759 if(option_label_exists(labelname
))
760 stem
= option_label(labelname
);
762 byte_xasprintf((char **)&stem
, "%s.png", args
[0]);
763 /* If the stem looks like it's reasonalby complete, use that */
765 || !strncmp(stem
, "http:", 5)
766 || !strncmp(stem
, "https:", 6))
769 /* Compute the URL */
770 if(option_label_exists("url.static"))
771 byte_xasprintf((char **)&url
, "%s/%s",
772 option_label("url.static"), stem
);
774 /* Default base is /disorder */
775 byte_xasprintf((char **)&url
, "/disorder/%s", stem
);
777 return sink_writes(output
, cgi_sgmlquote(url
)) < 0 ?
-1 : 0;
780 /** @brief Register DisOrder-specific expansions */
781 void dcgi_expansions(void) {
782 mx_register("arg", 1, 1, exp_arg
);
783 mx_register("enabled", 0, 0, exp_enabled
);
784 mx_register("error", 0, 0, exp_error
);
785 mx_register("image", 1, 1, exp_image
);
786 mx_register("isnew", 0, 0, exp_isnew
);
787 mx_register("isplaying", 0, 0, exp_isplaying
);
788 mx_register("isplaying", 0, 0, exp_isqueue
);
789 mx_register("isrecent", 0, 0, exp_isrecent
);
790 mx_register("length", 1, 1, exp_length
);
791 mx_register("movable", 1, 1, exp_movable
);
792 mx_register("part", 2, 3, exp_part
);
793 mx_register("paused", 0, 0, exp_paused
);
794 mx_register("pref", 2, 2, exp_pref
);
795 mx_register("quote", 1, 1, exp_quote
);
796 mx_register("random-enabled", 0, 0, exp_random_enabled
);
797 mx_register("removable", 1, 1, exp_removable
);
798 mx_register("resolve", 1, 1, exp_resolve
);
799 mx_register("server-version", 0, 0, exp_server_version
);
800 mx_register("state", 1, 1, exp_state
);
801 mx_register("thisurl", 0, 0, exp_thisurl
);
802 mx_register("trackstate", 1, 1, exp_trackstate
);
803 mx_register("transform", 2, 3, exp_transform
);
804 mx_register("url", 0, 0, exp_url
);
805 mx_register("user", 0, 0, exp_user
);
806 mx_register("userinfo", 1, 1, exp_userinfo
);
807 mx_register("version", 0, 0, exp_version
);
808 mx_register("volume", 1, 1, exp_volume
);
809 mx_register("when", 1, 1, exp_when
);
810 mx_register("who", 1, 1, exp_who
);
811 mx_register_magic("new", 1, 1, exp_new
);
812 mx_register_magic("playing", 0, 1, exp_playing
);
813 mx_register_magic("prefs", 2, 2, exp_prefs
);
814 mx_register_magic("queue", 1, 1, exp_queue
);
815 mx_register_magic("recent", 1, 1, exp_recent
);
816 mx_register_magic("right", 1, 3, exp_right
);