+ /* Get the list of messages */
+ snprintf(buf, sizeof buf, "LIST");
+ lprintf(CTDL_DEBUG, "<%s\n", buf);
+ if (sock_puts(sock, buf) <0) goto bail;
+ if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+ lprintf(CTDL_DEBUG, ">%s\n", buf);
+ if (strncasecmp(buf, "+OK", 3)) goto bail;
+
+ do {
+ if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+ lprintf(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) {
+
+ /* Tell the server to fetch the message */
+ snprintf(buf, sizeof buf, "RETR %d", msglist[i]);
+ lprintf(CTDL_DEBUG, "<%s\n", buf);
+ if (sock_puts(sock, buf) <0) goto bail;
+ if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+ lprintf(CTDL_DEBUG, ">%s\n", buf);
+ if (strncasecmp(buf, "+OK", 3)) goto bail;
+
+ /* If we get to this point, the message is on its way. Read it. */
+ body = CtdlReadMessageBody(".", config.c_maxmsglen, NULL, 1, sock);
+ if (body == NULL) goto bail;
+
+ lprintf(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);
+ if (msgnum > 0L) {
+ /* Message has been committed to the store, so delete it from the remote server */
+ snprintf(buf, sizeof buf, "DELE %d", msglist[i]);
+ lprintf(CTDL_DEBUG, "<%s\n", buf);
+ if (sock_puts(sock, buf) <0) goto bail;
+ if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+ lprintf(CTDL_DEBUG, ">%s\n", buf);
+ if (strncasecmp(buf, "+OK", 3)) goto bail;
+ }
+ CtdlFreeMessage(msg);
+ }
+
+ /* Log out */
+ snprintf(buf, sizeof buf, "QUIT");
+ lprintf(CTDL_DEBUG, "<%s\n", buf);
+ if (sock_puts(sock, buf) <0) goto bail;
+ if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
+ lprintf(CTDL_DEBUG, ">%s\n", buf);