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