b159c9c12185ad9d74e02f0db0b40fde7dfec67c
[citadel.git] / ctdlsh / main.c
1 /*
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.
4  */
5
6 #include "ctdlsh.h"
7
8
9 int cmd_quit(int sock, char *cmdbuf) {
10         return(cmdret_exit);
11 }
12
13
14 /*
15  * Commands understood by ctdlsh
16  */
17 typedef struct {
18         char *name;
19         ctdlsh_cmdfunc_t *func;
20         char *doc;
21 } COMMAND;
22
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"         },
36         {       NULL,           NULL,           NULL                                    }
37 };
38
39
40 int cmd_help(int sock, char *cmdbuf) {
41         int i;
42
43         for (i=0; commands[i].func != NULL; ++i) {
44                 printf("%-10s %s\n", commands[i].name, commands[i].doc);
45         }
46 }
47
48
49 /* Auto-completer function */
50 char *command_generator(const char *text, int state) {
51         static int list_index;
52         static int len;
53         char *name;
54
55         if (!state) {
56                 list_index = 0;
57                 len = strlen(text);
58         }
59
60         while (name = commands[list_index].name) {
61                 ++list_index;
62
63                 if (!strncmp(name, text, len)) {
64                         return(strdup(name));
65                 }
66         }
67
68         return(NULL);
69 }
70
71
72 /* Auto-completer function */
73 char **ctdlsh_completion(const char *text, int start, int end) {
74         char **matches = (char **) NULL;
75
76         rl_completer_word_break_characters = " ";
77         if (start == 0) {
78                 matches = rl_completion_matches(text, command_generator);
79         }
80         else {
81                 rl_bind_key('\t', rl_abort);
82         }
83
84         return (matches);
85 }
86
87
88 int do_one_command(int server_socket, char *cmd) {
89         int i;
90         int ret;
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);
94                 }
95         }
96         return ret;
97 }
98
99
100 void do_main_loop(int server_socket) {
101         char *cmd = NULL;
102         char prompt[1024];
103         char buf[1024];
104         char server_reply[1024];
105         int i;
106         int ret = (-1);
107
108         strcpy(prompt, "> ");
109
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);
113         if (buf[0] == '1') {
114                 i = 0;
115                 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
116                         if (i == 1) {
117                                 sprintf(prompt, "\n%s> ", buf);
118                         }
119                         ++i;
120                 }
121         }
122
123         /* Tell libreadline how we will help with auto-completion of commands */
124         rl_attempted_completion_function = ctdlsh_completion;
125
126         /* Here we go ... main command loop */
127         while ((ret != cmdret_exit) && (cmd = readline(prompt))) {
128
129                 if ((cmd) && (*cmd)) {
130                         add_history(cmd);
131                         ret = do_one_command(server_socket, cmd);
132
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);
136                                 //}
137                         //}
138
139                 }
140
141                 free(cmd);
142         }
143 }
144
145
146 /*
147  * If you don't know what main() does by now you probably shouldn't be reading this code.
148  */
149 int main(int argc, char **argv)
150 {
151         int server_socket = 0;
152         char buf[1024];
153         int i;
154         char *ctdldir = CTDLDIR;
155         char cmd[1024] = { 0 } ;
156         int exitcode = 0;
157
158         for (i=1; i<argc; ++i) {
159                 if (!strcmp(argv[i], "-h")) {
160                         ctdldir = argv[++i];
161                 }
162                 else {
163                         if (strlen(cmd) > 0) {
164                                 strcat(cmd, " ");
165                         }
166                         strcat(cmd, argv[i]);
167                 }
168         }
169
170         int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
171
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"
176                 );
177                 printf("Trying %s...\n", ctdldir);
178         }
179
180         sprintf(buf, "%s/citadel-admin.socket", ctdldir);
181         server_socket = uds_connectsock(buf);
182         if (server_socket < 0) {
183                 exit(1);
184         }
185
186         sock_getln(server_socket, buf, sizeof buf);
187         if (buf[0] == '2') {
188                 if (is_interactive) {
189                         printf("Connected: %s\n", buf);
190                         do_main_loop(server_socket);
191                 }
192                 else {
193                         exitcode = do_one_command(server_socket, cmd);
194                 }
195         }
196
197         sock_puts(server_socket, "QUIT");
198         sock_getln(server_socket, buf, sizeof buf);
199         if (is_interactive) {
200                 printf("%s\n", buf);
201         }
202         close(server_socket);
203         exit(exitcode);
204 }