+/* fetch message from POP server */
+static int pop_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
+{
+ int ret;
+ void *uidl;
+ char buf[LONG_STRING];
+ char path[_POSIX_PATH_MAX];
+ progress_t bar;
+ pop_data_t *pop_data = (pop_data_t *) ctx->data;
+ POP_CACHE *cache;
+ HEADER *h = ctx->hdrs[msgno];
+
+ /* see if we already have the message in our cache */
+ cache = &pop_data->cache[h->index % POP_CACHE_LEN];
+
+ if (cache->path) {
+ if (cache->index == h->index) {
+ /* yes, so just return a pointer to the message */
+ msg->fp = fopen (cache->path, "r");
+ if (msg->fp)
+ return 0;
+
+ mutt_perror (cache->path);
+ mutt_sleep (2);
+ return -1;
+ }
+ else {
+ /* clear the previous entry */
+ unlink (cache->path);
+ p_delete(&cache->path);
+ }
+ }
+
+ for (;;) {
+ if (pop_reconnect (ctx) != PQ_OK)
+ return -1;
+
+ /* verify that massage index is correct */
+ if (h->refno < 0) {
+ mutt_error
+ _("The message index is incorrect. Try reopening the mailbox.");
+ mutt_sleep (2);
+ return -1;
+ }
+
+ bar.size = h->content->length + h->content->offset - 1;
+ bar.msg = _("Fetching message...");
+ mutt_progress_bar (&bar, 0);
+
+ msg->fp = m_tempfile(path, sizeof(path), NONULL(mod_core.tmpdir), NULL);
+ if (!msg->fp) {
+ mutt_error(_("Could not create temporary file"));
+ mutt_sleep(2);
+ return -1;
+ }
+
+ snprintf(buf, sizeof(buf), "RETR %d\r\n", h->refno);
+ ret = pop_fetch_data(pop_data, buf, &bar, fetch_message, msg->fp);
+ if (ret == PQ_OK)
+ break;
+
+ m_fclose(&msg->fp);
+ unlink (path);
+
+ if (ret == PQ_ERR) {
+ mutt_error ("%s", pop_data->err_msg);
+ mutt_sleep (2);
+ return -1;
+ }
+
+ if (ret == PFD_FUNCT_ERROR) {
+ mutt_error _("Can't write message to temporary file!");
+
+ mutt_sleep (2);
+ return -1;
+ }
+ }
+
+ /* Update the header information. Previously, we only downloaded a
+ * portion of the headers, those required for the main display.
+ */
+ cache->index = h->index;
+ cache->path = m_strdup(path);
+ rewind (msg->fp);
+ uidl = h->data;
+ envelope_delete(&h->env);
+ h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
+ h->data = uidl;
+ h->lines = 0;
+ fgets (buf, sizeof (buf), msg->fp);
+ while (!feof (msg->fp)) {
+ ctx->hdrs[msgno]->lines++;
+ fgets (buf, sizeof (buf), msg->fp);
+ }
+
+ h->content->length = ftello (msg->fp) - h->content->offset;
+
+ /* This needs to be done in case this is a multipart message */
+ h->security = crypt_query (h->content);
+
+ mutt_clear_error ();
+ rewind (msg->fp);
+
+ return 0;
+}
+