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) {
15 * Commands understood by ctdlsh
19 ctdlsh_cmdfunc_t *func;
23 COMMAND commands[] = {
24 { "?", cmd_help, "Display this message" },
25 { "help", cmd_help, "Display this message" },
26 { "date", cmd_datetime, "Print the server's date and time" },
27 { "config", cmd_config, "Configure the Citadel server" },
28 { "export", cmd_export, "Export all Citadel databases" },
29 { "shutdown", cmd_shutdown, "Shut down the Citadel server" },
30 { "time", cmd_datetime, "Print the server's date and time" },
31 { "passwd", cmd_passwd, "Set or change an account password" },
32 { "who", cmd_who, "Display a list of online users" },
33 { "exit", cmd_quit, "Quit using ctdlsh" },
34 { "quit", cmd_quit, "Quit using ctdlsh" },
35 { "mailq", cmd_mailq, "Show the outbound email queue" },
40 int cmd_help(int sock, char *cmdbuf) {
43 for (i=0; commands[i].func != NULL; ++i) {
44 printf("%-10s %s\n", commands[i].name, commands[i].doc);
49 /* Auto-completer function */
50 char *command_generator(const char *text, int state) {
51 static int list_index;
60 while (name = commands[list_index].name) {
63 if (!strncmp(name, text, len)) {
72 /* Auto-completer function */
73 char **ctdlsh_completion(const char *text, int start, int end) {
74 char **matches = (char **) NULL;
76 rl_completer_word_break_characters = " ";
78 matches = rl_completion_matches(text, command_generator);
81 rl_bind_key('\t', rl_abort);
88 int do_one_command(int server_socket, char *cmd) {
91 for (i=0; commands[i].func != NULL; ++i) {
92 if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
93 ret = (*commands[i].func) (server_socket, cmd);
100 void do_main_loop(int server_socket) {
104 char server_reply[1024];
108 strcpy(prompt, "> ");
110 /* Do an INFO command and learn the hostname for the prompt */
111 sock_puts(server_socket, "INFO");
112 sock_getln(server_socket, buf, sizeof buf);
115 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
117 sprintf(prompt, "\n%s> ", buf);
123 /* Tell libreadline how we will help with auto-completion of commands */
124 rl_attempted_completion_function = ctdlsh_completion;
126 /* Here we go ... main command loop */
127 while ((ret != cmdret_exit) && (cmd = readline(prompt))) {
129 if ((cmd) && (*cmd)) {
131 ret = do_one_command(server_socket, cmd);
133 //for (i=0; commands[i].func != NULL; ++i) {
134 //if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
135 //ret = (*commands[i].func) (server_socket, cmd);
147 * If you don't know what main() does by now you probably shouldn't be reading this code.
149 int main(int argc, char **argv)
151 int server_socket = 0;
154 char *ctdldir = CTDLDIR;
155 char cmd[1024] = { 0 } ;
158 for (i=1; i<argc; ++i) {
159 if (!strcmp(argv[i], "-h")) {
163 if (strlen(cmd) > 0) {
166 strcat(cmd, argv[i]);
170 int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
172 if (is_interactive) {
173 printf("\nCitadel administration shell (c) 2009-2017 by citadel.org\n"
174 "This is open source software made available to you under the terms\n"
175 "of the GNU General Public License v3. All other rights reserved.\n"
177 printf("Trying %s...\n", ctdldir);
180 sprintf(buf, "%s/citadel-admin.socket", ctdldir);
181 server_socket = uds_connectsock(buf);
182 if (server_socket < 0) {
186 sock_getln(server_socket, buf, sizeof buf);
188 if (is_interactive) {
189 printf("Connected: %s\n", buf);
190 do_main_loop(server_socket);
193 exitcode = do_one_command(server_socket, cmd);
197 sock_puts(server_socket, "QUIT");
198 sock_getln(server_socket, buf, sizeof buf);
199 if (is_interactive) {
202 close(server_socket);