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),
101 strcmp(buf, "000")) {
103 sprintf(prompt, "\n%s> ", buf);
109 /* Here we go ... main command loop */
110 rl_attempted_completion_function = command_name_completion;
111 while ((cmd = readline(prompt)), cmd) {
114 ret = do_one_command(server_socket, cmd);
122 * If you don't know what main() does by now you probably shouldn't be reading this code.
124 int main(int argc, char **argv)
126 int server_socket = 0;
129 char *ctdldir = CTDLDIR;
130 char cmd[1024] = { 0 };
133 for (i = 1; i < argc; ++i) {
134 if (!strcmp(argv[i], "-h")) {
137 if (strlen(cmd) > 0) {
140 strcat(cmd, argv[i]);
144 int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
146 if (is_interactive) {
148 ("\nCitadel administration shell (c) 2009-2020 by citadel.org\n"
149 "This is open source software made available to you under the terms\n"
150 "of the GNU General Public License v3. All other rights reserved.\n");
151 printf("Connecting to Citadel server in %s...\n", ctdldir);
154 sprintf(buf, "%s/citadel-admin.socket", ctdldir);
155 server_socket = uds_connectsock(buf);
156 if (server_socket < 0) {
160 sock_getln(server_socket, buf, sizeof buf);
162 if (is_interactive) {
163 printf("Connected: %s\n", buf);
164 do_main_loop(server_socket);
166 exitcode = do_one_command(server_socket, cmd);
170 sock_puts(server_socket, "QUIT");
171 sock_getln(server_socket, buf, sizeof buf);
172 if (is_interactive) {
175 close(server_socket);