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