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