2 * (c) 2009-2017 by Art Cancro and citadel.org
3 * This program is released under the terms of the GNU General Public License v3.
9 int cmd_quit(int sock, char *cmdbuf)
16 * Commands understood by ctdlsh
20 ctdlsh_cmdfunc_t *func;
24 COMMAND commands[] = {
25 {"?", cmd_help, "Display this message"},
26 {"help", cmd_help, "Display this message"},
27 {"date", cmd_datetime, "Print the server's date and time"},
28 {"config", cmd_config, "Configure the Citadel server"},
29 {"export", cmd_export, "Export all Citadel databases"},
30 {"shutdown", cmd_shutdown, "Shut down the Citadel server"},
31 {"time", cmd_datetime, "Print the server's date and time"},
32 {"passwd", cmd_passwd, "Set or change an account password"},
33 {"who", cmd_who, "Display a list of online users"},
34 {"exit", cmd_quit, "Quit using ctdlsh"},
35 {"quit", cmd_quit, "Quit using ctdlsh"},
36 {"mailq", cmd_mailq, "Show the outbound email queue"},
41 int cmd_help(int sock, char *cmdbuf)
45 for (i = 0; commands[i].func != NULL; ++i) {
46 printf("%-10s %s\n", commands[i].name, commands[i].doc);
51 /* Auto-completer function */
52 char *command_generator(const char *text, int state)
54 static int list_index;
63 while (name = commands[list_index].name) {
66 if (!strncmp(name, text, len)) {
67 return (strdup(name));
75 /* Auto-completer function */
76 char **ctdlsh_completion(const char *text, int start, int end)
78 char **matches = (char **) NULL;
80 rl_completer_word_break_characters = " ";
82 matches = rl_completion_matches(text, command_generator);
84 rl_bind_key('\t', rl_abort);
91 int do_one_command(int server_socket, char *cmd)
95 for (i = 0; commands[i].func != NULL; ++i) {
96 if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
97 ret = (*commands[i].func) (server_socket, cmd);
104 void do_main_loop(int server_socket)
109 char server_reply[1024];
113 strcpy(prompt, "> ");
115 /* Do an INFO command and learn the hostname for the prompt */
116 sock_puts(server_socket, "INFO");
117 sock_getln(server_socket, buf, sizeof buf);
120 while (sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
122 sprintf(prompt, "\n%s> ", buf);
128 /* Tell libreadline how we will help with auto-completion of commands */
129 rl_attempted_completion_function = ctdlsh_completion;
131 /* Here we go ... main command loop */
132 while ((ret != cmdret_exit) && (cmd = readline(prompt))) {
134 if ((cmd) && (*cmd)) {
136 ret = do_one_command(server_socket, cmd);
138 //for (i=0; commands[i].func != NULL; ++i) {
139 //if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
140 //ret = (*commands[i].func) (server_socket, cmd);
152 * If you don't know what main() does by now you probably shouldn't be reading this code.
154 int main(int argc, char **argv)
156 int server_socket = 0;
159 char *ctdldir = CTDLDIR;
160 char cmd[1024] = { 0 };
163 for (i = 1; i < argc; ++i) {
164 if (!strcmp(argv[i], "-h")) {
167 if (strlen(cmd) > 0) {
170 strcat(cmd, argv[i]);
174 int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
176 if (is_interactive) {
177 printf("\nCitadel administration shell (c) 2009-2017 by citadel.org\n"
178 "This is open source software made available to you under the terms\n"
179 "of the GNU General Public License v3. All other rights reserved.\n");
180 printf("Trying %s...\n", ctdldir);
183 sprintf(buf, "%s/citadel-admin.socket", ctdldir);
184 server_socket = uds_connectsock(buf);
185 if (server_socket < 0) {
189 sock_getln(server_socket, buf, sizeof buf);
191 if (is_interactive) {
192 printf("Connected: %s\n", buf);
193 do_main_loop(server_socket);
195 exitcode = do_one_command(server_socket, cmd);
199 sock_puts(server_socket, "QUIT");
200 sock_getln(server_socket, buf, sizeof buf);
201 if (is_interactive) {
204 close(server_socket);