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.
11 * Commands understood by ctdlsh
15 ctdlsh_cmdfunc_t *func;
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"},
36 char *cmd[5]; // increase this if we need to have larger commands
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" },
51 int num_wows = sizeof(wows) / sizeof(struct wow);
57 int cmd_help(int sock, char *cmdbuf)
61 for (i = 0; wows[i].cmd[0]; ++i) {
62 printf("%-10s %s\n", wows[i].cmd[0], wows[i].description);
67 /* Auto-completer function */
68 char *command_generator(const char *text, int state)
70 static int list_index;
79 while (name = commands[list_index].name) {
82 if (!strncmp(name, text, len)) {
83 return (strdup(name));
91 /* Auto-completer function */
92 char **ctdlsh_completion(const char *text, int start, int end)
96 printf("\033[7mcompletion: text='%s', start=%d, end=%d\033[0m\n", text, start , end);
102 char **matches = (char **) NULL;
104 rl_completer_word_break_characters = " ";
106 matches = rl_completion_matches(text, command_generator);
108 rl_bind_key('\t', rl_abort);
115 int do_one_command(int server_socket, char *cmd)
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);
128 void do_main_loop(int server_socket)
133 char server_reply[1024];
138 strcpy(prompt, "> ");
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);
145 while (sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
147 sprintf(prompt, "\n%s> ", buf);
153 /* Tell libreadline how we will help with auto-completion of commands */
154 rl_attempted_completion_function = ctdlsh_completion;
156 /* Here we go ... main command loop */
157 while ( (cmd = readline(prompt)) , cmd ) {
160 ret = do_one_command(server_socket, cmd);
168 * If you don't know what main() does by now you probably shouldn't be reading this code.
170 int main(int argc, char **argv)
172 int server_socket = 0;
175 char *ctdldir = CTDLDIR;
176 char cmd[1024] = { 0 };
179 for (i = 1; i < argc; ++i) {
180 if (!strcmp(argv[i], "-h")) {
183 if (strlen(cmd) > 0) {
186 strcat(cmd, argv[i]);
190 int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
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);
199 sprintf(buf, "%s/citadel-admin.socket", ctdldir);
200 server_socket = uds_connectsock(buf);
201 if (server_socket < 0) {
205 sock_getln(server_socket, buf, sizeof buf);
207 if (is_interactive) {
208 printf("Connected: %s\n", buf);
209 do_main_loop(server_socket);
211 exitcode = do_one_command(server_socket, cmd);
215 sock_puts(server_socket, "QUIT");
216 sock_getln(server_socket, buf, sizeof buf);
217 if (is_interactive) {
220 close(server_socket);