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>
30 #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];
50 struct pop3aggr *palist = NULL;
53 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass, int delete_from_server)
63 struct CtdlMessage *msg = NULL;
67 struct cdbdata *cdbut;
70 lprintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
71 lprintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
72 sock = sock_connect(pop3host, "110", "tcp");
74 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
78 lprintf(CTDL_DEBUG, "Connected!\n");
80 /* Read the server greeting */
81 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
82 lprintf(CTDL_DEBUG, ">%s\n", buf);
83 if (strncasecmp(buf, "+OK", 3)) goto bail;
85 /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will
86 * automatically be appended by sock_puts(). Believe it or not, leaving out the CR
87 * will cause problems if the server happens to be Exchange, which is so b0rken it
88 * actually barfs on LF-terminated newlines.
90 snprintf(buf, sizeof buf, "USER %s\r", pop3user);
91 lprintf(CTDL_DEBUG, "<%s\n", buf);
92 if (sock_puts(sock, buf) <0) goto bail;
93 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
94 lprintf(CTDL_DEBUG, ">%s\n", buf);
95 if (strncasecmp(buf, "+OK", 3)) goto bail;
98 snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
99 lprintf(CTDL_DEBUG, "<PASS <password>\n");
100 if (sock_puts(sock, buf) <0) goto bail;
101 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
102 lprintf(CTDL_DEBUG, ">%s\n", buf);
103 if (strncasecmp(buf, "+OK", 3)) goto bail;
105 /* Get the list of messages */
106 snprintf(buf, sizeof buf, "LIST\r");
107 lprintf(CTDL_DEBUG, "<%s\n", buf);
108 if (sock_puts(sock, buf) <0) goto bail;
109 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
110 lprintf(CTDL_DEBUG, ">%s\n", buf);
111 if (strncasecmp(buf, "+OK", 3)) goto bail;
114 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
115 lprintf(CTDL_DEBUG, ">%s\n", buf);
116 msg_to_fetch = atoi(buf);
117 if (msg_to_fetch > 0) {
118 if (alloc_msgs == 0) {
120 msglist = malloc((alloc_msgs * (sizeof(int))));
122 else if (num_msgs >= alloc_msgs) {
123 alloc_msgs = alloc_msgs * 2;
124 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
126 if (msglist == NULL) goto bail;
127 msglist[num_msgs++] = msg_to_fetch;
129 } while (buf[0] != '.');
131 if (num_msgs) for (i=0; i<num_msgs; ++i) {
133 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
134 snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
135 lprintf(CTDL_DEBUG, "<%s\n", buf);
136 if (sock_puts(sock, buf) <0) goto bail;
137 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
138 lprintf(CTDL_DEBUG, ">%s\n", buf);
139 if (strncasecmp(buf, "+OK", 3)) goto bail;
140 extract_token(this_uidl, buf, 2, ' ', sizeof this_uidl);
142 snprintf(utmsgid, sizeof utmsgid, "pop3/%s/%s@%s", roomname, this_uidl, pop3host);
144 cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
146 /* message has already been seen */
147 lprintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
150 /* rewrite the record anyway, to update the timestamp */
151 strcpy(ut.ut_msgid, utmsgid);
152 ut.ut_timestamp = time(NULL);
153 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
156 /* Message has not been seen. Tell the server to fetch the message... */
157 snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
158 lprintf(CTDL_DEBUG, "<%s\n", buf);
159 if (sock_puts(sock, buf) <0) goto bail;
160 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
161 lprintf(CTDL_DEBUG, ">%s\n", buf);
162 if (strncasecmp(buf, "+OK", 3)) goto bail;
164 /* If we get to this point, the message is on its way. Read it. */
165 body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
166 if (body == NULL) goto bail;
168 lprintf(CTDL_DEBUG, "Converting message...\n");
169 msg = convert_internet_message(body);
170 body = NULL; /* yes, this should be dereferenced, NOT freed */
172 /* Do Something With It (tm) */
173 msgnum = CtdlSubmitMsg(msg, NULL, roomname);
175 /* Message has been committed to the store */
177 if (delete_from_server) {
178 snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
179 lprintf(CTDL_DEBUG, "<%s\n", buf);
180 if (sock_puts(sock, buf) <0) goto bail;
181 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
182 lprintf(CTDL_DEBUG, ">%s\n", buf); /* errors here are non-fatal */
185 /* write the uidl to the use table so we don't fetch this message again */
186 strcpy(ut.ut_msgid, utmsgid);
187 ut.ut_timestamp = time(NULL);
188 cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid),
189 &ut, sizeof(struct UseTable) );
191 CtdlFreeMessage(msg);
196 snprintf(buf, sizeof buf, "QUIT\r");
197 lprintf(CTDL_DEBUG, "<%s\n", buf);
198 if (sock_puts(sock, buf) <0) goto bail;
199 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
200 lprintf(CTDL_DEBUG, ">%s\n", buf);
201 bail: sock_close(sock);
202 if (msglist) free(msglist);
207 * Scan a room's netconfig to determine whether it requires POP3 aggregation
209 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
211 char filename[PATH_MAX];
215 struct pop3aggr *pptr;
217 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
219 /* Only do net processing for rooms that have netconfigs */
220 fp = fopen(filename, "r");
225 while (fgets(buf, sizeof buf, fp) != NULL) {
226 buf[strlen(buf)-1] = 0;
228 extract_token(instr, buf, 0, '|', sizeof instr);
229 if (!strcasecmp(instr, "pop3client")) {
230 pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
232 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
233 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
234 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
235 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
248 void pop3client_scan(void) {
249 static time_t last_run = 0L;
250 static int doing_pop3client = 0;
251 struct pop3aggr *pptr;
254 * Run POP3 aggregation no more frequently than once every n seconds
256 if ( (time(NULL) - last_run) < config.c_net_freq ) {
261 * This is a simple concurrency check to make sure only one pop3client run
262 * is done at a time. We could do this with a mutex, but since we
263 * don't really require extremely fine granularity here, we'll do it
264 * with a static variable instead.
266 if (doing_pop3client) return;
267 doing_pop3client = 1;
269 lprintf(CTDL_DEBUG, "pop3client started\n");
270 ForEachRoom(pop3client_scan_room, NULL);
272 while (palist != NULL) {
273 /* FIXME set delete_from_server to 1 if the user wants to */
274 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass, 0);
276 palist = palist->next;
280 lprintf(CTDL_DEBUG, "pop3client ended\n");
281 last_run = time(NULL);
282 doing_pop3client = 0;
286 CTDL_MODULE_INIT(pop3client)
288 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
290 /* return our Subversion id for the Log */