00ca50895a0ce2976b545337b4aed6def1279e3d
[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) && (*cmd)) ) {
150                 add_history(cmd);
151                 ret = do_one_command(server_socket, cmd);
152                 free(cmd);
153         }
154 }
155
156
157 /*
158  * If you don't know what main() does by now you probably shouldn't be reading this code.
159  */
160 int main(int argc, char **argv)
161 {
162         int server_socket = 0;
163         char buf[1024];
164         int i;
165         char *ctdldir = CTDLDIR;
166         char cmd[1024] = { 0 };
167         int exitcode = 0;
168
169         for (i = 1; i < argc; ++i) {
170                 if (!strcmp(argv[i], "-h")) {
171                         ctdldir = argv[++i];
172                 } else {
173                         if (strlen(cmd) > 0) {
174                                 strcat(cmd, " ");
175                         }
176                         strcat(cmd, argv[i]);
177                 }
178         }
179
180         int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
181
182         if (is_interactive) {
183                 printf("\nCitadel administration shell (c) 2009-2019 by citadel.org\n"
184                        "This is open source software made available to you under the terms\n"
185                        "of the GNU General Public License v3.  All other rights reserved.\n");
186                 printf("Connecting to Citadel server in %s...\n", ctdldir);
187         }
188
189         sprintf(buf, "%s/citadel-admin.socket", ctdldir);
190         server_socket = uds_connectsock(buf);
191         if (server_socket < 0) {
192                 exit(1);
193         }
194
195         sock_getln(server_socket, buf, sizeof buf);
196         if (buf[0] == '2') {
197                 if (is_interactive) {
198                         printf("Connected: %s\n", buf);
199                         do_main_loop(server_socket);
200                 } else {
201                         exitcode = do_one_command(server_socket, cmd);
202                 }
203         }
204
205         sock_puts(server_socket, "QUIT");
206         sock_getln(server_socket, buf, sizeof buf);
207         if (is_interactive) {
208                 printf("%s\n", buf);
209         }
210         close(server_socket);
211         exit(exitcode);
212 }