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