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