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 Locate a track by ID */
30 static struct queue_entry *findtrack(const char *id) {
31 struct queue_entry *q;
33 dcgi_lookup(DCGI_PLAYING);
34 if(dcgi_playing && !strcmp(dcgi_playing->id, id))
36 dcgi_lookup(DCGI_QUEUE);
37 for(q = dcgi_queue; q; q = q->next)
38 if(!strcmp(q->id, id))
40 dcgi_lookup(DCGI_RECENT);
41 for(q = dcgi_recent; q; q = q->next)
42 if(!strcmp(q->id, id))
47 /** @brief Return @p i as a string */
48 static const char *make_index(int i) {
51 byte_xasprintf(&s, "%d", i);
57 * Expands to the server's version string, or a (safe to use) error
58 * value if the server is unavailable or broken.
60 static int exp_server_version(int attribute((unused)) nargs,
61 char attribute((unused)) **args,
63 void attribute((unused)) *u) {
67 if(disorder_version(dcgi_client, (char **)&v))
68 v = "(cannot get version)";
70 v = "(server not running)";
71 return sink_writes(output, cgi_sgmlquote(v)) < 0 ? -1 : 0;
76 * Expands to the local version string.
78 static int exp_version(int attribute((unused)) nargs,
79 char attribute((unused)) **args,
81 void attribute((unused)) *u) {
82 return sink_writes(output,
83 cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0;
88 * Expands to the base URL of the web interface.
90 static int exp_url(int attribute((unused)) nargs,
91 char attribute((unused)) **args,
93 void attribute((unused)) *u) {
94 return sink_writes(output,
95 cgi_sgmlquote(config->url)) < 0 ? -1 : 0;
100 * Expands to the CGI argument NAME, or the empty string if there is
103 static int exp_arg(int attribute((unused)) nargs,
106 void attribute((unused)) *u) {
107 const char *s = cgi_get(args[0]);
109 return sink_writes(output,
110 cgi_sgmlquote(s)) < 0 ? -1 : 0;
117 * Expands to the logged-in username (which might be "guest"), or to
118 * the empty string if not connected.
120 static int exp_user(int attribute((unused)) nargs,
121 char attribute((unused)) **args,
123 void attribute((unused)) *u) {
126 if(dcgi_client && (user = disorder_user(dcgi_client)))
127 return sink_writes(output, cgi_sgmlquote(user)) < 0 ? -1 : 0;
131 /* @part{TRACK|ID}{PART}{CONTEXT}
133 * Expands to a track name part.
135 * A track may be identified by name or by queue ID.
137 * CONTEXT may be omitted. If it is then 'display' is assumed.
139 * If the CONTEXT is 'short' then the 'display' part is looked up, and the
140 * result truncated according to the length defined by the short_display
141 * configuration directive.
143 static int exp_part(int nargs,
146 void attribute((unused)) *u) {
147 const char *track = args[0], *part = args[1];
148 const char *context = nargs > 2 ? args[2] : "display";
151 if(track[0] != '/') {
152 struct queue_entry *q = findtrack(track);
160 && !disorder_part(dcgi_client, &s,
162 !strcmp(context, "short") ? "display" : context,
164 return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
170 * SGML-quotes STRING. Note that most expansion results are already suitable
171 * quoted, so this expansion is usually not required.
173 static int exp_quote(int attribute((unused)) nargs,
176 void attribute((unused)) *u) {
177 return sink_writes(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0;
182 * Expands to the name of the submitter of track ID, which must be a playing
183 * track, in the queue, or in the recent list.
185 static int exp_who(int attribute((unused)) nargs,
188 void attribute((unused)) *u) {
189 struct queue_entry *q = findtrack(args[0]);
191 if(q && q->submitter)
192 return sink_writes(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0;
198 * Expands to the time a track started or is expected to start. The track must
199 * be a playing track, in the queue, or in the recent list.
201 static int exp_when(int attribute((unused)) nargs,
204 void attribute((unused)) *u) {
205 struct queue_entry *q = findtrack(args[0]);
206 const struct tm *w = 0;
210 case playing_isscratch:
211 case playing_unplayed:
214 w = localtime(&q->expected);
217 case playing_no_player:
219 case playing_scratched:
220 case playing_started:
222 case playing_quitting:
224 w = localtime(&q->played);
228 return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0;
230 return sink_writes(output, " ") < 0 ? -1 : 0;
235 * Expands to the length of a track, identified by its queue ID or its name.
236 * If it is the playing track (identified by ID) then the amount played so far
239 static int exp_length(int attribute((unused)) nargs,
242 void attribute((unused)) *u) {
243 struct queue_entry *q;
247 if(args[0][0] == '/')
248 /* Track identified by name */
251 /* Track identified by queue ID */
252 if(!(q = findtrack(args[0])))
254 if(q->state == playing_started || q->state == playing_paused)
255 if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0)
259 if(dcgi_client && disorder_length(dcgi_client, name, &length))
260 return sink_printf(output, "%ld:%02ld",
261 length / 60, length % 60) < 0 ? -1 : 0;
262 return sink_writes(output, " ") < 0 ? -1 : 0;
267 * Expands to "true" if track ID is removable (or scratchable, if it is the
268 * playing track) and "false" otherwise.
270 static int exp_removable(int attribute((unused)) nargs,
273 void attribute((unused)) *u) {
274 struct queue_entry *q = findtrack(args[0]);
275 /* TODO would be better to reject recent */
277 if(!q || !dcgi_client)
278 return mx_bool_result(output, 0);
279 dcgi_lookup(DCGI_RIGHTS);
280 return mx_bool_result(output,
281 (q == dcgi_playing ? right_scratchable : right_removable)
282 (dcgi_rights, disorder_user(dcgi_client), q));
287 * Expands to "true" if track ID is movable and "false" otherwise.
289 static int exp_movable(int attribute((unused)) nargs,
292 void attribute((unused)) *u) {
293 struct queue_entry *q = findtrack(args[0]);
294 /* TODO would be better to recent playing/recent */
296 if(!q || !dcgi_client)
297 return mx_bool_result(output, 0);
298 dcgi_lookup(DCGI_RIGHTS);
299 return mx_bool_result(output,
300 right_movable(dcgi_rights, disorder_user(dcgi_client), q));
303 /* @playing{TEMPLATE}
305 * Expands to TEMPLATE, with:
306 * - @id@ expanded to the queue ID of the playing track
307 * - @track@ expanded to its UNQUOTED name
309 * If no track is playing expands to nothing.
311 * TEMPLATE is optional. If it is left out then instead expands to the queue
312 * ID of the playing track.
314 static int exp_playing(int nargs,
315 const struct mx_node **args,
318 dcgi_lookup(DCGI_PLAYING);
322 return sink_writes(output, dcgi_playing->id) < 0 ? -1 : 0;
323 return mx_expand(mx_rewritel(args[0],
324 "id", dcgi_playing->id,
325 "track", dcgi_playing->track,
332 * For each track in the queue, expands TEMPLATE with the following expansions:
333 * - @id@ to the queue ID of the track
334 * - @track@ to the UNQUOTED track name
335 * - @index@ to the track number from 0
336 * - @parity@ to "even" or "odd" alternately
337 * - @first@ to "true" on the first track and "false" otherwise
338 * - @last@ to "true" on the last track and "false" otherwise
340 static int exp_queue(int attribute((unused)) nargs,
341 const struct mx_node **args,
344 struct queue_entry *q;
347 dcgi_lookup(DCGI_QUEUE);
348 for(q = dcgi_queue, i = 0; q; q = q->next, ++i)
349 if((rc = mx_expand(mx_rewritel(args[0],
352 "index", make_index(i),
353 "parity", i % 2 ? "odd" : "even",
354 "first", q == dcgi_queue ? "true" : "false",
355 "last", q->next ? "false" : "true",
364 * For each track in the recently played list, expands TEMPLATE with the
365 * following expansions:
366 * - @id@ to the queue ID of the track
367 * - @track@ to the UNQUOTED track name
368 * - @index@ to the track number from 0
369 * - @parity@ to "even" or "odd" alternately
370 * - @first@ to "true" on the first track and "false" otherwise
371 * - @last@ to "true" on the last track and "false" otherwise
373 static int exp_recent(int attribute((unused)) nargs,
374 const struct mx_node **args,
377 struct queue_entry *q;
380 dcgi_lookup(DCGI_RECENT);
381 for(q = dcgi_recent, i = 0; q; q = q->next, ++i)
382 if((rc = mx_expand(mx_rewritel(args[0],
385 "index", make_index(i),
386 "parity", i % 2 ? "odd" : "even",
387 "first", q == dcgi_recent ? "true" : "false",
388 "last", q->next ? "false" : "true",
397 * For each track in the newly added list, expands TEMPLATE wit the following
399 * - @track@ to the UNQUOTED track name
400 * - @index@ to the track number from 0
401 * - @parity@ to "even" or "odd" alternately
402 * - @first@ to "true" on the first track and "false" otherwise
403 * - @last@ to "true" on the last track and "false" otherwise
405 * Note that unlike @playing@, @queue@ and @recent@ which are otherwise
406 * superficially similar, there is no @id@ sub-expansion here.
408 static int exp_new(int attribute((unused)) nargs,
409 const struct mx_node **args,
414 dcgi_lookup(DCGI_NEW);
415 /* TODO perhaps we should generate an ID value for tracks in the new list */
416 for(i = 0; i < dcgi_nnew; ++i)
417 if((rc = mx_expand(mx_rewritel(args[0],
418 "track", dcgi_new[i],
419 "index", make_index(i),
420 "parity", i % 2 ? "odd" : "even",
421 "first", i == 0 ? "true" : "false",
422 "last", i == dcgi_nnew - 1 ? "false" : "true",
431 * Expands to the volume in a given channel. CHANNEL must be "left" or
434 static int exp_volume(int attribute((unused)) nargs,
437 void attribute((unused)) *u) {
438 dcgi_lookup(DCGI_VOLUME);
439 return sink_printf(output, "%d",
440 !strcmp(args[0], "left")
441 ? dcgi_volume_left : dcgi_volume_right) < 0 ? -1 : 0;
446 * Expands to "true" if there is a playing track, otherwise "false".
448 static int exp_isplaying(int attribute((unused)) nargs,
449 char attribute((unused)) **args,
451 void attribute((unused)) *u) {
452 dcgi_lookup(DCGI_PLAYING);
453 return mx_bool_result(output, !!dcgi_playing);
458 * Expands to "true" if there the queue is nonempty, otherwise "false".
460 static int exp_isqueue(int attribute((unused)) nargs,
461 char attribute((unused)) **args,
463 void attribute((unused)) *u) {
464 dcgi_lookup(DCGI_QUEUE);
465 return mx_bool_result(output, !!dcgi_queue);
470 * Expands to "true" if there the recently played list is nonempty, otherwise
473 static int exp_isrecent(int attribute((unused)) nargs,
474 char attribute((unused)) **args,
476 void attribute((unused)) *u) {
477 dcgi_lookup(DCGI_RECENT);
478 return mx_bool_result(output, !!dcgi_recent);
483 * Expands to "true" if there the newly added track list is nonempty, otherwise
486 static int exp_isnew(int attribute((unused)) nargs,
487 char attribute((unused)) **args,
489 void attribute((unused)) *u) {
490 dcgi_lookup(DCGI_NEW);
491 return mx_bool_result(output, !!dcgi_nnew);
496 * Expands to a track preference.
498 static int exp_pref(int attribute((unused)) nargs,
501 void attribute((unused)) *u) {
504 if(dcgi_client && !disorder_get(dcgi_client, args[0], args[1], &value))
505 return sink_writes(output, cgi_sgmlquote(value)) < 0 ? -1 : 0;
509 /* @prefs{TRACK}{TEMPLATE}
511 * For each track preference of track TRACK, expands TEMPLATE with the
512 * following expansions:
513 * - @name@ to the UNQUOTED preference name
514 * - @index@ to the preference number from 0
515 * - @value@ to the UNQUOTED preference value
516 * - @parity@ to "even" or "odd" alternately
517 * - @first@ to "true" on the first preference and "false" otherwise
518 * - @last@ to "true" on the last preference and "false" otherwise
520 * Use @quote@ to quote preference names and values where necessary; see below.
522 static int exp_prefs(int attribute((unused)) nargs,
523 const struct mx_node **args,
527 struct kvp *k, *head;
530 if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)")))
532 if(!dcgi_client || disorder_prefs(dcgi_client, track, &head))
534 for(k = head, i = 0; k; k = k->next, ++i)
535 if((rc = mx_expand(mx_rewritel(args[1],
536 "index", make_index(i),
537 "parity", i % 2 ? "odd" : "even",
540 "first", k == head ? "true" : "false",
541 "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_writes(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(dcgi_client, &e);
575 return mx_bool_result(output, e);
580 * Expands to "true" if random play is enabled, otherwise "false".
582 static int exp_random_enabled(int attribute((unused)) nargs,
583 char attribute((unused)) **args,
585 void attribute((unused)) *u) {
589 disorder_random_enabled(dcgi_client, &e);
590 return mx_bool_result(output, e);
593 /* @trackstate{TRACK}
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(dcgi_client, &track, args[0]))
609 dcgi_lookup(DCGI_PLAYING);
610 if(dcgi_playing && !strcmp(track, dcgi_playing->track))
611 return sink_writes(output, "playing") < 0 ? -1 : 0;
612 dcgi_lookup(DCGI_QUEUE);
613 for(q = dcgi_queue; q; q = q->next)
614 if(!strcmp(track, q->track))
615 return sink_writes(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 sink_writes(output, cgi_thisurl(config->url)) < 0 ? -1 : 0;
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(dcgi_client && !disorder_resolve(dcgi_client, &r, args[0]))
644 return sink_writes(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) {
657 dcgi_lookup(DCGI_PLAYING);
658 return mx_bool_result(output, (dcgi_playing
659 && dcgi_playing->state == playing_paused));
664 * Expands to the current state of track ID.
666 static int exp_state(int attribute((unused)) nargs,
669 void attribute((unused)) *u) {
670 struct queue_entry *q = findtrack(args[0]);
673 return sink_writes(output, playing_states[q->state]) < 0 ? -1 : 0;
677 /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
679 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
680 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
682 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
683 * the user has the right and "false" otherwise.
685 * If there is no connection to the server then expands to nothing (in all
688 static int exp_right(int nargs,
689 const struct mx_node **args,
698 dcgi_lookup(DCGI_RIGHTS);
699 if((rc = mx_expandstr(args[0], &right, u, "argument #0 (RIGHT)")))
701 if(parse_rights(right, &r, 1/*report*/))
703 /* Single-argument form */
705 return mx_bool_result(output, !!(r & dcgi_rights));
706 /* Multiple argument form */
708 return mx_expand(args[1], output, u);
710 return mx_expand(args[2], output, u);
714 /* @userinfo{PROPERTY}
716 * Expands to the named property of the current user.
718 static int exp_userinfo(int attribute((unused)) nargs,
721 void attribute((unused)) *u) {
725 && !disorder_userinfo(dcgi_client, disorder_user(dcgi_client),
727 return sink_writes(output, v) < 0 ? -1 : 0;
733 * Expands to the latest error string.
735 static int exp_error(int attribute((unused)) nargs,
736 char attribute((unused)) **args,
738 void attribute((unused)) *u) {
739 return sink_writes(output, cgi_sgmlquote(dcgi_error_string)) < 0 ? -1 : 0;
742 /** @brief Register DisOrder-specific expansions */
743 void dcgi_expansions(void) {
744 mx_register("arg", 1, 1, exp_arg);
745 mx_register("enabled", 0, 0, exp_enabled);
746 mx_register("error", 0, 0, exp_error);
747 mx_register("isnew", 0, 0, exp_isnew);
748 mx_register("isplaying", 0, 0, exp_isplaying);
749 mx_register("isplaying", 0, 0, exp_isqueue);
750 mx_register("isrecent", 0, 0, exp_isrecent);
751 mx_register("length", 1, 1, exp_length);
752 mx_register("movable", 1, 1, exp_movable);
753 mx_register("part", 2, 3, exp_part);
754 mx_register("paused", 0, 0, exp_paused);
755 mx_register("pref", 2, 2, exp_pref);
756 mx_register("quote", 1, 1, exp_quote);
757 mx_register("random-enabled", 0, 0, exp_random_enabled);
758 mx_register("removable", 1, 1, exp_removable);
759 mx_register("resolve", 1, 1, exp_resolve);
760 mx_register("server-version", 0, 0, exp_server_version);
761 mx_register("state", 1, 1, exp_state);
762 mx_register("thisurl", 0, 0, exp_thisurl);
763 mx_register("trackstate", 1, 1, exp_trackstate);
764 mx_register("transform", 2, 3, exp_transform);
765 mx_register("url", 0, 0, exp_url);
766 mx_register("user", 0, 0, exp_user);
767 mx_register("userinfo", 1, 1, exp_userinfo);
768 mx_register("version", 0, 0, exp_version);
769 mx_register("volume", 1, 1, exp_volume);
770 mx_register("when", 1, 1, exp_when);
771 mx_register("who", 1, 1, exp_who);
772 mx_register_magic("new", 1, 1, exp_new);
773 mx_register_magic("playing", 0, 1, exp_playing);
774 mx_register_magic("prefs", 2, 2, exp_prefs);
775 mx_register_magic("queue", 1, 1, exp_queue);
776 mx_register_magic("recent", 1, 1, exp_recent);
777 mx_register_magic("right", 1, 3, exp_right);