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)
94 char **matches = (char **) NULL;
96 rl_completer_word_break_characters = " ";
98 matches = rl_completion_matches(text, command_generator);
100 rl_bind_key('\t', rl_abort);
107 int do_one_command(int server_socket, char *cmd)
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);
120 void do_main_loop(int server_socket)
125 char server_reply[1024];
130 strcpy(prompt, "> ");
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);
137 while (sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
139 sprintf(prompt, "\n%s> ", buf);
145 /* Tell libreadline how we will help with auto-completion of commands */
146 rl_attempted_completion_function = ctdlsh_completion;
148 /* Here we go ... main command loop */
149 while ( (cmd = readline(prompt)) , ((cmd) && (*cmd)) ) {
151 ret = do_one_command(server_socket, cmd);
158 * If you don't know what main() does by now you probably shouldn't be reading this code.
160 int main(int argc, char **argv)
162 int server_socket = 0;
165 char *ctdldir = CTDLDIR;
166 char cmd[1024] = { 0 };
169 for (i = 1; i < argc; ++i) {
170 if (!strcmp(argv[i], "-h")) {
173 if (strlen(cmd) > 0) {
176 strcat(cmd, argv[i]);
180 int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
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);
189 sprintf(buf, "%s/citadel-admin.socket", ctdldir);
190 server_socket = uds_connectsock(buf);
191 if (server_socket < 0) {
195 sock_getln(server_socket, buf, sizeof buf);
197 if (is_interactive) {
198 printf("Connected: %s\n", buf);
199 do_main_loop(server_socket);
201 exitcode = do_one_command(server_socket, cmd);
205 sock_puts(server_socket, "QUIT");
206 sock_getln(server_socket, buf, sizeof buf);
207 if (is_interactive) {
210 close(server_socket);