- else if (num_msgs >= alloc_msgs) {
- alloc_msgs = alloc_msgs * 2;
- msglist = realloc(msglist, (alloc_msgs * sizeof(int)));
- }
- if (msglist == NULL) goto bail;
- msglist[num_msgs++] = msg_to_fetch;
- }
- } while (buf[0] != '.');
-
- if (num_msgs) for (i=0; i<num_msgs; ++i) {
-
- /* Find out the UIDL of the message, to determine whether we've already downloaded it */
- snprintf(buf, sizeof buf, "UIDL %d\r", msglist[i]);
- CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
- if (sock_puts(sock, buf) <0) goto bail;
- if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
- CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
- if (strncasecmp(buf, "+OK", 3)) goto bail;
- extract_token(this_uidl, buf, 2, ' ', sizeof this_uidl);
-
- snprintf(utmsgid, sizeof utmsgid, "pop3/%s/%s@%s", roomname, this_uidl, pop3host);
-
- if (CtdlThreadCheckStop())
- goto bail;
-
- cdbut = cdb_fetch(CDB_USETABLE, utmsgid, strlen(utmsgid));
- if (cdbut != NULL) {
- /* message has already been seen */
- CtdlLogPrintf(CTDL_DEBUG, "%s has already been seen\n", utmsgid);
- cdb_free(cdbut);
-
- /* rewrite the record anyway, to update the timestamp */
- strcpy(ut.ut_msgid, utmsgid);
- ut.ut_timestamp = time(NULL);
- cdb_store(CDB_USETABLE, utmsgid, strlen(utmsgid), &ut, sizeof(struct UseTable) );
- }
- else {
- /* Message has not been seen. Tell the server to fetch the message... */
- snprintf(buf, sizeof buf, "RETR %d\r", msglist[i]);
- CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
- if (sock_puts(sock, buf) <0) goto bail;
- if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
- CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
- if (strncasecmp(buf, "+OK", 3)) goto bail;
-
- if (CtdlThreadCheckStop())
- goto bail;
-
- /* If we get to this point, the message is on its way. Read it. */
- body = CtdlReadMessageBody(HKEY("."), config.c_maxmsglen, NULL, 1, sock);
- if (body == NULL) goto bail;
-
- CtdlLogPrintf(CTDL_DEBUG, "Converting message...\n");
- msg = convert_internet_message(body);
- body = NULL; /* yes, this should be dereferenced, NOT freed */
-
- /* Do Something With It (tm) */
- msgnum = CtdlSubmitMsg(msg, NULL, roomname, 0);
- if (msgnum > 0L) {
- /* Message has been committed to the store */
-
- if (!keep) {
- snprintf(buf, sizeof buf, "DELE %d\r", msglist[i]);
- CtdlLogPrintf(CTDL_DEBUG, "<%s\n", buf);
- if (sock_puts(sock, buf) <0) goto bail;
- if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
- CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf); /* errors here are non-fatal */
+ int this_msg = atoi(oneuidl);
+ char *c = strchr(oneuidl, ' ');
+ if (c) strcpy(oneuidl, ++c);
+
+ // Make up the Use Table record so we can check if we've already seen this message.
+ StrBuf *UT = NewStrBuf();
+ StrBufPrintf(UT, "pop3/%s/%s:%s@%s", room, oneuidl, user, host);
+ int already_seen = CheckIfAlreadySeen(UT);
+ FreeStrBuf(&UT);
+
+ // Only fetch the message if we haven't seen it before.
+ if (already_seen == 0) {
+ StrBuf *TheMsg = NewStrBuf();
+ snprintf(cmd, sizeof cmd, "RETR %d", this_msg);
+ curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, cmd);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, TheMsg);
+ res = curl_easy_perform(curl);
+ if (res == CURLE_OK) {
+ struct CtdlMessage *msg = convert_internet_message_buf(&TheMsg);
+ CtdlSubmitMsg(msg, NULL, room);
+ CM_Free(msg);
+ }
+ else {
+ FreeStrBuf(&TheMsg);