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 int auto_reconfigure;
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 void cf_version(disorder_client *c,
95 char attribute((unused)) **argv) {
98 if(disorder_version(c, &v)) exit(EXIT_FAILURE);
99 xprintf("%s\n", nullcheck(utf82mb(v)));
102 static void print_queue_entry(const struct queue_entry *q) {
103 if(q->track) xprintf("track %s\n", nullcheck(utf82mb(q->track)));
104 if(q->id) xprintf(" id %s\n", nullcheck(utf82mb(q->id)));
105 if(q->submitter) xprintf(" submitted by %s at %s",
106 nullcheck(utf82mb(q->submitter)), ctime(&q->when));
107 if(q->played) xprintf(" played at %s", ctime(&q->played));
108 if(q->state == playing_started
109 || q->state == playing_paused) xprintf(" %lds so far", q->sofar);
110 else if(q->expected) xprintf(" might start at %s", ctime(&q->expected));
111 if(q->scratched) xprintf(" scratched by %s\n",
112 nullcheck(utf82mb(q->scratched)));
113 else xprintf(" %s\n", playing_states[q->state]);
114 if(q->wstat) xprintf(" %s\n", wstat(q->wstat));
117 static void cf_playing(disorder_client *c,
118 char attribute((unused)) **argv) {
119 struct queue_entry *q;
121 if(disorder_playing(c, &q)) exit(EXIT_FAILURE);
123 print_queue_entry(q);
125 xprintf("nothing\n");
128 static void cf_play(disorder_client *c, char **argv) {
130 if(disorder_play(c, *argv++)) exit(EXIT_FAILURE);
133 static void cf_remove(disorder_client *c, char **argv) {
134 if(disorder_remove(c, argv[0])) exit(EXIT_FAILURE);
137 static void cf_disable(disorder_client *c,
138 char attribute((unused)) **argv) {
139 if(disorder_disable(c)) exit(EXIT_FAILURE);
142 static void cf_enable(disorder_client *c, char attribute((unused)) **argv) {
143 if(disorder_enable(c)) exit(EXIT_FAILURE);
146 static void cf_scratch(disorder_client *c,
148 if(disorder_scratch(c, argv[0])) exit(EXIT_FAILURE);
151 static void cf_shutdown(disorder_client *c,
152 char attribute((unused)) **argv) {
153 if(disorder_shutdown(c)) exit(EXIT_FAILURE);
156 static void cf_reconfigure(disorder_client *c,
157 char attribute((unused)) **argv) {
158 /* Re-check configuration for server */
159 if(config_read(1)) fatal(0, "cannot read configuration");
160 if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
163 static void cf_rescan(disorder_client *c, char attribute((unused)) **argv) {
164 if(disorder_rescan(c)) exit(EXIT_FAILURE);
167 static void cf_somequeue(disorder_client *c,
168 int (*fn)(disorder_client *c,
169 struct queue_entry **qp)) {
170 struct queue_entry *q;
172 if(fn(c, &q)) exit(EXIT_FAILURE);
174 print_queue_entry(q);
179 static void cf_recent(disorder_client *c, char attribute((unused)) **argv) {
180 cf_somequeue(c, disorder_recent);
183 static void cf_queue(disorder_client *c, char attribute((unused)) **argv) {
184 cf_somequeue(c, disorder_queue);
187 static void cf_quack(disorder_client attribute((unused)) *c,
188 char attribute((unused)) **argv) {
190 " .------------------.\n"
191 " | Naath is a babe! |\n"
192 " `---------+--------'\n"
196 "~~~~~~~~~~~~~~~~~~~~~~\n"
200 static void cf_somelist(disorder_client *c, 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(c, 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(disorder_client *c,
222 cf_somelist(c, argv, disorder_directories);
225 static void cf_files(disorder_client *c, char **argv) {
226 cf_somelist(c, argv, disorder_files);
229 static void cf_allfiles(disorder_client *c, char **argv) {
230 cf_somelist(c, argv, disorder_allfiles);
233 static void cf_get(disorder_client *c, char **argv) {
236 if(disorder_get(c, argv[0], argv[1], &value)) exit(EXIT_FAILURE);
237 xprintf("%s\n", nullcheck(utf82mb(value)));
240 static void cf_length(disorder_client *c, char **argv) {
243 if(disorder_length(c, argv[0], &length)) exit(EXIT_FAILURE);
244 xprintf("%ld\n", length);
247 static void cf_set(disorder_client *c, char **argv) {
248 if(disorder_set(c, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
251 static void cf_unset(disorder_client *c, char **argv) {
252 if(disorder_unset(c, argv[0], argv[1])) exit(EXIT_FAILURE);
255 static void cf_prefs(disorder_client *c, char **argv) {
258 if(disorder_prefs(c, argv[0], &k)) exit(EXIT_FAILURE);
259 for(; k; k = k->next)
261 nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
264 static void cf_search(disorder_client *c, char **argv) {
268 if(disorder_search(c, *argv, &results, &nresults)) exit(EXIT_FAILURE);
269 for(n = 0; n < nresults; ++n)
270 xprintf("%s\n", nullcheck(utf82mb(results[n])));
273 static void cf_random_disable(disorder_client *c,
274 char attribute((unused)) **argv) {
275 if(disorder_random_disable(c)) exit(EXIT_FAILURE);
278 static void cf_random_enable(disorder_client *c,
279 char attribute((unused)) **argv) {
280 if(disorder_random_enable(c)) exit(EXIT_FAILURE);
283 static void cf_stats(disorder_client *c,
284 char attribute((unused)) **argv) {
287 if(disorder_stats(c, &vec, 0)) exit(EXIT_FAILURE);
289 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
292 static void cf_get_volume(disorder_client *c,
293 char attribute((unused)) **argv) {
296 if(disorder_get_volume(c, &l, &r)) exit(EXIT_FAILURE);
297 xprintf("%d %d\n", l, r);
300 static void cf_set_volume(disorder_client *c,
302 if(disorder_set_volume(c, atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE);
305 static void cf_become(disorder_client *c,
307 if(disorder_become(c, argv[0])) exit(EXIT_FAILURE);
310 static void cf_log(disorder_client *c,
311 char attribute((unused)) **argv) {
312 if(disorder_log(c, sink_stdio("stdout", stdout))) exit(EXIT_FAILURE);
315 static void cf_move(disorder_client *c,
320 if((e = xstrtol(&n, argv[1], 0, 10)))
321 fatal(e, "cannot convert '%s'", argv[1]);
322 if(n > INT_MAX || n < INT_MIN)
323 fatal(e, "%ld out of range", n);
324 if(disorder_move(c, argv[0], (int)n)) exit(EXIT_FAILURE);
327 static void cf_part(disorder_client *c,
331 if(disorder_part(c, &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
332 xprintf("%s\n", nullcheck(utf82mb(s)));
335 static int isarg_filename(const char *s) {
339 static void cf_authorize(disorder_client *c,
341 if(!authorize(c, argv[0]))
342 auto_reconfigure = 1;
345 static void cf_resolve(disorder_client *c,
349 if(disorder_resolve(c, &track, argv[0])) exit(EXIT_FAILURE);
350 xprintf("%s\n", nullcheck(utf82mb(track)));
353 static void cf_pause(disorder_client *c,
354 char attribute((unused)) **argv) {
355 if(disorder_pause(c)) exit(EXIT_FAILURE);
358 static void cf_resume(disorder_client *c,
359 char attribute((unused)) **argv) {
360 if(disorder_resume(c)) exit(EXIT_FAILURE);
363 static void cf_tags(disorder_client *c,
364 char attribute((unused)) **argv) {
367 if(disorder_tags(c, &vec, 0)) exit(EXIT_FAILURE);
369 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
372 static void cf_users(disorder_client *c,
373 char attribute((unused)) **argv) {
376 if(disorder_users(c, &vec, 0)) exit(EXIT_FAILURE);
378 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
381 static void cf_get_global(disorder_client *c, char **argv) {
384 if(disorder_get_global(c, argv[0], &value)) exit(EXIT_FAILURE);
385 xprintf("%s\n", nullcheck(utf82mb(value)));
388 static void cf_set_global(disorder_client *c, char **argv) {
389 if(disorder_set_global(c, argv[0], argv[1])) exit(EXIT_FAILURE);
392 static void cf_unset_global(disorder_client *c, char **argv) {
393 if(disorder_unset_global(c, argv[0])) exit(EXIT_FAILURE);
396 static int isarg_integer(const char *s) {
399 if(!isdigit((unsigned char)*s))
406 static void cf_new(disorder_client *c,
410 if(disorder_new_tracks(c, &vec, 0, argv[0] ? atoi(argv[0]) : 0))
413 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
416 static void cf_rtp_address(disorder_client *c,
417 char attribute((unused)) **argv) {
418 char *address, *port;
420 if(disorder_rtp_address(c, &address, &port)) exit(EXIT_FAILURE);
421 xprintf("address: %s\nport: %s\n", address, port);
424 static void cf_adduser(disorder_client *c,
426 if(disorder_adduser(c, argv[0], argv[1]))
430 static void cf_deluser(disorder_client *c,
432 if(disorder_deluser(c, argv[0]))
436 static void cf_edituser(disorder_client *c,
438 if(disorder_edituser(c, argv[0], argv[1], argv[2]))
442 static void cf_userinfo(disorder_client *c, char **argv) {
445 if(disorder_userinfo(c, argv[0], argv[1], &s))
447 xprintf("%s\n", nullcheck(utf82mb(s)));
450 static const struct command {
453 void (*fn)(disorder_client *c, char **);
454 int (*isarg)(const char *);
455 const char *argstr, *desc;
457 { "adduser", 2, 2, cf_adduser, 0, "USER PASSWORD",
458 "Create a new user" },
459 { "allfiles", 1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]",
460 "List all files and directories in DIR" },
461 { "authorize", 1, 1, cf_authorize, 0, "USER",
462 "Authorize USER to connect to the server" },
463 { "become", 1, 1, cf_become, 0, "USER",
464 "Become user USER" },
465 { "deluser", 1, 1, cf_deluser, 0, "USER",
467 { "dirs", 1, 2, cf_dirs, isarg_regexp, "DIR [~REGEXP]",
468 "List directories in DIR" },
469 { "disable", 0, 0, cf_disable, 0, "",
471 { "disable-random", 0, 0, cf_random_disable, 0, "",
472 "Disable random play" },
473 { "edituser", 3, 3, cf_edituser, 0, "USER PROPERTY VALUE",
474 "Set a property of a user" },
475 { "enable", 0, 0, cf_enable, 0, "",
477 { "enable-random", 0, 0, cf_random_enable, 0, "",
478 "Enable random play" },
479 { "files", 1, 2, cf_files, isarg_regexp, "DIR [~REGEXP]",
480 "List files in DIR" },
481 { "get", 2, 2, cf_get, 0, "TRACK NAME",
482 "Get a preference value" },
483 { "get-global", 1, 1, cf_get_global, 0, "NAME",
484 "Get a global preference value" },
485 { "get-volume", 0, 0, cf_get_volume, 0, "",
486 "Get the current volume" },
487 { "length", 1, 1, cf_length, 0, "TRACK",
488 "Get the length of TRACK in seconds" },
489 { "log", 0, 0, cf_log, 0, "",
490 "Copy event log to stdout" },
491 { "move", 2, 2, cf_move, 0, "TRACK DELTA",
492 "Move a track in the queue" },
493 { "new", 0, 1, cf_new, isarg_integer, "[MAX]",
494 "Get the most recently added MAX tracks" },
495 { "part", 3, 3, cf_part, 0, "TRACK CONTEXT PART",
496 "Find a track name part" },
497 { "pause", 0, 0, cf_pause, 0, "",
498 "Pause the currently playing track" },
499 { "play", 1, INT_MAX, cf_play, isarg_filename, "TRACKS...",
500 "Add TRACKS to the end of the queue" },
501 { "playing", 0, 0, cf_playing, 0, "",
502 "Report the playing track" },
503 { "prefs", 1, 1, cf_prefs, 0, "TRACK",
504 "Display all the preferences for TRACK" },
505 { "quack", 0, 0, cf_quack, 0, 0, 0 },
506 { "queue", 0, 0, cf_queue, 0, "",
507 "Display the current queue" },
508 { "random-disable", 0, 0, cf_random_disable, 0, "",
509 "Disable random play" },
510 { "random-enable", 0, 0, cf_random_enable, 0, "",
511 "Enable random play" },
512 { "recent", 0, 0, cf_recent, 0, "",
513 "Display recently played track" },
514 { "reconfigure", 0, 0, cf_reconfigure, 0, "",
515 "Reconfigure the daemon" },
516 { "remove", 1, 1, cf_remove, 0, "TRACK",
517 "Remove a track from the queue" },
518 { "rescan", 0, 0, cf_rescan, 0, "",
519 "Rescan for new tracks" },
520 { "resolve", 1, 1, cf_resolve, 0, "TRACK",
521 "Resolve alias for TRACK" },
522 { "resume", 0, 0, cf_resume, 0, "",
523 "Resume after a pause" },
524 { "rtp-address", 0, 0, cf_rtp_address, 0, "",
525 "Report server's broadcast address" },
526 { "scratch", 0, 0, cf_scratch, 0, "",
527 "Scratch the currently playing track" },
528 { "scratch-id", 1, 1, cf_scratch, 0, "ID",
529 "Scratch the currently playing track" },
530 { "search", 1, 1, cf_search, 0, "WORDS",
531 "Display tracks matching all the words" },
532 { "set", 3, 3, cf_set, 0, "TRACK NAME VALUE",
533 "Set a preference value" },
534 { "set-global", 2, 2, cf_set_global, 0, "NAME VALUE",
535 "Set a global preference value" },
536 { "set-volume", 2, 2, cf_set_volume, 0, "LEFT RIGHT",
538 { "shutdown", 0, 0, cf_shutdown, 0, "",
539 "Shut down the daemon" },
540 { "stats", 0, 0, cf_stats, 0, "",
541 "Display server statistics" },
542 { "tags", 0, 0, cf_tags, 0, "",
544 { "unset", 2, 2, cf_unset, 0, "TRACK NAME",
545 "Unset a preference" },
546 { "unset-global", 1, 1, cf_unset_global, 0, "NAME",
547 "Unset a global preference" },
548 { "userinfo", 2, 2, cf_userinfo, 0, "USER PROPERTY",
549 "Get a property of as user" },
550 { "users", 0, 0, cf_users, 0, "",
552 { "version", 0, 0, cf_version, 0, "",
553 "Display the server version" },
556 static void help_commands(void) {
557 unsigned n, max = 0, l;
559 xprintf("Command summary:\n");
560 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
561 if(!commands[n].desc) continue;
562 l = strlen(commands[n].name);
563 if(*commands[n].argstr)
564 l += strlen(commands[n].argstr) + 1;
568 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
569 if(!commands[n].desc) continue;
570 l = strlen(commands[n].name);
571 if(*commands[n].argstr)
572 l += strlen(commands[n].argstr) + 1;
573 xprintf(" %s%s%s%*s %s\n", commands[n].name,
574 *commands[n].argstr ? " " : "",
583 int main(int argc, char **argv) {
584 int n, i, j, local = 0;
585 disorder_client *c = 0;
588 const char *user = 0, *password = 0;
591 /* garbage-collect PCRE's memory */
592 pcre_malloc = xmalloc;
594 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
595 while((n = getopt_long(argc, argv, "hVc:dHlNu:p:", options, 0)) >= 0) {
598 case 'H': help_commands();
600 case 'c': configfile = optarg; break;
601 case 'd': debugging = 1; break;
602 case 'l': local = 1; break;
603 case 'N': config_per_user = 0; break;
604 case 'u': user = optarg; break;
605 case 'p': password = optarg; break;
606 default: fatal(0, "invalid option");
609 if(config_read(0)) fatal(0, "cannot read configuration");
611 config->username = user;
612 config->password = 0;
615 config->password = password;
617 config->connect.n = 0;
618 if(!(c = disorder_new(1))) exit(EXIT_FAILURE);
619 if(disorder_connect(c)) exit(EXIT_FAILURE);
621 /* accumulate command args */
623 if((i = TABLE_FIND(commands, struct command, name, argv[n])) < 0)
624 fatal(0, "unknown command '%s'", argv[n]);
625 if(n + commands[i].min >= argc)
626 fatal(0, "missing arguments to '%s'", argv[n]);
629 for(j = 0; j < commands[i].min; ++j)
630 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
631 for(; j < commands[i].max
633 && commands[i].isarg(argv[n + j]); ++j)
634 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
635 vector_terminate(&args);
636 commands[i].fn(c, args.vec);
639 if(auto_reconfigure) {
641 if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
643 if(c && disorder_close(c)) exit(EXIT_FAILURE);
644 if(fclose(stdout) < 0) fatal(errno, "error closing stdout");