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