Cleanup and added funny joke haha
[citadel.git] / ctdlsh / src / main.c
1 /*
2  * (c) 2009-2012 by Art Cancro and citadel.org
3  * This program is released under the terms of the GNU General Public License v3.
4  */
5
6 #include "ctdlsh.h"
7
8
9 int cmd_quit(int sock, char *cmdbuf) {
10         return(cmdret_exit);
11 }
12
13
14 /*
15  * Commands understood by ctdlsh
16  */
17 typedef struct {
18         char *name;
19         ctdlsh_cmdfunc_t *func;
20         char *doc;
21 } COMMAND;
22
23 COMMAND commands[] = {
24         {       "?",            cmd_help,       "Display this message"                  },
25         {       "help",         cmd_help,       "Display this message"                  },
26         {       "quit",         cmd_quit,       "Quit using ctdlsh"                     },
27         {       "exit",         cmd_quit,       "Quit using ctdlsh"                     },
28         {       "date",         cmd_datetime,   "Print the server's date and time"      },
29         {       "time",         cmd_datetime,   "Print the server's date and time"      },
30         {       "passwd",       cmd_passwd,     "Set or change an account password"     },
31         {       "shutdown",     cmd_shutdown,   "Shut down the Citadel server"          },
32         {       NULL,           NULL,           NULL                                    }
33 };
34
35
36 int cmd_help(int sock, char *cmdbuf) {
37         int i;
38
39         for (i=0; commands[i].func != NULL; ++i) {
40                 printf("%-10s %s\n", commands[i].name, commands[i].doc);
41         }
42 }
43
44
45 /* Auto-completer function */
46 char *command_generator(const char *text, int state) {
47         static int list_index;
48         static int len;
49         char *name;
50
51         if (!state) {
52                 list_index = 0;
53                 len = strlen(text);
54         }
55
56         while (name = commands[list_index].name) {
57                 ++list_index;
58
59                 if (!strncmp(name, text, len)) {
60                         return(strdup(name));
61                 }
62         }
63
64         return(NULL);
65 }
66
67
68 /* Auto-completer function */
69 char **ctdlsh_completion(const char *text, int start, int end) {
70         char **matches = (char **) NULL;
71
72         if (start == 0) {
73                 matches = rl_completion_matches(text, command_generator);
74         }
75         else {
76                 rl_bind_key('\t', rl_abort);
77         }
78
79         return (matches);
80 }
81
82
83 void do_main_loop(int server_socket) {
84         char *cmd = NULL;
85         char prompt[1024];
86         char buf[1024];
87         char server_reply[1024];
88         int i;
89         int ret = (-1);
90
91         strcpy(prompt, "> ");
92
93         /* Do an INFO command and learn the hostname for the prompt */
94         sock_puts(server_socket, "INFO");
95         sock_getln(server_socket, buf, sizeof buf);
96         if (buf[0] == '1') {
97                 i = 0;
98                 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
99                         if (i == 1) {
100                                 sprintf(prompt, "\n%s> ", buf);
101                         }
102                         ++i;
103                 }
104         }
105
106         /* Tell libreadline how we will help with auto-completion of commands */
107         rl_attempted_completion_function = ctdlsh_completion;
108
109         /* Here we go ... main command loop */
110         while ((ret != cmdret_exit) && (cmd = readline(prompt))) {
111
112                 if ((cmd) && (*cmd)) {
113                         add_history(cmd);
114
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);
118                                 }
119                         }
120
121                 }
122
123                 free(cmd);
124         }
125 }
126
127
128 /*
129  * If you don't know what main() does by now you probably shouldn't be reading this code.
130  */
131 int main(int argc, char **argv)
132 {
133         int server_socket = 0;
134         char buf[1024];
135         int c;
136         char *ctdldir = CTDLDIR;
137
138         printf("\nCitadel administration shell v" PACKAGE_VERSION " (c) 2009-2012 by citadel.org\n"
139                 "This is open source software made available to you under the terms\n"
140                 "of the GNU General Public License v3.  All other rights reserved.\n"
141         );
142
143         opterr = 0;
144         while ((c = getopt (argc, argv, "h:")) != -1) {
145                 switch(c) {
146                 case 'h':
147                         ctdldir = optarg;
148                         break;
149                 case '?':
150                         if (optopt == 'h') {
151                                 fprintf(stderr, "Option -%c requires an argument\n", optopt);
152                         }
153                         else {
154                                 fprintf(stderr, "Unknown option '-%c'\n", optopt);
155                                 fprintf(stderr, "usage: %s [-h citadel_dir]\n", argv[0]);
156                         }
157                         exit(1);
158                 }
159         }
160
161         printf("Trying %s...\n", ctdldir);
162         sprintf(buf, "%s/citadel-admin.socket", ctdldir);
163         server_socket = uds_connectsock(buf);
164         if (server_socket < 0) {
165                 exit(1);
166         }
167
168         sock_getln(server_socket, buf, sizeof buf);
169         if (buf[0] == '2') {
170                 do_main_loop(server_socket);
171         }
172
173         sock_puts(server_socket, "QUIT");
174         sock_getln(server_socket, buf, sizeof buf);
175         printf("%s\n", buf);
176         close(server_socket);
177         exit(0);
178 }