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