2 * Consolidate mail from remote POP3 accounts.
4 * Copyright (c) 2007-2009 by the citadel.org team
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #if TIME_WITH_SYS_TIME
26 # include <sys/time.h>
30 # include <sys/time.h>
39 #include <sys/types.h>
41 #include <libcitadel.h>
44 #include "citserver.h"
47 #include "ctdl_module.h"
48 #include "clientsocket.h"
50 #include "internet_addressing.h"
52 #include "citadel_dirs.h"
55 struct pop3aggr *next;
56 char roomname[ROOMNAMELEN];
64 struct pop3aggr *palist = NULL;
67 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass, int keep)
77 struct CtdlMessage *msg = NULL;
81 struct cdbdata *cdbut;
85 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
86 CtdlLogPrintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
88 if (CtdlThreadCheckStop())
91 sock = sock_connect(pop3host, "110");
93 CtdlLogPrintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
97 if (CtdlThreadCheckStop())
100 CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
101 CCC->sReadBuf = NewStrBuf();
102 CCC->sMigrateBuf = NewStrBuf();
105 /* Read the server greeting */
106 if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
107 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
108 if (strncasecmp(buf, "+OK", 3)) goto bail;
110 if (CtdlThreadCheckStop())
113 /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will
114 * automatically be appended by sock_puts(). Believe it or not, leaving out the CR
115 * will cause problems if the server happens to be Exchange, which is so b0rken it
116 * actually barfs on LF-terminated newlines.
118 snprintf(buf, sizeof buf, "USER %s\r", pop3user);
119 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
120 if (sock_puts(&sock, buf) <0) goto bail;
121 if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
122 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
123 if (strncasecmp(buf, "+OK", 3)) goto bail;
125 if (CtdlThreadCheckStop())
129 snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
130 CtdlLogPrintf(CTDL_DEBUG, "<PASS <password>\n");
131 if (sock_puts(&sock, buf) <0) goto bail;
132 if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
133 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
134 if (strncasecmp(buf, "+OK", 3)) goto bail;
136 if (CtdlThreadCheckStop())
139 /* Get the list of messages */
140 snprintf(buf, sizeof buf, "LIST\r");
141 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
142 if (sock_puts(&sock, buf) <0) goto bail;
143 if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
144 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
145 if (strncasecmp(buf, "+OK", 3)) goto bail;
147 if (CtdlThreadCheckStop())
151 if (CtdlThreadCheckStop())
154 if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
155 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
156 msg_to_fetch = atoi(buf);
157 if (msg_to_fetch > 0) {
158 if (alloc_msgs == 0) {
160 msglist = malloc((alloc_msgs * (sizeof(int))));
162 else if (num_msgs >= alloc_msgs) {
163 alloc_msgs = alloc_msgs * 2;
164 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
166 if (msglist == NULL) goto bail;
167 msglist[num_msgs++] = msg_to_fetch;
169 } while (buf[0] != '.');
171 if (num_msgs) for (i=0; i<num_msgs; ++i) {
173 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
174 snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
175 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
176 if (sock_puts(&sock, buf) <0) goto bail;
177 if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
178 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
179 if (strncasecmp(buf, "+OK", 3)) goto bail;
180 extract_token(this_uidl, buf, 2, ' ', sizeof this_uidl);
182 snprintf(utmsgid, sizeof utmsgid, "pop3/%s/%s@%s", roomname, this_uidl, pop3host);
184 if (CtdlThreadCheckStop())
187 cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
189 /* message has already been seen */
190 CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
193 /* rewrite the record anyway, to update the timestamp */
194 strcpy(ut.ut_msgid, utmsgid);
195 ut.ut_timestamp = time(NULL);
196 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
199 /* Message has not been seen. Tell the server to fetch the message... */
200 snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
201 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
202 if (sock_puts(&sock, buf) <0) goto bail;
203 if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
204 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
205 if (strncasecmp(buf, "+OK", 3)) goto bail;
207 if (CtdlThreadCheckStop())
210 /* If we get to this point, the message is on its way. Read it. */
211 body = CtdlReadMessageBody(HKEY("."), config.c_maxmsglen, NULL, 1, &sock);
212 if (body == NULL) goto bail;
214 CtdlLogPrintf(CTDL_DEBUG, "Converting message...\n");
215 msg = convert_internet_message(body);
216 body = NULL; /* yes, this should be dereferenced, NOT freed */
218 /* Do Something With It (tm) */
219 msgnum = CtdlSubmitMsg(msg, NULL, roomname, 0);
221 /* Message has been committed to the store */
224 snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
225 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
226 if (sock_puts(&sock, buf) <0) goto bail;
227 if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
228 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf); /* errors here are non-fatal */
231 /* write the uidl to the use table so we don't fetch this message again */
232 strcpy(ut.ut_msgid, utmsgid);
233 ut.ut_timestamp = time(NULL);
234 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid),
235 &ut, sizeof(struct UseTable) );
237 CtdlFreeMessage(msg);
242 snprintf(buf, sizeof buf, "QUIT\r");
243 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
244 if (sock_puts(&sock, buf) <0) goto bail;
245 if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
246 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
248 FreeStrBuf(&CCC->sReadBuf);
249 FreeStrBuf(&CCC->sMigrateBuf);
253 if (msglist) free(msglist);
258 * Scan a room's netconfig to determine whether it requires POP3 aggregation
260 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
262 char filename[PATH_MAX];
266 struct pop3aggr *pptr;
268 if (CtdlThreadCheckStop())
271 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
273 /* Only do net processing for rooms that have netconfigs */
274 fp = fopen(filename, "r");
279 while (fgets(buf, sizeof buf, fp) != NULL) {
280 buf[strlen(buf)-1] = 0;
282 extract_token(instr, buf, 0, '|', sizeof instr);
283 if (!strcasecmp(instr, "pop3client")) {
284 pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
286 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
287 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
288 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
289 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
290 pptr->keep = extract_int(buf, 4);
291 pptr->interval = extract_long(buf, 5);
304 void pop3client_scan(void) {
305 static time_t last_run = 0L;
306 static int doing_pop3client = 0;
307 struct pop3aggr *pptr;
310 if (config.c_pop3_fastest < config.c_pop3_fetch)
311 fastest_scan = config.c_pop3_fastest;
313 fastest_scan = config.c_pop3_fetch;
316 * Run POP3 aggregation no more frequently than once every n seconds
318 if ( (time(NULL) - last_run) < fastest_scan ) {
323 * This is a simple concurrency check to make sure only one pop3client run
324 * is done at a time. We could do this with a mutex, but since we
325 * don't really require extremely fine granularity here, we'll do it
326 * with a static variable instead.
328 if (doing_pop3client) return;
329 doing_pop3client = 1;
331 CtdlLogPrintf(CTDL_DEBUG, "pop3client started\n");
332 CtdlForEachRoom(pop3client_scan_room, NULL);
334 while (palist != NULL && !CtdlThreadCheckStop()) {
335 if ((palist->interval && time(NULL) > (last_run + palist->interval))
336 || (time(NULL) > last_run + config.c_pop3_fetch))
337 pop3_do_fetching(palist->roomname, palist->pop3host,
338 palist->pop3user, palist->pop3pass, palist->keep);
340 palist = palist->next;
344 CtdlLogPrintf(CTDL_DEBUG, "pop3client ended\n");
345 last_run = time(NULL);
346 doing_pop3client = 0;
350 CTDL_MODULE_INIT(pop3client)
354 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
357 /* return our Subversion id for the Log */