| 1 | /* |
| 2 | * This file is part of DisOrder. |
| 3 | * Copyright (C) 2004, 2005, 2006, 2007 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 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> |
| 37 | #include <pcre.h> |
| 38 | #include <ctype.h> |
| 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" |
| 56 | #include "version.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 | { 0, 0, 0, 0 } |
| 71 | }; |
| 72 | |
| 73 | /* display usage message and terminate */ |
| 74 | static void help(void) { |
| 75 | xprintf("Usage:\n" |
| 76 | " disorder [OPTIONS] COMMAND ...\n" |
| 77 | "Options:\n" |
| 78 | " --help, -h Display usage message\n" |
| 79 | " --help-commands, -H List commands\n" |
| 80 | " --version, -V Display version number\n" |
| 81 | " --config PATH, -c PATH Set configuration file\n" |
| 82 | " --local, -l Force connection to local server\n" |
| 83 | " --debug, -d Turn on debugging\n"); |
| 84 | xfclose(stdout); |
| 85 | exit(0); |
| 86 | } |
| 87 | |
| 88 | static disorder_client *getclient(void) { |
| 89 | if(!client) { |
| 90 | if(!(client = disorder_new(1))) exit(EXIT_FAILURE); |
| 91 | if(disorder_connect(client)) exit(EXIT_FAILURE); |
| 92 | } |
| 93 | return client; |
| 94 | } |
| 95 | |
| 96 | static void cf_version(char attribute((unused)) **argv) { |
| 97 | char *v; |
| 98 | |
| 99 | if(disorder_version(getclient(), &v)) exit(EXIT_FAILURE); |
| 100 | xprintf("%s\n", nullcheck(utf82mb(v))); |
| 101 | } |
| 102 | |
| 103 | static void print_queue_entry(const struct queue_entry *q) { |
| 104 | if(q->track) xprintf("track %s\n", nullcheck(utf82mb(q->track))); |
| 105 | if(q->id) xprintf(" id %s\n", nullcheck(utf82mb(q->id))); |
| 106 | if(q->submitter) xprintf(" submitted by %s at %s", |
| 107 | nullcheck(utf82mb(q->submitter)), ctime(&q->when)); |
| 108 | if(q->played) xprintf(" played at %s", ctime(&q->played)); |
| 109 | if(q->state == playing_started |
| 110 | || q->state == playing_paused) xprintf(" %lds so far", q->sofar); |
| 111 | else if(q->expected) xprintf(" might start at %s", ctime(&q->expected)); |
| 112 | if(q->scratched) xprintf(" scratched by %s\n", |
| 113 | nullcheck(utf82mb(q->scratched))); |
| 114 | else xprintf(" %s\n", playing_states[q->state]); |
| 115 | if(q->wstat) xprintf(" %s\n", wstat(q->wstat)); |
| 116 | } |
| 117 | |
| 118 | static void cf_playing(char attribute((unused)) **argv) { |
| 119 | struct queue_entry *q; |
| 120 | |
| 121 | if(disorder_playing(getclient(), &q)) exit(EXIT_FAILURE); |
| 122 | if(q) |
| 123 | print_queue_entry(q); |
| 124 | else |
| 125 | xprintf("nothing\n"); |
| 126 | } |
| 127 | |
| 128 | static void cf_play(char **argv) { |
| 129 | while(*argv) |
| 130 | if(disorder_play(getclient(), *argv++)) exit(EXIT_FAILURE); |
| 131 | } |
| 132 | |
| 133 | static void cf_remove(char **argv) { |
| 134 | if(disorder_remove(getclient(), argv[0])) exit(EXIT_FAILURE); |
| 135 | } |
| 136 | |
| 137 | static void cf_disable(char attribute((unused)) **argv) { |
| 138 | if(disorder_disable(getclient())) exit(EXIT_FAILURE); |
| 139 | } |
| 140 | |
| 141 | static void cf_enable(char attribute((unused)) **argv) { |
| 142 | if(disorder_enable(getclient())) exit(EXIT_FAILURE); |
| 143 | } |
| 144 | |
| 145 | static void cf_scratch(char **argv) { |
| 146 | if(disorder_scratch(getclient(), argv[0])) exit(EXIT_FAILURE); |
| 147 | } |
| 148 | |
| 149 | static void cf_shutdown(char attribute((unused)) **argv) { |
| 150 | if(disorder_shutdown(getclient())) exit(EXIT_FAILURE); |
| 151 | } |
| 152 | |
| 153 | static void cf_reconfigure(char attribute((unused)) **argv) { |
| 154 | /* Re-check configuration for server */ |
| 155 | if(config_read(1)) fatal(0, "cannot read configuration"); |
| 156 | if(disorder_reconfigure(getclient())) exit(EXIT_FAILURE); |
| 157 | } |
| 158 | |
| 159 | static void cf_rescan(char attribute((unused)) **argv) { |
| 160 | if(disorder_rescan(getclient())) exit(EXIT_FAILURE); |
| 161 | } |
| 162 | |
| 163 | static void cf_somequeue(int (*fn)(disorder_client *c, |
| 164 | struct queue_entry **qp)) { |
| 165 | struct queue_entry *q; |
| 166 | |
| 167 | if(fn(getclient(), &q)) exit(EXIT_FAILURE); |
| 168 | while(q) { |
| 169 | print_queue_entry(q); |
| 170 | q = q->next; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | static void cf_recent(char attribute((unused)) **argv) { |
| 175 | cf_somequeue(disorder_recent); |
| 176 | } |
| 177 | |
| 178 | static void cf_queue(char attribute((unused)) **argv) { |
| 179 | cf_somequeue(disorder_queue); |
| 180 | } |
| 181 | |
| 182 | static void cf_quack(char attribute((unused)) **argv) { |
| 183 | xprintf("\n" |
| 184 | " .------------------.\n" |
| 185 | " | Naath is a babe! |\n" |
| 186 | " `---------+--------'\n" |
| 187 | " \\\n" |
| 188 | " >0\n" |
| 189 | " (<)'\n" |
| 190 | "~~~~~~~~~~~~~~~~~~~~~~\n" |
| 191 | "\n"); |
| 192 | } |
| 193 | |
| 194 | static void cf_somelist(char **argv, |
| 195 | int (*fn)(disorder_client *c, |
| 196 | const char *arg, const char *re, |
| 197 | char ***vecp, int *nvecp)) { |
| 198 | char **vec; |
| 199 | const char *re; |
| 200 | |
| 201 | if(argv[1]) |
| 202 | re = xstrdup(argv[1] + 1); |
| 203 | else |
| 204 | re = 0; |
| 205 | if(fn(getclient(), argv[0], re, &vec, 0)) exit(EXIT_FAILURE); |
| 206 | while(*vec) |
| 207 | xprintf("%s\n", nullcheck(utf82mb(*vec++))); |
| 208 | } |
| 209 | |
| 210 | static int isarg_regexp(const char *s) { |
| 211 | return s[0] == '~'; |
| 212 | } |
| 213 | |
| 214 | static void cf_dirs(char **argv) { |
| 215 | cf_somelist(argv, disorder_directories); |
| 216 | } |
| 217 | |
| 218 | static void cf_files(char **argv) { |
| 219 | cf_somelist(argv, disorder_files); |
| 220 | } |
| 221 | |
| 222 | static void cf_allfiles(char **argv) { |
| 223 | cf_somelist(argv, disorder_allfiles); |
| 224 | } |
| 225 | |
| 226 | static void cf_get(char **argv) { |
| 227 | char *value; |
| 228 | |
| 229 | if(disorder_get(getclient(), argv[0], argv[1], &value)) exit(EXIT_FAILURE); |
| 230 | xprintf("%s\n", nullcheck(utf82mb(value))); |
| 231 | } |
| 232 | |
| 233 | static void cf_length(char **argv) { |
| 234 | long length; |
| 235 | |
| 236 | if(disorder_length(getclient(), argv[0], &length)) exit(EXIT_FAILURE); |
| 237 | xprintf("%ld\n", length); |
| 238 | } |
| 239 | |
| 240 | static void cf_set(char **argv) { |
| 241 | if(disorder_set(getclient(), argv[0], argv[1], argv[2])) exit(EXIT_FAILURE); |
| 242 | } |
| 243 | |
| 244 | static void cf_unset(char **argv) { |
| 245 | if(disorder_unset(getclient(), argv[0], argv[1])) exit(EXIT_FAILURE); |
| 246 | } |
| 247 | |
| 248 | static void cf_prefs(char **argv) { |
| 249 | struct kvp *k; |
| 250 | |
| 251 | if(disorder_prefs(getclient(), argv[0], &k)) exit(EXIT_FAILURE); |
| 252 | for(; k; k = k->next) |
| 253 | xprintf("%s = %s\n", |
| 254 | nullcheck(utf82mb(k->name)), nullcheck(utf82mb(k->value))); |
| 255 | } |
| 256 | |
| 257 | static void cf_search(char **argv) { |
| 258 | char **results; |
| 259 | int nresults, n; |
| 260 | |
| 261 | if(disorder_search(getclient(), *argv, &results, &nresults)) exit(EXIT_FAILURE); |
| 262 | for(n = 0; n < nresults; ++n) |
| 263 | xprintf("%s\n", nullcheck(utf82mb(results[n]))); |
| 264 | } |
| 265 | |
| 266 | static void cf_random_disable(char attribute((unused)) **argv) { |
| 267 | if(disorder_random_disable(getclient())) exit(EXIT_FAILURE); |
| 268 | } |
| 269 | |
| 270 | static void cf_random_enable(char attribute((unused)) **argv) { |
| 271 | if(disorder_random_enable(getclient())) exit(EXIT_FAILURE); |
| 272 | } |
| 273 | |
| 274 | static void cf_stats(char attribute((unused)) **argv) { |
| 275 | char **vec; |
| 276 | |
| 277 | if(disorder_stats(getclient(), &vec, 0)) exit(EXIT_FAILURE); |
| 278 | while(*vec) |
| 279 | xprintf("%s\n", nullcheck(utf82mb(*vec++))); |
| 280 | } |
| 281 | |
| 282 | static void cf_get_volume(char attribute((unused)) **argv) { |
| 283 | int l, r; |
| 284 | |
| 285 | if(disorder_get_volume(getclient(), &l, &r)) exit(EXIT_FAILURE); |
| 286 | xprintf("%d %d\n", l, r); |
| 287 | } |
| 288 | |
| 289 | static void cf_set_volume(char **argv) { |
| 290 | if(disorder_set_volume(getclient(), atoi(argv[0]), atoi(argv[1]))) exit(EXIT_FAILURE); |
| 291 | } |
| 292 | |
| 293 | static void cf_log(char attribute((unused)) **argv) { |
| 294 | if(disorder_log(getclient(), sink_stdio("stdout", stdout))) exit(EXIT_FAILURE); |
| 295 | } |
| 296 | |
| 297 | static void cf_move(char **argv) { |
| 298 | long n; |
| 299 | int e; |
| 300 | |
| 301 | if((e = xstrtol(&n, argv[1], 0, 10))) |
| 302 | fatal(e, "cannot convert '%s'", argv[1]); |
| 303 | if(n > INT_MAX || n < INT_MIN) |
| 304 | fatal(e, "%ld out of range", n); |
| 305 | if(disorder_move(getclient(), argv[0], (int)n)) exit(EXIT_FAILURE); |
| 306 | } |
| 307 | |
| 308 | static void cf_part(char **argv) { |
| 309 | char *s; |
| 310 | |
| 311 | if(disorder_part(getclient(), &s, argv[0], argv[1], argv[2])) exit(EXIT_FAILURE); |
| 312 | xprintf("%s\n", nullcheck(utf82mb(s))); |
| 313 | } |
| 314 | |
| 315 | static int isarg_filename(const char *s) { |
| 316 | return s[0] == '/'; |
| 317 | } |
| 318 | |
| 319 | static void cf_authorize(char **argv) { |
| 320 | authorize(getclient(), argv[0], argv[1]); |
| 321 | } |
| 322 | |
| 323 | static void cf_resolve(char **argv) { |
| 324 | char *track; |
| 325 | |
| 326 | if(disorder_resolve(getclient(), &track, argv[0])) exit(EXIT_FAILURE); |
| 327 | xprintf("%s\n", nullcheck(utf82mb(track))); |
| 328 | } |
| 329 | |
| 330 | static void cf_pause(char attribute((unused)) **argv) { |
| 331 | if(disorder_pause(getclient())) exit(EXIT_FAILURE); |
| 332 | } |
| 333 | |
| 334 | static void cf_resume(char attribute((unused)) **argv) { |
| 335 | if(disorder_resume(getclient())) exit(EXIT_FAILURE); |
| 336 | } |
| 337 | |
| 338 | static void cf_tags(char attribute((unused)) **argv) { |
| 339 | char **vec; |
| 340 | |
| 341 | if(disorder_tags(getclient(), &vec, 0)) exit(EXIT_FAILURE); |
| 342 | while(*vec) |
| 343 | xprintf("%s\n", nullcheck(utf82mb(*vec++))); |
| 344 | } |
| 345 | |
| 346 | static void cf_users(char attribute((unused)) **argv) { |
| 347 | char **vec; |
| 348 | |
| 349 | if(disorder_users(getclient(), &vec, 0)) exit(EXIT_FAILURE); |
| 350 | while(*vec) |
| 351 | xprintf("%s\n", nullcheck(utf82mb(*vec++))); |
| 352 | } |
| 353 | |
| 354 | static void cf_get_global(char **argv) { |
| 355 | char *value; |
| 356 | |
| 357 | if(disorder_get_global(getclient(), argv[0], &value)) exit(EXIT_FAILURE); |
| 358 | xprintf("%s\n", nullcheck(utf82mb(value))); |
| 359 | } |
| 360 | |
| 361 | static void cf_set_global(char **argv) { |
| 362 | if(disorder_set_global(getclient(), argv[0], argv[1])) exit(EXIT_FAILURE); |
| 363 | } |
| 364 | |
| 365 | static void cf_unset_global(char **argv) { |
| 366 | if(disorder_unset_global(getclient(), argv[0])) exit(EXIT_FAILURE); |
| 367 | } |
| 368 | |
| 369 | static int isarg_integer(const char *s) { |
| 370 | if(!*s) return 0; |
| 371 | while(*s) { |
| 372 | if(!isdigit((unsigned char)*s)) |
| 373 | return 0; |
| 374 | ++s; |
| 375 | } |
| 376 | return 1; |
| 377 | } |
| 378 | |
| 379 | static void cf_new(char **argv) { |
| 380 | char **vec; |
| 381 | |
| 382 | if(disorder_new_tracks(getclient(), &vec, 0, argv[0] ? atoi(argv[0]) : 0)) |
| 383 | exit(EXIT_FAILURE); |
| 384 | while(*vec) |
| 385 | xprintf("%s\n", nullcheck(utf82mb(*vec++))); |
| 386 | } |
| 387 | |
| 388 | static void cf_rtp_address(char attribute((unused)) **argv) { |
| 389 | char *address, *port; |
| 390 | |
| 391 | if(disorder_rtp_address(getclient(), &address, &port)) exit(EXIT_FAILURE); |
| 392 | xprintf("address: %s\nport: %s\n", address, port); |
| 393 | } |
| 394 | |
| 395 | static int isarg_rights(const char *arg) { |
| 396 | return strchr(arg, ',') || !parse_rights(arg, 0, 0); |
| 397 | } |
| 398 | |
| 399 | static void cf_adduser(char **argv) { |
| 400 | if(disorder_adduser(getclient(), argv[0], argv[1], argv[2])) |
| 401 | exit(EXIT_FAILURE); |
| 402 | } |
| 403 | |
| 404 | static void cf_deluser(char **argv) { |
| 405 | if(disorder_deluser(getclient(), argv[0])) |
| 406 | exit(EXIT_FAILURE); |
| 407 | } |
| 408 | |
| 409 | static void cf_edituser(char **argv) { |
| 410 | if(disorder_edituser(getclient(), argv[0], argv[1], argv[2])) |
| 411 | exit(EXIT_FAILURE); |
| 412 | } |
| 413 | |
| 414 | static void cf_userinfo(char **argv) { |
| 415 | char *s; |
| 416 | |
| 417 | if(disorder_userinfo(getclient(), argv[0], argv[1], &s)) |
| 418 | exit(EXIT_FAILURE); |
| 419 | xprintf("%s\n", nullcheck(utf82mb(s))); |
| 420 | } |
| 421 | |
| 422 | static int isarg_option(const char *arg) { |
| 423 | return arg[0] == '-'; |
| 424 | } |
| 425 | |
| 426 | static int argvlen(char **argv) { |
| 427 | char **start = argv; |
| 428 | |
| 429 | while(*argv) |
| 430 | ++argv; |
| 431 | return argv - start; |
| 432 | } |
| 433 | |
| 434 | static const struct option setup_guest_options[] = { |
| 435 | { "help", no_argument, 0, 'h' }, |
| 436 | { "online-registration", no_argument, 0, 'r' }, |
| 437 | { "no-online-registration", no_argument, 0, 'R' }, |
| 438 | { 0, 0, 0, 0 } |
| 439 | }; |
| 440 | |
| 441 | static void help_setup_guest(void) { |
| 442 | xprintf("Usage:\n" |
| 443 | " disorder setup-guest [OPTIONS]\n" |
| 444 | "Options:\n" |
| 445 | " --help, -h Display usage message\n" |
| 446 | " --online-registration Enable online registration (default)\n" |
| 447 | " --no-online-registration Disable online registration\n"); |
| 448 | xfclose(stdout); |
| 449 | exit(0); |
| 450 | } |
| 451 | |
| 452 | static void cf_setup_guest(char **argv) { |
| 453 | int n, online_registration = 1; |
| 454 | |
| 455 | while((n = getopt_long(argvlen(argv) + 1, argv - 1, |
| 456 | "hrR", setup_guest_options, 0)) >= 0) { |
| 457 | switch(n) { |
| 458 | case 'h': help_setup_guest(); |
| 459 | case 'r': online_registration = 1; break; |
| 460 | case 'R': online_registration = 0; break; |
| 461 | default: fatal(0, "invalid option"); |
| 462 | } |
| 463 | } |
| 464 | if(online_registration && !config->mail_sender) |
| 465 | fatal(0, "you MUST set mail_sender if you want online registration"); |
| 466 | if(disorder_adduser(getclient(), "guest", "", |
| 467 | online_registration ? "read,register" : "read")) |
| 468 | exit(EXIT_FAILURE); |
| 469 | } |
| 470 | |
| 471 | static const struct command { |
| 472 | const char *name; |
| 473 | int min, max; |
| 474 | void (*fn)(char **); |
| 475 | int (*isarg)(const char *); |
| 476 | const char *argstr, *desc; |
| 477 | } commands[] = { |
| 478 | { "adduser", 2, 3, cf_adduser, isarg_rights, "USER PASSWORD [RIGHTS]", |
| 479 | "Create a new user" }, |
| 480 | { "allfiles", 1, 2, cf_allfiles, isarg_regexp, "DIR [~REGEXP]", |
| 481 | "List all files and directories in DIR" }, |
| 482 | { "authorize", 1, 2, cf_authorize, isarg_rights, "USER [RIGHTS]", |
| 483 | "Authorize USER to connect to the server" }, |
| 484 | { "deluser", 1, 1, cf_deluser, 0, "USER", |
| 485 | "Delete a user" }, |
| 486 | { "dirs", 1, 2, cf_dirs, isarg_regexp, "DIR [~REGEXP]", |
| 487 | "List directories in DIR" }, |
| 488 | { "disable", 0, 0, cf_disable, 0, "", |
| 489 | "Disable play" }, |
| 490 | { "disable-random", 0, 0, cf_random_disable, 0, "", |
| 491 | "Disable random play" }, |
| 492 | { "edituser", 3, 3, cf_edituser, 0, "USER PROPERTY VALUE", |
| 493 | "Set a property of a user" }, |
| 494 | { "enable", 0, 0, cf_enable, 0, "", |
| 495 | "Enable play" }, |
| 496 | { "enable-random", 0, 0, cf_random_enable, 0, "", |
| 497 | "Enable random play" }, |
| 498 | { "files", 1, 2, cf_files, isarg_regexp, "DIR [~REGEXP]", |
| 499 | "List files in DIR" }, |
| 500 | { "get", 2, 2, cf_get, 0, "TRACK NAME", |
| 501 | "Get a preference value" }, |
| 502 | { "get-global", 1, 1, cf_get_global, 0, "NAME", |
| 503 | "Get a global preference value" }, |
| 504 | { "get-volume", 0, 0, cf_get_volume, 0, "", |
| 505 | "Get the current volume" }, |
| 506 | { "length", 1, 1, cf_length, 0, "TRACK", |
| 507 | "Get the length of TRACK in seconds" }, |
| 508 | { "log", 0, 0, cf_log, 0, "", |
| 509 | "Copy event log to stdout" }, |
| 510 | { "move", 2, 2, cf_move, 0, "TRACK DELTA", |
| 511 | "Move a track in the queue" }, |
| 512 | { "new", 0, 1, cf_new, isarg_integer, "[MAX]", |
| 513 | "Get the most recently added MAX tracks" }, |
| 514 | { "part", 3, 3, cf_part, 0, "TRACK CONTEXT PART", |
| 515 | "Find a track name part" }, |
| 516 | { "pause", 0, 0, cf_pause, 0, "", |
| 517 | "Pause the currently playing track" }, |
| 518 | { "play", 1, INT_MAX, cf_play, isarg_filename, "TRACKS...", |
| 519 | "Add TRACKS to the end of the queue" }, |
| 520 | { "playing", 0, 0, cf_playing, 0, "", |
| 521 | "Report the playing track" }, |
| 522 | { "prefs", 1, 1, cf_prefs, 0, "TRACK", |
| 523 | "Display all the preferences for TRACK" }, |
| 524 | { "quack", 0, 0, cf_quack, 0, 0, 0 }, |
| 525 | { "queue", 0, 0, cf_queue, 0, "", |
| 526 | "Display the current queue" }, |
| 527 | { "random-disable", 0, 0, cf_random_disable, 0, "", |
| 528 | "Disable random play" }, |
| 529 | { "random-enable", 0, 0, cf_random_enable, 0, "", |
| 530 | "Enable random play" }, |
| 531 | { "recent", 0, 0, cf_recent, 0, "", |
| 532 | "Display recently played track" }, |
| 533 | { "reconfigure", 0, 0, cf_reconfigure, 0, "", |
| 534 | "Reconfigure the daemon" }, |
| 535 | { "remove", 1, 1, cf_remove, 0, "TRACK", |
| 536 | "Remove a track from the queue" }, |
| 537 | { "rescan", 0, 0, cf_rescan, 0, "", |
| 538 | "Rescan for new tracks" }, |
| 539 | { "resolve", 1, 1, cf_resolve, 0, "TRACK", |
| 540 | "Resolve alias for TRACK" }, |
| 541 | { "resume", 0, 0, cf_resume, 0, "", |
| 542 | "Resume after a pause" }, |
| 543 | { "rtp-address", 0, 0, cf_rtp_address, 0, "", |
| 544 | "Report server's broadcast address" }, |
| 545 | { "scratch", 0, 0, cf_scratch, 0, "", |
| 546 | "Scratch the currently playing track" }, |
| 547 | { "scratch-id", 1, 1, cf_scratch, 0, "ID", |
| 548 | "Scratch the currently playing track" }, |
| 549 | { "search", 1, 1, cf_search, 0, "WORDS", |
| 550 | "Display tracks matching all the words" }, |
| 551 | { "set", 3, 3, cf_set, 0, "TRACK NAME VALUE", |
| 552 | "Set a preference value" }, |
| 553 | { "set-global", 2, 2, cf_set_global, 0, "NAME VALUE", |
| 554 | "Set a global preference value" }, |
| 555 | { "set-volume", 2, 2, cf_set_volume, 0, "LEFT RIGHT", |
| 556 | "Set the volume" }, |
| 557 | { "setup-guest", 0, INT_MAX, cf_setup_guest, isarg_option, "[OPTIONS]", |
| 558 | "Create the guest login" }, |
| 559 | { "shutdown", 0, 0, cf_shutdown, 0, "", |
| 560 | "Shut down the daemon" }, |
| 561 | { "stats", 0, 0, cf_stats, 0, "", |
| 562 | "Display server statistics" }, |
| 563 | { "tags", 0, 0, cf_tags, 0, "", |
| 564 | "List known tags" }, |
| 565 | { "unset", 2, 2, cf_unset, 0, "TRACK NAME", |
| 566 | "Unset a preference" }, |
| 567 | { "unset-global", 1, 1, cf_unset_global, 0, "NAME", |
| 568 | "Unset a global preference" }, |
| 569 | { "userinfo", 2, 2, cf_userinfo, 0, "USER PROPERTY", |
| 570 | "Get a property of as user" }, |
| 571 | { "users", 0, 0, cf_users, 0, "", |
| 572 | "List all users" }, |
| 573 | { "version", 0, 0, cf_version, 0, "", |
| 574 | "Display the server version" }, |
| 575 | }; |
| 576 | |
| 577 | static void help_commands(void) { |
| 578 | unsigned n, max = 0, l; |
| 579 | |
| 580 | xprintf("Command summary:\n"); |
| 581 | for(n = 0; n < sizeof commands / sizeof *commands; ++n) { |
| 582 | if(!commands[n].desc) continue; |
| 583 | l = strlen(commands[n].name); |
| 584 | if(*commands[n].argstr) |
| 585 | l += strlen(commands[n].argstr) + 1; |
| 586 | if(l > max) |
| 587 | max = l; |
| 588 | } |
| 589 | for(n = 0; n < sizeof commands / sizeof *commands; ++n) { |
| 590 | if(!commands[n].desc) continue; |
| 591 | l = strlen(commands[n].name); |
| 592 | if(*commands[n].argstr) |
| 593 | l += strlen(commands[n].argstr) + 1; |
| 594 | xprintf(" %s%s%s%*s %s\n", commands[n].name, |
| 595 | *commands[n].argstr ? " " : "", |
| 596 | commands[n].argstr, |
| 597 | max - l, "", |
| 598 | commands[n].desc); |
| 599 | } |
| 600 | xfclose(stdout); |
| 601 | exit(0); |
| 602 | } |
| 603 | |
| 604 | int main(int argc, char **argv) { |
| 605 | int n, i, j, local = 0; |
| 606 | int status = 0; |
| 607 | struct vector args; |
| 608 | const char *user = 0, *password = 0; |
| 609 | |
| 610 | mem_init(); |
| 611 | /* garbage-collect PCRE's memory */ |
| 612 | pcre_malloc = xmalloc; |
| 613 | pcre_free = xfree; |
| 614 | if(!setlocale(LC_CTYPE, "")) fatal(errno, "error calling setlocale"); |
| 615 | while((n = getopt_long(argc, argv, "+hVc:dHlNu:p:", options, 0)) >= 0) { |
| 616 | switch(n) { |
| 617 | case 'h': help(); |
| 618 | case 'H': help_commands(); |
| 619 | case 'V': version("disorder"); |
| 620 | case 'c': configfile = optarg; break; |
| 621 | case 'd': debugging = 1; break; |
| 622 | case 'l': local = 1; break; |
| 623 | case 'N': config_per_user = 0; break; |
| 624 | case 'u': user = optarg; break; |
| 625 | case 'p': password = optarg; break; |
| 626 | default: fatal(0, "invalid option"); |
| 627 | } |
| 628 | } |
| 629 | if(config_read(0)) fatal(0, "cannot read configuration"); |
| 630 | if(user) { |
| 631 | config->username = user; |
| 632 | config->password = 0; |
| 633 | } |
| 634 | if(password) |
| 635 | config->password = password; |
| 636 | if(local) |
| 637 | config->connect.n = 0; |
| 638 | n = optind; |
| 639 | optind = 1; /* for subsequent getopt calls */ |
| 640 | /* accumulate command args */ |
| 641 | while(n < argc) { |
| 642 | if((i = TABLE_FIND(commands, struct command, name, argv[n])) < 0) |
| 643 | fatal(0, "unknown command '%s'", argv[n]); |
| 644 | if(n + commands[i].min >= argc) |
| 645 | fatal(0, "missing arguments to '%s'", argv[n]); |
| 646 | vector_init(&args); |
| 647 | /* Include the command name in the args, but at element -1, for |
| 648 | * the benefit of subcommand getopt calls */ |
| 649 | vector_append(&args, argv[n]); |
| 650 | n++; |
| 651 | for(j = 0; j < commands[i].min; ++j) |
| 652 | vector_append(&args, nullcheck(mb2utf8(argv[n + j]))); |
| 653 | for(; j < commands[i].max |
| 654 | && n + j < argc |
| 655 | && commands[i].isarg(argv[n + j]); ++j) |
| 656 | vector_append(&args, nullcheck(mb2utf8(argv[n + j]))); |
| 657 | vector_terminate(&args); |
| 658 | commands[i].fn(args.vec + 1); |
| 659 | n += j; |
| 660 | } |
| 661 | if(client && disorder_close(client)) exit(EXIT_FAILURE); |
| 662 | if(fclose(stdout) < 0) fatal(errno, "error closing stdout"); |
| 663 | return status; |
| 664 | } |
| 665 | |
| 666 | /* |
| 667 | Local Variables: |
| 668 | c-basic-offset:2 |
| 669 | comment-column:40 |
| 670 | End: |
| 671 | */ |