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;
61 lprintf(CTDL_DEBUG, "POP3: %s %s %s <password>\n", roomname, pop3host, pop3user);
62 lprintf(CTDL_NOTICE, "Connecting to <%s>\n", pop3host);
63 sock = sock_connect(pop3host, "110", "tcp");
65 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
69 lprintf(CTDL_DEBUG, "Connected!\n");
71 /* Read the server greeting */
72 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
73 lprintf(CTDL_DEBUG, ">%s\n", buf);
74 if (strncasecmp(buf, "+OK", 3)) goto bail;
76 /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will
77 * automatically be appended by sock_puts(). Believe it or not, leaving out the CR
78 * will cause problems if the server happens to be Exchange, which is so b0rken it
79 * actually barfs on LF-terminated newlines.
81 snprintf(buf, sizeof buf, "USER %s\r", pop3user);
82 lprintf(CTDL_DEBUG, "<%s\n", buf);
83 if (sock_puts(sock, buf) <0) goto bail;
84 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
85 lprintf(CTDL_DEBUG, ">%s\n", buf);
86 if (strncasecmp(buf, "+OK", 3)) goto bail;
89 snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
90 lprintf(CTDL_DEBUG, "<PASS <password>\n");
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;
96 /* Get the list of messages */
97 snprintf(buf, sizeof buf, "LIST\r");
98 lprintf(CTDL_DEBUG, "<%s\n", buf);
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;
105 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
106 lprintf(CTDL_DEBUG, ">%s\n", buf);
107 msg_to_fetch = atoi(buf);
108 if (msg_to_fetch > 0) {
109 if (alloc_msgs == 0) {
111 msglist = malloc((alloc_msgs * (sizeof(int))));
113 else if (num_msgs >= alloc_msgs) {
114 alloc_msgs = alloc_msgs * 2;
115 msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
117 if (msglist == NULL) goto bail;
118 msglist[num_msgs++] = msg_to_fetch;
120 } while (buf[0] != '.');
122 if (num_msgs) for (i=0; i<num_msgs; ++i) {
124 /* Tell the server to fetch the message */
125 snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
126 lprintf(CTDL_DEBUG, "<%s\n", buf);
127 if (sock_puts(sock, buf) <0) goto bail;
128 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
129 lprintf(CTDL_DEBUG, ">%s\n", buf);
130 if (strncasecmp(buf, "+OK", 3)) goto bail;
132 /* If we get to this point, the message is on its way. Read it. */
133 body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
134 if (body == NULL) goto bail;
136 lprintf(CTDL_DEBUG, "Converting message...\n");
137 msg = convert_internet_message(body);
138 body = NULL; /* yes, this should be dereferenced, NOT freed */
140 /* Do Something With It (tm) */
141 msgnum = CtdlSubmitMsg(msg, NULL, roomname);
143 /* Message has been committed to the store, so delete it from the remote server */
144 snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
145 lprintf(CTDL_DEBUG, "<%s\n", buf);
146 if (sock_puts(sock, buf) <0) goto bail;
147 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
148 lprintf(CTDL_DEBUG, ">%s\n", buf);
149 if (strncasecmp(buf, "+OK", 3)) goto bail;
151 CtdlFreeMessage(msg);
155 snprintf(buf, sizeof buf, "QUIT\r");
156 lprintf(CTDL_DEBUG, "<%s\n", buf);
157 if (sock_puts(sock, buf) <0) goto bail;
158 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
159 lprintf(CTDL_DEBUG, ">%s\n", buf);
160 bail: sock_close(sock);
161 if (msglist) free(msglist);
166 * Scan a room's netconfig to determine whether it requires POP3 aggregation
168 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
170 char filename[PATH_MAX];
174 struct pop3aggr *pptr;
176 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
178 /* Only do net processing for rooms that have netconfigs */
179 fp = fopen(filename, "r");
184 while (fgets(buf, sizeof buf, fp) != NULL) {
185 buf[strlen(buf)-1] = 0;
187 extract_token(instr, buf, 0, '|', sizeof instr);
188 if (!strcasecmp(instr, "pop3client")) {
189 pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
191 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
192 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
193 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
194 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
207 void pop3client_scan(void) {
208 static time_t last_run = 0L;
209 static int doing_pop3client = 0;
210 struct pop3aggr *pptr;
213 * Run POP3 aggregation no more frequently than once every n seconds
215 if ( (time(NULL) - last_run) < config.c_net_freq ) {
220 * This is a simple concurrency check to make sure only one pop3client run
221 * is done at a time. We could do this with a mutex, but since we
222 * don't really require extremely fine granularity here, we'll do it
223 * with a static variable instead.
225 if (doing_pop3client) return;
226 doing_pop3client = 1;
228 lprintf(CTDL_DEBUG, "pop3client started\n");
229 ForEachRoom(pop3client_scan_room, NULL);
231 while (palist != NULL) {
232 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass);
234 palist = palist->next;
238 lprintf(CTDL_DEBUG, "pop3client ended\n");
239 last_run = time(NULL);
240 doing_pop3client = 0;
244 CTDL_MODULE_INIT(pop3client)
246 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
248 /* return our Subversion id for the Log */