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