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");
467 /* We'll need a new connection as we are going to stop being guest */
469 if(disorder_connect_user(c
, username
, password
)) {
470 cgi_set_option("error", "loginfailed");
471 expand_template(ds
, output
, "login");
474 if(disorder_make_cookie(c
, &login_cookie
)) {
475 cgi_set_option("error", "cookiefailed");
476 expand_template(ds
, output
, "login");
479 /* Use the new connection henceforth */
482 /* We have a new cookie */
483 header_cookie(output
->sink
);
484 cgi_set_option("status", "loginok");
485 if((back
= cgi_get("back")) && *back
)
486 /* Redirect back to somewhere or other */
487 redirect(output
->sink
);
489 /* Stick to the login page */
490 expand_template(ds
, output
, "login");
493 static void act_logout(cgi_sink
*output
,
495 disorder_revoke(ds
->g
->client
);
497 /* Reconnect as guest */
498 disorder_cgi_login(ds
, output
);
499 /* Back to the login page */
500 cgi_set_option("status", "logoutok");
501 expand_template(ds
, output
, "login");
504 static void act_register(cgi_sink
*output
,
506 const char *username
, *password
, *password2
, *email
;
507 char *confirm
, *content_type
;
508 const char *text
, *encoding
, *charset
;
510 username
= cgi_get("username");
511 password
= cgi_get("password1");
512 password2
= cgi_get("password2");
513 email
= cgi_get("email");
515 if(!username
|| !*username
) {
516 cgi_set_option("error", "nousername");
517 expand_template(ds
, output
, "login");
520 if(!password
|| !*password
) {
521 cgi_set_option("error", "nopassword");
522 expand_template(ds
, output
, "login");
525 if(!password2
|| !*password2
|| strcmp(password
, password2
)) {
526 cgi_set_option("error", "passwordmismatch");
527 expand_template(ds
, output
, "login");
530 if(!email
|| !*email
) {
531 cgi_set_option("error", "noemail");
532 expand_template(ds
, output
, "login");
535 /* We could well do better address validation but for now we'll just do the
537 if(!strchr(email
, '@')) {
538 cgi_set_option("error", "bademail");
539 expand_template(ds
, output
, "login");
542 if(disorder_register(ds
->g
->client
, username
, password
, email
, &confirm
)) {
543 cgi_set_option("error", "cannotregister");
544 expand_template(ds
, output
, "login");
547 /* Send the user a mail */
548 /* TODO templatize this */
549 byte_xasprintf((char **)&text
,
550 "Welcome to DisOrder. To active your login, please visit this URL:\n"
552 "%s?c=%s\n", config
->url
, urlencodestring(confirm
));
553 if(!(text
= mime_encode_text(text
, &charset
, &encoding
)))
554 fatal(0, "cannot encode email");
555 byte_xasprintf(&content_type
, "text/plain;charset=%s",
556 quote822(charset
, 0));
557 sendmail("", config
->mail_sender
, email
, "Welcome to DisOrder",
558 encoding
, content_type
, text
); /* TODO error checking */
559 /* We'll go back to the login page with a suitable message */
560 cgi_set_option("status", "registered");
561 expand_template(ds
, output
, "login");
564 static void act_confirm(cgi_sink
*output
,
566 const char *confirmation
;
568 if(!(confirmation
= cgi_get("c"))) {
569 cgi_set_option("error", "noconfirm");
570 expand_template(ds
, output
, "login");
572 /* Confirm our registration */
573 if(disorder_confirm(ds
->g
->client
, confirmation
)) {
574 cgi_set_option("error", "badconfirm");
575 expand_template(ds
, output
, "login");
578 if(disorder_make_cookie(ds
->g
->client
, &login_cookie
)) {
579 cgi_set_option("error", "cookiefailed");
580 expand_template(ds
, output
, "login");
583 /* Discard any cached data JIC */
585 /* We have a new cookie */
586 header_cookie(output
->sink
);
587 cgi_set_option("status", "confirmed");
588 expand_template(ds
, output
, "login");
591 static void act_edituser(cgi_sink
*output
,
593 const char *email
= cgi_get("email"), *password
= cgi_get("changepassword1");
594 const char *password2
= cgi_get("changepassword2");
598 if((password
&& *password
) || (password
&& *password2
)) {
599 if(!password
|| !password2
|| strcmp(password
, password2
)) {
600 cgi_set_option("error", "passwordmismatch");
601 expand_template(ds
, output
, "login");
605 password
= password2
= 0;
608 if(disorder_edituser(ds
->g
->client
, disorder_user(ds
->g
->client
),
610 cgi_set_option("error", "badedit");
611 expand_template(ds
, output
, "login");
616 if(disorder_edituser(ds
->g
->client
, disorder_user(ds
->g
->client
),
617 "password", password
)) {
618 cgi_set_option("error", "badedit");
619 expand_template(ds
, output
, "login");
625 login_cookie
= 0; /* it'll be invalid now */
626 /* This is a bit duplicative of act_login() */
628 if(disorder_connect_user(c
, disorder_user(ds
->g
->client
), password
)) {
629 cgi_set_option("error", "loginfailed");
630 expand_template(ds
, output
, "login");
633 if(disorder_make_cookie(c
, &login_cookie
)) {
634 cgi_set_option("error", "cookiefailed");
635 expand_template(ds
, output
, "login");
638 /* Use the new connection henceforth */
641 /* We have a new cookie */
642 header_cookie(output
->sink
);
644 cgi_set_option("status", "edited");
645 expand_template(ds
, output
, "login");
649 static const struct action
{
651 void (*handler
)(cgi_sink
*output
, dcgi_state
*ds
);
653 { "confirm", act_confirm
},
654 { "disable", act_disable
},
655 { "edituser", act_edituser
},
656 { "enable", act_enable
},
657 { "login", act_login
},
658 { "logout", act_logout
},
659 { "move", act_move
},
660 { "pause", act_pause
},
661 { "play", act_play
},
662 { "playing", act_playing
},
663 { "prefs", act_prefs
},
664 { "random-disable", act_random_disable
},
665 { "random-enable", act_random_enable
},
666 { "register", act_register
},
667 { "remove", act_remove
},
668 { "resume", act_resume
},
669 { "scratch", act_scratch
},
670 { "volume", act_volume
},
673 /* expansions *****************************************************************/
675 static void exp_include(int attribute((unused
)) nargs
,
679 expand(output
, args
[0], u
);
682 static void exp_server_version(int attribute((unused
)) nargs
,
683 char attribute((unused
)) **args
,
690 if(disorder_version(ds
->g
->client
, (char **)&v
)) v
= "(cannot get version)";
692 v
= "(server not running)";
693 cgi_output(output
, "%s", v
);
696 static void exp_version(int attribute((unused
)) nargs
,
697 char attribute((unused
)) **args
,
699 void attribute((unused
)) *u
) {
700 cgi_output(output
, "%s", disorder_short_version_string
);
703 static void exp_nonce(int attribute((unused
)) nargs
,
704 char attribute((unused
)) **args
,
706 void attribute((unused
)) *u
) {
707 cgi_output(output
, "%s", nonce());
710 static void exp_label(int attribute((unused
)) nargs
,
713 void attribute((unused
)) *u
) {
714 cgi_output(output
, "%s", cgi_label(args
[0]));
717 struct trackinfo_state
{
719 const struct queue_entry
*q
;
724 static void exp_who(int attribute((unused
)) nargs
,
725 char attribute((unused
)) **args
,
730 if(ds
->track
&& ds
->track
->submitter
)
731 cgi_output(output
, "%s", ds
->track
->submitter
);
734 static void exp_length(int attribute((unused
)) nargs
,
735 char attribute((unused
)) **args
,
742 && (ds
->track
->state
== playing_started
743 || ds
->track
->state
== playing_paused
)
744 && ds
->track
->sofar
>= 0)
745 cgi_output(output
, "%ld:%02ld/",
746 ds
->track
->sofar
/ 60, ds
->track
->sofar
% 60);
749 disorder_length(ds
->g
->client
, ds
->track
->track
, &length
);
751 disorder_length(ds
->g
->client
, ds
->tracks
[0], &length
);
753 cgi_output(output
, "%ld:%02ld", length
/ 60, length
% 60);
755 sink_printf(output
->sink
, "%s", " ");
758 static void exp_when(int attribute((unused
)) nargs
,
759 char attribute((unused
)) **args
,
763 const struct tm
*w
= 0;
766 switch(ds
->track
->state
) {
767 case playing_isscratch
:
768 case playing_unplayed
:
770 if(ds
->track
->expected
)
771 w
= localtime(&ds
->track
->expected
);
774 case playing_no_player
:
776 case playing_scratched
:
777 case playing_started
:
779 case playing_quitting
:
780 if(ds
->track
->played
)
781 w
= localtime(&ds
->track
->played
);
785 cgi_output(output
, "%d:%02d", w
->tm_hour
, w
->tm_min
);
787 sink_printf(output
->sink
, " ");
790 static void exp_part(int nargs
,
795 const char *s
, *track
, *part
, *context
;
801 track
= ds
->track
->track
;
803 track
= ds
->tracks
[0];
821 if(disorder_part(ds
->g
->client
, (char **)&s
, track
,
822 !strcmp(context
, "short") ?
"display" : context
, part
))
823 fatal(0, "disorder_part() failed");
824 if(!strcmp(context
, "short"))
825 s
= truncate_for_display(s
, config
->short_display
);
826 cgi_output(output
, "%s", s
);
828 sink_printf(output
->sink
, " ");
831 static void exp_playing(int attribute((unused
)) nargs
,
838 lookups(ds
, DC_PLAYING
);
839 memset(&s
, 0, sizeof s
);
842 s
.track
= ds
->g
->playing
;
843 expandstring(output
, args
[0], &s
);
847 static void exp_queue(int attribute((unused
)) nargs
,
853 struct queue_entry
*q
;
855 lookups(ds
, DC_QUEUE
);
856 memset(&s
, 0, sizeof s
);
859 for(q
= ds
->g
->queue
; q
; q
= q
->next
) {
862 expandstring(output
, args
[0], &s
);
868 static void exp_recent(int attribute((unused
)) nargs
,
874 struct queue_entry
*q
;
876 lookups(ds
, DC_RECENT
);
877 memset(&s
, 0, sizeof s
);
880 for(q
= ds
->g
->recent
; q
; q
= q
->next
) {
883 expandstring(output
, args
[0], &s
);
889 static void exp_new(int attribute((unused
)) nargs
,
897 memset(&s
, 0, sizeof s
);
900 for(s
.index
= 0; s
.index
< ds
->g
->nnew
; ++s
.index
) {
901 s
.last
= s
.index
+ 1 < ds
->g
->nnew
;
902 s
.tracks
= &ds
->g
->new[s
.index
];
903 expandstring(output
, args
[0], &s
);
908 static void exp_url(int attribute((unused
)) nargs
,
909 char attribute((unused
)) **args
,
911 void attribute((unused
)) *u
) {
912 cgi_output(output
, "%s", config
->url
);
920 static int compare_result(const void *a
, const void *b
) {
921 const struct result
*ra
= a
, *rb
= b
;
924 if(!(c
= strcmp(ra
->sort
, rb
->sort
)))
925 c
= strcmp(ra
->track
, rb
->track
);
929 static void exp_search(int nargs
,
933 dcgi_state
*ds
= u
, substate
;
935 const char *q
, *context
, *part
, *template;
951 assert(!"should never happen");
952 part
= context
= template = 0; /* quieten compiler */
954 if(ds
->tracks
== 0) {
955 /* we are the top level, let's get some search results */
956 if(!(q
= cgi_get("query"))) return; /* no results yet */
957 if(disorder_search(ds
->g
->client
, q
, &tracks
, &ntracks
)) return;
961 ntracks
= ds
->ntracks
;
963 assert(ntracks
!= 0);
964 /* sort tracks by the appropriate part */
965 r
= xmalloc(ntracks
* sizeof *r
);
966 for(n
= 0; n
< ntracks
; ++n
) {
967 r
[n
].track
= tracks
[n
];
968 if(disorder_part(ds
->g
->client
, (char **)&r
[n
].sort
,
969 tracks
[n
], context
, part
))
970 fatal(0, "disorder_part() failed");
972 qsort(r
, ntracks
, sizeof (struct result
), compare_result
);
973 /* expand the 2nd arg once for each group. We re-use the passed-in tracks
974 * array as we know it's guaranteed to be big enough and isn't going to be
975 * used for anything else any more. */
976 memset(&substate
, 0, sizeof substate
);
981 substate
.tracks
= tracks
;
982 substate
.ntracks
= 0;
985 && !strcmp(r
[m
].sort
, r
[n
].sort
))
986 tracks
[substate
.ntracks
++] = r
[m
++].track
;
987 substate
.last
= (m
== ntracks
);
988 expandstring(output
, template, &substate
);
993 assert(substate
.last
!= 0);
996 static void exp_arg(int attribute((unused
)) nargs
,
999 void attribute((unused
)) *u
) {
1002 if((v
= cgi_get(args
[0])))
1003 cgi_output(output
, "%s", v
);
1006 static void exp_stats(int attribute((unused
)) nargs
,
1007 char attribute((unused
)) **args
,
1013 cgi_opentag(output
->sink
, "pre", "class", "stats", (char *)0);
1014 if(!disorder_stats(ds
->g
->client
, &v
, 0)) {
1016 cgi_output(output
, "%s\n", *v
++);
1018 cgi_closetag(output
->sink
, "pre");
1021 static void exp_volume(int attribute((unused
)) nargs
,
1027 lookups(ds
, DC_VOLUME
);
1028 if(!strcmp(args
[0], "left"))
1029 cgi_output(output
, "%d", ds
->g
->volume_left
);
1031 cgi_output(output
, "%d", ds
->g
->volume_right
);
1034 static void exp_shell(int attribute((unused
)) nargs
,
1037 void attribute((unused
)) *u
) {
1043 if(!(pid
= xfork())) {
1048 execlp("sh", "sh", "-c", args
[0], (char *)0);
1049 fatal(errno
, "error executing sh");
1052 while((n
= read(p
[0], buffer
, sizeof buffer
))) {
1054 if(errno
== EINTR
) continue;
1055 else fatal(errno
, "error reading from pipe");
1057 output
->sink
->write(output
->sink
, buffer
, n
);
1060 while((n
= waitpid(pid
, &w
, 0)) < 0 && errno
== EINTR
)
1062 if(n
< 0) fatal(errno
, "error calling waitpid");
1064 error(0, "shell command '%s' %s", args
[0], wstat(w
));
1067 static inline int str2bool(const char *s
) {
1068 return !strcmp(s
, "true");
1071 static inline const char *bool2str(int n
) {
1072 return n ?
"true" : "false";
1075 static char *expandarg(const char *arg
, dcgi_state
*ds
) {
1081 output
.sink
= sink_dynstr(&d
);
1082 expandstring(&output
, arg
, ds
);
1083 dynstr_terminate(&d
);
1087 static void exp_prefs(int attribute((unused
)) nargs
,
1092 dcgi_state substate
;
1094 const char *file
= expandarg(args
[0], ds
);
1096 memset(&substate
, 0, sizeof substate
);
1099 if(disorder_prefs(ds
->g
->client
, file
, &k
)) return;
1101 substate
.last
= !k
->next
;
1103 expandstring(output
, args
[1], &substate
);
1110 static void exp_pref(int attribute((unused
)) nargs
,
1117 if(!disorder_get(ds
->g
->client
, args
[0], args
[1], &value
))
1118 cgi_output(output
, "%s", value
);
1121 static void exp_if(int nargs
,
1126 int n
= str2bool(expandarg(args
[0], ds
)) ?
1 : 2;
1129 expandstring(output
, args
[n
], ds
);
1132 static void exp_and(int nargs
,
1139 for(n
= 0; n
< nargs
; ++n
)
1140 if(!str2bool(expandarg(args
[n
], ds
))) {
1144 sink_printf(output
->sink
, "%s", bool2str(result
));
1147 static void exp_or(int nargs
,
1154 for(n
= 0; n
< nargs
; ++n
)
1155 if(str2bool(expandarg(args
[n
], ds
))) {
1159 sink_printf(output
->sink
, "%s", bool2str(result
));
1162 static void exp_not(int attribute((unused
)) nargs
,
1165 void attribute((unused
)) *u
) {
1166 sink_printf(output
->sink
, "%s", bool2str(!str2bool(args
[0])));
1169 static void exp_isplaying(int attribute((unused
)) nargs
,
1170 char attribute((unused
)) **args
,
1175 lookups(ds
, DC_PLAYING
);
1176 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->playing
));
1179 static void exp_isqueue(int attribute((unused
)) nargs
,
1180 char attribute((unused
)) **args
,
1185 lookups(ds
, DC_QUEUE
);
1186 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->queue
));
1189 static void exp_isrecent(int attribute((unused
)) nargs
,
1190 char attribute((unused
)) **args
,
1195 lookups(ds
, DC_RECENT
);
1196 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->recent
));
1199 static void exp_isnew(int attribute((unused
)) nargs
,
1200 char attribute((unused
)) **args
,
1205 lookups(ds
, DC_NEW
);
1206 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->nnew
));
1209 static void exp_id(int attribute((unused
)) nargs
,
1210 char attribute((unused
)) **args
,
1216 cgi_output(output
, "%s", ds
->track
->id
);
1219 static void exp_track(int attribute((unused
)) nargs
,
1220 char attribute((unused
)) **args
,
1226 cgi_output(output
, "%s", ds
->track
->track
);
1229 static void exp_parity(int attribute((unused
)) nargs
,
1230 char attribute((unused
)) **args
,
1235 cgi_output(output
, "%s", ds
->index
% 2 ?
"odd" : "even");
1238 static void exp_comment(int attribute((unused
)) nargs
,
1239 char attribute((unused
)) **args
,
1240 cgi_sink
attribute((unused
)) *output
,
1241 void attribute((unused
)) *u
) {
1245 static void exp_prefname(int attribute((unused
)) nargs
,
1246 char attribute((unused
)) **args
,
1251 if(ds
->pref
&& ds
->pref
->name
)
1252 cgi_output(output
, "%s", ds
->pref
->name
);
1255 static void exp_prefvalue(int attribute((unused
)) nargs
,
1256 char attribute((unused
)) **args
,
1261 if(ds
->pref
&& ds
->pref
->value
)
1262 cgi_output(output
, "%s", ds
->pref
->value
);
1265 static void exp_isfiles(int attribute((unused
)) nargs
,
1266 char attribute((unused
)) **args
,
1271 lookups(ds
, DC_FILES
);
1272 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->nfiles
));
1275 static void exp_isdirectories(int attribute((unused
)) nargs
,
1276 char attribute((unused
)) **args
,
1281 lookups(ds
, DC_DIRS
);
1282 sink_printf(output
->sink
, "%s", bool2str(!!ds
->g
->ndirs
));
1285 static void exp_choose(int attribute((unused
)) nargs
,
1290 dcgi_state substate
;
1294 const char *type
, *what
= expandarg(args
[0], ds
);
1296 if(!strcmp(what
, "files")) {
1297 lookups(ds
, DC_FILES
);
1298 files
= ds
->g
->files
;
1299 nfiles
= ds
->g
->nfiles
;
1301 } else if(!strcmp(what
, "directories")) {
1302 lookups(ds
, DC_DIRS
);
1303 files
= ds
->g
->dirs
;
1304 nfiles
= ds
->g
->ndirs
;
1307 error(0, "unknown @choose@ argument '%s'", what
);
1310 e
= xmalloc(nfiles
* sizeof (struct entry
));
1311 for(n
= 0; n
< nfiles
; ++n
) {
1312 e
[n
].path
= files
[n
];
1313 e
[n
].sort
= trackname_transform(type
, files
[n
], "sort");
1314 e
[n
].display
= trackname_transform(type
, files
[n
], "display");
1316 qsort(e
, nfiles
, sizeof (struct entry
), compare_entry
);
1317 memset(&substate
, 0, sizeof substate
);
1320 for(n
= 0; n
< nfiles
; ++n
) {
1321 substate
.last
= (n
== nfiles
- 1);
1323 substate
.entry
= &e
[n
];
1324 expandstring(output
, args
[1], &substate
);
1329 static void exp_file(int attribute((unused
)) nargs
,
1330 char attribute((unused
)) **args
,
1336 cgi_output(output
, "%s", ds
->entry
->path
);
1338 cgi_output(output
, "%s", ds
->track
->track
);
1340 cgi_output(output
, "%s", ds
->tracks
[0]);
1343 static void exp_transform(int nargs
,
1346 void attribute((unused
)) *u
) {
1347 const char *context
= nargs
> 2 ? args
[2] : "display";
1349 cgi_output(output
, "%s", trackname_transform(args
[1], args
[0], context
));
1352 static void exp_urlquote(int attribute((unused
)) nargs
,
1355 void attribute((unused
)) *u
) {
1356 cgi_output(output
, "%s", urlencodestring(args
[0]));
1359 static void exp_scratchable(int attribute((unused
)) nargs
,
1360 char attribute((unused
)) **args
,
1362 void attribute((unused
)) *u
) {
1365 lookups(ds
, DC_PLAYING
|DC_RIGHTS
);
1366 sink_printf(output
->sink
, "%s",
1367 bool2str(right_scratchable(ds
->g
->rights
,
1368 disorder_user(ds
->g
->client
),
1372 static void exp_removable(int attribute((unused
)) nargs
,
1373 char attribute((unused
)) **args
,
1375 void attribute((unused
)) *u
) {
1378 lookups(ds
, DC_RIGHTS
);
1379 sink_printf(output
->sink
, "%s",
1380 bool2str(right_removable(ds
->g
->rights
,
1381 disorder_user(ds
->g
->client
),
1385 static void exp_movable(int attribute((unused
)) nargs
,
1386 char attribute((unused
)) **args
,
1388 void attribute((unused
)) *u
) {
1391 lookups(ds
, DC_RIGHTS
);
1392 sink_printf(output
->sink
, "%s",
1393 bool2str(right_movable(ds
->g
->rights
,
1394 disorder_user(ds
->g
->client
),
1398 static void exp_navigate(int attribute((unused
)) nargs
,
1403 dcgi_state substate
;
1404 const char *path
= expandarg(args
[0], ds
);
1409 memset(&substate
, 0, sizeof substate
);
1411 ptr
= path
+ 1; /* skip root */
1413 substate
.nav_path
= path
;
1416 while(*ptr
&& *ptr
!= '/')
1418 substate
.last
= !*ptr
;
1419 substate
.nav_len
= ptr
- path
;
1420 substate
.nav_dirlen
= dirlen
;
1421 expandstring(output
, args
[1], &substate
);
1422 dirlen
= substate
.nav_len
;
1429 static void exp_fullname(int attribute((unused
)) nargs
,
1430 char attribute((unused
)) **args
,
1434 cgi_output(output
, "%.*s", ds
->nav_len
, ds
->nav_path
);
1437 static void exp_basename(int nargs
,
1445 if((s
= strrchr(args
[0], '/'))) ++s
;
1447 cgi_output(output
, "%s", s
);
1449 cgi_output(output
, "%.*s", ds
->nav_len
- ds
->nav_dirlen
- 1,
1450 ds
->nav_path
+ ds
->nav_dirlen
+ 1);
1453 static void exp_dirname(int nargs
,
1461 if((s
= strrchr(args
[0], '/')))
1462 cgi_output(output
, "%.*s", (int)(s
- args
[0]), args
[0]);
1464 cgi_output(output
, "%.*s", ds
->nav_dirlen
, ds
->nav_path
);
1467 static void exp_eq(int attribute((unused
)) nargs
,
1470 void attribute((unused
)) *u
) {
1471 cgi_output(output
, "%s", bool2str(!strcmp(args
[0], args
[1])));
1474 static void exp_ne(int attribute((unused
)) nargs
,
1477 void attribute((unused
)) *u
) {
1478 cgi_output(output
, "%s", bool2str(strcmp(args
[0], args
[1])));
1481 static void exp_enabled(int attribute((unused
)) nargs
,
1482 char attribute((unused
)) **args
,
1489 disorder_enabled(ds
->g
->client
, &enabled
);
1490 cgi_output(output
, "%s", bool2str(enabled
));
1493 static void exp_random_enabled(int attribute((unused
)) nargs
,
1494 char attribute((unused
)) **args
,
1501 disorder_random_enabled(ds
->g
->client
, &enabled
);
1502 cgi_output(output
, "%s", bool2str(enabled
));
1505 static void exp_trackstate(int attribute((unused
)) nargs
,
1510 struct queue_entry
*q
;
1513 if(disorder_resolve(ds
->g
->client
, &track
, args
[0])) return;
1514 lookups(ds
, DC_QUEUE
|DC_PLAYING
);
1515 if(ds
->g
->playing
&& !strcmp(ds
->g
->playing
->track
, track
))
1516 cgi_output(output
, "playing");
1518 for(q
= ds
->g
->queue
; q
&& strcmp(q
->track
, track
); q
= q
->next
)
1521 cgi_output(output
, "queued");
1525 static void exp_thisurl(int attribute((unused
)) nargs
,
1526 char attribute((unused
)) **args
,
1528 void attribute((unused
)) *u
) {
1529 kvp_set(&cgi_args
, "nonce", nonce()); /* nonces had better differ! */
1530 cgi_output(output
, "%s?%s", config
->url
, kvp_urlencode(cgi_args
, 0));
1533 static void exp_isfirst(int attribute((unused
)) nargs
,
1534 char attribute((unused
)) **args
,
1539 sink_printf(output
->sink
, "%s", bool2str(!!ds
->first
));
1542 static void exp_islast(int attribute((unused
)) nargs
,
1543 char attribute((unused
)) **args
,
1548 sink_printf(output
->sink
, "%s", bool2str(!!ds
->last
));
1551 static void exp_action(int attribute((unused
)) nargs
,
1552 char attribute((unused
)) **args
,
1554 void attribute((unused
)) *u
) {
1555 const char *action
= cgi_get("action"), *mgmt
;
1557 if(!action
) action
= "playing";
1558 if(!strcmp(action
, "playing")
1559 && (mgmt
= cgi_get("mgmt"))
1560 && !strcmp(mgmt
, "true"))
1562 sink_printf(output
->sink
, "%s", action
);
1565 static void exp_resolve(int attribute((unused
)) nargs
,
1568 void attribute((unused
)) *u
) {
1572 if(!disorder_resolve(ds
->g
->client
, &track
, args
[0]))
1573 sink_printf(output
->sink
, "%s", track
);
1576 static void exp_paused(int attribute((unused
)) nargs
,
1577 char attribute((unused
)) **args
,
1583 lookups(ds
, DC_PLAYING
);
1584 if(ds
->g
->playing
&& ds
->g
->playing
->state
== playing_paused
)
1586 cgi_output(output
, "%s", bool2str(paused
));
1589 static void exp_state(int attribute((unused
)) nargs
,
1590 char attribute((unused
)) **args
,
1596 cgi_output(output
, "%s", playing_states
[ds
->track
->state
]);
1599 static void exp_files(int attribute((unused
)) nargs
,
1604 dcgi_state substate
;
1605 const char *nfiles_arg
, *directory
;
1606 int nfiles
, numfile
;
1609 memset(&substate
, 0, sizeof substate
);
1611 if((directory
= cgi_get("directory"))) {
1612 /* Prefs for whole directory. */
1613 lookups(ds
, DC_FILES
);
1614 /* Synthesize args for the file list. */
1615 nfiles
= ds
->g
->nfiles
;
1616 for(numfile
= 0; numfile
< nfiles
; ++numfile
) {
1617 k
= xmalloc(sizeof *k
);
1618 byte_xasprintf((char **)&k
->name
, "%d_file", numfile
);
1619 k
->value
= ds
->g
->files
[numfile
];
1624 /* Args already present. */
1625 if((nfiles_arg
= cgi_get("files"))) nfiles
= atoi(nfiles_arg
);
1628 for(numfile
= 0; numfile
< nfiles
; ++numfile
) {
1629 substate
.index
= numfile
;
1630 expandstring(output
, args
[0], &substate
);
1634 static void exp_index(int attribute((unused
)) nargs
,
1635 char attribute((unused
)) **args
,
1640 cgi_output(output
, "%d", ds
->index
);
1643 static void exp_nfiles(int attribute((unused
)) nargs
,
1644 char attribute((unused
)) **args
,
1648 const char *files_arg
;
1650 if(cgi_get("directory")) {
1651 lookups(ds
, DC_FILES
);
1652 cgi_output(output
, "%d", ds
->g
->nfiles
);
1653 } else if((files_arg
= cgi_get("files")))
1654 cgi_output(output
, "%s", files_arg
);
1656 cgi_output(output
, "1");
1659 static void exp_user(int attribute((unused
)) nargs
,
1660 char attribute((unused
)) **args
,
1663 dcgi_state
*const ds
= u
;
1665 cgi_output(output
, "%s", disorder_user(ds
->g
->client
));
1668 static void exp_right(int attribute((unused
)) nargs
,
1672 dcgi_state
*const ds
= u
;
1673 const char *right
= expandarg(args
[0], ds
);
1676 lookups(ds
, DC_RIGHTS
);
1677 if(parse_rights(right
, &r
, 1/*report*/))
1680 cgi_output(output
, "%s", bool2str(!!(r
& ds
->g
->rights
)));
1681 else if(r
& ds
->g
->rights
)
1682 expandstring(output
, args
[1], ds
);
1684 expandstring(output
, args
[2], ds
);
1687 static void exp_userinfo(int attribute((unused
)) nargs
,
1691 dcgi_state
*const ds
= u
;
1694 if(disorder_userinfo(ds
->g
->client
, disorder_user(ds
->g
->client
), args
[0],
1697 cgi_output(output
, "%s", value
);
1700 static const struct cgi_expansion expansions
[] = {
1701 { "#", 0, INT_MAX
, EXP_MAGIC
, exp_comment
},
1702 { "action", 0, 0, 0, exp_action
},
1703 { "and", 0, INT_MAX
, EXP_MAGIC
, exp_and
},
1704 { "arg", 1, 1, 0, exp_arg
},
1705 { "basename", 0, 1, 0, exp_basename
},
1706 { "choose", 2, 2, EXP_MAGIC
, exp_choose
},
1707 { "dirname", 0, 1, 0, exp_dirname
},
1708 { "enabled", 0, 0, 0, exp_enabled
},
1709 { "eq", 2, 2, 0, exp_eq
},
1710 { "file", 0, 0, 0, exp_file
},
1711 { "files", 1, 1, EXP_MAGIC
, exp_files
},
1712 { "fullname", 0, 0, 0, exp_fullname
},
1713 { "id", 0, 0, 0, exp_id
},
1714 { "if", 2, 3, EXP_MAGIC
, exp_if
},
1715 { "include", 1, 1, 0, exp_include
},
1716 { "index", 0, 0, 0, exp_index
},
1717 { "isdirectories", 0, 0, 0, exp_isdirectories
},
1718 { "isfiles", 0, 0, 0, exp_isfiles
},
1719 { "isfirst", 0, 0, 0, exp_isfirst
},
1720 { "islast", 0, 0, 0, exp_islast
},
1721 { "isnew", 0, 0, 0, exp_isnew
},
1722 { "isplaying", 0, 0, 0, exp_isplaying
},
1723 { "isqueue", 0, 0, 0, exp_isqueue
},
1724 { "isrecent", 0, 0, 0, exp_isrecent
},
1725 { "label", 1, 1, 0, exp_label
},
1726 { "length", 0, 0, 0, exp_length
},
1727 { "movable", 0, 0, 0, exp_movable
},
1728 { "navigate", 2, 2, EXP_MAGIC
, exp_navigate
},
1729 { "ne", 2, 2, 0, exp_ne
},
1730 { "new", 1, 1, EXP_MAGIC
, exp_new
},
1731 { "nfiles", 0, 0, 0, exp_nfiles
},
1732 { "nonce", 0, 0, 0, exp_nonce
},
1733 { "not", 1, 1, 0, exp_not
},
1734 { "or", 0, INT_MAX
, EXP_MAGIC
, exp_or
},
1735 { "parity", 0, 0, 0, exp_parity
},
1736 { "part", 1, 3, 0, exp_part
},
1737 { "paused", 0, 0, 0, exp_paused
},
1738 { "playing", 1, 1, EXP_MAGIC
, exp_playing
},
1739 { "pref", 2, 2, 0, exp_pref
},
1740 { "prefname", 0, 0, 0, exp_prefname
},
1741 { "prefs", 2, 2, EXP_MAGIC
, exp_prefs
},
1742 { "prefvalue", 0, 0, 0, exp_prefvalue
},
1743 { "queue", 1, 1, EXP_MAGIC
, exp_queue
},
1744 { "random-enabled", 0, 0, 0, exp_random_enabled
},
1745 { "recent", 1, 1, EXP_MAGIC
, exp_recent
},
1746 { "removable", 0, 0, 0, exp_removable
},
1747 { "resolve", 1, 1, 0, exp_resolve
},
1748 { "right", 1, 3, EXP_MAGIC
, exp_right
},
1749 { "scratchable", 0, 0, 0, exp_scratchable
},
1750 { "search", 2, 3, EXP_MAGIC
, exp_search
},
1751 { "server-version", 0, 0, 0, exp_server_version
},
1752 { "shell", 1, 1, 0, exp_shell
},
1753 { "state", 0, 0, 0, exp_state
},
1754 { "stats", 0, 0, 0, exp_stats
},
1755 { "thisurl", 0, 0, 0, exp_thisurl
},
1756 { "track", 0, 0, 0, exp_track
},
1757 { "trackstate", 1, 1, 0, exp_trackstate
},
1758 { "transform", 2, 3, 0, exp_transform
},
1759 { "url", 0, 0, 0, exp_url
},
1760 { "urlquote", 1, 1, 0, exp_urlquote
},
1761 { "user", 0, 0, 0, exp_user
},
1762 { "userinfo", 1, 1, 0, exp_userinfo
},
1763 { "version", 0, 0, 0, exp_version
},
1764 { "volume", 1, 1, 0, exp_volume
},
1765 { "when", 0, 0, 0, exp_when
},
1766 { "who", 0, 0, 0, exp_who
}
1769 static void expand(cgi_sink
*output
,
1770 const char *template,
1772 cgi_expand(template,
1773 expansions
, sizeof expansions
/ sizeof *expansions
,
1778 static void expandstring(cgi_sink
*output
,
1781 cgi_expand_string("",
1783 expansions
, sizeof expansions
/ sizeof *expansions
,
1788 static void perform_action(cgi_sink
*output
, dcgi_state
*ds
,
1789 const char *action
) {
1792 /* We don't ever want anything to be cached */
1793 cgi_header(output
->sink
, "Cache-Control", "no-cache");
1794 if((n
= TABLE_FIND(actions
, struct action
, name
, action
)) >= 0)
1795 actions
[n
].handler(output
, ds
);
1797 expand_template(ds
, output
, action
);
1800 void disorder_cgi(cgi_sink
*output
, dcgi_state
*ds
) {
1801 const char *action
= cgi_get("action");
1804 /* We allow URLs which are just confirm=... in order to keep confirmation
1805 * URLs, which are user-facing, as short as possible. */
1811 perform_action(output
, ds
, action
);
1814 void disorder_cgi_error(cgi_sink
*output
, dcgi_state
*ds
,
1816 cgi_set_option("error", msg
);
1817 perform_action(output
, ds
, "error");
1820 /** @brief Log in as the current user or guest if none */
1821 void disorder_cgi_login(dcgi_state
*ds
, cgi_sink
*output
) {
1822 /* Create a new connection */
1823 ds
->g
->client
= disorder_new(0);
1824 /* Forget everything we knew */
1827 if(disorder_connect_cookie(ds
->g
->client
, login_cookie
)) {
1828 disorder_cgi_error(output
, ds
, "connect");
1831 /* If there was a cookie but it went bad, we forget it */
1832 if(login_cookie
&& !strcmp(disorder_user(ds
->g
->client
), "guest"))