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