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];
46 struct pop3aggr *palist = NULL;
48 void pop3_do_fetching(char *roomname, char *pop3host, char *pop3user, char *pop3pass)
58 struct CtdlMessage *msg = NULL;
62 lprintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
63 lprintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
64 sock = sock_connect(pop3host, "110", "tcp");
66 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
70 lprintf(CTDL_DEBUG, "Connected!\n");
72 /* Read the server greeting */
73 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
74 lprintf(CTDL_DEBUG, ">%s\n", buf);
75 if (strncasecmp(buf, "+OK", 3)) goto bail;
77 /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will
78 * automatically be appended by sock_puts(). Believe it or not, leaving out the CR
79 * will cause problems if the server happens to be Exchange, which is so b0rken it
80 * actually barfs on LF-terminated newlines.
82 snprintf(buf, sizeof buf, "USER %s\r", pop3user);
83 lprintf(CTDL_DEBUG, "<%s\n", buf);
84 if (sock_puts(sock, buf) <0) goto bail;
85 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
86 lprintf(CTDL_DEBUG, ">%s\n", buf);
87 if (strncasecmp(buf, "+OK", 3)) goto bail;
90 snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
91 lprintf(CTDL_DEBUG, "<PASS <password>\n");
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;
97 /* Get the list of messages */
98 snprintf(buf, sizeof buf, "LIST\r");
99 lprintf(CTDL_DEBUG, "<%s\n", buf);
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;
106 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
107 lprintf(CTDL_DEBUG, ">%s\n", buf);
108 msg_to_fetch = atoi(buf);
109 if (msg_to_fetch > 0) {
110 if (alloc_msgs == 0) {
112 msglist = malloc((alloc_msgs * (sizeof(int))));
114 else if (num_msgs >= alloc_msgs) {
115 alloc_msgs = alloc_msgs * 2;
116 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
118 if (msglist == NULL) goto bail;
119 msglist[num_msgs++] = msg_to_fetch;
121 } while (buf[0] != '.');
123 if (num_msgs) for (i=0; i<num_msgs; ++i) {
125 /* Find out the UIDL of the message, to determine whether we've already downloaded it */
126 snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
127 lprintf(CTDL_DEBUG, "<%s\n", buf);
128 if (sock_puts(sock, buf) <0) goto bail;
129 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
130 lprintf(CTDL_DEBUG, ">%s\n", buf);
131 if (strncasecmp(buf, "+OK", 3)) goto bail;
132 extract_token(this_uidl, buf, 3, ' ', sizeof this_uidl);
134 /* Tell the server to fetch the message */
135 snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
136 lprintf(CTDL_DEBUG, "<%s\n", buf);
137 if (sock_puts(sock, buf) <0) goto bail;
138 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
139 lprintf(CTDL_DEBUG, ">%s\n", buf);
140 if (strncasecmp(buf, "+OK", 3)) goto bail;
142 /* If we get to this point, the message is on its way. Read it. */
143 body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
144 if (body == NULL) goto bail;
146 lprintf(CTDL_DEBUG, "Converting message...\n");
147 msg = convert_internet_message(body);
148 body = NULL; /* yes, this should be dereferenced, NOT freed */
150 /* Do Something With It (tm) */
151 msgnum = CtdlSubmitMsg(msg, NULL, roomname);
153 /* Message has been committed to the store, so delete it from the remote server */
154 snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
155 lprintf(CTDL_DEBUG, "<%s\n", buf);
156 if (sock_puts(sock, buf) <0) goto bail;
157 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
158 lprintf(CTDL_DEBUG, ">%s\n", buf);
159 if (strncasecmp(buf, "+OK", 3)) goto bail;
161 CtdlFreeMessage(msg);
165 snprintf(buf, sizeof buf, "QUIT\r");
166 lprintf(CTDL_DEBUG, "<%s\n", buf);
167 if (sock_puts(sock, buf) <0) goto bail;
168 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
169 lprintf(CTDL_DEBUG, ">%s\n", buf);
170 bail: sock_close(sock);
171 if (msglist) free(msglist);
176 * Scan a room's netconfig to determine whether it requires POP3 aggregation
178 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
180 char filename[PATH_MAX];
184 struct pop3aggr *pptr;
186 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
188 /* Only do net processing for rooms that have netconfigs */
189 fp = fopen(filename, "r");
194 while (fgets(buf, sizeof buf, fp) != NULL) {
195 buf[strlen(buf)-1] = 0;
197 extract_token(instr, buf, 0, '|', sizeof instr);
198 if (!strcasecmp(instr, "pop3client")) {
199 pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
201 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
202 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
203 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
204 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
217 void pop3client_scan(void) {
218 static time_t last_run = 0L;
219 static int doing_pop3client = 0;
220 struct pop3aggr *pptr;
223 * Run POP3 aggregation no more frequently than once every n seconds
225 if ( (time(NULL) - last_run) < config.c_net_freq ) {
230 * This is a simple concurrency check to make sure only one pop3client run
231 * is done at a time. We could do this with a mutex, but since we
232 * don't really require extremely fine granularity here, we'll do it
233 * with a static variable instead.
235 if (doing_pop3client) return;
236 doing_pop3client = 1;
238 lprintf(CTDL_DEBUG, "pop3client started\n");
239 ForEachRoom(pop3client_scan_room, NULL);
241 while (palist != NULL) {
242 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass);
244 palist = palist->next;
248 lprintf(CTDL_DEBUG, "pop3client ended\n");
249 last_run = time(NULL);
250 doing_pop3client = 0;
254 CTDL_MODULE_INIT(pop3client)
256 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
258 /* return our Subversion id for the Log */