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