2 * (c) 2009-2020 by Art Cancro and citadel.org
3 * This program is open source. It runs great on the Linux operating system.
4 * It's released under the General Public License (GPL) version 3.
11 * Commands understood by ctdlsh
15 ctdlsh_cmdfunc_t *func;
19 COMMAND commands[] = {
20 {"?", cmd_help, "Display this message"},
21 {"help", cmd_help, "Display this message"},
22 {"date", cmd_datetime, "Print the server's date and time"},
23 {"config", cmd_config, "Configure the Citadel server"},
24 {"export", cmd_export, "Export all Citadel databases"},
25 {"shutdown", cmd_shutdown, "Shut down the Citadel server"},
26 {"time", cmd_datetime, "Print the server's date and time"},
27 {"passwd", cmd_passwd, "Set or change an account password"},
28 {"who", cmd_who, "Display a list of online users"},
29 {"mailq", cmd_mailq, "Show the outbound email queue"},
35 int cmd_help(int sock, char *cmdbuf)
39 for (i = 0; commands[i].func != NULL; ++i) {
40 printf("%10s %s\n", commands[i].name, commands[i].doc);
45 int do_one_command(int server_socket, char *cmd)
49 for (i = 0; commands[i].func != NULL; ++i) {
50 if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
51 ret = (*commands[i].func) (server_socket, cmd);
57 char *command_name_generator(const char *text, int state)
59 static int list_index, len;
67 while (name = commands[list_index++].name) {
68 if (strncmp(name, text, len) == 0) {
77 char **command_name_completion(const char *text, int start, int end)
79 rl_attempted_completion_over = 1;
80 return rl_completion_matches(text, command_name_generator);
84 void do_main_loop(int server_socket)
89 char server_reply[1024];
95 /* Do an INFO command and learn the hostname for the prompt */
96 sock_puts(server_socket, "INFO");
97 sock_getln(server_socket, buf, sizeof buf);
100 while (sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
102 sprintf(prompt, "\n%s> ", buf);
108 /* Here we go ... main command loop */
109 rl_attempted_completion_function = command_name_completion;
110 while ( (cmd = readline(prompt)) , cmd ) {
113 ret = do_one_command(server_socket, cmd);
121 * If you don't know what main() does by now you probably shouldn't be reading this code.
123 int main(int argc, char **argv)
125 int server_socket = 0;
128 char *ctdldir = CTDLDIR;
129 char cmd[1024] = { 0 };
132 for (i = 1; i < argc; ++i) {
133 if (!strcmp(argv[i], "-h")) {
136 if (strlen(cmd) > 0) {
139 strcat(cmd, argv[i]);
143 int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
145 if (is_interactive) {
146 printf("\nCitadel administration shell (c) 2009-2020 by citadel.org\n"
147 "This is open source software made available to you under the terms\n"
148 "of the GNU General Public License v3. All other rights reserved.\n");
149 printf("Connecting to Citadel server in %s...\n", ctdldir);
152 sprintf(buf, "%s/citadel-admin.socket", ctdldir);
153 server_socket = uds_connectsock(buf);
154 if (server_socket < 0) {
158 sock_getln(server_socket, buf, sizeof buf);
160 if (is_interactive) {
161 printf("Connected: %s\n", buf);
162 do_main_loop(server_socket);
164 exitcode = do_one_command(server_socket, cmd);
168 sock_puts(server_socket, "QUIT");
169 sock_getln(server_socket, buf, sizeof buf);
170 if (is_interactive) {
173 close(server_socket);