2 * (c) 2009-2017 by Art Cancro and citadel.org
3 * This program is released under the terms of the GNU General Public License v3.
9 int cmd_quit(int sock, char *cmdbuf)
16 * Commands understood by ctdlsh
20 ctdlsh_cmdfunc_t *func;
24 COMMAND commands[] = {
25 {"?", cmd_help, "Display this message"},
26 {"help", cmd_help, "Display this message"},
27 {"date", cmd_datetime, "Print the server's date and time"},
28 {"config", cmd_config, "Configure the Citadel server"},
29 {"export", cmd_export, "Export all Citadel databases"},
30 {"shutdown", cmd_shutdown, "Shut down the Citadel server"},
31 {"time", cmd_datetime, "Print the server's date and time"},
32 {"passwd", cmd_passwd, "Set or change an account password"},
33 {"who", cmd_who, "Display a list of online users"},
34 {"exit", cmd_quit, "Quit using ctdlsh"},
35 {"quit", cmd_quit, "Quit using ctdlsh"},
36 {"mailq", cmd_mailq, "Show the outbound email queue"},
43 char *cmd[5]; // increase this if we need to have larger commands
48 {{ "show", "eggs" } , "show how many eggs are available for serving" },
49 {{ "kill", "mark", "zuckerberg" } , "die motherfucker die" },
50 {{ "show", "undead", "zombies", "real" } , "show how many zombies are actually undead" },
51 {{ "show", "undead", "zombies", "hollywood" } , "show how many zombies are hollywood communists" }
61 int cmd_help(int sock, char *cmdbuf)
65 for (i = 0; commands[i].func != NULL; ++i) {
66 printf("%-10s %s\n", commands[i].name, commands[i].doc);
71 /* Auto-completer function */
72 char *command_generator(const char *text, int state)
74 static int list_index;
83 while (name = commands[list_index].name) {
86 if (!strncmp(name, text, len)) {
87 return (strdup(name));
95 /* Auto-completer function */
96 char **ctdlsh_completion(const char *text, int start, int end)
98 char **matches = (char **) NULL;
100 rl_completer_word_break_characters = " ";
102 matches = rl_completion_matches(text, command_generator);
104 rl_bind_key('\t', rl_abort);
111 int do_one_command(int server_socket, char *cmd)
115 for (i = 0; commands[i].func != NULL; ++i) {
116 if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
117 ret = (*commands[i].func) (server_socket, cmd);
124 void do_main_loop(int server_socket)
129 char server_reply[1024];
134 strcpy(prompt, "> ");
136 /* Do an INFO command and learn the hostname for the prompt */
137 sock_puts(server_socket, "INFO");
138 sock_getln(server_socket, buf, sizeof buf);
141 while (sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
143 sprintf(prompt, "\n%s> ", buf);
149 /* Tell libreadline how we will help with auto-completion of commands */
150 rl_attempted_completion_function = ctdlsh_completion;
152 /* Here we go ... main command loop */
153 while ((ret != cmdret_exit) && (cmd = readline(prompt))) {
155 if ((cmd) && (*cmd)) {
157 ret = do_one_command(server_socket, cmd);
159 //for (i=0; commands[i].func != NULL; ++i) {
160 //if (!strncasecmp(cmd, commands[i].name, strlen(commands[i].name))) {
161 //ret = (*commands[i].func) (server_socket, cmd);
173 * If you don't know what main() does by now you probably shouldn't be reading this code.
175 int main(int argc, char **argv)
177 int server_socket = 0;
180 char *ctdldir = CTDLDIR;
181 char cmd[1024] = { 0 };
185 int num_wows = sizeof(wows) / sizeof(struct wow);
187 for (i=0; i<num_wows; ++i) {
188 printf("%s\n", wows[i].description);
189 for (j=0; j<5; ++j) {
190 printf("%d '%s'\n", j, wows[i].cmd[j]);
203 for (i = 1; i < argc; ++i) {
204 if (!strcmp(argv[i], "-h")) {
207 if (strlen(cmd) > 0) {
210 strcat(cmd, argv[i]);
214 int is_interactive = ((strlen(cmd) == 0) ? 1 : 0);
216 if (is_interactive) {
217 printf("\nCitadel administration shell (c) 2009-2017 by citadel.org\n"
218 "This is open source software made available to you under the terms\n"
219 "of the GNU General Public License v3. All other rights reserved.\n");
220 printf("Trying %s...\n", ctdldir);
223 sprintf(buf, "%s/citadel-admin.socket", ctdldir);
224 server_socket = uds_connectsock(buf);
225 if (server_socket < 0) {
229 sock_getln(server_socket, buf, sizeof buf);
231 if (is_interactive) {
232 printf("Connected: %s\n", buf);
233 do_main_loop(server_socket);
235 exitcode = do_one_command(server_socket, cmd);
239 sock_puts(server_socket, "QUIT");
240 sock_getln(server_socket, buf, sizeof buf);
241 if (is_interactive) {
244 close(server_socket);