Quieten compiler
[disorder] / server / macros-disorder.c
1 /*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
4 *
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.
9 *
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.
14 *
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
18 * USA
19 */
20 /** @file server/macros-disorder.c
21 * @brief DisOrder-specific expansions
22 */
23
24 #include "disorder-cgi.h"
25
26 /** @brief For error template */
27 char *dcgi_error_string;
28
29 /** @brief Return @p i as a string */
30 static const char *make_index(int i) {
31 char *s;
32
33 byte_xasprintf(&s, "%d", i);
34 return s;
35 }
36
37 /* @server-version
38 *
39 * Expands to the server's version string, or a (safe to use) error
40 * value if the server is unavailable or broken.
41 */
42 static int exp_server_version(int attribute((unused)) nargs,
43 char attribute((unused)) **args,
44 struct sink *output,
45 void attribute((unused)) *u) {
46 const char *v;
47
48 if(dcgi_client) {
49 if(disorder_version(dcgi_client, (char **)&v))
50 v = "(cannot get version)";
51 } else
52 v = "(server not running)";
53 return sink_writes(output, cgi_sgmlquote(v)) < 0 ? -1 : 0;
54 }
55
56 /* @version
57 *
58 * Expands to the local version string.
59 */
60 static int exp_version(int attribute((unused)) nargs,
61 char attribute((unused)) **args,
62 struct sink *output,
63 void attribute((unused)) *u) {
64 return sink_writes(output,
65 cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0;
66 }
67
68 /* @url
69 *
70 * Expands to the base URL of the web interface.
71 */
72 static int exp_url(int attribute((unused)) nargs,
73 char attribute((unused)) **args,
74 struct sink *output,
75 void attribute((unused)) *u) {
76 return sink_writes(output,
77 cgi_sgmlquote(config->url)) < 0 ? -1 : 0;
78 }
79
80 /* @arg{NAME}
81 *
82 * Expands to the UNQUOTED form of CGI argument NAME, or the empty string if
83 * there is no such argument. Use @argq for a quick way to quote the argument.
84 */
85 static int exp_arg(int attribute((unused)) nargs,
86 char **args,
87 struct sink *output,
88 void attribute((unused)) *u) {
89 const char *s = cgi_get(args[0]);
90
91 if(s)
92 return sink_writes(output, s) < 0 ? -1 : 0;
93 else
94 return 0;
95 }
96
97 /* @argq{NAME}
98 *
99 * Expands to the (quoted) form of CGI argument NAME, or the empty string if
100 * there is no such argument. Use @arg for the unquoted argument.
101 */
102 static int exp_argq(int attribute((unused)) nargs,
103 char **args,
104 struct sink *output,
105 void attribute((unused)) *u) {
106 const char *s = cgi_get(args[0]);
107
108 if(s)
109 return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
110 else
111 return 0;
112 }
113
114 /* @user
115 *
116 * Expands to the logged-in username (which might be "guest"), or to
117 * the empty string if not connected.
118 */
119 static int exp_user(int attribute((unused)) nargs,
120 char attribute((unused)) **args,
121 struct sink *output,
122 void attribute((unused)) *u) {
123 const char *user;
124
125 if(dcgi_client && (user = disorder_user(dcgi_client)))
126 return sink_writes(output, cgi_sgmlquote(user)) < 0 ? -1 : 0;
127 return 0;
128 }
129
130 /* @part{TRACK|ID}{PART}{CONTEXT}
131 *
132 * Expands to a track name part.
133 *
134 * A track may be identified by name or by queue ID.
135 *
136 * CONTEXT may be omitted. If it is then 'display' is assumed.
137 *
138 * If the CONTEXT is 'short' then the 'display' part is looked up, and the
139 * result truncated according to the length defined by the short_display
140 * configuration directive.
141 */
142 static int exp_part(int nargs,
143 char **args,
144 struct sink *output,
145 void attribute((unused)) *u) {
146 const char *track = args[0], *part = args[1];
147 const char *context = nargs > 2 ? args[2] : "display";
148 char *s;
149
150 if(track[0] != '/') {
151 struct queue_entry *q = dcgi_findtrack(track);
152
153 if(q)
154 track = q->track;
155 else
156 return 0;
157 }
158 if(dcgi_client
159 && !disorder_part(dcgi_client, &s,
160 track,
161 !strcmp(context, "short") ? "display" : context,
162 part))
163 return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
164 return 0;
165 }
166
167 /* @quote{STRING}
168 *
169 * SGML-quotes STRING. Note that most expansion results are already suitable
170 * quoted, so this expansion is usually not required.
171 */
172 static int exp_quote(int attribute((unused)) nargs,
173 char **args,
174 struct sink *output,
175 void attribute((unused)) *u) {
176 return sink_writes(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0;
177 }
178
179 /* @who{ID}
180 *
181 * Expands to the name of the submitter of track ID, which must be a playing
182 * track, in the queue, or in the recent list.
183 */
184 static int exp_who(int attribute((unused)) nargs,
185 char **args,
186 struct sink *output,
187 void attribute((unused)) *u) {
188 struct queue_entry *q = dcgi_findtrack(args[0]);
189
190 if(q && q->submitter)
191 return sink_writes(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0;
192 return 0;
193 }
194
195 /* @when{ID}
196 *
197 * Expands to the time a track started or is expected to start. The track must
198 * be a playing track, in the queue, or in the recent list.
199 */
200 static int exp_when(int attribute((unused)) nargs,
201 char **args,
202 struct sink *output,
203 void attribute((unused)) *u) {
204 struct queue_entry *q = dcgi_findtrack(args[0]);
205 const struct tm *w = 0;
206
207 if(q) {
208 switch(q->state) {
209 case playing_isscratch:
210 case playing_unplayed:
211 case playing_random:
212 if(q->expected)
213 w = localtime(&q->expected);
214 break;
215 case playing_failed:
216 case playing_no_player:
217 case playing_ok:
218 case playing_scratched:
219 case playing_started:
220 case playing_paused:
221 case playing_quitting:
222 if(q->played)
223 w = localtime(&q->played);
224 break;
225 }
226 if(w)
227 return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0;
228 }
229 return sink_writes(output, "&nbsp;") < 0 ? -1 : 0;
230 }
231
232 /* @length{ID|TRACK}
233 *
234 * Expands to the length of a track, identified by its queue ID or its name.
235 * If it is the playing track (identified by ID) then the amount played so far
236 * is included.
237 */
238 static int exp_length(int attribute((unused)) nargs,
239 char **args,
240 struct sink *output,
241 void attribute((unused)) *u) {
242 struct queue_entry *q;
243 long length = 0;
244 const char *name;
245
246 if(args[0][0] == '/')
247 /* Track identified by name */
248 name = args[0];
249 else {
250 /* Track identified by queue ID */
251 if(!(q = dcgi_findtrack(args[0])))
252 return 0;
253 if(q->state == playing_started || q->state == playing_paused)
254 if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0)
255 return -1;
256 name = q->track;
257 }
258 if(dcgi_client && disorder_length(dcgi_client, name, &length))
259 return sink_printf(output, "%ld:%02ld",
260 length / 60, length % 60) < 0 ? -1 : 0;
261 return sink_writes(output, "&nbsp;") < 0 ? -1 : 0;
262 }
263
264 /* @removable{ID}
265 *
266 * Expands to "true" if track ID is removable (or scratchable, if it is the
267 * playing track) and "false" otherwise.
268 */
269 static int exp_removable(int attribute((unused)) nargs,
270 char **args,
271 struct sink *output,
272 void attribute((unused)) *u) {
273 struct queue_entry *q = dcgi_findtrack(args[0]);
274 /* TODO would be better to reject recent */
275
276 if(!q || !dcgi_client)
277 return mx_bool_result(output, 0);
278 dcgi_lookup(DCGI_RIGHTS);
279 return mx_bool_result(output,
280 (q == dcgi_playing ? right_scratchable : right_removable)
281 (dcgi_rights, disorder_user(dcgi_client), q));
282 }
283
284 /* @movable{ID}{DIR}
285 *
286 * Expands to "true" if track ID is movable and "false" otherwise.
287 *
288 * DIR (which is optional) should be a non-zero integer. If it is negative
289 * then the intended move is down (later in the queue), if it is positive then
290 * the intended move is up (earlier in the queue). The first track is not
291 * movable up and the last track not movable down.
292 */
293 static int exp_movable(int nargs,
294 char **args,
295 struct sink *output,
296 void attribute((unused)) *u) {
297 struct queue_entry *q = dcgi_findtrack(args[0]);
298 /* TODO would be better to recent playing/recent */
299
300 if(!q || !dcgi_client)
301 return mx_bool_result(output, 0);
302 if(nargs > 1) {
303 const long dir = atoi(args[1]);
304
305 if(dir > 0 && q == dcgi_queue)
306 return mx_bool_result(output, 0);
307 if(dir < 0 && q->next == 0)
308 return mx_bool_result(output, 0);
309 }
310 dcgi_lookup(DCGI_RIGHTS);
311 return mx_bool_result(output,
312 right_movable(dcgi_rights,
313 disorder_user(dcgi_client),
314 q));
315 }
316
317 /* @playing{TEMPLATE}
318 *
319 * Expands to TEMPLATE, with:
320 * - @id expanded to the queue ID of the playing track
321 * - @track expanded to its UNQUOTED name
322 *
323 * If no track is playing expands to nothing.
324 *
325 * TEMPLATE is optional. If it is left out then instead expands to the queue
326 * ID of the playing track.
327 */
328 static int exp_playing(int nargs,
329 const struct mx_node **args,
330 struct sink *output,
331 void *u) {
332 dcgi_lookup(DCGI_PLAYING);
333 if(!dcgi_playing)
334 return 0;
335 if(!nargs)
336 return sink_writes(output, dcgi_playing->id) < 0 ? -1 : 0;
337 return mx_expand(mx_rewritel(args[0],
338 "id", dcgi_playing->id,
339 "track", dcgi_playing->track,
340 (char *)0),
341 output, u);
342 }
343
344 /* @queue{TEMPLATE}
345 *
346 * For each track in the queue, expands TEMPLATE with the following expansions:
347 * - @id to the queue ID of the track
348 * - @track to the UNQUOTED track name
349 * - @index to the track number from 0
350 * - @parity to "even" or "odd" alternately
351 * - @first to "true" on the first track and "false" otherwise
352 * - @last to "true" on the last track and "false" otherwise
353 */
354 static int exp_queue(int attribute((unused)) nargs,
355 const struct mx_node **args,
356 struct sink *output,
357 void *u) {
358 struct queue_entry *q;
359 int rc, i;
360
361 dcgi_lookup(DCGI_QUEUE);
362 for(q = dcgi_queue, i = 0; q; q = q->next, ++i)
363 if((rc = mx_expand(mx_rewritel(args[0],
364 "id", q->id,
365 "track", q->track,
366 "index", make_index(i),
367 "parity", i % 2 ? "odd" : "even",
368 "first", q == dcgi_queue ? "true" : "false",
369 "last", q->next ? "false" : "true",
370 (char *)0),
371 output, u)))
372 return rc;
373 return 0;
374 }
375
376 /* @recent{TEMPLATE}
377 *
378 * For each track in the recently played list, expands TEMPLATE with the
379 * following expansions:
380 * - @id to the queue ID of the track
381 * - @track to the UNQUOTED track name
382 * - @index to the track number from 0
383 * - @parity to "even" or "odd" alternately
384 * - @first to "true" on the first track and "false" otherwise
385 * - @last to "true" on the last track and "false" otherwise
386 */
387 static int exp_recent(int attribute((unused)) nargs,
388 const struct mx_node **args,
389 struct sink *output,
390 void *u) {
391 struct queue_entry *q;
392 int rc, i;
393
394 dcgi_lookup(DCGI_RECENT);
395 for(q = dcgi_recent, i = 0; q; q = q->next, ++i)
396 if((rc = mx_expand(mx_rewritel(args[0],
397 "id", q->id,
398 "track", q->track,
399 "index", make_index(i),
400 "parity", i % 2 ? "odd" : "even",
401 "first", q == dcgi_recent ? "true" : "false",
402 "last", q->next ? "false" : "true",
403 (char *)0),
404 output, u)))
405 return rc;
406 return 0;
407 }
408
409 /* @new{TEMPLATE}
410 *
411 * For each track in the newly added list, expands TEMPLATE wit the following
412 * expansions:
413 * - @track to the UNQUOTED track name
414 * - @index to the track number from 0
415 * - @parity to "even" or "odd" alternately
416 * - @first to "true" on the first track and "false" otherwise
417 * - @last to "true" on the last track and "false" otherwise
418 *
419 * Note that unlike @playing, @queue and @recent which are otherwise
420 * superficially similar, there is no @id sub-expansion here.
421 */
422 static int exp_new(int attribute((unused)) nargs,
423 const struct mx_node **args,
424 struct sink *output,
425 void *u) {
426 int rc, i;
427
428 dcgi_lookup(DCGI_NEW);
429 /* TODO perhaps we should generate an ID value for tracks in the new list */
430 for(i = 0; i < dcgi_nnew; ++i)
431 if((rc = mx_expand(mx_rewritel(args[0],
432 "track", dcgi_new[i],
433 "index", make_index(i),
434 "parity", i % 2 ? "odd" : "even",
435 "first", i == 0 ? "true" : "false",
436 "last", i == dcgi_nnew - 1 ? "false" : "true",
437 (char *)0),
438 output, u)))
439 return rc;
440 return 0;
441 }
442
443 /* @volume{CHANNEL}
444 *
445 * Expands to the volume in a given channel. CHANNEL must be "left" or
446 * "right".
447 */
448 static int exp_volume(int attribute((unused)) nargs,
449 char **args,
450 struct sink *output,
451 void attribute((unused)) *u) {
452 dcgi_lookup(DCGI_VOLUME);
453 return sink_printf(output, "%d",
454 !strcmp(args[0], "left")
455 ? dcgi_volume_left : dcgi_volume_right) < 0 ? -1 : 0;
456 }
457
458 /* @isplaying
459 *
460 * Expands to "true" if there is a playing track, otherwise "false".
461 */
462 static int exp_isplaying(int attribute((unused)) nargs,
463 char attribute((unused)) **args,
464 struct sink *output,
465 void attribute((unused)) *u) {
466 dcgi_lookup(DCGI_PLAYING);
467 return mx_bool_result(output, !!dcgi_playing);
468 }
469
470 /* @isqueue
471 *
472 * Expands to "true" if there the queue is nonempty, otherwise "false".
473 */
474 static int exp_isqueue(int attribute((unused)) nargs,
475 char attribute((unused)) **args,
476 struct sink *output,
477 void attribute((unused)) *u) {
478 dcgi_lookup(DCGI_QUEUE);
479 return mx_bool_result(output, !!dcgi_queue);
480 }
481
482 /* @isrecent@
483 *
484 * Expands to "true" if there the recently played list is nonempty, otherwise
485 * "false".
486 */
487 static int exp_isrecent(int attribute((unused)) nargs,
488 char attribute((unused)) **args,
489 struct sink *output,
490 void attribute((unused)) *u) {
491 dcgi_lookup(DCGI_RECENT);
492 return mx_bool_result(output, !!dcgi_recent);
493 }
494
495 /* @isnew
496 *
497 * Expands to "true" if there the newly added track list is nonempty, otherwise
498 * "false".
499 */
500 static int exp_isnew(int attribute((unused)) nargs,
501 char attribute((unused)) **args,
502 struct sink *output,
503 void attribute((unused)) *u) {
504 dcgi_lookup(DCGI_NEW);
505 return mx_bool_result(output, !!dcgi_nnew);
506 }
507
508 /* @pref{TRACK}{KEY}
509 *
510 * Expands to a track preference.
511 */
512 static int exp_pref(int attribute((unused)) nargs,
513 char **args,
514 struct sink *output,
515 void attribute((unused)) *u) {
516 char *value;
517
518 if(dcgi_client && !disorder_get(dcgi_client, args[0], args[1], &value))
519 return sink_writes(output, cgi_sgmlquote(value)) < 0 ? -1 : 0;
520 return 0;
521 }
522
523 /* @prefs{TRACK}{TEMPLATE}
524 *
525 * For each track preference of track TRACK, expands TEMPLATE with the
526 * following expansions:
527 * - @name to the UNQUOTED preference name
528 * - @index to the preference number from 0
529 * - @value to the UNQUOTED preference value
530 * - @parity to "even" or "odd" alternately
531 * - @first to "true" on the first preference and "false" otherwise
532 * - @last to "true" on the last preference and "false" otherwise
533 *
534 * Use @quote to quote preference names and values where necessary; see below.
535 */
536 static int exp_prefs(int attribute((unused)) nargs,
537 const struct mx_node **args,
538 struct sink *output,
539 void *u) {
540 int rc, i;
541 struct kvp *k, *head;
542 char *track;
543
544 if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)")))
545 return rc;
546 if(!dcgi_client || disorder_prefs(dcgi_client, track, &head))
547 return 0;
548 for(k = head, i = 0; k; k = k->next, ++i)
549 if((rc = mx_expand(mx_rewritel(args[1],
550 "index", make_index(i),
551 "parity", i % 2 ? "odd" : "even",
552 "name", k->name,
553 "value", k->value,
554 "first", k == head ? "true" : "false",
555 "last", k->next ? "false" : "true",
556 (char *)0),
557 output, u)))
558 return rc;
559 return 0;
560 }
561
562 /* @transform{TRACK}{TYPE}{CONTEXT}
563 *
564 * Transforms a track name (if TYPE is "track") or directory name (if type is
565 * "dir"). CONTEXT should be the context, if it is left out then "display" is
566 * assumed.
567 */
568 static int exp_transform(int nargs,
569 char **args,
570 struct sink *output,
571 void attribute((unused)) *u) {
572 const char *t = trackname_transform(args[1], args[0],
573 (nargs > 2 ? args[2] : "display"));
574 return sink_writes(output, cgi_sgmlquote(t)) < 0 ? -1 : 0;
575 }
576
577 /* @enabled@
578 *
579 * Expands to "true" if playing is enabled, otherwise "false".
580 */
581 static int exp_enabled(int attribute((unused)) nargs,
582 char attribute((unused)) **args,
583 struct sink *output,
584 void attribute((unused)) *u) {
585 int e = 0;
586
587 if(dcgi_client)
588 disorder_enabled(dcgi_client, &e);
589 return mx_bool_result(output, e);
590 }
591
592 /* @random-enabled
593 *
594 * Expands to "true" if random play is enabled, otherwise "false".
595 */
596 static int exp_random_enabled(int attribute((unused)) nargs,
597 char attribute((unused)) **args,
598 struct sink *output,
599 void attribute((unused)) *u) {
600 int e = 0;
601
602 if(dcgi_client)
603 disorder_random_enabled(dcgi_client, &e);
604 return mx_bool_result(output, e);
605 }
606
607 /* @trackstate{TRACK}
608 *
609 * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
610 * the queue, otherwise to nothing.
611 */
612 static int exp_trackstate(int attribute((unused)) nargs,
613 char **args,
614 struct sink *output,
615 void attribute((unused)) *u) {
616 char *track;
617 struct queue_entry *q;
618
619 if(!dcgi_client)
620 return 0;
621 if(disorder_resolve(dcgi_client, &track, args[0]))
622 return 0;
623 dcgi_lookup(DCGI_PLAYING);
624 if(dcgi_playing && !strcmp(track, dcgi_playing->track))
625 return sink_writes(output, "playing") < 0 ? -1 : 0;
626 dcgi_lookup(DCGI_QUEUE);
627 for(q = dcgi_queue; q; q = q->next)
628 if(!strcmp(track, q->track))
629 return sink_writes(output, "queued") < 0 ? -1 : 0;
630 return 0;
631 }
632
633 /* @thisurl
634 *
635 * Expands to an UNQUOTED URL which points back to the current page. (NB it
636 * might not be byte for byte identical - for instance, CGI arguments might be
637 * re-ordered.)
638 */
639 static int exp_thisurl(int attribute((unused)) nargs,
640 char attribute((unused)) **args,
641 struct sink *output,
642 void attribute((unused)) *u) {
643 return sink_writes(output, cgi_thisurl(config->url)) < 0 ? -1 : 0;
644 }
645
646 /* @resolve{TRACK}
647 *
648 * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
649 * nothing if it is not a valid track.
650 */
651 static int exp_resolve(int attribute((unused)) nargs,
652 char **args,
653 struct sink *output,
654 void attribute((unused)) *u) {
655 char *r;
656
657 if(dcgi_client && !disorder_resolve(dcgi_client, &r, args[0]))
658 return sink_writes(output, r) < 0 ? -1 : 0;
659 return 0;
660 }
661
662 /* @paused
663 *
664 * Expands to "true" if the playing track is paused, to "false" if it is
665 * playing (or if there is no playing track at all).
666 */
667 static int exp_paused(int attribute((unused)) nargs,
668 char attribute((unused)) **args,
669 struct sink *output,
670 void attribute((unused)) *u) {
671 dcgi_lookup(DCGI_PLAYING);
672 return mx_bool_result(output, (dcgi_playing
673 && dcgi_playing->state == playing_paused));
674 }
675
676 /* @state{ID}@
677 *
678 * Expands to the current state of track ID.
679 */
680 static int exp_state(int attribute((unused)) nargs,
681 char **args,
682 struct sink *output,
683 void attribute((unused)) *u) {
684 struct queue_entry *q = dcgi_findtrack(args[0]);
685
686 if(q)
687 return sink_writes(output, playing_states[q->state]) < 0 ? -1 : 0;
688 return 0;
689 }
690
691 /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
692 *
693 * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
694 * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out.
695 *
696 * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
697 * the user has the right and "false" otherwise.
698 *
699 * If there is no connection to the server then expands to nothing (in all
700 * cases).
701 */
702 static int exp_right(int nargs,
703 const struct mx_node **args,
704 struct sink *output,
705 void *u) {
706 char *right;
707 rights_type r;
708 int rc;
709
710 if(!dcgi_client)
711 return 0;
712 dcgi_lookup(DCGI_RIGHTS);
713 if((rc = mx_expandstr(args[0], &right, u, "argument #0 (RIGHT)")))
714 return rc;
715 if(parse_rights(right, &r, 1/*report*/))
716 return 0;
717 /* Single-argument form */
718 if(nargs == 1)
719 return mx_bool_result(output, !!(r & dcgi_rights));
720 /* Multiple argument form */
721 if(r & dcgi_rights)
722 return mx_expand(args[1], output, u);
723 if(nargs == 3)
724 return mx_expand(args[2], output, u);
725 return 0;
726 }
727
728 /* @userinfo{PROPERTY}
729 *
730 * Expands to the named property of the current user.
731 */
732 static int exp_userinfo(int attribute((unused)) nargs,
733 char **args,
734 struct sink *output,
735 void attribute((unused)) *u) {
736 char *v;
737
738 if(dcgi_client
739 && !disorder_userinfo(dcgi_client, disorder_user(dcgi_client),
740 args[0], &v))
741 return sink_writes(output, v) < 0 ? -1 : 0;
742 return 0;
743 }
744
745 /* @error
746 *
747 * Expands to the latest error string.
748 */
749 static int exp_error(int attribute((unused)) nargs,
750 char attribute((unused)) **args,
751 struct sink *output,
752 void attribute((unused)) *u) {
753 return sink_writes(output, dcgi_error_string) < 0 ? -1 : 0;
754 }
755
756 /* @image{NAME}
757 *
758 * Expands to the URL of the image called NAME.
759 *
760 * Firstly if there is a label called images.NAME then the image stem will be
761 * the value of that label. Otherwise the stem will be NAME.png.
762 *
763 * If the label url.static exists then it will give the base URL for images.
764 * Otherwise the base url will be /disorder/.
765 */
766 static int exp_image(int attribute((unused)) nargs,
767 char **args,
768 struct sink *output,
769 void attribute((unused)) *u) {
770 const char *url, *stem;
771 char *labelname;
772
773 /* Compute the stem */
774 byte_xasprintf(&labelname, "images.%s", args[0]);
775 if(option_label_exists(labelname))
776 stem = option_label(labelname);
777 else
778 byte_xasprintf((char **)&stem, "%s.png", args[0]);
779 /* If the stem looks like it's reasonalby complete, use that */
780 if(stem[0] == '/'
781 || !strncmp(stem, "http:", 5)
782 || !strncmp(stem, "https:", 6))
783 url = stem;
784 else {
785 /* Compute the URL */
786 if(option_label_exists("url.static"))
787 byte_xasprintf((char **)&url, "%s/%s",
788 option_label("url.static"), stem);
789 else
790 /* Default base is /disorder */
791 byte_xasprintf((char **)&url, "/disorder/%s", stem);
792 }
793 return sink_writes(output, cgi_sgmlquote(url)) < 0 ? -1 : 0;
794 }
795
796 /** @brief Entry in a list of tracks or directories */
797 struct entry {
798 /** @brief Track name */
799 const char *track;
800 /** @brief Sort key */
801 const char *sort;
802 /** @brief Display key */
803 const char *display;
804 };
805
806 /** @brief Compare two @ref entry objects */
807 static int compare_entry(const void *a, const void *b) {
808 const struct entry *ea = a, *eb = b;
809
810 return compare_tracks(ea->sort, eb->sort,
811 ea->display, eb->display,
812 ea->track, eb->track);
813 }
814
815 /** @brief Implementation of exp_tracks() and exp_dirs() */
816 static int exp__files_dirs(int nargs,
817 const struct mx_node **args,
818 struct sink *output,
819 void *u,
820 const char *type,
821 int (*fn)(disorder_client *client,
822 const char *name,
823 const char *re,
824 char ***vecp,
825 int *nvecp)) {
826 char **tracks, *dir, *re;
827 int n, ntracks, rc;
828 const struct mx_node *m;
829 struct entry *e;
830
831 if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)")))
832 return rc;
833 if(nargs == 3) {
834 if((rc = mx_expandstr(args[1], &re, u, "argument #1 (RE)")))
835 return rc;
836 m = args[2];
837 } else {
838 re = 0;
839 m = args[1];
840 }
841 if(!dcgi_client)
842 return 0;
843 /* Get the list */
844 if(fn(dcgi_client, dir, re, &tracks, &ntracks))
845 return 0;
846 /* Sort it. NB trackname_transform() does not go to the server. */
847 e = xcalloc(ntracks, sizeof *e);
848 for(n = 0; n < ntracks; ++n) {
849 e->track = tracks[n];
850 e[n].track = tracks[n];
851 e[n].sort = trackname_transform(type, tracks[n], "sort");
852 e[n].display = trackname_transform(type, tracks[n], "display");
853 }
854 qsort(e, ntracks, sizeof (struct entry), compare_entry);
855 /* Expand the subsiduary templates. We chuck in @sort and @display because
856 * it is particularly easy to do so. */
857 for(n = 0; n < ntracks; ++n)
858 if((rc = mx_expand(mx_rewritel(m,
859 "index", make_index(n),
860 "parity", n % 2 ? "odd" : "even",
861 "track", tracks[n],
862 "first", n == 0 ? "true" : "false",
863 "last", n + 1 == ntracks ? "false" : "true",
864 "sort", e[n].sort,
865 "display", e[n].display,
866 (char *)0),
867 output, u)))
868 return rc;
869 return 0;
870
871 }
872
873 /** @tracks{DIR}{RE}{TEMPLATE}
874 *
875 * For each track below DIR, expands TEMPLATE with the
876 * following expansions:
877 * - @track to the UNQUOTED track name
878 * - @index to the track number from 0
879 * - @parity to "even" or "odd" alternately
880 * - @first to "true" on the first track and "false" otherwise
881 * - @last to "true" on the last track and "false" otherwise
882 * - @sort to the sort key for this track
883 * - @display to the UNQUOTED display string for this track
884 *
885 * RE is optional and if present is the regexp to match against.
886 */
887 static int exp_tracks(int nargs,
888 const struct mx_node **args,
889 struct sink *output,
890 void *u) {
891 return exp__files_dirs(nargs, args, output, u, "track", disorder_files);
892 }
893
894 /** @dirs{DIR}{RE}{TEMPLATE}
895 *
896 * For each directory below DIR, expands TEMPLATE with the
897 * following expansions:
898 * - @track to the UNQUOTED directory name
899 * - @index to the directory number from 0
900 * - @parity to "even" or "odd" alternately
901 * - @first to "true" on the first directory and "false" otherwise
902 * - @last to "true" on the last directory and "false" otherwise
903 * - @sort to the sort key for this directory
904 * - @display to the UNQUOTED display string for this directory
905 *
906 * RE is optional and if present is the regexp to match against.
907 */
908 static int exp_dirs(int nargs,
909 const struct mx_node **args,
910 struct sink *output,
911 void *u) {
912 return exp__files_dirs(nargs, args, output, u, "dir", disorder_directories);
913 }
914
915 static int exp__search_shim(disorder_client *c, const char *terms,
916 const char attribute((unused)) *re,
917 char ***vecp, int *nvecp) {
918 return disorder_search(c, terms, vecp, nvecp);
919 }
920
921 /** @search{KEYWORDS}{TEMPLATE}
922 *
923 * For each track matching KEYWORDS, expands TEMPLATE with the
924 * following expansions:
925 * - @track to the UNQUOTED directory name
926 * - @index to the directory number from 0
927 * - @parity to "even" or "odd" alternately
928 * - @first to "true" on the first directory and "false" otherwise
929 * - @last to "true" on the last directory and "false" otherwise
930 * - @sort to the sort key for this track
931 * - @display to the UNQUOTED display string for this track
932 */
933 static int exp_search(int nargs,
934 const struct mx_node **args,
935 struct sink *output,
936 void *u) {
937 return exp__files_dirs(nargs, args, output, u, "track", exp__search_shim);
938 }
939
940 /** @brief Register DisOrder-specific expansions */
941 void dcgi_expansions(void) {
942 mx_register("arg", 1, 1, exp_arg);
943 mx_register("argq", 1, 1, exp_argq);
944 mx_register("enabled", 0, 0, exp_enabled);
945 mx_register("error", 0, 0, exp_error);
946 mx_register("image", 1, 1, exp_image);
947 mx_register("isnew", 0, 0, exp_isnew);
948 mx_register("isplaying", 0, 0, exp_isplaying);
949 mx_register("isplaying", 0, 0, exp_isqueue);
950 mx_register("isrecent", 0, 0, exp_isrecent);
951 mx_register("length", 1, 1, exp_length);
952 mx_register("movable", 1, 1, exp_movable);
953 mx_register("part", 2, 3, exp_part);
954 mx_register("paused", 0, 0, exp_paused);
955 mx_register("pref", 2, 2, exp_pref);
956 mx_register("quote", 1, 1, exp_quote);
957 mx_register("random-enabled", 0, 0, exp_random_enabled);
958 mx_register("removable", 1, 1, exp_removable);
959 mx_register("resolve", 1, 1, exp_resolve);
960 mx_register("server-version", 0, 0, exp_server_version);
961 mx_register("state", 1, 1, exp_state);
962 mx_register("thisurl", 0, 0, exp_thisurl);
963 mx_register("trackstate", 1, 1, exp_trackstate);
964 mx_register("transform", 2, 3, exp_transform);
965 mx_register("url", 0, 0, exp_url);
966 mx_register("user", 0, 0, exp_user);
967 mx_register("userinfo", 1, 1, exp_userinfo);
968 mx_register("version", 0, 0, exp_version);
969 mx_register("volume", 1, 1, exp_volume);
970 mx_register("when", 1, 1, exp_when);
971 mx_register("who", 1, 1, exp_who);
972 mx_register_magic("dirs", 2, 3, exp_dirs);
973 mx_register_magic("new", 1, 1, exp_new);
974 mx_register_magic("playing", 0, 1, exp_playing);
975 mx_register_magic("prefs", 2, 2, exp_prefs);
976 mx_register_magic("queue", 1, 1, exp_queue);
977 mx_register_magic("recent", 1, 1, exp_recent);
978 mx_register_magic("right", 1, 3, exp_right);
979 mx_register_magic("search", 2, 2, exp_search);
980 mx_register_magic("tracks", 2, 3, exp_tracks);
981 }
982
983 /*
984 Local Variables:
985 c-basic-offset:2
986 comment-column:40
987 fill-column:79
988 indent-tabs-mode:nil
989 End:
990 */