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