Merge from trunk
[disorder] / clients / disorder.c
1 /*
2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2008 Richard Kettlewell
4 *
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 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 /** @file clients/disorder.c
19 * @brief Command-line client
20 */
21
22 #include "common.h"
23
24 #include <getopt.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
28 #include <errno.h>
29 #include <locale.h>
30 #include <time.h>
31 #include <stddef.h>
32 #include <unistd.h>
33 #include <pcre.h>
34 #include <ctype.h>
35 #include <gcrypt.h>
36 #include <langinfo.h>
37
38 #include "configuration.h"
39 #include "syscalls.h"
40 #include "log.h"
41 #include "queue.h"
42 #include "client.h"
43 #include "wstat.h"
44 #include "table.h"
45 #include "charset.h"
46 #include "kvp.h"
47 #include "split.h"
48 #include "sink.h"
49 #include "mem.h"
50 #include "defs.h"
51 #include "authorize.h"
52 #include "vector.h"
53 #include "version.h"
54 #include "dateparse.h"
55 #include "trackdb.h"
56 #include "inputline.h"
57
58 static disorder_client *client;
59
60 static const struct option options[] = {
61 { "help", no_argument, 0, 'h' },
62 { "version", no_argument, 0, 'V' },
63 { "config", required_argument, 0, 'c' },
64 { "debug", no_argument, 0, 'd' },
65 { "local", no_argument, 0, 'l' },
66 { "no-per-user-config", no_argument, 0, 'N' },
67 { "help-commands", no_argument, 0, 'H' },
68 { "user", required_argument, 0, 'u' },
69 { "password", required_argument, 0, 'p' },
70 { "wait-for-root", no_argument, 0, 'W' },
71 { 0, 0, 0, 0 }
72 };
73
74 /* display usage message and terminate */
75 static void help(void) {
76 xprintf("Usage:\n"
77 " disorder [OPTIONS] COMMAND ...\n"
78 "Options:\n"
79 " --help, -h Display usage message\n"
80 " --help-commands, -H List commands\n"
81 " --version, -V Display version number\n"
82 " --config PATH, -c PATH Set configuration file\n"
83 " --local, -l Force connection to local server\n"
84 " --debug, -d Turn on debugging\n");
85 xfclose(stdout);
86 exit(0);
87 }
88
89 static disorder_client *getclient(void) {
90 if(!client) {
91 if(!(client = disorder_new(1))) exit(EXIT_FAILURE);
92 if(disorder_connect(client)) exit(EXIT_FAILURE);
93 }
94 return client;
95 }
96
97 static void cf_version(char attribute((unused)) **argv) {
98 char *v;
99
100 if(disorder_version(getclient(), &v)) exit(EXIT_FAILURE);
101 xprintf("%s\n", nullcheck(utf82mb(v)));
102 }
103
104 static void print_queue_entry(const struct queue_entry *q) {
105 if(q->track) xprintf("track %s\n", nullcheck(utf82mb(q->track)));
106 if(q->id) xprintf(" id %s\n", nullcheck(utf82mb(q->id)));
107 switch(q->origin) {
108 case origin_adopted:
109 case origin_picked:
110 case origin_scheduled:
111 xprintf(" %s by %s at %s",
112 track_origins[q->origin],
113 nullcheck(utf82mb(q->submitter)), ctime(&q->when));
114 break;
115 default:
116 break;
117 }
118 if(q->played) xprintf(" played at %s", ctime(&q->played));
119 if(q->state == playing_started
120 || q->state == playing_paused) xprintf(" %lds so far", q->sofar);
121 else if(q->expected) xprintf(" might start at %s", ctime(&q->expected));
122 if(q->scratched) xprintf(" scratched by %s\n",
123 nullcheck(utf82mb(q->scratched)));
124 else xprintf(" %s\n", playing_states[q->state]);
125 if(q->wstat) xprintf(" %s\n", wstat(q->wstat));
126 }
127
128 static void cf_playing(char attribute((unused)) **argv) {
129 struct queue_entry *q;
130
131 if(disorder_playing(getclient(), &q)) exit(EXIT_FAILURE);
132 if(q)
133 print_queue_entry(q);
134 else
135 xprintf("nothing\n");
136 }
137
138 static void cf_play(char **argv) {
139 while(*argv)
140 if(disorder_play(getclient(), *argv++)) exit(EXIT_FAILURE);
141 }
142
143 static void cf_remove(char **argv) {
144 if(disorder_remove(getclient(), argv[0])) exit(EXIT_FAILURE);
145 }
146
147 static void cf_disable(char attribute((unused)) **argv) {
148 if(disorder_disable(getclient())) exit(EXIT_FAILURE);
149 }
150
151 static void cf_enable(char attribute((unused)) **argv) {
152 if(disorder_enable(getclient())) exit(EXIT_FAILURE);
153 }
154
155 static void cf_scratch(char **argv) {
156 if(disorder_scratch(getclient(), argv[0])) exit(EXIT_FAILURE);
157 }
158
159 static void cf_shutdown(char attribute((unused)) **argv) {
160 if(disorder_shutdown(getclient())) exit(EXIT_FAILURE);
161 }
162
163 static void cf_reconfigure(char attribute((unused)) **argv) {
164 /* Re-check configuration for server */
165 if(config_read(1, NULL)) fatal(0, "cannot read configuration");
166 if(disorder_reconfigure(getclient())) exit(EXIT_FAILURE);
167 }
168
169 static void cf_rescan(char attribute((unused)) **argv) {
170 if(disorder_rescan(getclient())) exit(EXIT_FAILURE);
171 }
172
173 static void cf_somequeue(int (*fn)(disorder_client *c,
174 struct queue_entry **qp)) {
175 struct queue_entry *q;
176
177 if(fn(getclient(), &q)) exit(EXIT_FAILURE);
178 while(q) {
179 print_queue_entry(q);
180 q = q->next;
181 }
182 }
183
184 static void cf_recent(char attribute((unused)) **argv) {
185 cf_somequeue(disorder_recent);
186 }
187
188 static void cf_queue(char attribute((unused)) **argv) {
189 cf_somequeue(disorder_queue);
190 }
191
192 static void cf_quack(char attribute((unused)) **argv) {
193 if(!strcasecmp(nl_langinfo(CODESET), "utf-8")) {
194 #define TL "\xE2\x95\xAD"
195 #define TR "\xE2\x95\xAE"
196 #define BR "\xE2\x95\xAF"
197 #define BL "\xE2\x95\xB0"
198 #define H "\xE2\x94\x80"
199 #define V "\xE2\x94\x82"
200 #define T "\xE2\x94\xAC"
201 xprintf("\n"
202 " "TL H H H H H H H H H H H H H H H H H H TR"\n"
203 " "V" Naath is a babe! "V"\n"
204 " "BL H H H H H H H H H T H H H H H H H H BR"\n"
205 " \\\n"
206 " >0\n"
207 " (<)'\n"
208 "~~~~~~~~~~~~~~~~~~~~~~\n"
209 "\n");
210 } else {
211 xprintf("\n"
212 " .------------------.\n"
213 " | Naath is a babe! |\n"
214 " `---------+--------'\n"
215 " \\\n"
216 " >0\n"
217 " (<)'\n"
218 "~~~~~~~~~~~~~~~~~~~~~~\n"
219 "\n");
220 }
221 }
222
223 static void cf_somelist(char **argv,
224 int (*fn)(disorder_client *c,
225 const char *arg, const char *re,
226 char ***vecp, int *nvecp)) {
227 char **vec;
228 const char *re;
229
230 if(argv[1])
231 re = xstrdup(argv[1] + 1);
232 else
233 re = 0;
234 if(fn(getclient(), argv[0], re, &vec, 0)) exit(EXIT_FAILURE);
235 while(*vec)
236 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
237 }
238
239 static int isarg_regexp(const char *s) {
240 return s[0] == '~';
241 }
242
243 static void cf_dirs(char **argv) {
244 cf_somelist(argv, disorder_directories);
245 }
246
247 static void cf_files(char **argv) {
248 cf_somelist(argv, disorder_files);
249 }
250
251 static void cf_allfiles(char **argv) {
252 cf_somelist(argv, disorder_allfiles);
253 }
254
255 static void cf_get(char **argv) {
256 char *value;
257
258 if(disorder_get(getclient(), argv[0], argv[1], &value)) exit(EXIT_FAILURE);
259 xprintf("%s\n", nullcheck(utf82mb(value)));
260 }
261
262 static void cf_length(char **argv) {
263 long length;
264
265 if(disorder_length(getclient(), argv[0], &length)) exit(EXIT_FAILURE);
266 xprintf("%ld\n", length);
267 }
268
269 static void cf_set(char **argv) {
270 if(disorder_set(getclient(), argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
271 }
272
273 static void cf_unset(char **argv) {
274 if(disorder_unset(getclient(), argv[0], argv[1])) exit(EXIT_FAILURE);
275 }
276
277 static void cf_prefs(char **argv) {
278 struct kvp *k;
279
280 if(disorder_prefs(getclient(), argv[0], &k)) exit(EXIT_FAILURE);
281 for(; k; k = k->next)
282 xprintf("%s = %s\n",
283 nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
284 }
285
286 static void cf_search(char **argv) {
287 char **results;
288 int nresults, n;
289
290 if(disorder_search(getclient(), *argv, &results, &nresults)) exit(EXIT_FAILURE);
291 for(n = 0; n < nresults; ++n)
292 xprintf("%s\n", nullcheck(utf82mb(results[n])));
293 }
294
295 static void cf_random_disable(char attribute((unused)) **argv) {
296 if(disorder_random_disable(getclient())) exit(EXIT_FAILURE);
297 }
298
299 static void cf_random_enable(char attribute((unused)) **argv) {
300 if(disorder_random_enable(getclient())) exit(EXIT_FAILURE);
301 }
302
303 static void cf_stats(char attribute((unused)) **argv) {
304 char **vec;
305
306 if(disorder_stats(getclient(), &vec, 0)) exit(EXIT_FAILURE);
307 while(*vec)
308 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
309 }
310
311 static void cf_get_volume(char attribute((unused)) **argv) {
312 int l, r;
313
314 if(disorder_get_volume(getclient(), &l, &r)) exit(EXIT_FAILURE);
315 xprintf("%d %d\n", l, r);
316 }
317
318 static void cf_set_volume(char **argv) {
319 if(disorder_set_volume(getclient(), atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE);
320 }
321
322 static void cf_log(char attribute((unused)) **argv) {
323 if(disorder_log(getclient(), sink_stdio("stdout", stdout))) exit(EXIT_FAILURE);
324 }
325
326 static void cf_move(char **argv) {
327 long n;
328 int e;
329
330 if((e = xstrtol(&n, argv[1], 0, 10)))
331 fatal(e, "cannot convert '%s'", argv[1]);
332 if(n > INT_MAX || n < INT_MIN)
333 fatal(e, "%ld out of range", n);
334 if(disorder_move(getclient(), argv[0], (int)n)) exit(EXIT_FAILURE);
335 }
336
337 static void cf_part(char **argv) {
338 char *s;
339
340 if(disorder_part(getclient(), &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
341 xprintf("%s\n", nullcheck(utf82mb(s)));
342 }
343
344 static int isarg_filename(const char *s) {
345 return s[0] == '/';
346 }
347
348 static void cf_authorize(char **argv) {
349 authorize(getclient(), argv[0], argv[1]);
350 }
351
352 static void cf_resolve(char **argv) {
353 char *track;
354
355 if(disorder_resolve(getclient(), &track, argv[0])) exit(EXIT_FAILURE);
356 xprintf("%s\n", nullcheck(utf82mb(track)));
357 }
358
359 static void cf_pause(char attribute((unused)) **argv) {
360 if(disorder_pause(getclient())) exit(EXIT_FAILURE);
361 }
362
363 static void cf_resume(char attribute((unused)) **argv) {
364 if(disorder_resume(getclient())) exit(EXIT_FAILURE);
365 }
366
367 static void cf_tags(char attribute((unused)) **argv) {
368 char **vec;
369
370 if(disorder_tags(getclient(), &vec, 0)) exit(EXIT_FAILURE);
371 while(*vec)
372 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
373 }
374
375 static void cf_users(char attribute((unused)) **argv) {
376 char **vec;
377
378 if(disorder_users(getclient(), &vec, 0)) exit(EXIT_FAILURE);
379 while(*vec)
380 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
381 }
382
383 static void cf_get_global(char **argv) {
384 char *value;
385
386 if(disorder_get_global(getclient(), argv[0], &value)) exit(EXIT_FAILURE);
387 xprintf("%s\n", nullcheck(utf82mb(value)));
388 }
389
390 static void cf_set_global(char **argv) {
391 if(disorder_set_global(getclient(), argv[0], argv[1])) exit(EXIT_FAILURE);
392 }
393
394 static void cf_unset_global(char **argv) {
395 if(disorder_unset_global(getclient(), argv[0])) exit(EXIT_FAILURE);
396 }
397
398 static int isarg_integer(const char *s) {
399 if(!*s) return 0;
400 while(*s) {
401 if(!isdigit((unsigned char)*s))
402 return 0;
403 ++s;
404 }
405 return 1;
406 }
407
408 static void cf_new(char **argv) {
409 char **vec;
410
411 if(disorder_new_tracks(getclient(), &vec, 0, argv[0] ? atoi(argv[0]) : 0))
412 exit(EXIT_FAILURE);
413 while(*vec)
414 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
415 }
416
417 static void cf_rtp_address(char attribute((unused)) **argv) {
418 char *address, *port;
419
420 if(disorder_rtp_address(getclient(), &address, &port)) exit(EXIT_FAILURE);
421 xprintf("address: %s\nport: %s\n", address, port);
422 }
423
424 static int isarg_rights(const char *arg) {
425 return strchr(arg, ',') || !parse_rights(arg, 0, 0);
426 }
427
428 static void cf_adduser(char **argv) {
429 if(disorder_adduser(getclient(), argv[0], argv[1], argv[2]))
430 exit(EXIT_FAILURE);
431 }
432
433 static void cf_deluser(char **argv) {
434 if(disorder_deluser(getclient(), argv[0]))
435 exit(EXIT_FAILURE);
436 }
437
438 static void cf_edituser(char **argv) {
439 if(disorder_edituser(getclient(), argv[0], argv[1], argv[2]))
440 exit(EXIT_FAILURE);
441 }
442
443 static void cf_userinfo(char **argv) {
444 char *s;
445
446 if(disorder_userinfo(getclient(), argv[0], argv[1], &s))
447 exit(EXIT_FAILURE);
448 xprintf("%s\n", nullcheck(utf82mb(s)));
449 }
450
451 static int isarg_option(const char *arg) {
452 return arg[0] == '-';
453 }
454
455 static int argvlen(char **argv) {
456 char **start = argv;
457
458 while(*argv)
459 ++argv;
460 return argv - start;
461 }
462
463 static const struct option setup_guest_options[] = {
464 { "help", no_argument, 0, 'h' },
465 { "online-registration", no_argument, 0, 'r' },
466 { "no-online-registration", no_argument, 0, 'R' },
467 { 0, 0, 0, 0 }
468 };
469
470 static void help_setup_guest(void) {
471 xprintf("Usage:\n"
472 " disorder setup-guest [OPTIONS]\n"
473 "Options:\n"
474 " --help, -h Display usage message\n"
475 " --online-registration Enable online registration (default)\n"
476 " --no-online-registration Disable online registration\n");
477 xfclose(stdout);
478 exit(0);
479 }
480
481 static void cf_setup_guest(char **argv) {
482 int n, online_registration = 1;
483
484 while((n = getopt_long(argvlen(argv) + 1, argv - 1,
485 "hrR", setup_guest_options, 0)) >= 0) {
486 switch(n) {
487 case 'h': help_setup_guest();
488 case 'r': online_registration = 1; break;
489 case 'R': online_registration = 0; break;
490 default: fatal(0, "invalid option");
491 }
492 }
493 if(online_registration && !config->mail_sender)
494 fatal(0, "you MUST set mail_sender if you want online registration");
495 if(disorder_adduser(getclient(), "guest", "",
496 online_registration ? "read,register" : "read"))
497 exit(EXIT_FAILURE);
498 }
499
500 struct scheduled_event {
501 time_t when;
502 struct kvp *actiondata;
503 char *id;
504 };
505
506 static int compare_event(const void *av, const void *bv) {
507 struct scheduled_event *a = (void *)av, *b = (void *)bv;
508
509 /* Primary sort key is the trigger time */
510 if(a->when < b->when)
511 return -1;
512 else if(a->when > b->when)
513 return 1;
514 /* For events that go off at the same time just sort by ID */
515 return strcmp(a->id, b->id);
516 }
517
518 static void cf_schedule_list(char attribute((unused)) **argv) {
519 char **ids;
520 int nids, n;
521 struct scheduled_event *events;
522 char tb[128];
523 const char *action, *key, *value, *priority;
524 int prichar;
525
526 /* Get all known events */
527 if(disorder_schedule_list(getclient(), &ids, &nids))
528 exit(EXIT_FAILURE);
529 events = xcalloc(nids, sizeof *events);
530 for(n = 0; n < nids; ++n) {
531 events[n].id = ids[n];
532 if(disorder_schedule_get(getclient(), ids[n], &events[n].actiondata))
533 exit(EXIT_FAILURE);
534 events[n].when = atoll(kvp_get(events[n].actiondata, "when"));
535 }
536 /* Sort by trigger time */
537 qsort(events, nids, sizeof *events, compare_event);
538 /* Display them */
539 for(n = 0; n < nids; ++n) {
540 strftime(tb, sizeof tb, "%Y-%m-%d %H:%M:%S %Z", localtime(&events[n].when));
541 action = kvp_get(events[n].actiondata, "action");
542 priority = kvp_get(events[n].actiondata, "priority");
543 if(!strcmp(priority, "junk"))
544 prichar = 'J';
545 else if(!strcmp(priority, "normal"))
546 prichar = 'N';
547 else
548 prichar = '?';
549 xprintf("%11s %-25s %c %-8s %s",
550 events[n].id, tb, prichar, kvp_get(events[n].actiondata, "who"),
551 action);
552 if(!strcmp(action, "play"))
553 xprintf(" %s",
554 nullcheck(utf82mb(kvp_get(events[n].actiondata, "track"))));
555 else if(!strcmp(action, "set-global")) {
556 key = kvp_get(events[n].actiondata, "key");
557 value = kvp_get(events[n].actiondata, "value");
558 if(value)
559 xprintf(" %s=%s",
560 nullcheck(utf82mb(key)),
561 nullcheck(utf82mb(value)));
562 else
563 xprintf(" %s unset",
564 nullcheck(utf82mb(key)));
565 }
566 xprintf("\n");
567 }
568 }
569
570 static void cf_schedule_del(char **argv) {
571 if(disorder_schedule_del(getclient(), argv[0]))
572 exit(EXIT_FAILURE);
573 }
574
575 static void cf_schedule_play(char **argv) {
576 if(disorder_schedule_add(getclient(),
577 dateparse(argv[0]),
578 argv[1],
579 "play",
580 argv[2]))
581 exit(EXIT_FAILURE);
582 }
583
584 static void cf_schedule_set_global(char **argv) {
585 if(disorder_schedule_add(getclient(),
586 dateparse(argv[0]),
587 argv[1],
588 "set-global",
589 argv[2],
590 argv[3]))
591 exit(EXIT_FAILURE);
592 }
593
594 static void cf_schedule_unset_global(char **argv) {
595 if(disorder_schedule_add(getclient(),
596 dateparse(argv[0]),
597 argv[1],
598 "set-global",
599 argv[2],
600 (char *)0))
601 exit(EXIT_FAILURE);
602 }
603
604 static void cf_adopt(char **argv) {
605 if(disorder_adopt(getclient(), argv[0]))
606 exit(EXIT_FAILURE);
607 }
608
609 static void cf_playlists(char attribute((unused)) **argv) {
610 char **vec;
611
612 if(disorder_playlists(getclient(), &vec, 0))
613 exit(EXIT_FAILURE);
614 while(*vec)
615 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
616 }
617
618 static void cf_playlist_del(char **argv) {
619 if(disorder_playlist_delete(getclient(), argv[0]))
620 exit(EXIT_FAILURE);
621 }
622
623 static void cf_playlist_get(char **argv) {
624 char **vec;
625
626 if(disorder_playlist_get(getclient(), argv[0], &vec, 0))
627 exit(EXIT_FAILURE);
628 while(*vec)
629 xprintf("%s\n", nullcheck(utf82mb(*vec++)));
630 }
631
632 static void cf_playlist_set(char **argv) {
633 struct vector v[1];
634 FILE *input;
635 const char *tag;
636 char *l;
637
638 if(argv[1]) {
639 // Read track list from file
640 if(!(input = fopen(argv[1], "r")))
641 fatal(errno, "opening %s", argv[1]);
642 tag = argv[1];
643 } else {
644 // Read track list from standard input
645 input = stdin;
646 tag = "stdin";
647 }
648 vector_init(v);
649 while(!inputline(tag, input, &l, '\n')) {
650 if(!strcmp(l, "."))
651 break;
652 vector_append(v, l);
653 }
654 if(ferror(input))
655 fatal(errno, "reading %s", tag);
656 if(input != stdin)
657 fclose(input);
658 if(disorder_playlist_lock(getclient(), argv[0])
659 || disorder_playlist_set(getclient(), argv[0], v->vec, v->nvec)
660 || disorder_playlist_unlock(getclient()))
661 exit(EXIT_FAILURE);
662 }
663
664 static const struct command {
665 const char *name;
666 int min, max;
667 void (*fn)(char **);
668 int (*isarg)(const char *);
669 const char *argstr, *desc;
670 } commands[] = {
671 { "adduser", 2, 3, cf_adduser, isarg_rights, "USERNAME PASSWORD [RIGHTS]",
672 "Create a new user" },
673 { "adopt", 1, 1, cf_adopt, 0, "ID",
674 "Adopt a randomly picked track" },
675 { "allfiles", 1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]",
676 "List all files and directories in DIR" },
677 { "authorize", 1, 2, cf_authorize, isarg_rights, "USERNAME [RIGHTS]",
678 "Authorize user USERNAME to connect" },
679 { "deluser", 1, 1, cf_deluser, 0, "USERNAME",
680 "Delete user USERNAME" },
681 { "dirs", 1, 2, cf_dirs, isarg_regexp, "DIR [~REGEXP]",
682 "List directories in DIR" },
683 { "disable", 0, 0, cf_disable, 0, "",
684 "Disable play" },
685 { "disable-random", 0, 0, cf_random_disable, 0, "",
686 "Disable random play" },
687 { "edituser", 3, 3, cf_edituser, 0, "USERNAME PROPERTY VALUE",
688 "Set a property of user USERNAME" },
689 { "enable", 0, 0, cf_enable, 0, "",
690 "Enable play" },
691 { "enable-random", 0, 0, cf_random_enable, 0, "",
692 "Enable random play" },
693 { "files", 1, 2, cf_files, isarg_regexp, "DIR [~REGEXP]",
694 "List files in DIR" },
695 { "get", 2, 2, cf_get, 0, "TRACK NAME",
696 "Get a preference value" },
697 { "get-global", 1, 1, cf_get_global, 0, "NAME",
698 "Get a global preference value" },
699 { "get-volume", 0, 0, cf_get_volume, 0, "",
700 "Get the current volume" },
701 { "length", 1, 1, cf_length, 0, "TRACK",
702 "Get the length of TRACK in seconds" },
703 { "log", 0, 0, cf_log, 0, "",
704 "Copy event log to stdout" },
705 { "move", 2, 2, cf_move, 0, "TRACK DELTA",
706 "Move a track in the queue" },
707 { "new", 0, 1, cf_new, isarg_integer, "[MAX]",
708 "Get the most recently added MAX tracks" },
709 { "part", 3, 3, cf_part, 0, "TRACK CONTEXT PART",
710 "Find a track name part" },
711 { "pause", 0, 0, cf_pause, 0, "",
712 "Pause the currently playing track" },
713 { "play", 1, INT_MAX, cf_play, isarg_filename, "TRACKS...",
714 "Add TRACKS to the end of the queue" },
715 { "playing", 0, 0, cf_playing, 0, "",
716 "Report the playing track" },
717 { "playlist-del", 1, 1, cf_playlist_del, 0, "PLAYLIST",
718 "Delete a playlist" },
719 { "playlist-get", 1, 1, cf_playlist_get, 0, "PLAYLIST",
720 "Get the contents of a playlist" },
721 { "playlist-set", 1, 2, cf_playlist_set, isarg_filename, "PLAYLIST [PATH]",
722 "Set the contents of a playlist" },
723 { "playlists", 0, 0, cf_playlists, 0, "",
724 "List playlists" },
725 { "prefs", 1, 1, cf_prefs, 0, "TRACK",
726 "Display all the preferences for TRACK" },
727 { "quack", 0, 0, cf_quack, 0, 0, 0 },
728 { "queue", 0, 0, cf_queue, 0, "",
729 "Display the current queue" },
730 { "random-disable", 0, 0, cf_random_disable, 0, "",
731 "Disable random play" },
732 { "random-enable", 0, 0, cf_random_enable, 0, "",
733 "Enable random play" },
734 { "recent", 0, 0, cf_recent, 0, "",
735 "Display recently played track" },
736 { "reconfigure", 0, 0, cf_reconfigure, 0, "",
737 "Reconfigure the daemon" },
738 { "remove", 1, 1, cf_remove, 0, "TRACK",
739 "Remove a track from the queue" },
740 { "rescan", 0, 0, cf_rescan, 0, "",
741 "Rescan for new tracks" },
742 { "resolve", 1, 1, cf_resolve, 0, "TRACK",
743 "Resolve alias for TRACK" },
744 { "resume", 0, 0, cf_resume, 0, "",
745 "Resume after a pause" },
746 { "rtp-address", 0, 0, cf_rtp_address, 0, "",
747 "Report server's broadcast address" },
748 { "schedule-del", 1, 1, cf_schedule_del, 0, "EVENT",
749 "Delete a scheduled event" },
750 { "schedule-list", 0, 0, cf_schedule_list, 0, "",
751 "List scheduled events" },
752 { "schedule-play", 3, 3, cf_schedule_play, 0, "WHEN PRI TRACK",
753 "Play TRACK later" },
754 { "schedule-set-global", 4, 4, cf_schedule_set_global, 0, "WHEN PRI NAME VAL",
755 "Set a global preference later" },
756 { "schedule-unset-global", 3, 3, cf_schedule_unset_global, 0, "WHEN PRI NAME",
757 "Unset a global preference later" },
758 { "scratch", 0, 0, cf_scratch, 0, "",
759 "Scratch the currently playing track" },
760 { "scratch-id", 1, 1, cf_scratch, 0, "ID",
761 "Scratch the currently playing track" },
762 { "search", 1, 1, cf_search, 0, "WORDS",
763 "Display tracks matching all the words" },
764 { "set", 3, 3, cf_set, 0, "TRACK NAME VALUE",
765 "Set a preference value" },
766 { "set-global", 2, 2, cf_set_global, 0, "NAME VALUE",
767 "Set a global preference value" },
768 { "set-volume", 2, 2, cf_set_volume, 0, "LEFT RIGHT",
769 "Set the volume" },
770 { "setup-guest", 0, INT_MAX, cf_setup_guest, isarg_option, "[OPTIONS]",
771 "Create the guest login" },
772 { "shutdown", 0, 0, cf_shutdown, 0, "",
773 "Shut down the daemon" },
774 { "stats", 0, 0, cf_stats, 0, "",
775 "Display server statistics" },
776 { "tags", 0, 0, cf_tags, 0, "",
777 "List known tags" },
778 { "unset", 2, 2, cf_unset, 0, "TRACK NAME",
779 "Unset a preference" },
780 { "unset-global", 1, 1, cf_unset_global, 0, "NAME",
781 "Unset a global preference" },
782 { "userinfo", 2, 2, cf_userinfo, 0, "USERNAME PROPERTY",
783 "Get a property of a user" },
784 { "users", 0, 0, cf_users, 0, "",
785 "List all users" },
786 { "version", 0, 0, cf_version, 0, "",
787 "Display the server version" },
788 };
789
790 static void help_commands(void) {
791 unsigned n, max = 0, l;
792
793 xprintf("Command summary:\n");
794 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
795 if(!commands[n].desc) continue;
796 l = strlen(commands[n].name);
797 if(*commands[n].argstr)
798 l += strlen(commands[n].argstr) + 1;
799 if(l > max)
800 max = l;
801 }
802 for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
803 if(!commands[n].desc) continue;
804 l = strlen(commands[n].name);
805 if(*commands[n].argstr)
806 l += strlen(commands[n].argstr) + 1;
807 xprintf(" %s%s%s%*s %s\n", commands[n].name,
808 *commands[n].argstr ? " " : "",
809 commands[n].argstr,
810 max - l, "",
811 commands[n].desc);
812 }
813 xfclose(stdout);
814 exit(0);
815 }
816
817 static void wait_for_root(void) {
818 const char *password;
819
820 while(!trackdb_readable()) {
821 info("waiting for trackdb...");
822 sleep(1);
823 }
824 trackdb_init(TRACKDB_NO_RECOVER|TRACKDB_NO_UPGRADE);
825 for(;;) {
826 trackdb_open(TRACKDB_READ_ONLY);
827 password = trackdb_get_password("root");
828 trackdb_close();
829 if(password)
830 break;
831 info("waiting for root user to be created...");
832 sleep(1);
833 }
834 trackdb_deinit();
835 }
836
837 int main(int argc, char **argv) {
838 int n, i, j, local = 0, wfr = 0;
839 int status = 0;
840 struct vector args;
841 const char *user = 0, *password = 0;
842
843 mem_init();
844 /* garbage-collect PCRE's memory */
845 pcre_malloc = xmalloc;
846 pcre_free = xfree;
847 if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
848 if(!setlocale(LC_TIME, "")) fatal(errno, "error calling setlocale");
849 while((n = getopt_long(argc, argv, "+hVc:dHlNu:p:W", options, 0)) >= 0) {
850 switch(n) {
851 case 'h': help();
852 case 'H': help_commands();
853 case 'V': version("disorder");
854 case 'c': configfile = optarg; break;
855 case 'd': debugging = 1; break;
856 case 'l': local = 1; break;
857 case 'N': config_per_user = 0; break;
858 case 'u': user = optarg; break;
859 case 'p': password = optarg; break;
860 case 'W': wfr = 1; break;
861 default: fatal(0, "invalid option");
862 }
863 }
864 if(config_read(0, NULL)) fatal(0, "cannot read configuration");
865 if(user) {
866 config->username = user;
867 config->password = 0;
868 }
869 if(password)
870 config->password = password;
871 if(local)
872 config->connect.af = -1;
873 if(wfr)
874 wait_for_root();
875 n = optind;
876 optind = 1; /* for subsequent getopt calls */
877 /* gcrypt initialization */
878 if(!gcry_check_version(NULL))
879 disorder_fatal(0, "gcry_check_version failed");
880 gcry_control(GCRYCTL_INIT_SECMEM, 0);
881 gcry_control (GCRYCTL_INITIALIZATION_FINISHED, 0);
882 /* accumulate command args */
883 while(n < argc) {
884 if((i = TABLE_FIND(commands, name, argv[n])) < 0)
885 fatal(0, "unknown command '%s'", argv[n]);
886 if(n + commands[i].min >= argc)
887 fatal(0, "missing arguments to '%s'", argv[n]);
888 vector_init(&args);
889 /* Include the command name in the args, but at element -1, for
890 * the benefit of subcommand getopt calls */
891 vector_append(&args, argv[n]);
892 n++;
893 for(j = 0; j < commands[i].min; ++j)
894 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
895 for(; j < commands[i].max
896 && n + j < argc
897 && commands[i].isarg(argv[n + j]); ++j)
898 vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
899 vector_terminate(&args);
900 commands[i].fn(args.vec + 1);
901 n += j;
902 }
903 if(client && disorder_close(client)) exit(EXIT_FAILURE);
904 if(fclose(stdout) < 0) fatal(errno, "error closing stdout");
905 return status;
906 }
907
908 /*
909 Local Variables:
910 c-basic-offset:2
911 comment-column:40
912 End:
913 */