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 | ||
1e97629d | 24 | #include "disorder-cgi.h" |
9faa7a88 | 25 | |
bca4e2b7 | 26 | /** @brief For error template */ |
1e97629d | 27 | char *dcgi_error_string; |
bca4e2b7 | 28 | |
9faa7a88 RK |
29 | /** @brief Return @p i as a string */ |
30 | static const char *make_index(int i) { | |
31 | char *s; | |
32 | ||
33 | byte_xasprintf(&s, "%d", i); | |
34 | return s; | |
35 | } | |
36 | ||
bca4e2b7 | 37 | /* @server-version |
9faa7a88 RK |
38 | * |
39 | * Expands to the server's version string, or a (safe to use) error | |
40 | * value if the server is unavailable or broken. | |
41 | */ | |
42 | static int exp_server_version(int attribute((unused)) nargs, | |
43 | char attribute((unused)) **args, | |
44 | struct sink *output, | |
45 | void attribute((unused)) *u) { | |
46 | const char *v; | |
47 | ||
1e97629d RK |
48 | if(dcgi_client) { |
49 | if(disorder_version(dcgi_client, (char **)&v)) | |
9faa7a88 RK |
50 | v = "(cannot get version)"; |
51 | } else | |
52 | v = "(server not running)"; | |
71634563 | 53 | return sink_writes(output, cgi_sgmlquote(v)) < 0 ? -1 : 0; |
9faa7a88 RK |
54 | } |
55 | ||
bca4e2b7 | 56 | /* @version |
9faa7a88 RK |
57 | * |
58 | * Expands to the local version string. | |
59 | */ | |
60 | static int exp_version(int attribute((unused)) nargs, | |
61 | char attribute((unused)) **args, | |
62 | struct sink *output, | |
63 | void attribute((unused)) *u) { | |
71634563 RK |
64 | return sink_writes(output, |
65 | cgi_sgmlquote(disorder_short_version_string)) < 0 ? -1 : 0; | |
9faa7a88 RK |
66 | } |
67 | ||
bca4e2b7 | 68 | /* @url |
9faa7a88 RK |
69 | * |
70 | * Expands to the base URL of the web interface. | |
71 | */ | |
72 | static int exp_url(int attribute((unused)) nargs, | |
73 | char attribute((unused)) **args, | |
74 | struct sink *output, | |
75 | void attribute((unused)) *u) { | |
71634563 RK |
76 | return sink_writes(output, |
77 | cgi_sgmlquote(config->url)) < 0 ? -1 : 0; | |
9faa7a88 RK |
78 | } |
79 | ||
bca4e2b7 | 80 | /* @arg{NAME} |
9faa7a88 | 81 | * |
48e472fa RK |
82 | * Expands to the UNQUOTED form of CGI argument NAME, or the empty string if |
83 | * there is no such argument. Use @argq for a quick way to quote the argument. | |
9faa7a88 RK |
84 | */ |
85 | static int exp_arg(int attribute((unused)) nargs, | |
86 | char **args, | |
87 | struct sink *output, | |
88 | void attribute((unused)) *u) { | |
89 | const char *s = cgi_get(args[0]); | |
2257512d | 90 | |
9faa7a88 | 91 | if(s) |
48e472fa RK |
92 | return sink_writes(output, s) < 0 ? -1 : 0; |
93 | else | |
94 | return 0; | |
95 | } | |
96 | ||
97 | /* @argq{NAME} | |
98 | * | |
99 | * Expands to the (quoted) form of CGI argument NAME, or the empty string if | |
100 | * there is no such argument. Use @arg for the unquoted argument. | |
101 | */ | |
102 | static int exp_argq(int attribute((unused)) nargs, | |
103 | char **args, | |
104 | struct sink *output, | |
105 | void attribute((unused)) *u) { | |
106 | const char *s = cgi_get(args[0]); | |
107 | ||
108 | if(s) | |
109 | return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0; | |
9faa7a88 RK |
110 | else |
111 | return 0; | |
112 | } | |
113 | ||
bca4e2b7 | 114 | /* @user |
9faa7a88 RK |
115 | * |
116 | * Expands to the logged-in username (which might be "guest"), or to | |
117 | * the empty string if not connected. | |
118 | */ | |
119 | static int exp_user(int attribute((unused)) nargs, | |
120 | char attribute((unused)) **args, | |
121 | struct sink *output, | |
122 | void attribute((unused)) *u) { | |
71634563 | 123 | const char *user; |
9faa7a88 | 124 | |
1e97629d | 125 | if(dcgi_client && (user = disorder_user(dcgi_client))) |
71634563 | 126 | return sink_writes(output, cgi_sgmlquote(user)) < 0 ? -1 : 0; |
9faa7a88 RK |
127 | return 0; |
128 | } | |
129 | ||
130 | /* @part{TRACK|ID}{PART}{CONTEXT} | |
131 | * | |
132 | * Expands to a track name part. | |
133 | * | |
134 | * A track may be identified by name or by queue ID. | |
135 | * | |
136 | * CONTEXT may be omitted. If it is then 'display' is assumed. | |
137 | * | |
138 | * If the CONTEXT is 'short' then the 'display' part is looked up, and the | |
139 | * result truncated according to the length defined by the short_display | |
140 | * configuration directive. | |
141 | */ | |
142 | static int exp_part(int nargs, | |
143 | char **args, | |
144 | struct sink *output, | |
145 | void attribute((unused)) *u) { | |
146 | const char *track = args[0], *part = args[1]; | |
147 | const char *context = nargs > 2 ? args[2] : "display"; | |
148 | char *s; | |
149 | ||
150 | if(track[0] != '/') { | |
a2c4ad5f | 151 | struct queue_entry *q = dcgi_findtrack(track); |
9faa7a88 RK |
152 | |
153 | if(q) | |
154 | track = q->track; | |
155 | else | |
156 | return 0; | |
157 | } | |
99955407 RK |
158 | fprintf(stderr, "track=[%s] part=[%s] context=[%s] dcgi_client=[%p]\n", |
159 | track, | |
160 | part, | |
161 | !strcmp(context, "short") ? "display" : context, | |
162 | dcgi_client); | |
1e97629d RK |
163 | if(dcgi_client |
164 | && !disorder_part(dcgi_client, &s, | |
71634563 RK |
165 | track, |
166 | !strcmp(context, "short") ? "display" : context, | |
167 | part)) | |
168 | return sink_writes(output, cgi_sgmlquote(s)) < 0 ? -1 : 0; | |
9faa7a88 RK |
169 | return 0; |
170 | } | |
171 | ||
172 | /* @quote{STRING} | |
173 | * | |
174 | * SGML-quotes STRING. Note that most expansion results are already suitable | |
175 | * quoted, so this expansion is usually not required. | |
176 | */ | |
177 | static int exp_quote(int attribute((unused)) nargs, | |
178 | char **args, | |
179 | struct sink *output, | |
180 | void attribute((unused)) *u) { | |
71634563 | 181 | return sink_writes(output, cgi_sgmlquote(args[0])) < 0 ? -1 : 0; |
9faa7a88 RK |
182 | } |
183 | ||
184 | /* @who{ID} | |
185 | * | |
186 | * Expands to the name of the submitter of track ID, which must be a playing | |
187 | * track, in the queue, or in the recent list. | |
188 | */ | |
189 | static int exp_who(int attribute((unused)) nargs, | |
190 | char **args, | |
191 | struct sink *output, | |
192 | void attribute((unused)) *u) { | |
a2c4ad5f | 193 | struct queue_entry *q = dcgi_findtrack(args[0]); |
9faa7a88 RK |
194 | |
195 | if(q && q->submitter) | |
71634563 | 196 | return sink_writes(output, cgi_sgmlquote(q->submitter)) < 0 ? -1 : 0; |
9faa7a88 RK |
197 | return 0; |
198 | } | |
199 | ||
200 | /* @when{ID} | |
201 | * | |
202 | * Expands to the time a track started or is expected to start. The track must | |
203 | * be a playing track, in the queue, or in the recent list. | |
204 | */ | |
205 | static int exp_when(int attribute((unused)) nargs, | |
206 | char **args, | |
207 | struct sink *output, | |
208 | void attribute((unused)) *u) { | |
a2c4ad5f | 209 | struct queue_entry *q = dcgi_findtrack(args[0]); |
9faa7a88 RK |
210 | const struct tm *w = 0; |
211 | ||
212 | if(q) { | |
213 | switch(q->state) { | |
214 | case playing_isscratch: | |
215 | case playing_unplayed: | |
216 | case playing_random: | |
217 | if(q->expected) | |
218 | w = localtime(&q->expected); | |
219 | break; | |
220 | case playing_failed: | |
221 | case playing_no_player: | |
222 | case playing_ok: | |
223 | case playing_scratched: | |
224 | case playing_started: | |
225 | case playing_paused: | |
226 | case playing_quitting: | |
227 | if(q->played) | |
228 | w = localtime(&q->played); | |
229 | break; | |
230 | } | |
231 | if(w) | |
232 | return sink_printf(output, "%d:%02d", w->tm_hour, w->tm_min) < 0 ? -1 : 0; | |
233 | } | |
71634563 | 234 | return sink_writes(output, " ") < 0 ? -1 : 0; |
9faa7a88 RK |
235 | } |
236 | ||
237 | /* @length{ID|TRACK} | |
238 | * | |
239 | * Expands to the length of a track, identified by its queue ID or its name. | |
240 | * If it is the playing track (identified by ID) then the amount played so far | |
241 | * is included. | |
242 | */ | |
243 | static int exp_length(int attribute((unused)) nargs, | |
244 | char **args, | |
245 | struct sink *output, | |
246 | void attribute((unused)) *u) { | |
247 | struct queue_entry *q; | |
248 | long length = 0; | |
249 | const char *name; | |
250 | ||
251 | if(args[0][0] == '/') | |
252 | /* Track identified by name */ | |
253 | name = args[0]; | |
254 | else { | |
255 | /* Track identified by queue ID */ | |
a2c4ad5f | 256 | if(!(q = dcgi_findtrack(args[0]))) |
9faa7a88 | 257 | return 0; |
71634563 | 258 | if(q->state == playing_started || q->state == playing_paused) |
9faa7a88 RK |
259 | if(sink_printf(output, "%ld:%02ld/", q->sofar / 60, q->sofar % 60) < 0) |
260 | return -1; | |
261 | name = q->track; | |
262 | } | |
1e97629d | 263 | if(dcgi_client && disorder_length(dcgi_client, name, &length)) |
9faa7a88 RK |
264 | return sink_printf(output, "%ld:%02ld", |
265 | length / 60, length % 60) < 0 ? -1 : 0; | |
71634563 | 266 | return sink_writes(output, " ") < 0 ? -1 : 0; |
9faa7a88 RK |
267 | } |
268 | ||
bca4e2b7 | 269 | /* @removable{ID} |
9faa7a88 RK |
270 | * |
271 | * Expands to "true" if track ID is removable (or scratchable, if it is the | |
272 | * playing track) and "false" otherwise. | |
273 | */ | |
274 | static int exp_removable(int attribute((unused)) nargs, | |
275 | char **args, | |
276 | struct sink *output, | |
277 | void attribute((unused)) *u) { | |
a2c4ad5f | 278 | struct queue_entry *q = dcgi_findtrack(args[0]); |
9faa7a88 RK |
279 | /* TODO would be better to reject recent */ |
280 | ||
1e97629d | 281 | if(!q || !dcgi_client) |
9faa7a88 | 282 | return mx_bool_result(output, 0); |
1e97629d | 283 | dcgi_lookup(DCGI_RIGHTS); |
9faa7a88 | 284 | return mx_bool_result(output, |
1e97629d RK |
285 | (q == dcgi_playing ? right_scratchable : right_removable) |
286 | (dcgi_rights, disorder_user(dcgi_client), q)); | |
9faa7a88 RK |
287 | } |
288 | ||
af106f2d | 289 | /* @movable{ID}{DIR} |
9faa7a88 RK |
290 | * |
291 | * Expands to "true" if track ID is movable and "false" otherwise. | |
af106f2d RK |
292 | * |
293 | * DIR (which is optional) should be a non-zero integer. If it is negative | |
294 | * then the intended move is down (later in the queue), if it is positive then | |
295 | * the intended move is up (earlier in the queue). The first track is not | |
296 | * movable up and the last track not movable down. | |
9faa7a88 | 297 | */ |
af106f2d | 298 | static int exp_movable(int nargs, |
9faa7a88 RK |
299 | char **args, |
300 | struct sink *output, | |
301 | void attribute((unused)) *u) { | |
a2c4ad5f | 302 | struct queue_entry *q = dcgi_findtrack(args[0]); |
9faa7a88 RK |
303 | /* TODO would be better to recent playing/recent */ |
304 | ||
1e97629d | 305 | if(!q || !dcgi_client) |
9faa7a88 | 306 | return mx_bool_result(output, 0); |
af106f2d RK |
307 | if(nargs > 1) { |
308 | const long dir = atoi(args[1]); | |
309 | ||
310 | if(dir > 0 && q == dcgi_queue) | |
311 | return mx_bool_result(output, 0); | |
312 | if(dir < 0 && q->next == 0) | |
313 | return mx_bool_result(output, 0); | |
314 | } | |
1e97629d | 315 | dcgi_lookup(DCGI_RIGHTS); |
9faa7a88 | 316 | return mx_bool_result(output, |
af106f2d RK |
317 | right_movable(dcgi_rights, |
318 | disorder_user(dcgi_client), | |
319 | q)); | |
9faa7a88 RK |
320 | } |
321 | ||
322 | /* @playing{TEMPLATE} | |
323 | * | |
324 | * Expands to TEMPLATE, with: | |
9bf61b29 RK |
325 | * - @id expanded to the queue ID of the playing track |
326 | * - @track expanded to its UNQUOTED name | |
9faa7a88 RK |
327 | * |
328 | * If no track is playing expands to nothing. | |
329 | * | |
330 | * TEMPLATE is optional. If it is left out then instead expands to the queue | |
331 | * ID of the playing track. | |
332 | */ | |
333 | static int exp_playing(int nargs, | |
334 | const struct mx_node **args, | |
335 | struct sink *output, | |
71634563 | 336 | void *u) { |
1e97629d RK |
337 | dcgi_lookup(DCGI_PLAYING); |
338 | if(!dcgi_playing) | |
9faa7a88 RK |
339 | return 0; |
340 | if(!nargs) | |
1e97629d | 341 | return sink_writes(output, dcgi_playing->id) < 0 ? -1 : 0; |
71634563 | 342 | return mx_expand(mx_rewritel(args[0], |
1e97629d RK |
343 | "id", dcgi_playing->id, |
344 | "track", dcgi_playing->track, | |
71634563 RK |
345 | (char *)0), |
346 | output, u); | |
9faa7a88 RK |
347 | } |
348 | ||
349 | /* @queue{TEMPLATE} | |
350 | * | |
351 | * For each track in the queue, expands TEMPLATE with the following expansions: | |
9bf61b29 RK |
352 | * - @id to the queue ID of the track |
353 | * - @track to the UNQUOTED track name | |
354 | * - @index to the track number from 0 | |
355 | * - @parity to "even" or "odd" alternately | |
356 | * - @first to "true" on the first track and "false" otherwise | |
357 | * - @last to "true" on the last track and "false" otherwise | |
9faa7a88 RK |
358 | */ |
359 | static int exp_queue(int attribute((unused)) nargs, | |
360 | const struct mx_node **args, | |
361 | struct sink *output, | |
71634563 | 362 | void *u) { |
9faa7a88 RK |
363 | struct queue_entry *q; |
364 | int rc, i; | |
365 | ||
1e97629d RK |
366 | dcgi_lookup(DCGI_QUEUE); |
367 | for(q = dcgi_queue, i = 0; q; q = q->next, ++i) | |
71634563 RK |
368 | if((rc = mx_expand(mx_rewritel(args[0], |
369 | "id", q->id, | |
370 | "track", q->track, | |
371 | "index", make_index(i), | |
372 | "parity", i % 2 ? "odd" : "even", | |
1e97629d | 373 | "first", q == dcgi_queue ? "true" : "false", |
71634563 RK |
374 | "last", q->next ? "false" : "true", |
375 | (char *)0), | |
376 | output, u))) | |
9faa7a88 RK |
377 | return rc; |
378 | return 0; | |
379 | } | |
380 | ||
381 | /* @recent{TEMPLATE} | |
382 | * | |
383 | * For each track in the recently played list, expands TEMPLATE with the | |
384 | * following expansions: | |
9bf61b29 RK |
385 | * - @id to the queue ID of the track |
386 | * - @track to the UNQUOTED track name | |
387 | * - @index to the track number from 0 | |
388 | * - @parity to "even" or "odd" alternately | |
389 | * - @first to "true" on the first track and "false" otherwise | |
390 | * - @last to "true" on the last track and "false" otherwise | |
9faa7a88 RK |
391 | */ |
392 | static int exp_recent(int attribute((unused)) nargs, | |
393 | const struct mx_node **args, | |
394 | struct sink *output, | |
71634563 | 395 | void *u) { |
9faa7a88 RK |
396 | struct queue_entry *q; |
397 | int rc, i; | |
398 | ||
1e97629d RK |
399 | dcgi_lookup(DCGI_RECENT); |
400 | for(q = dcgi_recent, i = 0; q; q = q->next, ++i) | |
71634563 RK |
401 | if((rc = mx_expand(mx_rewritel(args[0], |
402 | "id", q->id, | |
403 | "track", q->track, | |
404 | "index", make_index(i), | |
405 | "parity", i % 2 ? "odd" : "even", | |
1e97629d | 406 | "first", q == dcgi_recent ? "true" : "false", |
71634563 RK |
407 | "last", q->next ? "false" : "true", |
408 | (char *)0), | |
409 | output, u))) | |
9faa7a88 RK |
410 | return rc; |
411 | return 0; | |
412 | } | |
413 | ||
414 | /* @new{TEMPLATE} | |
415 | * | |
416 | * For each track in the newly added list, expands TEMPLATE wit the following | |
417 | * expansions: | |
9bf61b29 RK |
418 | * - @track to the UNQUOTED track name |
419 | * - @index to the track number from 0 | |
420 | * - @parity to "even" or "odd" alternately | |
421 | * - @first to "true" on the first track and "false" otherwise | |
422 | * - @last to "true" on the last track and "false" otherwise | |
9faa7a88 | 423 | * |
9bf61b29 RK |
424 | * Note that unlike @playing, @queue and @recent which are otherwise |
425 | * superficially similar, there is no @id sub-expansion here. | |
9faa7a88 RK |
426 | */ |
427 | static int exp_new(int attribute((unused)) nargs, | |
428 | const struct mx_node **args, | |
429 | struct sink *output, | |
71634563 | 430 | void *u) { |
9faa7a88 RK |
431 | int rc, i; |
432 | ||
1e97629d | 433 | dcgi_lookup(DCGI_NEW); |
9faa7a88 | 434 | /* TODO perhaps we should generate an ID value for tracks in the new list */ |
1e97629d | 435 | for(i = 0; i < dcgi_nnew; ++i) |
71634563 | 436 | if((rc = mx_expand(mx_rewritel(args[0], |
1e97629d | 437 | "track", dcgi_new[i], |
71634563 RK |
438 | "index", make_index(i), |
439 | "parity", i % 2 ? "odd" : "even", | |
440 | "first", i == 0 ? "true" : "false", | |
1e97629d | 441 | "last", i == dcgi_nnew - 1 ? "false" : "true", |
71634563 RK |
442 | (char *)0), |
443 | output, u))) | |
9faa7a88 RK |
444 | return rc; |
445 | return 0; | |
446 | } | |
447 | ||
bca4e2b7 | 448 | /* @volume{CHANNEL} |
9faa7a88 RK |
449 | * |
450 | * Expands to the volume in a given channel. CHANNEL must be "left" or | |
451 | * "right". | |
452 | */ | |
453 | static int exp_volume(int attribute((unused)) nargs, | |
454 | char **args, | |
455 | struct sink *output, | |
456 | void attribute((unused)) *u) { | |
1e97629d | 457 | dcgi_lookup(DCGI_VOLUME); |
71634563 RK |
458 | return sink_printf(output, "%d", |
459 | !strcmp(args[0], "left") | |
1e97629d | 460 | ? dcgi_volume_left : dcgi_volume_right) < 0 ? -1 : 0; |
9faa7a88 RK |
461 | } |
462 | ||
bca4e2b7 | 463 | /* @isplaying |
9faa7a88 RK |
464 | * |
465 | * Expands to "true" if there is a playing track, otherwise "false". | |
466 | */ | |
467 | static int exp_isplaying(int attribute((unused)) nargs, | |
468 | char attribute((unused)) **args, | |
469 | struct sink *output, | |
470 | void attribute((unused)) *u) { | |
1e97629d RK |
471 | dcgi_lookup(DCGI_PLAYING); |
472 | return mx_bool_result(output, !!dcgi_playing); | |
9faa7a88 RK |
473 | } |
474 | ||
bca4e2b7 | 475 | /* @isqueue |
9faa7a88 RK |
476 | * |
477 | * Expands to "true" if there the queue is nonempty, otherwise "false". | |
478 | */ | |
479 | static int exp_isqueue(int attribute((unused)) nargs, | |
480 | char attribute((unused)) **args, | |
481 | struct sink *output, | |
482 | void attribute((unused)) *u) { | |
1e97629d RK |
483 | dcgi_lookup(DCGI_QUEUE); |
484 | return mx_bool_result(output, !!dcgi_queue); | |
9faa7a88 RK |
485 | } |
486 | ||
487 | /* @isrecent@ | |
488 | * | |
489 | * Expands to "true" if there the recently played list is nonempty, otherwise | |
490 | * "false". | |
491 | */ | |
492 | static int exp_isrecent(int attribute((unused)) nargs, | |
493 | char attribute((unused)) **args, | |
494 | struct sink *output, | |
495 | void attribute((unused)) *u) { | |
1e97629d RK |
496 | dcgi_lookup(DCGI_RECENT); |
497 | return mx_bool_result(output, !!dcgi_recent); | |
9faa7a88 RK |
498 | } |
499 | ||
bca4e2b7 | 500 | /* @isnew |
9faa7a88 RK |
501 | * |
502 | * Expands to "true" if there the newly added track list is nonempty, otherwise | |
503 | * "false". | |
504 | */ | |
505 | static int exp_isnew(int attribute((unused)) nargs, | |
506 | char attribute((unused)) **args, | |
507 | struct sink *output, | |
508 | void attribute((unused)) *u) { | |
1e97629d RK |
509 | dcgi_lookup(DCGI_NEW); |
510 | return mx_bool_result(output, !!dcgi_nnew); | |
9faa7a88 RK |
511 | } |
512 | ||
bca4e2b7 | 513 | /* @pref{TRACK}{KEY} |
9faa7a88 RK |
514 | * |
515 | * Expands to a track preference. | |
516 | */ | |
517 | static int exp_pref(int attribute((unused)) nargs, | |
518 | char **args, | |
519 | struct sink *output, | |
520 | void attribute((unused)) *u) { | |
521 | char *value; | |
522 | ||
1e97629d | 523 | if(dcgi_client && !disorder_get(dcgi_client, args[0], args[1], &value)) |
71634563 RK |
524 | return sink_writes(output, cgi_sgmlquote(value)) < 0 ? -1 : 0; |
525 | return 0; | |
9faa7a88 RK |
526 | } |
527 | ||
bca4e2b7 | 528 | /* @prefs{TRACK}{TEMPLATE} |
9faa7a88 RK |
529 | * |
530 | * For each track preference of track TRACK, expands TEMPLATE with the | |
531 | * following expansions: | |
9bf61b29 RK |
532 | * - @name to the UNQUOTED preference name |
533 | * - @index to the preference number from 0 | |
534 | * - @value to the UNQUOTED preference value | |
535 | * - @parity to "even" or "odd" alternately | |
536 | * - @first to "true" on the first preference and "false" otherwise | |
537 | * - @last to "true" on the last preference and "false" otherwise | |
9faa7a88 | 538 | * |
9bf61b29 | 539 | * Use @quote to quote preference names and values where necessary; see below. |
9faa7a88 RK |
540 | */ |
541 | static int exp_prefs(int attribute((unused)) nargs, | |
542 | const struct mx_node **args, | |
543 | struct sink *output, | |
71634563 | 544 | void *u) { |
9faa7a88 RK |
545 | int rc, i; |
546 | struct kvp *k, *head; | |
547 | char *track; | |
548 | ||
549 | if((rc = mx_expandstr(args[0], &track, u, "argument #0 (TRACK)"))) | |
550 | return rc; | |
1e97629d | 551 | if(!dcgi_client || disorder_prefs(dcgi_client, track, &head)) |
9faa7a88 RK |
552 | return 0; |
553 | for(k = head, i = 0; k; k = k->next, ++i) | |
71634563 RK |
554 | if((rc = mx_expand(mx_rewritel(args[1], |
555 | "index", make_index(i), | |
556 | "parity", i % 2 ? "odd" : "even", | |
557 | "name", k->name, | |
558 | "value", k->value, | |
559 | "first", k == head ? "true" : "false", | |
560 | "last", k->next ? "false" : "true", | |
561 | (char *)0), | |
562 | output, u))) | |
9faa7a88 RK |
563 | return rc; |
564 | return 0; | |
565 | } | |
566 | ||
bca4e2b7 | 567 | /* @transform{TRACK}{TYPE}{CONTEXT} |
9faa7a88 RK |
568 | * |
569 | * Transforms a track name (if TYPE is "track") or directory name (if type is | |
570 | * "dir"). CONTEXT should be the context, if it is left out then "display" is | |
571 | * assumed. | |
572 | */ | |
573 | static int exp_transform(int nargs, | |
574 | char **args, | |
575 | struct sink *output, | |
576 | void attribute((unused)) *u) { | |
577 | const char *t = trackname_transform(args[1], args[0], | |
71634563 RK |
578 | (nargs > 2 ? args[2] : "display")); |
579 | return sink_writes(output, cgi_sgmlquote(t)) < 0 ? -1 : 0; | |
9faa7a88 RK |
580 | } |
581 | ||
582 | /* @enabled@ | |
583 | * | |
584 | * Expands to "true" if playing is enabled, otherwise "false". | |
585 | */ | |
586 | static int exp_enabled(int attribute((unused)) nargs, | |
587 | char attribute((unused)) **args, | |
588 | struct sink *output, | |
589 | void attribute((unused)) *u) { | |
1e97629d | 590 | int e = 0; |
9faa7a88 | 591 | |
1e97629d RK |
592 | if(dcgi_client) |
593 | disorder_enabled(dcgi_client, &e); | |
594 | return mx_bool_result(output, e); | |
9faa7a88 RK |
595 | } |
596 | ||
bca4e2b7 | 597 | /* @random-enabled |
9faa7a88 RK |
598 | * |
599 | * Expands to "true" if random play is enabled, otherwise "false". | |
600 | */ | |
71634563 RK |
601 | static int exp_random_enabled(int attribute((unused)) nargs, |
602 | char attribute((unused)) **args, | |
603 | struct sink *output, | |
604 | void attribute((unused)) *u) { | |
1e97629d | 605 | int e = 0; |
9faa7a88 | 606 | |
1e97629d RK |
607 | if(dcgi_client) |
608 | disorder_random_enabled(dcgi_client, &e); | |
609 | return mx_bool_result(output, e); | |
9faa7a88 RK |
610 | } |
611 | ||
04024c2c | 612 | /* @trackstate{TRACK} |
9faa7a88 RK |
613 | * |
614 | * Expands to "playing" if TRACK is currently playing, or "queue" if it is in | |
615 | * the queue, otherwise to nothing. | |
616 | */ | |
617 | static int exp_trackstate(int attribute((unused)) nargs, | |
618 | char **args, | |
619 | struct sink *output, | |
620 | void attribute((unused)) *u) { | |
621 | char *track; | |
622 | struct queue_entry *q; | |
623 | ||
1e97629d | 624 | if(!dcgi_client) |
9faa7a88 | 625 | return 0; |
1e97629d | 626 | if(disorder_resolve(dcgi_client, &track, args[0])) |
9faa7a88 | 627 | return 0; |
1e97629d RK |
628 | dcgi_lookup(DCGI_PLAYING); |
629 | if(dcgi_playing && !strcmp(track, dcgi_playing->track)) | |
71634563 | 630 | return sink_writes(output, "playing") < 0 ? -1 : 0; |
1e97629d RK |
631 | dcgi_lookup(DCGI_QUEUE); |
632 | for(q = dcgi_queue; q; q = q->next) | |
9faa7a88 | 633 | if(!strcmp(track, q->track)) |
71634563 | 634 | return sink_writes(output, "queued") < 0 ? -1 : 0; |
9faa7a88 RK |
635 | return 0; |
636 | } | |
637 | ||
bca4e2b7 | 638 | /* @thisurl |
9faa7a88 RK |
639 | * |
640 | * Expands to an UNQUOTED URL which points back to the current page. (NB it | |
641 | * might not be byte for byte identical - for instance, CGI arguments might be | |
642 | * re-ordered.) | |
643 | */ | |
644 | static int exp_thisurl(int attribute((unused)) nargs, | |
645 | char attribute((unused)) **args, | |
646 | struct sink *output, | |
647 | void attribute((unused)) *u) { | |
71634563 | 648 | return sink_writes(output, cgi_thisurl(config->url)) < 0 ? -1 : 0; |
9faa7a88 RK |
649 | } |
650 | ||
bca4e2b7 | 651 | /* @resolve{TRACK} |
9faa7a88 RK |
652 | * |
653 | * Expands to an UNQUOTED name for the TRACK that is not an alias, or to | |
654 | * nothing if it is not a valid track. | |
655 | */ | |
656 | static int exp_resolve(int attribute((unused)) nargs, | |
657 | char **args, | |
658 | struct sink *output, | |
659 | void attribute((unused)) *u) { | |
660 | char *r; | |
661 | ||
1e97629d | 662 | if(dcgi_client && !disorder_resolve(dcgi_client, &r, args[0])) |
71634563 | 663 | return sink_writes(output, r) < 0 ? -1 : 0; |
9faa7a88 RK |
664 | return 0; |
665 | } | |
666 | ||
bca4e2b7 | 667 | /* @paused |
9faa7a88 RK |
668 | * |
669 | * Expands to "true" if the playing track is paused, to "false" if it is | |
670 | * playing (or if there is no playing track at all). | |
671 | */ | |
672 | static int exp_paused(int attribute((unused)) nargs, | |
673 | char attribute((unused)) **args, | |
674 | struct sink *output, | |
48e472fa | 675 | void attribute((unused)) *u) { |
1e97629d RK |
676 | dcgi_lookup(DCGI_PLAYING); |
677 | return mx_bool_result(output, (dcgi_playing | |
678 | && dcgi_playing->state == playing_paused)); | |
9faa7a88 RK |
679 | } |
680 | ||
681 | /* @state{ID}@ | |
682 | * | |
683 | * Expands to the current state of track ID. | |
684 | */ | |
685 | static int exp_state(int attribute((unused)) nargs, | |
686 | char **args, | |
687 | struct sink *output, | |
688 | void attribute((unused)) *u) { | |
a2c4ad5f | 689 | struct queue_entry *q = dcgi_findtrack(args[0]); |
9faa7a88 RK |
690 | |
691 | if(q) | |
71634563 | 692 | return sink_writes(output, playing_states[q->state]) < 0 ? -1 : 0; |
9faa7a88 RK |
693 | return 0; |
694 | } | |
695 | ||
696 | /* @right{RIGHT}{WITH-RIGHT}{WITHOUT-RIGHT}@ | |
697 | * | |
698 | * Expands to WITH-RIGHT if the current user has right RIGHT, otherwise to | |
699 | * WITHOUT-RIGHT. The WITHOUT-RIGHT argument may be left out. | |
700 | * | |
701 | * If both WITH-RIGHT and WITHOUT-RIGHT are left out then expands to "true" if | |
702 | * the user has the right and "false" otherwise. | |
703 | * | |
704 | * If there is no connection to the server then expands to nothing (in all | |
705 | * cases). | |
706 | */ | |
707 | static int exp_right(int nargs, | |
708 | const struct mx_node **args, | |
709 | struct sink *output, | |
71634563 | 710 | void *u) { |
9faa7a88 RK |
711 | char *right; |
712 | rights_type r; | |
71634563 | 713 | int rc; |
9faa7a88 | 714 | |
1e97629d | 715 | if(!dcgi_client) |
9faa7a88 | 716 | return 0; |
1e97629d | 717 | dcgi_lookup(DCGI_RIGHTS); |
71634563 | 718 | if((rc = mx_expandstr(args[0], &right, u, "argument #0 (RIGHT)"))) |
9faa7a88 RK |
719 | return rc; |
720 | if(parse_rights(right, &r, 1/*report*/)) | |
721 | return 0; | |
722 | /* Single-argument form */ | |
723 | if(nargs == 1) | |
1e97629d | 724 | return mx_bool_result(output, !!(r & dcgi_rights)); |
9faa7a88 | 725 | /* Multiple argument form */ |
1e97629d | 726 | if(r & dcgi_rights) |
71634563 | 727 | return mx_expand(args[1], output, u); |
9faa7a88 | 728 | if(nargs == 3) |
71634563 | 729 | return mx_expand(args[2], output, u); |
9faa7a88 RK |
730 | return 0; |
731 | } | |
732 | ||
bca4e2b7 | 733 | /* @userinfo{PROPERTY} |
9faa7a88 RK |
734 | * |
735 | * Expands to the named property of the current user. | |
736 | */ | |
737 | static int exp_userinfo(int attribute((unused)) nargs, | |
738 | char **args, | |
739 | struct sink *output, | |
740 | void attribute((unused)) *u) { | |
741 | char *v; | |
742 | ||
1e97629d RK |
743 | if(dcgi_client |
744 | && !disorder_userinfo(dcgi_client, disorder_user(dcgi_client), | |
745 | args[0], &v)) | |
71634563 | 746 | return sink_writes(output, v) < 0 ? -1 : 0; |
9faa7a88 RK |
747 | return 0; |
748 | } | |
749 | ||
bca4e2b7 RK |
750 | /* @error |
751 | * | |
752 | * Expands to the latest error string. | |
753 | */ | |
754 | static int exp_error(int attribute((unused)) nargs, | |
755 | char attribute((unused)) **args, | |
756 | struct sink *output, | |
757 | void attribute((unused)) *u) { | |
0d0253c9 | 758 | return sink_writes(output, dcgi_error_string) < 0 ? -1 : 0; |
bca4e2b7 RK |
759 | } |
760 | ||
30d85588 RK |
761 | /* @image{NAME} |
762 | * | |
763 | * Expands to the URL of the image called NAME. | |
764 | * | |
765 | * Firstly if there is a label called images.NAME then the image stem will be | |
766 | * the value of that label. Otherwise the stem will be NAME.png. | |
767 | * | |
768 | * If the label url.static exists then it will give the base URL for images. | |
769 | * Otherwise the base url will be /disorder/. | |
770 | */ | |
771 | static int exp_image(int attribute((unused)) nargs, | |
772 | char **args, | |
773 | struct sink *output, | |
774 | void attribute((unused)) *u) { | |
775 | const char *url, *stem; | |
776 | char *labelname; | |
777 | ||
778 | /* Compute the stem */ | |
779 | byte_xasprintf(&labelname, "images.%s", args[0]); | |
780 | if(option_label_exists(labelname)) | |
781 | stem = option_label(labelname); | |
782 | else | |
783 | byte_xasprintf((char **)&stem, "%s.png", args[0]); | |
784 | /* If the stem looks like it's reasonalby complete, use that */ | |
785 | if(stem[0] == '/' | |
786 | || !strncmp(stem, "http:", 5) | |
787 | || !strncmp(stem, "https:", 6)) | |
788 | url = stem; | |
789 | else { | |
790 | /* Compute the URL */ | |
791 | if(option_label_exists("url.static")) | |
792 | byte_xasprintf((char **)&url, "%s/%s", | |
793 | option_label("url.static"), stem); | |
794 | else | |
795 | /* Default base is /disorder */ | |
796 | byte_xasprintf((char **)&url, "/disorder/%s", stem); | |
797 | } | |
798 | return sink_writes(output, cgi_sgmlquote(url)) < 0 ? -1 : 0; | |
799 | } | |
800 | ||
9bf61b29 RK |
801 | /** @brief Entry in a list of tracks or directories */ |
802 | struct entry { | |
803 | /** @brief Track name */ | |
804 | const char *track; | |
805 | /** @brief Sort key */ | |
806 | const char *sort; | |
807 | /** @brief Display key */ | |
808 | const char *display; | |
809 | }; | |
810 | ||
811 | /** @brief Compare two @ref entry objects */ | |
812 | static int compare_entry(const void *a, const void *b) { | |
813 | const struct entry *ea = a, *eb = b; | |
814 | ||
815 | return compare_tracks(ea->sort, eb->sort, | |
816 | ea->display, eb->display, | |
817 | ea->track, eb->track); | |
818 | } | |
819 | ||
820 | /** @brief Implementation of exp_tracks() and exp_dirs() */ | |
821 | static int exp__files_dirs(int nargs, | |
822 | const struct mx_node **args, | |
823 | struct sink *output, | |
824 | void *u, | |
825 | const char *type, | |
826 | int (*fn)(disorder_client *client, | |
827 | const char *name, | |
828 | const char *re, | |
829 | char ***vecp, | |
830 | int *nvecp)) { | |
831 | char **tracks, *dir, *re; | |
832 | int n, ntracks, rc; | |
833 | const struct mx_node *m; | |
834 | struct entry *e; | |
835 | ||
836 | if((rc = mx_expandstr(args[0], &dir, u, "argument #0 (DIR)"))) | |
837 | return rc; | |
838 | if(nargs == 3) { | |
839 | if((rc = mx_expandstr(args[1], &re, u, "argument #1 (RE)"))) | |
840 | return rc; | |
841 | m = args[2]; | |
842 | } else { | |
843 | re = 0; | |
844 | m = args[1]; | |
845 | } | |
846 | if(!dcgi_client) | |
847 | return 0; | |
848 | /* Get the list */ | |
849 | if(fn(dcgi_client, dir, re, &tracks, &ntracks)) | |
850 | return 0; | |
851 | /* Sort it. NB trackname_transform() does not go to the server. */ | |
852 | e = xcalloc(ntracks, sizeof *e); | |
853 | for(n = 0; n < ntracks; ++n) { | |
854 | e->track = tracks[n]; | |
855 | e[n].track = tracks[n]; | |
856 | e[n].sort = trackname_transform(type, tracks[n], "sort"); | |
857 | e[n].display = trackname_transform(type, tracks[n], "display"); | |
858 | } | |
859 | qsort(e, ntracks, sizeof (struct entry), compare_entry); | |
48e472fa RK |
860 | /* Expand the subsiduary templates. We chuck in @sort and @display because |
861 | * it is particularly easy to do so. */ | |
9bf61b29 RK |
862 | for(n = 0; n < ntracks; ++n) |
863 | if((rc = mx_expand(mx_rewritel(m, | |
864 | "index", make_index(n), | |
865 | "parity", n % 2 ? "odd" : "even", | |
866 | "track", tracks[n], | |
867 | "first", n == 0 ? "true" : "false", | |
868 | "last", n + 1 == ntracks ? "false" : "true", | |
48e472fa RK |
869 | "sort", e[n].sort, |
870 | "display", e[n].display, | |
9bf61b29 RK |
871 | (char *)0), |
872 | output, u))) | |
873 | return rc; | |
874 | return 0; | |
875 | ||
876 | } | |
877 | ||
878 | /** @tracks{DIR}{RE}{TEMPLATE} | |
879 | * | |
880 | * For each track below DIR, expands TEMPLATE with the | |
881 | * following expansions: | |
882 | * - @track to the UNQUOTED track name | |
883 | * - @index to the track number from 0 | |
884 | * - @parity to "even" or "odd" alternately | |
885 | * - @first to "true" on the first track and "false" otherwise | |
886 | * - @last to "true" on the last track and "false" otherwise | |
48e472fa RK |
887 | * - @sort to the sort key for this track |
888 | * - @display to the UNQUOTED display string for this track | |
9bf61b29 RK |
889 | * |
890 | * RE is optional and if present is the regexp to match against. | |
891 | */ | |
892 | static int exp_tracks(int nargs, | |
893 | const struct mx_node **args, | |
894 | struct sink *output, | |
895 | void *u) { | |
896 | return exp__files_dirs(nargs, args, output, u, "track", disorder_files); | |
897 | } | |
898 | ||
899 | /** @dirs{DIR}{RE}{TEMPLATE} | |
900 | * | |
901 | * For each directory below DIR, expands TEMPLATE with the | |
902 | * following expansions: | |
903 | * - @track to the UNQUOTED directory name | |
904 | * - @index to the directory number from 0 | |
905 | * - @parity to "even" or "odd" alternately | |
906 | * - @first to "true" on the first directory and "false" otherwise | |
907 | * - @last to "true" on the last directory and "false" otherwise | |
48e472fa RK |
908 | * - @sort to the sort key for this directory |
909 | * - @display to the UNQUOTED display string for this directory | |
9bf61b29 RK |
910 | * |
911 | * RE is optional and if present is the regexp to match against. | |
912 | */ | |
913 | static int exp_dirs(int nargs, | |
914 | const struct mx_node **args, | |
915 | struct sink *output, | |
916 | void *u) { | |
917 | return exp__files_dirs(nargs, args, output, u, "dir", disorder_directories); | |
918 | } | |
919 | ||
b43dc0cc RK |
920 | static int exp__search_shim(disorder_client *c, const char *terms, |
921 | const char attribute((unused)) *re, | |
922 | char ***vecp, int *nvecp) { | |
923 | return disorder_search(c, terms, vecp, nvecp); | |
924 | } | |
925 | ||
926 | /** @search{KEYWORDS}{TEMPLATE} | |
927 | * | |
928 | * For each track matching KEYWORDS, expands TEMPLATE with the | |
929 | * following expansions: | |
930 | * - @track to the UNQUOTED directory name | |
931 | * - @index to the directory number from 0 | |
932 | * - @parity to "even" or "odd" alternately | |
933 | * - @first to "true" on the first directory and "false" otherwise | |
934 | * - @last to "true" on the last directory and "false" otherwise | |
935 | * - @sort to the sort key for this track | |
936 | * - @display to the UNQUOTED display string for this track | |
937 | */ | |
938 | static int exp_search(int nargs, | |
99955407 RK |
939 | const struct mx_node **args, |
940 | struct sink *output, | |
941 | void *u) { | |
b43dc0cc RK |
942 | return exp__files_dirs(nargs, args, output, u, "track", exp__search_shim); |
943 | } | |
944 | ||
99955407 RK |
945 | static int exp_label(int attribute((unused)) nargs, |
946 | char **args, | |
947 | struct sink *output, | |
948 | void attribute((unused)) *u) { | |
949 | return sink_writes(output, option_label(args[0])) < 0 ? -1 : 0; | |
950 | } | |
951 | ||
9faa7a88 | 952 | /** @brief Register DisOrder-specific expansions */ |
1e97629d | 953 | void dcgi_expansions(void) { |
71634563 | 954 | mx_register("arg", 1, 1, exp_arg); |
48e472fa | 955 | mx_register("argq", 1, 1, exp_argq); |
71634563 RK |
956 | mx_register("enabled", 0, 0, exp_enabled); |
957 | mx_register("error", 0, 0, exp_error); | |
30d85588 | 958 | mx_register("image", 1, 1, exp_image); |
71634563 RK |
959 | mx_register("isnew", 0, 0, exp_isnew); |
960 | mx_register("isplaying", 0, 0, exp_isplaying); | |
99955407 | 961 | mx_register("isqueue", 0, 0, exp_isqueue); |
71634563 | 962 | mx_register("isrecent", 0, 0, exp_isrecent); |
99955407 | 963 | mx_register("label", 1, 1, exp_label); |
71634563 | 964 | mx_register("length", 1, 1, exp_length); |
99955407 | 965 | mx_register("movable", 1, 2, exp_movable); |
71634563 RK |
966 | mx_register("part", 2, 3, exp_part); |
967 | mx_register("paused", 0, 0, exp_paused); | |
968 | mx_register("pref", 2, 2, exp_pref); | |
969 | mx_register("quote", 1, 1, exp_quote); | |
970 | mx_register("random-enabled", 0, 0, exp_random_enabled); | |
971 | mx_register("removable", 1, 1, exp_removable); | |
972 | mx_register("resolve", 1, 1, exp_resolve); | |
973 | mx_register("server-version", 0, 0, exp_server_version); | |
974 | mx_register("state", 1, 1, exp_state); | |
975 | mx_register("thisurl", 0, 0, exp_thisurl); | |
976 | mx_register("trackstate", 1, 1, exp_trackstate); | |
977 | mx_register("transform", 2, 3, exp_transform); | |
978 | mx_register("url", 0, 0, exp_url); | |
979 | mx_register("user", 0, 0, exp_user); | |
980 | mx_register("userinfo", 1, 1, exp_userinfo); | |
981 | mx_register("version", 0, 0, exp_version); | |
982 | mx_register("volume", 1, 1, exp_volume); | |
983 | mx_register("when", 1, 1, exp_when); | |
984 | mx_register("who", 1, 1, exp_who); | |
9bf61b29 | 985 | mx_register_magic("dirs", 2, 3, exp_dirs); |
71634563 RK |
986 | mx_register_magic("new", 1, 1, exp_new); |
987 | mx_register_magic("playing", 0, 1, exp_playing); | |
988 | mx_register_magic("prefs", 2, 2, exp_prefs); | |
989 | mx_register_magic("queue", 1, 1, exp_queue); | |
990 | mx_register_magic("recent", 1, 1, exp_recent); | |
991 | mx_register_magic("right", 1, 3, exp_right); | |
b43dc0cc | 992 | mx_register_magic("search", 2, 2, exp_search); |
9bf61b29 | 993 | mx_register_magic("tracks", 2, 3, exp_tracks); |
bca4e2b7 RK |
994 | } |
995 | ||
9faa7a88 RK |
996 | /* |
997 | Local Variables: | |
998 | c-basic-offset:2 | |
999 | comment-column:40 | |
1000 | fill-column:79 | |
1001 | indent-tabs-mode:nil | |
1002 | End: | |
1003 | */ |