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