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