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 For error template */
43 /** @brief Cached data */
44 static unsigned flags;
46 #define DC_QUEUE 0x0001
47 #define DC_PLAYING 0x0002
48 #define DC_RECENT 0x0004
49 #define DC_VOLUME 0x0008
50 #define DC_DIRS 0x0010
51 #define DC_FILES 0x0020
53 #define DC_RIGHTS 0x0080
55 static struct queue_entry *queue;
56 static struct queue_entry *playing;
57 static struct queue_entry *recent;
59 static int volume_left;
60 static int volume_right;
71 static rights_type rights;
73 /** @brief Fetch cachable data */
74 static void lookup(unsigned want) {
75 unsigned need = want ^ (flags & want);
76 struct queue_entry *r, *rnext;
83 disorder_queue(client, &queue);
85 disorder_playing(client, &playing);
87 disorder_new_tracks(client, &new, &nnew, 0);
88 if(need & DC_RECENT) {
89 /* we need to reverse the order of the list */
90 disorder_recent(client, &r);
99 disorder_get_volume(client,
100 &volume_left, &volume_right);
101 if(need & (DC_FILES|DC_DIRS)) {
102 if(!(dir = cgi_get("directory")))
104 re = cgi_get("regexp");
106 if(disorder_directories(client, dir, re,
110 if(disorder_files(client, dir, re,
114 if(need & DC_RIGHTS) {
115 rights = RIGHT_READ; /* fail-safe */
116 if(!disorder_userinfo(client, disorder_user(client),
118 parse_rights(rights, &rights, 1);
123 /** @brief Locate a track by ID */
124 static struct queue_entry *findtrack(const char *id) {
125 struct queue_entry *q;
128 if(playing && !strcmp(playing->id, id))
131 for(q = queue; q; q = q->next)
132 if(!strcmp(q->id, id))
135 for(q = recent; q; q = q->next)
136 if(!strcmp(q->id, id))
140 /** @brief Return @p i as a string */
141 static const char *make_index(int i) {
144 byte_xasprintf(&s, "%d", i);
150 * Expands to the server's version string, or a (safe to use) error
151 * value if the server is unavailable or broken.
153 static int exp_server_version(int attribute((unused)) nargs,
154 char attribute((unused)) **args,
156 void attribute((unused)) *u) {
160 if(disorder_version(client, (char **)&v))
161 v = "(cannot get version)";
163 v = "(server not running)";
164 return sink_write(output, cgi_sgmlquote(v)) < 0 ? -1 : 0;
169 * Expands to the local version string.
171 static int exp_version(int attribute((unused)) nargs,
172 char attribute((unused)) **args,
174 void attribute((unused)) *u) {
175 return sink_write(output,
176 cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0;
181 * Expands to the base URL of the web interface.
183 static int exp_url(int attribute((unused)) nargs,
184 char attribute((unused)) **args,
186 void attribute((unused)) *u) {
187 return sink_write(output,
188 cgi_sgmlquote(config->url)) < 0 ? -1 : 0;
193 * Expands to the CGI argument NAME, or the empty string if there is
196 static int exp_arg(int attribute((unused)) nargs,
199 void attribute((unused)) *u) {
200 const char *s = cgi_get(args[0]);
202 return sink_write(output,
203 cgi_sgmlquote(s)) < 0 ? -1 : 0;
210 * Expands to the logged-in username (which might be "guest"), or to
211 * the empty string if not connected.
213 static int exp_user(int attribute((unused)) nargs,
214 char attribute((unused)) **args,
216 void attribute((unused)) *u) {
219 if(client && (u = disorder_user(client)))
220 return sink_write(output, cgi_sgmlquote(u)) < 0 ? -1 : 0;
224 /* @part{TRACK|ID}{PART}{CONTEXT}
226 * Expands to a track name part.
228 * A track may be identified by name or by queue ID.
230 * CONTEXT may be omitted. If it is then 'display' is assumed.
232 * If the CONTEXT is 'short' then the 'display' part is looked up, and the
233 * result truncated according to the length defined by the short_display
234 * configuration directive.
236 static int exp_part(int nargs,
239 void attribute((unused)) *u) {
240 const char *track = args[0], *part = args[1];
241 const char *context = nargs > 2 ? args[2] : "display";
244 if(track[0] != '/') {
245 struct queue_entry *q = findtrack(track);
253 && !disorder_get(client, &s, track,
254 !strcmp(context, "short") ? "display" : context,
256 return sink_write(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
262 * SGML-quotes STRING. Note that most expansion results are already suitable
263 * quoted, so this expansion is usually not required.
265 static int exp_quote(int attribute((unused)) nargs,
268 void attribute((unused)) *u) {
269 return sink_write(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0;
274 * Expands to the name of the submitter of track ID, which must be a playing
275 * track, in the queue, or in the recent list.
277 static int exp_who(int attribute((unused)) nargs,
280 void attribute((unused)) *u) {
281 struct queue_entry *q = findtrack(args[0]);
283 if(q && q->submitter)
284 return sink_write(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0;
290 * Expands to the time a track started or is expected to start. The track must
291 * be a playing track, in the queue, or in the recent list.
293 static int exp_when(int attribute((unused)) nargs,
296 void attribute((unused)) *u) {
297 struct queue_entry *q = findtrack(args[0]);
298 const struct tm *w = 0;
302 case playing_isscratch:
303 case playing_unplayed:
306 w = localtime(&q->expected);
309 case playing_no_player:
311 case playing_scratched:
312 case playing_started:
314 case playing_quitting:
316 w = localtime(&q->played);
320 return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0;
322 return sink_write(output, " ") < 0 ? -1 : 0;
327 * Expands to the length of a track, identified by its queue ID or its name.
328 * If it is the playing track (identified by ID) then the amount played so far
331 static int exp_length(int attribute((unused)) nargs,
334 void attribute((unused)) *u) {
335 struct queue_entry *q;
339 if(args[0][0] == '/')
340 /* Track identified by name */
343 /* Track identified by queue ID */
344 if(!(q = findtrack(args[0])))
346 if(q->state == play_start || q->state == playing_paused)
347 if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0)
351 if(client && disorder_length(client, name, &length))
352 return sink_printf(output, "%ld:%02ld",
353 length / 60, length % 60) < 0 ? -1 : 0;
354 return sink_write(output, " ") < 0 ? -1 : 0;
359 * Expands to "true" if track ID is removable (or scratchable, if it is the
360 * playing track) and "false" otherwise.
362 static int exp_removable(int attribute((unused)) nargs,
365 void attribute((unused)) *u) {
366 struct queue_entry *q = findtrack(args[0]);
367 /* TODO would be better to reject recent */
370 return mx_bool_result(output, 0);
372 return mx_bool_result(output,
373 (q == playing ? right_scratchable : right_removable)
374 (rights, disorder_user(client), q));
379 * Expands to "true" if track ID is movable and "false" otherwise.
381 static int exp_movable(int attribute((unused)) nargs,
384 void attribute((unused)) *u) {
385 struct queue_entry *q = findtrack(args[0]);
386 /* TODO would be better to recent playing/recent */
389 return mx_bool_result(output, 0);
391 return mx_bool_result(output,
392 right_movable(rights, disorder_user(client), q));
395 /* @playing{TEMPLATE}
397 * Expands to TEMPLATE, with:
398 * - @id@ expanded to the queue ID of the playing track
399 * - @track@ expanded to its UNQUOTED name
401 * If no track is playing expands to nothing.
403 * TEMPLATE is optional. If it is left out then instead expands to the queue
404 * ID of the playing track.
406 static int exp_playing(int nargs,
407 const struct mx_node **args,
409 void attribute((unused)) *u) {
414 return sink_write(output, playing->id) < 0 ? -1 : 0;
415 return mx_rewritel(args[0],
417 "track", playing->track,
423 * For each track in the queue, expands TEMPLATE with the following expansions:
424 * - @id@ to the queue ID of the track
425 * - @track@ to the UNQUOTED track name
426 * - @index@ to the track number from 0
427 * - @parity@ to "even" or "odd" alternately
428 * - @first@ to "true" on the first track and "false" otherwise
429 * - @last@ to "true" on the last track and "false" otherwise
431 static int exp_queue(int attribute((unused)) nargs,
432 const struct mx_node **args,
434 void attribute((unused)) *u) {
435 struct queue_entry *q;
439 for(q = queue, i = 0; q; q = q->next, ++i)
440 if((rc = mx_rewritel(args[0],
443 "index", make_index(i),
444 "parity", i % 2 ? "odd" : "even",
445 "first", q == queue ? "true" : "false",
446 "last", q->next ? "false" : "true",
454 * For each track in the recently played list, expands TEMPLATE with the
455 * following expansions:
456 * - @id@ to the queue ID of the track
457 * - @track@ to the UNQUOTED track name
458 * - @index@ to the track number from 0
459 * - @parity@ to "even" or "odd" alternately
460 * - @first@ to "true" on the first track and "false" otherwise
461 * - @last@ to "true" on the last track and "false" otherwise
463 static int exp_recent(int attribute((unused)) nargs,
464 const struct mx_node **args,
466 void attribute((unused)) *u) {
467 struct queue_entry *q;
471 for(q = recent, i = 0; q; q = q->next, ++i)
472 if((rc = mx_rewritel(args[0],
475 "index", make_index(i),
476 "parity", i % 2 ? "odd" : "even",
477 "first", q == recent ? "true" : "false",
478 "last", q->next ? "false" : "true",
486 * For each track in the newly added list, expands TEMPLATE wit the following
488 * - @track@ to the UNQUOTED track name
489 * - @index@ to the track number from 0
490 * - @parity@ to "even" or "odd" alternately
491 * - @first@ to "true" on the first track and "false" otherwise
492 * - @last@ to "true" on the last track and "false" otherwise
494 * Note that unlike @playing@, @queue@ and @recent@ which are otherwise
495 * superficially similar, there is no @id@ sub-expansion here.
497 static int exp_new(int attribute((unused)) nargs,
498 const struct mx_node **args,
500 void attribute((unused)) *u) {
501 struct queue_entry *q;
505 /* TODO perhaps we should generate an ID value for tracks in the new list */
506 for(i = 0; i < nnew; ++i)
507 if((rc = mx_rewritel(args[0],
509 "index", make_index(i),
510 "parity", i % 2 ? "odd" : "even",
511 "first", i == 0 ? "true" : "false",
512 "last", i == nnew - 1 ? "false" : "true",
520 * Expands to the volume in a given channel. CHANNEL must be "left" or
523 static int exp_volume(int attribute((unused)) nargs,
526 void attribute((unused)) *u) {
528 return sink_write(output, "%d",
529 !strcmp(args[0], "left")
530 ? volume_left : volume_right) < 0 ? -1 : 0;
535 * Expands to "true" if there is a playing track, otherwise "false".
537 static int exp_isplaying(int attribute((unused)) nargs,
538 char attribute((unused)) **args,
540 void attribute((unused)) *u) {
542 return mx_bool_result(output, !!playing);
547 * Expands to "true" if there the queue is nonempty, otherwise "false".
549 static int exp_isqueue(int attribute((unused)) nargs,
550 char attribute((unused)) **args,
552 void attribute((unused)) *u) {
554 return mx_bool_result(output, !!queue);
559 * Expands to "true" if there the recently played list is nonempty, otherwise
562 static int exp_isrecent(int attribute((unused)) nargs,
563 char attribute((unused)) **args,
565 void attribute((unused)) *u) {
567 return mx_bool_result(output, !!recent);
572 * Expands to "true" if there the newly added track list is nonempty, otherwise
575 static int exp_isnew(int attribute((unused)) nargs,
576 char attribute((unused)) **args,
578 void attribute((unused)) *u) {
580 return mx_bool_result(output, !!nnew);
585 * Expands to a track preference.
587 static int exp_pref(int attribute((unused)) nargs,
590 void attribute((unused)) *u) {
593 if(client && !disorder_get(client, args[0], args[1], &value))
594 return sink_write(output, cgi_sgmlquote(value)) < 0 ? -1 : 0;
597 /* @prefs{TRACK}{TEMPLATE}
599 * For each track preference of track TRACK, expands TEMPLATE with the
600 * following expansions:
601 * - @name@ to the UNQUOTED preference name
602 * - @index@ to the preference number from 0
603 * - @value@ to the UNQUOTED preference value
604 * - @parity@ to "even" or "odd" alternately
605 * - @first@ to "true" on the first preference and "false" otherwise
606 * - @last@ to "true" on the last preference and "false" otherwise
608 * Use @quote@ to quote preference names and values where necessary; see below.
610 static int exp_prefs(int attribute((unused)) nargs,
611 const struct mx_node **args,
613 void attribute((unused)) *u) {
615 struct kvp *k, *head;
618 if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)")))
620 if(!client || disorder_prefs(client, track, &head))
622 for(k = head, i = 0; k; k = k->next, ++i)
623 if((rc = mx_rewritel(args[1],
624 "index", make_index(i),
625 "parity", i % 2 ? "odd" : "even",
628 "first", k == head ? "true" : "false",
629 "last", k->next ? "false" : "true",
635 /* @transform{TRACK}{TYPE}{CONTEXT}
637 * Transforms a track name (if TYPE is "track") or directory name (if type is
638 * "dir"). CONTEXT should be the context, if it is left out then "display" is
641 static int exp_transform(int nargs,
644 void attribute((unused)) *u) {
645 const char *t = trackname_transform(args[1], args[0],
646 (nargs > 2 ? args[2] : "display")));
647 return sink_write(output, cgi_sgmlquote(t)) < 0 ? -1 : 0;
652 * Expands to "true" if playing is enabled, otherwise "false".
654 static int exp_enabled(int attribute((unused)) nargs,
655 char attribute((unused)) **args,
657 void attribute((unused)) *u) {
661 disorder_enabled(client, &enabled);
662 return mx_bool_result(output, enabled);
667 * Expands to "true" if random play is enabled, otherwise "false".
669 static int exp_enabled(int attribute((unused)) nargs,
670 char attribute((unused)) **args,
672 void attribute((unused)) *u) {
676 disorder_random_enabled(client, &enabled);
677 return mx_bool_result(output, enabled);
682 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
683 * the queue, otherwise to nothing.
685 static int exp_trackstate(int attribute((unused)) nargs,
688 void attribute((unused)) *u) {
690 struct queue_entry *q;
694 if(disorder_resolve(client, &track, args[0]))
697 if(playing && !strcmp(track, playing->track))
698 return sink_write(output, "playing") < 0 ? -1 : 0;
700 for(q = queue; q; q = q->next)
701 if(!strcmp(track, q->track))
702 return sink_write(output, "queued") < 0 ? -1 : 0;
708 * Expands to an UNQUOTED URL which points back to the current page. (NB it
709 * might not be byte for byte identical - for instance, CGI arguments might be
712 static int exp_thisurl(int attribute((unused)) nargs,
713 char attribute((unused)) **args,
715 void attribute((unused)) *u) {
716 return cgi_thisurl(config->url);
721 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
722 * nothing if it is not a valid track.
724 static int exp_resolve(int attribute((unused)) nargs,
727 void attribute((unused)) *u) {
730 if(client && !disorder_resolve(client, &r, args[0]))
731 return sink_write(output, r) < 0 ? -1 : 0;
737 * Expands to "true" if the playing track is paused, to "false" if it is
738 * playing (or if there is no playing track at all).
740 static int exp_paused(int attribute((unused)) nargs,
741 char attribute((unused)) **args,
743 void attribute((unused)) *u) {
745 return mx_bool_result(output, playing && playing->state == playing_paused);
750 * Expands to the current state of track ID.
752 static int exp_state(int attribute((unused)) nargs,
755 void attribute((unused)) *u) {
756 struct queue_entry *q = findtrack(args[0]);
759 return sink_write(output, playing_states[q->state]) < 0 ? -1 : 0;
763 /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
765 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
766 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
768 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
769 * the user has the right and "false" otherwise.
771 * If there is no connection to the server then expands to nothing (in all
774 static int exp_right(int nargs,
775 const struct mx_node **args,
777 void attribute((unused)) *u) {
784 if((rc = mx_expandstr(args[0], &rightname, u, "argument #0 (RIGHT)")))
786 if(parse_rights(right, &r, 1/*report*/))
788 /* Single-argument form */
790 return mx_bool_result(output, !!(r & rights));
791 /* Multiple argument form */
793 return mx_expandl(args[1], (char *)0);
795 return mx_expandl(args[2], (char *)0);
799 /* @userinfo{PROPERTY}
801 * Expands to the named property of the current user.
803 static int exp_userinfo(int attribute((unused)) nargs,
806 void attribute((unused)) *u) {
809 if(client && !disorder_userinfo(client, disorder_user(client), args[0], &v))
810 return sink_write(output, v) < 0 ? -1 : 0;
816 * Expands to the latest error string.
818 static int exp_error(int attribute((unused)) nargs,
819 char attribute((unused)) **args,
821 void attribute((unused)) *u) {
822 return sink_write(output, cgi_sgmlquote(error_string)) < 0 ? -1 : 0;
825 /* @userinfo{PROPERTY}@
827 /** @brief Register DisOrder-specific expansions */
828 void register_disorder_expansions(void) {
829 mx_register(exp_arg, 1, 1, "arg");
830 mx_register(exp_enabled, 0, 0, "enabled");
831 mx_register(exp_error, 0, 0, "error");
832 mx_register(exp_isnew, 0, 0, "isnew");
833 mx_register(exp_isplaying, 0, 0, "isplaying");
834 mx_register(exp_isqueue, 0, 0, "isplaying");
835 mx_register(exp_isrecent, 0, 0, "isrecent");
836 mx_register(exp_length, 1, 1, "length");
837 mx_register(exp_movable, 1, 1, "movable");
838 mx_register(exp_part, 2, 3, "part");
839 mx_register(exp_pref, 2, 2, "pref");
840 mx_register(exp_quote, 1, 1, "quote");
841 mx_register(exp_random_enabled, 0, 0, "random-enabled");
842 mx_register(exp_removable, 1, 1, "removable");
843 mx_register(exp_resolve, 1, 1, "resolve");
844 mx_register(exp_right, 1, 3, "right");
845 mx_register(exp_server_version, 0, 0, "server-version");
846 mx_register(exp_state, 1, 1, "state");
847 mx_register(exp_thisurl, 0, 0, "thisurl");
848 mx_register(exp_trackstate, 1, 1, "trackstate");
849 mx_register(exp_transform, 2, 3, "transform");
850 mx_register(exp_url, 0, 0, "url");
851 mx_register(exp_user, 0, 0, "user");
852 mx_register(exp_userinfo, 1, 1, "userinfo");
853 mx_register(exp_version, 0, 0, "version");
854 mx_register(exp_volume, 1, 1, "volume");
855 mx_register(exp_when, 1, 1, "when");
856 mx_register(exp_who, 1, 1, "who");
857 mx_register_magic(exp_new, 1, 1, "new");
858 mx_register_magic(exp_playing, 0, 1, "playing");
859 mx_register_magic(exp_prefs, 2, 2, "prefs");
860 mx_register_magic(exp_queue, 1, 1, "queue");
861 mx_register_magic(exp_recent, 1, 1, "recent");
864 void disorder_macros_reset(void) {
865 /* Junk the old connection if there is one */
867 disorder_close(client);
868 /* Create a new connection */
869 client = disorder_new(0);
870 /* Forget everything we knew */