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