]> code.citadel.org Git - citadel.git/blob - citadel/getmail.c
* now getmail does s.th. almost usefull
[citadel.git] / citadel / getmail.c
1 /*
2  * $Id: sendcommand.c 5736 2007-11-10 23:12:19Z dothebart $
3  *
4  * Command-line utility to transmit a server command.
5  *
6  */
7
8
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <sys/types.h>
12 #include <sys/wait.h>
13 #include <string.h>
14 #include <fcntl.h>
15 #include <stdio.h>
16 #include <ctype.h>
17
18 #if TIME_WITH_SYS_TIME
19 # include <sys/time.h>
20 # include <time.h>
21 #else
22 # if HAVE_SYS_TIME_H
23 #  include <sys/time.h>
24 # else
25 #  include <time.h>
26 # endif
27 #endif
28
29 #include <signal.h>
30 #include <errno.h>
31 #include <limits.h>
32 #include <libcitadel.h>
33 #include "citadel.h"
34 #include "citadel_ipc.h"
35 #include "server.h"
36 #include "config.h"
37
38 #define LOCKFILE "/tmp/LCK.sendcommand"
39
40 static CtdlIPC *ipc = NULL;
41
42 /*
43  * make sure only one copy of sendcommand runs at a time, using lock files
44  */
45 int set_lockfile(void)
46 {
47         FILE *lfp;
48         int onppid;
49
50         if ((lfp = fopen(LOCKFILE, "r")) != NULL) {
51                 fscanf(lfp, "%d", &onppid);
52                 fclose(lfp);
53                 if (!kill(onppid, 0) || errno == EPERM)
54                         return 1;
55         }
56         lfp = fopen(LOCKFILE, "w");
57         fprintf(lfp, "%ld\n", (long) getpid());
58         fclose(lfp);
59         return (0);
60 }
61
62 void remove_lockfile(void)
63 {
64         unlink(LOCKFILE);
65 }
66
67 /*
68  * Why both cleanup() and nq_cleanup() ?  Notice the alarm() call in
69  * cleanup() .  If for some reason sendcommand hangs waiting for the server
70  * to clean up, the alarm clock goes off and the program exits anyway.
71  * The cleanup() routine makes a check to ensure it's not reentering, in
72  * case the ipc module looped it somehow.
73  */
74 void nq_cleanup(int e)
75 {
76         remove_lockfile();
77         exit(e);
78 }
79
80 /*
81  * send binary to server
82  */
83 void serv_write(CtdlIPC *ipc, const char *buf, unsigned int nbytes)
84 {
85         unsigned int bytes_written = 0;
86         int retval;
87 /*
88 #if defined(HAVE_OPENSSL)
89         if (ipc->ssl) {
90                 serv_write_ssl(ipc, buf, nbytes);
91                 return;
92         }
93 #endif
94 */
95         while (bytes_written < nbytes) {
96                 retval = write(ipc->sock, &buf[bytes_written],
97                                nbytes - bytes_written);
98                 if (retval < 1) {
99                         connection_died(ipc, 0);
100                         return;
101                 }
102                 bytes_written += retval;
103         }
104 }
105
106
107 void cleanup(int e)
108 {
109         static int nested = 0;
110
111         alarm(30);
112         signal(SIGALRM, nq_cleanup);
113         serv_write(ipc, "\n", 1);
114         if (nested++ < 1)
115                 CtdlIPCQuit(ipc);
116         nq_cleanup(e);
117 }
118
119 /*
120  * This is implemented as a function rather than as a macro because the
121  * client-side IPC modules expect logoff() to be defined.  They call logoff()
122  * when a problem connecting or staying connected to the server occurs.
123  */
124 void logoff(int e)
125 {
126         cleanup(e);
127 }
128
129 /*
130  * Connect sendcommand to the Citadel server running on this computer.
131  */
132 void np_attach_to_server(char *host, char *port)
133 {
134         char buf[SIZ];
135         char hostbuf[256] = "";
136         char portbuf[256] = "";
137         char *args[] =
138         {"getmail", NULL};
139         int r;
140
141         fprintf(stderr, "Attaching to server...\n");
142         strncpy(hostbuf, host, 256);
143         strncpy(portbuf, port, 256);
144         ipc = CtdlIPC_new(1, args, hostbuf, portbuf);
145         if (!ipc) {
146                 fprintf(stderr, "Can't connect: %s\n", strerror(errno));
147                 exit(3);
148         }
149         CtdlIPC_chat_recv(ipc, buf);
150         fprintf(stderr, "%s\n", &buf[4]);
151         snprintf(buf, sizeof buf, "IPGM %d", config.c_ipgm_secret);
152         r = CtdlIPCInternalProgram(ipc, config.c_ipgm_secret, buf);
153         fprintf(stderr, "%s\n", buf);
154         if (r / 100 != 2) {
155                 cleanup(2);
156         }
157 }
158
159
160 void sendcommand_die(void) {
161         exit(0);
162 }
163
164
165 /*
166  * main
167  */
168 int main(int argc, char **argv)
169 {
170         int a, r, i;
171         char cmd[5][SIZ];
172         char buf[SIZ];
173
174         int relh=0;
175         int home=0;
176         int n=0;
177         char relhome[PATH_MAX]="";
178         char ctdldir[PATH_MAX]=CTDLDIR;
179         fd_set read_fd;
180         struct timeval tv;
181         int ret, err;
182         int server_shutting_down = 0;
183         struct ctdlipcroom *Room;
184         struct ctdlipcmessage *mret;
185         char cret[SIZ];
186         unsigned long *msgarr;
187
188         strcpy(ctdl_home_directory, DEFAULT_PORT);
189
190         /*
191          * Change directories if specified
192          */
193         for (a = 1; a < argc && n < 5; ++a) {
194                 if (!strncmp(argv[a], "-h", 2)) {
195                         relh=argv[a][2]!='/';
196                         if (!relh) safestrncpy(ctdl_home_directory, &argv[a][2],
197                                                                    sizeof ctdl_home_directory);
198                         else
199                                 safestrncpy(relhome, &argv[a][2],
200                                                         sizeof relhome);
201                         home_specified = 1;
202                         home=1;
203                 } else {
204
205                         strcpy(cmd[n++], argv[a]);
206                 }
207         }
208
209         calc_dirs_n_files(relh, home, relhome, ctdldir, 0);
210         get_config();
211
212         signal(SIGINT, cleanup);
213         signal(SIGQUIT, cleanup);
214         signal(SIGHUP, cleanup);
215         signal(SIGTERM, cleanup);
216
217         fprintf(stderr, "getmail: started (pid=%d) "
218                         "running in %s\n",
219                         (int) getpid(),
220                         ctdl_home_directory);
221         fflush(stderr);
222
223         alarm(5);
224         signal(SIGALRM, nq_cleanup); /* Set up a watchdog type timer in case we hang */
225         
226         np_attach_to_server(UDS, ctdl_run_dir);
227         fflush(stderr);
228         setIPCDeathHook(sendcommand_die);
229
230         fprintf(stderr, "GOTO %s\n", cmd[0]);
231         CtdlIPCGotoRoom(ipc, cmd[0], "", &Room, cret);
232         fprintf(stderr, "%s\n", cret);
233
234         r = CtdlIPCGetMessages(ipc, 0, 0, NULL, &msgarr, buf);
235         for (i = 0; i < r ; i ++)
236         {
237                 printf("Message: %ld\n", msgarr[i]);
238         }
239
240
241         CtdlIPCGetSingleMessage(ipc, atol(cmd[1]) ,atol(cmd[1]),4, &mret, cret);
242         fprintf(stderr, "%s\n", cret);
243         fprintf(stderr, "%s: %s\n", "path", mret->path);
244         fprintf(stderr, "%s: %s\n", "subject", mret->subject);
245         fprintf(stderr, "%s: %s\n", "email", mret->email);
246         CtdlIPCQuit(ipc);
247         exit (1);
248         CtdlIPC_chat_send(ipc, cmd[4]);
249         CtdlIPC_chat_recv(ipc, buf);
250         fprintf(stderr, "%s\n", buf);
251
252         tv.tv_sec = 0;
253         tv.tv_usec = 1000;
254
255         if (!strncasecmp(&buf[1], "31", 2)) {
256                 server_shutting_down = 1;
257         }
258
259         if (buf[0] == '1') {
260                 while (CtdlIPC_chat_recv(ipc, buf), strcmp(buf, "000")) {
261                         printf("%s\n", buf);
262                         alarm(5); /* Kick the watchdog timer */
263                 }
264         } else if (buf[0] == '4') {
265                 do {
266                         if (fgets(buf, sizeof buf, stdin) == NULL)
267                                 strcpy(buf, "000");
268                         if (!IsEmptyStr(buf))
269                                 if (buf[strlen(buf) - 1] == '\n')
270                                         buf[strlen(buf) - 1] = 0;
271                         if (!IsEmptyStr(buf))
272                                 if (buf[strlen(buf) - 1] == '\r')
273                                         buf[strlen(buf) - 1] = 0;
274                         if (strcmp(buf, "000"))
275                                 CtdlIPC_chat_send(ipc, buf);
276                         
277                         FD_ZERO(&read_fd);
278                         FD_SET(ipc->sock, &read_fd);
279                         ret = select(ipc->sock+1, &read_fd, NULL, NULL,  &tv);
280                         err = errno;
281                         if (err!=0)
282                                 printf("select failed: %d", err);
283
284                         if (ret == -1) {
285                                 if (!(errno == EINTR || errno == EAGAIN))
286                                         printf("select failed: %d", err);
287                                 return 1;
288                         }
289
290                         if (ret != 0) {
291                                 size_t n;
292                                 char rbuf[SIZ];
293
294                                 rbuf[0] = '\0';
295                                 n = read(ipc->sock, rbuf, SIZ);
296                                 if (n>0) {
297                                         rbuf[n]='\0';
298                                         fprintf (stderr, rbuf);
299                                         fflush (stdout);
300                                 }
301                         }
302                         alarm(5); /* Kick the watchdog timer */
303                 } while (strcmp(buf, "000"));
304                 CtdlIPC_chat_send(ipc, "\n");
305                 CtdlIPC_chat_send(ipc, "000");
306         }
307         alarm(0);       /* Shutdown the watchdog timer */
308         fprintf(stderr, "sendcommand: processing ended.\n");
309
310         /* Clean up and log off ... unless the server indicated that the command
311          * we sent is shutting it down, in which case we want to just cut the
312          * connection and exit.
313          */
314         if (server_shutting_down) {
315                 nq_cleanup(0);
316         }
317         else {
318                 cleanup(0);
319         }
320         return 0;
321 }