2 * (c) 2009-2011 by Art Cancro and citadel.org
3 * This program is released under the terms of the GNU General Public License v3.
12 #include <readline/readline.h>
15 #define CTDLDIR "/appl/citadel"
19 int com_quit(char *cmdbuf) {
27 * Commands understood by ctdlsh
35 COMMAND commands[] = {
36 { "quit", com_quit, "Quit using ctdlsh" },
37 { "exit", com_quit, "Quit using ctdlsh" },
42 int discover_ipgm_secret(char *dirname) {
44 struct partial_config ccc;
45 char configfile[1024];
47 sprintf(configfile, "%s/citadel.config", dirname);
48 fd = open(configfile, O_RDONLY);
50 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
54 if (read(fd, &ccc, sizeof(struct partial_config)) != sizeof(struct partial_config)) {
55 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
59 fprintf(stderr, "%s: %s\n", configfile, strerror(errno));
62 return(ccc.c_ipgm_secret);
66 void do_main_loop(int server_socket) {
70 char server_reply[1024];
75 /* Do an INFO command and learn the hostname for the prompt */
76 sock_puts(server_socket, "INFO");
77 sock_getln(server_socket, buf, sizeof buf);
80 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
82 sprintf(prompt, "\n%s> ", buf);
88 /* Here we go ... main command loop */
89 while (cmd = readline(prompt)) {
91 if ((cmd) && (*cmd)) {
94 sock_puts(server_socket, cmd);
95 sock_getln(server_socket, server_reply, sizeof server_reply);
96 printf("%s\n", server_reply);
98 if ((server_reply[0] == '4') || (server_reply[0] == '8')) {
99 /* we might consider putting something here */
100 sock_puts(server_socket, "000");
103 if ((server_reply[0] == '1') || (server_reply[0] == '8')) {
104 while(sock_getln(server_socket, buf, sizeof buf), strcmp(buf, "000")) {
115 int main(int argc, char **argv)
117 int server_socket = 0;
119 int ipgm_secret = (-1);
121 char *ctdldir = CTDLDIR;
123 printf("\nCitadel administration shell v" PACKAGE_VERSION "\n");
124 printf("(c) 2009-2011 citadel.org GPLv3\n");
127 while ((c = getopt (argc, argv, "h:")) != -1) {
134 fprintf(stderr, "Option -%c requires an argument\n", optopt);
137 fprintf(stderr, "Unknown option '-%c'\n", optopt);
138 fprintf(stderr, "usage: %s [-h citadel_dir]\n", argv[0]);
144 ipgm_secret = discover_ipgm_secret(ctdldir);
145 if (ipgm_secret < 0) {
149 printf("Trying %s...\n", ctdldir);
150 sprintf(buf, "%s/citadel.socket", ctdldir);
151 server_socket = uds_connectsock(buf);
152 if (server_socket < 0) {
156 sock_getln(server_socket, buf, sizeof buf);
159 sock_printf(server_socket, "IPGM %d\n", ipgm_secret);
160 sock_getln(server_socket, buf, sizeof buf);
164 do_main_loop(server_socket);
167 sock_puts(server_socket, "QUIT");
168 sock_getln(server_socket, buf, sizeof buf);
170 close(server_socket);