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