4 * Consolidate mail from remote POP3 accounts.
12 #if TIME_WITH_SYS_TIME
13 # include <sys/time.h>
17 # include <sys/time.h>
28 #include "citserver.h"
33 #include "ctdl_module.h"
34 #include "clientsocket.h"
36 #include "internet_addressing.h"
39 struct pop3aggr *next;
40 char roomname[ROOMNAMELEN];
51 struct pop3aggr *palist = NULL;
53 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass)
63 struct CtdlMessage *msg = NULL;
66 struct uidl *new_uidl_map = NULL;
69 lprintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
70 lprintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
71 sock = sock_connect(pop3host, "110", "tcp");
73 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
77 lprintf(CTDL_DEBUG, "Connected!\n");
79 /* Read the server greeting */
80 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
81 lprintf(CTDL_DEBUG, ">%s\n", buf);
82 if (strncasecmp(buf, "+OK", 3)) goto bail;
84 /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will
85 * automatically be appended by sock_puts(). Believe it or not, leaving out the CR
86 * will cause problems if the server happens to be Exchange, which is so b0rken it
87 * actually barfs on LF-terminated newlines.
89 snprintf(buf, sizeof buf, "USER %s\r", pop3user);
90 lprintf(CTDL_DEBUG, "<%s\n", buf);
91 if (sock_puts(sock, buf) <0) goto bail;
92 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
93 lprintf(CTDL_DEBUG, ">%s\n", buf);
94 if (strncasecmp(buf, "+OK", 3)) goto bail;
97 snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
98 lprintf(CTDL_DEBUG, "<PASS <password>\n");
99 if (sock_puts(sock, buf) <0) goto bail;
100 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
101 lprintf(CTDL_DEBUG, ">%s\n", buf);
102 if (strncasecmp(buf, "+OK", 3)) goto bail;
104 /* Get the list of messages */
105 snprintf(buf, sizeof buf, "LIST\r");
106 lprintf(CTDL_DEBUG, "<%s\n", buf);
107 if (sock_puts(sock, buf) <0) goto bail;
108 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
109 lprintf(CTDL_DEBUG, ">%s\n", buf);
110 if (strncasecmp(buf, "+OK", 3)) goto bail;
113 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
114 lprintf(CTDL_DEBUG, ">%s\n", buf);
115 msg_to_fetch = atoi(buf);
116 if (msg_to_fetch > 0) {
117 if (alloc_msgs == 0) {
119 msglist = malloc((alloc_msgs * (sizeof(int))));
121 else if (num_msgs >= alloc_msgs) {
122 alloc_msgs = alloc_msgs * 2;
123 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
125 if (msglist == NULL) goto bail;
126 msglist[num_msgs++] = msg_to_fetch;
128 } while (buf[0] != '.');
130 if (num_msgs) for (i=0; i<num_msgs; ++i) {
132 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
133 snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
134 lprintf(CTDL_DEBUG, "<%s\n", buf);
135 if (sock_puts(sock, buf) <0) goto bail;
136 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
137 lprintf(CTDL_DEBUG, ">%s\n", buf);
138 if (strncasecmp(buf, "+OK", 3)) goto bail;
139 extract_token(this_uidl, buf, 3, ' ', sizeof this_uidl);
141 uptr = (struct uidl *) malloc(sizeof(struct uidl));
143 safestrncpy(uptr->uidl, this_uidl, sizeof uptr->uidl);
144 uptr->next = new_uidl_map;
148 /* Tell the server to fetch the message */
149 snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
150 lprintf(CTDL_DEBUG, "<%s\n", buf);
151 if (sock_puts(sock, buf) <0) goto bail;
152 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
153 lprintf(CTDL_DEBUG, ">%s\n", buf);
154 if (strncasecmp(buf, "+OK", 3)) goto bail;
156 /* If we get to this point, the message is on its way. Read it. */
157 body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
158 if (body == NULL) goto bail;
160 lprintf(CTDL_DEBUG, "Converting message...\n");
161 msg = convert_internet_message(body);
162 body = NULL; /* yes, this should be dereferenced, NOT freed */
164 /* Do Something With It (tm) */
165 msgnum = CtdlSubmitMsg(msg, NULL, roomname);
167 /* Message has been committed to the store, so delete it from the remote server */
168 snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
169 lprintf(CTDL_DEBUG, "<%s\n", buf);
170 if (sock_puts(sock, buf) <0) goto bail;
171 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
172 lprintf(CTDL_DEBUG, ">%s\n", buf);
173 if (strncasecmp(buf, "+OK", 3)) goto bail;
175 CtdlFreeMessage(msg);
179 snprintf(buf, sizeof buf, "QUIT\r");
180 lprintf(CTDL_DEBUG, "<%s\n", buf);
181 if (sock_puts(sock, buf) <0) goto bail;
182 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
183 lprintf(CTDL_DEBUG, ">%s\n", buf);
184 bail: sock_close(sock);
185 if (msglist) free(msglist);
187 while (new_uidl_map != NULL) {
188 uptr = new_uidl_map->next;
196 * Scan a room's netconfig to determine whether it requires POP3 aggregation
198 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
200 char filename[PATH_MAX];
204 struct pop3aggr *pptr;
206 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
208 /* Only do net processing for rooms that have netconfigs */
209 fp = fopen(filename, "r");
214 while (fgets(buf, sizeof buf, fp) != NULL) {
215 buf[strlen(buf)-1] = 0;
217 extract_token(instr, buf, 0, '|', sizeof instr);
218 if (!strcasecmp(instr, "pop3client")) {
219 pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
221 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
222 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
223 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
224 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
237 void pop3client_scan(void) {
238 static time_t last_run = 0L;
239 static int doing_pop3client = 0;
240 struct pop3aggr *pptr;
243 * Run POP3 aggregation no more frequently than once every n seconds
245 if ( (time(NULL) - last_run) < config.c_net_freq ) {
250 * This is a simple concurrency check to make sure only one pop3client run
251 * is done at a time. We could do this with a mutex, but since we
252 * don't really require extremely fine granularity here, we'll do it
253 * with a static variable instead.
255 if (doing_pop3client) return;
256 doing_pop3client = 1;
258 lprintf(CTDL_DEBUG, "pop3client started\n");
259 ForEachRoom(pop3client_scan_room, NULL);
261 while (palist != NULL) {
262 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass);
264 palist = palist->next;
268 lprintf(CTDL_DEBUG, "pop3client ended\n");
269 last_run = time(NULL);
270 doing_pop3client = 0;
274 CTDL_MODULE_INIT(pop3client)
276 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
278 /* return our Subversion id for the Log */