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