chiark / gitweb /
dirname/basename expansions; template fiddling
[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 Locate a track by ID */
30 static struct queue_entry *findtrack(const char *id) {
31   struct queue_entry *q;
32
33   dcgi_lookup(DCGI_PLAYING);
34   if(dcgi_playing && !strcmp(dcgi_playing->id, id))
35     return dcgi_playing;
36   dcgi_lookup(DCGI_QUEUE);
37   for(q = dcgi_queue; q; q = q->next)
38     if(!strcmp(q->id, id))
39       return q;
40   dcgi_lookup(DCGI_RECENT);
41   for(q = dcgi_recent; q; q = q->next)
42     if(!strcmp(q->id, id))
43       return q;
44   return NULL;
45 }
46
47 /** @brief Return @p i as a string */
48 static const char *make_index(int i) {
49   char *s;
50
51   byte_xasprintf(&s, "%d", i);
52   return s;
53 }
54
55 /* @server-version
56  *
57  * Expands to the server's version string, or a (safe to use) error
58  * value if the server is unavailable or broken.
59  */
60 static int exp_server_version(int attribute((unused)) nargs,
61                               char attribute((unused)) **args,
62                               struct sink *output,
63                               void attribute((unused)) *u) {
64   const char *v;
65   
66   if(dcgi_client) {
67     if(disorder_version(dcgi_client, (char **)&v))
68       v = "(cannot get version)";
69   } else
70     v = "(server not running)";
71   return sink_writes(output, cgi_sgmlquote(v)) < 0 ? -1 : 0;
72 }
73
74 /* @version
75  *
76  * Expands to the local version string.
77  */
78 static int exp_version(int attribute((unused)) nargs,
79                        char attribute((unused)) **args,
80                        struct sink *output,
81                        void attribute((unused)) *u) {
82   return sink_writes(output,
83                      cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0;
84 }
85
86 /* @url
87  *
88  * Expands to the base URL of the web interface.
89  */
90 static int exp_url(int attribute((unused)) nargs,
91                    char attribute((unused)) **args,
92                    struct sink *output,
93                    void attribute((unused)) *u) {
94   return sink_writes(output,
95                      cgi_sgmlquote(config->url)) < 0 ? -1 : 0;
96 }
97
98 /* @arg{NAME}
99  *
100  * Expands to the CGI argument NAME, or the empty string if there is
101  * no such argument.
102  */
103 static int exp_arg(int attribute((unused)) nargs,
104                    char **args,
105                    struct sink *output,
106                    void attribute((unused)) *u) {
107   const char *s = cgi_get(args[0]);
108
109   if(s)
110     return sink_writes(output,
111                        cgi_sgmlquote(s)) < 0 ? -1 : 0;
112   else
113     return 0;
114 }
115
116 /* @user
117  *
118  * Expands to the logged-in username (which might be "guest"), or to
119  * the empty string if not connected.
120  */
121 static int exp_user(int attribute((unused)) nargs,
122                     char attribute((unused)) **args,
123                     struct sink *output,
124                     void attribute((unused)) *u) {
125   const char *user;
126   
127   if(dcgi_client && (user = disorder_user(dcgi_client)))
128     return sink_writes(output, cgi_sgmlquote(user)) < 0 ? -1 : 0;
129   return 0;
130 }
131
132 /* @part{TRACK|ID}{PART}{CONTEXT}
133  *
134  * Expands to a track name part.
135  *
136  * A track may be identified by name or by queue ID.
137  *
138  * CONTEXT may be omitted.  If it is then 'display' is assumed.
139  *
140  * If the CONTEXT is 'short' then the 'display' part is looked up, and the
141  * result truncated according to the length defined by the short_display
142  * configuration directive.
143  */
144 static int exp_part(int nargs,
145                     char **args,
146                     struct sink *output,
147                     void attribute((unused)) *u) {
148   const char *track = args[0], *part = args[1];
149   const char *context = nargs > 2 ? args[2] : "display";
150   char *s;
151
152   if(track[0] != '/') {
153     struct queue_entry *q = findtrack(track);
154
155     if(q)
156       track = q->track;
157     else
158       return 0;
159   }
160   if(dcgi_client
161      && !disorder_part(dcgi_client, &s,
162                        track,
163                        !strcmp(context, "short") ? "display" : context,
164                        part))
165     return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0;
166   return 0;
167 }
168
169 /* @quote{STRING}
170  *
171  * SGML-quotes STRING.  Note that most expansion results are already suitable
172  * quoted, so this expansion is usually not required.
173  */
174 static int exp_quote(int attribute((unused)) nargs,
175                      char **args,
176                      struct sink *output,
177                      void attribute((unused)) *u) {
178   return sink_writes(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0;
179 }
180
181 /* @who{ID}
182  *
183  * Expands to the name of the submitter of track ID, which must be a playing
184  * track, in the queue, or in the recent list.
185  */
186 static int exp_who(int attribute((unused)) nargs,
187                    char **args,
188                    struct sink *output,
189                    void attribute((unused)) *u) {
190   struct queue_entry *q = findtrack(args[0]);
191
192   if(q && q->submitter)
193     return sink_writes(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0;
194   return 0;
195 }
196
197 /* @when{ID}
198  *
199  * Expands to the time a track started or is expected to start.  The track must
200  * be a playing track, in the queue, or in the recent list.
201  */
202 static int exp_when(int attribute((unused)) nargs,
203                    char **args,
204                    struct sink *output,
205                     void attribute((unused)) *u) {
206   struct queue_entry *q = findtrack(args[0]);
207   const struct tm *w = 0;
208
209   if(q) {
210     switch(q->state) {
211     case playing_isscratch:
212     case playing_unplayed:
213     case playing_random:
214       if(q->expected)
215         w = localtime(&q->expected);
216       break;
217     case playing_failed:
218     case playing_no_player:
219     case playing_ok:
220     case playing_scratched:
221     case playing_started:
222     case playing_paused:
223     case playing_quitting:
224       if(q->played)
225         w = localtime(&q->played);
226       break;
227     }
228     if(w)
229       return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0;
230   }
231   return sink_writes(output, "&nbsp;") < 0 ? -1 : 0;
232 }
233
234 /* @length{ID|TRACK}
235  *
236  * Expands to the length of a track, identified by its queue ID or its name.
237  * If it is the playing track (identified by ID) then the amount played so far
238  * is included.
239  */
240 static int exp_length(int attribute((unused)) nargs,
241                    char **args,
242                    struct sink *output,
243                    void attribute((unused)) *u) {
244   struct queue_entry *q;
245   long length = 0;
246   const char *name;
247
248   if(args[0][0] == '/')
249     /* Track identified by name */
250     name = args[0];
251   else {
252     /* Track identified by queue ID */
253     if(!(q = findtrack(args[0])))
254       return 0;
255     if(q->state == playing_started || q->state == playing_paused)
256       if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0)
257         return -1;
258     name = q->track;
259   }
260   if(dcgi_client && disorder_length(dcgi_client, name, &length))
261     return sink_printf(output, "%ld:%02ld",
262                        length / 60, length % 60) < 0 ? -1 : 0;
263   return sink_writes(output, "&nbsp;") < 0 ? -1 : 0;
264 }
265
266 /* @removable{ID}
267  *
268  * Expands to "true" if track ID is removable (or scratchable, if it is the
269  * playing track) and "false" otherwise.
270  */
271 static int exp_removable(int attribute((unused)) nargs,
272                          char **args,
273                          struct sink *output,
274                          void attribute((unused)) *u) {
275   struct queue_entry *q = findtrack(args[0]);
276   /* TODO would be better to reject recent */
277
278   if(!q || !dcgi_client)
279     return mx_bool_result(output, 0);
280   dcgi_lookup(DCGI_RIGHTS);
281   return mx_bool_result(output,
282                         (q == dcgi_playing ? right_scratchable : right_removable)
283                             (dcgi_rights, disorder_user(dcgi_client), q));
284 }
285
286 /* @movable{ID}
287  *
288  * Expands to "true" if track ID is movable and "false" otherwise.
289  */
290 static int exp_movable(int attribute((unused)) nargs,
291                        char **args,
292                        struct sink *output,
293                        void attribute((unused)) *u) {
294   struct queue_entry *q = findtrack(args[0]);
295   /* TODO would be better to recent playing/recent */
296
297   if(!q || !dcgi_client)
298     return mx_bool_result(output, 0);
299   dcgi_lookup(DCGI_RIGHTS);
300   return mx_bool_result(output,
301                         right_movable(dcgi_rights, disorder_user(dcgi_client), q));
302 }
303
304 /* @playing{TEMPLATE}
305  *
306  * Expands to TEMPLATE, with:
307  * - @id@ expanded to the queue ID of the playing track
308  * - @track@ expanded to its UNQUOTED name
309  *
310  * If no track is playing expands to nothing.
311  *
312  * TEMPLATE is optional.  If it is left out then instead expands to the queue
313  * ID of the playing track.
314  */
315 static int exp_playing(int nargs,
316                        const struct mx_node **args,
317                        struct sink *output,
318                        void *u) {
319   dcgi_lookup(DCGI_PLAYING);
320   if(!dcgi_playing)
321     return 0;
322   if(!nargs)
323     return sink_writes(output, dcgi_playing->id) < 0 ? -1 : 0;
324   return mx_expand(mx_rewritel(args[0],
325                                "id", dcgi_playing->id,
326                                "track", dcgi_playing->track,
327                                (char *)0),
328                    output, u);
329 }
330
331 /* @queue{TEMPLATE}
332  *
333  * For each track in the queue, expands TEMPLATE with the following expansions:
334  * - @id@ to the queue ID of the track
335  * - @track@ to the UNQUOTED track name
336  * - @index@ to the track number from 0
337  * - @parity@ to "even" or "odd" alternately
338  * - @first@ to "true" on the first track and "false" otherwise
339  * - @last@ to "true" on the last track and "false" otherwise
340  */
341 static int exp_queue(int attribute((unused)) nargs,
342                      const struct mx_node **args,
343                      struct sink *output,
344                      void *u) {
345   struct queue_entry *q;
346   int rc, i;
347   
348   dcgi_lookup(DCGI_QUEUE);
349   for(q = dcgi_queue, i = 0; q; q = q->next, ++i)
350     if((rc = mx_expand(mx_rewritel(args[0],
351                                    "id", q->id,
352                                    "track", q->track,
353                                    "index", make_index(i),
354                                    "parity", i % 2 ? "odd" : "even",
355                                    "first", q == dcgi_queue ? "true" : "false",
356                                    "last", q->next ? "false" : "true",
357                                    (char *)0),
358                        output, u)))
359       return rc;
360   return 0;
361 }
362
363 /* @recent{TEMPLATE}
364  *
365  * For each track in the recently played list, expands TEMPLATE with the
366  * following expansions:
367  * - @id@ to the queue ID of the track
368  * - @track@  to the UNQUOTED track name
369  * - @index@ to the track number from 0
370  * - @parity@ to "even" or "odd" alternately
371  * - @first@ to "true" on the first track and "false" otherwise
372  * - @last@ to "true" on the last track and "false" otherwise
373  */
374 static int exp_recent(int attribute((unused)) nargs,
375                       const struct mx_node **args,
376                       struct sink *output,
377                       void *u) {
378   struct queue_entry *q;
379   int rc, i;
380   
381   dcgi_lookup(DCGI_RECENT);
382   for(q = dcgi_recent, i = 0; q; q = q->next, ++i)
383     if((rc = mx_expand(mx_rewritel(args[0],
384                                    "id", q->id,
385                                    "track", q->track,
386                                    "index", make_index(i),
387                                    "parity", i % 2 ? "odd" : "even",
388                                    "first", q == dcgi_recent ? "true" : "false",
389                                    "last", q->next ? "false" : "true",
390                                    (char *)0),
391                        output, u)))
392       return rc;
393   return 0;
394 }
395
396 /* @new{TEMPLATE}
397  *
398  * For each track in the newly added list, expands TEMPLATE wit the following
399  * expansions:
400  * - @track@ to the UNQUOTED track name
401  * - @index@ to the track number from 0
402  * - @parity@ to "even" or "odd" alternately
403  * - @first@ to "true" on the first track and "false" otherwise
404  * - @last@ to "true" on the last track and "false" otherwise
405  *
406  * Note that unlike @playing@, @queue@ and @recent@ which are otherwise
407  * superficially similar, there is no @id@ sub-expansion here.
408  */
409 static int exp_new(int attribute((unused)) nargs,
410                    const struct mx_node **args,
411                    struct sink *output,
412                    void *u) {
413   int rc, i;
414   
415   dcgi_lookup(DCGI_NEW);
416   /* TODO perhaps we should generate an ID value for tracks in the new list */
417   for(i = 0; i < dcgi_nnew; ++i)
418     if((rc = mx_expand(mx_rewritel(args[0],
419                                    "track", dcgi_new[i],
420                                    "index", make_index(i),
421                                    "parity", i % 2 ? "odd" : "even",
422                                    "first", i == 0 ? "true" : "false",
423                                    "last", i == dcgi_nnew - 1 ? "false" : "true",
424                                    (char *)0),
425                        output, u)))
426       return rc;
427   return 0;
428 }
429
430 /* @volume{CHANNEL}
431  *
432  * Expands to the volume in a given channel.  CHANNEL must be "left" or
433  * "right".
434  */
435 static int exp_volume(int attribute((unused)) nargs,
436                       char **args,
437                       struct sink *output,
438                       void attribute((unused)) *u) {
439   dcgi_lookup(DCGI_VOLUME);
440   return sink_printf(output, "%d",
441                      !strcmp(args[0], "left")
442                          ? dcgi_volume_left : dcgi_volume_right) < 0 ? -1 : 0;
443 }
444
445 /* @isplaying
446  *
447  * Expands to "true" if there is a playing track, otherwise "false".
448  */
449 static int exp_isplaying(int attribute((unused)) nargs,
450                          char attribute((unused)) **args,
451                          struct sink *output,
452                          void attribute((unused)) *u) {
453   dcgi_lookup(DCGI_PLAYING);
454   return mx_bool_result(output, !!dcgi_playing);
455 }
456
457 /* @isqueue
458  *
459  * Expands to "true" if there the queue is nonempty, otherwise "false".
460  */
461 static int exp_isqueue(int attribute((unused)) nargs,
462                        char attribute((unused)) **args,
463                        struct sink *output,
464                        void attribute((unused)) *u) {
465   dcgi_lookup(DCGI_QUEUE);
466   return mx_bool_result(output, !!dcgi_queue);
467 }
468
469 /* @isrecent@
470  *
471  * Expands to "true" if there the recently played list is nonempty, otherwise
472  * "false".
473  */
474 static int exp_isrecent(int attribute((unused)) nargs,
475                         char attribute((unused)) **args,
476                         struct sink *output,
477                         void attribute((unused)) *u) {
478   dcgi_lookup(DCGI_RECENT);
479   return mx_bool_result(output, !!dcgi_recent);
480 }
481
482 /* @isnew
483  *
484  * Expands to "true" if there the newly added track list is nonempty, otherwise
485  * "false".
486  */
487 static int exp_isnew(int attribute((unused)) nargs,
488                      char attribute((unused)) **args,
489                      struct sink *output,
490                      void attribute((unused)) *u) {
491   dcgi_lookup(DCGI_NEW);
492   return mx_bool_result(output, !!dcgi_nnew);
493 }
494
495 /* @pref{TRACK}{KEY}
496  *
497  * Expands to a track preference.
498  */
499 static int exp_pref(int attribute((unused)) nargs,
500                     char **args,
501                     struct sink *output,
502                     void attribute((unused)) *u) {
503   char *value;
504
505   if(dcgi_client && !disorder_get(dcgi_client, args[0], args[1], &value))
506     return sink_writes(output, cgi_sgmlquote(value)) < 0 ? -1 : 0;
507   return 0;
508 }
509
510 /* @prefs{TRACK}{TEMPLATE}
511  *
512  * For each track preference of track TRACK, expands TEMPLATE with the
513  * following expansions:
514  * - @name@ to the UNQUOTED preference name
515  * - @index@ to the preference number from 0
516  * - @value@ to the UNQUOTED preference value
517  * - @parity@ to "even" or "odd" alternately
518  * - @first@ to "true" on the first preference and "false" otherwise
519  * - @last@ to "true" on the last preference and "false" otherwise
520  *
521  * Use @quote@ to quote preference names and values where necessary; see below.
522  */
523 static int exp_prefs(int attribute((unused)) nargs,
524                      const struct mx_node **args,
525                      struct sink *output,
526                      void *u) {
527   int rc, i;
528   struct kvp *k, *head;
529   char *track;
530
531   if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)")))
532     return rc;
533   if(!dcgi_client || disorder_prefs(dcgi_client, track, &head))
534     return 0;
535   for(k = head, i = 0; k; k = k->next, ++i)
536     if((rc = mx_expand(mx_rewritel(args[1],
537                                    "index", make_index(i),
538                                    "parity", i % 2 ? "odd" : "even",
539                                    "name", k->name,
540                                    "value", k->value,
541                                    "first", k == head ? "true" : "false",
542                                    "last", k->next ? "false" : "true",
543                                    (char *)0),
544                        output, u)))
545       return rc;
546   return 0;
547 }
548
549 /* @transform{TRACK}{TYPE}{CONTEXT}
550  *
551  * Transforms a track name (if TYPE is "track") or directory name (if type is
552  * "dir").  CONTEXT should be the context, if it is left out then "display" is
553  * assumed.
554  */
555 static int exp_transform(int nargs,
556                          char **args,
557                          struct sink *output,
558                          void attribute((unused)) *u) {
559   const char *t = trackname_transform(args[1], args[0],
560                                       (nargs > 2 ? args[2] : "display"));
561   return sink_writes(output, cgi_sgmlquote(t)) < 0 ? -1 : 0;
562 }
563
564 /* @enabled@
565  *
566  * Expands to "true" if playing is enabled, otherwise "false".
567  */
568 static int exp_enabled(int attribute((unused)) nargs,
569                        char attribute((unused)) **args,
570                        struct sink *output,
571                        void attribute((unused)) *u) {
572   int e = 0;
573
574   if(dcgi_client)
575     disorder_enabled(dcgi_client, &e);
576   return mx_bool_result(output, e);
577 }
578
579 /* @random-enabled
580  *
581  * Expands to "true" if random play is enabled, otherwise "false".
582  */
583 static int exp_random_enabled(int attribute((unused)) nargs,
584                               char attribute((unused)) **args,
585                               struct sink *output,
586                               void attribute((unused)) *u) {
587   int e = 0;
588
589   if(dcgi_client)
590     disorder_random_enabled(dcgi_client, &e);
591   return mx_bool_result(output, e);
592 }
593
594 /* @trackstate{TRACK}
595  *
596  * Expands to "playing" if TRACK is currently playing, or "queue" if it is in
597  * the queue, otherwise to nothing.
598  */
599 static int exp_trackstate(int attribute((unused)) nargs,
600                           char **args,
601                           struct sink *output,
602                           void attribute((unused)) *u) {
603   char *track;
604   struct queue_entry *q;
605
606   if(!dcgi_client)
607     return 0;
608   if(disorder_resolve(dcgi_client, &track, args[0]))
609     return 0;
610   dcgi_lookup(DCGI_PLAYING);
611   if(dcgi_playing && !strcmp(track, dcgi_playing->track))
612     return sink_writes(output, "playing") < 0 ? -1 : 0;
613   dcgi_lookup(DCGI_QUEUE);
614   for(q = dcgi_queue; q; q = q->next)
615     if(!strcmp(track, q->track))
616       return sink_writes(output, "queued") < 0 ? -1 : 0;
617   return 0;
618 }
619
620 /* @thisurl
621  *
622  * Expands to an UNQUOTED URL which points back to the current page.  (NB it
623  * might not be byte for byte identical - for instance, CGI arguments might be
624  * re-ordered.)
625  */
626 static int exp_thisurl(int attribute((unused)) nargs,
627                        char attribute((unused)) **args,
628                        struct sink *output,
629                        void attribute((unused)) *u) {
630   return sink_writes(output, cgi_thisurl(config->url)) < 0 ? -1 : 0;
631 }
632
633 /* @resolve{TRACK}
634  *
635  * Expands to an UNQUOTED name for the TRACK that is not an alias, or to
636  * nothing if it is not a valid track.
637  */
638 static int exp_resolve(int attribute((unused)) nargs,
639                        char **args,
640                        struct sink *output,
641                        void attribute((unused)) *u) {
642   char *r;
643
644   if(dcgi_client && !disorder_resolve(dcgi_client, &r, args[0]))
645     return sink_writes(output, r) < 0 ? -1 : 0;
646   return 0;
647 }
648
649 /* @paused
650  *
651  * Expands to "true" if the playing track is paused, to "false" if it is
652  * playing (or if there is no playing track at all).
653  */
654 static int exp_paused(int attribute((unused)) nargs,
655                       char attribute((unused)) **args,
656                       struct sink *output,
657                       void attribute((unused)) *u) {
658   dcgi_lookup(DCGI_PLAYING);
659   return mx_bool_result(output, (dcgi_playing
660                                  && dcgi_playing->state == playing_paused));
661 }
662
663 /* @state{ID}@
664  *
665  * Expands to the current state of track ID.
666  */
667 static int exp_state(int attribute((unused)) nargs,
668                      char **args,
669                      struct sink *output,
670                      void attribute((unused)) *u) {
671   struct queue_entry *q = findtrack(args[0]);
672
673   if(q)
674     return sink_writes(output, playing_states[q->state]) < 0 ? -1 : 0;
675   return 0;
676 }
677
678 /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@
679  *
680  * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to
681  * WITHOUT-RIGHT.  The WITHOUT-RIGHT argument may be left out.
682  *
683  * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if
684  * the user has the right and "false" otherwise.
685  *
686  * If there is no connection to the server then expands to nothing (in all
687  * cases).
688  */
689 static int exp_right(int nargs,
690                      const struct mx_node **args,
691                      struct sink *output,
692                      void *u) {
693   char *right;
694   rights_type r;
695   int rc;
696
697   if(!dcgi_client)
698     return 0;
699   dcgi_lookup(DCGI_RIGHTS);
700   if((rc = mx_expandstr(args[0], &right, u, "argument #0 (RIGHT)")))
701     return rc;
702   if(parse_rights(right, &r, 1/*report*/))
703     return 0;
704   /* Single-argument form */
705   if(nargs == 1)
706     return mx_bool_result(output, !!(r & dcgi_rights));
707   /* Multiple argument form */
708   if(r & dcgi_rights)
709     return mx_expand(args[1], output, u);
710   if(nargs == 3)
711     return mx_expand(args[2], output, u);
712   return 0;
713 }
714
715 /* @userinfo{PROPERTY}
716  *
717  * Expands to the named property of the current user.
718  */
719 static int exp_userinfo(int attribute((unused)) nargs,
720                         char **args,
721                         struct sink *output,
722                         void attribute((unused)) *u) {
723   char *v;
724
725   if(dcgi_client
726      && !disorder_userinfo(dcgi_client, disorder_user(dcgi_client),
727                            args[0], &v))
728     return sink_writes(output, v) < 0 ? -1 : 0;
729   return 0;
730 }
731
732 /* @error
733  *
734  * Expands to the latest error string.
735  */
736 static int exp_error(int attribute((unused)) nargs,
737                      char attribute((unused)) **args,
738                      struct sink *output,
739                      void attribute((unused)) *u) {
740   return sink_writes(output, cgi_sgmlquote(dcgi_error_string)) < 0 ? -1 : 0;
741 }
742
743 /** @brief Register DisOrder-specific expansions */
744 void dcgi_expansions(void) {
745   mx_register("arg", 1, 1, exp_arg);
746   mx_register("enabled", 0, 0, exp_enabled);
747   mx_register("error", 0, 0, exp_error);
748   mx_register("isnew", 0, 0, exp_isnew);
749   mx_register("isplaying", 0, 0, exp_isplaying);
750   mx_register("isplaying", 0, 0, exp_isqueue);
751   mx_register("isrecent", 0, 0, exp_isrecent);
752   mx_register("length", 1, 1, exp_length);
753   mx_register("movable", 1, 1, exp_movable);
754   mx_register("part", 2, 3, exp_part);
755   mx_register("paused", 0, 0, exp_paused);
756   mx_register("pref", 2, 2, exp_pref);
757   mx_register("quote", 1, 1, exp_quote);
758   mx_register("random-enabled", 0, 0, exp_random_enabled);
759   mx_register("removable", 1, 1, exp_removable);
760   mx_register("resolve", 1, 1, exp_resolve);
761   mx_register("server-version", 0, 0, exp_server_version);
762   mx_register("state", 1, 1, exp_state);
763   mx_register("thisurl", 0, 0, exp_thisurl);
764   mx_register("trackstate", 1, 1, exp_trackstate);
765   mx_register("transform", 2, 3, exp_transform);
766   mx_register("url", 0, 0, exp_url);
767   mx_register("user", 0, 0, exp_user);
768   mx_register("userinfo", 1, 1, exp_userinfo);
769   mx_register("version", 0, 0, exp_version);
770   mx_register("volume", 1, 1, exp_volume);
771   mx_register("when", 1, 1, exp_when);
772   mx_register("who", 1, 1, exp_who);
773   mx_register_magic("new", 1, 1, exp_new);
774   mx_register_magic("playing", 0, 1, exp_playing);
775   mx_register_magic("prefs", 2, 2, exp_prefs);
776   mx_register_magic("queue", 1, 1, exp_queue);
777   mx_register_magic("recent", 1, 1, exp_recent);
778   mx_register_magic("right", 1, 3, exp_right);
779 }
780
781 /*
782 Local Variables:
783 c-basic-offset:2
784 comment-column:40
785 fill-column:79
786 indent-tabs-mode:nil
787 End:
788 */