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
30 #include "macros-disorder.h"
32 /** @brief Client to use for DisOrder-specific expansions
34 * The caller should arrange for this to be created before any of
35 * these expansions are used (if it cannot connect then it's safe to
38 disorder_client
*client
;
40 /** @brief Cached data */
41 static unsigned flags
;
43 #define DC_QUEUE 0x0001
44 #define DC_PLAYING 0x0002
45 #define DC_RECENT 0x0004
46 #define DC_VOLUME 0x0008
47 #define DC_DIRS 0x0010
48 #define DC_FILES 0x0020
50 #define DC_RIGHTS 0x0080
52 static struct queue_entry
*queue
;
53 static struct queue_entry
*playing
;
54 static struct queue_entry
*recent
;
56 static int volume_left
;
57 static int volume_right
;
68 static rights_type rights
;
70 /** @brief Fetch cachable data */
71 static void lookup(unsigned want
) {
72 unsigned need
= want
^ (flags
& want
);
73 struct queue_entry
*r
, *rnext
;
80 disorder_queue(client
, &queue
);
82 disorder_playing(client
, &playing
);
84 disorder_new_tracks(client
, &new, &nnew
, 0);
85 if(need
& DC_RECENT
) {
86 /* we need to reverse the order of the list */
87 disorder_recent(client
, &r
);
96 disorder_get_volume(client
,
97 &volume_left
, &volume_right
);
98 if(need
& (DC_FILES
|DC_DIRS
)) {
99 if(!(dir
= cgi_get("directory")))
101 re
= cgi_get("regexp");
103 if(disorder_directories(client
, dir
, re
,
107 if(disorder_files(client
, dir
, re
,
111 if(need
& DC_RIGHTS
) {
112 rights
= RIGHT_READ
; /* fail-safe */
113 if(!disorder_userinfo(client
, disorder_user(client
),
115 parse_rights(rights
, &rights
, 1);
120 /** @brief Locate a track by ID */
121 static struct queue_entry
*findtrack(const char *id
) {
122 struct queue_entry
*q
;
125 if(playing
&& !strcmp(playing
->id
, id
))
128 for(q
= queue
; q
; q
= q
->next
)
129 if(!strcmp(q
->id
, id
))
132 for(q
= recent
; q
; q
= q
->next
)
133 if(!strcmp(q
->id
, id
))
137 /** @brief Return @p i as a string */
138 static const char *make_index(int i
) {
141 byte_xasprintf(&s
, "%d", i
);
147 * Expands to the server's version string, or a (safe to use) error
148 * value if the server is unavailable or broken.
150 static int exp_server_version(int attribute((unused
)) nargs
,
151 char attribute((unused
)) **args
,
153 void attribute((unused
)) *u
) {
157 if(disorder_version(client
, (char **)&v
))
158 v
= "(cannot get version)";
160 v
= "(server not running)";
161 return sink_write(output
, cgi_sgmlquote(v
)) < 0 ?
-1 : 0;
166 * Expands to the local version string.
168 static int exp_version(int attribute((unused
)) nargs
,
169 char attribute((unused
)) **args
,
171 void attribute((unused
)) *u
) {
172 return sink_write(output
,
173 cgi_sgmlquote(disorder_short_version_string
)) < 0 ?
-1 : 0;
178 * Expands to the base URL of the web interface.
180 static int exp_url(int attribute((unused
)) nargs
,
181 char attribute((unused
)) **args
,
183 void attribute((unused
)) *u
) {
184 return sink_write(output
,
185 cgi_sgmlquote(config
->url
)) < 0 ?
-1 : 0;
190 * Expands to the CGI argument NAME, or the empty string if there is
193 static int exp_arg(int attribute((unused
)) nargs
,
196 void attribute((unused
)) *u
) {
197 const char *s
= cgi_get(args
[0]);
199 return sink_write(output
,
200 cgi_sgmlquote(s
)) < 0 ?
-1 : 0;
207 * Expands to the logged-in username (which might be "guest"), or to
208 * the empty string if not connected.
210 static int exp_user(int attribute((unused
)) nargs
,
211 char attribute((unused
)) **args
,
213 void attribute((unused
)) *u
) {
216 if(client
&& (u
= disorder_user(client
)))
217 return sink_write(output
, cgi_sgmlquote(u
)) < 0 ?
-1 : 0;
221 /* @part{TRACK|ID}{PART}{CONTEXT}
223 * Expands to a track name part.
225 * A track may be identified by name or by queue ID.
227 * CONTEXT may be omitted. If it is then 'display' is assumed.
229 * If the CONTEXT is 'short' then the 'display' part is looked up, and the
230 * result truncated according to the length defined by the short_display
231 * configuration directive.
233 static int exp_part(int nargs
,
236 void attribute((unused
)) *u
) {
237 const char *track
= args
[0], *part
= args
[1];
238 const char *context
= nargs
> 2 ? args
[2] : "display";
241 if(track
[0] != '/') {
242 struct queue_entry
*q
= findtrack(track
);
250 && !disorder_get(client
, &s
, track
,
251 !strcmp(context
, "short") ?
"display" : context
,
253 return sink_write(output
, cgi_sgmlquote(s
)) < 0 ?
-1 : 0;
259 * SGML-quotes STRING. Note that most expansion results are already suitable
260 * quoted, so this expansion is usually not required.
262 static int exp_quote(int attribute((unused
)) nargs
,
265 void attribute((unused
)) *u
) {
266 return sink_write(output
, cgi_sgmlquote(args
[0])) < 0 ?
-1 : 0;
271 * Expands to the name of the submitter of track ID, which must be a playing
272 * track, in the queue, or in the recent list.
274 static int exp_who(int attribute((unused
)) nargs
,
277 void attribute((unused
)) *u
) {
278 struct queue_entry
*q
= findtrack(args
[0]);
280 if(q
&& q
->submitter
)
281 return sink_write(output
, cgi_sgmlquote(q
->submitter
)) < 0 ?
-1 : 0;
287 * Expands to the time a track started or is expected to start. The track must
288 * be a playing track, in the queue, or in the recent list.
290 static int exp_when(int attribute((unused
)) nargs
,
293 void attribute((unused
)) *u
) {
294 struct queue_entry
*q
= findtrack(args
[0]);
295 const struct tm
*w
= 0;
299 case playing_isscratch
:
300 case playing_unplayed
:
303 w
= localtime(&q
->expected
);
306 case playing_no_player
:
308 case playing_scratched
:
309 case playing_started
:
311 case playing_quitting
:
313 w
= localtime(&q
->played
);
317 return sink_printf(output
, "%d:%02d", w
->tm_hour
, w
->tm_min
) < 0 ?
-1 : 0;
319 return sink_write(output
, " ") < 0 ?
-1 : 0;
324 * Expands to the length of a track, identified by its queue ID or its name.
325 * If it is the playing track (identified by ID) then the amount played so far
328 static int exp_length(int attribute((unused
)) nargs
,
331 void attribute((unused
)) *u
) {
332 struct queue_entry
*q
;
336 if(args
[0][0] == '/')
337 /* Track identified by name */
340 /* Track identified by queue ID */
341 if(!(q
= findtrack(args
[0])))
343 if(q
->state
== play_start
|| q
->state
== playing_paused
)
344 if(sink_printf(output
, "%ld:%02ld/", q
->sofar
/ 60, q
->sofar
% 60) < 0)
348 if(client
&& disorder_length(client
, name
, &length
))
349 return sink_printf(output
, "%ld:%02ld",
350 length
/ 60, length
% 60) < 0 ?
-1 : 0;
351 return sink_write(output
, " ") < 0 ?
-1 : 0;
356 * Expands to "true" if track ID is removable (or scratchable, if it is the
357 * playing track) and "false" otherwise.
359 static int exp_removable(int attribute((unused
)) nargs
,
362 void attribute((unused
)) *u
) {
363 struct queue_entry
*q
= findtrack(args
[0]);
364 /* TODO would be better to reject recent */
367 return mx_bool_result(output
, 0);
369 return mx_bool_result(output
,
370 (q
== playing ? right_scratchable
: right_removable
)
371 (rights
, disorder_user(client
), q
));
376 * Expands to "true" if track ID is movable and "false" otherwise.
378 static int exp_movable(int attribute((unused
)) nargs
,
381 void attribute((unused
)) *u
) {
382 struct queue_entry
*q
= findtrack(args
[0]);
383 /* TODO would be better to recent playing/recent */
386 return mx_bool_result(output
, 0);
388 return mx_bool_result(output
,
389 right_movable(rights
, disorder_user(client
), q
));
392 /* @playing{TEMPLATE}
394 * Expands to TEMPLATE, with:
395 * - @id@ expanded to the queue ID of the playing track
396 * - @track@ expanded to its UNQUOTED name
398 * If no track is playing expands to nothing.
400 * TEMPLATE is optional. If it is left out then instead expands to the queue
401 * ID of the playing track.
403 static int exp_playing(int nargs
,
404 const struct mx_node
**args
,
406 void attribute((unused
)) *u
) {
411 return sink_write(output
, playing
->id
) < 0 ?
-1 : 0;
412 return mx_rewritel(args
[0],
414 "track", playing
->track
,
420 * For each track in the queue, expands TEMPLATE with the following expansions:
421 * - @id@ to the queue ID of the track
422 * - @track@ to the UNQUOTED track name
423 * - @index@ to the track number from 0
424 * - @parity@ to "even" or "odd" alternately
425 * - @first@ to "true" on the first track and "false" otherwise
426 * - @last@ to "true" on the last track and "false" otherwise
428 static int exp_queue(int attribute((unused
)) nargs
,
429 const struct mx_node
**args
,
431 void attribute((unused
)) *u
) {
432 struct queue_entry
*q
;
436 for(q
= queue
, i
= 0; q
; q
= q
->next
, ++i
)
437 if((rc
= mx_rewritel(args
[0],
440 "index", make_index(i
),
441 "parity", i
% 2 ?
"odd" : "even",
442 "first", q
== queue ?
"true" : "false",
443 "last", q
->next ?
"false" : "true",
451 * For each track in the recently played list, expands TEMPLATE with the
452 * following expansions:
453 * - @id@ to the queue ID of the track
454 * - @track@ to the UNQUOTED track name
455 * - @index@ to the track number from 0
456 * - @parity@ to "even" or "odd" alternately
457 * - @first@ to "true" on the first track and "false" otherwise
458 * - @last@ to "true" on the last track and "false" otherwise
460 static int exp_recent(int attribute((unused
)) nargs
,
461 const struct mx_node
**args
,
463 void attribute((unused
)) *u
) {
464 struct queue_entry
*q
;
468 for(q
= recent
, i
= 0; q
; q
= q
->next
, ++i
)
469 if((rc
= mx_rewritel(args
[0],
472 "index", make_index(i
),
473 "parity", i
% 2 ?
"odd" : "even",
474 "first", q
== recent ?
"true" : "false",
475 "last", q
->next ?
"false" : "true",
483 * For each track in the newly added list, expands TEMPLATE wit the following
485 * - @track@ to the UNQUOTED track name
486 * - @index@ to the track number from 0
487 * - @parity@ to "even" or "odd" alternately
488 * - @first@ to "true" on the first track and "false" otherwise
489 * - @last@ to "true" on the last track and "false" otherwise
491 * Note that unlike @playing@, @queue@ and @recent@ which are otherwise
492 * superficially similar, there is no @id@ sub-expansion here.
494 static int exp_new(int attribute((unused
)) nargs
,
495 const struct mx_node
**args
,
497 void attribute((unused
)) *u
) {
498 struct queue_entry
*q
;
502 /* TODO perhaps we should generate an ID value for tracks in the new list */
503 for(i
= 0; i
< nnew
; ++i
)
504 if((rc
= mx_rewritel(args
[0],
506 "index", make_index(i
),
507 "parity", i
% 2 ?
"odd" : "even",
508 "first", i
== 0 ?
"true" : "false",
509 "last", i
== nnew
- 1 ?
"false" : "true",
517 * Expands to the volume in a given channel. CHANNEL must be "left" or
520 static int exp_volume(int attribute((unused
)) nargs
,
523 void attribute((unused
)) *u
) {
525 return sink_write(output
, "%d",
526 !strcmp(args
[0], "left")
527 ? volume_left
: volume_right
) < 0 ?
-1 : 0;
532 * Expands to "true" if there is a playing track, otherwise "false".
534 static int exp_isplaying(int attribute((unused
)) nargs
,
535 char attribute((unused
)) **args
,
537 void attribute((unused
)) *u
) {
539 return mx_bool_result(output
, !!playing
);
544 * Expands to "true" if there the queue is nonempty, otherwise "false".
546 static int exp_isqueue(int attribute((unused
)) nargs
,
547 char attribute((unused
)) **args
,
549 void attribute((unused
)) *u
) {
551 return mx_bool_result(output
, !!queue
);
556 * Expands to "true" if there the recently played list is nonempty, otherwise
559 static int exp_isrecent(int attribute((unused
)) nargs
,
560 char attribute((unused
)) **args
,
562 void attribute((unused
)) *u
) {
564 return mx_bool_result(output
, !!recent
);
569 * Expands to "true" if there the newly added track list is nonempty, otherwise
572 static int exp_isnew(int attribute((unused
)) nargs
,
573 char attribute((unused
)) **args
,
575 void attribute((unused
)) *u
) {
577 return mx_bool_result(output
, !!nnew
);
580 /* @pref{TRACK}{KEY}@
582 * Expands to a track preference.
584 static int exp_pref(int attribute((unused
)) nargs
,
587 void attribute((unused
)) *u
) {
590 if(client
&& !disorder_get(client
, args
[0], args
[1], &value
))
591 return sink_write(output
, cgi_sgmlquote(value
)) < 0 ?
-1 : 0;
594 /* @prefs{TRACK}{TEMPLATE}@
596 * For each track preference of track TRACK, expands TEMPLATE with the
597 * following expansions:
598 * - @name@ to the UNQUOTED preference name
599 * - @index@ to the preference number from 0
600 * - @value@ to the UNQUOTED preference value
601 * - @parity@ to "even" or "odd" alternately
602 * - @first@ to "true" on the first preference and "false" otherwise
603 * - @last@ to "true" on the last preference and "false" otherwise
605 * Use @quote@ to quote preference names and values where necessary; see below.
607 static int exp_prefs(int attribute((unused
)) nargs
,
608 const struct mx_node
**args
,
610 void attribute((unused
)) *u
) {
612 struct kvp
*k
, *head
;
615 if((rc
= mx_expandstr(args
[0], &track
, u
, "argument #0 (TRACK)")))
617 if(!client
|| disorder_prefs(client
, track
, &head
))
619 for(k
= head
, i
= 0; k
; k
= k
->next
, ++i
)
620 if((rc
= mx_rewritel(args
[1],
621 "index", make_index(i
),
622 "parity", i
% 2 ?
"odd" : "even",
625 "first", k
== head ?
"true" : "false",
626 "last", k
->next ?
"false" : "true",
632 /* @transform{TRACK}{TYPE}{CONTEXT}@
634 * Transforms a track name (if TYPE is "track") or directory name (if type is
635 * "dir"). CONTEXT should be the context, if it is left out then "display" is
638 static int exp_transform(int nargs
,
641 void attribute((unused
)) *u
) {
642 const char *t
= trackname_transform(args
[1], args
[0],
643 (nargs
> 2 ? args
[2] : "display")));
644 return sink_write(output
, cgi_sgmlquote(t
)) < 0 ?
-1 : 0;
649 * Expands to "true" if playing is enabled, otherwise "false".
651 static int exp_enabled(int attribute((unused
)) nargs
,
652 char attribute((unused
)) **args
,
654 void attribute((unused
)) *u
) {
658 disorder_enabled(client
, &enabled
);
659 return mx_bool_result(output
, enabled
);
664 * Expands to "true" if random play is enabled, otherwise "false".
666 static int exp_enabled(int attribute((unused
)) nargs
,
667 char attribute((unused
)) **args
,
669 void attribute((unused
)) *u
) {
673 disorder_random_enabled(client
, &enabled
);
674 return mx_bool_result(output
, enabled
);
677 /* @trackstate{TRACK}@
679 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
680 * the queue, otherwise to nothing.
682 static int exp_trackstate(int attribute((unused
)) nargs
,
685 void attribute((unused
)) *u
) {
687 struct queue_entry
*q
;
691 if(disorder_resolve(client
, &track
, args
[0]))
694 if(playing
&& !strcmp(track
, playing
->track
))
695 return sink_write(output
, "playing") < 0 ?
-1 : 0;
697 for(q
= queue
; q
; q
= q
->next
)
698 if(!strcmp(track
, q
->track
))
699 return sink_write(output
, "queued") < 0 ?
-1 : 0;
705 * Expands to an UNQUOTED URL which points back to the current page. (NB it
706 * might not be byte for byte identical - for instance, CGI arguments might be
709 static int exp_thisurl(int attribute((unused
)) nargs
,
710 char attribute((unused
)) **args
,
712 void attribute((unused
)) *u
) {
713 return cgi_thisurl(config
->url
);
718 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
719 * nothing if it is not a valid track.
721 static int exp_resolve(int attribute((unused
)) nargs
,
724 void attribute((unused
)) *u
) {
727 if(client
&& !disorder_resolve(client
, &r
, args
[0]))
728 return sink_write(output
, r
) < 0 ?
-1 : 0;
734 * Expands to "true" if the playing track is paused, to "false" if it is
735 * playing (or if there is no playing track at all).
737 static int exp_paused(int attribute((unused
)) nargs
,
738 char attribute((unused
)) **args
,
740 void attribute((unused
)) *u
) {
742 return mx_bool_result(output
, playing
&& playing
->state
== playing_paused
);
747 * Expands to the current state of track ID.
749 static int exp_state(int attribute((unused
)) nargs
,
752 void attribute((unused
)) *u
) {
753 struct queue_entry
*q
= findtrack(args
[0]);
756 return sink_write(output
, playing_states
[q
->state
]) < 0 ?
-1 : 0;
760 /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
762 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
763 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
765 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
766 * the user has the right and "false" otherwise.
768 * If there is no connection to the server then expands to nothing (in all
771 static int exp_right(int nargs
,
772 const struct mx_node
**args
,
774 void attribute((unused
)) *u
) {
781 if((rc
= mx_expandstr(args
[0], &rightname
, u
, "argument #0 (RIGHT)")))
783 if(parse_rights(right
, &r
, 1/*report*/))
785 /* Single-argument form */
787 return mx_bool_result(output
, !!(r
& rights
));
788 /* Multiple argument form */
790 return mx_expandl(args
[1], (char *)0);
792 return mx_expandl(args
[2], (char *)0);
796 /* @userinfo{PROPERTY}@
798 * Expands to the named property of the current user.
800 static int exp_userinfo(int attribute((unused
)) nargs
,
803 void attribute((unused
)) *u
) {
806 if(client
&& !disorder_userinfo(client
, disorder_user(client
), args
[0], &v
))
807 return sink_write(output
, v
) < 0 ?
-1 : 0;
811 /** @brief Register DisOrder-specific expansions */
812 void register_disorder_expansions(void) {
813 mx_register(exp_arg
, 1, 1, "arg");
814 mx_register(exp_enabled
, 0, 0, "enabled");
815 mx_register(exp_isnew
, 0, 0, "isnew");
816 mx_register(exp_isplaying
, 0, 0, "isplaying");
817 mx_register(exp_isqueue
, 0, 0, "isplaying");
818 mx_register(exp_isrecent
, 0, 0, "isrecent");
819 mx_register(exp_length
, 1, 1, "length");
820 mx_register(exp_movable
, 1, 1, "movable");
821 mx_register(exp_part
, 2, 3, "part");
822 mx_register(exp_pref
, 2, 2, "pref");
823 mx_register(exp_quote
, 1, 1, "quote");
824 mx_register(exp_random_enabled
, 0, 0, "random-enabled");
825 mx_register(exp_removable
, 1, 1, "removable");
826 mx_register(exp_resolve
, 1, 1, "resolve");
827 mx_register(exp_right
, 1, 3, "right");
828 mx_register(exp_server_version
, 0, 0, "server-version");
829 mx_register(exp_state
, 1, 1, "state");
830 mx_register(exp_thisurl
, 0, 0, "thisurl");
831 mx_register(exp_trackstate
, 1, 1, "trackstate");
832 mx_register(exp_transform
, 2, 3, "transform");
833 mx_register(exp_url
, 0, 0, "url");
834 mx_register(exp_user
, 0, 0, "user");
835 mx_register(exp_userinfo
, 1, 1, "userinfo");
836 mx_register(exp_version
, 0, 0, "version");
837 mx_register(exp_volume
, 1, 1, "volume");
838 mx_register(exp_when
, 1, 1, "when");
839 mx_register(exp_who
, 1, 1, "who");
840 mx_register_magic(exp_new
, 1, 1, "new");
841 mx_register_magic(exp_playing
, 0, 1, "playing");
842 mx_register_magic(exp_prefs
, 2, 2, "prefs");
843 mx_register_magic(exp_queue
, 1, 1, "queue");
844 mx_register_magic(exp_recent
, 1, 1, "recent");