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