* make QP encoding of senders optional, so we can flag it if we need it.
[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 <libcitadel.h>
29 #include "citadel.h"
30 #include "server.h"
31 #include "citserver.h"
32 #include "support.h"
33 #include "config.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 "database.h"
40 #include "citadel_dirs.h"
41
42 struct pop3aggr {
43         struct pop3aggr *next;
44         char roomname[ROOMNAMELEN];
45         char pop3host[128];
46         char pop3user[128];
47         char pop3pass[128];
48         int keep;
49         time_t interval;
50 };
51
52 struct pop3aggr *palist = NULL;
53
54
55 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass, int keep)
56 {
57         int sock;
58         char buf[SIZ];
59         int msg_to_fetch = 0;
60         int *msglist = NULL;
61         int num_msgs = 0;
62         int alloc_msgs = 0;
63         int i;
64         char *body = NULL;
65         struct CtdlMessage *msg = NULL;
66         long msgnum = 0;
67         char this_uidl[64];
68         char utmsgid[SIZ];
69         struct cdbdata *cdbut;
70         struct UseTable ut;
71
72         CtdlLogPrintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
73         CtdlLogPrintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
74         
75         if (CtdlThreadCheckStop())
76                 return;
77                 
78         sock = sock_connect(pop3host, "110", "tcp");
79         if (sock < 0) {
80                 CtdlLogPrintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
81                 return;
82         }
83         
84         if (CtdlThreadCheckStop())
85                 goto bail;
86
87         CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
88
89         /* Read the server greeting */
90         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
91         CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
92         if (strncasecmp(buf, "+OK", 3)) goto bail;
93
94         if (CtdlThreadCheckStop())
95                 goto bail;
96
97         /* Identify ourselves.  NOTE: we have to append a CR to each command.  The LF will
98          * automatically be appended by sock_puts().  Believe it or not, leaving out the CR
99          * will cause problems if the server happens to be Exchange, which is so b0rken it
100          * actually barfs on LF-terminated newlines.
101          */
102         snprintf(buf, sizeof buf, "USER %s\r", pop3user);
103         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
104         if (sock_puts(sock, buf) <0) goto bail;
105         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
106         CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
107         if (strncasecmp(buf, "+OK", 3)) goto bail;
108
109         if (CtdlThreadCheckStop())
110                 goto bail;
111
112         /* Password */
113         snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
114         CtdlLogPrintf(CTDL_DEBUG, "<PASS <password>\n");
115         if (sock_puts(sock, buf) <0) goto bail;
116         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
117         CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
118         if (strncasecmp(buf, "+OK", 3)) goto bail;
119
120         if (CtdlThreadCheckStop())
121                 goto bail;
122
123         /* Get the list of messages */
124         snprintf(buf, sizeof buf, "LIST\r");
125         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
126         if (sock_puts(sock, buf) <0) goto bail;
127         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
128         CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
129         if (strncasecmp(buf, "+OK", 3)) goto bail;
130
131         if (CtdlThreadCheckStop())
132                 goto bail;
133
134         do {
135                 if (CtdlThreadCheckStop())
136                         goto bail;
137
138                 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
139                 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
140                 msg_to_fetch = atoi(buf);
141                 if (msg_to_fetch > 0) {
142                         if (alloc_msgs == 0) {
143                                 alloc_msgs = 100;
144                                 msglist = malloc((alloc_msgs * (sizeof(int))));
145                         }
146                         else if (num_msgs >= alloc_msgs) {
147                                 alloc_msgs = alloc_msgs * 2;
148                                 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
149                         }
150                         if (msglist == NULL) goto bail;
151                         msglist[num_msgs++] = msg_to_fetch;
152                 }
153         } while (buf[0] != '.');
154
155         if (num_msgs) for (i=0; i<num_msgs; ++i) {
156
157                 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
158                 snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
159                 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
160                 if (sock_puts(sock, buf) <0) goto bail;
161                 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
162                 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
163                 if (strncasecmp(buf, "+OK", 3)) goto bail;
164                 extract_token(this_uidl, buf, 2, ' ', sizeof this_uidl);
165
166                 snprintf(utmsgid, sizeof utmsgid, "pop3/%s/%s@%s", roomname, this_uidl, pop3host);
167
168                 if (CtdlThreadCheckStop())
169                         goto bail;
170
171                 cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
172                 if (cdbut != NULL) {
173                         /* message has already been seen */
174                         CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
175                         cdb_free(cdbut);
176
177                         /* rewrite the record anyway, to update the timestamp */
178                         strcpy(ut.ut_msgid, utmsgid);
179                         ut.ut_timestamp = time(NULL);
180                         cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
181                 }
182                 else {
183                         /* Message has not been seen. Tell the server to fetch the message... */
184                         snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
185                         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
186                         if (sock_puts(sock, buf) <0) goto bail;
187                         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
188                         CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
189                         if (strncasecmp(buf, "+OK", 3)) goto bail;
190         
191                         if (CtdlThreadCheckStop())
192                                 goto bail;
193
194                         /* If we get to this point, the message is on its way.  Read it. */
195                         body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
196                         if (body == NULL) goto bail;
197         
198                         CtdlLogPrintf(CTDL_DEBUG, "Converting message...\n");
199                         msg = convert_internet_message(body);
200                         body = NULL;    /* yes, this should be dereferenced, NOT freed */
201         
202                         /* Do Something With It (tm) */
203                         msgnum = CtdlSubmitMsg(msg, NULL, roomname, 0);
204                         if (msgnum > 0L) {
205                                 /* Message has been committed to the store */
206         
207                                 if (!keep) {
208                                         snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
209                                         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
210                                         if (sock_puts(sock, buf) <0) goto bail;
211                                         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
212                                         CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf); /* errors here are non-fatal */
213                                 }
214
215                                 /* write the uidl to the use table so we don't fetch this message again */
216                                 strcpy(ut.ut_msgid, utmsgid);
217                                 ut.ut_timestamp = time(NULL);
218                                 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid),
219                                                 &ut, sizeof(struct UseTable) );
220                         }
221                         CtdlFreeMessage(msg);
222                 }
223         }
224
225         /* Log out */
226         snprintf(buf, sizeof buf, "QUIT\r");
227         CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
228         if (sock_puts(sock, buf) <0) goto bail;
229         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
230         CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
231 bail:   sock_close(sock);
232         if (msglist) free(msglist);
233 }
234
235
236 /*
237  * Scan a room's netconfig to determine whether it requires POP3 aggregation
238  */
239 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
240 {
241         char filename[PATH_MAX];
242         char buf[1024];
243         char instr[32];
244         FILE *fp;
245         struct pop3aggr *pptr;
246
247         if (CtdlThreadCheckStop())
248                 return;
249
250         assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
251
252         /* Only do net processing for rooms that have netconfigs */
253         fp = fopen(filename, "r");
254         if (fp == NULL) {
255                 return;
256         }
257
258         while (fgets(buf, sizeof buf, fp) != NULL) {
259                 buf[strlen(buf)-1] = 0;
260
261                 extract_token(instr, buf, 0, '|', sizeof instr);
262                 if (!strcasecmp(instr, "pop3client")) {
263                         pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
264                         if (pptr != NULL) {
265                                 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
266                                 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
267                                 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
268                                 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
269                                 pptr->keep = extract_int(buf, 4);
270                                 pptr->interval = extract_long(buf, 5);
271                                 pptr->next = palist;
272                                 palist = pptr;
273                         }
274                 }
275
276         }
277
278         fclose(fp);
279
280 }
281
282
283 void pop3client_scan(void) {
284         static time_t last_run = 0L;
285         static int doing_pop3client = 0;
286         struct pop3aggr *pptr;
287         time_t fastest_scan;
288         
289         if (config.c_pop3_fastest < config.c_pop3_fetch)
290                 fastest_scan = config.c_pop3_fastest;
291         else
292                 fastest_scan = config.c_pop3_fetch;
293
294         /*
295          * Run POP3 aggregation no more frequently than once every n seconds
296          */
297         if ( (time(NULL) - last_run) < fastest_scan ) {
298                 return;
299         }
300
301         /*
302          * This is a simple concurrency check to make sure only one pop3client run
303          * is done at a time.  We could do this with a mutex, but since we
304          * don't really require extremely fine granularity here, we'll do it
305          * with a static variable instead.
306          */
307         if (doing_pop3client) return;
308         doing_pop3client = 1;
309
310         CtdlLogPrintf(CTDL_DEBUG, "pop3client started\n");
311         ForEachRoom(pop3client_scan_room, NULL);
312
313         while (palist != NULL && !CtdlThreadCheckStop()) {
314                 if ((palist->interval && time(NULL) > (last_run + palist->interval))
315                         || (time(NULL) > last_run + config.c_pop3_fetch))
316                                 pop3_do_fetching(palist->roomname, palist->pop3host,
317                                         palist->pop3user, palist->pop3pass, palist->keep);
318                 pptr = palist;
319                 palist = palist->next;
320                 free(pptr);
321         }
322
323         CtdlLogPrintf(CTDL_DEBUG, "pop3client ended\n");
324         last_run = time(NULL);
325         doing_pop3client = 0;
326 }
327
328
329 CTDL_MODULE_INIT(pop3client)
330 {
331         if (!threading)
332         {
333                 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
334         }
335         
336         /* return our Subversion id for the Log */
337         return "$Id$";
338 }