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