4 * Consolidate mail from remote POP3 accounts.
6 * Copyright (c) 2007-2009 by the citadel.org team
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #if TIME_WITH_SYS_TIME
28 # include <sys/time.h>
32 # include <sys/time.h>
41 #include <sys/types.h>
43 #include <libcitadel.h>
46 #include "citserver.h"
50 #include "ctdl_module.h"
51 #include "clientsocket.h"
53 #include "internet_addressing.h"
55 #include "citadel_dirs.h"
58 struct pop3aggr *next;
59 char roomname[ROOMNAMELEN];
67 struct pop3aggr *palist = NULL;
70 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass, int keep)
80 struct CtdlMessage *msg = NULL;
84 struct cdbdata *cdbut;
87 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
88 CtdlLogPrintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
90 if (CtdlThreadCheckStop())
93 sock = sock_connect(pop3host, "110", "tcp");
95 CtdlLogPrintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
99 if (CtdlThreadCheckStop())
102 CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
104 /* Read the server greeting */
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;
109 if (CtdlThreadCheckStop())
112 /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will
113 * automatically be appended by sock_puts(). Believe it or not, leaving out the CR
114 * will cause problems if the server happens to be Exchange, which is so b0rken it
115 * actually barfs on LF-terminated newlines.
117 snprintf(buf, sizeof buf, "USER %s\r", pop3user);
118 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
119 if (sock_puts(sock, buf) <0) goto bail;
120 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
121 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
122 if (strncasecmp(buf, "+OK", 3)) goto bail;
124 if (CtdlThreadCheckStop())
128 snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
129 CtdlLogPrintf(CTDL_DEBUG, "<PASS <password>\n");
130 if (sock_puts(sock, buf) <0) goto bail;
131 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
132 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
133 if (strncasecmp(buf, "+OK", 3)) goto bail;
135 if (CtdlThreadCheckStop())
138 /* Get the list of messages */
139 snprintf(buf, sizeof buf, "LIST\r");
140 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
141 if (sock_puts(sock, buf) <0) goto bail;
142 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
143 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
144 if (strncasecmp(buf, "+OK", 3)) goto bail;
146 if (CtdlThreadCheckStop())
150 if (CtdlThreadCheckStop())
153 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
154 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
155 msg_to_fetch = atoi(buf);
156 if (msg_to_fetch > 0) {
157 if (alloc_msgs == 0) {
159 msglist = malloc((alloc_msgs * (sizeof(int))));
161 else if (num_msgs >= alloc_msgs) {
162 alloc_msgs = alloc_msgs * 2;
163 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
165 if (msglist == NULL) goto bail;
166 msglist[num_msgs++] = msg_to_fetch;
168 } while (buf[0] != '.');
170 if (num_msgs) for (i=0; i<num_msgs; ++i) {
172 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
173 snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
174 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
175 if (sock_puts(sock, buf) <0) goto bail;
176 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
177 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
178 if (strncasecmp(buf, "+OK", 3)) goto bail;
179 extract_token(this_uidl, buf, 2, ' ', sizeof this_uidl);
181 snprintf(utmsgid, sizeof utmsgid, "pop3/%s/%s@%s", roomname, this_uidl, pop3host);
183 if (CtdlThreadCheckStop())
186 cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
188 /* message has already been seen */
189 CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
192 /* rewrite the record anyway, to update the timestamp */
193 strcpy(ut.ut_msgid, utmsgid);
194 ut.ut_timestamp = time(NULL);
195 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
198 /* Message has not been seen. Tell the server to fetch the message... */
199 snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
200 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
201 if (sock_puts(sock, buf) <0) goto bail;
202 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
203 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
204 if (strncasecmp(buf, "+OK", 3)) goto bail;
206 if (CtdlThreadCheckStop())
209 /* If we get to this point, the message is on its way. Read it. */
210 body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
211 if (body == NULL) goto bail;
213 CtdlLogPrintf(CTDL_DEBUG, "Converting message...\n");
214 msg = convert_internet_message(body);
215 body = NULL; /* yes, this should be dereferenced, NOT freed */
217 /* Do Something With It (tm) */
218 msgnum = CtdlSubmitMsg(msg, NULL, roomname, 0);
220 /* Message has been committed to the store */
223 snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
224 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
225 if (sock_puts(sock, buf) <0) goto bail;
226 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
227 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf); /* errors here are non-fatal */
230 /* write the uidl to the use table so we don't fetch this message again */
231 strcpy(ut.ut_msgid, utmsgid);
232 ut.ut_timestamp = time(NULL);
233 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid),
234 &ut, sizeof(struct UseTable) );
236 CtdlFreeMessage(msg);
241 snprintf(buf, sizeof buf, "QUIT\r");
242 CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
243 if (sock_puts(sock, buf) <0) goto bail;
244 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
245 CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
246 bail: sock_close(sock);
247 if (msglist) free(msglist);
252 * Scan a room's netconfig to determine whether it requires POP3 aggregation
254 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
256 char filename[PATH_MAX];
260 struct pop3aggr *pptr;
262 if (CtdlThreadCheckStop())
265 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
267 /* Only do net processing for rooms that have netconfigs */
268 fp = fopen(filename, "r");
273 while (fgets(buf, sizeof buf, fp) != NULL) {
274 buf[strlen(buf)-1] = 0;
276 extract_token(instr, buf, 0, '|', sizeof instr);
277 if (!strcasecmp(instr, "pop3client")) {
278 pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
280 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
281 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
282 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
283 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
284 pptr->keep = extract_int(buf, 4);
285 pptr->interval = extract_long(buf, 5);
298 void pop3client_scan(void) {
299 static time_t last_run = 0L;
300 static int doing_pop3client = 0;
301 struct pop3aggr *pptr;
304 if (config.c_pop3_fastest < config.c_pop3_fetch)
305 fastest_scan = config.c_pop3_fastest;
307 fastest_scan = config.c_pop3_fetch;
310 * Run POP3 aggregation no more frequently than once every n seconds
312 if ( (time(NULL) - last_run) < fastest_scan ) {
317 * This is a simple concurrency check to make sure only one pop3client run
318 * is done at a time. We could do this with a mutex, but since we
319 * don't really require extremely fine granularity here, we'll do it
320 * with a static variable instead.
322 if (doing_pop3client) return;
323 doing_pop3client = 1;
325 CtdlLogPrintf(CTDL_DEBUG, "pop3client started\n");
326 ForEachRoom(pop3client_scan_room, NULL);
328 while (palist != NULL && !CtdlThreadCheckStop()) {
329 if ((palist->interval && time(NULL) > (last_run + palist->interval))
330 || (time(NULL) > last_run + config.c_pop3_fetch))
331 pop3_do_fetching(palist->roomname, palist->pop3host,
332 palist->pop3user, palist->pop3pass, palist->keep);
334 palist = palist->next;
338 CtdlLogPrintf(CTDL_DEBUG, "pop3client ended\n");
339 last_run = time(NULL);
340 doing_pop3client = 0;
344 CTDL_MODULE_INIT(pop3client)
348 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
351 /* return our Subversion id for the Log */