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]);
110 return sink_writes(output,
111 cgi_sgmlquote(s)) < 0 ? -1 : 0;
118 * Expands to the logged-in username (which might be "guest"), or to
119 * the empty string if not connected.
121 static int exp_user(int attribute((unused)) nargs,
122 char attribute((unused)) **args,
124 void attribute((unused)) *u) {
127 if(dcgi_client && (user = disorder_user(dcgi_client)))
128 return sink_writes(output, cgi_sgmlquote(user)) < 0 ? -1 : 0;
132 /* @part{TRACK|ID}{PART}{CONTEXT}
134 * Expands to a track name part.
136 * A track may be identified by name or by queue ID.
138 * CONTEXT may be omitted. If it is then 'display' is assumed.
140 * If the CONTEXT is 'short' then the 'display' part is looked up, and the
141 * result truncated according to the length defined by the short_display
142 * configuration directive.
144 static int exp_part(int nargs,
147 void attribute((unused)) *u) {
148 const char *track = args[0], *part = args[1];
149 const char *context = nargs > 2 ? args[2] : "display";
152 if(track[0] != '/') {
153 struct queue_entry *q = findtrack(track);
161 && !disorder_part(dcgi_client, &s,
163 !strcmp(context, "short") ? "display" : context,
165 return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
171 * SGML-quotes STRING. Note that most expansion results are already suitable
172 * quoted, so this expansion is usually not required.
174 static int exp_quote(int attribute((unused)) nargs,
177 void attribute((unused)) *u) {
178 return sink_writes(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0;
183 * Expands to the name of the submitter of track ID, which must be a playing
184 * track, in the queue, or in the recent list.
186 static int exp_who(int attribute((unused)) nargs,
189 void attribute((unused)) *u) {
190 struct queue_entry *q = findtrack(args[0]);
192 if(q && q->submitter)
193 return sink_writes(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0;
199 * Expands to the time a track started or is expected to start. The track must
200 * be a playing track, in the queue, or in the recent list.
202 static int exp_when(int attribute((unused)) nargs,
205 void attribute((unused)) *u) {
206 struct queue_entry *q = findtrack(args[0]);
207 const struct tm *w = 0;
211 case playing_isscratch:
212 case playing_unplayed:
215 w = localtime(&q->expected);
218 case playing_no_player:
220 case playing_scratched:
221 case playing_started:
223 case playing_quitting:
225 w = localtime(&q->played);
229 return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0;
231 return sink_writes(output, " ") < 0 ? -1 : 0;
236 * Expands to the length of a track, identified by its queue ID or its name.
237 * If it is the playing track (identified by ID) then the amount played so far
240 static int exp_length(int attribute((unused)) nargs,
243 void attribute((unused)) *u) {
244 struct queue_entry *q;
248 if(args[0][0] == '/')
249 /* Track identified by name */
252 /* Track identified by queue ID */
253 if(!(q = findtrack(args[0])))
255 if(q->state == playing_started || q->state == playing_paused)
256 if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0)
260 if(dcgi_client && disorder_length(dcgi_client, name, &length))
261 return sink_printf(output, "%ld:%02ld",
262 length / 60, length % 60) < 0 ? -1 : 0;
263 return sink_writes(output, " ") < 0 ? -1 : 0;
268 * Expands to "true" if track ID is removable (or scratchable, if it is the
269 * playing track) and "false" otherwise.
271 static int exp_removable(int attribute((unused)) nargs,
274 void attribute((unused)) *u) {
275 struct queue_entry *q = findtrack(args[0]);
276 /* TODO would be better to reject recent */
278 if(!q || !dcgi_client)
279 return mx_bool_result(output, 0);
280 dcgi_lookup(DCGI_RIGHTS);
281 return mx_bool_result(output,
282 (q == dcgi_playing ? right_scratchable : right_removable)
283 (dcgi_rights, disorder_user(dcgi_client), q));
288 * Expands to "true" if track ID is movable and "false" otherwise.
290 static int exp_movable(int attribute((unused)) nargs,
293 void attribute((unused)) *u) {
294 struct queue_entry *q = findtrack(args[0]);
295 /* TODO would be better to recent playing/recent */
297 if(!q || !dcgi_client)
298 return mx_bool_result(output, 0);
299 dcgi_lookup(DCGI_RIGHTS);
300 return mx_bool_result(output,
301 right_movable(dcgi_rights, disorder_user(dcgi_client), q));
304 /* @playing{TEMPLATE}
306 * Expands to TEMPLATE, with:
307 * - @id@ expanded to the queue ID of the playing track
308 * - @track@ expanded to its UNQUOTED name
310 * If no track is playing expands to nothing.
312 * TEMPLATE is optional. If it is left out then instead expands to the queue
313 * ID of the playing track.
315 static int exp_playing(int nargs,
316 const struct mx_node **args,
319 dcgi_lookup(DCGI_PLAYING);
323 return sink_writes(output, dcgi_playing->id) < 0 ? -1 : 0;
324 return mx_expand(mx_rewritel(args[0],
325 "id", dcgi_playing->id,
326 "track", dcgi_playing->track,
333 * For each track in the queue, expands TEMPLATE with the following expansions:
334 * - @id@ to the queue ID of the track
335 * - @track@ to the UNQUOTED track name
336 * - @index@ to the track number from 0
337 * - @parity@ to "even" or "odd" alternately
338 * - @first@ to "true" on the first track and "false" otherwise
339 * - @last@ to "true" on the last track and "false" otherwise
341 static int exp_queue(int attribute((unused)) nargs,
342 const struct mx_node **args,
345 struct queue_entry *q;
348 dcgi_lookup(DCGI_QUEUE);
349 for(q = dcgi_queue, i = 0; q; q = q->next, ++i)
350 if((rc = mx_expand(mx_rewritel(args[0],
353 "index", make_index(i),
354 "parity", i % 2 ? "odd" : "even",
355 "first", q == dcgi_queue ? "true" : "false",
356 "last", q->next ? "false" : "true",
365 * For each track in the recently played list, expands TEMPLATE with the
366 * following expansions:
367 * - @id@ to the queue ID of the track
368 * - @track@ to the UNQUOTED track name
369 * - @index@ to the track number from 0
370 * - @parity@ to "even" or "odd" alternately
371 * - @first@ to "true" on the first track and "false" otherwise
372 * - @last@ to "true" on the last track and "false" otherwise
374 static int exp_recent(int attribute((unused)) nargs,
375 const struct mx_node **args,
378 struct queue_entry *q;
381 dcgi_lookup(DCGI_RECENT);
382 for(q = dcgi_recent, i = 0; q; q = q->next, ++i)
383 if((rc = mx_expand(mx_rewritel(args[0],
386 "index", make_index(i),
387 "parity", i % 2 ? "odd" : "even",
388 "first", q == dcgi_recent ? "true" : "false",
389 "last", q->next ? "false" : "true",
398 * For each track in the newly added list, expands TEMPLATE wit the following
400 * - @track@ to the UNQUOTED track name
401 * - @index@ to the track number from 0
402 * - @parity@ to "even" or "odd" alternately
403 * - @first@ to "true" on the first track and "false" otherwise
404 * - @last@ to "true" on the last track and "false" otherwise
406 * Note that unlike @playing@, @queue@ and @recent@ which are otherwise
407 * superficially similar, there is no @id@ sub-expansion here.
409 static int exp_new(int attribute((unused)) nargs,
410 const struct mx_node **args,
415 dcgi_lookup(DCGI_NEW);
416 /* TODO perhaps we should generate an ID value for tracks in the new list */
417 for(i = 0; i < dcgi_nnew; ++i)
418 if((rc = mx_expand(mx_rewritel(args[0],
419 "track", dcgi_new[i],
420 "index", make_index(i),
421 "parity", i % 2 ? "odd" : "even",
422 "first", i == 0 ? "true" : "false",
423 "last", i == dcgi_nnew - 1 ? "false" : "true",
432 * Expands to the volume in a given channel. CHANNEL must be "left" or
435 static int exp_volume(int attribute((unused)) nargs,
438 void attribute((unused)) *u) {
439 dcgi_lookup(DCGI_VOLUME);
440 return sink_printf(output, "%d",
441 !strcmp(args[0], "left")
442 ? dcgi_volume_left : dcgi_volume_right) < 0 ? -1 : 0;
447 * Expands to "true" if there is a playing track, otherwise "false".
449 static int exp_isplaying(int attribute((unused)) nargs,
450 char attribute((unused)) **args,
452 void attribute((unused)) *u) {
453 dcgi_lookup(DCGI_PLAYING);
454 return mx_bool_result(output, !!dcgi_playing);
459 * Expands to "true" if there the queue is nonempty, otherwise "false".
461 static int exp_isqueue(int attribute((unused)) nargs,
462 char attribute((unused)) **args,
464 void attribute((unused)) *u) {
465 dcgi_lookup(DCGI_QUEUE);
466 return mx_bool_result(output, !!dcgi_queue);
471 * Expands to "true" if there the recently played list is nonempty, otherwise
474 static int exp_isrecent(int attribute((unused)) nargs,
475 char attribute((unused)) **args,
477 void attribute((unused)) *u) {
478 dcgi_lookup(DCGI_RECENT);
479 return mx_bool_result(output, !!dcgi_recent);
484 * Expands to "true" if there the newly added track list is nonempty, otherwise
487 static int exp_isnew(int attribute((unused)) nargs,
488 char attribute((unused)) **args,
490 void attribute((unused)) *u) {
491 dcgi_lookup(DCGI_NEW);
492 return mx_bool_result(output, !!dcgi_nnew);
497 * Expands to a track preference.
499 static int exp_pref(int attribute((unused)) nargs,
502 void attribute((unused)) *u) {
505 if(dcgi_client && !disorder_get(dcgi_client, args[0], args[1], &value))
506 return sink_writes(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,
528 struct kvp *k, *head;
531 if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)")))
533 if(!dcgi_client || disorder_prefs(dcgi_client, track, &head))
535 for(k = head, i = 0; k; k = k->next, ++i)
536 if((rc = mx_expand(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",
549 /* @transform{TRACK}{TYPE}{CONTEXT}
551 * Transforms a track name (if TYPE is "track") or directory name (if type is
552 * "dir"). CONTEXT should be the context, if it is left out then "display" is
555 static int exp_transform(int nargs,
558 void attribute((unused)) *u) {
559 const char *t = trackname_transform(args[1], args[0],
560 (nargs > 2 ? args[2] : "display"));
561 return sink_writes(output, cgi_sgmlquote(t)) < 0 ? -1 : 0;
566 * Expands to "true" if playing is enabled, otherwise "false".
568 static int exp_enabled(int attribute((unused)) nargs,
569 char attribute((unused)) **args,
571 void attribute((unused)) *u) {
575 disorder_enabled(dcgi_client, &e);
576 return mx_bool_result(output, e);
581 * Expands to "true" if random play is enabled, otherwise "false".
583 static int exp_random_enabled(int attribute((unused)) nargs,
584 char attribute((unused)) **args,
586 void attribute((unused)) *u) {
590 disorder_random_enabled(dcgi_client, &e);
591 return mx_bool_result(output, e);
594 /* @trackstate{TRACK}
596 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
597 * the queue, otherwise to nothing.
599 static int exp_trackstate(int attribute((unused)) nargs,
602 void attribute((unused)) *u) {
604 struct queue_entry *q;
608 if(disorder_resolve(dcgi_client, &track, args[0]))
610 dcgi_lookup(DCGI_PLAYING);
611 if(dcgi_playing && !strcmp(track, dcgi_playing->track))
612 return sink_writes(output, "playing") < 0 ? -1 : 0;
613 dcgi_lookup(DCGI_QUEUE);
614 for(q = dcgi_queue; q; q = q->next)
615 if(!strcmp(track, q->track))
616 return sink_writes(output, "queued") < 0 ? -1 : 0;
622 * Expands to an UNQUOTED URL which points back to the current page. (NB it
623 * might not be byte for byte identical - for instance, CGI arguments might be
626 static int exp_thisurl(int attribute((unused)) nargs,
627 char attribute((unused)) **args,
629 void attribute((unused)) *u) {
630 return sink_writes(output, cgi_thisurl(config->url)) < 0 ? -1 : 0;
635 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
636 * nothing if it is not a valid track.
638 static int exp_resolve(int attribute((unused)) nargs,
641 void attribute((unused)) *u) {
644 if(dcgi_client && !disorder_resolve(dcgi_client, &r, args[0]))
645 return sink_writes(output, r) < 0 ? -1 : 0;
651 * Expands to "true" if the playing track is paused, to "false" if it is
652 * playing (or if there is no playing track at all).
654 static int exp_paused(int attribute((unused)) nargs,
655 char attribute((unused)) **args,
657 void attribute((unused)) *u) {
658 dcgi_lookup(DCGI_PLAYING);
659 return mx_bool_result(output, (dcgi_playing
660 && dcgi_playing->state == playing_paused));
665 * Expands to the current state of track ID.
667 static int exp_state(int attribute((unused)) nargs,
670 void attribute((unused)) *u) {
671 struct queue_entry *q = findtrack(args[0]);
674 return sink_writes(output, playing_states[q->state]) < 0 ? -1 : 0;
678 /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
680 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
681 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
683 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
684 * the user has the right and "false" otherwise.
686 * If there is no connection to the server then expands to nothing (in all
689 static int exp_right(int nargs,
690 const struct mx_node **args,
699 dcgi_lookup(DCGI_RIGHTS);
700 if((rc = mx_expandstr(args[0], &right, u, "argument #0 (RIGHT)")))
702 if(parse_rights(right, &r, 1/*report*/))
704 /* Single-argument form */
706 return mx_bool_result(output, !!(r & dcgi_rights));
707 /* Multiple argument form */
709 return mx_expand(args[1], output, u);
711 return mx_expand(args[2], output, u);
715 /* @userinfo{PROPERTY}
717 * Expands to the named property of the current user.
719 static int exp_userinfo(int attribute((unused)) nargs,
722 void attribute((unused)) *u) {
726 && !disorder_userinfo(dcgi_client, disorder_user(dcgi_client),
728 return sink_writes(output, v) < 0 ? -1 : 0;
734 * Expands to the latest error string.
736 static int exp_error(int attribute((unused)) nargs,
737 char attribute((unused)) **args,
739 void attribute((unused)) *u) {
740 return sink_writes(output, cgi_sgmlquote(dcgi_error_string)) < 0 ? -1 : 0;
743 /** @brief Register DisOrder-specific expansions */
744 void dcgi_expansions(void) {
745 mx_register("arg", 1, 1, exp_arg);
746 mx_register("enabled", 0, 0, exp_enabled);
747 mx_register("error", 0, 0, exp_error);
748 mx_register("isnew", 0, 0, exp_isnew);
749 mx_register("isplaying", 0, 0, exp_isplaying);
750 mx_register("isplaying", 0, 0, exp_isqueue);
751 mx_register("isrecent", 0, 0, exp_isrecent);
752 mx_register("length", 1, 1, exp_length);
753 mx_register("movable", 1, 1, exp_movable);
754 mx_register("part", 2, 3, exp_part);
755 mx_register("paused", 0, 0, exp_paused);
756 mx_register("pref", 2, 2, exp_pref);
757 mx_register("quote", 1, 1, exp_quote);
758 mx_register("random-enabled", 0, 0, exp_random_enabled);
759 mx_register("removable", 1, 1, exp_removable);
760 mx_register("resolve", 1, 1, exp_resolve);
761 mx_register("server-version", 0, 0, exp_server_version);
762 mx_register("state", 1, 1, exp_state);
763 mx_register("thisurl", 0, 0, exp_thisurl);
764 mx_register("trackstate", 1, 1, exp_trackstate);
765 mx_register("transform", 2, 3, exp_transform);
766 mx_register("url", 0, 0, exp_url);
767 mx_register("user", 0, 0, exp_user);
768 mx_register("userinfo", 1, 1, exp_userinfo);
769 mx_register("version", 0, 0, exp_version);
770 mx_register("volume", 1, 1, exp_volume);
771 mx_register("when", 1, 1, exp_when);
772 mx_register("who", 1, 1, exp_who);
773 mx_register_magic("new", 1, 1, exp_new);
774 mx_register_magic("playing", 0, 1, exp_playing);
775 mx_register_magic("prefs", 2, 2, exp_prefs);
776 mx_register_magic("queue", 1, 1, exp_queue);
777 mx_register_magic("recent", 1, 1, exp_recent);
778 mx_register_magic("right", 1, 3, exp_right);