-
- if (CtdlThreadCheckStop())
- goto bail;
-
- CtdlLogPrintf(CTDL_DEBUG, "Connected!\n");
- CCC->sReadBuf = NewStrBuf();
- CCC->sMigrateBuf = NewStrBuf();
- CCC->sPos = NULL;
-
- /* Read the server greeting */
- 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;
-
- /* Identify ourselves. NOTE: we have to append a CR to each command. The LF will
- * automatically be appended by sock_puts(). Believe it or not, leaving out the CR
- * will cause problems if the server happens to be Exchange, which is so b0rken it
- * actually barfs on LF-terminated newlines.
- */
- snprintf(buf, sizeof buf, "USER %s\r", pop3user);
- 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;
-
- /* Password */
- snprintf(buf, sizeof buf, "PASS %s\r", pop3pass);
- CtdlLogPrintf(CTDL_DEBUG, "<PASS <password>\n");
- 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;
-
- /* Get the list of messages */
- snprintf(buf, sizeof buf, "LIST\r");
- 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;
-
- do {
- if (CtdlThreadCheckStop())
- goto bail;
-
- if (sock_getln(&sock, buf, sizeof buf) < 0) goto bail;
- CtdlLogPrintf(CTDL_DEBUG, ">%s\n", buf);
- msg_to_fetch = atoi(buf);
- if (msg_to_fetch > 0) {
- if (alloc_msgs == 0) {
- alloc_msgs = 100;
- msglist = malloc((alloc_msgs * (sizeof(int))));
- }
- 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;