2 * This file is part of DisOrder.
3 * Copyright (C) 2004-2009 Richard Kettlewell
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.
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.
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/>.
18 /** @file clients/disorder.c
19 * @brief Command-line client
25 #include <sys/types.h>
26 #include <sys/socket.h>
38 #include "configuration.h"
51 #include "authorize.h"
54 #include "dateparse.h"
56 #include "inputline.h"
58 static disorder_client
*client
;
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' },
74 /* display usage message and terminate */
75 static void help(void) {
77 " disorder [OPTIONS] COMMAND ...\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");
89 static disorder_client
*getclient(void) {
91 if(!(client
= disorder_new(1))) exit(EXIT_FAILURE
);
92 if(disorder_connect(client
)) exit(EXIT_FAILURE
);
97 static void cf_version(char attribute((unused
)) **argv
) {
100 if(disorder_version(getclient(), &v
)) exit(EXIT_FAILURE
);
101 v
= nullcheck(utf82mb_f(v
));
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
)));
112 case origin_scheduled
:
113 xprintf(" %s by %s at %s",
114 track_origins
[q
->origin
],
115 nullcheck(utf82mb(q
->submitter
)), ctime(&q
->when
));
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
));
130 static void cf_playing(char attribute((unused
)) **argv
) {
131 struct queue_entry
*q
;
133 if(disorder_playing(getclient(), &q
)) exit(EXIT_FAILURE
);
135 print_queue_entry(q
);
137 xprintf("nothing\n");
140 static void cf_play(char **argv
) {
142 if(disorder_play(getclient(), *argv
++)) exit(EXIT_FAILURE
);
145 static void cf_remove(char **argv
) {
146 if(disorder_remove(getclient(), argv
[0])) exit(EXIT_FAILURE
);
149 static void cf_disable(char attribute((unused
)) **argv
) {
150 if(disorder_disable(getclient())) exit(EXIT_FAILURE
);
153 static void cf_enable(char attribute((unused
)) **argv
) {
154 if(disorder_enable(getclient())) exit(EXIT_FAILURE
);
157 static void cf_scratch(char **argv
) {
158 if(disorder_scratch(getclient(), argv
[0])) exit(EXIT_FAILURE
);
161 static void cf_shutdown(char attribute((unused
)) **argv
) {
162 if(disorder_shutdown(getclient())) exit(EXIT_FAILURE
);
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
);
172 static void cf_rescan(char attribute((unused
)) **argv
) {
173 if(disorder_rescan(getclient())) exit(EXIT_FAILURE
);
176 static void cf_somequeue(int (*fn
)(disorder_client
*c
,
177 struct queue_entry
**qp
)) {
178 struct queue_entry
*q
;
180 if(fn(getclient(), &q
)) exit(EXIT_FAILURE
);
182 print_queue_entry(q
);
187 static void cf_recent(char attribute((unused
)) **argv
) {
188 cf_somequeue(disorder_recent
);
191 static void cf_queue(char attribute((unused
)) **argv
) {
192 cf_somequeue(disorder_queue
);
195 static void cf_quack(char attribute((unused
)) **argv
) {
196 if(!strcasecmp(nl_langinfo(CODESET
), "utf-8")) {
197 #define TL "\xE2\x95\xAD"
198 #define TR "\xE2\x95\xAE"
199 #define BR "\xE2\x95\xAF"
200 #define BL "\xE2\x95\xB0"
201 #define H "\xE2\x94\x80"
202 #define V "\xE2\x94\x82"
203 #define T "\xE2\x94\xAC"
205 " "TL H H H H H H H H H H H H H H H H H H TR
"\n"
206 " "V
" Naath is a babe! "V
"\n"
207 " "BL H H H H H H H H H T H H H H H H H H BR
"\n"
211 "~~~~~~~~~~~~~~~~~~~~~~\n"
215 " .------------------.\n"
216 " | Naath is a babe! |\n"
217 " `---------+--------'\n"
221 "~~~~~~~~~~~~~~~~~~~~~~\n"
226 static void cf_somelist(char **argv
,
227 int (*fn
)(disorder_client
*c
,
228 const char *arg
, const char *re
,
229 char ***vecp
, int *nvecp
)) {
234 re
= xstrdup(argv
[1] + 1);
237 if(fn(getclient(), argv
[0], re
, &base
, 0)) exit(EXIT_FAILURE
);
238 for(vec
= base
; *vec
; ++vec
)
239 xprintf("%s\n", nullcheck(utf82mb_f(*vec
)));
243 static int isarg_regexp(const char *s
) {
247 static void cf_dirs(char **argv
) {
248 cf_somelist(argv
, disorder_directories
);
251 static void cf_files(char **argv
) {
252 cf_somelist(argv
, disorder_files
);
255 static void cf_allfiles(char **argv
) {
256 cf_somelist(argv
, disorder_allfiles
);
259 static void cf_get(char **argv
) {
262 if(disorder_get(getclient(), argv
[0], argv
[1], &value
)) exit(EXIT_FAILURE
);
263 xprintf("%s\n", nullcheck(utf82mb_f(value
)));
266 static void cf_length(char **argv
) {
269 if(disorder_length(getclient(), argv
[0], &length
)) exit(EXIT_FAILURE
);
270 xprintf("%ld\n", length
);
273 static void cf_set(char **argv
) {
274 if(disorder_set(getclient(), argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
277 static void cf_unset(char **argv
) {
278 if(disorder_unset(getclient(), argv
[0], argv
[1])) exit(EXIT_FAILURE
);
281 static void cf_prefs(char **argv
) {
284 if(disorder_prefs(getclient(), argv
[0], &k
)) exit(EXIT_FAILURE
);
285 for(; k
; k
= k
->next
)
287 nullcheck(utf82mb(k
->name
)), nullcheck(utf82mb(k
->value
)));
290 static void cf_search(char **argv
) {
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
])));
299 static void cf_random_disable(char attribute((unused
)) **argv
) {
300 if(disorder_random_disable(getclient())) exit(EXIT_FAILURE
);
303 static void cf_random_enable(char attribute((unused
)) **argv
) {
304 if(disorder_random_enable(getclient())) exit(EXIT_FAILURE
);
307 static void cf_stats(char attribute((unused
)) **argv
) {
310 if(disorder_stats(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
312 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
315 static void cf_get_volume(char attribute((unused
)) **argv
) {
318 if(disorder_get_volume(getclient(), &l
, &r
)) exit(EXIT_FAILURE
);
319 xprintf("%d %d\n", l
, r
);
322 static void cf_set_volume(char **argv
) {
323 if(disorder_set_volume(getclient(), atoi(argv
[0]), atoi(argv
[1]))) exit(EXIT_FAILURE
);
326 static void cf_log(char attribute((unused
)) **argv
) {
327 if(disorder_log(getclient(), sink_stdio("stdout", stdout
))) exit(EXIT_FAILURE
);
330 static void cf_move(char **argv
) {
334 if((e
= xstrtol(&n
, argv
[1], 0, 10)))
335 disorder_fatal(e
, "cannot convert '%s'", argv
[1]);
336 if(n
> INT_MAX
|| n
< INT_MIN
)
337 disorder_fatal(e
, "%ld out of range", n
);
338 if(disorder_move(getclient(), argv
[0], (int)n
)) exit(EXIT_FAILURE
);
341 static void cf_part(char **argv
) {
344 if(disorder_part(getclient(), &s
, argv
[0], argv
[1], argv
[2])) exit(EXIT_FAILURE
);
345 xprintf("%s\n", nullcheck(utf82mb_f(s
)));
348 static int isarg_filename(const char *s
) {
352 static void cf_authorize(char **argv
) {
353 authorize(getclient(), argv
[0], argv
[1]);
356 static void cf_resolve(char **argv
) {
359 if(disorder_resolve(getclient(), &track
, argv
[0])) exit(EXIT_FAILURE
);
360 xprintf("%s\n", nullcheck(utf82mb_f(track
)));
363 static void cf_pause(char attribute((unused
)) **argv
) {
364 if(disorder_pause(getclient())) exit(EXIT_FAILURE
);
367 static void cf_resume(char attribute((unused
)) **argv
) {
368 if(disorder_resume(getclient())) exit(EXIT_FAILURE
);
371 static void cf_tags(char attribute((unused
)) **argv
) {
374 if(disorder_tags(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
376 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
379 static void cf_users(char attribute((unused
)) **argv
) {
382 if(disorder_users(getclient(), &vec
, 0)) exit(EXIT_FAILURE
);
384 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
387 static void cf_get_global(char **argv
) {
390 if(disorder_get_global(getclient(), argv
[0], &value
)) exit(EXIT_FAILURE
);
391 xprintf("%s\n", nullcheck(utf82mb_f(value
)));
394 static void cf_set_global(char **argv
) {
395 if(disorder_set_global(getclient(), argv
[0], argv
[1])) exit(EXIT_FAILURE
);
398 static void cf_unset_global(char **argv
) {
399 if(disorder_unset_global(getclient(), argv
[0])) exit(EXIT_FAILURE
);
402 static int isarg_integer(const char *s
) {
405 if(!isdigit((unsigned char)*s
))
412 static void cf_new(char **argv
) {
415 if(disorder_new_tracks(getclient(), &vec
, 0, argv
[0] ?
atoi(argv
[0]) : 0))
418 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
421 static void cf_rtp_address(char attribute((unused
)) **argv
) {
422 char *address
, *port
;
424 if(disorder_rtp_address(getclient(), &address
, &port
)) exit(EXIT_FAILURE
);
425 xprintf("address: %s\nport: %s\n", address
, port
);
428 static int isarg_rights(const char *arg
) {
429 return strchr(arg
, ',') || !parse_rights(arg
, 0, 0);
432 static void cf_adduser(char **argv
) {
433 if(disorder_adduser(getclient(), argv
[0], argv
[1], argv
[2]))
437 static void cf_deluser(char **argv
) {
438 if(disorder_deluser(getclient(), argv
[0]))
442 static void cf_edituser(char **argv
) {
443 if(disorder_edituser(getclient(), argv
[0], argv
[1], argv
[2]))
447 static void cf_userinfo(char **argv
) {
450 if(disorder_userinfo(getclient(), argv
[0], argv
[1], &s
))
452 xprintf("%s\n", nullcheck(utf82mb_f(s
)));
455 static int isarg_option(const char *arg
) {
456 return arg
[0] == '-';
459 static int argvlen(char **argv
) {
467 static const struct option setup_guest_options
[] = {
468 { "help", no_argument
, 0, 'h' },
469 { "online-registration", no_argument
, 0, 'r' },
470 { "no-online-registration", no_argument
, 0, 'R' },
474 static void help_setup_guest(void) {
476 " disorder setup-guest [OPTIONS]\n"
478 " --help, -h Display usage message\n"
479 " --online-registration Enable online registration (default)\n"
480 " --no-online-registration Disable online registration\n");
485 static void cf_setup_guest(char **argv
) {
486 int n
, online_registration
= 1;
488 while((n
= getopt_long(argvlen(argv
) + 1, argv
- 1,
489 "hrR", setup_guest_options
, 0)) >= 0) {
491 case 'h': help_setup_guest();
492 case 'r': online_registration
= 1; break;
493 case 'R': online_registration
= 0; break;
494 default: disorder_fatal(0, "invalid option");
497 if(online_registration
&& !config
->mail_sender
)
498 disorder_fatal(0, "you MUST set mail_sender if you want online registration");
499 if(disorder_adduser(getclient(), "guest", "",
500 online_registration ?
"read,register" : "read"))
504 struct scheduled_event
{
506 struct kvp
*actiondata
;
510 static int compare_event(const void *av
, const void *bv
) {
511 struct scheduled_event
*a
= (void *)av
, *b
= (void *)bv
;
513 /* Primary sort key is the trigger time */
514 if(a
->when
< b
->when
)
516 else if(a
->when
> b
->when
)
518 /* For events that go off at the same time just sort by ID */
519 return strcmp(a
->id
, b
->id
);
522 static void cf_schedule_list(char attribute((unused
)) **argv
) {
525 struct scheduled_event
*events
;
527 const char *action
, *key
, *value
, *priority
;
530 /* Get all known events */
531 if(disorder_schedule_list(getclient(), &ids
, &nids
))
533 events
= xcalloc(nids
, sizeof *events
);
534 for(n
= 0; n
< nids
; ++n
) {
535 events
[n
].id
= ids
[n
];
536 if(disorder_schedule_get(getclient(), ids
[n
], &events
[n
].actiondata
))
538 events
[n
].when
= atoll(kvp_get(events
[n
].actiondata
, "when"));
540 /* Sort by trigger time */
541 qsort(events
, nids
, sizeof *events
, compare_event
);
543 for(n
= 0; n
< nids
; ++n
) {
544 strftime(tb
, sizeof tb
, "%Y-%m-%d %H:%M:%S %Z", localtime(&events
[n
].when
));
545 action
= kvp_get(events
[n
].actiondata
, "action");
546 priority
= kvp_get(events
[n
].actiondata
, "priority");
547 if(!strcmp(priority
, "junk"))
549 else if(!strcmp(priority
, "normal"))
553 xprintf("%11s %-25s %c %-8s %s",
554 events
[n
].id
, tb
, prichar
, kvp_get(events
[n
].actiondata
, "who"),
556 if(!strcmp(action
, "play"))
558 nullcheck(utf82mb(kvp_get(events
[n
].actiondata
, "track"))));
559 else if(!strcmp(action
, "set-global")) {
560 key
= kvp_get(events
[n
].actiondata
, "key");
561 value
= kvp_get(events
[n
].actiondata
, "value");
564 nullcheck(utf82mb(key
)),
565 nullcheck(utf82mb(value
)));
568 nullcheck(utf82mb(key
)));
574 static void cf_schedule_del(char **argv
) {
575 if(disorder_schedule_del(getclient(), argv
[0]))
579 static void cf_schedule_play(char **argv
) {
580 if(disorder_schedule_add(getclient(),
588 static void cf_schedule_set_global(char **argv
) {
589 if(disorder_schedule_add(getclient(),
598 static void cf_schedule_unset_global(char **argv
) {
599 if(disorder_schedule_add(getclient(),
608 static void cf_adopt(char **argv
) {
609 if(disorder_adopt(getclient(), argv
[0]))
613 static void cf_playlists(char attribute((unused
)) **argv
) {
616 if(disorder_playlists(getclient(), &vec
, 0))
619 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
622 static void cf_playlist_del(char **argv
) {
623 if(disorder_playlist_delete(getclient(), argv
[0]))
627 static void cf_playlist_get(char **argv
) {
630 if(disorder_playlist_get(getclient(), argv
[0], &vec
, 0))
633 xprintf("%s\n", nullcheck(utf82mb(*vec
++)));
636 static void cf_playlist_set(char **argv
) {
643 // Read track list from file
644 if(!(input
= fopen(argv
[1], "r")))
645 disorder_fatal(errno
, "opening %s", argv
[1]);
648 // Read track list from standard input
653 while(!inputline(tag
, input
, &l
, '\n')) {
659 disorder_fatal(errno
, "reading %s", tag
);
662 if(disorder_playlist_lock(getclient(), argv
[0])
663 || disorder_playlist_set(getclient(), argv
[0], v
->vec
, v
->nvec
)
664 || disorder_playlist_unlock(getclient()))
668 static const struct command
{
672 int (*isarg
)(const char *);
673 const char *argstr
, *desc
;
675 { "adduser", 2, 3, cf_adduser
, isarg_rights
, "USERNAME PASSWORD [RIGHTS]",
676 "Create a new user" },
677 { "adopt", 1, 1, cf_adopt
, 0, "ID",
678 "Adopt a randomly picked track" },
679 { "allfiles", 1, 2, cf_allfiles
, isarg_regexp
, "DIR [~REGEXP]",
680 "List all files and directories in DIR" },
681 { "authorize", 1, 2, cf_authorize
, isarg_rights
, "USERNAME [RIGHTS]",
682 "Authorize user USERNAME to connect" },
683 { "deluser", 1, 1, cf_deluser
, 0, "USERNAME",
684 "Delete user USERNAME" },
685 { "dirs", 1, 2, cf_dirs
, isarg_regexp
, "DIR [~REGEXP]",
686 "List directories in DIR" },
687 { "disable", 0, 0, cf_disable
, 0, "",
689 { "disable-random", 0, 0, cf_random_disable
, 0, "",
690 "Disable random play" },
691 { "edituser", 3, 3, cf_edituser
, 0, "USERNAME PROPERTY VALUE",
692 "Set a property of user USERNAME" },
693 { "enable", 0, 0, cf_enable
, 0, "",
695 { "enable-random", 0, 0, cf_random_enable
, 0, "",
696 "Enable random play" },
697 { "files", 1, 2, cf_files
, isarg_regexp
, "DIR [~REGEXP]",
698 "List files in DIR" },
699 { "get", 2, 2, cf_get
, 0, "TRACK NAME",
700 "Get a preference value" },
701 { "get-global", 1, 1, cf_get_global
, 0, "NAME",
702 "Get a global preference value" },
703 { "get-volume", 0, 0, cf_get_volume
, 0, "",
704 "Get the current volume" },
705 { "length", 1, 1, cf_length
, 0, "TRACK",
706 "Get the length of TRACK in seconds" },
707 { "log", 0, 0, cf_log
, 0, "",
708 "Copy event log to stdout" },
709 { "move", 2, 2, cf_move
, 0, "TRACK DELTA",
710 "Move a track in the queue" },
711 { "new", 0, 1, cf_new
, isarg_integer
, "[MAX]",
712 "Get the most recently added MAX tracks" },
713 { "part", 3, 3, cf_part
, 0, "TRACK CONTEXT PART",
714 "Find a track name part" },
715 { "pause", 0, 0, cf_pause
, 0, "",
716 "Pause the currently playing track" },
717 { "play", 1, INT_MAX
, cf_play
, isarg_filename
, "TRACKS...",
718 "Add TRACKS to the end of the queue" },
719 { "playing", 0, 0, cf_playing
, 0, "",
720 "Report the playing track" },
721 { "playlist-del", 1, 1, cf_playlist_del
, 0, "PLAYLIST",
722 "Delete a playlist" },
723 { "playlist-get", 1, 1, cf_playlist_get
, 0, "PLAYLIST",
724 "Get the contents of a playlist" },
725 { "playlist-set", 1, 2, cf_playlist_set
, isarg_filename
, "PLAYLIST [PATH]",
726 "Set the contents of a playlist" },
727 { "playlists", 0, 0, cf_playlists
, 0, "",
729 { "prefs", 1, 1, cf_prefs
, 0, "TRACK",
730 "Display all the preferences for TRACK" },
731 { "quack", 0, 0, cf_quack
, 0, 0, 0 },
732 { "queue", 0, 0, cf_queue
, 0, "",
733 "Display the current queue" },
734 { "random-disable", 0, 0, cf_random_disable
, 0, "",
735 "Disable random play" },
736 { "random-enable", 0, 0, cf_random_enable
, 0, "",
737 "Enable random play" },
738 { "recent", 0, 0, cf_recent
, 0, "",
739 "Display recently played track" },
740 { "reconfigure", 0, 0, cf_reconfigure
, 0, "",
741 "Reconfigure the daemon" },
742 { "remove", 1, 1, cf_remove
, 0, "TRACK",
743 "Remove a track from the queue" },
744 { "rescan", 0, 0, cf_rescan
, 0, "",
745 "Rescan for new tracks" },
746 { "resolve", 1, 1, cf_resolve
, 0, "TRACK",
747 "Resolve alias for TRACK" },
748 { "resume", 0, 0, cf_resume
, 0, "",
749 "Resume after a pause" },
750 { "rtp-address", 0, 0, cf_rtp_address
, 0, "",
751 "Report server's broadcast address" },
752 { "schedule-del", 1, 1, cf_schedule_del
, 0, "EVENT",
753 "Delete a scheduled event" },
754 { "schedule-list", 0, 0, cf_schedule_list
, 0, "",
755 "List scheduled events" },
756 { "schedule-play", 3, 3, cf_schedule_play
, 0, "WHEN PRI TRACK",
757 "Play TRACK later" },
758 { "schedule-set-global", 4, 4, cf_schedule_set_global
, 0, "WHEN PRI NAME VAL",
759 "Set a global preference later" },
760 { "schedule-unset-global", 3, 3, cf_schedule_unset_global
, 0, "WHEN PRI NAME",
761 "Unset a global preference later" },
762 { "scratch", 0, 0, cf_scratch
, 0, "",
763 "Scratch the currently playing track" },
764 { "scratch-id", 1, 1, cf_scratch
, 0, "ID",
765 "Scratch the currently playing track" },
766 { "search", 1, 1, cf_search
, 0, "WORDS",
767 "Display tracks matching all the words" },
768 { "set", 3, 3, cf_set
, 0, "TRACK NAME VALUE",
769 "Set a preference value" },
770 { "set-global", 2, 2, cf_set_global
, 0, "NAME VALUE",
771 "Set a global preference value" },
772 { "set-volume", 2, 2, cf_set_volume
, 0, "LEFT RIGHT",
774 { "setup-guest", 0, INT_MAX
, cf_setup_guest
, isarg_option
, "[OPTIONS]",
775 "Create the guest login" },
776 { "shutdown", 0, 0, cf_shutdown
, 0, "",
777 "Shut down the daemon" },
778 { "stats", 0, 0, cf_stats
, 0, "",
779 "Display server statistics" },
780 { "tags", 0, 0, cf_tags
, 0, "",
782 { "unset", 2, 2, cf_unset
, 0, "TRACK NAME",
783 "Unset a preference" },
784 { "unset-global", 1, 1, cf_unset_global
, 0, "NAME",
785 "Unset a global preference" },
786 { "userinfo", 2, 2, cf_userinfo
, 0, "USERNAME PROPERTY",
787 "Get a property of a user" },
788 { "users", 0, 0, cf_users
, 0, "",
790 { "version", 0, 0, cf_version
, 0, "",
791 "Display the server version" },
794 static void help_commands(void) {
795 unsigned n
, max
= 0, l
;
797 xprintf("Command summary:\n");
798 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
799 if(!commands
[n
].desc
) continue;
800 l
= strlen(commands
[n
].name
);
801 if(*commands
[n
].argstr
)
802 l
+= strlen(commands
[n
].argstr
) + 1;
806 for(n
= 0; n
< sizeof commands
/ sizeof *commands
; ++n
) {
807 if(!commands
[n
].desc
) continue;
808 l
= strlen(commands
[n
].name
);
809 if(*commands
[n
].argstr
)
810 l
+= strlen(commands
[n
].argstr
) + 1;
811 xprintf(" %s%s%s%*s %s\n", commands
[n
].name
,
812 *commands
[n
].argstr ?
" " : "",
821 static void wait_for_root(void) {
822 const char *password
;
824 while(!trackdb_readable()) {
825 disorder_info("waiting for trackdb...");
828 trackdb_init(TRACKDB_NO_RECOVER
|TRACKDB_NO_UPGRADE
);
830 trackdb_open(TRACKDB_READ_ONLY
);
831 password
= trackdb_get_password("root");
835 disorder_info("waiting for root user to be created...");
838 trackdb_deinit(NULL
);
841 int main(int argc
, char **argv
) {
842 int n
, i
, j
, local
= 0, wfr
= 0;
845 const char *user
= 0, *password
= 0;
848 /* garbage-collect PCRE's memory */
849 pcre_malloc
= xmalloc
;
851 if(!setlocale(LC_CTYPE
, "")) disorder_fatal(errno
, "error calling setlocale");
852 if(!setlocale(LC_TIME
, "")) disorder_fatal(errno
, "error calling setlocale");
853 while((n
= getopt_long(argc
, argv
, "+hVc:dHlNu:p:W", options
, 0)) >= 0) {
856 case 'H': help_commands();
857 case 'V': version("disorder");
858 case 'c': configfile
= optarg
; break;
859 case 'd': debugging
= 1; break;
860 case 'l': local
= 1; break;
861 case 'N': config_per_user
= 0; break;
862 case 'u': user
= optarg
; break;
863 case 'p': password
= optarg
; break;
864 case 'W': wfr
= 1; break;
865 default: disorder_fatal(0, "invalid option");
868 if(config_read(0, NULL
)) disorder_fatal(0, "cannot read configuration");
870 xfree(config
->username
);
871 config
->username
= xstrdup(user
);
872 config
->password
= 0;
875 xfree(config
->password
);
876 config
->password
= xstrdup(password
);
879 config
->connect
.af
= -1;
883 optind
= 1; /* for subsequent getopt calls */
884 /* gcrypt initialization */
885 if(!gcry_check_version(NULL
))
886 disorder_fatal(0, "gcry_check_version failed");
887 gcry_control(GCRYCTL_INIT_SECMEM
, 0);
888 gcry_control (GCRYCTL_INITIALIZATION_FINISHED
, 0);
889 /* accumulate command args */
891 if((i
= TABLE_FIND(commands
, name
, argv
[n
])) < 0)
892 disorder_fatal(0, "unknown command '%s'", argv
[n
]);
893 if(n
+ commands
[i
].min
>= argc
)
894 disorder_fatal(0, "missing arguments to '%s'", argv
[n
]);
896 /* Include the command name in the args, but at element -1, for
897 * the benefit of subcommand getopt calls */
898 vector_append(&args
, xstrdup(argv
[n
]));
900 for(j
= 0; j
< commands
[i
].min
; ++j
)
901 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
902 for(; j
< commands
[i
].max
904 && commands
[i
].isarg(argv
[n
+ j
]); ++j
)
905 vector_append(&args
, nullcheck(mb2utf8(argv
[n
+ j
])));
906 vector_terminate(&args
);
907 commands
[i
].fn(args
.vec
+ 1);
911 if(client
&& disorder_close(client
)) exit(EXIT_FAILURE
);
912 if(fclose(stdout
) < 0) disorder_fatal(errno
, "error closing stdout");