468c8b261dcd6a158b1ff6bdca542da680b7e73a
[citadel.git] / ctdlsh / main.c
1 /*
2  * (c) 2009-2019 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.
5  */
6
7 #include "ctdlsh.h"
8
9
10 /*
11  * Commands understood by ctdlsh
12  */
13 typedef struct {
14         char *name;
15         ctdlsh_cmdfunc_t *func;
16         char *doc;
17 } COMMAND;
18
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"},
30         {NULL, NULL, NULL}
31 };
32
33
34
35 struct wow {
36         char *cmd[5];                   // increase this if we need to have larger commands
37         char *description;
38 };
39
40 struct wow wows[] = {
41         {{ "help" }                                     , "list available commands"                             },
42         {{ "date" }                                     , "print the server's date and time"                    },
43         {{ "show", "eggs" }                             , "show how many eggs are available for serving"        },
44         {{ "kill", "mark", "zuckerberg" }               , "die motherfucker die"                                },
45         {{ "show", "undead", "zombies", "real" }        , "show how many zombies are actually undead"           },
46         {{ "show", "undead", "zombies", "hollywood" }   , "show how many zombies are hollywood communists"      },
47         {{ NULL }                                       , "NULL"                                                }
48 };
49
50
51 int num_wows = sizeof(wows) / sizeof(struct wow);
52
53
54
55
56
57 int cmd_help(int sock, char *cmdbuf)
58 {
59         int i;
60
61         for (i = 0; wows[i].cmd[0]; ++i) {
62                 printf("%-10s %s\n", wows[i].cmd[0], wows[i].description);
63         }
64 }
65
66
67 /* Auto-completer function */
68 char *command_generator(const char *text, int state)
69 {
70         static int list_index;
71         static int len;
72         char *name;
73
74         if (!state) {
75                 list_index = 0;
76                 len = strlen(text);
77         }
78
79         while (name = commands[list_index].name) {
80                 ++list_index;
81
82                 if (!strncmp(name, text, len)) {
83                         return (strdup(name));
84                 }
85         }
86
87         return (NULL);
88 }
89
90
91 /* Auto-completer function */
92 char **ctdlsh_completion(const char *text, int start, int end)
93 {
94
95
96         printf("\033[7mcompletion: text='%s', start=%d, end=%d\033[0m\n", text, start , end);
97
98
99
100
101
102         char **matches = (char **) NULL;
103
104         rl_completer_word_break_characters = " ";
105         if (start == 0) {
106                 matches = rl_completion_matches(text, command_generator);
107         } else {
108                 rl_bind_key('\t', rl_abort);
109         }
110
111         return (matches);
112 }
113
114
115 int do_one_command(int server_socket, char *cmd)
116 {
117         int i;
118         int ret;
119         for (i = 0; commands[i].func != NULL; ++i) {
120                 if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
121                         ret = (*commands[i].func) (server_socket, cmd);
122                 }
123         }
124         return ret;
125 }
126
127
128 void do_main_loop(int server_socket)
129 {
130         char *cmd = NULL;
131         char prompt[1024];
132         char buf[1024];
133         char server_reply[1024];
134         int i;
135         int ret = (-1);
136
137
138         strcpy(prompt, "> ");
139
140         /* Do an INFO command and learn the hostname for the prompt */
141         sock_puts(server_socket, "INFO");
142         sock_getln(server_socket, buf, sizeof buf);
143         if (buf[0] == '1') {
144                 i = 0;
145                 while (sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
146                         if (i == 1) {
147                                 sprintf(prompt, "\n%s> ", buf);
148                         }
149                         ++i;
150                 }
151         }
152
153         /* Tell libreadline how we will help with auto-completion of commands */
154         rl_attempted_completion_function = ctdlsh_completion;
155
156         /* Here we go ... main command loop */
157         while ( (cmd = readline(prompt)) , cmd ) {
158                 if (*cmd) {
159                         add_history(cmd);
160                         ret = do_one_command(server_socket, cmd);
161                 }
162                 free(cmd);
163         }
164 }
165
166
167 /*
168  * If you don't know what main() does by now you probably shouldn't be reading this code.
169  */
170 int main(int argc, char **argv)
171 {
172         int server_socket = 0;
173         char buf[1024];
174         int i;
175         char *ctdldir = CTDLDIR;
176         char cmd[1024] = { 0 };
177         int exitcode = 0;
178
179         for (i = 1; i < argc; ++i) {
180                 if (!strcmp(argv[i], "-h")) {
181                         ctdldir = argv[++i];
182                 } else {
183                         if (strlen(cmd) > 0) {
184                                 strcat(cmd, " ");
185                         }
186                         strcat(cmd, argv[i]);
187                 }
188         }
189
190         int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
191
192         if (is_interactive) {
193                 printf("\nCitadel administration shell (c) 2009-2019 by citadel.org\n"
194                        "This is open source software made available to you under the terms\n"
195                        "of the GNU General Public License v3.  All other rights reserved.\n");
196                 printf("Connecting to Citadel server in %s...\n", ctdldir);
197         }
198
199         sprintf(buf, "%s/citadel-admin.socket", ctdldir);
200         server_socket = uds_connectsock(buf);
201         if (server_socket < 0) {
202                 exit(1);
203         }
204
205         sock_getln(server_socket, buf, sizeof buf);
206         if (buf[0] == '2') {
207                 if (is_interactive) {
208                         printf("Connected: %s\n", buf);
209                         do_main_loop(server_socket);
210                 } else {
211                         exitcode = do_one_command(server_socket, cmd);
212                 }
213         }
214
215         sock_puts(server_socket, "QUIT");
216         sock_getln(server_socket, buf, sizeof buf);
217         if (is_interactive) {
218                 printf("%s\n", buf);
219         }
220         close(server_socket);
221         exit(exitcode);
222 }