4 * Consolidate mail from remote POP3 accounts.
12 #if TIME_WITH_SYS_TIME
13 # include <sys/time.h>
17 # include <sys/time.h>
26 #include <sys/types.h>
28 #include <libcitadel.h>
31 #include "citserver.h"
35 #include "ctdl_module.h"
36 #include "clientsocket.h"
38 #include "internet_addressing.h"
40 #include "citadel_dirs.h"
43 struct pop3aggr *next;
44 char roomname[ROOMNAMELEN];
52 struct pop3aggr *palist = NULL;
55 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass, int keep)
65 struct CtdlMessage *msg = NULL;
69 struct cdbdata *cdbut;
72 CtdlLogPrintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
73 CtdlLogPrintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
75 if (CtdlThreadCheckStop())
78 sock = sock_connect(pop3host, "110", "tcp");
80 CtdlLogPrintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
84 if (CtdlThreadCheckStop())
87 CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
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;
94 if (CtdlThreadCheckStop())
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.
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;
109 if (CtdlThreadCheckStop())
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;
120 if (CtdlThreadCheckStop())
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;
131 if (CtdlThreadCheckStop())
135 if (CtdlThreadCheckStop())
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) {
144 msglist = malloc((alloc_msgs * (sizeof(int))));
146 else if (num_msgs >= alloc_msgs) {
147 alloc_msgs = alloc_msgs * 2;
148 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
150 if (msglist == NULL) goto bail;
151 msglist[num_msgs++] = msg_to_fetch;
153 } while (buf[0] != '.');
155 if (num_msgs) for (i=0; i<num_msgs; ++i) {
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);
166 snprintf(utmsgid, sizeof utmsgid, "pop3/%s/%s@%s", roomname, this_uidl, pop3host);
168 if (CtdlThreadCheckStop())
171 cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
173 /* message has already been seen */
174 CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
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) );
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;
191 if (CtdlThreadCheckStop())
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;
198 CtdlLogPrintf(CTDL_DEBUG, "Converting message...\n");
199 msg = convert_internet_message(body);
200 body = NULL; /* yes, this should be dereferenced, NOT freed */
202 /* Do Something With It (tm) */
203 msgnum = CtdlSubmitMsg(msg, NULL, roomname, 0);
205 /* Message has been committed to the store */
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 */
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) );
221 CtdlFreeMessage(msg);
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);
237 * Scan a room's netconfig to determine whether it requires POP3 aggregation
239 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
241 char filename[PATH_MAX];
245 struct pop3aggr *pptr;
247 if (CtdlThreadCheckStop())
250 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
252 /* Only do net processing for rooms that have netconfigs */
253 fp = fopen(filename, "r");
258 while (fgets(buf, sizeof buf, fp) != NULL) {
259 buf[strlen(buf)-1] = 0;
261 extract_token(instr, buf, 0, '|', sizeof instr);
262 if (!strcasecmp(instr, "pop3client")) {
263 pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
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);
283 void pop3client_scan(void) {
284 static time_t last_run = 0L;
285 static int doing_pop3client = 0;
286 struct pop3aggr *pptr;
289 if (config.c_pop3_fastest < config.c_pop3_fetch)
290 fastest_scan = config.c_pop3_fastest;
292 fastest_scan = config.c_pop3_fetch;
295 * Run POP3 aggregation no more frequently than once every n seconds
297 if ( (time(NULL) - last_run) < fastest_scan ) {
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.
307 if (doing_pop3client) return;
308 doing_pop3client = 1;
310 CtdlLogPrintf(CTDL_DEBUG, "pop3client started\n");
311 ForEachRoom(pop3client_scan_room, NULL);
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);
319 palist = palist->next;
323 CtdlLogPrintf(CTDL_DEBUG, "pop3client ended\n");
324 last_run = time(NULL);
325 doing_pop3client = 0;
329 CTDL_MODULE_INIT(pop3client)
333 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
336 /* return our Subversion id for the Log */