2 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
3 * Copyright (C) 1999-2002 Brendan Cully <brendan@kublai.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 /* message parsing/updating functions */
30 #include "mutt_curses.h"
31 #include "imap_private.h"
45 static void flush_buffer(char* buf, size_t* len, CONNECTION* conn);
46 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf,
48 static int msg_has_flag (LIST* flag_list, const char* flag);
49 static int msg_parse_fetch (IMAP_HEADER* h, char* s);
50 static char* msg_parse_flags (IMAP_HEADER* h, char* s);
53 static int msg_fetch_header_fetch (CONTEXT* ctx, IMAP_HEADER* h, char* buf,
55 static size_t imap_hcache_keylen (const char *fn);
56 #endif /* USE_HCACHE */
59 * Changed to read many headers instead of just one. It will return the
60 * msgno of the last message read. It will return a value other than
61 * msgend if mail comes in while downloading headers (in theory).
63 int imap_read_headers (IMAP_DATA* idata, int msgbegin, int msgend)
66 char buf[LONG_STRING];
69 char tempfile[_POSIX_PATH_MAX];
72 int rc, mfhrc, oldmsgcount;
74 const char *want_headers = "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO LINES LIST-POST X-LABEL";
78 uint64_t *uid_validity = NULL;
80 #endif /* USE_HCACHE */
85 hc = mutt_hcache_open (HeaderCache, ctx->path);
86 #endif /* USE_HCACHE */
88 if (mutt_bit_isset (idata->capabilities,IMAP4REV1))
90 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s%s%s)]",
91 want_headers, ImapHeaders ? " " : "", ImapHeaders ? ImapHeaders : "");
93 else if (mutt_bit_isset (idata->capabilities,IMAP4))
95 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s%s%s)",
96 want_headers, ImapHeaders ? " " : "", ImapHeaders ? ImapHeaders : "");
99 { /* Unable to fetch headers for lower versions */
100 mutt_error _("Unable to fetch headers from this IMAP server version.");
101 mutt_sleep (2); /* pause a moment to let the user see the error */
103 mutt_hcache_close (hc);
104 #endif /* USE_HCACHE */
108 /* instead of downloading all headers and then parsing them, we parse them
109 * as they come in. */
110 mutt_mktemp (tempfile);
111 if (!(fp = safe_fopen (tempfile, "w+")))
113 mutt_error (_("Could not create temporary file %s"), tempfile);
116 mutt_hcache_close (hc);
117 #endif /* USE_HCACHE */
122 /* make sure context has room to hold the mailbox */
123 while ((msgend) >= idata->ctx->hdrmax)
124 mx_alloc_memory (idata->ctx);
126 oldmsgcount = ctx->msgcount;
127 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
128 idata->newMailCount = 0;
131 snprintf (buf, sizeof (buf),
132 "FETCH %d:%d (UID FLAGS)", msgbegin + 1, msgend + 1);
133 fetchlast = msgend + 1;
135 imap_cmd_start (idata, buf);
137 for (msgno = msgbegin; msgno <= msgend ; msgno++)
139 if (ReadInc && (!msgno || ((msgno+1) % ReadInc == 0)))
140 mutt_message (_("Evaluating cache... [%d/%d]"), msgno + 1,
144 memset (&h, 0, sizeof (h));
145 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
150 rc = imap_cmd_step (idata);
151 if (rc != IMAP_CMD_CONTINUE)
154 if ((mfhrc = msg_fetch_header_fetch (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
159 /* make sure we don't get remnants from older larger message headers */
162 sprintf(uid_buf, "/%u", h.data->uid); /* XXX --tg 21:41 04-07-11 */
163 uid_validity = (uint64_t *) mutt_hcache_fetch (hc, uid_buf, &imap_hcache_keylen);
165 if (uid_validity != NULL
166 && *uid_validity == idata->uid_validity) {
167 ctx->hdrs[msgno] = mutt_hcache_restore((unsigned char *) uid_validity, 0);
168 ctx->hdrs[msgno]->index = h.sid - 1;
169 if (h.sid != ctx->msgcount + 1)
170 dprint (1, (debugfile, "imap_read_headers: msgcount and sequence ID are inconsistent!"));
171 /* messages which have not been expunged are ACTIVE (borrowed from mh
173 ctx->hdrs[msgno]->active = 1;
174 ctx->hdrs[msgno]->read = h.read;
175 ctx->hdrs[msgno]->old = h.old;
176 ctx->hdrs[msgno]->deleted = h.deleted;
177 ctx->hdrs[msgno]->flagged = h.flagged;
178 ctx->hdrs[msgno]->replied = h.replied;
179 ctx->hdrs[msgno]->changed = h.changed;
180 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
181 ctx->hdrs[msgno]->data = (void *) (h.data);
190 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
191 ((msgno + 1) >= fetchlast)));
193 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
195 imap_free_header_data ((void**) &h.data);
197 mutt_hcache_close (hc);
202 fetchlast = msgbegin;
203 #endif /* USE_HCACHE */
205 for (msgno = msgbegin; msgno <= msgend ; msgno++)
207 if (ReadInc && (!msgno || ((msgno+1) % ReadInc == 0)))
208 mutt_message (_("Fetching message headers... [%d/%d]"), msgno + 1,
211 if (ctx->hdrs[msgno])
214 if (msgno + 1 > fetchlast)
216 fetchlast = msgno + 1;
217 while((fetchlast <= msgend) && (! ctx->hdrs[fetchlast]))
221 * Make one request for everything. This makes fetching headers an
222 * order of magnitude faster if you have a large mailbox.
224 * If we get more messages while doing this, we make another
225 * request for all the new messages.
227 snprintf (buf, sizeof (buf),
228 "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)", msgno + 1,
231 imap_cmd_start (idata, buf);
236 memset (&h, 0, sizeof (h));
237 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
239 /* this DO loop does two things:
240 * 1. handles untagged messages, so we can try again on the same msg
241 * 2. fetches the tagged response at the end of the last message.
247 rc = imap_cmd_step (idata);
248 if (rc != IMAP_CMD_CONTINUE)
251 if ((mfhrc = msg_fetch_header (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
256 /* make sure we don't get remnants from older larger message headers */
259 /* update context with message header */
260 ctx->hdrs[msgno] = mutt_new_header ();
262 ctx->hdrs[msgno]->index = h.sid - 1;
263 if (h.sid != ctx->msgcount + 1)
264 dprint (1, (debugfile, "imap_read_headers: msgcount and sequence ID are inconsistent!"));
265 /* messages which have not been expunged are ACTIVE (borrowed from mh
267 ctx->hdrs[msgno]->active = 1;
268 ctx->hdrs[msgno]->read = h.read;
269 ctx->hdrs[msgno]->old = h.old;
270 ctx->hdrs[msgno]->deleted = h.deleted;
271 ctx->hdrs[msgno]->flagged = h.flagged;
272 ctx->hdrs[msgno]->replied = h.replied;
273 ctx->hdrs[msgno]->changed = h.changed;
274 ctx->hdrs[msgno]->received = h.received;
275 ctx->hdrs[msgno]->data = (void *) (h.data);
278 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
279 * on h.received being set */
280 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno],
282 /* content built as a side-effect of mutt_read_rfc822_header */
283 ctx->hdrs[msgno]->content->length = h.content_length;
286 sprintf(uid_buf, "/%u", h.data->uid);
287 mutt_hcache_store(hc, uid_buf, ctx->hdrs[msgno], idata->uid_validity, &imap_hcache_keylen);
288 #endif /* USE_HCACHE */
292 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
293 ((msgno + 1) >= fetchlast)));
295 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
297 imap_free_header_data ((void**) &h.data);
300 mutt_hcache_close (hc);
301 #endif /* USE_HCACHE */
305 /* in case we get new mail while fetching the headers */
306 if (idata->reopen & IMAP_NEWMAIL_PENDING)
308 msgend = idata->newMailCount - 1;
309 while ((msgend) >= ctx->hdrmax)
310 mx_alloc_memory (ctx);
311 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
312 idata->newMailCount = 0;
317 mutt_hcache_close (hc);
318 #endif /* USE_HCACHE */
322 if (ctx->msgcount > oldmsgcount)
323 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
328 int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
332 char buf[LONG_STRING];
333 char path[_POSIX_PATH_MAX];
341 /* Sam's weird courier server returns an OK response even when FETCH
342 * fails. Thanks Sam. */
345 idata = (IMAP_DATA*) ctx->data;
346 h = ctx->hdrs[msgno];
348 /* see if we already have the message in our cache */
349 cacheno = HEADER_DATA(h)->uid % IMAP_CACHE_LEN;
350 cache = &idata->cache[cacheno];
354 if (cache->uid == HEADER_DATA(h)->uid)
356 /* yes, so just return a pointer to the message */
357 if (!(msg->fp = fopen (cache->path, "r")))
359 mutt_perror (cache->path);
366 /* clear the previous entry */
367 unlink (cache->path);
373 mutt_message _("Fetching message...");
375 cache->uid = HEADER_DATA(h)->uid;
377 cache->path = safe_strdup (path);
378 if (!(msg->fp = safe_fopen (path, "w+")))
384 /* mark this header as currently inactive so the command handler won't
385 * also try to update it. HACK until all this code can be moved into the
389 snprintf (buf, sizeof (buf), "UID FETCH %u %s", HEADER_DATA(h)->uid,
390 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
391 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") :
394 imap_cmd_start (idata, buf);
397 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
401 pc = imap_next_word (pc);
402 pc = imap_next_word (pc);
404 if (!ascii_strncasecmp ("FETCH", pc, 5))
408 pc = imap_next_word (pc);
411 if (ascii_strncasecmp ("UID", pc, 3) == 0)
413 pc = imap_next_word (pc);
415 if (uid != HEADER_DATA(h)->uid)
416 mutt_error (_("The message index is incorrect. Try reopening the mailbox."));
418 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
419 (ascii_strncasecmp ("BODY[]", pc, 6) == 0))
421 pc = imap_next_word (pc);
422 if (imap_get_literal_count(pc, &bytes) < 0)
424 imap_error ("imap_fetch_message()", buf);
427 if (imap_read_literal (msg->fp, idata, bytes) < 0)
429 /* pick up trailing line */
430 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
436 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
437 * change (eg from \Unseen to \Seen).
438 * Uncommitted changes in mutt take precedence. If we decide to
439 * incrementally update flags later, this won't stop us syncing */
440 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed)
442 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
448 while (rc == IMAP_CMD_CONTINUE);
450 /* see comment before command start. */
453 if (rc != IMAP_CMD_OK)
456 if (!fetched || !imap_code (idata->cmd.buf))
459 /* Update the header information. Previously, we only downloaded a
460 * portion of the headers, those required for the main display.
463 /* It may be that the Status header indicates a message is read, but the
464 * IMAP server doesn't know the message has been \Seen. So we capture
465 * the server's notion of 'read' and if it differs from the message info
466 * picked up in mutt_read_rfc822_header, we mark the message (and context
467 * changed). Another possiblity: ignore Status on IMAP?*/
469 /* I hate do this here, since it's so low-level, but I'm not sure where
470 * I can abstract it. Problem: the id and subj hashes lose their keys when
471 * mutt_free_envelope gets called, but keep their spots in the hash. This
472 * confuses threading. Alternatively we could try to merge the new
473 * envelope into the old one. Also messy and lowlevel. */
474 if (ctx->id_hash && h->env->message_id)
475 hash_delete (ctx->id_hash, h->env->message_id, h, NULL);
476 if (ctx->subj_hash && h->env->real_subj)
477 hash_delete (ctx->subj_hash, h->env->real_subj, h, NULL);
478 mutt_free_envelope (&h->env);
479 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
480 if (ctx->id_hash && h->env->message_id)
481 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
482 if (ctx->subj_hash && h->env->real_subj)
483 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
485 /* see above. We want the new status in h->read, so we unset it manually
486 * and let mutt_set_flag set it correctly, updating context. */
490 mutt_set_flag (ctx, h, M_NEW, read);
494 fgets (buf, sizeof (buf), msg->fp);
495 while (!feof (msg->fp))
498 fgets (buf, sizeof (buf), msg->fp);
501 h->content->length = ftell (msg->fp) - h->content->offset;
503 /* This needs to be done in case this is a multipart message */
504 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
505 h->security = crypt_query (h->content);
514 safe_fclose (&msg->fp);
517 unlink (cache->path);
524 int imap_append_message (CONTEXT *ctx, MESSAGE *msg)
528 char buf[LONG_STRING];
529 char mbox[LONG_STRING];
530 char mailbox[LONG_STRING];
536 idata = (IMAP_DATA*) ctx->data;
538 if (imap_parse_path (ctx->path, &mx))
541 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
543 if ((fp = fopen (msg->path, "r")) == NULL)
545 mutt_perror (msg->path);
549 /* currently we set the \Seen flag on all messages, but probably we
550 * should scan the message Status header for flag info. Since we're
551 * already rereading the whole file for length it isn't any more
552 * expensive (it'd be nice if we had the file size passed in already
553 * by the code that writes the file, but that's a lot of changes.
554 * Ideally we'd have a HEADER structure with flag info here... */
555 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
557 if(c == '\n' && last != '\r')
564 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
565 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) {%lu}", mbox,
566 msg->flags.read ? "\\Seen" : "",
567 msg->flags.read && (msg->flags.replied || msg->flags.flagged) ? " " : "",
568 msg->flags.replied ? "\\Answered" : "",
569 msg->flags.replied && msg->flags.flagged ? " " : "",
570 msg->flags.flagged ? "\\Flagged" : "",
571 (unsigned long) len);
573 imap_cmd_start (idata, buf);
576 rc = imap_cmd_step (idata);
577 while (rc == IMAP_CMD_CONTINUE);
579 if (rc != IMAP_CMD_RESPOND)
583 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
586 pc = idata->cmd.buf + SEQLEN;
588 pc = imap_next_word (pc);
589 mutt_error ("%s", pc);
595 mutt_message _("Uploading message ...");
597 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
599 if (c == '\n' && last != '\r')
604 if (len > sizeof(buf) - 3)
605 flush_buffer(buf, &len, idata->conn);
609 flush_buffer(buf, &len, idata->conn);
611 mutt_socket_write (idata->conn, "\r\n");
615 rc = imap_cmd_step (idata);
616 while (rc == IMAP_CMD_CONTINUE);
618 if (!imap_code (idata->cmd.buf))
622 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
624 pc = idata->cmd.buf + SEQLEN;
626 pc = imap_next_word (pc);
627 mutt_error ("%s", pc);
640 /* imap_copy_messages: use server COPY command to copy messages to another
645 * 1: non-fatal error - try fetch/append */
646 int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
651 char mbox[LONG_STRING];
652 char mmbox[LONG_STRING];
657 idata = (IMAP_DATA*) ctx->data;
659 if (imap_parse_path (dest, &mx))
661 dprint (1, (debugfile, "imap_copy_messages: bad destination %s\n", dest));
665 /* check that the save-to folder is in the same account */
666 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account)))
668 dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n",
673 if (h && h->attach_del)
675 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
679 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
681 memset (&cmd, 0, sizeof (cmd));
682 mutt_buffer_addstr (&cmd, "UID COPY ");
684 /* Null HEADER* means copy tagged messages */
687 /* if any messages have attachments to delete, fall through to FETCH
688 * and APPEND. TODO: Copy what we can with COPY, fall through for the
690 for (n = 0; n < ctx->msgcount; n++)
692 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
694 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
699 rc = imap_make_msg_set (idata, &cmd, M_TAG, 0);
702 dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
705 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
709 mutt_message (_("Copying message %d to %s..."), h->index+1, mbox);
710 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
711 mutt_buffer_addstr (&cmd, uid);
714 /* let's get it on */
715 mutt_buffer_addstr (&cmd, " ");
716 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
717 mutt_buffer_addstr (&cmd, mmbox);
719 rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
722 /* bail out if command failed for reasons other than nonexistent target */
723 if (ascii_strncasecmp (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11))
725 imap_error ("imap_copy_messages", idata->cmd.buf);
728 dprint (2, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
729 snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
730 if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1)
735 if (imap_create_mailbox (idata, mbox) < 0)
739 rc = imap_exec (idata, cmd.data, 0);
743 imap_error ("imap_copy_messages", idata->cmd.buf);
751 for (n = 0; n < ctx->msgcount; n++)
753 if (ctx->hdrs[n]->tagged)
755 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
756 mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
757 if (option (OPTDELETEUNTAG))
758 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
763 mutt_set_flag (ctx, h, M_DELETE, 1);
764 mutt_set_flag (ctx, h, M_APPENDED, 1);
765 if (option (OPTDELETEUNTAG))
766 mutt_set_flag (ctx, h, M_TAG, 0);
782 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
783 * appear in the folder flags list. Why wouldn't they? */
784 void imap_add_keywords (char* s, HEADER* h, LIST* mailbox_flags, size_t slen)
788 if (!mailbox_flags || !HEADER_DATA(h) || !HEADER_DATA(h)->keywords)
791 keywords = HEADER_DATA(h)->keywords->next;
795 if (msg_has_flag (mailbox_flags, keywords->data))
797 safe_strcat (s, slen, keywords->data);
798 safe_strcat (s, slen, " ");
800 keywords = keywords->next;
804 /* imap_free_header_data: free IMAP_HEADER structure */
805 void imap_free_header_data (void** data)
807 /* this should be safe even if the list wasn't used */
808 mutt_free_list (&(((IMAP_HEADER_DATA*) *data)->keywords));
813 /* imap_set_flags: fill out the message header according to the flags from
814 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
815 char* imap_set_flags (IMAP_DATA* idata, HEADER* h, char* s)
817 CONTEXT* ctx = idata->ctx;
819 unsigned char readonly;
821 memset (&newh, 0, sizeof (newh));
822 newh.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
824 dprint (2, (debugfile, "imap_fetch_message: parsing FLAGS\n"));
825 if ((s = msg_parse_flags (&newh, s)) == NULL)
831 /* YAUH (yet another ugly hack): temporarily set context to
832 * read-write even if it's read-only, so *server* updates of
833 * flags can be processed by mutt_set_flag. ctx->changed must
834 * be restored afterwards */
835 readonly = ctx->readonly;
838 mutt_set_flag (ctx, h, M_NEW, !(newh.read || newh.old));
839 mutt_set_flag (ctx, h, M_OLD, newh.old);
840 mutt_set_flag (ctx, h, M_READ, newh.read);
841 mutt_set_flag (ctx, h, M_DELETE, newh.deleted);
842 mutt_set_flag (ctx, h, M_FLAG, newh.flagged);
843 mutt_set_flag (ctx, h, M_REPLIED, newh.replied);
845 /* this message is now definitively *not* changed (mutt_set_flag
846 * marks things changed as a side-effect) */
848 ctx->changed &= ~readonly;
849 ctx->readonly = readonly;
851 mutt_free_list (&(HEADER_DATA(h)->keywords));
852 HEADER_DATA(h)->keywords = newh.data->keywords;
859 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
860 * Expects string beginning with * n FETCH.
863 * -1 if the string is not a fetch response
864 * -2 if the string is a corrupt fetch response */
865 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf, FILE* fp)
869 int rc = -1; /* default now is that string isn't FETCH response*/
871 idata = (IMAP_DATA*) ctx->data;
876 /* skip to message number */
877 buf = imap_next_word (buf);
881 buf = imap_next_word (buf);
882 if (ascii_strncasecmp ("FETCH", buf, 5))
885 rc = -2; /* we've got a FETCH response, for better or worse */
886 if (!(buf = strchr (buf, '(')))
890 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
891 * read header lines and call it again. Silly. */
892 if (msg_parse_fetch (h, buf) != -2)
895 if (imap_get_literal_count (buf, &bytes) == 0)
897 imap_read_literal (fp, idata, bytes);
899 /* we may have other fields of the FETCH _after_ the literal
900 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
901 * This all has to go - we should accept literals and nonliterals
902 * interchangeably at any time. */
903 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
906 if (msg_parse_fetch (h, idata->cmd.buf) == -1)
910 rc = 0; /* success */
912 /* subtract headers from message size - unfortunately only the subset of
913 * headers we've requested. */
914 h->content_length -= bytes;
920 static size_t imap_hcache_keylen (const char *fn)
922 return mutt_strlen(fn);
925 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
926 * Expects string beginning with * n FETCH.
929 * -1 if the string is not a fetch response
930 * -2 if the string is a corrupt fetch response */
931 static int msg_fetch_header_fetch (CONTEXT* ctx, IMAP_HEADER* h, char* buf, FILE* fp)
934 int rc = -1; /* default now is that string isn't FETCH response*/
936 idata = (IMAP_DATA*) ctx->data;
941 /* skip to message number */
942 buf = imap_next_word (buf);
946 buf = imap_next_word (buf);
947 if (ascii_strncasecmp ("FETCH", buf, 5))
950 rc = -2; /* we've got a FETCH response, for better or worse */
951 if (!(buf = strchr (buf, '(')))
955 if (msg_parse_fetch (h, buf) < 0) {
959 if (!(buf = strchr (buf, ')')))
965 #endif /* USE_HCACHE */
968 /* msg_has_flag: do a caseless comparison of the flag against a flag list,
969 * return 1 if found or flag list has '\*', 0 otherwise */
970 static int msg_has_flag (LIST* flag_list, const char* flag)
975 flag_list = flag_list->next;
978 if (!ascii_strncasecmp (flag_list->data, flag, strlen (flag_list->data)))
981 flag_list = flag_list->next;
987 /* msg_parse_fetch: handle headers returned from header fetch */
988 static int msg_parse_fetch (IMAP_HEADER *h, char *s)
990 char tmp[SHORT_STRING];
1000 if (ascii_strncasecmp ("FLAGS", s, 5) == 0)
1002 if ((s = msg_parse_flags (h, s)) == NULL)
1005 else if (ascii_strncasecmp ("UID", s, 3) == 0)
1009 h->data->uid = (unsigned int) atoi (s);
1011 s = imap_next_word (s);
1013 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0)
1019 dprint (1, (debugfile, "msg_parse_fetch(): bogus INTERNALDATE entry: %s\n", s));
1024 while (*s && *s != '\"')
1028 s++; /* skip past the trailing " */
1030 h->received = imap_parse_date (tmp);
1032 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0)
1037 while (isdigit ((unsigned char) *s))
1040 h->content_length = atoi (tmp);
1042 else if (!ascii_strncasecmp ("BODY", s, 4) ||
1043 !ascii_strncasecmp ("RFC822.HEADER", s, 13))
1045 /* handle above, in msg_fetch_header */
1049 s++; /* end of request */
1052 /* got something i don't understand */
1053 imap_error ("msg_parse_fetch", s);
1061 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1062 static char* msg_parse_flags (IMAP_HEADER* h, char* s)
1066 /* sanity-check string */
1067 if (ascii_strncasecmp ("FLAGS", s, 5) != 0)
1069 dprint (1, (debugfile, "msg_parse_flags: not a FLAGS response: %s\n",
1077 dprint (1, (debugfile, "msg_parse_flags: bogus FLAGS response: %s\n",
1084 while (*s && *s != ')')
1086 if (ascii_strncasecmp ("\\deleted", s, 8) == 0)
1091 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0)
1096 else if (ascii_strncasecmp ("\\answered", s, 9) == 0)
1101 else if (ascii_strncasecmp ("\\seen", s, 5) == 0)
1106 else if (ascii_strncasecmp ("\\recent", s, 5) == 0)
1113 /* store custom flags as well */
1115 char* flag_word = s;
1117 if (!h->data->keywords)
1118 h->data->keywords = mutt_new_list ();
1120 while (*s && !ISSPACE (*s) && *s != ')')
1124 mutt_add_list (h->data->keywords, flag_word);
1130 /* wrap up, or note bad flags response */
1133 /* if a message is neither seen nor recent, it is OLD. */
1134 if (option (OPTMARKOLD) && !recent && !(h->read))
1140 dprint (1, (debugfile,
1141 "msg_parse_flags: Unterminated FLAGS response: %s\n", s));
1148 static void flush_buffer(char *buf, size_t *len, CONNECTION *conn)
1151 mutt_socket_write(conn, buf);