do nothing with blank lines, exit on EOF
[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         char **matches = (char **) NULL;
95
96         rl_completer_word_break_characters = " ";
97         if (start == 0) {
98                 matches = rl_completion_matches(text, command_generator);
99         } else {
100                 rl_bind_key('\t', rl_abort);
101         }
102
103         return (matches);
104 }
105
106
107 int do_one_command(int server_socket, char *cmd)
108 {
109         int i;
110         int ret;
111         for (i = 0; commands[i].func != NULL; ++i) {
112                 if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
113                         ret = (*commands[i].func) (server_socket, cmd);
114                 }
115         }
116         return ret;
117 }
118
119
120 void do_main_loop(int server_socket)
121 {
122         char *cmd = NULL;
123         char prompt[1024];
124         char buf[1024];
125         char server_reply[1024];
126         int i;
127         int ret = (-1);
128
129
130         strcpy(prompt, "> ");
131
132         /* Do an INFO command and learn the hostname for the prompt */
133         sock_puts(server_socket, "INFO");
134         sock_getln(server_socket, buf, sizeof buf);
135         if (buf[0] == '1') {
136                 i = 0;
137                 while (sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
138                         if (i == 1) {
139                                 sprintf(prompt, "\n%s> ", buf);
140                         }
141                         ++i;
142                 }
143         }
144
145         /* Tell libreadline how we will help with auto-completion of commands */
146         rl_attempted_completion_function = ctdlsh_completion;
147
148         /* Here we go ... main command loop */
149         while ( (cmd = readline(prompt)) , cmd ) {
150                 if (*cmd) {
151                         add_history(cmd);
152                         ret = do_one_command(server_socket, cmd);
153                 }
154                 free(cmd);
155         }
156 }
157
158
159 /*
160  * If you don't know what main() does by now you probably shouldn't be reading this code.
161  */
162 int main(int argc, char **argv)
163 {
164         int server_socket = 0;
165         char buf[1024];
166         int i;
167         char *ctdldir = CTDLDIR;
168         char cmd[1024] = { 0 };
169         int exitcode = 0;
170
171         for (i = 1; i < argc; ++i) {
172                 if (!strcmp(argv[i], "-h")) {
173                         ctdldir = argv[++i];
174                 } else {
175                         if (strlen(cmd) > 0) {
176                                 strcat(cmd, " ");
177                         }
178                         strcat(cmd, argv[i]);
179                 }
180         }
181
182         int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
183
184         if (is_interactive) {
185                 printf("\nCitadel administration shell (c) 2009-2019 by citadel.org\n"
186                        "This is open source software made available to you under the terms\n"
187                        "of the GNU General Public License v3.  All other rights reserved.\n");
188                 printf("Connecting to Citadel server in %s...\n", ctdldir);
189         }
190
191         sprintf(buf, "%s/citadel-admin.socket", ctdldir);
192         server_socket = uds_connectsock(buf);
193         if (server_socket < 0) {
194                 exit(1);
195         }
196
197         sock_getln(server_socket, buf, sizeof buf);
198         if (buf[0] == '2') {
199                 if (is_interactive) {
200                         printf("Connected: %s\n", buf);
201                         do_main_loop(server_socket);
202                 } else {
203                         exitcode = do_one_command(server_socket, cmd);
204                 }
205         }
206
207         sock_puts(server_socket, "QUIT");
208         sock_getln(server_socket, buf, sizeof buf);
209         if (is_interactive) {
210                 printf("%s\n", buf);
211         }
212         close(server_socket);
213         exit(exitcode);
214 }