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
25 #include <sys/types.h>
26 #include <sys/socket.h>
40 #include "configuration.h"
54 #include "authorize.h"
57 static disorder_client
*client
;
59 static const struct option options
[] = {
60 { "help", no_argument
, 0, 'h' },
61 { "version", no_argument
, 0, 'V' },
62 { "config", required_argument
, 0, 'c' },
63 { "debug", no_argument
, 0, 'd' },
64 { "local", no_argument
, 0, 'l' },
65 { "no-per-user-config", no_argument
, 0, 'N' },
66 { "help-commands", no_argument
, 0, 'H' },
67 { "user", required_argument
, 0, 'u' },
68 { "password", required_argument
, 0, 'p' },
72 /* display usage message and terminate */
73 static void help(void) {
75 " disorder [OPTIONS] COMMAND ...\n"
77 " --help, -h Display usage message\n"
78 " --help-commands, -H List commands\n"
79 " --version, -V Display version number\n"
80 " --config PATH, -c PATH Set configuration file\n"
81 " --local, -l Force connection to local server\n"
82 " --debug, -d Turn on debugging\n");
87 /* display version number and terminate */
88 static void version(void) {
89 xprintf("%s", disorder_version_string
);
94 static disorder_client
*getclient(void) {
96 if(!(client
= disorder_new(1))) exit(EXIT_FAILURE
);
97 if(disorder_connect(client
)) exit(EXIT_FAILURE
);
102 static void cf_version(char attribute((unused
)) **argv
) {
105 if(disorder_version(getclient(), &v
)) exit(EXIT_FAILURE
);
106 xprintf("%s\n", nullcheck(utf82mb(v
)));
109 static void print_queue_entry(const struct queue_entry
*q
) {
110 if(q
->track
) xprintf("track %s\n", nullcheck(utf82mb(q
->track
)));
111 if(q
->id
) xprintf(" id %s\n", nullcheck(utf82mb(q
->id
)));
112 if(q
->submitter
) xprintf(" submitted by %s at %s",
113 nullcheck(utf82mb(q
->submitter
)), ctime(&q
->when
));
114 if(q
->played
) xprintf(" played at %s", ctime(&q
->played
));
115 if(q
->state
== playing_started
116 || q
->state
== playing_paused
) xprintf(" %lds so far", q
->sofar
);
117 else if(q
->expected
) xprintf(" might start at %s", ctime(&q
->expected
));
118 if(q
->scratched
) xprintf(" scratched by %s\n",
119 nullcheck(utf82mb(q
->scratched
)));
120 else xprintf(" %s\n", playing_states
[q
->state
]);
121 if(q
->wstat
) xprintf(" %s\n", wstat(q
->wstat
));
124 static void cf_playing(char attribute((unused
)) **argv
) {
125 struct queue_entry
*q
;
127 if(disorder_playing(getclient(), &q
)) exit(EXIT_FAILURE
);
129 print_queue_entry(q
);
131 xprintf("nothing\n");
134 static void cf_play(char **argv
) {
136 if(disorder_play(getclient(), *argv
++)) exit(EXIT_FAILURE
);
139 static void cf_remove(char **argv
) {
140 if(disorder_remove(getclient(), argv
[0])) exit(EXIT_FAILURE
);
143 static void cf_disable(char attribute((unused
)) **argv
) {
144 if(disorder_disable(getclient())) exit(EXIT_FAILURE
);
147 static void cf_enable(char attribute((unused
)) **argv
) {
148 if(disorder_enable(getclient())) exit(EXIT_FAILURE
);
151 static void cf_scratch(char **argv
) {
152 if(disorder_scratch(getclient(), argv
[0])) exit(EXIT_FAILURE
);
155 static void cf_shutdown(char attribute((unused
)) **argv
) {
156 if(disorder_shutdown(getclient())) exit(EXIT_FAILURE
);
159 static void cf_reconfigure(char attribute((unused
)) **argv
) {
160 /* Re-check configuration for server */
161 if(config_read(1)) fatal(0, "cannot read configuration");
162 if(disorder_reconfigure(getclient())) exit(EXIT_FAILURE
);
165 static void cf_rescan(char attribute((unused
)) **argv
) {
166 if(disorder_rescan(getclient())) exit(EXIT_FAILURE
);
169 static void cf_somequeue(int (*fn
)(disorder_client
*c
,
170 struct queue_entry
**qp
)) {
171 struct queue_entry
*q
;
173 if(fn(getclient(), &q
)) exit(EXIT_FAILURE
);
175 print_queue_entry(q
);
180 static void cf_recent(char attribute((unused
)) **argv
) {
181 cf_somequeue(disorder_recent
);
184 static void cf_queue(char attribute((unused
)) **argv
) {
185 cf_somequeue(disorder_queue
);
188 static void cf_quack(char attribute((unused
)) **argv
) {
190 " .------------------.\n"
191 " | Naath is a babe! |\n"
192 " `---------+--------'\n"
196 "~~~~~~~~~~~~~~~~~~~~~~\n"
200 static void cf_somelist(char **argv
,
201 int (*fn
)(disorder_client
*c
,
202 const char *arg
, const char *re
,
203 char ***vecp
, int *nvecp
)) {
208 re
= xstrdup(argv
[1] + 1);
211 if(fn(getclient(), argv
[0], re
, &vec
, 0)) exit(EXIT_FAILURE
);
213 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
216 static int isarg_regexp(const char *s
) {
220 static void cf_dirs(char **argv
) {
221 cf_somelist(argv
, disorder_directories
);
224 static void cf_files(char **argv
) {
225 cf_somelist(argv
, disorder_files
);
228 static void cf_allfiles(char **argv
) {
229 cf_somelist(argv
, disorder_allfiles
);
232 static void cf_get(char **argv
) {
235 if(disorder_get(getclient(), argv
[0], argv
[1], &value
)) exit(EXIT_FAILURE
);
236 xprintf("%s\n", nullcheck(utf82mb(value
)));
239 static void cf_length(char **argv
) {
242 if(disorder_length(getclient(), argv
[0], &length
)) exit(EXIT_FAILURE
);
243 xprintf("%ld\n", length
);
246 static void cf_set(char **argv
) {
247 if(disorder_set(getclient(), argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
250 static void cf_unset(char **argv
) {
251 if(disorder_unset(getclient(), argv
[0], argv
[1])) exit(EXIT_FAILURE
);
254 static void cf_prefs(char **argv
) {
257 if(disorder_prefs(getclient(), argv
[0], &k
)) exit(EXIT_FAILURE
);
258 for(; k
; k
= k
->next
)
260 nullcheck(utf82mb(k
->name
)), nullcheck(utf82mb(k
->value
)));
263 static void cf_search(char **argv
) {
267 if(disorder_search(getclient(), *argv
, &results
, &nresults
)) exit(EXIT_FAILURE
);
268 for(n
= 0; n
< nresults
; ++n
)
269 xprintf("%s\n", nullcheck(utf82mb(results
[n
])));
272 static void cf_random_disable(char attribute((unused
)) **argv
) {
273 if(disorder_random_disable(getclient())) exit(EXIT_FAILURE
);
276 static void cf_random_enable(char attribute((unused
)) **argv
) {
277 if(disorder_random_enable(getclient())) exit(EXIT_FAILURE
);
280 static void cf_stats(char attribute((unused
)) **argv
) {
283 if(disorder_stats(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
285 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
288 static void cf_get_volume(char attribute((unused
)) **argv
) {
291 if(disorder_get_volume(getclient(), &l
, &r
)) exit(EXIT_FAILURE
);
292 xprintf("%d %d\n", l
, r
);
295 static void cf_set_volume(char **argv
) {
296 if(disorder_set_volume(getclient(), atoi(argv
[0]), atoi(argv
[1]))) exit(EXIT_FAILURE
);
299 static void cf_log(char attribute((unused
)) **argv
) {
300 if(disorder_log(getclient(), sink_stdio("stdout", stdout
))) exit(EXIT_FAILURE
);
303 static void cf_move(char **argv
) {
307 if((e
= xstrtol(&n
, argv
[1], 0, 10)))
308 fatal(e
, "cannot convert '%s'", argv
[1]);
309 if(n
> INT_MAX
|| n
< INT_MIN
)
310 fatal(e
, "%ld out of range", n
);
311 if(disorder_move(getclient(), argv
[0], (int)n
)) exit(EXIT_FAILURE
);
314 static void cf_part(char **argv
) {
317 if(disorder_part(getclient(), &s
, argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
318 xprintf("%s\n", nullcheck(utf82mb(s
)));
321 static int isarg_filename(const char *s
) {
325 static void cf_authorize(char **argv
) {
326 authorize(getclient(), argv
[0], argv
[1]);
329 static void cf_resolve(char **argv
) {
332 if(disorder_resolve(getclient(), &track
, argv
[0])) exit(EXIT_FAILURE
);
333 xprintf("%s\n", nullcheck(utf82mb(track
)));
336 static void cf_pause(char attribute((unused
)) **argv
) {
337 if(disorder_pause(getclient())) exit(EXIT_FAILURE
);
340 static void cf_resume(char attribute((unused
)) **argv
) {
341 if(disorder_resume(getclient())) exit(EXIT_FAILURE
);
344 static void cf_tags(char attribute((unused
)) **argv
) {
347 if(disorder_tags(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
349 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
352 static void cf_users(char attribute((unused
)) **argv
) {
355 if(disorder_users(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
357 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
360 static void cf_get_global(char **argv
) {
363 if(disorder_get_global(getclient(), argv
[0], &value
)) exit(EXIT_FAILURE
);
364 xprintf("%s\n", nullcheck(utf82mb(value
)));
367 static void cf_set_global(char **argv
) {
368 if(disorder_set_global(getclient(), argv
[0], argv
[1])) exit(EXIT_FAILURE
);
371 static void cf_unset_global(char **argv
) {
372 if(disorder_unset_global(getclient(), argv
[0])) exit(EXIT_FAILURE
);
375 static int isarg_integer(const char *s
) {
378 if(!isdigit((unsigned char)*s
))
385 static void cf_new(char **argv
) {
388 if(disorder_new_tracks(getclient(), &vec
, 0, argv
[0] ?
atoi(argv
[0]) : 0))
391 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
394 static void cf_rtp_address(char attribute((unused
)) **argv
) {
395 char *address
, *port
;
397 if(disorder_rtp_address(getclient(), &address
, &port
)) exit(EXIT_FAILURE
);
398 xprintf("address: %s\nport: %s\n", address
, port
);
401 static int isarg_rights(const char *arg
) {
402 return strchr(arg
, ',') || !parse_rights(arg
, 0, 0);
405 static void cf_adduser(char **argv
) {
406 if(disorder_adduser(getclient(), argv
[0], argv
[1], argv
[2]))
410 static void cf_deluser(char **argv
) {
411 if(disorder_deluser(getclient(), argv
[0]))
415 static void cf_edituser(char **argv
) {
416 if(disorder_edituser(getclient(), argv
[0], argv
[1], argv
[2]))
420 static void cf_userinfo(char **argv
) {
423 if(disorder_userinfo(getclient(), argv
[0], argv
[1], &s
))
425 xprintf("%s\n", nullcheck(utf82mb(s
)));
428 static int isarg_option(const char *arg
) {
429 return arg
[0] == '-';
432 static int argvlen(char **argv
) {
440 static const struct option setup_guest_options
[] = {
441 { "help", no_argument
, 0, 'h' },
442 { "online-registration", no_argument
, 0, 'r' },
443 { "no-online-registration", no_argument
, 0, 'R' },
447 static void help_setup_guest(void) {
449 " disorder setup-guest [OPTIONS]\n"
451 " --help, -h Display usage message\n"
452 " --online-registration Enable online registration (default)\n"
453 " --no-online-registration Disable online registration\n");
458 static void cf_setup_guest(char **argv
) {
459 int n
, online_registration
= 1;
461 while((n
= getopt_long(argvlen(argv
) + 1, argv
- 1,
462 "hrR", setup_guest_options
, 0)) >= 0) {
464 case 'h': help_setup_guest();
465 case 'r': online_registration
= 1; break;
466 case 'R': online_registration
= 0; break;
467 default: fatal(0, "invalid option");
470 if(online_registration
&& !config
->mail_sender
)
471 fatal(0, "you MUST set mail_sender if you want online registration");
472 if(disorder_adduser(getclient(), "guest", "",
473 online_registration ?
"read,register" : "read"))
477 static const struct command
{
481 int (*isarg
)(const char *);
482 const char *argstr
, *desc
;
484 { "adduser", 2, 3, cf_adduser
, isarg_rights
, "USER PASSWORD [RIGHTS]",
485 "Create a new user" },
486 { "allfiles", 1, 2, cf_allfiles
, isarg_regexp
, "DIR [~REGEXP]",
487 "List all files and directories in DIR" },
488 { "authorize", 1, 2, cf_authorize
, isarg_rights
, "USER [RIGHTS]",
489 "Authorize USER to connect to the server" },
490 { "deluser", 1, 1, cf_deluser
, 0, "USER",
492 { "dirs", 1, 2, cf_dirs
, isarg_regexp
, "DIR [~REGEXP]",
493 "List directories in DIR" },
494 { "disable", 0, 0, cf_disable
, 0, "",
496 { "disable-random", 0, 0, cf_random_disable
, 0, "",
497 "Disable random play" },
498 { "edituser", 3, 3, cf_edituser
, 0, "USER PROPERTY VALUE",
499 "Set a property of a user" },
500 { "enable", 0, 0, cf_enable
, 0, "",
502 { "enable-random", 0, 0, cf_random_enable
, 0, "",
503 "Enable random play" },
504 { "files", 1, 2, cf_files
, isarg_regexp
, "DIR [~REGEXP]",
505 "List files in DIR" },
506 { "get", 2, 2, cf_get
, 0, "TRACK NAME",
507 "Get a preference value" },
508 { "get-global", 1, 1, cf_get_global
, 0, "NAME",
509 "Get a global preference value" },
510 { "get-volume", 0, 0, cf_get_volume
, 0, "",
511 "Get the current volume" },
512 { "length", 1, 1, cf_length
, 0, "TRACK",
513 "Get the length of TRACK in seconds" },
514 { "log", 0, 0, cf_log
, 0, "",
515 "Copy event log to stdout" },
516 { "move", 2, 2, cf_move
, 0, "TRACK DELTA",
517 "Move a track in the queue" },
518 { "new", 0, 1, cf_new
, isarg_integer
, "[MAX]",
519 "Get the most recently added MAX tracks" },
520 { "part", 3, 3, cf_part
, 0, "TRACK CONTEXT PART",
521 "Find a track name part" },
522 { "pause", 0, 0, cf_pause
, 0, "",
523 "Pause the currently playing track" },
524 { "play", 1, INT_MAX
, cf_play
, isarg_filename
, "TRACKS...",
525 "Add TRACKS to the end of the queue" },
526 { "playing", 0, 0, cf_playing
, 0, "",
527 "Report the playing track" },
528 { "prefs", 1, 1, cf_prefs
, 0, "TRACK",
529 "Display all the preferences for TRACK" },
530 { "quack", 0, 0, cf_quack
, 0, 0, 0 },
531 { "queue", 0, 0, cf_queue
, 0, "",
532 "Display the current queue" },
533 { "random-disable", 0, 0, cf_random_disable
, 0, "",
534 "Disable random play" },
535 { "random-enable", 0, 0, cf_random_enable
, 0, "",
536 "Enable random play" },
537 { "recent", 0, 0, cf_recent
, 0, "",
538 "Display recently played track" },
539 { "reconfigure", 0, 0, cf_reconfigure
, 0, "",
540 "Reconfigure the daemon" },
541 { "remove", 1, 1, cf_remove
, 0, "TRACK",
542 "Remove a track from the queue" },
543 { "rescan", 0, 0, cf_rescan
, 0, "",
544 "Rescan for new tracks" },
545 { "resolve", 1, 1, cf_resolve
, 0, "TRACK",
546 "Resolve alias for TRACK" },
547 { "resume", 0, 0, cf_resume
, 0, "",
548 "Resume after a pause" },
549 { "rtp-address", 0, 0, cf_rtp_address
, 0, "",
550 "Report server's broadcast address" },
551 { "scratch", 0, 0, cf_scratch
, 0, "",
552 "Scratch the currently playing track" },
553 { "scratch-id", 1, 1, cf_scratch
, 0, "ID",
554 "Scratch the currently playing track" },
555 { "search", 1, 1, cf_search
, 0, "WORDS",
556 "Display tracks matching all the words" },
557 { "set", 3, 3, cf_set
, 0, "TRACK NAME VALUE",
558 "Set a preference value" },
559 { "set-global", 2, 2, cf_set_global
, 0, "NAME VALUE",
560 "Set a global preference value" },
561 { "set-volume", 2, 2, cf_set_volume
, 0, "LEFT RIGHT",
563 { "setup-guest", 0, INT_MAX
, cf_setup_guest
, isarg_option
, "[OPTIONS]",
564 "Create the guest login" },
565 { "shutdown", 0, 0, cf_shutdown
, 0, "",
566 "Shut down the daemon" },
567 { "stats", 0, 0, cf_stats
, 0, "",
568 "Display server statistics" },
569 { "tags", 0, 0, cf_tags
, 0, "",
571 { "unset", 2, 2, cf_unset
, 0, "TRACK NAME",
572 "Unset a preference" },
573 { "unset-global", 1, 1, cf_unset_global
, 0, "NAME",
574 "Unset a global preference" },
575 { "userinfo", 2, 2, cf_userinfo
, 0, "USER PROPERTY",
576 "Get a property of as user" },
577 { "users", 0, 0, cf_users
, 0, "",
579 { "version", 0, 0, cf_version
, 0, "",
580 "Display the server version" },
583 static void help_commands(void) {
584 unsigned n
, max
= 0, l
;
586 xprintf("Command summary:\n");
587 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
588 if(!commands
[n
].desc
) continue;
589 l
= strlen(commands
[n
].name
);
590 if(*commands
[n
].argstr
)
591 l
+= strlen(commands
[n
].argstr
) + 1;
595 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
596 if(!commands
[n
].desc
) continue;
597 l
= strlen(commands
[n
].name
);
598 if(*commands
[n
].argstr
)
599 l
+= strlen(commands
[n
].argstr
) + 1;
600 xprintf(" %s%s%s%*s %s\n", commands
[n
].name
,
601 *commands
[n
].argstr ?
" " : "",
610 int main(int argc
, char **argv
) {
611 int n
, i
, j
, local
= 0;
614 const char *user
= 0, *password
= 0;
617 /* garbage-collect PCRE's memory */
618 pcre_malloc
= xmalloc
;
620 if(!setlocale(LC_CTYPE
, "")) fatal(errno
, "error calling setlocale");
621 while((n
= getopt_long(argc
, argv
, "hVc:dHlNu:p:", options
, 0)) >= 0) {
624 case 'H': help_commands();
626 case 'c': configfile
= optarg
; break;
627 case 'd': debugging
= 1; break;
628 case 'l': local
= 1; break;
629 case 'N': config_per_user
= 0; break;
630 case 'u': user
= optarg
; break;
631 case 'p': password
= optarg
; break;
632 default: fatal(0, "invalid option");
635 if(config_read(0)) fatal(0, "cannot read configuration");
637 config
->username
= user
;
638 config
->password
= 0;
641 config
->password
= password
;
643 config
->connect
.n
= 0;
645 optind
= 1; /* for subsequent getopt calls */
646 /* accumulate command args */
648 if((i
= TABLE_FIND(commands
, struct command
, name
, argv
[n
])) < 0)
649 fatal(0, "unknown command '%s'", argv
[n
]);
650 if(n
+ commands
[i
].min
>= argc
)
651 fatal(0, "missing arguments to '%s'", argv
[n
]);
653 /* Include the command name in the args, but at element -1, for
654 * the benefit of subcommand getopt calls */
655 vector_append(&args
, argv
[n
]);
657 for(j
= 0; j
< commands
[i
].min
; ++j
)
658 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
659 for(; j
< commands
[i
].max
661 && commands
[i
].isarg(argv
[n
+ j
]); ++j
)
662 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
663 vector_terminate(&args
);
664 commands
[i
].fn(args
.vec
+ 1);
667 if(client
&& disorder_close(client
)) exit(EXIT_FAILURE
);
668 if(fclose(stdout
) < 0) fatal(errno
, "error closing stdout");