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
40 #include "macros-disorder.h"
44 #include "configuration.h"
45 #include "trackname.h"
47 /** @brief For error template */
50 /** @brief Locate a track by ID */
51 static struct queue_entry
*findtrack(const char *id
) {
52 struct queue_entry
*q
;
55 if(playing
&& !strcmp(playing
->id
, id
))
58 for(q
= queue
; q
; q
= q
->next
)
59 if(!strcmp(q
->id
, id
))
62 for(q
= recent
; q
; q
= q
->next
)
63 if(!strcmp(q
->id
, id
))
68 /** @brief Return @p i as a string */
69 static const char *make_index(int i
) {
72 byte_xasprintf(&s
, "%d", i
);
78 * Expands to the server's version string, or a (safe to use) error
79 * value if the server is unavailable or broken.
81 static int exp_server_version(int attribute((unused
)) nargs
,
82 char attribute((unused
)) **args
,
84 void attribute((unused
)) *u
) {
88 if(disorder_version(client
, (char **)&v
))
89 v
= "(cannot get version)";
91 v
= "(server not running)";
92 return sink_writes(output
, cgi_sgmlquote(v
)) < 0 ?
-1 : 0;
97 * Expands to the local version string.
99 static int exp_version(int attribute((unused
)) nargs
,
100 char attribute((unused
)) **args
,
102 void attribute((unused
)) *u
) {
103 return sink_writes(output
,
104 cgi_sgmlquote(disorder_short_version_string
)) < 0 ?
-1 : 0;
109 * Expands to the base URL of the web interface.
111 static int exp_url(int attribute((unused
)) nargs
,
112 char attribute((unused
)) **args
,
114 void attribute((unused
)) *u
) {
115 return sink_writes(output
,
116 cgi_sgmlquote(config
->url
)) < 0 ?
-1 : 0;
121 * Expands to the CGI argument NAME, or the empty string if there is
124 static int exp_arg(int attribute((unused
)) nargs
,
127 void attribute((unused
)) *u
) {
128 const char *s
= cgi_get(args
[0]);
130 return sink_writes(output
,
131 cgi_sgmlquote(s
)) < 0 ?
-1 : 0;
138 * Expands to the logged-in username (which might be "guest"), or to
139 * the empty string if not connected.
141 static int exp_user(int attribute((unused
)) nargs
,
142 char attribute((unused
)) **args
,
144 void attribute((unused
)) *u
) {
147 if(client
&& (user
= disorder_user(client
)))
148 return sink_writes(output
, cgi_sgmlquote(user
)) < 0 ?
-1 : 0;
152 /* @part{TRACK|ID}{PART}{CONTEXT}
154 * Expands to a track name part.
156 * A track may be identified by name or by queue ID.
158 * CONTEXT may be omitted. If it is then 'display' is assumed.
160 * If the CONTEXT is 'short' then the 'display' part is looked up, and the
161 * result truncated according to the length defined by the short_display
162 * configuration directive.
164 static int exp_part(int nargs
,
167 void attribute((unused
)) *u
) {
168 const char *track
= args
[0], *part
= args
[1];
169 const char *context
= nargs
> 2 ? args
[2] : "display";
172 if(track
[0] != '/') {
173 struct queue_entry
*q
= findtrack(track
);
181 && !disorder_part(client
, &s
,
183 !strcmp(context
, "short") ?
"display" : context
,
185 return sink_writes(output
, cgi_sgmlquote(s
)) < 0 ?
-1 : 0;
191 * SGML-quotes STRING. Note that most expansion results are already suitable
192 * quoted, so this expansion is usually not required.
194 static int exp_quote(int attribute((unused
)) nargs
,
197 void attribute((unused
)) *u
) {
198 return sink_writes(output
, cgi_sgmlquote(args
[0])) < 0 ?
-1 : 0;
203 * Expands to the name of the submitter of track ID, which must be a playing
204 * track, in the queue, or in the recent list.
206 static int exp_who(int attribute((unused
)) nargs
,
209 void attribute((unused
)) *u
) {
210 struct queue_entry
*q
= findtrack(args
[0]);
212 if(q
&& q
->submitter
)
213 return sink_writes(output
, cgi_sgmlquote(q
->submitter
)) < 0 ?
-1 : 0;
219 * Expands to the time a track started or is expected to start. The track must
220 * be a playing track, in the queue, or in the recent list.
222 static int exp_when(int attribute((unused
)) nargs
,
225 void attribute((unused
)) *u
) {
226 struct queue_entry
*q
= findtrack(args
[0]);
227 const struct tm
*w
= 0;
231 case playing_isscratch
:
232 case playing_unplayed
:
235 w
= localtime(&q
->expected
);
238 case playing_no_player
:
240 case playing_scratched
:
241 case playing_started
:
243 case playing_quitting
:
245 w
= localtime(&q
->played
);
249 return sink_printf(output
, "%d:%02d", w
->tm_hour
, w
->tm_min
) < 0 ?
-1 : 0;
251 return sink_writes(output
, " ") < 0 ?
-1 : 0;
256 * Expands to the length of a track, identified by its queue ID or its name.
257 * If it is the playing track (identified by ID) then the amount played so far
260 static int exp_length(int attribute((unused
)) nargs
,
263 void attribute((unused
)) *u
) {
264 struct queue_entry
*q
;
268 if(args
[0][0] == '/')
269 /* Track identified by name */
272 /* Track identified by queue ID */
273 if(!(q
= findtrack(args
[0])))
275 if(q
->state
== playing_started
|| q
->state
== playing_paused
)
276 if(sink_printf(output
, "%ld:%02ld/", q
->sofar
/ 60, q
->sofar
% 60) < 0)
280 if(client
&& disorder_length(client
, name
, &length
))
281 return sink_printf(output
, "%ld:%02ld",
282 length
/ 60, length
% 60) < 0 ?
-1 : 0;
283 return sink_writes(output
, " ") < 0 ?
-1 : 0;
288 * Expands to "true" if track ID is removable (or scratchable, if it is the
289 * playing track) and "false" otherwise.
291 static int exp_removable(int attribute((unused
)) nargs
,
294 void attribute((unused
)) *u
) {
295 struct queue_entry
*q
= findtrack(args
[0]);
296 /* TODO would be better to reject recent */
299 return mx_bool_result(output
, 0);
301 return mx_bool_result(output
,
302 (q
== playing ? right_scratchable
: right_removable
)
303 (rights
, disorder_user(client
), q
));
308 * Expands to "true" if track ID is movable and "false" otherwise.
310 static int exp_movable(int attribute((unused
)) nargs
,
313 void attribute((unused
)) *u
) {
314 struct queue_entry
*q
= findtrack(args
[0]);
315 /* TODO would be better to recent playing/recent */
318 return mx_bool_result(output
, 0);
320 return mx_bool_result(output
,
321 right_movable(rights
, disorder_user(client
), q
));
324 /* @playing{TEMPLATE}
326 * Expands to TEMPLATE, with:
327 * - @id@ expanded to the queue ID of the playing track
328 * - @track@ expanded to its UNQUOTED name
330 * If no track is playing expands to nothing.
332 * TEMPLATE is optional. If it is left out then instead expands to the queue
333 * ID of the playing track.
335 static int exp_playing(int nargs
,
336 const struct mx_node
**args
,
343 return sink_writes(output
, playing
->id
) < 0 ?
-1 : 0;
344 return mx_expand(mx_rewritel(args
[0],
346 "track", playing
->track
,
353 * For each track in the queue, expands TEMPLATE with the following expansions:
354 * - @id@ to the queue ID of the track
355 * - @track@ to the UNQUOTED track name
356 * - @index@ to the track number from 0
357 * - @parity@ to "even" or "odd" alternately
358 * - @first@ to "true" on the first track and "false" otherwise
359 * - @last@ to "true" on the last track and "false" otherwise
361 static int exp_queue(int attribute((unused
)) nargs
,
362 const struct mx_node
**args
,
365 struct queue_entry
*q
;
369 for(q
= queue
, i
= 0; q
; q
= q
->next
, ++i
)
370 if((rc
= mx_expand(mx_rewritel(args
[0],
373 "index", make_index(i
),
374 "parity", i
% 2 ?
"odd" : "even",
375 "first", q
== queue ?
"true" : "false",
376 "last", q
->next ?
"false" : "true",
385 * For each track in the recently played list, expands TEMPLATE with the
386 * following expansions:
387 * - @id@ to the queue ID of the track
388 * - @track@ to the UNQUOTED track name
389 * - @index@ to the track number from 0
390 * - @parity@ to "even" or "odd" alternately
391 * - @first@ to "true" on the first track and "false" otherwise
392 * - @last@ to "true" on the last track and "false" otherwise
394 static int exp_recent(int attribute((unused
)) nargs
,
395 const struct mx_node
**args
,
398 struct queue_entry
*q
;
402 for(q
= recent
, i
= 0; q
; q
= q
->next
, ++i
)
403 if((rc
= mx_expand(mx_rewritel(args
[0],
406 "index", make_index(i
),
407 "parity", i
% 2 ?
"odd" : "even",
408 "first", q
== recent ?
"true" : "false",
409 "last", q
->next ?
"false" : "true",
418 * For each track in the newly added list, expands TEMPLATE wit the following
420 * - @track@ to the UNQUOTED track name
421 * - @index@ to the track number from 0
422 * - @parity@ to "even" or "odd" alternately
423 * - @first@ to "true" on the first track and "false" otherwise
424 * - @last@ to "true" on the last track and "false" otherwise
426 * Note that unlike @playing@, @queue@ and @recent@ which are otherwise
427 * superficially similar, there is no @id@ sub-expansion here.
429 static int exp_new(int attribute((unused
)) nargs
,
430 const struct mx_node
**args
,
436 /* TODO perhaps we should generate an ID value for tracks in the new list */
437 for(i
= 0; i
< nnew
; ++i
)
438 if((rc
= mx_expand(mx_rewritel(args
[0],
440 "index", make_index(i
),
441 "parity", i
% 2 ?
"odd" : "even",
442 "first", i
== 0 ?
"true" : "false",
443 "last", i
== nnew
- 1 ?
"false" : "true",
452 * Expands to the volume in a given channel. CHANNEL must be "left" or
455 static int exp_volume(int attribute((unused
)) nargs
,
458 void attribute((unused
)) *u
) {
460 return sink_printf(output
, "%d",
461 !strcmp(args
[0], "left")
462 ? volume_left
: volume_right
) < 0 ?
-1 : 0;
467 * Expands to "true" if there is a playing track, otherwise "false".
469 static int exp_isplaying(int attribute((unused
)) nargs
,
470 char attribute((unused
)) **args
,
472 void attribute((unused
)) *u
) {
474 return mx_bool_result(output
, !!playing
);
479 * Expands to "true" if there the queue is nonempty, otherwise "false".
481 static int exp_isqueue(int attribute((unused
)) nargs
,
482 char attribute((unused
)) **args
,
484 void attribute((unused
)) *u
) {
486 return mx_bool_result(output
, !!queue
);
491 * Expands to "true" if there the recently played list is nonempty, otherwise
494 static int exp_isrecent(int attribute((unused
)) nargs
,
495 char attribute((unused
)) **args
,
497 void attribute((unused
)) *u
) {
499 return mx_bool_result(output
, !!recent
);
504 * Expands to "true" if there the newly added track list is nonempty, otherwise
507 static int exp_isnew(int attribute((unused
)) nargs
,
508 char attribute((unused
)) **args
,
510 void attribute((unused
)) *u
) {
512 return mx_bool_result(output
, !!nnew
);
517 * Expands to a track preference.
519 static int exp_pref(int attribute((unused
)) nargs
,
522 void attribute((unused
)) *u
) {
525 if(client
&& !disorder_get(client
, args
[0], args
[1], &value
))
526 return sink_writes(output
, cgi_sgmlquote(value
)) < 0 ?
-1 : 0;
530 /* @prefs{TRACK}{TEMPLATE}
532 * For each track preference of track TRACK, expands TEMPLATE with the
533 * following expansions:
534 * - @name@ to the UNQUOTED preference name
535 * - @index@ to the preference number from 0
536 * - @value@ to the UNQUOTED preference value
537 * - @parity@ to "even" or "odd" alternately
538 * - @first@ to "true" on the first preference and "false" otherwise
539 * - @last@ to "true" on the last preference and "false" otherwise
541 * Use @quote@ to quote preference names and values where necessary; see below.
543 static int exp_prefs(int attribute((unused
)) nargs
,
544 const struct mx_node
**args
,
548 struct kvp
*k
, *head
;
551 if((rc
= mx_expandstr(args
[0], &track
, u
, "argument #0 (TRACK)")))
553 if(!client
|| disorder_prefs(client
, track
, &head
))
555 for(k
= head
, i
= 0; k
; k
= k
->next
, ++i
)
556 if((rc
= mx_expand(mx_rewritel(args
[1],
557 "index", make_index(i
),
558 "parity", i
% 2 ?
"odd" : "even",
561 "first", k
== head ?
"true" : "false",
562 "last", k
->next ?
"false" : "true",
569 /* @transform{TRACK}{TYPE}{CONTEXT}
571 * Transforms a track name (if TYPE is "track") or directory name (if type is
572 * "dir"). CONTEXT should be the context, if it is left out then "display" is
575 static int exp_transform(int nargs
,
578 void attribute((unused
)) *u
) {
579 const char *t
= trackname_transform(args
[1], args
[0],
580 (nargs
> 2 ? args
[2] : "display"));
581 return sink_writes(output
, cgi_sgmlquote(t
)) < 0 ?
-1 : 0;
586 * Expands to "true" if playing is enabled, otherwise "false".
588 static int exp_enabled(int attribute((unused
)) nargs
,
589 char attribute((unused
)) **args
,
591 void attribute((unused
)) *u
) {
595 disorder_enabled(client
, &enabled
);
596 return mx_bool_result(output
, enabled
);
601 * Expands to "true" if random play is enabled, otherwise "false".
603 static int exp_random_enabled(int attribute((unused
)) nargs
,
604 char attribute((unused
)) **args
,
606 void attribute((unused
)) *u
) {
610 disorder_random_enabled(client
, &enabled
);
611 return mx_bool_result(output
, enabled
);
614 /* @trackstate{TRACK}
616 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
617 * the queue, otherwise to nothing.
619 static int exp_trackstate(int attribute((unused
)) nargs
,
622 void attribute((unused
)) *u
) {
624 struct queue_entry
*q
;
628 if(disorder_resolve(client
, &track
, args
[0]))
631 if(playing
&& !strcmp(track
, playing
->track
))
632 return sink_writes(output
, "playing") < 0 ?
-1 : 0;
634 for(q
= queue
; q
; q
= q
->next
)
635 if(!strcmp(track
, q
->track
))
636 return sink_writes(output
, "queued") < 0 ?
-1 : 0;
642 * Expands to an UNQUOTED URL which points back to the current page. (NB it
643 * might not be byte for byte identical - for instance, CGI arguments might be
646 static int exp_thisurl(int attribute((unused
)) nargs
,
647 char attribute((unused
)) **args
,
649 void attribute((unused
)) *u
) {
650 return sink_writes(output
, cgi_thisurl(config
->url
)) < 0 ?
-1 : 0;
655 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
656 * nothing if it is not a valid track.
658 static int exp_resolve(int attribute((unused
)) nargs
,
661 void attribute((unused
)) *u
) {
664 if(client
&& !disorder_resolve(client
, &r
, args
[0]))
665 return sink_writes(output
, r
) < 0 ?
-1 : 0;
671 * Expands to "true" if the playing track is paused, to "false" if it is
672 * playing (or if there is no playing track at all).
674 static int exp_paused(int attribute((unused
)) nargs
,
675 char attribute((unused
)) **args
,
677 void attribute((unused
)) *u
) {
679 return mx_bool_result(output
, playing
&& playing
->state
== playing_paused
);
684 * Expands to the current state of track ID.
686 static int exp_state(int attribute((unused
)) nargs
,
689 void attribute((unused
)) *u
) {
690 struct queue_entry
*q
= findtrack(args
[0]);
693 return sink_writes(output
, playing_states
[q
->state
]) < 0 ?
-1 : 0;
697 /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
699 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
700 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
702 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
703 * the user has the right and "false" otherwise.
705 * If there is no connection to the server then expands to nothing (in all
708 static int exp_right(int nargs
,
709 const struct mx_node
**args
,
719 if((rc
= mx_expandstr(args
[0], &right
, u
, "argument #0 (RIGHT)")))
721 if(parse_rights(right
, &r
, 1/*report*/))
723 /* Single-argument form */
725 return mx_bool_result(output
, !!(r
& rights
));
726 /* Multiple argument form */
728 return mx_expand(args
[1], output
, u
);
730 return mx_expand(args
[2], output
, u
);
734 /* @userinfo{PROPERTY}
736 * Expands to the named property of the current user.
738 static int exp_userinfo(int attribute((unused
)) nargs
,
741 void attribute((unused
)) *u
) {
744 if(client
&& !disorder_userinfo(client
, disorder_user(client
), args
[0], &v
))
745 return sink_writes(output
, v
) < 0 ?
-1 : 0;
751 * Expands to the latest error string.
753 static int exp_error(int attribute((unused
)) nargs
,
754 char attribute((unused
)) **args
,
756 void attribute((unused
)) *u
) {
757 return sink_writes(output
, cgi_sgmlquote(error_string
)) < 0 ?
-1 : 0;
760 /** @brief Register DisOrder-specific expansions */
761 void register_disorder_expansions(void) {
762 mx_register("arg", 1, 1, exp_arg
);
763 mx_register("enabled", 0, 0, exp_enabled
);
764 mx_register("error", 0, 0, exp_error
);
765 mx_register("isnew", 0, 0, exp_isnew
);
766 mx_register("isplaying", 0, 0, exp_isplaying
);
767 mx_register("isplaying", 0, 0, exp_isqueue
);
768 mx_register("isrecent", 0, 0, exp_isrecent
);
769 mx_register("length", 1, 1, exp_length
);
770 mx_register("movable", 1, 1, exp_movable
);
771 mx_register("part", 2, 3, exp_part
);
772 mx_register("paused", 0, 0, exp_paused
);
773 mx_register("pref", 2, 2, exp_pref
);
774 mx_register("quote", 1, 1, exp_quote
);
775 mx_register("random-enabled", 0, 0, exp_random_enabled
);
776 mx_register("removable", 1, 1, exp_removable
);
777 mx_register("resolve", 1, 1, exp_resolve
);
778 mx_register("server-version", 0, 0, exp_server_version
);
779 mx_register("state", 1, 1, exp_state
);
780 mx_register("thisurl", 0, 0, exp_thisurl
);
781 mx_register("trackstate", 1, 1, exp_trackstate
);
782 mx_register("transform", 2, 3, exp_transform
);
783 mx_register("url", 0, 0, exp_url
);
784 mx_register("user", 0, 0, exp_user
);
785 mx_register("userinfo", 1, 1, exp_userinfo
);
786 mx_register("version", 0, 0, exp_version
);
787 mx_register("volume", 1, 1, exp_volume
);
788 mx_register("when", 1, 1, exp_when
);
789 mx_register("who", 1, 1, exp_who
);
790 mx_register_magic("new", 1, 1, exp_new
);
791 mx_register_magic("playing", 0, 1, exp_playing
);
792 mx_register_magic("prefs", 2, 2, exp_prefs
);
793 mx_register_magic("queue", 1, 1, exp_queue
);
794 mx_register_magic("recent", 1, 1, exp_recent
);
795 mx_register_magic("right", 1, 3, exp_right
);