Create and maintain a directory for pop3client uidl maps
[citadel.git] / citadel / modules / pop3client / serv_pop3client.c
1 /*
2  * $Id$
3  *
4  * Consolidate mail from remote POP3 accounts.
5  *
6  */
7
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <stdio.h>
11
12 #if TIME_WITH_SYS_TIME
13 # include <sys/time.h>
14 # include <time.h>
15 #else
16 # if HAVE_SYS_TIME_H
17 #  include <sys/time.h>
18 # else
19 #  include <time.h>
20 # endif
21 #endif
22
23 #include <ctype.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include "citadel.h"
29 #include "server.h"
30 #include "citserver.h"
31 #include "support.h"
32 #include "config.h"
33 #include "tools.h"
34 #include "room_ops.h"
35 #include "ctdl_module.h"
36 #include "clientsocket.h"
37 #include "msgbase.h"
38 #include "internet_addressing.h"
39 #include "citadel_dirs.h"
40
41 struct pop3aggr {
42         struct pop3aggr *next;
43         char roomname[ROOMNAMELEN];
44         char pop3host[128];
45         char pop3user[128];
46         char pop3pass[128];
47 };
48
49 struct uidl {
50         struct uidl *next;
51         char uidl[64];
52 };
53
54 struct pop3aggr *palist = NULL;
55
56 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass)
57 {
58         int sock;
59         char buf[SIZ];
60         int msg_to_fetch = 0;
61         int *msglist = NULL;
62         int num_msgs = 0;
63         int alloc_msgs = 0;
64         int i;
65         char *body = NULL;
66         struct CtdlMessage *msg = NULL;
67         long msgnum = 0;
68         char this_uidl[64];
69         struct uidl *new_uidl_map = NULL;
70         struct uidl *uptr;
71
72         lprintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
73         lprintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
74         sock = sock_connect(pop3host, "110", "tcp");
75         if (sock < 0) {
76                 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
77                 return;
78         }
79         
80         lprintf(CTDL_DEBUG, "Connected!\n");
81
82         /* Read the server greeting */
83         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
84         lprintf(CTDL_DEBUG, ">%s\n", buf);
85         if (strncasecmp(buf, "+OK", 3)) goto bail;
86
87         /* Identify ourselves.  NOTE: we have to append a CR to each command.  The LF will
88          * automatically be appended by sock_puts().  Believe it or not, leaving out the CR
89          * will cause problems if the server happens to be Exchange, which is so b0rken it
90          * actually barfs on LF-terminated newlines.
91          */
92         snprintf(buf, sizeof buf, "USER %s\r", pop3user);
93         lprintf(CTDL_DEBUG, "<%s\n", buf);
94         if (sock_puts(sock, buf) <0) goto bail;
95         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
96         lprintf(CTDL_DEBUG, ">%s\n", buf);
97         if (strncasecmp(buf, "+OK", 3)) goto bail;
98
99         /* Password */
100         snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
101         lprintf(CTDL_DEBUG, "<PASS <password>\n");
102         if (sock_puts(sock, buf) <0) goto bail;
103         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
104         lprintf(CTDL_DEBUG, ">%s\n", buf);
105         if (strncasecmp(buf, "+OK", 3)) goto bail;
106
107         /* Get the list of messages */
108         snprintf(buf, sizeof buf, "LIST\r");
109         lprintf(CTDL_DEBUG, "<%s\n", buf);
110         if (sock_puts(sock, buf) <0) goto bail;
111         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
112         lprintf(CTDL_DEBUG, ">%s\n", buf);
113         if (strncasecmp(buf, "+OK", 3)) goto bail;
114
115         do {
116                 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
117                 lprintf(CTDL_DEBUG, ">%s\n", buf);
118                 msg_to_fetch = atoi(buf);
119                 if (msg_to_fetch > 0) {
120                         if (alloc_msgs == 0) {
121                                 alloc_msgs = 100;
122                                 msglist = malloc((alloc_msgs * (sizeof(int))));
123                         }
124                         else if (num_msgs >= alloc_msgs) {
125                                 alloc_msgs = alloc_msgs * 2;
126                                 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
127                         }
128                         if (msglist == NULL) goto bail;
129                         msglist[num_msgs++] = msg_to_fetch;
130                 }
131         } while (buf[0] != '.');
132
133         if (num_msgs) for (i=0; i<num_msgs; ++i) {
134
135                 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
136                 snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
137                 lprintf(CTDL_DEBUG, "<%s\n", buf);
138                 if (sock_puts(sock, buf) <0) goto bail;
139                 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
140                 lprintf(CTDL_DEBUG, ">%s\n", buf);
141                 if (strncasecmp(buf, "+OK", 3)) goto bail;
142                 extract_token(this_uidl, buf, 3, ' ', sizeof this_uidl);
143
144                 uptr = (struct uidl *) malloc(sizeof(struct uidl));
145                 if (uptr != NULL) {
146                         safestrncpy(uptr->uidl, this_uidl, sizeof uptr->uidl);
147                         uptr->next = new_uidl_map;
148                         new_uidl_map = uptr;
149                 }
150
151                 /* Tell the server to fetch the message */
152                 snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
153                 lprintf(CTDL_DEBUG, "<%s\n", buf);
154                 if (sock_puts(sock, buf) <0) goto bail;
155                 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
156                 lprintf(CTDL_DEBUG, ">%s\n", buf);
157                 if (strncasecmp(buf, "+OK", 3)) goto bail;
158
159                 /* If we get to this point, the message is on its way.  Read it. */
160                 body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
161                 if (body == NULL) goto bail;
162
163                 lprintf(CTDL_DEBUG, "Converting message...\n");
164                 msg = convert_internet_message(body);
165                 body = NULL;    /* yes, this should be dereferenced, NOT freed */
166
167                 /* Do Something With It (tm) */
168                 msgnum = CtdlSubmitMsg(msg, NULL, roomname);
169                 if (msgnum > 0L) {
170                         /* Message has been committed to the store, so delete it from the remote server */
171                         snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
172                         lprintf(CTDL_DEBUG, "<%s\n", buf);
173                         if (sock_puts(sock, buf) <0) goto bail;
174                         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
175                         lprintf(CTDL_DEBUG, ">%s\n", buf);
176                         if (strncasecmp(buf, "+OK", 3)) goto bail;
177                 }
178                 CtdlFreeMessage(msg);
179         }
180
181         /* Log out */
182         snprintf(buf, sizeof buf, "QUIT\r");
183         lprintf(CTDL_DEBUG, "<%s\n", buf);
184         if (sock_puts(sock, buf) <0) goto bail;
185         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
186         lprintf(CTDL_DEBUG, ">%s\n", buf);
187 bail:   sock_close(sock);
188         if (msglist) free(msglist);
189
190         while (new_uidl_map != NULL) {
191                 uptr = new_uidl_map->next;
192                 free(new_uidl_map);
193                 new_uidl_map = uptr;
194         }
195 }
196
197
198 /*
199  * Scan a room's netconfig to determine whether it requires POP3 aggregation
200  */
201 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
202 {
203         char filename[PATH_MAX];
204         char buf[1024];
205         char instr[32];
206         FILE *fp;
207         struct pop3aggr *pptr;
208
209         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
210
211         /* Only do net processing for rooms that have netconfigs */
212         fp = fopen(filename, "r");
213         if (fp == NULL) {
214                 return;
215         }
216
217         while (fgets(buf, sizeof buf, fp) != NULL) {
218                 buf[strlen(buf)-1] = 0;
219
220                 extract_token(instr, buf, 0, '|', sizeof instr);
221                 if (!strcasecmp(instr, "pop3client")) {
222                         pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
223                         if (pptr != NULL) {
224                                 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
225                                 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
226                                 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
227                                 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
228                                 pptr->next = palist;
229                                 palist = pptr;
230                         }
231                 }
232
233         }
234
235         fclose(fp);
236
237 }
238
239
240 void pop3client_scan(void) {
241         static time_t last_run = 0L;
242         static int doing_pop3client = 0;
243         struct pop3aggr *pptr;
244
245         /*
246          * Run POP3 aggregation no more frequently than once every n seconds
247          */
248         if ( (time(NULL) - last_run) < config.c_net_freq ) {
249                 return;
250         }
251
252         /*
253          * This is a simple concurrency check to make sure only one pop3client run
254          * is done at a time.  We could do this with a mutex, but since we
255          * don't really require extremely fine granularity here, we'll do it
256          * with a static variable instead.
257          */
258         if (doing_pop3client) return;
259         doing_pop3client = 1;
260
261         /* We can silently fail on these if the directory already exists. */
262         mkdir(ctdl_uidlmap_dir, 0700);
263         chmod(ctdl_uidlmap_dir, 0700);
264         chown(ctdl_uidlmap_dir, config.c_ctdluid, -1);
265
266         lprintf(CTDL_DEBUG, "pop3client started\n");
267         ForEachRoom(pop3client_scan_room, NULL);
268
269         while (palist != NULL) {
270                 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass);
271                 pptr = palist;
272                 palist = palist->next;
273                 free(pptr);
274         }
275
276         lprintf(CTDL_DEBUG, "pop3client ended\n");
277         last_run = time(NULL);
278         doing_pop3client = 0;
279 }
280
281
282 CTDL_MODULE_INIT(pop3client)
283 {
284         CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
285
286         /* return our Subversion id for the Log */
287         return "$Id$";
288 }