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