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