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>
43 #include "configuration.h"
54 #include "trackname.h"
63 static void expand(cgi_sink
*output
,
66 static void expandstring(cgi_sink
*output
,
76 static const char *nonce(void) {
77 static unsigned long count
;
80 byte_xasprintf(&s
, "%lx%lx%lx",
81 (unsigned long)time(0),
82 (unsigned long)getpid(),
87 static int compare_entry(const void *a
, const void *b
) {
88 const struct entry
*ea
= a
, *eb
= b
;
90 return compare_tracks(ea
->sort
, eb
->sort
,
91 ea
->display
, eb
->display
,
95 static const char *front_url(void) {
99 /* preserve management interface visibility */
100 if((mgmt
= cgi_get("mgmt")) && !strcmp(mgmt
, "true")) {
101 byte_xasprintf(&url
, "%s?mgmt=true", config
->url
);
107 static void header_cookie(struct sink
*output
) {
111 memset(&u
, 0, sizeof u
);
113 parse_url(config
->url
, &u
);
115 dynstr_append_string(d
, "disorder=");
116 dynstr_append_string(d
, quote822(login_cookie
, 0));
118 /* Force browser to discard cookie */
119 dynstr_append_string(d
, "disorder=none;Max-Age=0");
122 /* The default domain matches the request host, so we need not override
123 * that. But the default path only goes up to the rightmost /, which would
124 * cause the browser to expose the cookie to other CGI programs on the same
126 dynstr_append_string(d
, ";Path=");
127 dynstr_append_string(d
, quote822(u
.path
, 0));
130 cgi_header(output
, "Set-Cookie", d
->vec
);
133 static void redirect(struct sink
*output
) {
136 back
= cgi_get("back");
137 cgi_header(output
, "Location", back
&& *back ? back
: front_url());
138 header_cookie(output
);
142 static void expand_template(dcgi_state
*ds
, cgi_sink
*output
,
143 const char *action
) {
144 cgi_header(output
->sink
, "Content-Type", "text/html");
145 header_cookie(output
->sink
);
146 cgi_body(output
->sink
);
147 expand(output
, action
, ds
);
150 static void lookups(dcgi_state
*ds
, unsigned want
) {
152 struct queue_entry
*r
, *rnext
;
153 const char *dir
, *re
;
156 if(ds
->g
->client
&& (need
= want
^ (ds
->g
->flags
& want
)) != 0) {
158 disorder_queue(ds
->g
->client
, &ds
->g
->queue
);
159 if(need
& DC_PLAYING
)
160 disorder_playing(ds
->g
->client
, &ds
->g
->playing
);
162 disorder_new_tracks(ds
->g
->client
, &ds
->g
->new, &ds
->g
->nnew
, 0);
163 if(need
& DC_RECENT
) {
164 /* we need to reverse the order of the list */
165 disorder_recent(ds
->g
->client
, &r
);
168 r
->next
= ds
->g
->recent
;
174 disorder_get_volume(ds
->g
->client
,
175 &ds
->g
->volume_left
, &ds
->g
->volume_right
);
176 if(need
& (DC_FILES
|DC_DIRS
)) {
177 if(!(dir
= cgi_get("directory")))
179 re
= cgi_get("regexp");
181 if(disorder_directories(ds
->g
->client
, dir
, re
,
182 &ds
->g
->dirs
, &ds
->g
->ndirs
))
185 if(disorder_files(ds
->g
->client
, dir
, re
,
186 &ds
->g
->files
, &ds
->g
->nfiles
))
189 if(need
& DC_RIGHTS
) {
190 ds
->g
->rights
= RIGHT_READ
; /* fail-safe */
191 if(!disorder_userinfo(ds
->g
->client
, disorder_user(ds
->g
->client
),
193 parse_rights(rights
, &ds
->g
->rights
, 1);
195 ds
->g
->flags
|= need
;
199 /* actions ********************************************************************/
201 static void act_disable(cgi_sink
*output
,
204 disorder_disable(ds
->g
->client
);
205 redirect(output
->sink
);
208 static void act_enable(cgi_sink
*output
,
211 disorder_enable(ds
->g
->client
);
212 redirect(output
->sink
);
215 static void act_random_disable(cgi_sink
*output
,
218 disorder_random_disable(ds
->g
->client
);
219 redirect(output
->sink
);
222 static void act_random_enable(cgi_sink
*output
,
225 disorder_random_enable(ds
->g
->client
);
226 redirect(output
->sink
);
229 static void act_remove(cgi_sink
*output
,
233 if(!(id
= cgi_get("id"))) fatal(0, "missing id argument");
235 disorder_remove(ds
->g
->client
, id
);
236 redirect(output
->sink
);
239 static void act_move(cgi_sink
*output
,
241 const char *id
, *delta
;
243 if(!(id
= cgi_get("id"))) fatal(0, "missing id argument");
244 if(!(delta
= cgi_get("delta"))) fatal(0, "missing delta argument");
246 disorder_move(ds
->g
->client
, id
, atoi(delta
));
247 redirect(output
->sink
);
250 static void act_scratch(cgi_sink
*output
,
253 disorder_scratch(ds
->g
->client
, cgi_get("id"));
254 redirect(output
->sink
);
257 static void act_playing(cgi_sink
*output
, dcgi_state
*ds
) {
259 long refresh
= config
->refresh
, length
;
261 int random_enabled
= 0;
264 lookups(ds
, DC_PLAYING
|DC_QUEUE
);
265 cgi_header(output
->sink
, "Content-Type", "text/html");
266 disorder_random_enabled(ds
->g
->client
, &random_enabled
);
267 disorder_enabled(ds
->g
->client
, &enabled
);
269 && ds
->g
->playing
->state
== playing_started
/* i.e. not paused */
270 && !disorder_length(ds
->g
->client
, ds
->g
->playing
->track
, &length
)
272 && ds
->g
->playing
->sofar
>= 0) {
273 /* Try to put the next refresh at the start of the next track. */
275 fin
= now
+ length
- ds
->g
->playing
->sofar
+ config
->gap
;
276 if(now
+ refresh
> fin
)
279 if(ds
->g
->queue
&& ds
->g
->queue
->state
== playing_isscratch
) {
280 /* next track is a scratch, don't leave more than the inter-track gap */
281 if(refresh
> config
->gap
)
282 refresh
= config
->gap
;
284 if(!ds
->g
->playing
&& ((ds
->g
->queue
285 && ds
->g
->queue
->state
!= playing_random
)
286 || random_enabled
) && enabled
) {
287 /* no track playing but playing is enabled and there is something coming
288 * up, must be in a gap */
289 if(refresh
> config
->gap
)
290 refresh
= config
->gap
;
292 byte_snprintf(r
, sizeof r
, "%ld;url=%s", refresh
> 0 ? refresh
: 1,
294 cgi_header(output
->sink
, "Refresh", r
);
295 header_cookie(output
->sink
);
296 cgi_body(output
->sink
);
297 expand(output
, "playing", ds
);
300 static void act_play(cgi_sink
*output
,
302 const char *track
, *dir
;
307 if((track
= cgi_get("file"))) {
308 disorder_play(ds
->g
->client
, track
);
309 } else if((dir
= cgi_get("directory"))) {
310 if(disorder_files(ds
->g
->client
, dir
, 0, &tracks
, &ntracks
)) ntracks
= 0;
312 e
= xmalloc(ntracks
* sizeof (struct entry
));
313 for(n
= 0; n
< ntracks
; ++n
) {
314 e
[n
].path
= tracks
[n
];
315 e
[n
].sort
= trackname_transform("track", tracks
[n
], "sort");
316 e
[n
].display
= trackname_transform("track", tracks
[n
], "display");
318 qsort(e
, ntracks
, sizeof (struct entry
), compare_entry
);
319 for(n
= 0; n
< ntracks
; ++n
)
320 disorder_play(ds
->g
->client
, e
[n
].path
);
323 /* XXX error handling */
324 redirect(output
->sink
);
327 static int clamp(int n
, int min
, int max
) {
335 static const char *volume_url(void) {
338 byte_xasprintf(&url
, "%s?action=volume", config
->url
);
342 static void act_volume(cgi_sink
*output
, dcgi_state
*ds
) {
343 const char *l
, *r
, *d
, *back
;
344 int nd
, changed
= 0;;
346 if((d
= cgi_get("delta"))) {
347 lookups(ds
, DC_VOLUME
);
348 nd
= clamp(atoi(d
), -255, 255);
349 disorder_set_volume(ds
->g
->client
,
350 clamp(ds
->g
->volume_left
+ nd
, 0, 255),
351 clamp(ds
->g
->volume_right
+ nd
, 0, 255));
353 } else if((l
= cgi_get("left")) && (r
= cgi_get("right"))) {
354 disorder_set_volume(ds
->g
->client
, atoi(l
), atoi(r
));
358 /* redirect back to ourselves (but without the volume-changing bits in the
360 cgi_header(output
->sink
, "Location",
361 (back
= cgi_get("back")) ? back
: volume_url());
362 header_cookie(output
->sink
);
363 cgi_body(output
->sink
);
365 cgi_header(output
->sink
, "Content-Type", "text/html");
366 header_cookie(output
->sink
);
367 cgi_body(output
->sink
);
368 expand(output
, "volume", ds
);
372 static void act_prefs_errors(const char *msg
,
373 void attribute((unused
)) *u
) {
374 fatal(0, "error splitting parts list: %s", msg
);
377 static const char *numbered_arg(const char *argname
, int numfile
) {
380 byte_xasprintf(&fullname
, "%d_%s", numfile
, argname
);
381 return cgi_get(fullname
);
384 static void process_prefs(dcgi_state
*ds
, int numfile
) {
385 const char *file
, *name
, *value
, *part
, *parts
, *current
, *context
;
388 if(!(file
= numbered_arg("file", numfile
)))
389 /* The first file doesn't need numbering. */
390 if(numfile
> 0 || !(file
= cgi_get("file")))
392 if((parts
= numbered_arg("parts", numfile
))
393 || (parts
= cgi_get("parts"))) {
394 /* Default context is display. Other contexts not actually tested. */
395 if(!(context
= numbered_arg("context", numfile
))) context
= "display";
396 partslist
= split(parts
, 0, 0, act_prefs_errors
, 0);
397 while((part
= *partslist
++)) {
398 if(!(value
= numbered_arg(part
, numfile
)))
400 /* If it's already right (whether regexps or db) don't change anything,
401 * so we don't fill the database up with rubbish. */
402 if(disorder_part(ds
->g
->client
, (char **)¤t
,
403 file
, context
, part
))
404 fatal(0, "disorder_part() failed");
405 if(!strcmp(current
, value
))
407 byte_xasprintf((char **)&name
, "trackname_%s_%s", context
, part
);
408 disorder_set(ds
->g
->client
, file
, name
, value
);
410 if((value
= numbered_arg("random", numfile
)))
411 disorder_unset(ds
->g
->client
, file
, "pick_at_random");
413 disorder_set(ds
->g
->client
, file
, "pick_at_random", "0");
414 if((value
= numbered_arg("tags", numfile
)))
415 disorder_set(ds
->g
->client
, file
, "tags", value
);
416 } else if((name
= cgi_get("name"))) {
417 /* Raw preferences. Not well supported in the templates at the moment. */
418 value
= cgi_get("value");
420 disorder_set(ds
->g
->client
, file
, name
, value
);
422 disorder_unset(ds
->g
->client
, file
, name
);
426 static void act_prefs(cgi_sink
*output
, dcgi_state
*ds
) {
430 if((files
= cgi_get("files"))) nfiles
= atoi(files
);
432 for(numfile
= 0; numfile
< nfiles
; ++numfile
)
433 process_prefs(ds
, numfile
);
434 cgi_header(output
->sink
, "Content-Type", "text/html");
435 header_cookie(output
->sink
);
436 cgi_body(output
->sink
);
437 expand(output
, "prefs", ds
);
440 static void act_pause(cgi_sink
*output
,
443 disorder_pause(ds
->g
->client
);
444 redirect(output
->sink
);
447 static void act_resume(cgi_sink
*output
,
450 disorder_resume(ds
->g
->client
);
451 redirect(output
->sink
);
454 static void act_login(cgi_sink
*output
,
456 const char *username
, *password
, *back
;
459 username
= cgi_get("username");
460 password
= cgi_get("password");
461 if(!username
|| !password
462 || !strcmp(username
, "guest")/*bodge to avoid guest cookies*/) {
463 /* We're just visiting the login page */
464 expand_template(ds
, output
, "login");
468 if(disorder_connect_user(c
, username
, password
)) {
469 cgi_set_option("error", "loginfailed");
470 expand_template(ds
, output
, "login");
473 if(disorder_make_cookie(c
, &login_cookie
)) {
474 cgi_set_option("error", "cookiefailed");
475 expand_template(ds
, output
, "login");
478 /* We have a new cookie */
479 header_cookie(output
->sink
);
480 if((back
= cgi_get("back")) && back
)
481 /* Redirect back to somewhere or other */
482 redirect(output
->sink
);
484 /* Stick to the login page */
485 expand_template(ds
, output
, "login");
488 static void act_logout(cgi_sink
*output
,
490 disorder_revoke(ds
->g
->client
);
492 /* Reconnect as guest */
493 disorder_cgi_login(ds
, output
);
494 /* Back to the login page */
495 expand_template(ds
, output
, "login");
498 static void act_register(cgi_sink
*output
,
500 const char *username
, *password
, *email
;
501 char *confirm
, *content_type
;
502 const char *text
, *encoding
, *charset
;
504 username
= cgi_get("username");
505 password
= cgi_get("password");
506 email
= cgi_get("email");
508 if(!username
|| !*username
) {
509 cgi_set_option("error", "nousername");
510 expand_template(ds
, output
, "login");
513 if(!password
|| !*password
) {
514 cgi_set_option("error", "nopassword");
515 expand_template(ds
, output
, "login");
518 if(!email
|| !*email
) {
519 cgi_set_option("error", "noemail");
520 expand_template(ds
, output
, "login");
523 /* We could well do better address validation but for now we'll just do the
525 if(!strchr(email
, '@')) {
526 cgi_set_option("error", "bademail");
527 expand_template(ds
, output
, "login");
530 if(disorder_register(ds
->g
->client
, username
, password
, email
, &confirm
)) {
531 cgi_set_option("error", "cannotregister");
532 expand_template(ds
, output
, "login");
535 /* Send the user a mail */
536 /* TODO templatize this */
537 byte_xasprintf((char **)&text
,
538 "Welcome to DisOrder. To active your login, please visit this URL:\n"
540 "%s?c=%s\n", config
->url
, urlencodestring(confirm
));
541 if(!(text
= mime_encode_text(text
, &charset
, &encoding
)))
542 fatal(0, "cannot encode email");
543 byte_xasprintf(&content_type
, "text/plain;charset=%s",
544 quote822(charset
, 0));
545 sendmail("", config
->mail_sender
, email
, "Welcome to DisOrder",
546 encoding
, content_type
, text
); /* TODO error checking */
547 /* We'll go back to the login page with a suitable message */
548 cgi_set_option("registered", "registeredok");
549 expand_template(ds
, output
, "login");
552 static void act_confirm(cgi_sink
*output
,
554 const char *confirmation
;
556 if(!(confirmation
= cgi_get("c"))) {
557 cgi_set_option("error", "noconfirm");
558 expand_template(ds
, output
, "login");
560 if(disorder_confirm(ds
->g
->client
, confirmation
)) {
561 cgi_set_option("error", "badconfirm");
562 expand_template(ds
, output
, "login");
564 cgi_set_option("confirmed", "confirmedok");
565 expand_template(ds
, output
, "login");
568 static const struct action
{
570 void (*handler
)(cgi_sink
*output
, dcgi_state
*ds
);
572 { "confirm", act_confirm
},
573 { "disable", act_disable
},
574 { "enable", act_enable
},
575 { "login", act_login
},
576 { "logout", act_logout
},
577 { "move", act_move
},
578 { "pause", act_pause
},
579 { "play", act_play
},
580 { "playing", act_playing
},
581 { "prefs", act_prefs
},
582 { "random-disable", act_random_disable
},
583 { "random-enable", act_random_enable
},
584 { "register", act_register
},
585 { "remove", act_remove
},
586 { "resume", act_resume
},
587 { "scratch", act_scratch
},
588 { "volume", act_volume
},
591 /* expansions *****************************************************************/
593 static void exp_include(int attribute((unused
)) nargs
,
597 expand(output
, args
[0], u
);
600 static void exp_server_version(int attribute((unused
)) nargs
,
601 char attribute((unused
)) **args
,
608 if(disorder_version(ds
->g
->client
, (char **)&v
)) v
= "(cannot get version)";
610 v
= "(server not running)";
611 cgi_output(output
, "%s", v
);
614 static void exp_version(int attribute((unused
)) nargs
,
615 char attribute((unused
)) **args
,
617 void attribute((unused
)) *u
) {
618 cgi_output(output
, "%s", disorder_short_version_string
);
621 static void exp_nonce(int attribute((unused
)) nargs
,
622 char attribute((unused
)) **args
,
624 void attribute((unused
)) *u
) {
625 cgi_output(output
, "%s", nonce());
628 static void exp_label(int attribute((unused
)) nargs
,
631 void attribute((unused
)) *u
) {
632 cgi_output(output
, "%s", cgi_label(args
[0]));
635 struct trackinfo_state
{
637 const struct queue_entry
*q
;
642 static void exp_who(int attribute((unused
)) nargs
,
643 char attribute((unused
)) **args
,
648 if(ds
->track
&& ds
->track
->submitter
)
649 cgi_output(output
, "%s", ds
->track
->submitter
);
652 static void exp_length(int attribute((unused
)) nargs
,
653 char attribute((unused
)) **args
,
660 && (ds
->track
->state
== playing_started
661 || ds
->track
->state
== playing_paused
)
662 && ds
->track
->sofar
>= 0)
663 cgi_output(output
, "%ld:%02ld/",
664 ds
->track
->sofar
/ 60, ds
->track
->sofar
% 60);
667 disorder_length(ds
->g
->client
, ds
->track
->track
, &length
);
669 disorder_length(ds
->g
->client
, ds
->tracks
[0], &length
);
671 cgi_output(output
, "%ld:%02ld", length
/ 60, length
% 60);
673 sink_printf(output
->sink
, "%s", " ");
676 static void exp_when(int attribute((unused
)) nargs
,
677 char attribute((unused
)) **args
,
681 const struct tm
*w
= 0;
684 switch(ds
->track
->state
) {
685 case playing_isscratch
:
686 case playing_unplayed
:
688 if(ds
->track
->expected
)
689 w
= localtime(&ds
->track
->expected
);
692 case playing_no_player
:
694 case playing_scratched
:
695 case playing_started
:
697 case playing_quitting
:
698 if(ds
->track
->played
)
699 w
= localtime(&ds
->track
->played
);
703 cgi_output(output
, "%d:%02d", w
->tm_hour
, w
->tm_min
);
705 sink_printf(output
->sink
, " ");
708 static void exp_part(int nargs
,
713 const char *s
, *track
, *part
, *context
;
719 track
= ds
->track
->track
;
721 track
= ds
->tracks
[0];
739 if(disorder_part(ds
->g
->client
, (char **)&s
, track
,
740 !strcmp(context
, "short") ?
"display" : context
, part
))
741 fatal(0, "disorder_part() failed");
742 if(!strcmp(context
, "short"))
743 s
= truncate_for_display(s
, config
->short_display
);
744 cgi_output(output
, "%s", s
);
746 sink_printf(output
->sink
, " ");
749 static void exp_playing(int attribute((unused
)) nargs
,
756 lookups(ds
, DC_PLAYING
);
757 memset(&s
, 0, sizeof s
);
760 s
.track
= ds
->g
->playing
;
761 expandstring(output
, args
[0], &s
);
765 static void exp_queue(int attribute((unused
)) nargs
,
771 struct queue_entry
*q
;
773 lookups(ds
, DC_QUEUE
);
774 memset(&s
, 0, sizeof s
);
777 for(q
= ds
->g
->queue
; q
; q
= q
->next
) {
780 expandstring(output
, args
[0], &s
);
786 static void exp_recent(int attribute((unused
)) nargs
,
792 struct queue_entry
*q
;
794 lookups(ds
, DC_RECENT
);
795 memset(&s
, 0, sizeof s
);
798 for(q
= ds
->g
->recent
; q
; q
= q
->next
) {
801 expandstring(output
, args
[0], &s
);
807 static void exp_new(int attribute((unused
)) nargs
,
815 memset(&s
, 0, sizeof s
);
818 for(s
.index
= 0; s
.index
< ds
->g
->nnew
; ++s
.index
) {
819 s
.last
= s
.index
+ 1 < ds
->g
->nnew
;
820 s
.tracks
= &ds
->g
->new[s
.index
];
821 expandstring(output
, args
[0], &s
);
826 static void exp_url(int attribute((unused
)) nargs
,
827 char attribute((unused
)) **args
,
829 void attribute((unused
)) *u
) {
830 cgi_output(output
, "%s", config
->url
);
838 static int compare_result(const void *a
, const void *b
) {
839 const struct result
*ra
= a
, *rb
= b
;
842 if(!(c
= strcmp(ra
->sort
, rb
->sort
)))
843 c
= strcmp(ra
->track
, rb
->track
);
847 static void exp_search(int nargs
,
851 dcgi_state
*ds
= u
, substate
;
853 const char *q
, *context
, *part
, *template;
869 assert(!"should never happen");
870 part
= context
= template = 0; /* quieten compiler */
872 if(ds
->tracks
== 0) {
873 /* we are the top level, let's get some search results */
874 if(!(q
= cgi_get("query"))) return; /* no results yet */
875 if(disorder_search(ds
->g
->client
, q
, &tracks
, &ntracks
)) return;
879 ntracks
= ds
->ntracks
;
881 assert(ntracks
!= 0);
882 /* sort tracks by the appropriate part */
883 r
= xmalloc(ntracks
* sizeof *r
);
884 for(n
= 0; n
< ntracks
; ++n
) {
885 r
[n
].track
= tracks
[n
];
886 if(disorder_part(ds
->g
->client
, (char **)&r
[n
].sort
,
887 tracks
[n
], context
, part
))
888 fatal(0, "disorder_part() failed");
890 qsort(r
, ntracks
, sizeof (struct result
), compare_result
);
891 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
892 * array as we know it's guaranteed to be big enough and isn't going to be
893 * used for anything else any more. */
894 memset(&substate
, 0, sizeof substate
);
899 substate
.tracks
= tracks
;
900 substate
.ntracks
= 0;
903 && !strcmp(r
[m
].sort
, r
[n
].sort
))
904 tracks
[substate
.ntracks
++] = r
[m
++].track
;
905 substate
.last
= (m
== ntracks
);
906 expandstring(output
, template, &substate
);
911 assert(substate
.last
!= 0);
914 static void exp_arg(int attribute((unused
)) nargs
,
917 void attribute((unused
)) *u
) {
920 if((v
= cgi_get(args
[0])))
921 cgi_output(output
, "%s", v
);
924 static void exp_stats(int attribute((unused
)) nargs
,
925 char attribute((unused
)) **args
,
931 cgi_opentag(output
->sink
, "pre", "class", "stats", (char *)0);
932 if(!disorder_stats(ds
->g
->client
, &v
, 0)) {
934 cgi_output(output
, "%s\n", *v
++);
936 cgi_closetag(output
->sink
, "pre");
939 static void exp_volume(int attribute((unused
)) nargs
,
945 lookups(ds
, DC_VOLUME
);
946 if(!strcmp(args
[0], "left"))
947 cgi_output(output
, "%d", ds
->g
->volume_left
);
949 cgi_output(output
, "%d", ds
->g
->volume_right
);
952 static void exp_shell(int attribute((unused
)) nargs
,
955 void attribute((unused
)) *u
) {
961 if(!(pid
= xfork())) {
966 execlp("sh", "sh", "-c", args
[0], (char *)0);
967 fatal(errno
, "error executing sh");
970 while((n
= read(p
[0], buffer
, sizeof buffer
))) {
972 if(errno
== EINTR
) continue;
973 else fatal(errno
, "error reading from pipe");
975 output
->sink
->write(output
->sink
, buffer
, n
);
978 while((n
= waitpid(pid
, &w
, 0)) < 0 && errno
== EINTR
)
980 if(n
< 0) fatal(errno
, "error calling waitpid");
982 error(0, "shell command '%s' %s", args
[0], wstat(w
));
985 static inline int str2bool(const char *s
) {
986 return !strcmp(s
, "true");
989 static inline const char *bool2str(int n
) {
990 return n ?
"true" : "false";
993 static char *expandarg(const char *arg
, dcgi_state
*ds
) {
999 output
.sink
= sink_dynstr(&d
);
1000 expandstring(&output
, arg
, ds
);
1001 dynstr_terminate(&d
);
1005 static void exp_prefs(int attribute((unused
)) nargs
,
1010 dcgi_state substate
;
1012 const char *file
= expandarg(args
[0], ds
);
1014 memset(&substate
, 0, sizeof substate
);
1017 if(disorder_prefs(ds
->g
->client
, file
, &k
)) return;
1019 substate
.last
= !k
->next
;
1021 expandstring(output
, args
[1], &substate
);
1028 static void exp_pref(int attribute((unused
)) nargs
,
1035 if(!disorder_get(ds
->g
->client
, args
[0], args
[1], &value
))
1036 cgi_output(output
, "%s", value
);
1039 static void exp_if(int nargs
,
1044 int n
= str2bool(expandarg(args
[0], ds
)) ?
1 : 2;
1047 expandstring(output
, args
[n
], ds
);
1050 static void exp_and(int nargs
,
1057 for(n
= 0; n
< nargs
; ++n
)
1058 if(!str2bool(expandarg(args
[n
], ds
))) {
1062 sink_printf(output
->sink
, "%s", bool2str(result
));
1065 static void exp_or(int nargs
,
1072 for(n
= 0; n
< nargs
; ++n
)
1073 if(str2bool(expandarg(args
[n
], ds
))) {
1077 sink_printf(output
->sink
, "%s", bool2str(result
));
1080 static void exp_not(int attribute((unused
)) nargs
,
1083 void attribute((unused
)) *u
) {
1084 sink_printf(output
->sink
, "%s", bool2str(!str2bool(args
[0])));
1087 static void exp_isplaying(int attribute((unused
)) nargs
,
1088 char attribute((unused
)) **args
,
1093 lookups(ds
, DC_PLAYING
);
1094 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->playing
));
1097 static void exp_isqueue(int attribute((unused
)) nargs
,
1098 char attribute((unused
)) **args
,
1103 lookups(ds
, DC_QUEUE
);
1104 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->queue
));
1107 static void exp_isrecent(int attribute((unused
)) nargs
,
1108 char attribute((unused
)) **args
,
1113 lookups(ds
, DC_RECENT
);
1114 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->recent
));
1117 static void exp_isnew(int attribute((unused
)) nargs
,
1118 char attribute((unused
)) **args
,
1123 lookups(ds
, DC_NEW
);
1124 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->nnew
));
1127 static void exp_id(int attribute((unused
)) nargs
,
1128 char attribute((unused
)) **args
,
1134 cgi_output(output
, "%s", ds
->track
->id
);
1137 static void exp_track(int attribute((unused
)) nargs
,
1138 char attribute((unused
)) **args
,
1144 cgi_output(output
, "%s", ds
->track
->track
);
1147 static void exp_parity(int attribute((unused
)) nargs
,
1148 char attribute((unused
)) **args
,
1153 cgi_output(output
, "%s", ds
->index
% 2 ?
"odd" : "even");
1156 static void exp_comment(int attribute((unused
)) nargs
,
1157 char attribute((unused
)) **args
,
1158 cgi_sink
attribute((unused
)) *output
,
1159 void attribute((unused
)) *u
) {
1163 static void exp_prefname(int attribute((unused
)) nargs
,
1164 char attribute((unused
)) **args
,
1169 if(ds
->pref
&& ds
->pref
->name
)
1170 cgi_output(output
, "%s", ds
->pref
->name
);
1173 static void exp_prefvalue(int attribute((unused
)) nargs
,
1174 char attribute((unused
)) **args
,
1179 if(ds
->pref
&& ds
->pref
->value
)
1180 cgi_output(output
, "%s", ds
->pref
->value
);
1183 static void exp_isfiles(int attribute((unused
)) nargs
,
1184 char attribute((unused
)) **args
,
1189 lookups(ds
, DC_FILES
);
1190 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->nfiles
));
1193 static void exp_isdirectories(int attribute((unused
)) nargs
,
1194 char attribute((unused
)) **args
,
1199 lookups(ds
, DC_DIRS
);
1200 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->ndirs
));
1203 static void exp_choose(int attribute((unused
)) nargs
,
1208 dcgi_state substate
;
1212 const char *type
, *what
= expandarg(args
[0], ds
);
1214 if(!strcmp(what
, "files")) {
1215 lookups(ds
, DC_FILES
);
1216 files
= ds
->g
->files
;
1217 nfiles
= ds
->g
->nfiles
;
1219 } else if(!strcmp(what
, "directories")) {
1220 lookups(ds
, DC_DIRS
);
1221 files
= ds
->g
->dirs
;
1222 nfiles
= ds
->g
->ndirs
;
1225 error(0, "unknown @choose@ argument '%s'", what
);
1228 e
= xmalloc(nfiles
* sizeof (struct entry
));
1229 for(n
= 0; n
< nfiles
; ++n
) {
1230 e
[n
].path
= files
[n
];
1231 e
[n
].sort
= trackname_transform(type
, files
[n
], "sort");
1232 e
[n
].display
= trackname_transform(type
, files
[n
], "display");
1234 qsort(e
, nfiles
, sizeof (struct entry
), compare_entry
);
1235 memset(&substate
, 0, sizeof substate
);
1238 for(n
= 0; n
< nfiles
; ++n
) {
1239 substate
.last
= (n
== nfiles
- 1);
1241 substate
.entry
= &e
[n
];
1242 expandstring(output
, args
[1], &substate
);
1247 static void exp_file(int attribute((unused
)) nargs
,
1248 char attribute((unused
)) **args
,
1254 cgi_output(output
, "%s", ds
->entry
->path
);
1256 cgi_output(output
, "%s", ds
->track
->track
);
1258 cgi_output(output
, "%s", ds
->tracks
[0]);
1261 static void exp_transform(int nargs
,
1264 void attribute((unused
)) *u
) {
1265 const char *context
= nargs
> 2 ? args
[2] : "display";
1267 cgi_output(output
, "%s", trackname_transform(args
[1], args
[0], context
));
1270 static void exp_urlquote(int attribute((unused
)) nargs
,
1273 void attribute((unused
)) *u
) {
1274 cgi_output(output
, "%s", urlencodestring(args
[0]));
1277 static void exp_scratchable(int attribute((unused
)) nargs
,
1278 char attribute((unused
)) **args
,
1280 void attribute((unused
)) *u
) {
1283 lookups(ds
, DC_PLAYING
|DC_RIGHTS
);
1284 sink_printf(output
->sink
, "%s",
1285 bool2str(right_scratchable(ds
->g
->rights
,
1286 disorder_user(ds
->g
->client
),
1290 static void exp_removable(int attribute((unused
)) nargs
,
1291 char attribute((unused
)) **args
,
1293 void attribute((unused
)) *u
) {
1296 lookups(ds
, DC_RIGHTS
);
1297 sink_printf(output
->sink
, "%s",
1298 bool2str(right_removable(ds
->g
->rights
,
1299 disorder_user(ds
->g
->client
),
1303 static void exp_movable(int attribute((unused
)) nargs
,
1304 char attribute((unused
)) **args
,
1306 void attribute((unused
)) *u
) {
1309 lookups(ds
, DC_RIGHTS
);
1310 sink_printf(output
->sink
, "%s",
1311 bool2str(right_movable(ds
->g
->rights
,
1312 disorder_user(ds
->g
->client
),
1316 static void exp_navigate(int attribute((unused
)) nargs
,
1321 dcgi_state substate
;
1322 const char *path
= expandarg(args
[0], ds
);
1327 memset(&substate
, 0, sizeof substate
);
1329 ptr
= path
+ 1; /* skip root */
1331 substate
.nav_path
= path
;
1334 while(*ptr
&& *ptr
!= '/')
1336 substate
.last
= !*ptr
;
1337 substate
.nav_len
= ptr
- path
;
1338 substate
.nav_dirlen
= dirlen
;
1339 expandstring(output
, args
[1], &substate
);
1340 dirlen
= substate
.nav_len
;
1347 static void exp_fullname(int attribute((unused
)) nargs
,
1348 char attribute((unused
)) **args
,
1352 cgi_output(output
, "%.*s", ds
->nav_len
, ds
->nav_path
);
1355 static void exp_basename(int nargs
,
1363 if((s
= strrchr(args
[0], '/'))) ++s
;
1365 cgi_output(output
, "%s", s
);
1367 cgi_output(output
, "%.*s", ds
->nav_len
- ds
->nav_dirlen
- 1,
1368 ds
->nav_path
+ ds
->nav_dirlen
+ 1);
1371 static void exp_dirname(int nargs
,
1379 if((s
= strrchr(args
[0], '/')))
1380 cgi_output(output
, "%.*s", (int)(s
- args
[0]), args
[0]);
1382 cgi_output(output
, "%.*s", ds
->nav_dirlen
, ds
->nav_path
);
1385 static void exp_eq(int attribute((unused
)) nargs
,
1388 void attribute((unused
)) *u
) {
1389 cgi_output(output
, "%s", bool2str(!strcmp(args
[0], args
[1])));
1392 static void exp_ne(int attribute((unused
)) nargs
,
1395 void attribute((unused
)) *u
) {
1396 cgi_output(output
, "%s", bool2str(strcmp(args
[0], args
[1])));
1399 static void exp_enabled(int attribute((unused
)) nargs
,
1400 char attribute((unused
)) **args
,
1407 disorder_enabled(ds
->g
->client
, &enabled
);
1408 cgi_output(output
, "%s", bool2str(enabled
));
1411 static void exp_random_enabled(int attribute((unused
)) nargs
,
1412 char attribute((unused
)) **args
,
1419 disorder_random_enabled(ds
->g
->client
, &enabled
);
1420 cgi_output(output
, "%s", bool2str(enabled
));
1423 static void exp_trackstate(int attribute((unused
)) nargs
,
1428 struct queue_entry
*q
;
1431 if(disorder_resolve(ds
->g
->client
, &track
, args
[0])) return;
1432 lookups(ds
, DC_QUEUE
|DC_PLAYING
);
1433 if(ds
->g
->playing
&& !strcmp(ds
->g
->playing
->track
, track
))
1434 cgi_output(output
, "playing");
1436 for(q
= ds
->g
->queue
; q
&& strcmp(q
->track
, track
); q
= q
->next
)
1439 cgi_output(output
, "queued");
1443 static void exp_thisurl(int attribute((unused
)) nargs
,
1444 char attribute((unused
)) **args
,
1446 void attribute((unused
)) *u
) {
1447 kvp_set(&cgi_args
, "nonce", nonce()); /* nonces had better differ! */
1448 cgi_output(output
, "%s?%s", config
->url
, kvp_urlencode(cgi_args
, 0));
1451 static void exp_isfirst(int attribute((unused
)) nargs
,
1452 char attribute((unused
)) **args
,
1457 sink_printf(output
->sink
, "%s", bool2str(!!ds
->first
));
1460 static void exp_islast(int attribute((unused
)) nargs
,
1461 char attribute((unused
)) **args
,
1466 sink_printf(output
->sink
, "%s", bool2str(!!ds
->last
));
1469 static void exp_action(int attribute((unused
)) nargs
,
1470 char attribute((unused
)) **args
,
1472 void attribute((unused
)) *u
) {
1473 const char *action
= cgi_get("action"), *mgmt
;
1475 if(!action
) action
= "playing";
1476 if(!strcmp(action
, "playing")
1477 && (mgmt
= cgi_get("mgmt"))
1478 && !strcmp(mgmt
, "true"))
1480 sink_printf(output
->sink
, "%s", action
);
1483 static void exp_resolve(int attribute((unused
)) nargs
,
1486 void attribute((unused
)) *u
) {
1490 if(!disorder_resolve(ds
->g
->client
, &track
, args
[0]))
1491 sink_printf(output
->sink
, "%s", track
);
1494 static void exp_paused(int attribute((unused
)) nargs
,
1495 char attribute((unused
)) **args
,
1501 lookups(ds
, DC_PLAYING
);
1502 if(ds
->g
->playing
&& ds
->g
->playing
->state
== playing_paused
)
1504 cgi_output(output
, "%s", bool2str(paused
));
1507 static void exp_state(int attribute((unused
)) nargs
,
1508 char attribute((unused
)) **args
,
1514 cgi_output(output
, "%s", playing_states
[ds
->track
->state
]);
1517 static void exp_files(int attribute((unused
)) nargs
,
1522 dcgi_state substate
;
1523 const char *nfiles_arg
, *directory
;
1524 int nfiles
, numfile
;
1527 memset(&substate
, 0, sizeof substate
);
1529 if((directory
= cgi_get("directory"))) {
1530 /* Prefs for whole directory. */
1531 lookups(ds
, DC_FILES
);
1532 /* Synthesize args for the file list. */
1533 nfiles
= ds
->g
->nfiles
;
1534 for(numfile
= 0; numfile
< nfiles
; ++numfile
) {
1535 k
= xmalloc(sizeof *k
);
1536 byte_xasprintf((char **)&k
->name
, "%d_file", numfile
);
1537 k
->value
= ds
->g
->files
[numfile
];
1542 /* Args already present. */
1543 if((nfiles_arg
= cgi_get("files"))) nfiles
= atoi(nfiles_arg
);
1546 for(numfile
= 0; numfile
< nfiles
; ++numfile
) {
1547 substate
.index
= numfile
;
1548 expandstring(output
, args
[0], &substate
);
1552 static void exp_index(int attribute((unused
)) nargs
,
1553 char attribute((unused
)) **args
,
1558 cgi_output(output
, "%d", ds
->index
);
1561 static void exp_nfiles(int attribute((unused
)) nargs
,
1562 char attribute((unused
)) **args
,
1566 const char *files_arg
;
1568 if(cgi_get("directory")) {
1569 lookups(ds
, DC_FILES
);
1570 cgi_output(output
, "%d", ds
->g
->nfiles
);
1571 } else if((files_arg
= cgi_get("files")))
1572 cgi_output(output
, "%s", files_arg
);
1574 cgi_output(output
, "1");
1577 static void exp_user(int attribute((unused
)) nargs
,
1578 char attribute((unused
)) **args
,
1581 dcgi_state
*const ds
= u
;
1583 cgi_output(output
, "%s", disorder_user(ds
->g
->client
));
1586 static void exp_right(int attribute((unused
)) nargs
,
1590 dcgi_state
*const ds
= u
;
1591 const char *right
= expandarg(args
[0], ds
);
1594 lookups(ds
, DC_RIGHTS
);
1595 if(parse_rights(right
, &r
, 1/*report*/))
1598 cgi_output(output
, "%s", bool2str(!!(r
& ds
->g
->rights
)));
1599 else if(r
& ds
->g
->rights
)
1600 expandstring(output
, args
[1], ds
);
1602 expandstring(output
, args
[2], ds
);
1605 static const struct cgi_expansion expansions
[] = {
1606 { "#", 0, INT_MAX
, EXP_MAGIC
, exp_comment
},
1607 { "action", 0, 0, 0, exp_action
},
1608 { "and", 0, INT_MAX
, EXP_MAGIC
, exp_and
},
1609 { "arg", 1, 1, 0, exp_arg
},
1610 { "basename", 0, 1, 0, exp_basename
},
1611 { "choose", 2, 2, EXP_MAGIC
, exp_choose
},
1612 { "dirname", 0, 1, 0, exp_dirname
},
1613 { "enabled", 0, 0, 0, exp_enabled
},
1614 { "eq", 2, 2, 0, exp_eq
},
1615 { "file", 0, 0, 0, exp_file
},
1616 { "files", 1, 1, EXP_MAGIC
, exp_files
},
1617 { "fullname", 0, 0, 0, exp_fullname
},
1618 { "id", 0, 0, 0, exp_id
},
1619 { "if", 2, 3, EXP_MAGIC
, exp_if
},
1620 { "include", 1, 1, 0, exp_include
},
1621 { "index", 0, 0, 0, exp_index
},
1622 { "isdirectories", 0, 0, 0, exp_isdirectories
},
1623 { "isfiles", 0, 0, 0, exp_isfiles
},
1624 { "isfirst", 0, 0, 0, exp_isfirst
},
1625 { "islast", 0, 0, 0, exp_islast
},
1626 { "isnew", 0, 0, 0, exp_isnew
},
1627 { "isplaying", 0, 0, 0, exp_isplaying
},
1628 { "isqueue", 0, 0, 0, exp_isqueue
},
1629 { "isrecent", 0, 0, 0, exp_isrecent
},
1630 { "label", 1, 1, 0, exp_label
},
1631 { "length", 0, 0, 0, exp_length
},
1632 { "movable", 0, 0, 0, exp_movable
},
1633 { "navigate", 2, 2, EXP_MAGIC
, exp_navigate
},
1634 { "ne", 2, 2, 0, exp_ne
},
1635 { "new", 1, 1, EXP_MAGIC
, exp_new
},
1636 { "nfiles", 0, 0, 0, exp_nfiles
},
1637 { "nonce", 0, 0, 0, exp_nonce
},
1638 { "not", 1, 1, 0, exp_not
},
1639 { "or", 0, INT_MAX
, EXP_MAGIC
, exp_or
},
1640 { "parity", 0, 0, 0, exp_parity
},
1641 { "part", 1, 3, 0, exp_part
},
1642 { "paused", 0, 0, 0, exp_paused
},
1643 { "playing", 1, 1, EXP_MAGIC
, exp_playing
},
1644 { "pref", 2, 2, 0, exp_pref
},
1645 { "prefname", 0, 0, 0, exp_prefname
},
1646 { "prefs", 2, 2, EXP_MAGIC
, exp_prefs
},
1647 { "prefvalue", 0, 0, 0, exp_prefvalue
},
1648 { "queue", 1, 1, EXP_MAGIC
, exp_queue
},
1649 { "random-enabled", 0, 0, 0, exp_random_enabled
},
1650 { "recent", 1, 1, EXP_MAGIC
, exp_recent
},
1651 { "removable", 0, 0, 0, exp_removable
},
1652 { "resolve", 1, 1, 0, exp_resolve
},
1653 { "right", 1, 3, EXP_MAGIC
, exp_right
},
1654 { "scratchable", 0, 0, 0, exp_scratchable
},
1655 { "search", 2, 3, EXP_MAGIC
, exp_search
},
1656 { "server-version", 0, 0, 0, exp_server_version
},
1657 { "shell", 1, 1, 0, exp_shell
},
1658 { "state", 0, 0, 0, exp_state
},
1659 { "stats", 0, 0, 0, exp_stats
},
1660 { "thisurl", 0, 0, 0, exp_thisurl
},
1661 { "track", 0, 0, 0, exp_track
},
1662 { "trackstate", 1, 1, 0, exp_trackstate
},
1663 { "transform", 2, 3, 0, exp_transform
},
1664 { "url", 0, 0, 0, exp_url
},
1665 { "urlquote", 1, 1, 0, exp_urlquote
},
1666 { "user", 0, 0, 0, exp_user
},
1667 { "version", 0, 0, 0, exp_version
},
1668 { "volume", 1, 1, 0, exp_volume
},
1669 { "when", 0, 0, 0, exp_when
},
1670 { "who", 0, 0, 0, exp_who
}
1673 static void expand(cgi_sink
*output
,
1674 const char *template,
1676 cgi_expand(template,
1677 expansions
, sizeof expansions
/ sizeof *expansions
,
1682 static void expandstring(cgi_sink
*output
,
1685 cgi_expand_string("",
1687 expansions
, sizeof expansions
/ sizeof *expansions
,
1692 static void perform_action(cgi_sink
*output
, dcgi_state
*ds
,
1693 const char *action
) {
1696 /* We don't ever want anything to be cached */
1697 cgi_header(output
->sink
, "Cache-Control", "no-cache");
1698 if((n
= TABLE_FIND(actions
, struct action
, name
, action
)) >= 0)
1699 actions
[n
].handler(output
, ds
);
1701 expand_template(ds
, output
, action
);
1704 void disorder_cgi(cgi_sink
*output
, dcgi_state
*ds
) {
1705 const char *action
= cgi_get("action");
1708 /* We allow URLs which are just confirm=... in order to keep confirmation
1709 * URLs, which are user-facing, as short as possible. */
1715 perform_action(output
, ds
, action
);
1718 void disorder_cgi_error(cgi_sink
*output
, dcgi_state
*ds
,
1720 cgi_set_option("error", msg
);
1721 perform_action(output
, ds
, "error");
1724 /** @brief Log in as the current user or guest if none */
1725 void disorder_cgi_login(dcgi_state
*ds
, cgi_sink
*output
) {
1726 /* Create a new connection */
1727 ds
->g
->client
= disorder_new(0);
1728 /* Forget everything we knew */
1731 if(disorder_connect_cookie(ds
->g
->client
, login_cookie
)) {
1732 disorder_cgi_error(output
, ds
, "connect");
1735 /* If there was a cookie but it went bad, we forget it */
1736 if(login_cookie
&& !strcmp(disorder_user(ds
->g
->client
), "guest"))