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