2 * This file is part of DisOrder.
3 * Copyright (C) 2004, 2005, 2006, 2007 Richard Kettlewell
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.
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.
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
26 #include <sys/types.h>
27 #include <sys/socket.h>
44 #include "configuration.h"
55 #include "trackname.h"
58 static void expand(cgi_sink
*output
,
61 static void expandstring(cgi_sink
*output
,
71 static const char *nonce(void) {
72 static unsigned long count
;
75 byte_xasprintf(&s
, "%lx%lx%lx",
76 (unsigned long)time(0),
77 (unsigned long)getpid(),
82 static int compare_entry(const void *a
, const void *b
) {
83 const struct entry
*ea
= a
, *eb
= b
;
85 return compare_tracks(ea
->sort
, eb
->sort
,
86 ea
->display
, eb
->display
,
90 static const char *front_url(void) {
94 /* preserve management interface visibility */
95 if((mgmt
= cgi_get("mgmt")) && !strcmp(mgmt
, "true")) {
96 byte_xasprintf(&url
, "%s?mgmt=true", config
->url
);
102 static void redirect(struct sink
*output
) {
105 cgi_header(output
, "Location",
106 (back
= cgi_get("back")) ? back
: front_url());
110 static void lookups(dcgi_state
*ds
, unsigned want
) {
112 struct queue_entry
*r
, *rnext
;
113 const char *dir
, *re
;
115 if(ds
->g
->client
&& (need
= want
^ (ds
->g
->flags
& want
)) != 0) {
117 disorder_queue(ds
->g
->client
, &ds
->g
->queue
);
118 if(need
& DC_PLAYING
)
119 disorder_playing(ds
->g
->client
, &ds
->g
->playing
);
121 disorder_new_tracks(ds
->g
->client
, &ds
->g
->new, &ds
->g
->nnew
, 0);
122 if(need
& DC_RECENT
) {
123 /* we need to reverse the order of the list */
124 disorder_recent(ds
->g
->client
, &r
);
127 r
->next
= ds
->g
->recent
;
133 disorder_get_volume(ds
->g
->client
,
134 &ds
->g
->volume_left
, &ds
->g
->volume_right
);
135 if(need
& (DC_FILES
|DC_DIRS
)) {
136 if(!(dir
= cgi_get("directory")))
138 re
= cgi_get("regexp");
140 if(disorder_directories(ds
->g
->client
, dir
, re
,
141 &ds
->g
->dirs
, &ds
->g
->ndirs
))
144 if(disorder_files(ds
->g
->client
, dir
, re
,
145 &ds
->g
->files
, &ds
->g
->nfiles
))
148 ds
->g
->flags
|= need
;
152 /* actions ********************************************************************/
154 static void act_disable(cgi_sink
*output
,
157 disorder_disable(ds
->g
->client
);
158 redirect(output
->sink
);
161 static void act_enable(cgi_sink
*output
,
164 disorder_enable(ds
->g
->client
);
165 redirect(output
->sink
);
168 static void act_random_disable(cgi_sink
*output
,
171 disorder_random_disable(ds
->g
->client
);
172 redirect(output
->sink
);
175 static void act_random_enable(cgi_sink
*output
,
178 disorder_random_enable(ds
->g
->client
);
179 redirect(output
->sink
);
182 static void act_remove(cgi_sink
*output
,
186 if(!(id
= cgi_get("id"))) fatal(0, "missing id argument");
188 disorder_remove(ds
->g
->client
, id
);
189 redirect(output
->sink
);
192 static void act_move(cgi_sink
*output
,
194 const char *id
, *delta
;
196 if(!(id
= cgi_get("id"))) fatal(0, "missing id argument");
197 if(!(delta
= cgi_get("delta"))) fatal(0, "missing delta argument");
199 disorder_move(ds
->g
->client
, id
, atoi(delta
));
200 redirect(output
->sink
);
203 static void act_scratch(cgi_sink
*output
,
206 disorder_scratch(ds
->g
->client
, cgi_get("id"));
207 redirect(output
->sink
);
210 static void act_playing(cgi_sink
*output
, dcgi_state
*ds
) {
212 long refresh
= config
->refresh
, length
;
214 int random_enabled
= 0;
217 lookups(ds
, DC_PLAYING
|DC_QUEUE
);
218 cgi_header(output
->sink
, "Content-Type", "text/html");
219 disorder_random_enabled(ds
->g
->client
, &random_enabled
);
220 disorder_enabled(ds
->g
->client
, &enabled
);
222 && ds
->g
->playing
->state
== playing_started
/* i.e. not paused */
223 && !disorder_length(ds
->g
->client
, ds
->g
->playing
->track
, &length
)
225 && ds
->g
->playing
->sofar
>= 0) {
226 /* Try to put the next refresh at the start of the next track. */
228 fin
= now
+ length
- ds
->g
->playing
->sofar
+ config
->gap
;
229 if(now
+ refresh
> fin
)
232 if(ds
->g
->queue
&& ds
->g
->queue
->state
== playing_isscratch
) {
233 /* next track is a scratch, don't leave more than the inter-track gap */
234 if(refresh
> config
->gap
)
235 refresh
= config
->gap
;
237 if(!ds
->g
->playing
&& ((ds
->g
->queue
238 && ds
->g
->queue
->state
!= playing_random
)
239 || random_enabled
) && enabled
) {
240 /* no track playing but playing is enabled and there is something coming
241 * up, must be in a gap */
242 if(refresh
> config
->gap
)
243 refresh
= config
->gap
;
245 byte_snprintf(r
, sizeof r
, "%ld;url=%s", refresh
> 0 ? refresh
: 1,
247 cgi_header(output
->sink
, "Refresh", r
);
248 cgi_body(output
->sink
);
249 expand(output
, "playing", ds
);
252 static void act_play(cgi_sink
*output
,
254 const char *track
, *dir
;
259 if((track
= cgi_get("file"))) {
260 disorder_play(ds
->g
->client
, track
);
261 } else if((dir
= cgi_get("directory"))) {
262 if(disorder_files(ds
->g
->client
, dir
, 0, &tracks
, &ntracks
)) ntracks
= 0;
264 e
= xmalloc(ntracks
* sizeof (struct entry
));
265 for(n
= 0; n
< ntracks
; ++n
) {
266 e
[n
].path
= tracks
[n
];
267 e
[n
].sort
= trackname_transform("track", tracks
[n
], "sort");
268 e
[n
].display
= trackname_transform("track", tracks
[n
], "display");
270 qsort(e
, ntracks
, sizeof (struct entry
), compare_entry
);
271 for(n
= 0; n
< ntracks
; ++n
)
272 disorder_play(ds
->g
->client
, e
[n
].path
);
275 /* XXX error handling */
276 redirect(output
->sink
);
279 static int clamp(int n
, int min
, int max
) {
287 static const char *volume_url(void) {
290 byte_xasprintf(&url
, "%s?action=volume", config
->url
);
294 static void act_volume(cgi_sink
*output
, dcgi_state
*ds
) {
295 const char *l
, *r
, *d
, *back
;
296 int nd
, changed
= 0;;
298 if((d
= cgi_get("delta"))) {
299 lookups(ds
, DC_VOLUME
);
300 nd
= clamp(atoi(d
), -255, 255);
301 disorder_set_volume(ds
->g
->client
,
302 clamp(ds
->g
->volume_left
+ nd
, 0, 255),
303 clamp(ds
->g
->volume_right
+ nd
, 0, 255));
305 } else if((l
= cgi_get("left")) && (r
= cgi_get("right"))) {
306 disorder_set_volume(ds
->g
->client
, atoi(l
), atoi(r
));
310 /* redirect back to ourselves (but without the volume-changing bits in the
312 cgi_header(output
->sink
, "Location",
313 (back
= cgi_get("back")) ? back
: volume_url());
314 cgi_body(output
->sink
);
316 cgi_header(output
->sink
, "Content-Type", "text/html");
317 cgi_body(output
->sink
);
318 expand(output
, "volume", ds
);
322 static void act_prefs_errors(const char *msg
,
323 void attribute((unused
)) *u
) {
324 fatal(0, "error splitting parts list: %s", msg
);
327 static const char *numbered_arg(const char *argname
, int numfile
) {
330 byte_xasprintf(&fullname
, "%d_%s", numfile
, argname
);
331 return cgi_get(fullname
);
334 static void process_prefs(dcgi_state
*ds
, int numfile
) {
335 const char *file
, *name
, *value
, *part
, *parts
, *current
, *context
;
338 if(!(file
= numbered_arg("file", numfile
)))
339 /* The first file doesn't need numbering. */
340 if(numfile
> 0 || !(file
= cgi_get("file")))
342 if((parts
= numbered_arg("parts", numfile
))
343 || (parts
= cgi_get("parts"))) {
344 /* Default context is display. Other contexts not actually tested. */
345 if(!(context
= numbered_arg("context", numfile
))) context
= "display";
346 partslist
= split(parts
, 0, 0, act_prefs_errors
, 0);
347 while((part
= *partslist
++)) {
348 if(!(value
= numbered_arg(part
, numfile
)))
350 /* If it's already right (whether regexps or db) don't change anything,
351 * so we don't fill the database up with rubbish. */
352 if(disorder_part(ds
->g
->client
, (char **)¤t
,
353 file
, context
, part
))
354 fatal(0, "disorder_part() failed");
355 if(!strcmp(current
, value
))
357 byte_xasprintf((char **)&name
, "trackname_%s_%s", context
, part
);
358 disorder_set(ds
->g
->client
, file
, name
, value
);
360 if((value
= numbered_arg("random", numfile
)))
361 disorder_unset(ds
->g
->client
, file
, "pick_at_random");
363 disorder_set(ds
->g
->client
, file
, "pick_at_random", "0");
364 if((value
= numbered_arg("tags", numfile
)))
365 disorder_set(ds
->g
->client
, file
, "tags", value
);
366 } else if((name
= cgi_get("name"))) {
367 /* Raw preferences. Not well supported in the templates at the moment. */
368 value
= cgi_get("value");
370 disorder_set(ds
->g
->client
, file
, name
, value
);
372 disorder_unset(ds
->g
->client
, file
, name
);
376 static void act_prefs(cgi_sink
*output
, dcgi_state
*ds
) {
380 if((files
= cgi_get("files"))) nfiles
= atoi(files
);
382 for(numfile
= 0; numfile
< nfiles
; ++numfile
)
383 process_prefs(ds
, numfile
);
384 cgi_header(output
->sink
, "Content-Type", "text/html");
385 cgi_body(output
->sink
);
386 expand(output
, "prefs", ds
);
389 static void act_pause(cgi_sink
*output
,
392 disorder_pause(ds
->g
->client
);
393 redirect(output
->sink
);
396 static void act_resume(cgi_sink
*output
,
399 disorder_resume(ds
->g
->client
);
400 redirect(output
->sink
);
403 static const struct action
{
405 void (*handler
)(cgi_sink
*output
, dcgi_state
*ds
);
407 { "disable", act_disable
},
408 { "enable", act_enable
},
409 { "move", act_move
},
410 { "pause", act_pause
},
411 { "play", act_play
},
412 { "playing", act_playing
},
413 { "prefs", act_prefs
},
414 { "random-disable", act_random_disable
},
415 { "random-enable", act_random_enable
},
416 { "remove", act_remove
},
417 { "resume", act_resume
},
418 { "scratch", act_scratch
},
419 { "volume", act_volume
},
422 /* expansions *****************************************************************/
424 static void exp_include(int attribute((unused
)) nargs
,
428 expand(output
, args
[0], u
);
431 static void exp_server_version(int attribute((unused
)) nargs
,
432 char attribute((unused
)) **args
,
439 if(disorder_version(ds
->g
->client
, (char **)&v
)) v
= "(cannot get version)";
441 v
= "(server not running)";
442 cgi_output(output
, "%s", v
);
445 static void exp_version(int attribute((unused
)) nargs
,
446 char attribute((unused
)) **args
,
448 void attribute((unused
)) *u
) {
449 cgi_output(output
, "%s", disorder_short_version_string
);
452 static void exp_nonce(int attribute((unused
)) nargs
,
453 char attribute((unused
)) **args
,
455 void attribute((unused
)) *u
) {
456 cgi_output(output
, "%s", nonce());
459 static void exp_label(int attribute((unused
)) nargs
,
462 void attribute((unused
)) *u
) {
463 cgi_output(output
, "%s", cgi_label(args
[0]));
466 struct trackinfo_state
{
468 const struct queue_entry
*q
;
473 static void exp_who(int attribute((unused
)) nargs
,
474 char attribute((unused
)) **args
,
479 if(ds
->track
&& ds
->track
->submitter
)
480 cgi_output(output
, "%s", ds
->track
->submitter
);
483 static void exp_length(int attribute((unused
)) nargs
,
484 char attribute((unused
)) **args
,
491 && (ds
->track
->state
== playing_started
492 || ds
->track
->state
== playing_paused
)
493 && ds
->track
->sofar
>= 0)
494 cgi_output(output
, "%ld:%02ld/",
495 ds
->track
->sofar
/ 60, ds
->track
->sofar
% 60);
498 disorder_length(ds
->g
->client
, ds
->track
->track
, &length
);
500 disorder_length(ds
->g
->client
, ds
->tracks
[0], &length
);
502 cgi_output(output
, "%ld:%02ld", length
/ 60, length
% 60);
504 sink_printf(output
->sink
, "%s", " ");
507 static void exp_when(int attribute((unused
)) nargs
,
508 char attribute((unused
)) **args
,
512 const struct tm
*w
= 0;
515 switch(ds
->track
->state
) {
516 case playing_isscratch
:
517 case playing_unplayed
:
519 if(ds
->track
->expected
)
520 w
= localtime(&ds
->track
->expected
);
523 case playing_no_player
:
525 case playing_scratched
:
526 case playing_started
:
528 case playing_quitting
:
529 if(ds
->track
->played
)
530 w
= localtime(&ds
->track
->played
);
534 cgi_output(output
, "%d:%02d", w
->tm_hour
, w
->tm_min
);
536 sink_printf(output
->sink
, " ");
539 static void exp_part(int nargs
,
544 const char *s
, *track
, *part
, *context
;
550 track
= ds
->track
->track
;
552 track
= ds
->tracks
[0];
570 if(disorder_part(ds
->g
->client
, (char **)&s
, track
,
571 !strcmp(context
, "short") ?
"display" : context
, part
))
572 fatal(0, "disorder_part() failed");
573 if(!strcmp(context
, "short"))
574 s
= truncate_for_display(s
, config
->short_display
);
575 cgi_output(output
, "%s", s
);
577 sink_printf(output
->sink
, " ");
580 static void exp_playing(int attribute((unused
)) nargs
,
587 lookups(ds
, DC_PLAYING
);
588 memset(&s
, 0, sizeof s
);
591 s
.track
= ds
->g
->playing
;
592 expandstring(output
, args
[0], &s
);
596 static void exp_queue(int attribute((unused
)) nargs
,
602 struct queue_entry
*q
;
604 lookups(ds
, DC_QUEUE
);
605 memset(&s
, 0, sizeof s
);
608 for(q
= ds
->g
->queue
; q
; q
= q
->next
) {
611 expandstring(output
, args
[0], &s
);
617 static void exp_recent(int attribute((unused
)) nargs
,
623 struct queue_entry
*q
;
625 lookups(ds
, DC_RECENT
);
626 memset(&s
, 0, sizeof s
);
629 for(q
= ds
->g
->recent
; q
; q
= q
->next
) {
632 expandstring(output
, args
[0], &s
);
638 static void exp_new(int attribute((unused
)) nargs
,
646 memset(&s
, 0, sizeof s
);
649 for(s
.index
= 0; s
.index
< ds
->g
->nnew
; ++s
.index
) {
650 s
.last
= s
.index
+ 1 < ds
->g
->nnew
;
651 s
.tracks
= &ds
->g
->new[s
.index
];
652 expandstring(output
, args
[0], &s
);
657 static void exp_url(int attribute((unused
)) nargs
,
658 char attribute((unused
)) **args
,
660 void attribute((unused
)) *u
) {
661 cgi_output(output
, "%s", config
->url
);
669 static int compare_result(const void *a
, const void *b
) {
670 const struct result
*ra
= a
, *rb
= b
;
673 if(!(c
= strcmp(ra
->sort
, rb
->sort
)))
674 c
= strcmp(ra
->track
, rb
->track
);
678 static void exp_search(int nargs
,
682 dcgi_state
*ds
= u
, substate
;
684 const char *q
, *context
, *part
, *template;
700 assert(!"should never happen");
701 part
= context
= template = 0; /* quieten compiler */
703 if(ds
->tracks
== 0) {
704 /* we are the top level, let's get some search results */
705 if(!(q
= cgi_get("query"))) return; /* no results yet */
706 if(disorder_search(ds
->g
->client
, q
, &tracks
, &ntracks
)) return;
710 ntracks
= ds
->ntracks
;
712 assert(ntracks
!= 0);
713 /* sort tracks by the appropriate part */
714 r
= xmalloc(ntracks
* sizeof *r
);
715 for(n
= 0; n
< ntracks
; ++n
) {
716 r
[n
].track
= tracks
[n
];
717 if(disorder_part(ds
->g
->client
, (char **)&r
[n
].sort
,
718 tracks
[n
], context
, part
))
719 fatal(0, "disorder_part() failed");
721 qsort(r
, ntracks
, sizeof (struct result
), compare_result
);
722 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
723 * array as we know it's guaranteed to be big enough and isn't going to be
724 * used for anything else any more. */
725 memset(&substate
, 0, sizeof substate
);
730 substate
.tracks
= tracks
;
731 substate
.ntracks
= 0;
734 && !strcmp(r
[m
].sort
, r
[n
].sort
))
735 tracks
[substate
.ntracks
++] = r
[m
++].track
;
736 substate
.last
= (m
== ntracks
);
737 expandstring(output
, template, &substate
);
742 assert(substate
.last
!= 0);
745 static void exp_arg(int attribute((unused
)) nargs
,
748 void attribute((unused
)) *u
) {
751 if((v
= cgi_get(args
[0])))
752 cgi_output(output
, "%s", v
);
755 static void exp_stats(int attribute((unused
)) nargs
,
756 char attribute((unused
)) **args
,
762 cgi_opentag(output
->sink
, "pre", "class", "stats", (char *)0);
763 if(!disorder_stats(ds
->g
->client
, &v
, 0)) {
765 cgi_output(output
, "%s\n", *v
++);
767 cgi_closetag(output
->sink
, "pre");
770 static void exp_volume(int attribute((unused
)) nargs
,
776 lookups(ds
, DC_VOLUME
);
777 if(!strcmp(args
[0], "left"))
778 cgi_output(output
, "%d", ds
->g
->volume_left
);
780 cgi_output(output
, "%d", ds
->g
->volume_right
);
783 static void exp_shell(int attribute((unused
)) nargs
,
786 void attribute((unused
)) *u
) {
792 if(!(pid
= xfork())) {
797 execlp("sh", "sh", "-c", args
[0], (char *)0);
798 fatal(errno
, "error executing sh");
801 while((n
= read(p
[0], buffer
, sizeof buffer
))) {
803 if(errno
== EINTR
) continue;
804 else fatal(errno
, "error reading from pipe");
806 output
->sink
->write(output
->sink
, buffer
, n
);
809 while((n
= waitpid(pid
, &w
, 0)) < 0 && errno
== EINTR
)
811 if(n
< 0) fatal(errno
, "error calling waitpid");
813 error(0, "shell command '%s' %s", args
[0], wstat(w
));
816 static inline int str2bool(const char *s
) {
817 return !strcmp(s
, "true");
820 static inline const char *bool2str(int n
) {
821 return n ?
"true" : "false";
824 static char *expandarg(const char *arg
, dcgi_state
*ds
) {
830 output
.sink
= sink_dynstr(&d
);
831 expandstring(&output
, arg
, ds
);
832 dynstr_terminate(&d
);
836 static void exp_prefs(int attribute((unused
)) nargs
,
843 const char *file
= expandarg(args
[0], ds
);
845 memset(&substate
, 0, sizeof substate
);
848 if(disorder_prefs(ds
->g
->client
, file
, &k
)) return;
850 substate
.last
= !k
->next
;
852 expandstring(output
, args
[1], &substate
);
859 static void exp_pref(int attribute((unused
)) nargs
,
866 if(!disorder_get(ds
->g
->client
, args
[0], args
[1], &value
))
867 cgi_output(output
, "%s", value
);
870 static void exp_if(int nargs
,
875 int n
= str2bool(expandarg(args
[0], ds
)) ?
1 : 2;
878 expandstring(output
, args
[n
], ds
);
881 static void exp_and(int nargs
,
888 for(n
= 0; n
< nargs
; ++n
)
889 if(!str2bool(expandarg(args
[n
], ds
))) {
893 sink_printf(output
->sink
, "%s", bool2str(result
));
896 static void exp_or(int nargs
,
903 for(n
= 0; n
< nargs
; ++n
)
904 if(str2bool(expandarg(args
[n
], ds
))) {
908 sink_printf(output
->sink
, "%s", bool2str(result
));
911 static void exp_not(int attribute((unused
)) nargs
,
914 void attribute((unused
)) *u
) {
915 sink_printf(output
->sink
, "%s", bool2str(!str2bool(args
[0])));
918 static void exp_isplaying(int attribute((unused
)) nargs
,
919 char attribute((unused
)) **args
,
924 lookups(ds
, DC_PLAYING
);
925 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->playing
));
928 static void exp_isqueue(int attribute((unused
)) nargs
,
929 char attribute((unused
)) **args
,
934 lookups(ds
, DC_QUEUE
);
935 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->queue
));
938 static void exp_isrecent(int attribute((unused
)) nargs
,
939 char attribute((unused
)) **args
,
944 lookups(ds
, DC_RECENT
);
945 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->recent
));
948 static void exp_isnew(int attribute((unused
)) nargs
,
949 char attribute((unused
)) **args
,
955 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->nnew
));
958 static void exp_id(int attribute((unused
)) nargs
,
959 char attribute((unused
)) **args
,
965 cgi_output(output
, "%s", ds
->track
->id
);
968 static void exp_track(int attribute((unused
)) nargs
,
969 char attribute((unused
)) **args
,
975 cgi_output(output
, "%s", ds
->track
->track
);
978 static void exp_parity(int attribute((unused
)) nargs
,
979 char attribute((unused
)) **args
,
984 cgi_output(output
, "%s", ds
->index
% 2 ?
"odd" : "even");
987 static void exp_comment(int attribute((unused
)) nargs
,
988 char attribute((unused
)) **args
,
989 cgi_sink
attribute((unused
)) *output
,
990 void attribute((unused
)) *u
) {
994 static void exp_prefname(int attribute((unused
)) nargs
,
995 char attribute((unused
)) **args
,
1000 if(ds
->pref
&& ds
->pref
->name
)
1001 cgi_output(output
, "%s", ds
->pref
->name
);
1004 static void exp_prefvalue(int attribute((unused
)) nargs
,
1005 char attribute((unused
)) **args
,
1010 if(ds
->pref
&& ds
->pref
->value
)
1011 cgi_output(output
, "%s", ds
->pref
->value
);
1014 static void exp_isfiles(int attribute((unused
)) nargs
,
1015 char attribute((unused
)) **args
,
1020 lookups(ds
, DC_FILES
);
1021 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->nfiles
));
1024 static void exp_isdirectories(int attribute((unused
)) nargs
,
1025 char attribute((unused
)) **args
,
1030 lookups(ds
, DC_DIRS
);
1031 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->ndirs
));
1034 static void exp_choose(int attribute((unused
)) nargs
,
1039 dcgi_state substate
;
1043 const char *type
, *what
= expandarg(args
[0], ds
);
1045 if(!strcmp(what
, "files")) {
1046 lookups(ds
, DC_FILES
);
1047 files
= ds
->g
->files
;
1048 nfiles
= ds
->g
->nfiles
;
1050 } else if(!strcmp(what
, "directories")) {
1051 lookups(ds
, DC_DIRS
);
1052 files
= ds
->g
->dirs
;
1053 nfiles
= ds
->g
->ndirs
;
1056 error(0, "unknown @choose@ argument '%s'", what
);
1059 e
= xmalloc(nfiles
* sizeof (struct entry
));
1060 for(n
= 0; n
< nfiles
; ++n
) {
1061 e
[n
].path
= files
[n
];
1062 e
[n
].sort
= trackname_transform(type
, files
[n
], "sort");
1063 e
[n
].display
= trackname_transform(type
, files
[n
], "display");
1065 qsort(e
, nfiles
, sizeof (struct entry
), compare_entry
);
1066 memset(&substate
, 0, sizeof substate
);
1069 for(n
= 0; n
< nfiles
; ++n
) {
1070 substate
.last
= (n
== nfiles
- 1);
1072 substate
.entry
= &e
[n
];
1073 expandstring(output
, args
[1], &substate
);
1078 static void exp_file(int attribute((unused
)) nargs
,
1079 char attribute((unused
)) **args
,
1085 cgi_output(output
, "%s", ds
->entry
->path
);
1087 cgi_output(output
, "%s", ds
->track
->track
);
1089 cgi_output(output
, "%s", ds
->tracks
[0]);
1092 static void exp_transform(int nargs
,
1095 void attribute((unused
)) *u
) {
1096 const char *context
= nargs
> 2 ? args
[2] : "display";
1098 cgi_output(output
, "%s", trackname_transform(args
[1], args
[0], context
));
1101 static void exp_urlquote(int attribute((unused
)) nargs
,
1104 void attribute((unused
)) *u
) {
1105 cgi_output(output
, "%s", urlencodestring(args
[0]));
1108 static void exp_scratchable(int attribute((unused
)) nargs
,
1109 char attribute((unused
)) **args
,
1111 void attribute((unused
)) *u
) {
1115 if(config
->restrictions
& RESTRICT_SCRATCH
) {
1116 lookups(ds
, DC_PLAYING
);
1117 result
= (ds
->g
->playing
1118 && (!ds
->g
->playing
->submitter
1119 || !strcmp(ds
->g
->playing
->submitter
,
1120 disorder_user(ds
->g
->client
))));
1123 sink_printf(output
->sink
, "%s", bool2str(result
));
1126 static void exp_removable(int attribute((unused
)) nargs
,
1127 char attribute((unused
)) **args
,
1129 void attribute((unused
)) *u
) {
1133 if(config
->restrictions
& RESTRICT_REMOVE
)
1135 && ds
->track
->submitter
1136 && !strcmp(ds
->track
->submitter
,
1137 disorder_user(ds
->g
->client
)));
1140 sink_printf(output
->sink
, "%s", bool2str(result
));
1143 static void exp_navigate(int attribute((unused
)) nargs
,
1148 dcgi_state substate
;
1149 const char *path
= expandarg(args
[0], ds
);
1154 memset(&substate
, 0, sizeof substate
);
1156 ptr
= path
+ 1; /* skip root */
1158 substate
.nav_path
= path
;
1161 while(*ptr
&& *ptr
!= '/')
1163 substate
.last
= !*ptr
;
1164 substate
.nav_len
= ptr
- path
;
1165 substate
.nav_dirlen
= dirlen
;
1166 expandstring(output
, args
[1], &substate
);
1167 dirlen
= substate
.nav_len
;
1174 static void exp_fullname(int attribute((unused
)) nargs
,
1175 char attribute((unused
)) **args
,
1179 cgi_output(output
, "%.*s", ds
->nav_len
, ds
->nav_path
);
1182 static void exp_basename(int nargs
,
1190 if((s
= strrchr(args
[0], '/'))) ++s
;
1192 cgi_output(output
, "%s", s
);
1194 cgi_output(output
, "%.*s", ds
->nav_len
- ds
->nav_dirlen
- 1,
1195 ds
->nav_path
+ ds
->nav_dirlen
+ 1);
1198 static void exp_dirname(int nargs
,
1206 if((s
= strrchr(args
[0], '/')))
1207 cgi_output(output
, "%.*s", (int)(s
- args
[0]), args
[0]);
1209 cgi_output(output
, "%.*s", ds
->nav_dirlen
, ds
->nav_path
);
1212 static void exp_eq(int attribute((unused
)) nargs
,
1215 void attribute((unused
)) *u
) {
1216 cgi_output(output
, "%s", bool2str(!strcmp(args
[0], args
[1])));
1219 static void exp_ne(int attribute((unused
)) nargs
,
1222 void attribute((unused
)) *u
) {
1223 cgi_output(output
, "%s", bool2str(strcmp(args
[0], args
[1])));
1226 static void exp_enabled(int attribute((unused
)) nargs
,
1227 char attribute((unused
)) **args
,
1234 disorder_enabled(ds
->g
->client
, &enabled
);
1235 cgi_output(output
, "%s", bool2str(enabled
));
1238 static void exp_random_enabled(int attribute((unused
)) nargs
,
1239 char attribute((unused
)) **args
,
1246 disorder_random_enabled(ds
->g
->client
, &enabled
);
1247 cgi_output(output
, "%s", bool2str(enabled
));
1250 static void exp_trackstate(int attribute((unused
)) nargs
,
1255 struct queue_entry
*q
;
1258 if(disorder_resolve(ds
->g
->client
, &track
, args
[0])) return;
1259 lookups(ds
, DC_QUEUE
|DC_PLAYING
);
1260 if(ds
->g
->playing
&& !strcmp(ds
->g
->playing
->track
, track
))
1261 cgi_output(output
, "playing");
1263 for(q
= ds
->g
->queue
; q
&& strcmp(q
->track
, track
); q
= q
->next
)
1266 cgi_output(output
, "queued");
1270 static void exp_thisurl(int attribute((unused
)) nargs
,
1271 char attribute((unused
)) **args
,
1273 void attribute((unused
)) *u
) {
1274 kvp_set(&cgi_args
, "nonce", nonce()); /* nonces had better differ! */
1275 cgi_output(output
, "%s?%s", config
->url
, kvp_urlencode(cgi_args
, 0));
1278 static void exp_isfirst(int attribute((unused
)) nargs
,
1279 char attribute((unused
)) **args
,
1284 sink_printf(output
->sink
, "%s", bool2str(!!ds
->first
));
1287 static void exp_islast(int attribute((unused
)) nargs
,
1288 char attribute((unused
)) **args
,
1293 sink_printf(output
->sink
, "%s", bool2str(!!ds
->last
));
1296 static void exp_action(int attribute((unused
)) nargs
,
1297 char attribute((unused
)) **args
,
1299 void attribute((unused
)) *u
) {
1300 const char *action
= cgi_get("action"), *mgmt
;
1302 if(!action
) action
= "playing";
1303 if(!strcmp(action
, "playing")
1304 && (mgmt
= cgi_get("mgmt"))
1305 && !strcmp(mgmt
, "true"))
1307 sink_printf(output
->sink
, "%s", action
);
1310 static void exp_resolve(int attribute((unused
)) nargs
,
1313 void attribute((unused
)) *u
) {
1317 if(!disorder_resolve(ds
->g
->client
, &track
, args
[0]))
1318 sink_printf(output
->sink
, "%s", track
);
1321 static void exp_paused(int attribute((unused
)) nargs
,
1322 char attribute((unused
)) **args
,
1328 lookups(ds
, DC_PLAYING
);
1329 if(ds
->g
->playing
&& ds
->g
->playing
->state
== playing_paused
)
1331 cgi_output(output
, "%s", bool2str(paused
));
1334 static void exp_state(int attribute((unused
)) nargs
,
1335 char attribute((unused
)) **args
,
1341 cgi_output(output
, "%s", playing_states
[ds
->track
->state
]);
1344 static void exp_files(int attribute((unused
)) nargs
,
1349 dcgi_state substate
;
1350 const char *nfiles_arg
, *directory
;
1351 int nfiles
, numfile
;
1354 memset(&substate
, 0, sizeof substate
);
1356 if((directory
= cgi_get("directory"))) {
1357 /* Prefs for whole directory. */
1358 lookups(ds
, DC_FILES
);
1359 /* Synthesize args for the file list. */
1360 nfiles
= ds
->g
->nfiles
;
1361 for(numfile
= 0; numfile
< nfiles
; ++numfile
) {
1362 k
= xmalloc(sizeof *k
);
1363 byte_xasprintf((char **)&k
->name
, "%d_file", numfile
);
1364 k
->value
= ds
->g
->files
[numfile
];
1369 /* Args already present. */
1370 if((nfiles_arg
= cgi_get("files"))) nfiles
= atoi(nfiles_arg
);
1373 for(numfile
= 0; numfile
< nfiles
; ++numfile
) {
1374 substate
.index
= numfile
;
1375 expandstring(output
, args
[0], &substate
);
1379 static void exp_index(int attribute((unused
)) nargs
,
1380 char attribute((unused
)) **args
,
1385 cgi_output(output
, "%d", ds
->index
);
1388 static void exp_nfiles(int attribute((unused
)) nargs
,
1389 char attribute((unused
)) **args
,
1393 const char *files_arg
;
1395 if(cgi_get("directory")) {
1396 lookups(ds
, DC_FILES
);
1397 cgi_output(output
, "%d", ds
->g
->nfiles
);
1398 } else if((files_arg
= cgi_get("files")))
1399 cgi_output(output
, "%s", files_arg
);
1401 cgi_output(output
, "1");
1404 static const struct cgi_expansion expansions
[] = {
1405 { "#", 0, INT_MAX
, EXP_MAGIC
, exp_comment
},
1406 { "action", 0, 0, 0, exp_action
},
1407 { "and", 0, INT_MAX
, EXP_MAGIC
, exp_and
},
1408 { "arg", 1, 1, 0, exp_arg
},
1409 { "basename", 0, 1, 0, exp_basename
},
1410 { "choose", 2, 2, EXP_MAGIC
, exp_choose
},
1411 { "dirname", 0, 1, 0, exp_dirname
},
1412 { "enabled", 0, 0, 0, exp_enabled
},
1413 { "eq", 2, 2, 0, exp_eq
},
1414 { "file", 0, 0, 0, exp_file
},
1415 { "files", 1, 1, EXP_MAGIC
, exp_files
},
1416 { "fullname", 0, 0, 0, exp_fullname
},
1417 { "id", 0, 0, 0, exp_id
},
1418 { "if", 2, 3, EXP_MAGIC
, exp_if
},
1419 { "include", 1, 1, 0, exp_include
},
1420 { "index", 0, 0, 0, exp_index
},
1421 { "isdirectories", 0, 0, 0, exp_isdirectories
},
1422 { "isfiles", 0, 0, 0, exp_isfiles
},
1423 { "isfirst", 0, 0, 0, exp_isfirst
},
1424 { "islast", 0, 0, 0, exp_islast
},
1425 { "isnew", 0, 0, 0, exp_isnew
},
1426 { "isplaying", 0, 0, 0, exp_isplaying
},
1427 { "isqueue", 0, 0, 0, exp_isqueue
},
1428 { "isrecent", 0, 0, 0, exp_isrecent
},
1429 { "label", 1, 1, 0, exp_label
},
1430 { "length", 0, 0, 0, exp_length
},
1431 { "navigate", 2, 2, EXP_MAGIC
, exp_navigate
},
1432 { "ne", 2, 2, 0, exp_ne
},
1433 { "new", 1, 1, EXP_MAGIC
, exp_new
},
1434 { "nfiles", 0, 0, 0, exp_nfiles
},
1435 { "nonce", 0, 0, 0, exp_nonce
},
1436 { "not", 1, 1, 0, exp_not
},
1437 { "or", 0, INT_MAX
, EXP_MAGIC
, exp_or
},
1438 { "parity", 0, 0, 0, exp_parity
},
1439 { "part", 1, 3, 0, exp_part
},
1440 { "paused", 0, 0, 0, exp_paused
},
1441 { "playing", 1, 1, EXP_MAGIC
, exp_playing
},
1442 { "pref", 2, 2, 0, exp_pref
},
1443 { "prefname", 0, 0, 0, exp_prefname
},
1444 { "prefs", 2, 2, EXP_MAGIC
, exp_prefs
},
1445 { "prefvalue", 0, 0, 0, exp_prefvalue
},
1446 { "queue", 1, 1, EXP_MAGIC
, exp_queue
},
1447 { "random-enabled", 0, 0, 0, exp_random_enabled
},
1448 { "recent", 1, 1, EXP_MAGIC
, exp_recent
},
1449 { "removable", 0, 0, 0, exp_removable
},
1450 { "resolve", 1, 1, 0, exp_resolve
},
1451 { "scratchable", 0, 0, 0, exp_scratchable
},
1452 { "search", 2, 3, EXP_MAGIC
, exp_search
},
1453 { "server-version", 0, 0, 0, exp_server_version
},
1454 { "shell", 1, 1, 0, exp_shell
},
1455 { "state", 0, 0, 0, exp_state
},
1456 { "stats", 0, 0, 0, exp_stats
},
1457 { "thisurl", 0, 0, 0, exp_thisurl
},
1458 { "track", 0, 0, 0, exp_track
},
1459 { "trackstate", 1, 1, 0, exp_trackstate
},
1460 { "transform", 2, 3, 0, exp_transform
},
1461 { "url", 0, 0, 0, exp_url
},
1462 { "urlquote", 1, 1, 0, exp_urlquote
},
1463 { "version", 0, 0, 0, exp_version
},
1464 { "volume", 1, 1, 0, exp_volume
},
1465 { "when", 0, 0, 0, exp_when
},
1466 { "who", 0, 0, 0, exp_who
}
1469 static void expand(cgi_sink
*output
,
1470 const char *template,
1472 cgi_expand(template,
1473 expansions
, sizeof expansions
/ sizeof *expansions
,
1478 static void expandstring(cgi_sink
*output
,
1481 cgi_expand_string("",
1483 expansions
, sizeof expansions
/ sizeof *expansions
,
1488 static void perform_action(cgi_sink
*output
, dcgi_state
*ds
,
1489 const char *action
) {
1492 if((n
= TABLE_FIND(actions
, struct action
, name
, action
)) >= 0)
1493 actions
[n
].handler(output
, ds
);
1495 cgi_header(output
->sink
, "Content-Type", "text/html");
1496 cgi_body(output
->sink
);
1497 expand(output
, action
, ds
);
1501 void disorder_cgi(cgi_sink
*output
, dcgi_state
*ds
) {
1502 const char *action
= cgi_get("action");
1504 if(!action
) action
= "playing";
1505 perform_action(output
, ds
, action
);
1508 void disorder_cgi_error(cgi_sink
*output
, dcgi_state
*ds
,
1510 cgi_set_option("error", msg
);
1511 perform_action(output
, ds
, "error");