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 static int exp_movable(int attribute((unused
)) nargs
,
275 void attribute((unused
)) *u
) {
276 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
277 /* TODO would be better to recent playing/recent */
279 if(!q
|| !dcgi_client
)
280 return mx_bool_result(output
, 0);
281 dcgi_lookup(DCGI_RIGHTS
);
282 return mx_bool_result(output
,
283 right_movable(dcgi_rights
, disorder_user(dcgi_client
), q
));
286 /* @playing{TEMPLATE}
288 * Expands to TEMPLATE, with:
289 * - @id@ expanded to the queue ID of the playing track
290 * - @track@ expanded to its UNQUOTED name
292 * If no track is playing expands to nothing.
294 * TEMPLATE is optional. If it is left out then instead expands to the queue
295 * ID of the playing track.
297 static int exp_playing(int nargs
,
298 const struct mx_node
**args
,
301 dcgi_lookup(DCGI_PLAYING
);
305 return sink_writes(output
, dcgi_playing
->id
) < 0 ?
-1 : 0;
306 return mx_expand(mx_rewritel(args
[0],
307 "id", dcgi_playing
->id
,
308 "track", dcgi_playing
->track
,
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
323 static int exp_queue(int attribute((unused
)) nargs
,
324 const struct mx_node
**args
,
327 struct queue_entry
*q
;
330 dcgi_lookup(DCGI_QUEUE
);
331 for(q
= dcgi_queue
, i
= 0; q
; q
= q
->next
, ++i
)
332 if((rc
= mx_expand(mx_rewritel(args
[0],
335 "index", make_index(i
),
336 "parity", i
% 2 ?
"odd" : "even",
337 "first", q
== dcgi_queue ?
"true" : "false",
338 "last", q
->next ?
"false" : "true",
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
356 static int exp_recent(int attribute((unused
)) nargs
,
357 const struct mx_node
**args
,
360 struct queue_entry
*q
;
363 dcgi_lookup(DCGI_RECENT
);
364 for(q
= dcgi_recent
, i
= 0; q
; q
= q
->next
, ++i
)
365 if((rc
= mx_expand(mx_rewritel(args
[0],
368 "index", make_index(i
),
369 "parity", i
% 2 ?
"odd" : "even",
370 "first", q
== dcgi_recent ?
"true" : "false",
371 "last", q
->next ?
"false" : "true",
380 * For each track in the newly added list, expands TEMPLATE wit the following
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
388 * Note that unlike @playing@, @queue@ and @recent@ which are otherwise
389 * superficially similar, there is no @id@ sub-expansion here.
391 static int exp_new(int attribute((unused
)) nargs
,
392 const struct mx_node
**args
,
397 dcgi_lookup(DCGI_NEW
);
398 /* TODO perhaps we should generate an ID value for tracks in the new list */
399 for(i
= 0; i
< dcgi_nnew
; ++i
)
400 if((rc
= mx_expand(mx_rewritel(args
[0],
401 "track", dcgi_new
[i
],
402 "index", make_index(i
),
403 "parity", i
% 2 ?
"odd" : "even",
404 "first", i
== 0 ?
"true" : "false",
405 "last", i
== dcgi_nnew
- 1 ?
"false" : "true",
414 * Expands to the volume in a given channel. CHANNEL must be "left" or
417 static int exp_volume(int attribute((unused
)) nargs
,
420 void attribute((unused
)) *u
) {
421 dcgi_lookup(DCGI_VOLUME
);
422 return sink_printf(output
, "%d",
423 !strcmp(args
[0], "left")
424 ? dcgi_volume_left
: dcgi_volume_right
) < 0 ?
-1 : 0;
429 * Expands to "true" if there is a playing track, otherwise "false".
431 static int exp_isplaying(int attribute((unused
)) nargs
,
432 char attribute((unused
)) **args
,
434 void attribute((unused
)) *u
) {
435 dcgi_lookup(DCGI_PLAYING
);
436 return mx_bool_result(output
, !!dcgi_playing
);
441 * Expands to "true" if there the queue is nonempty, otherwise "false".
443 static int exp_isqueue(int attribute((unused
)) nargs
,
444 char attribute((unused
)) **args
,
446 void attribute((unused
)) *u
) {
447 dcgi_lookup(DCGI_QUEUE
);
448 return mx_bool_result(output
, !!dcgi_queue
);
453 * Expands to "true" if there the recently played list is nonempty, otherwise
456 static int exp_isrecent(int attribute((unused
)) nargs
,
457 char attribute((unused
)) **args
,
459 void attribute((unused
)) *u
) {
460 dcgi_lookup(DCGI_RECENT
);
461 return mx_bool_result(output
, !!dcgi_recent
);
466 * Expands to "true" if there the newly added track list is nonempty, otherwise
469 static int exp_isnew(int attribute((unused
)) nargs
,
470 char attribute((unused
)) **args
,
472 void attribute((unused
)) *u
) {
473 dcgi_lookup(DCGI_NEW
);
474 return mx_bool_result(output
, !!dcgi_nnew
);
479 * Expands to a track preference.
481 static int exp_pref(int attribute((unused
)) nargs
,
484 void attribute((unused
)) *u
) {
487 if(dcgi_client
&& !disorder_get(dcgi_client
, args
[0], args
[1], &value
))
488 return sink_writes(output
, cgi_sgmlquote(value
)) < 0 ?
-1 : 0;
492 /* @prefs{TRACK}{TEMPLATE}
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
503 * Use @quote@ to quote preference names and values where necessary; see below.
505 static int exp_prefs(int attribute((unused
)) nargs
,
506 const struct mx_node
**args
,
510 struct kvp
*k
, *head
;
513 if((rc
= mx_expandstr(args
[0], &track
, u
, "argument #0 (TRACK)")))
515 if(!dcgi_client
|| disorder_prefs(dcgi_client
, track
, &head
))
517 for(k
= head
, i
= 0; k
; k
= k
->next
, ++i
)
518 if((rc
= mx_expand(mx_rewritel(args
[1],
519 "index", make_index(i
),
520 "parity", i
% 2 ?
"odd" : "even",
523 "first", k
== head ?
"true" : "false",
524 "last", k
->next ?
"false" : "true",
531 /* @transform{TRACK}{TYPE}{CONTEXT}
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
537 static int exp_transform(int nargs
,
540 void attribute((unused
)) *u
) {
541 const char *t
= trackname_transform(args
[1], args
[0],
542 (nargs
> 2 ? args
[2] : "display"));
543 return sink_writes(output
, cgi_sgmlquote(t
)) < 0 ?
-1 : 0;
548 * Expands to "true" if playing is enabled, otherwise "false".
550 static int exp_enabled(int attribute((unused
)) nargs
,
551 char attribute((unused
)) **args
,
553 void attribute((unused
)) *u
) {
557 disorder_enabled(dcgi_client
, &e
);
558 return mx_bool_result(output
, e
);
563 * Expands to "true" if random play is enabled, otherwise "false".
565 static int exp_random_enabled(int attribute((unused
)) nargs
,
566 char attribute((unused
)) **args
,
568 void attribute((unused
)) *u
) {
572 disorder_random_enabled(dcgi_client
, &e
);
573 return mx_bool_result(output
, e
);
576 /* @trackstate{TRACK}
578 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
579 * the queue, otherwise to nothing.
581 static int exp_trackstate(int attribute((unused
)) nargs
,
584 void attribute((unused
)) *u
) {
586 struct queue_entry
*q
;
590 if(disorder_resolve(dcgi_client
, &track
, args
[0]))
592 dcgi_lookup(DCGI_PLAYING
);
593 if(dcgi_playing
&& !strcmp(track
, dcgi_playing
->track
))
594 return sink_writes(output
, "playing") < 0 ?
-1 : 0;
595 dcgi_lookup(DCGI_QUEUE
);
596 for(q
= dcgi_queue
; q
; q
= q
->next
)
597 if(!strcmp(track
, q
->track
))
598 return sink_writes(output
, "queued") < 0 ?
-1 : 0;
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
608 static int exp_thisurl(int attribute((unused
)) nargs
,
609 char attribute((unused
)) **args
,
611 void attribute((unused
)) *u
) {
612 return sink_writes(output
, cgi_thisurl(config
->url
)) < 0 ?
-1 : 0;
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.
620 static int exp_resolve(int attribute((unused
)) nargs
,
623 void attribute((unused
)) *u
) {
626 if(dcgi_client
&& !disorder_resolve(dcgi_client
, &r
, args
[0]))
627 return sink_writes(output
, r
) < 0 ?
-1 : 0;
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).
636 static int exp_paused(int attribute((unused
)) nargs
,
637 char attribute((unused
)) **args
,
639 void attribute((unused
)) *u
) {
640 dcgi_lookup(DCGI_PLAYING
);
641 return mx_bool_result(output
, (dcgi_playing
642 && dcgi_playing
->state
== playing_paused
));
647 * Expands to the current state of track ID.
649 static int exp_state(int attribute((unused
)) nargs
,
652 void attribute((unused
)) *u
) {
653 struct queue_entry
*q
= dcgi_findtrack(args
[0]);
656 return sink_writes(output
, playing_states
[q
->state
]) < 0 ?
-1 : 0;
660 /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
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.
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.
668 * If there is no connection to the server then expands to nothing (in all
671 static int exp_right(int nargs
,
672 const struct mx_node
**args
,
681 dcgi_lookup(DCGI_RIGHTS
);
682 if((rc
= mx_expandstr(args
[0], &right
, u
, "argument #0 (RIGHT)")))
684 if(parse_rights(right
, &r
, 1/*report*/))
686 /* Single-argument form */
688 return mx_bool_result(output
, !!(r
& dcgi_rights
));
689 /* Multiple argument form */
691 return mx_expand(args
[1], output
, u
);
693 return mx_expand(args
[2], output
, u
);
697 /* @userinfo{PROPERTY}
699 * Expands to the named property of the current user.
701 static int exp_userinfo(int attribute((unused
)) nargs
,
704 void attribute((unused
)) *u
) {
708 && !disorder_userinfo(dcgi_client
, disorder_user(dcgi_client
),
710 return sink_writes(output
, v
) < 0 ?
-1 : 0;
716 * Expands to the latest error string.
718 static int exp_error(int attribute((unused
)) nargs
,
719 char attribute((unused
)) **args
,
721 void attribute((unused
)) *u
) {
722 return sink_writes(output
, cgi_sgmlquote(dcgi_error_string
)) < 0 ?
-1 : 0;
725 /** @brief Register DisOrder-specific expansions */
726 void dcgi_expansions(void) {
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
);