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"
33 /** @brief For error template */
36 /** @brief Locate a track by ID */
37 static struct queue_entry *findtrack(const char *id) {
38 struct queue_entry *q;
41 if(playing && !strcmp(playing->id, id))
44 for(q = queue; q; q = q->next)
45 if(!strcmp(q->id, id))
48 for(q = recent; q; q = q->next)
49 if(!strcmp(q->id, id))
53 /** @brief Return @p i as a string */
54 static const char *make_index(int i) {
57 byte_xasprintf(&s, "%d", i);
63 * Expands to the server's version string, or a (safe to use) error
64 * value if the server is unavailable or broken.
66 static int exp_server_version(int attribute((unused)) nargs,
67 char attribute((unused)) **args,
69 void attribute((unused)) *u) {
73 if(disorder_version(client, (char **)&v))
74 v = "(cannot get version)";
76 v = "(server not running)";
77 return sink_write(output, cgi_sgmlquote(v)) < 0 ? -1 : 0;
82 * Expands to the local version string.
84 static int exp_version(int attribute((unused)) nargs,
85 char attribute((unused)) **args,
87 void attribute((unused)) *u) {
88 return sink_write(output,
89 cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0;
94 * Expands to the base URL of the web interface.
96 static int exp_url(int attribute((unused)) nargs,
97 char attribute((unused)) **args,
99 void attribute((unused)) *u) {
100 return sink_write(output,
101 cgi_sgmlquote(config->url)) < 0 ? -1 : 0;
106 * Expands to the CGI argument NAME, or the empty string if there is
109 static int exp_arg(int attribute((unused)) nargs,
112 void attribute((unused)) *u) {
113 const char *s = cgi_get(args[0]);
115 return sink_write(output,
116 cgi_sgmlquote(s)) < 0 ? -1 : 0;
123 * Expands to the logged-in username (which might be "guest"), or to
124 * the empty string if not connected.
126 static int exp_user(int attribute((unused)) nargs,
127 char attribute((unused)) **args,
129 void attribute((unused)) *u) {
132 if(client && (u = disorder_user(client)))
133 return sink_write(output, cgi_sgmlquote(u)) < 0 ? -1 : 0;
137 /* @part{TRACK|ID}{PART}{CONTEXT}
139 * Expands to a track name part.
141 * A track may be identified by name or by queue ID.
143 * CONTEXT may be omitted. If it is then 'display' is assumed.
145 * If the CONTEXT is 'short' then the 'display' part is looked up, and the
146 * result truncated according to the length defined by the short_display
147 * configuration directive.
149 static int exp_part(int nargs,
152 void attribute((unused)) *u) {
153 const char *track = args[0], *part = args[1];
154 const char *context = nargs > 2 ? args[2] : "display";
157 if(track[0] != '/') {
158 struct queue_entry *q = findtrack(track);
166 && !disorder_get(client, &s, track,
167 !strcmp(context, "short") ? "display" : context,
169 return sink_write(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
175 * SGML-quotes STRING. Note that most expansion results are already suitable
176 * quoted, so this expansion is usually not required.
178 static int exp_quote(int attribute((unused)) nargs,
181 void attribute((unused)) *u) {
182 return sink_write(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0;
187 * Expands to the name of the submitter of track ID, which must be a playing
188 * track, in the queue, or in the recent list.
190 static int exp_who(int attribute((unused)) nargs,
193 void attribute((unused)) *u) {
194 struct queue_entry *q = findtrack(args[0]);
196 if(q && q->submitter)
197 return sink_write(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0;
203 * Expands to the time a track started or is expected to start. The track must
204 * be a playing track, in the queue, or in the recent list.
206 static int exp_when(int attribute((unused)) nargs,
209 void attribute((unused)) *u) {
210 struct queue_entry *q = findtrack(args[0]);
211 const struct tm *w = 0;
215 case playing_isscratch:
216 case playing_unplayed:
219 w = localtime(&q->expected);
222 case playing_no_player:
224 case playing_scratched:
225 case playing_started:
227 case playing_quitting:
229 w = localtime(&q->played);
233 return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0;
235 return sink_write(output, " ") < 0 ? -1 : 0;
240 * Expands to the length of a track, identified by its queue ID or its name.
241 * If it is the playing track (identified by ID) then the amount played so far
244 static int exp_length(int attribute((unused)) nargs,
247 void attribute((unused)) *u) {
248 struct queue_entry *q;
252 if(args[0][0] == '/')
253 /* Track identified by name */
256 /* Track identified by queue ID */
257 if(!(q = findtrack(args[0])))
259 if(q->state == play_start || q->state == playing_paused)
260 if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0)
264 if(client && disorder_length(client, name, &length))
265 return sink_printf(output, "%ld:%02ld",
266 length / 60, length % 60) < 0 ? -1 : 0;
267 return sink_write(output, " ") < 0 ? -1 : 0;
272 * Expands to "true" if track ID is removable (or scratchable, if it is the
273 * playing track) and "false" otherwise.
275 static int exp_removable(int attribute((unused)) nargs,
278 void attribute((unused)) *u) {
279 struct queue_entry *q = findtrack(args[0]);
280 /* TODO would be better to reject recent */
283 return mx_bool_result(output, 0);
285 return mx_bool_result(output,
286 (q == playing ? right_scratchable : right_removable)
287 (rights, disorder_user(client), q));
292 * Expands to "true" if track ID is movable and "false" otherwise.
294 static int exp_movable(int attribute((unused)) nargs,
297 void attribute((unused)) *u) {
298 struct queue_entry *q = findtrack(args[0]);
299 /* TODO would be better to recent playing/recent */
302 return mx_bool_result(output, 0);
304 return mx_bool_result(output,
305 right_movable(rights, disorder_user(client), q));
308 /* @playing{TEMPLATE}
310 * Expands to TEMPLATE, with:
311 * - @id@ expanded to the queue ID of the playing track
312 * - @track@ expanded to its UNQUOTED name
314 * If no track is playing expands to nothing.
316 * TEMPLATE is optional. If it is left out then instead expands to the queue
317 * ID of the playing track.
319 static int exp_playing(int nargs,
320 const struct mx_node **args,
322 void attribute((unused)) *u) {
327 return sink_write(output, playing->id) < 0 ? -1 : 0;
328 return mx_rewritel(args[0],
330 "track", playing->track,
336 * For each track in the queue, expands TEMPLATE with the following expansions:
337 * - @id@ to the queue ID of the track
338 * - @track@ to the UNQUOTED track name
339 * - @index@ to the track number from 0
340 * - @parity@ to "even" or "odd" alternately
341 * - @first@ to "true" on the first track and "false" otherwise
342 * - @last@ to "true" on the last track and "false" otherwise
344 static int exp_queue(int attribute((unused)) nargs,
345 const struct mx_node **args,
347 void attribute((unused)) *u) {
348 struct queue_entry *q;
352 for(q = queue, i = 0; q; q = q->next, ++i)
353 if((rc = mx_rewritel(args[0],
356 "index", make_index(i),
357 "parity", i % 2 ? "odd" : "even",
358 "first", q == queue ? "true" : "false",
359 "last", q->next ? "false" : "true",
367 * For each track in the recently played list, expands TEMPLATE with the
368 * following expansions:
369 * - @id@ to the queue ID of the track
370 * - @track@ to the UNQUOTED track name
371 * - @index@ to the track number from 0
372 * - @parity@ to "even" or "odd" alternately
373 * - @first@ to "true" on the first track and "false" otherwise
374 * - @last@ to "true" on the last track and "false" otherwise
376 static int exp_recent(int attribute((unused)) nargs,
377 const struct mx_node **args,
379 void attribute((unused)) *u) {
380 struct queue_entry *q;
384 for(q = recent, i = 0; q; q = q->next, ++i)
385 if((rc = mx_rewritel(args[0],
388 "index", make_index(i),
389 "parity", i % 2 ? "odd" : "even",
390 "first", q == recent ? "true" : "false",
391 "last", q->next ? "false" : "true",
399 * For each track in the newly added list, expands TEMPLATE wit the following
401 * - @track@ to the UNQUOTED track name
402 * - @index@ to the track number from 0
403 * - @parity@ to "even" or "odd" alternately
404 * - @first@ to "true" on the first track and "false" otherwise
405 * - @last@ to "true" on the last track and "false" otherwise
407 * Note that unlike @playing@, @queue@ and @recent@ which are otherwise
408 * superficially similar, there is no @id@ sub-expansion here.
410 static int exp_new(int attribute((unused)) nargs,
411 const struct mx_node **args,
413 void attribute((unused)) *u) {
414 struct queue_entry *q;
418 /* TODO perhaps we should generate an ID value for tracks in the new list */
419 for(i = 0; i < nnew; ++i)
420 if((rc = mx_rewritel(args[0],
422 "index", make_index(i),
423 "parity", i % 2 ? "odd" : "even",
424 "first", i == 0 ? "true" : "false",
425 "last", i == nnew - 1 ? "false" : "true",
433 * Expands to the volume in a given channel. CHANNEL must be "left" or
436 static int exp_volume(int attribute((unused)) nargs,
439 void attribute((unused)) *u) {
441 return sink_write(output, "%d",
442 !strcmp(args[0], "left")
443 ? volume_left : volume_right) < 0 ? -1 : 0;
448 * Expands to "true" if there is a playing track, otherwise "false".
450 static int exp_isplaying(int attribute((unused)) nargs,
451 char attribute((unused)) **args,
453 void attribute((unused)) *u) {
455 return mx_bool_result(output, !!playing);
460 * Expands to "true" if there the queue is nonempty, otherwise "false".
462 static int exp_isqueue(int attribute((unused)) nargs,
463 char attribute((unused)) **args,
465 void attribute((unused)) *u) {
467 return mx_bool_result(output, !!queue);
472 * Expands to "true" if there the recently played list is nonempty, otherwise
475 static int exp_isrecent(int attribute((unused)) nargs,
476 char attribute((unused)) **args,
478 void attribute((unused)) *u) {
480 return mx_bool_result(output, !!recent);
485 * Expands to "true" if there the newly added track list is nonempty, otherwise
488 static int exp_isnew(int attribute((unused)) nargs,
489 char attribute((unused)) **args,
491 void attribute((unused)) *u) {
493 return mx_bool_result(output, !!nnew);
498 * Expands to a track preference.
500 static int exp_pref(int attribute((unused)) nargs,
503 void attribute((unused)) *u) {
506 if(client && !disorder_get(client, args[0], args[1], &value))
507 return sink_write(output, cgi_sgmlquote(value)) < 0 ? -1 : 0;
510 /* @prefs{TRACK}{TEMPLATE}
512 * For each track preference of track TRACK, expands TEMPLATE with the
513 * following expansions:
514 * - @name@ to the UNQUOTED preference name
515 * - @index@ to the preference number from 0
516 * - @value@ to the UNQUOTED preference value
517 * - @parity@ to "even" or "odd" alternately
518 * - @first@ to "true" on the first preference and "false" otherwise
519 * - @last@ to "true" on the last preference and "false" otherwise
521 * Use @quote@ to quote preference names and values where necessary; see below.
523 static int exp_prefs(int attribute((unused)) nargs,
524 const struct mx_node **args,
526 void attribute((unused)) *u) {
528 struct kvp *k, *head;
531 if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)")))
533 if(!client || disorder_prefs(client, track, &head))
535 for(k = head, i = 0; k; k = k->next, ++i)
536 if((rc = mx_rewritel(args[1],
537 "index", make_index(i),
538 "parity", i % 2 ? "odd" : "even",
541 "first", k == head ? "true" : "false",
542 "last", k->next ? "false" : "true",
548 /* @transform{TRACK}{TYPE}{CONTEXT}
550 * Transforms a track name (if TYPE is "track") or directory name (if type is
551 * "dir"). CONTEXT should be the context, if it is left out then "display" is
554 static int exp_transform(int nargs,
557 void attribute((unused)) *u) {
558 const char *t = trackname_transform(args[1], args[0],
559 (nargs > 2 ? args[2] : "display")));
560 return sink_write(output, cgi_sgmlquote(t)) < 0 ? -1 : 0;
565 * Expands to "true" if playing is enabled, otherwise "false".
567 static int exp_enabled(int attribute((unused)) nargs,
568 char attribute((unused)) **args,
570 void attribute((unused)) *u) {
574 disorder_enabled(client, &enabled);
575 return mx_bool_result(output, enabled);
580 * Expands to "true" if random play is enabled, otherwise "false".
582 static int exp_enabled(int attribute((unused)) nargs,
583 char attribute((unused)) **args,
585 void attribute((unused)) *u) {
589 disorder_random_enabled(client, &enabled);
590 return mx_bool_result(output, enabled);
595 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
596 * the queue, otherwise to nothing.
598 static int exp_trackstate(int attribute((unused)) nargs,
601 void attribute((unused)) *u) {
603 struct queue_entry *q;
607 if(disorder_resolve(client, &track, args[0]))
610 if(playing && !strcmp(track, playing->track))
611 return sink_write(output, "playing") < 0 ? -1 : 0;
613 for(q = queue; q; q = q->next)
614 if(!strcmp(track, q->track))
615 return sink_write(output, "queued") < 0 ? -1 : 0;
621 * Expands to an UNQUOTED URL which points back to the current page. (NB it
622 * might not be byte for byte identical - for instance, CGI arguments might be
625 static int exp_thisurl(int attribute((unused)) nargs,
626 char attribute((unused)) **args,
628 void attribute((unused)) *u) {
629 return cgi_thisurl(config->url);
634 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
635 * nothing if it is not a valid track.
637 static int exp_resolve(int attribute((unused)) nargs,
640 void attribute((unused)) *u) {
643 if(client && !disorder_resolve(client, &r, args[0]))
644 return sink_write(output, r) < 0 ? -1 : 0;
650 * Expands to "true" if the playing track is paused, to "false" if it is
651 * playing (or if there is no playing track at all).
653 static int exp_paused(int attribute((unused)) nargs,
654 char attribute((unused)) **args,
656 void attribute((unused)) *u) {
658 return mx_bool_result(output, playing && playing->state == playing_paused);
663 * Expands to the current state of track ID.
665 static int exp_state(int attribute((unused)) nargs,
668 void attribute((unused)) *u) {
669 struct queue_entry *q = findtrack(args[0]);
672 return sink_write(output, playing_states[q->state]) < 0 ? -1 : 0;
676 /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
678 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
679 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
681 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
682 * the user has the right and "false" otherwise.
684 * If there is no connection to the server then expands to nothing (in all
687 static int exp_right(int nargs,
688 const struct mx_node **args,
690 void attribute((unused)) *u) {
697 if((rc = mx_expandstr(args[0], &rightname, u, "argument #0 (RIGHT)")))
699 if(parse_rights(right, &r, 1/*report*/))
701 /* Single-argument form */
703 return mx_bool_result(output, !!(r & rights));
704 /* Multiple argument form */
706 return mx_expandl(args[1], (char *)0);
708 return mx_expandl(args[2], (char *)0);
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) {
722 if(client && !disorder_userinfo(client, disorder_user(client), args[0], &v))
723 return sink_write(output, v) < 0 ? -1 : 0;
729 * Expands to the latest error string.
731 static int exp_error(int attribute((unused)) nargs,
732 char attribute((unused)) **args,
734 void attribute((unused)) *u) {
735 return sink_write(output, cgi_sgmlquote(error_string)) < 0 ? -1 : 0;
738 /* @userinfo{PROPERTY}@
740 /** @brief Register DisOrder-specific expansions */
741 void register_disorder_expansions(void) {
742 mx_register(exp_arg, 1, 1, "arg");
743 mx_register(exp_enabled, 0, 0, "enabled");
744 mx_register(exp_error, 0, 0, "error");
745 mx_register(exp_isnew, 0, 0, "isnew");
746 mx_register(exp_isplaying, 0, 0, "isplaying");
747 mx_register(exp_isqueue, 0, 0, "isplaying");
748 mx_register(exp_isrecent, 0, 0, "isrecent");
749 mx_register(exp_length, 1, 1, "length");
750 mx_register(exp_movable, 1, 1, "movable");
751 mx_register(exp_part, 2, 3, "part");
752 mx_register(exp_pref, 2, 2, "pref");
753 mx_register(exp_quote, 1, 1, "quote");
754 mx_register(exp_random_enabled, 0, 0, "random-enabled");
755 mx_register(exp_removable, 1, 1, "removable");
756 mx_register(exp_resolve, 1, 1, "resolve");
757 mx_register(exp_right, 1, 3, "right");
758 mx_register(exp_server_version, 0, 0, "server-version");
759 mx_register(exp_state, 1, 1, "state");
760 mx_register(exp_thisurl, 0, 0, "thisurl");
761 mx_register(exp_trackstate, 1, 1, "trackstate");
762 mx_register(exp_transform, 2, 3, "transform");
763 mx_register(exp_url, 0, 0, "url");
764 mx_register(exp_user, 0, 0, "user");
765 mx_register(exp_userinfo, 1, 1, "userinfo");
766 mx_register(exp_version, 0, 0, "version");
767 mx_register(exp_volume, 1, 1, "volume");
768 mx_register(exp_when, 1, 1, "when");
769 mx_register(exp_who, 1, 1, "who");
770 mx_register_magic(exp_new, 1, 1, "new");
771 mx_register_magic(exp_playing, 0, 1, "playing");
772 mx_register_magic(exp_prefs, 2, 2, "prefs");
773 mx_register_magic(exp_queue, 1, 1, "queue");
774 mx_register_magic(exp_recent, 1, 1, "recent");
777 void disorder_macros_reset(void) {
778 /* Junk the old connection if there is one */
780 disorder_close(client);
781 /* Create a new connection */
782 client = disorder_new(0);
783 /* Forget everything we knew */