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