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