+/* fetch message from POP server */
+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(MCore.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;
+ }
+ }