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"
39 #include "citadel_dirs.h"
42 struct pop3aggr *next;
43 char roomname[ROOMNAMELEN];
54 struct pop3aggr *palist = NULL;
56 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass)
66 struct CtdlMessage *msg = NULL;
69 struct uidl *new_uidl_map = NULL;
72 lprintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
73 lprintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
74 sock = sock_connect(pop3host, "110", "tcp");
76 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
80 lprintf(CTDL_DEBUG, "Connected!\n");
82 /* Read the server greeting */
83 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
84 lprintf(CTDL_DEBUG, ">%s\n", buf);
85 if (strncasecmp(buf, "+OK", 3)) goto bail;
87 /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will
88 * automatically be appended by sock_puts(). Believe it or not, leaving out the CR
89 * will cause problems if the server happens to be Exchange, which is so b0rken it
90 * actually barfs on LF-terminated newlines.
92 snprintf(buf, sizeof buf, "USER %s\r", pop3user);
93 lprintf(CTDL_DEBUG, "<%s\n", buf);
94 if (sock_puts(sock, buf) <0) goto bail;
95 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
96 lprintf(CTDL_DEBUG, ">%s\n", buf);
97 if (strncasecmp(buf, "+OK", 3)) goto bail;
100 snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
101 lprintf(CTDL_DEBUG, "<PASS <password>\n");
102 if (sock_puts(sock, buf) <0) goto bail;
103 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
104 lprintf(CTDL_DEBUG, ">%s\n", buf);
105 if (strncasecmp(buf, "+OK", 3)) goto bail;
107 /* Get the list of messages */
108 snprintf(buf, sizeof buf, "LIST\r");
109 lprintf(CTDL_DEBUG, "<%s\n", buf);
110 if (sock_puts(sock, buf) <0) goto bail;
111 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
112 lprintf(CTDL_DEBUG, ">%s\n", buf);
113 if (strncasecmp(buf, "+OK", 3)) goto bail;
116 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
117 lprintf(CTDL_DEBUG, ">%s\n", buf);
118 msg_to_fetch = atoi(buf);
119 if (msg_to_fetch > 0) {
120 if (alloc_msgs == 0) {
122 msglist = malloc((alloc_msgs * (sizeof(int))));
124 else if (num_msgs >= alloc_msgs) {
125 alloc_msgs = alloc_msgs * 2;
126 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
128 if (msglist == NULL) goto bail;
129 msglist[num_msgs++] = msg_to_fetch;
131 } while (buf[0] != '.');
133 if (num_msgs) for (i=0; i<num_msgs; ++i) {
135 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
136 snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
137 lprintf(CTDL_DEBUG, "<%s\n", buf);
138 if (sock_puts(sock, buf) <0) goto bail;
139 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
140 lprintf(CTDL_DEBUG, ">%s\n", buf);
141 if (strncasecmp(buf, "+OK", 3)) goto bail;
142 extract_token(this_uidl, buf, 3, ' ', sizeof this_uidl);
144 uptr = (struct uidl *) malloc(sizeof(struct uidl));
146 safestrncpy(uptr->uidl, this_uidl, sizeof uptr->uidl);
147 uptr->next = new_uidl_map;
151 /* Tell the server to fetch the message */
152 snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
153 lprintf(CTDL_DEBUG, "<%s\n", buf);
154 if (sock_puts(sock, buf) <0) goto bail;
155 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
156 lprintf(CTDL_DEBUG, ">%s\n", buf);
157 if (strncasecmp(buf, "+OK", 3)) goto bail;
159 /* If we get to this point, the message is on its way. Read it. */
160 body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
161 if (body == NULL) goto bail;
163 lprintf(CTDL_DEBUG, "Converting message...\n");
164 msg = convert_internet_message(body);
165 body = NULL; /* yes, this should be dereferenced, NOT freed */
167 /* Do Something With It (tm) */
168 msgnum = CtdlSubmitMsg(msg, NULL, roomname);
170 /* Message has been committed to the store, so delete it from the remote server */
171 snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
172 lprintf(CTDL_DEBUG, "<%s\n", buf);
173 if (sock_puts(sock, buf) <0) goto bail;
174 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
175 lprintf(CTDL_DEBUG, ">%s\n", buf);
176 if (strncasecmp(buf, "+OK", 3)) goto bail;
178 CtdlFreeMessage(msg);
182 snprintf(buf, sizeof buf, "QUIT\r");
183 lprintf(CTDL_DEBUG, "<%s\n", buf);
184 if (sock_puts(sock, buf) <0) goto bail;
185 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
186 lprintf(CTDL_DEBUG, ">%s\n", buf);
187 bail: sock_close(sock);
188 if (msglist) free(msglist);
190 while (new_uidl_map != NULL) {
191 uptr = new_uidl_map->next;
199 * Scan a room's netconfig to determine whether it requires POP3 aggregation
201 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
203 char filename[PATH_MAX];
207 struct pop3aggr *pptr;
209 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
211 /* Only do net processing for rooms that have netconfigs */
212 fp = fopen(filename, "r");
217 while (fgets(buf, sizeof buf, fp) != NULL) {
218 buf[strlen(buf)-1] = 0;
220 extract_token(instr, buf, 0, '|', sizeof instr);
221 if (!strcasecmp(instr, "pop3client")) {
222 pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
224 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
225 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
226 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
227 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
240 void pop3client_scan(void) {
241 static time_t last_run = 0L;
242 static int doing_pop3client = 0;
243 struct pop3aggr *pptr;
246 * Run POP3 aggregation no more frequently than once every n seconds
248 if ( (time(NULL) - last_run) < config.c_net_freq ) {
253 * This is a simple concurrency check to make sure only one pop3client run
254 * is done at a time. We could do this with a mutex, but since we
255 * don't really require extremely fine granularity here, we'll do it
256 * with a static variable instead.
258 if (doing_pop3client) return;
259 doing_pop3client = 1;
261 /* We can silently fail on these if the directory already exists. */
262 mkdir(ctdl_uidlmap_dir, 0700);
263 chmod(ctdl_uidlmap_dir, 0700);
264 chown(ctdl_uidlmap_dir, config.c_ctdluid, -1);
266 lprintf(CTDL_DEBUG, "pop3client started\n");
267 ForEachRoom(pop3client_scan_room, NULL);
269 while (palist != NULL) {
270 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass);
272 palist = palist->next;
276 lprintf(CTDL_DEBUG, "pop3client ended\n");
277 last_run = time(NULL);
278 doing_pop3client = 0;
282 CTDL_MODULE_INIT(pop3client)
284 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
286 /* return our Subversion id for the Log */