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, int delete_from_server)
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 */
172 if (delete_from_server) {
173 snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
174 lprintf(CTDL_DEBUG, "<%s\n", buf);
175 if (sock_puts(sock, buf) <0) goto bail;
176 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
177 lprintf(CTDL_DEBUG, ">%s\n", buf); /* errors here are non-fatal */
180 CtdlFreeMessage(msg);
184 snprintf(buf, sizeof buf, "QUIT\r");
185 lprintf(CTDL_DEBUG, "<%s\n", buf);
186 if (sock_puts(sock, buf) <0) goto bail;
187 if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
188 lprintf(CTDL_DEBUG, ">%s\n", buf);
189 bail: sock_close(sock);
190 if (msglist) free(msglist);
192 while (new_uidl_map != NULL) {
193 uptr = new_uidl_map->next;
201 * Scan a room's netconfig to determine whether it requires POP3 aggregation
203 void pop3client_scan_room(struct ctdlroom *qrbuf, void *data)
205 char filename[PATH_MAX];
209 struct pop3aggr *pptr;
211 assoc_file_name(filename, sizeof filename, qrbuf, ctdl_netcfg_dir);
213 /* Only do net processing for rooms that have netconfigs */
214 fp = fopen(filename, "r");
219 while (fgets(buf, sizeof buf, fp) != NULL) {
220 buf[strlen(buf)-1] = 0;
222 extract_token(instr, buf, 0, '|', sizeof instr);
223 if (!strcasecmp(instr, "pop3client")) {
224 pptr = (struct pop3aggr *) malloc(sizeof(struct pop3aggr));
226 safestrncpy(pptr->roomname, qrbuf->QRname, sizeof pptr->roomname);
227 extract_token(pptr->pop3host, buf, 1, '|', sizeof pptr->pop3host);
228 extract_token(pptr->pop3user, buf, 2, '|', sizeof pptr->pop3user);
229 extract_token(pptr->pop3pass, buf, 3, '|', sizeof pptr->pop3pass);
242 void pop3client_scan(void) {
243 static time_t last_run = 0L;
244 static int doing_pop3client = 0;
245 struct pop3aggr *pptr;
248 * Run POP3 aggregation no more frequently than once every n seconds
250 if ( (time(NULL) - last_run) < config.c_net_freq ) {
255 * This is a simple concurrency check to make sure only one pop3client run
256 * is done at a time. We could do this with a mutex, but since we
257 * don't really require extremely fine granularity here, we'll do it
258 * with a static variable instead.
260 if (doing_pop3client) return;
261 doing_pop3client = 1;
263 lprintf(CTDL_DEBUG, "pop3client started\n");
264 ForEachRoom(pop3client_scan_room, NULL);
266 while (palist != NULL) {
267 /* FIXME set delete_from_server to 1 if the user wants to */
268 pop3_do_fetching(palist->roomname, palist->pop3host, palist->pop3user, palist->pop3pass, 0);
270 palist = palist->next;
274 lprintf(CTDL_DEBUG, "pop3client ended\n");
275 last_run = time(NULL);
276 doing_pop3client = 0;
280 CTDL_MODULE_INIT(pop3client)
282 CtdlRegisterSessionHook(pop3client_scan, EVT_TIMER);
284 /* return our Subversion id for the Log */