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"
56 #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_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
, context
, part
))
571 fatal(0, "disorder_part() failed");
572 cgi_output(output
, "%s", s
);
574 sink_printf(output
->sink
, " ");
577 static void exp_playing(int attribute((unused
)) nargs
,
584 lookups(ds
, DC_PLAYING
);
585 memset(&s
, 0, sizeof s
);
588 s
.track
= ds
->g
->playing
;
589 expandstring(output
, args
[0], &s
);
593 static void exp_queue(int attribute((unused
)) nargs
,
599 struct queue_entry
*q
;
601 lookups(ds
, DC_QUEUE
);
602 memset(&s
, 0, sizeof s
);
605 for(q
= ds
->g
->queue
; q
; q
= q
->next
) {
608 expandstring(output
, args
[0], &s
);
614 static void exp_recent(int attribute((unused
)) nargs
,
620 struct queue_entry
*q
;
622 lookups(ds
, DC_RECENT
);
623 memset(&s
, 0, sizeof s
);
626 for(q
= ds
->g
->recent
; q
; q
= q
->next
) {
629 expandstring(output
, args
[0], &s
);
635 static void exp_new(int attribute((unused
)) nargs
,
643 memset(&s
, 0, sizeof s
);
646 for(s
.index
= 0; s
.index
< ds
->g
->nnew
; ++s
.index
) {
647 s
.last
= s
.index
+ 1 < ds
->g
->nnew
;
648 s
.tracks
= &ds
->g
->new[s
.index
];
649 expandstring(output
, args
[0], &s
);
654 static void exp_url(int attribute((unused
)) nargs
,
655 char attribute((unused
)) **args
,
657 void attribute((unused
)) *u
) {
658 cgi_output(output
, "%s", config
->url
);
666 static int compare_result(const void *a
, const void *b
) {
667 const struct result
*ra
= a
, *rb
= b
;
670 if(!(c
= strcmp(ra
->sort
, rb
->sort
)))
671 c
= strcmp(ra
->track
, rb
->track
);
675 static void exp_search(int nargs
,
679 dcgi_state
*ds
= u
, substate
;
681 const char *q
, *context
, *part
, *template;
697 assert(!"should never happen");
698 part
= context
= template = 0; /* quieten compiler */
700 if(ds
->tracks
== 0) {
701 /* we are the top level, let's get some search results */
702 if(!(q
= cgi_get("query"))) return; /* no results yet */
703 if(disorder_search(ds
->g
->client
, q
, &tracks
, &ntracks
)) return;
707 ntracks
= ds
->ntracks
;
709 assert(ntracks
!= 0);
710 /* sort tracks by the appropriate part */
711 r
= xmalloc(ntracks
* sizeof *r
);
712 for(n
= 0; n
< ntracks
; ++n
) {
713 r
[n
].track
= tracks
[n
];
714 if(disorder_part(ds
->g
->client
, (char **)&r
[n
].sort
,
715 tracks
[n
], context
, part
))
716 fatal(0, "disorder_part() failed");
718 qsort(r
, ntracks
, sizeof (struct result
), compare_result
);
719 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
720 * array as we know it's guaranteed to be big enough and isn't going to be
721 * used for anything else any more. */
722 memset(&substate
, 0, sizeof substate
);
727 substate
.tracks
= tracks
;
728 substate
.ntracks
= 0;
731 && !strcmp(r
[m
].sort
, r
[n
].sort
))
732 tracks
[substate
.ntracks
++] = r
[m
++].track
;
733 substate
.last
= (m
== ntracks
);
734 expandstring(output
, template, &substate
);
739 assert(substate
.last
!= 0);
742 static void exp_arg(int attribute((unused
)) nargs
,
745 void attribute((unused
)) *u
) {
748 if((v
= cgi_get(args
[0])))
749 cgi_output(output
, "%s", v
);
752 static void exp_stats(int attribute((unused
)) nargs
,
753 char attribute((unused
)) **args
,
759 cgi_opentag(output
->sink
, "pre", "class", "stats", (char *)0);
760 if(!disorder_stats(ds
->g
->client
, &v
, 0)) {
762 cgi_output(output
, "%s\n", *v
++);
764 cgi_closetag(output
->sink
, "pre");
767 static void exp_volume(int attribute((unused
)) nargs
,
773 lookups(ds
, DC_VOLUME
);
774 if(!strcmp(args
[0], "left"))
775 cgi_output(output
, "%d", ds
->g
->volume_left
);
777 cgi_output(output
, "%d", ds
->g
->volume_right
);
780 static void exp_shell(int attribute((unused
)) nargs
,
783 void attribute((unused
)) *u
) {
789 if(!(pid
= xfork())) {
794 execlp("sh", "sh", "-c", args
[0], (char *)0);
795 fatal(errno
, "error executing sh");
798 while((n
= read(p
[0], buffer
, sizeof buffer
))) {
800 if(errno
== EINTR
) continue;
801 else fatal(errno
, "error reading from pipe");
803 output
->sink
->write(output
->sink
, buffer
, n
);
806 while((n
= waitpid(pid
, &w
, 0)) < 0 && errno
== EINTR
)
808 if(n
< 0) fatal(errno
, "error calling waitpid");
810 error(0, "shell command '%s' %s", args
[0], wstat(w
));
813 static inline int str2bool(const char *s
) {
814 return !strcmp(s
, "true");
817 static inline const char *bool2str(int n
) {
818 return n ?
"true" : "false";
821 static char *expandarg(const char *arg
, dcgi_state
*ds
) {
827 output
.sink
= sink_dynstr(&d
);
828 expandstring(&output
, arg
, ds
);
829 dynstr_terminate(&d
);
833 static void exp_prefs(int attribute((unused
)) nargs
,
840 const char *file
= expandarg(args
[0], ds
);
842 memset(&substate
, 0, sizeof substate
);
845 if(disorder_prefs(ds
->g
->client
, file
, &k
)) return;
847 substate
.last
= !k
->next
;
849 expandstring(output
, args
[1], &substate
);
856 static void exp_pref(int attribute((unused
)) nargs
,
863 if(!disorder_get(ds
->g
->client
, args
[0], args
[1], &value
))
864 cgi_output(output
, "%s", value
);
867 static void exp_if(int nargs
,
872 int n
= str2bool(expandarg(args
[0], ds
)) ?
1 : 2;
875 expandstring(output
, args
[n
], ds
);
878 static void exp_and(int nargs
,
885 for(n
= 0; n
< nargs
; ++n
)
886 if(!str2bool(expandarg(args
[n
], ds
))) {
890 sink_printf(output
->sink
, "%s", bool2str(result
));
893 static void exp_or(int nargs
,
900 for(n
= 0; n
< nargs
; ++n
)
901 if(str2bool(expandarg(args
[n
], ds
))) {
905 sink_printf(output
->sink
, "%s", bool2str(result
));
908 static void exp_not(int attribute((unused
)) nargs
,
911 void attribute((unused
)) *u
) {
912 sink_printf(output
->sink
, "%s", bool2str(!str2bool(args
[0])));
915 static void exp_isplaying(int attribute((unused
)) nargs
,
916 char attribute((unused
)) **args
,
921 lookups(ds
, DC_PLAYING
);
922 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->playing
));
925 static void exp_isqueue(int attribute((unused
)) nargs
,
926 char attribute((unused
)) **args
,
931 lookups(ds
, DC_QUEUE
);
932 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->queue
));
935 static void exp_isrecent(int attribute((unused
)) nargs
,
936 char attribute((unused
)) **args
,
941 lookups(ds
, DC_RECENT
);
942 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->recent
));
945 static void exp_isnew(int attribute((unused
)) nargs
,
946 char attribute((unused
)) **args
,
952 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->nnew
));
955 static void exp_id(int attribute((unused
)) nargs
,
956 char attribute((unused
)) **args
,
962 cgi_output(output
, "%s", ds
->track
->id
);
965 static void exp_track(int attribute((unused
)) nargs
,
966 char attribute((unused
)) **args
,
972 cgi_output(output
, "%s", ds
->track
->track
);
975 static void exp_parity(int attribute((unused
)) nargs
,
976 char attribute((unused
)) **args
,
981 cgi_output(output
, "%s", ds
->index
% 2 ?
"odd" : "even");
984 static void exp_comment(int attribute((unused
)) nargs
,
985 char attribute((unused
)) **args
,
986 cgi_sink
attribute((unused
)) *output
,
987 void attribute((unused
)) *u
) {
991 static void exp_prefname(int attribute((unused
)) nargs
,
992 char attribute((unused
)) **args
,
997 if(ds
->pref
&& ds
->pref
->name
)
998 cgi_output(output
, "%s", ds
->pref
->name
);
1001 static void exp_prefvalue(int attribute((unused
)) nargs
,
1002 char attribute((unused
)) **args
,
1007 if(ds
->pref
&& ds
->pref
->value
)
1008 cgi_output(output
, "%s", ds
->pref
->value
);
1011 static void exp_isfiles(int attribute((unused
)) nargs
,
1012 char attribute((unused
)) **args
,
1017 lookups(ds
, DC_FILES
);
1018 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->nfiles
));
1021 static void exp_isdirectories(int attribute((unused
)) nargs
,
1022 char attribute((unused
)) **args
,
1027 lookups(ds
, DC_DIRS
);
1028 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->ndirs
));
1031 static void exp_choose(int attribute((unused
)) nargs
,
1036 dcgi_state substate
;
1040 const char *type
, *what
= expandarg(args
[0], ds
);
1042 if(!strcmp(what
, "files")) {
1043 lookups(ds
, DC_FILES
);
1044 files
= ds
->g
->files
;
1045 nfiles
= ds
->g
->nfiles
;
1047 } else if(!strcmp(what
, "directories")) {
1048 lookups(ds
, DC_DIRS
);
1049 files
= ds
->g
->dirs
;
1050 nfiles
= ds
->g
->ndirs
;
1053 error(0, "unknown @choose@ argument '%s'", what
);
1056 e
= xmalloc(nfiles
* sizeof (struct entry
));
1057 for(n
= 0; n
< nfiles
; ++n
) {
1058 e
[n
].path
= files
[n
];
1059 e
[n
].sort
= trackname_transform(type
, files
[n
], "sort");
1060 e
[n
].display
= trackname_transform(type
, files
[n
], "display");
1062 qsort(e
, nfiles
, sizeof (struct entry
), compare_entry
);
1063 memset(&substate
, 0, sizeof substate
);
1066 for(n
= 0; n
< nfiles
; ++n
) {
1067 substate
.last
= (n
== nfiles
- 1);
1069 substate
.entry
= &e
[n
];
1070 expandstring(output
, args
[1], &substate
);
1075 static void exp_file(int attribute((unused
)) nargs
,
1076 char attribute((unused
)) **args
,
1082 cgi_output(output
, "%s", ds
->entry
->path
);
1084 cgi_output(output
, "%s", ds
->track
->track
);
1086 cgi_output(output
, "%s", ds
->tracks
[0]);
1089 static void exp_transform(int nargs
,
1092 void attribute((unused
)) *u
) {
1093 const char *context
= nargs
> 2 ? args
[2] : "display";
1095 cgi_output(output
, "%s", trackname_transform(args
[1], args
[0], context
));
1098 static void exp_urlquote(int attribute((unused
)) nargs
,
1101 void attribute((unused
)) *u
) {
1102 cgi_output(output
, "%s", urlencodestring(args
[0]));
1105 static void exp_scratchable(int attribute((unused
)) nargs
,
1106 char attribute((unused
)) **args
,
1108 void attribute((unused
)) *u
) {
1112 if(config
->restrictions
& RESTRICT_SCRATCH
) {
1113 lookups(ds
, DC_PLAYING
);
1114 result
= (ds
->g
->playing
1115 && (!ds
->g
->playing
->submitter
1116 || !strcmp(ds
->g
->playing
->submitter
,
1117 disorder_user(ds
->g
->client
))));
1120 sink_printf(output
->sink
, "%s", bool2str(result
));
1123 static void exp_removable(int attribute((unused
)) nargs
,
1124 char attribute((unused
)) **args
,
1126 void attribute((unused
)) *u
) {
1130 if(config
->restrictions
& RESTRICT_REMOVE
)
1132 && ds
->track
->submitter
1133 && !strcmp(ds
->track
->submitter
,
1134 disorder_user(ds
->g
->client
)));
1137 sink_printf(output
->sink
, "%s", bool2str(result
));
1140 static void exp_navigate(int attribute((unused
)) nargs
,
1145 dcgi_state substate
;
1146 const char *path
= expandarg(args
[0], ds
);
1151 memset(&substate
, 0, sizeof substate
);
1153 ptr
= path
+ 1; /* skip root */
1155 substate
.nav_path
= path
;
1158 while(*ptr
&& *ptr
!= '/')
1160 substate
.last
= !*ptr
;
1161 substate
.nav_len
= ptr
- path
;
1162 substate
.nav_dirlen
= dirlen
;
1163 expandstring(output
, args
[1], &substate
);
1164 dirlen
= substate
.nav_len
;
1171 static void exp_fullname(int attribute((unused
)) nargs
,
1172 char attribute((unused
)) **args
,
1176 cgi_output(output
, "%.*s", ds
->nav_len
, ds
->nav_path
);
1179 static void exp_basename(int nargs
,
1187 if((s
= strrchr(args
[0], '/'))) ++s
;
1189 cgi_output(output
, "%s", s
);
1191 cgi_output(output
, "%.*s", ds
->nav_len
- ds
->nav_dirlen
- 1,
1192 ds
->nav_path
+ ds
->nav_dirlen
+ 1);
1195 static void exp_dirname(int nargs
,
1203 if((s
= strrchr(args
[0], '/')))
1204 cgi_output(output
, "%.*s", (int)(s
- args
[0]), args
[0]);
1206 cgi_output(output
, "%.*s", ds
->nav_dirlen
, ds
->nav_path
);
1209 static void exp_eq(int attribute((unused
)) nargs
,
1212 void attribute((unused
)) *u
) {
1213 cgi_output(output
, "%s", bool2str(!strcmp(args
[0], args
[1])));
1216 static void exp_ne(int attribute((unused
)) nargs
,
1219 void attribute((unused
)) *u
) {
1220 cgi_output(output
, "%s", bool2str(strcmp(args
[0], args
[1])));
1223 static void exp_enabled(int attribute((unused
)) nargs
,
1224 char attribute((unused
)) **args
,
1231 disorder_enabled(ds
->g
->client
, &enabled
);
1232 cgi_output(output
, "%s", bool2str(enabled
));
1235 static void exp_random_enabled(int attribute((unused
)) nargs
,
1236 char attribute((unused
)) **args
,
1243 disorder_random_enabled(ds
->g
->client
, &enabled
);
1244 cgi_output(output
, "%s", bool2str(enabled
));
1247 static void exp_trackstate(int attribute((unused
)) nargs
,
1252 struct queue_entry
*q
;
1255 if(disorder_resolve(ds
->g
->client
, &track
, args
[0])) return;
1256 lookups(ds
, DC_QUEUE
|DC_PLAYING
);
1257 if(ds
->g
->playing
&& !strcmp(ds
->g
->playing
->track
, track
))
1258 cgi_output(output
, "playing");
1260 for(q
= ds
->g
->queue
; q
&& strcmp(q
->track
, track
); q
= q
->next
)
1263 cgi_output(output
, "queued");
1267 static void exp_thisurl(int attribute((unused
)) nargs
,
1268 char attribute((unused
)) **args
,
1270 void attribute((unused
)) *u
) {
1271 kvp_set(&cgi_args
, "nonce", nonce()); /* nonces had better differ! */
1272 cgi_output(output
, "%s?%s", config
->url
, kvp_urlencode(cgi_args
, 0));
1275 static void exp_isfirst(int attribute((unused
)) nargs
,
1276 char attribute((unused
)) **args
,
1281 sink_printf(output
->sink
, "%s", bool2str(!!ds
->first
));
1284 static void exp_islast(int attribute((unused
)) nargs
,
1285 char attribute((unused
)) **args
,
1290 sink_printf(output
->sink
, "%s", bool2str(!!ds
->last
));
1293 static void exp_action(int attribute((unused
)) nargs
,
1294 char attribute((unused
)) **args
,
1296 void attribute((unused
)) *u
) {
1297 const char *action
= cgi_get("action"), *mgmt
;
1299 if(!action
) action
= "playing";
1300 if(!strcmp(action
, "playing")
1301 && (mgmt
= cgi_get("mgmt"))
1302 && !strcmp(mgmt
, "true"))
1304 sink_printf(output
->sink
, "%s", action
);
1307 static void exp_resolve(int attribute((unused
)) nargs
,
1310 void attribute((unused
)) *u
) {
1314 if(!disorder_resolve(ds
->g
->client
, &track
, args
[0]))
1315 sink_printf(output
->sink
, "%s", track
);
1318 static void exp_paused(int attribute((unused
)) nargs
,
1319 char attribute((unused
)) **args
,
1325 lookups(ds
, DC_PLAYING
);
1326 if(ds
->g
->playing
&& ds
->g
->playing
->state
== playing_paused
)
1328 cgi_output(output
, "%s", bool2str(paused
));
1331 static void exp_state(int attribute((unused
)) nargs
,
1332 char attribute((unused
)) **args
,
1338 cgi_output(output
, "%s", playing_states
[ds
->track
->state
]);
1341 static void exp_files(int attribute((unused
)) nargs
,
1346 dcgi_state substate
;
1347 const char *nfiles_arg
, *directory
;
1348 int nfiles
, numfile
;
1351 memset(&substate
, 0, sizeof substate
);
1353 if((directory
= cgi_get("directory"))) {
1354 /* Prefs for whole directory. */
1355 lookups(ds
, DC_FILES
);
1356 /* Synthesize args for the file list. */
1357 nfiles
= ds
->g
->nfiles
;
1358 for(numfile
= 0; numfile
< nfiles
; ++numfile
) {
1359 k
= xmalloc(sizeof *k
);
1360 byte_xasprintf((char **)&k
->name
, "%d_file", numfile
);
1361 k
->value
= ds
->g
->files
[numfile
];
1366 /* Args already present. */
1367 if((nfiles_arg
= cgi_get("files"))) nfiles
= atoi(nfiles_arg
);
1370 for(numfile
= 0; numfile
< nfiles
; ++numfile
) {
1371 substate
.index
= numfile
;
1372 expandstring(output
, args
[0], &substate
);
1376 static void exp_index(int attribute((unused
)) nargs
,
1377 char attribute((unused
)) **args
,
1382 cgi_output(output
, "%d", ds
->index
);
1385 static void exp_nfiles(int attribute((unused
)) nargs
,
1386 char attribute((unused
)) **args
,
1390 const char *files_arg
;
1392 if(cgi_get("directory")) {
1393 lookups(ds
, DC_FILES
);
1394 cgi_output(output
, "%d", ds
->g
->nfiles
);
1395 } else if((files_arg
= cgi_get("files")))
1396 cgi_output(output
, "%s", files_arg
);
1398 cgi_output(output
, "1");
1401 static const struct cgi_expansion expansions
[] = {
1402 { "#", 0, INT_MAX
, EXP_MAGIC
, exp_comment
},
1403 { "action", 0, 0, 0, exp_action
},
1404 { "and", 0, INT_MAX
, EXP_MAGIC
, exp_and
},
1405 { "arg", 1, 1, 0, exp_arg
},
1406 { "basename", 0, 1, 0, exp_basename
},
1407 { "choose", 2, 2, EXP_MAGIC
, exp_choose
},
1408 { "dirname", 0, 1, 0, exp_dirname
},
1409 { "enabled", 0, 0, 0, exp_enabled
},
1410 { "eq", 2, 2, 0, exp_eq
},
1411 { "file", 0, 0, 0, exp_file
},
1412 { "files", 1, 1, EXP_MAGIC
, exp_files
},
1413 { "fullname", 0, 0, 0, exp_fullname
},
1414 { "id", 0, 0, 0, exp_id
},
1415 { "if", 2, 3, EXP_MAGIC
, exp_if
},
1416 { "include", 1, 1, 0, exp_include
},
1417 { "index", 0, 0, 0, exp_index
},
1418 { "isdirectories", 0, 0, 0, exp_isdirectories
},
1419 { "isfiles", 0, 0, 0, exp_isfiles
},
1420 { "isfirst", 0, 0, 0, exp_isfirst
},
1421 { "islast", 0, 0, 0, exp_islast
},
1422 { "isnew", 0, 0, 0, exp_isnew
},
1423 { "isplaying", 0, 0, 0, exp_isplaying
},
1424 { "isqueue", 0, 0, 0, exp_isqueue
},
1425 { "isrecent", 0, 0, 0, exp_isrecent
},
1426 { "label", 1, 1, 0, exp_label
},
1427 { "length", 0, 0, 0, exp_length
},
1428 { "navigate", 2, 2, EXP_MAGIC
, exp_navigate
},
1429 { "ne", 2, 2, 0, exp_ne
},
1430 { "new", 1, 1, EXP_MAGIC
, exp_new
},
1431 { "nfiles", 0, 0, 0, exp_nfiles
},
1432 { "nonce", 0, 0, 0, exp_nonce
},
1433 { "not", 1, 1, 0, exp_not
},
1434 { "or", 0, INT_MAX
, EXP_MAGIC
, exp_or
},
1435 { "parity", 0, 0, 0, exp_parity
},
1436 { "part", 1, 3, 0, exp_part
},
1437 { "paused", 0, 0, 0, exp_paused
},
1438 { "playing", 1, 1, EXP_MAGIC
, exp_playing
},
1439 { "pref", 2, 2, 0, exp_pref
},
1440 { "prefname", 0, 0, 0, exp_prefname
},
1441 { "prefs", 2, 2, EXP_MAGIC
, exp_prefs
},
1442 { "prefvalue", 0, 0, 0, exp_prefvalue
},
1443 { "queue", 1, 1, EXP_MAGIC
, exp_queue
},
1444 { "random-enabled", 0, 0, 0, exp_random_enabled
},
1445 { "recent", 1, 1, EXP_MAGIC
, exp_recent
},
1446 { "removable", 0, 0, 0, exp_removable
},
1447 { "resolve", 1, 1, 0, exp_resolve
},
1448 { "scratchable", 0, 0, 0, exp_scratchable
},
1449 { "search", 2, 3, EXP_MAGIC
, exp_search
},
1450 { "server-version", 0, 0, 0, exp_server_version
},
1451 { "shell", 1, 1, 0, exp_shell
},
1452 { "state", 0, 0, 0, exp_state
},
1453 { "stats", 0, 0, 0, exp_stats
},
1454 { "thisurl", 0, 0, 0, exp_thisurl
},
1455 { "track", 0, 0, 0, exp_track
},
1456 { "trackstate", 1, 1, 0, exp_trackstate
},
1457 { "transform", 2, 3, 0, exp_transform
},
1458 { "url", 0, 0, 0, exp_url
},
1459 { "urlquote", 1, 1, 0, exp_urlquote
},
1460 { "version", 0, 0, 0, exp_version
},
1461 { "volume", 1, 1, 0, exp_volume
},
1462 { "when", 0, 0, 0, exp_when
},
1463 { "who", 0, 0, 0, exp_who
}
1466 static void expand(cgi_sink
*output
,
1467 const char *template,
1469 cgi_expand(template,
1470 expansions
, sizeof expansions
/ sizeof *expansions
,
1475 static void expandstring(cgi_sink
*output
,
1478 cgi_expand_string("",
1480 expansions
, sizeof expansions
/ sizeof *expansions
,
1485 static void perform_action(cgi_sink
*output
, dcgi_state
*ds
,
1486 const char *action
) {
1489 if((n
= TABLE_FIND(actions
, struct action
, name
, action
)) >= 0)
1490 actions
[n
].handler(output
, ds
);
1492 cgi_header(output
->sink
, "Content-Type", "text/html");
1493 cgi_body(output
->sink
);
1494 expand(output
, action
, ds
);
1498 void disorder_cgi(cgi_sink
*output
, dcgi_state
*ds
) {
1499 const char *action
= cgi_get("action");
1501 if(!action
) action
= "playing";
1502 perform_action(output
, ds
, action
);
1505 void disorder_cgi_error(cgi_sink
*output
, dcgi_state
*ds
,
1507 cgi_set_option("error", msg
);
1508 perform_action(output
, ds
, "error");