]> code.citadel.org Git - citadel.git/blobdiff - ctdlsh/src/main.c
* Fleshed out the ctdlsh interface a bit. Right now it's just a simple shell that...
[citadel.git] / ctdlsh / src / main.c
index 876b8e0f4cb8bf55fbb91a20d2d988c44c56db02..2658b7374134aa4eef9ee42cb884ce113c73eafa 100644 (file)
 #include <stdlib.h>
 #include <unistd.h>
 #include <stdio.h>
+#include <fcntl.h>
+#include <errno.h>
 #include <readline/readline.h>
+#include "ctdlsh.h"
+
+#define CTDLDIR        "/root/ctdl/trunk/citadel"
+
+
+int discover_ipgm_secret(void) {
+       int fd;
+       struct partial_config ccc;
+
+       fd = open(CTDLDIR "/citadel.config", O_RDONLY);
+       if (fd < 0) {
+               fprintf(stderr, "citadel.config: %s\n", strerror(errno));
+               return(-1);
+       }
+
+       if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
+               fprintf(stderr, "citadel.config: %s\n", strerror(errno));
+               return(-1);
+       }
+       if (close(fd) != 0) {
+               fprintf(stderr, "citadel.config: %s\n", strerror(errno));
+               return(-1);
+       }
+       return(ccc.c_ipgm_secret);
+}
+
+
+void do_main_loop(int server_socket) {
+       char *cmd = NULL;
+       char prompt[1024];
+       char buf[1024];
+       char server_reply[1024];
+       int i;
+
+       strcpy(prompt, "> ");
+
+       /* Do an INFO command and learn the hostname for the prompt */
+       sock_puts(server_socket, "INFO");
+       sock_getln(server_socket, buf, sizeof buf);
+       if (buf[0] == '1') {
+               i = 0;
+               while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
+                       if (i == 1) {
+                               sprintf(prompt, "\n%s> ", buf);
+                       }
+                       ++i;
+               }
+       }
+
+       /* Here we go ... main command loop */
+       while (cmd = readline(prompt)) {
+
+               if ((cmd) && (*cmd)) {
+                       add_history(cmd);
+
+                       sock_puts(server_socket, cmd);
+                       sock_getln(server_socket, server_reply, sizeof server_reply);
+                       printf("%s\n", server_reply);
+
+                       if ((server_reply[0] == '4') || (server_reply[0] == '8')) {
+                               // FIXME
+                       }
+
+                       if ((server_reply[0] == '1') || (server_reply[0] == '8')) {
+                               while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
+                                       printf("%s\n", buf);
+                               }
+                       }
+
+               }
+
+               free(cmd);
+       }
+}
 
 int main(int argc, char **argv)
 {
-       char *cmd = NULL;
-       char *prompt = "> ";
        int server_socket = 0;
        char buf[1024];
+       int ipgm_secret = (-1);
 
        printf("\nCitadel administration shell v" PACKAGE_VERSION "\n");
        printf("(c) 2009 citadel.org GPLv3\n");
 
+       ipgm_secret = discover_ipgm_secret();
+       if (ipgm_secret < 0) {
+               exit(1);
+       }
+
        printf("Attaching to server...\r");
        fflush(stdout);
-       server_socket = uds_connectsock("/root/ctdl/trunk/citadel/citadel.socket");
+       server_socket = uds_connectsock(CTDLDIR "/citadel.socket");
        if (server_socket < 0) {
                exit(1);
        }
@@ -30,16 +110,13 @@ int main(int argc, char **argv)
        sock_getln(server_socket, buf, sizeof buf);
        printf("%s\n", buf);
 
-       while (cmd = readline(prompt)) {
-
-               if ((cmd) && (*cmd)) {
-                       add_history(cmd);
-               }
+       sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
+       sock_getln(server_socket, buf, sizeof buf);
+       printf("%s\n", buf);
 
-               printf("\nHaha, you said: '%s'\n\n", cmd);
-               free(cmd);
+       if (buf[0] == '2') {
+               do_main_loop(server_socket);
        }
-       printf("\r");
 
        sock_puts(server_socket, "QUIT");
        sock_getln(server_socket, buf, sizeof buf);