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