* Fleshed out the ctdlsh interface a bit. Right now it's just a simple shell that...
[citadel.git] / ctdlsh / src / main.c
1 /*
2  * (c) 2009 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 <config.h>
7 #include <stdlib.h>
8 #include <unistd.h>
9 #include <stdio.h>
10 #include <fcntl.h>
11 #include <errno.h>
12 #include <readline/readline.h>
13 #include "ctdlsh.h"
14
15 #define CTDLDIR "/root/ctdl/trunk/citadel"
16
17
18 int discover_ipgm_secret(void) {
19         int fd;
20         struct partial_config ccc;
21
22         fd = open(CTDLDIR "/citadel.config", O_RDONLY);
23         if (fd < 0) {
24                 fprintf(stderr, "citadel.config: %s\n", strerror(errno));
25                 return(-1);
26         }
27
28         if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
29                 fprintf(stderr, "citadel.config: %s\n", strerror(errno));
30                 return(-1);
31         }
32         if (close(fd) != 0) {
33                 fprintf(stderr, "citadel.config: %s\n", strerror(errno));
34                 return(-1);
35         }
36         return(ccc.c_ipgm_secret);
37 }
38
39
40 void do_main_loop(int server_socket) {
41         char *cmd = NULL;
42         char prompt[1024];
43         char buf[1024];
44         char server_reply[1024];
45         int i;
46
47         strcpy(prompt, "> ");
48
49         /* Do an INFO command and learn the hostname for the prompt */
50         sock_puts(server_socket, "INFO");
51         sock_getln(server_socket, buf, sizeof buf);
52         if (buf[0] == '1') {
53                 i = 0;
54                 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
55                         if (i == 1) {
56                                 sprintf(prompt, "\n%s> ", buf);
57                         }
58                         ++i;
59                 }
60         }
61
62         /* Here we go ... main command loop */
63         while (cmd = readline(prompt)) {
64
65                 if ((cmd) && (*cmd)) {
66                         add_history(cmd);
67
68                         sock_puts(server_socket, cmd);
69                         sock_getln(server_socket, server_reply, sizeof server_reply);
70                         printf("%s\n", server_reply);
71
72                         if ((server_reply[0] == '4') || (server_reply[0] == '8')) {
73                                 // FIXME
74                         }
75
76                         if ((server_reply[0] == '1') || (server_reply[0] == '8')) {
77                                 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
78                                         printf("%s\n", buf);
79                                 }
80                         }
81
82                 }
83
84                 free(cmd);
85         }
86 }
87
88 int main(int argc, char **argv)
89 {
90         int server_socket = 0;
91         char buf[1024];
92         int ipgm_secret = (-1);
93
94         printf("\nCitadel administration shell v" PACKAGE_VERSION "\n");
95         printf("(c) 2009 citadel.org GPLv3\n");
96
97         ipgm_secret = discover_ipgm_secret();
98         if (ipgm_secret < 0) {
99                 exit(1);
100         }
101
102         printf("Attaching to server...\r");
103         fflush(stdout);
104         server_socket = uds_connectsock(CTDLDIR "/citadel.socket");
105         if (server_socket < 0) {
106                 exit(1);
107         }
108         printf("                      \r");
109
110         sock_getln(server_socket, buf, sizeof buf);
111         printf("%s\n", buf);
112
113         sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
114         sock_getln(server_socket, buf, sizeof buf);
115         printf("%s\n", buf);
116
117         if (buf[0] == '2') {
118                 do_main_loop(server_socket);
119         }
120
121         sock_puts(server_socket, "QUIT");
122         sock_getln(server_socket, buf, sizeof buf);
123         printf("%s\n", buf);
124         close(server_socket);
125         exit(0);
126 }