chiark / gitweb /
README just tells you to use disorder authorize now
[disorder] / clients / disorder.c
1 /*
2  * This file is part of DisOrder.
3  * Copyright (C) 2004, 2005, 2006, 2007 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 <config.h>
22 #include "types.h"
23
24 #include <getopt.h>
25 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <sys/un.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <locale.h>
33 #include <time.h>
34 #include <stddef.h>
35 #include <unistd.h>
36 #include <assert.h>
37 #include <pcre.h>
38 #include <ctype.h>
39
40 #include "configuration.h"
41 #include "syscalls.h"
42 #include "log.h"
43 #include "queue.h"
44 #include "client.h"
45 #include "wstat.h"
46 #include "table.h"
47 #include "charset.h"
48 #include "kvp.h"
49 #include "split.h"
50 #include "sink.h"
51 #include "plugin.h"
52 #include "mem.h"
53 #include "defs.h"
54 #include "authorize.h"
55 #include "vector.h"
56
57 static int auto_reconfigure;
58
59 static const struct option options[] = {
60   { "help", no_argument, 0, 'h' },
61   { "version", no_argument, 0, 'V' },
62   { "config", required_argument, 0, 'c' },
63   { "debug", no_argument, 0, 'd' },
64   { "help-commands", no_argument, 0, 'H' },
65   { 0, 0, 0, 0 }
66 };
67
68 /* display usage message and terminate */
69 static 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"
77           "  --debug, -d             Turn on debugging\n");
78   xfclose(stdout);
79   exit(0);
80 }
81
82 /* display version number and terminate */
83 static void version(void) {
84   xprintf("disorder version %s\n", disorder_version_string);
85   xfclose(stdout);
86   exit(0);
87 }
88
89 static void cf_version(disorder_client *c,
90                        char attribute((unused)) **argv) {
91   char *v;
92
93   if(disorder_version(c, &v)) exit(EXIT_FAILURE);
94   xprintf("%s\n", nullcheck(utf82mb(v)));
95 }
96
97 static void print_queue_entry(const struct queue_entry *q) {
98   if(q->track) xprintf("track %s\n", nullcheck(utf82mb(q->track)));
99   if(q->id) xprintf("  id %s\n", nullcheck(utf82mb(q->id)));
100   if(q->submitter) xprintf("  submitted by %s at %s",
101                            nullcheck(utf82mb(q->submitter)), ctime(&q->when));
102   if(q->played) xprintf("  played at %s", ctime(&q->played));
103   if(q->state == playing_started
104      || q->state == playing_paused) xprintf("  %lds so far",  q->sofar);
105   else if(q->expected) xprintf("  might start at %s", ctime(&q->expected));
106   if(q->scratched) xprintf("  scratched by %s\n",
107                            nullcheck(utf82mb(q->scratched)));
108   else xprintf("  %s\n", playing_states[q->state]);
109   if(q->wstat) xprintf("  %s\n", wstat(q->wstat));
110 }
111
112 static void cf_playing(disorder_client *c,
113                        char attribute((unused)) **argv) {
114   struct queue_entry *q;
115
116   if(disorder_playing(c, &q)) exit(EXIT_FAILURE);
117   if(q)
118     print_queue_entry(q);
119   else
120     xprintf("nothing\n");
121 }
122
123 static void cf_play(disorder_client *c, char **argv) {
124   while(*argv)
125     if(disorder_play(c, *argv++)) exit(EXIT_FAILURE);
126 }
127
128 static void cf_remove(disorder_client *c, char **argv) {
129   if(disorder_remove(c, argv[0])) exit(EXIT_FAILURE);
130 }
131
132 static void cf_disable(disorder_client *c,
133                        char attribute((unused)) **argv) {
134   if(disorder_disable(c)) exit(EXIT_FAILURE);
135 }
136
137 static void cf_enable(disorder_client *c, char attribute((unused)) **argv) {
138   if(disorder_enable(c)) exit(EXIT_FAILURE);
139 }
140
141 static void cf_scratch(disorder_client *c,
142                        char **argv) {
143   if(disorder_scratch(c, argv[0])) exit(EXIT_FAILURE);
144 }
145
146 static void cf_shutdown(disorder_client *c,
147                         char attribute((unused)) **argv) {
148   if(disorder_shutdown(c)) exit(EXIT_FAILURE);
149 }
150
151 static void cf_reconfigure(disorder_client *c,
152                            char attribute((unused)) **argv) {
153   /* Re-check configuration for server */
154   if(config_read(1)) fatal(0, "cannot read configuration");
155   if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
156 }
157
158 static void cf_rescan(disorder_client *c, char attribute((unused)) **argv) {
159   if(disorder_rescan(c)) exit(EXIT_FAILURE);
160 }
161
162 static void cf_somequeue(disorder_client *c,
163                          int (*fn)(disorder_client *c,
164                                    struct queue_entry **qp)) {
165   struct queue_entry *q;
166
167   if(fn(c, &q)) exit(EXIT_FAILURE);
168   while(q) {
169     print_queue_entry(q);
170     q = q->next;
171   }
172 }
173
174 static void cf_recent(disorder_client *c, char attribute((unused)) **argv) {
175   cf_somequeue(c, disorder_recent);
176 }
177
178 static void cf_queue(disorder_client *c, char attribute((unused)) **argv) {
179   cf_somequeue(c, disorder_queue);
180 }
181
182 static void cf_quack(disorder_client attribute((unused)) *c,
183                      char attribute((unused)) **argv) {
184   xprintf("\n"
185           " .------------------.\n"
186           " | Naath is a babe! |\n"
187           " `---------+--------'\n"
188           "            \\\n"
189           "              >0\n"
190           "               (<)'\n"
191           "~~~~~~~~~~~~~~~~~~~~~~\n"
192           "\n");
193 }
194
195 static void cf_somelist(disorder_client *c, char **argv,
196                         int (*fn)(disorder_client *c,
197                                   const char *arg, const char *re,
198                                   char ***vecp, int *nvecp)) {
199   char **vec;
200   const char *re;
201
202   if(argv[1])
203     re = xstrdup(argv[1] + 1);
204   else
205     re = 0;
206   if(fn(c, argv[0], re, &vec, 0)) exit(EXIT_FAILURE);
207   while(*vec)
208     xprintf("%s\n", nullcheck(utf82mb(*vec++)));
209 }
210
211 static int isarg_regexp(const char *s) {
212   return s[0] == '~';
213 }
214
215 static void cf_dirs(disorder_client *c,
216                     char **argv) {
217   cf_somelist(c, argv, disorder_directories);
218 }
219
220 static void cf_files(disorder_client *c, char **argv) {
221   cf_somelist(c, argv, disorder_files);
222 }
223
224 static void cf_allfiles(disorder_client *c, char **argv) {
225   cf_somelist(c, argv, disorder_allfiles);
226 }
227
228 static void cf_get(disorder_client *c, char **argv) {
229   char *value;
230
231   if(disorder_get(c, argv[0], argv[1], &value)) exit(EXIT_FAILURE);
232   xprintf("%s\n", nullcheck(utf82mb(value)));
233 }
234
235 static void cf_length(disorder_client *c, char **argv) {
236   long length;
237
238   if(disorder_length(c, argv[0], &length)) exit(EXIT_FAILURE);
239   xprintf("%ld\n", length);
240 }
241
242 static void cf_set(disorder_client *c, char **argv) {
243   if(disorder_set(c, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
244 }
245
246 static void cf_unset(disorder_client *c, char **argv) {
247   if(disorder_unset(c, argv[0], argv[1])) exit(EXIT_FAILURE);
248 }
249
250 static void cf_prefs(disorder_client *c, char **argv) {
251   struct kvp *k;
252
253   if(disorder_prefs(c, argv[0], &k)) exit(EXIT_FAILURE);
254   for(; k; k = k->next)
255     xprintf("%s = %s\n",
256             nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value)));
257 }
258
259 static void cf_search(disorder_client *c, char **argv) {
260   char **results;
261   int nresults, n;
262
263   if(disorder_search(c, *argv, &results, &nresults)) exit(EXIT_FAILURE);
264   for(n = 0; n < nresults; ++n)
265     xprintf("%s\n", nullcheck(utf82mb(results[n])));
266 }
267
268 static void cf_random_disable(disorder_client *c,
269                               char attribute((unused)) **argv) {
270   if(disorder_random_disable(c)) exit(EXIT_FAILURE);
271 }
272
273 static void cf_random_enable(disorder_client *c,
274                              char attribute((unused)) **argv) {
275   if(disorder_random_enable(c)) exit(EXIT_FAILURE);
276 }
277
278 static void cf_stats(disorder_client *c,
279                      char attribute((unused)) **argv) {
280   char **vec;
281
282   if(disorder_stats(c, &vec, 0)) exit(EXIT_FAILURE);
283   while(*vec)
284       xprintf("%s\n", nullcheck(utf82mb(*vec++)));
285 }
286
287 static void cf_get_volume(disorder_client *c,
288                           char attribute((unused)) **argv) {
289   int l, r;
290
291   if(disorder_get_volume(c, &l, &r)) exit(EXIT_FAILURE);
292   xprintf("%d %d\n", l, r);
293 }
294
295 static void cf_set_volume(disorder_client *c,
296                           char **argv) {
297   if(disorder_set_volume(c, atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE);
298 }
299
300 static void cf_become(disorder_client *c,
301                       char **argv) {
302   if(disorder_become(c, argv[0])) exit(EXIT_FAILURE);
303 }
304
305 static void cf_log(disorder_client *c,
306                    char attribute((unused)) **argv) {
307   if(disorder_log(c, sink_stdio("stdout", stdout))) exit(EXIT_FAILURE);
308 }
309
310 static void cf_move(disorder_client *c,
311                    char **argv) {
312   long n;
313   int e;
314   
315   if((e = xstrtol(&n, argv[1], 0, 10)))
316     fatal(e, "cannot convert '%s'", argv[1]);
317   if(n > INT_MAX || n < INT_MIN)
318     fatal(e, "%ld out of range", n);
319   if(disorder_move(c, argv[0], (int)n)) exit(EXIT_FAILURE);
320 }
321
322 static void cf_part(disorder_client *c,
323                     char **argv) {
324   char *s;
325
326   if(disorder_part(c, &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE);
327   xprintf("%s\n", nullcheck(utf82mb(s)));
328 }
329
330 static int isarg_filename(const char *s) {
331   return s[0] == '/';
332 }
333
334 static void cf_authorize(disorder_client attribute((unused)) *c,
335                          char **argv) {
336   if(!authorize(argv[0]))
337     auto_reconfigure = 1;
338 }
339
340 static void cf_resolve(disorder_client *c,
341                        char **argv) {
342   char *track;
343
344   if(disorder_resolve(c, &track, argv[0])) exit(EXIT_FAILURE);
345   xprintf("%s\n", nullcheck(utf82mb(track)));
346 }
347
348 static void cf_pause(disorder_client *c,
349                         char attribute((unused)) **argv) {
350   if(disorder_pause(c)) exit(EXIT_FAILURE);
351 }
352
353 static void cf_resume(disorder_client *c,
354                         char attribute((unused)) **argv) {
355   if(disorder_resume(c)) exit(EXIT_FAILURE);
356 }
357
358 static void cf_tags(disorder_client *c,
359                      char attribute((unused)) **argv) {
360   char **vec;
361
362   if(disorder_tags(c, &vec, 0)) exit(EXIT_FAILURE);
363   while(*vec)
364       xprintf("%s\n", nullcheck(utf82mb(*vec++)));
365 }
366
367 static void cf_get_global(disorder_client *c, char **argv) {
368   char *value;
369
370   if(disorder_get_global(c, argv[0], &value)) exit(EXIT_FAILURE);
371   xprintf("%s\n", nullcheck(utf82mb(value)));
372 }
373
374 static void cf_set_global(disorder_client *c, char **argv) {
375   if(disorder_set_global(c, argv[0], argv[1])) exit(EXIT_FAILURE);
376 }
377
378 static void cf_unset_global(disorder_client *c, char **argv) {
379   if(disorder_unset_global(c, argv[0])) exit(EXIT_FAILURE);
380 }
381
382 static int isarg_integer(const char *s) {
383   if(!*s) return 0;
384   while(*s) {
385     if(!isdigit((unsigned char)*s))
386       return 0;
387     ++s;
388   }
389   return 1;
390 }
391
392 static void cf_new(disorder_client *c,
393                    char **argv) {
394   char **vec;
395
396   if(disorder_new_tracks(c, &vec, 0, argv[0] ? atoi(argv[0]) : 0))
397     exit(EXIT_FAILURE);
398   while(*vec)
399     xprintf("%s\n", nullcheck(utf82mb(*vec++)));
400 }
401
402 static void cf_rtp_address(disorder_client *c,
403                            char attribute((unused)) **argv) {
404   char *address, *port;
405
406   if(disorder_rtp_address(c, &address, &port)) exit(EXIT_FAILURE);
407   xprintf("address: %s\nport: %s\n", address, port);
408 }
409
410 static const struct command {
411   const char *name;
412   int min, max;
413   void (*fn)(disorder_client *c, char **);
414   int (*isarg)(const char *);
415   const char *argstr, *desc;
416 } commands[] = {
417   { "allfiles",       1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]",
418                       "List all files and directories in DIR" },
419   { "authorize",      1, 1, cf_authorize, 0, "USER",
420                       "Authorize USER to connect to the server" },
421   { "become",         1, 1, cf_become, 0, "USER",
422                       "Become user USER" },
423   { "dirs",           1, 2, cf_dirs, isarg_regexp, "DIR [~REGEXP]",
424                       "List directories in DIR" },
425   { "disable",        0, 0, cf_disable, 0, "",
426                       "Disable play" },
427   { "disable-random", 0, 0, cf_random_disable, 0, "",
428                       "Disable random play" },
429   { "enable",         0, 0, cf_enable, 0, "",
430                       "Enable play" },
431   { "enable-random",  0, 0, cf_random_enable, 0, "",
432                       "Enable random play" },
433   { "files",          1, 2, cf_files, isarg_regexp, "DIR [~REGEXP]",
434                       "List files in DIR" },
435   { "get",            2, 2, cf_get, 0, "TRACK NAME",
436                       "Get a preference value" },
437   { "get-global",     1, 1, cf_get_global, 0, "NAME",
438                       "Get a global preference value" },
439   { "get-volume",     0, 0, cf_get_volume, 0, "",
440                       "Get the current volume" },
441   { "length",         1, 1, cf_length, 0, "TRACK",
442                       "Get the length of TRACK in seconds" },
443   { "log",            0, 0, cf_log, 0, "",
444                       "Copy event log to stdout" },
445   { "move",           2, 2, cf_move, 0, "TRACK DELTA",
446                       "Move a track in the queue" },
447   { "new",            0, 1, cf_new, isarg_integer, "[MAX]",
448                       "Get the most recently added MAX tracks" },
449   { "part",           3, 3, cf_part, 0, "TRACK CONTEXT PART",
450                       "Find a track name part" },
451   { "pause",          0, 0, cf_pause, 0, "",
452                       "Pause the currently playing track" },
453   { "play",           1, INT_MAX, cf_play, isarg_filename, "TRACKS...",
454                       "Add TRACKS to the end of the queue" },
455   { "playing",        0, 0, cf_playing, 0, "",
456                       "Report the playing track" },
457   { "prefs",          1, 1, cf_prefs, 0, "TRACK",
458                       "Display all the preferences for TRACK" },
459   { "quack",          0, 0, cf_quack, 0, 0, 0 },
460   { "queue",          0, 0, cf_queue, 0, "",
461                       "Display the current queue" },
462   { "random-disable", 0, 0, cf_random_disable, 0, "",
463                       "Disable random play" },
464   { "random-enable",  0, 0, cf_random_enable, 0, "",
465                       "Enable random play" },
466   { "recent",         0, 0, cf_recent, 0, "",
467                       "Display recently played track" },
468   { "reconfigure",    0, 0, cf_reconfigure, 0, "",
469                       "Reconfigure the daemon" },
470   { "remove",         1, 1, cf_remove, 0, "TRACK",
471                       "Remove a track from the queue" },
472   { "rescan",         0, 0, cf_rescan, 0, "",
473                       "Rescan for new tracks" },
474   { "resolve",        1, 1, cf_resolve, 0, "TRACK",
475                       "Resolve alias for TRACK" },
476   { "resume",         0, 0, cf_resume, 0, "",
477                       "Resume after a pause" },
478   { "rtp-address",    0, 0, cf_rtp_address, 0, "",
479                       "Report server's broadcast address" },
480   { "scratch",        0, 0, cf_scratch, 0, "",
481                       "Scratch the currently playing track" },
482   { "scratch-id",     1, 1, cf_scratch, 0, "ID",
483                       "Scratch the currently playing track" },
484   { "search",         1, 1, cf_search, 0, "WORDS",
485                       "Display tracks matching all the words" },
486   { "set",            3, 3, cf_set, 0, "TRACK NAME VALUE",
487                       "Set a preference value" },
488   { "set-global",     2, 2, cf_set_global, 0, "NAME VALUE",
489                       "Set a global preference value" },
490   { "set-volume",     2, 2, cf_set_volume, 0, "LEFT RIGHT",
491                       "Set the volume" },
492   { "shutdown",       0, 0, cf_shutdown, 0, "",
493                       "Shut down the daemon" },
494   { "stats",          0, 0, cf_stats, 0, "",
495                       "Display server statistics" },
496   { "tags",           0, 0, cf_tags, 0, "",
497                       "List known tags" },
498   { "unset",          2, 2, cf_unset, 0, "TRACK NAME",
499                       "Unset a preference" },
500   { "unset-global",   1, 1, cf_unset_global, 0, "NAME",
501                       "Unset a global preference" },
502   { "version",        0, 0, cf_version, 0, "",
503                       "Display the server version" },
504 };
505
506 static void help_commands(void) {
507   unsigned n, max = 0, l;
508
509   xprintf("Command summary:\n");
510   for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
511     if(!commands[n].desc) continue;
512     l = strlen(commands[n].name);
513     if(*commands[n].argstr)
514       l += strlen(commands[n].argstr) + 1;
515     if(l > max)
516       max = l;
517   }
518   for(n = 0; n < sizeof commands / sizeof *commands; ++n) {
519     if(!commands[n].desc) continue;
520     l = strlen(commands[n].name);
521     if(*commands[n].argstr)
522       l += strlen(commands[n].argstr) + 1;
523     xprintf("  %s%s%s%*s  %s\n", commands[n].name,
524             *commands[n].argstr ? " " : "",
525             commands[n].argstr,
526             max - l, "",
527             commands[n].desc);
528   }
529   xfclose(stdout);
530   exit(0);
531 }
532
533 int main(int argc, char **argv) {
534   int n, i, j;
535   disorder_client *c = 0;
536   int status = 0;
537   struct vector args;
538
539   mem_init();
540   /* garbage-collect PCRE's memory */
541   pcre_malloc = xmalloc;
542   pcre_free = xfree;
543   if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale");
544   while((n = getopt_long(argc, argv, "hVc:dHL", options, 0)) >= 0) {
545     switch(n) {
546     case 'h': help();
547     case 'H': help_commands();
548     case 'V': version();
549     case 'c': configfile = optarg; break;
550     case 'd': debugging = 1; break;
551     default: fatal(0, "invalid option");
552     }
553   }
554   if(config_read(0)) fatal(0, "cannot read configuration");
555   if(!(c = disorder_new(1))) exit(EXIT_FAILURE);
556   if(disorder_connect(c)) exit(EXIT_FAILURE);
557   n = optind;
558   /* accumulate command args */
559   while(n < argc) {
560     if((i = TABLE_FIND(commands, struct command, name, argv[n])) < 0)
561       fatal(0, "unknown command '%s'", argv[n]);
562     if(n + commands[i].min >= argc)
563       fatal(0, "missing arguments to '%s'", argv[n]);
564     n++;
565     vector_init(&args);
566     for(j = 0; j < commands[i].min; ++j)
567       vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
568     for(; j < commands[i].max
569           && n + j < argc
570           && commands[i].isarg(argv[n + j]); ++j)
571       vector_append(&args, nullcheck(mb2utf8(argv[n + j])));
572     vector_terminate(&args);
573     commands[i].fn(c, args.vec);
574     n += j;
575   }
576   if(auto_reconfigure) {
577     assert(c != 0);
578     if(disorder_reconfigure(c)) exit(EXIT_FAILURE);
579   }
580   if(c && disorder_close(c)) exit(EXIT_FAILURE);
581   if(fclose(stdout) < 0) fatal(errno, "error closing stdout");
582   return status;
583 }
584
585 /*
586 Local Variables:
587 c-basic-offset:2
588 comment-column:40
589 End:
590 */