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