2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
4 * Copyright (C) 1999-2002 Brendan Cully <brendan@kublai.com>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
11 /* message parsing/updating functions */
21 #include "mutt_curses.h"
22 #include "imap_private.h"
33 #include "lib/debug.h"
41 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn);
42 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
44 static int msg_has_flag (LIST * flag_list, const char *flag);
45 static int msg_parse_fetch (IMAP_HEADER * h, char *s);
46 static char *msg_parse_flags (IMAP_HEADER * h, char *s);
49 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
51 static size_t imap_hcache_keylen (const char *fn);
52 #endif /* USE_HCACHE */
55 * Changed to read many headers instead of just one. It will return the
56 * msgno of the last message read. It will return a value other than
57 * msgend if mail comes in while downloading headers (in theory).
59 int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend)
62 char buf[LONG_STRING];
65 char tempfile[_POSIX_PATH_MAX];
68 int rc, mfhrc, oldmsgcount;
70 const char *want_headers =
71 "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO LINES LIST-POST X-LABEL";
75 unsigned long *uid_validity = NULL;
77 #endif /* USE_HCACHE */
81 if (mutt_bit_isset (idata->capabilities, IMAP4REV1)) {
82 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s%s%s)]",
83 want_headers, ImapHeaders ? " " : "",
84 ImapHeaders ? ImapHeaders : "");
86 else if (mutt_bit_isset (idata->capabilities, IMAP4)) {
87 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s%s%s)",
88 want_headers, ImapHeaders ? " " : "",
89 ImapHeaders ? ImapHeaders : "");
91 else { /* Unable to fetch headers for lower versions */
92 mutt_error _("Unable to fetch headers from this IMAP server version.");
94 mutt_sleep (2); /* pause a moment to let the user see the error */
98 /* instead of downloading all headers and then parsing them, we parse them
100 mutt_mktemp (tempfile);
101 if (!(fp = safe_fopen (tempfile, "w+"))) {
102 mutt_error (_("Could not create temporary file %s"), tempfile);
108 /* make sure context has room to hold the mailbox */
109 while ((msgend) >= idata->ctx->hdrmax)
110 mx_alloc_memory (idata->ctx);
112 oldmsgcount = ctx->msgcount;
113 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
114 idata->newMailCount = 0;
117 hc = mutt_hcache_open (HeaderCache, ctx->path);
119 snprintf (buf, sizeof (buf),
120 "FETCH %d:%d (UID FLAGS)", msgbegin + 1, msgend + 1);
121 fetchlast = msgend + 1;
123 imap_cmd_start (idata, buf);
125 for (msgno = msgbegin; msgno <= msgend; msgno++) {
126 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
127 mutt_message (_("Evaluating cache... [%d/%d]"), msgno + 1, msgend + 1);
130 memset (&h, 0, sizeof (h));
131 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
135 rc = imap_cmd_step (idata);
136 if (rc != IMAP_CMD_CONTINUE)
140 msg_fetch_header_fetch (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
145 /* make sure we don't get remnants from older larger message headers */
148 sprintf (uid_buf, "/%u", h.data->uid); /* XXX --tg 21:41 04-07-11 */
150 (unsigned long *) mutt_hcache_fetch (hc, uid_buf, &imap_hcache_keylen);
152 if (uid_validity != NULL && *uid_validity == idata->uid_validity) {
153 ctx->hdrs[msgno] = mutt_hcache_restore((unsigned char *) uid_validity, 0);
154 ctx->hdrs[msgno]->index = h.sid - 1;
155 if (h.sid != ctx->msgcount + 1)
156 debug_print (1, ("imap_read_headers: msgcount and sequence ID are inconsistent!"));
157 /* messages which have not been expunged are ACTIVE (borrowed from mh
159 ctx->hdrs[msgno]->active = 1;
160 ctx->hdrs[msgno]->read = h.read;
161 ctx->hdrs[msgno]->old = h.old;
162 ctx->hdrs[msgno]->deleted = h.deleted;
163 ctx->hdrs[msgno]->flagged = h.flagged;
164 ctx->hdrs[msgno]->replied = h.replied;
165 ctx->hdrs[msgno]->changed = h.changed;
166 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
167 ctx->hdrs[msgno]->data = (void *) (h.data);
174 FREE (&uid_validity);
177 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
178 ((msgno + 1) >= fetchlast)));
180 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
181 imap_free_header_data ((void **) &h.data);
183 mutt_hcache_close (hc);
188 fetchlast = msgbegin;
189 #endif /* USE_HCACHE */
191 for (msgno = msgbegin; msgno <= msgend; msgno++) {
192 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
193 mutt_message (_("Fetching message headers... [%d/%d]"), msgno + 1,
196 if (ctx->hdrs[msgno])
199 if (msgno + 1 > fetchlast) {
200 fetchlast = msgno + 1;
201 while ((fetchlast <= msgend) && (!ctx->hdrs[fetchlast]))
205 * Make one request for everything. This makes fetching headers an
206 * order of magnitude faster if you have a large mailbox.
208 * If we get more messages while doing this, we make another
209 * request for all the new messages.
211 snprintf (buf, sizeof (buf),
212 "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)",
213 msgno + 1, fetchlast, hdrreq);
215 imap_cmd_start (idata, buf);
220 memset (&h, 0, sizeof (h));
221 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
223 /* this DO loop does two things:
224 * 1. handles untagged messages, so we can try again on the same msg
225 * 2. fetches the tagged response at the end of the last message.
230 rc = imap_cmd_step (idata);
231 if (rc != IMAP_CMD_CONTINUE)
235 msg_fetch_header (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
240 /* make sure we don't get remnants from older larger message headers */
243 /* update context with message header */
244 ctx->hdrs[msgno] = mutt_new_header ();
246 ctx->hdrs[msgno]->index = h.sid - 1;
247 if (h.sid != ctx->msgcount + 1)
248 debug_print (1, ("msgcount and sequence ID are inconsistent!\n"));
249 /* messages which have not been expunged are ACTIVE (borrowed from mh
251 ctx->hdrs[msgno]->active = 1;
252 ctx->hdrs[msgno]->read = h.read;
253 ctx->hdrs[msgno]->old = h.old;
254 ctx->hdrs[msgno]->deleted = h.deleted;
255 ctx->hdrs[msgno]->flagged = h.flagged;
256 ctx->hdrs[msgno]->replied = h.replied;
257 ctx->hdrs[msgno]->changed = h.changed;
258 ctx->hdrs[msgno]->received = h.received;
259 ctx->hdrs[msgno]->data = (void *) (h.data);
262 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
263 * on h.received being set */
264 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno],
266 /* content built as a side-effect of mutt_read_rfc822_header */
267 ctx->hdrs[msgno]->content->length = h.content_length;
270 sprintf (uid_buf, "/%u", h.data->uid);
271 mutt_hcache_store (hc, uid_buf, ctx->hdrs[msgno], idata->uid_validity,
272 &imap_hcache_keylen);
273 #endif /* USE_HCACHE */
277 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
278 ((msgno + 1) >= fetchlast)));
280 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
281 imap_free_header_data ((void **) &h.data);
284 mutt_hcache_close (hc);
285 #endif /* USE_HCACHE */
289 /* in case we get new mail while fetching the headers */
290 if (idata->reopen & IMAP_NEWMAIL_PENDING) {
291 msgend = idata->newMailCount - 1;
292 while ((msgend) >= ctx->hdrmax)
293 mx_alloc_memory (ctx);
294 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
295 idata->newMailCount = 0;
300 mutt_hcache_close (hc);
301 #endif /* USE_HCACHE */
305 if (ctx->msgcount > oldmsgcount)
306 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
311 int imap_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
316 char buf[LONG_STRING];
317 char path[_POSIX_PATH_MAX];
326 /* Sam's weird courier server returns an OK response even when FETCH
327 * fails. Thanks Sam. */
330 idata = (IMAP_DATA *) ctx->data;
331 h = ctx->hdrs[msgno];
333 /* see if we already have the message in our cache */
334 cacheno = HEADER_DATA (h)->uid % IMAP_CACHE_LEN;
335 cache = &idata->cache[cacheno];
338 if (cache->uid == HEADER_DATA (h)->uid) {
339 /* yes, so just return a pointer to the message */
340 if (!(msg->fp = fopen (cache->path, "r"))) {
341 mutt_perror (cache->path);
347 /* clear the previous entry */
348 unlink (cache->path);
354 mutt_message _("Fetching message...");
356 cache->uid = HEADER_DATA (h)->uid;
358 cache->path = safe_strdup (path);
359 if (!(msg->fp = safe_fopen (path, "w+"))) {
364 /* mark this header as currently inactive so the command handler won't
365 * also try to update it. HACK until all this code can be moved into the
369 snprintf (buf, sizeof (buf), "UID FETCH %u %s", HEADER_DATA (h)->uid,
370 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
371 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") : "RFC822"));
373 imap_cmd_start (idata, buf);
375 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
379 pc = imap_next_word (pc);
380 pc = imap_next_word (pc);
382 if (!ascii_strncasecmp ("FETCH", pc, 5)) {
384 pc = imap_next_word (pc);
387 if (ascii_strncasecmp ("UID", pc, 3) == 0) {
388 pc = imap_next_word (pc);
390 if (uid != HEADER_DATA (h)->uid)
392 ("The message index is incorrect. Try reopening the mailbox."));
394 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
395 (ascii_strncasecmp ("BODY[]", pc, 6) == 0)) {
396 pc = imap_next_word (pc);
397 if (imap_get_literal_count (pc, &bytes) < 0) {
398 imap_error ("imap_fetch_message()", buf);
401 if (imap_read_literal (msg->fp, idata, bytes) < 0)
403 /* pick up trailing line */
404 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
410 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
411 * change (eg from \Unseen to \Seen).
412 * Uncommitted changes in mutt take precedence. If we decide to
413 * incrementally update flags later, this won't stop us syncing */
414 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed) {
415 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
421 while (rc == IMAP_CMD_CONTINUE);
423 /* see comment before command start. */
426 if (rc != IMAP_CMD_OK)
429 if (!fetched || !imap_code (idata->cmd.buf))
432 /* Update the header information. Previously, we only downloaded a
433 * portion of the headers, those required for the main display.
436 /* It may be that the Status header indicates a message is read, but the
437 * IMAP server doesn't know the message has been \Seen. So we capture
438 * the server's notion of 'read' and if it differs from the message info
439 * picked up in mutt_read_rfc822_header, we mark the message (and context
440 * changed). Another possiblity: ignore Status on IMAP?*/
442 newenv = mutt_read_rfc822_header (msg->fp, h, 0, 0);
443 mutt_merge_envelopes(h->env, &newenv);
445 /* see above. We want the new status in h->read, so we unset it manually
446 * and let mutt_set_flag set it correctly, updating context. */
447 if (read != h->read) {
449 mutt_set_flag (ctx, h, M_NEW, read);
453 fgets (buf, sizeof (buf), msg->fp);
454 while (!feof (msg->fp)) {
456 fgets (buf, sizeof (buf), msg->fp);
459 h->content->length = ftell (msg->fp) - h->content->offset;
461 /* This needs to be done in case this is a multipart message */
462 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
463 h->security = crypt_query (h->content);
472 safe_fclose (&msg->fp);
474 unlink (cache->path);
481 int imap_append_message (CONTEXT * ctx, MESSAGE * msg)
485 char buf[LONG_STRING];
486 char mbox[LONG_STRING];
487 char mailbox[LONG_STRING];
493 idata = (IMAP_DATA *) ctx->data;
495 if (imap_parse_path (ctx->path, &mx))
498 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
500 if ((fp = fopen (msg->path, "r")) == NULL) {
501 mutt_perror (msg->path);
505 /* currently we set the \Seen flag on all messages, but probably we
506 * should scan the message Status header for flag info. Since we're
507 * already rereading the whole file for length it isn't any more
508 * expensive (it'd be nice if we had the file size passed in already
509 * by the code that writes the file, but that's a lot of changes.
510 * Ideally we'd have a HEADER structure with flag info here... */
511 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
512 if (c == '\n' && last != '\r')
519 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
520 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) {%lu}", mbox,
521 msg->flags.read ? "\\Seen" : "",
522 msg->flags.read && (msg->flags.replied
523 || msg->flags.flagged) ? " " : "",
524 msg->flags.replied ? "\\Answered" : "", msg->flags.replied
525 && msg->flags.flagged ? " " : "",
526 msg->flags.flagged ? "\\Flagged" : "", (unsigned long) len);
528 imap_cmd_start (idata, buf);
531 rc = imap_cmd_step (idata);
532 while (rc == IMAP_CMD_CONTINUE);
534 if (rc != IMAP_CMD_RESPOND) {
537 debug_print (1, ("command failed: %s\n", idata->cmd.buf));
539 pc = idata->cmd.buf + SEQLEN;
541 pc = imap_next_word (pc);
542 mutt_error ("%s", pc);
548 mutt_message _("Uploading message ...");
550 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
551 if (c == '\n' && last != '\r')
556 if (len > sizeof (buf) - 3)
557 flush_buffer (buf, &len, idata->conn);
561 flush_buffer (buf, &len, idata->conn);
563 mutt_socket_write (idata->conn, "\r\n");
567 rc = imap_cmd_step (idata);
568 while (rc == IMAP_CMD_CONTINUE);
570 if (!imap_code (idata->cmd.buf)) {
573 debug_print (1, ("command failed: %s\n", idata->cmd.buf));
574 pc = idata->cmd.buf + SEQLEN;
576 pc = imap_next_word (pc);
577 mutt_error ("%s", pc);
590 /* imap_copy_messages: use server COPY command to copy messages to another
595 * 1: non-fatal error - try fetch/append */
596 int imap_copy_messages (CONTEXT * ctx, HEADER * h, char *dest, int delete)
599 BUFFER cmd, sync_cmd;
601 char mbox[LONG_STRING];
602 char mmbox[LONG_STRING];
606 int err_continue = M_NO;
608 idata = (IMAP_DATA *) ctx->data;
610 if (imap_parse_path (dest, &mx)) {
611 debug_print (1, ("bad destination %s\n", dest));
615 /* check that the save-to folder is in the same account */
616 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account))) {
617 debug_print (3, ("%s not same server as %s\n", dest, ctx->path));
621 if (h && h->attach_del) {
622 debug_print (3, ("Message contains attachments to be deleted\n"));
626 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
628 memset (&sync_cmd, 0, sizeof (sync_cmd));
629 memset (&cmd, 0, sizeof (cmd));
630 mutt_buffer_addstr (&cmd, "UID COPY ");
632 /* Null HEADER* means copy tagged messages */
634 /* if any messages have attachments to delete, fall through to FETCH
635 * and APPEND. TODO: Copy what we can with COPY, fall through for the
637 for (n = 0; n < ctx->msgcount; n++) {
638 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del) {
639 debug_print (3, ("Message contains attachments to be deleted\n"));
643 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
644 ctx->hdrs[n]->changed)
646 rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
649 debug_print (1, ("could not sync\n"));
655 rc = imap_make_msg_set (idata, &cmd, M_TAG, 0);
657 debug_print (1, ("No messages tagged\n"));
660 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
663 mutt_message (_("Copying message %d to %s..."), h->index + 1, mbox);
664 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
665 mutt_buffer_addstr (&cmd, uid);
667 if (h->active && h->changed)
669 rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
672 debug_print (1, ("could not sync\n"));
678 /* let's get it on */
679 mutt_buffer_addstr (&cmd, " ");
680 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
681 mutt_buffer_addstr (&cmd, mmbox);
683 rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
685 /* bail out if command failed for reasons other than nonexistent target */
686 if (ascii_strncasecmp
687 (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11)) {
688 imap_error ("imap_copy_messages", idata->cmd.buf);
691 debug_print (2, ("server suggests TRYCREATE\n"));
692 snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
693 if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1) {
697 if (imap_create_mailbox (idata, mbox) < 0)
701 rc = imap_exec (idata, cmd.data, 0);
704 imap_error ("imap_copy_messages", idata->cmd.buf);
711 for (n = 0; n < ctx->msgcount; n++) {
712 if (ctx->hdrs[n]->tagged) {
713 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
714 mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
715 if (option (OPTDELETEUNTAG))
716 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
720 mutt_set_flag (ctx, h, M_DELETE, 1);
721 mutt_set_flag (ctx, h, M_APPENDED, 1);
722 if (option (OPTDELETEUNTAG))
723 mutt_set_flag (ctx, h, M_TAG, 0);
730 FREE (&sync_cmd.data);
738 FREE (&sync_cmd.data);
743 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
744 * appear in the folder flags list. Why wouldn't they? */
745 void imap_add_keywords (char *s, HEADER * h, LIST * mailbox_flags,
750 if (!mailbox_flags || !HEADER_DATA (h) || !HEADER_DATA (h)->keywords)
753 keywords = HEADER_DATA (h)->keywords->next;
756 if (msg_has_flag (mailbox_flags, keywords->data)) {
757 safe_strcat (s, slen, keywords->data);
758 safe_strcat (s, slen, " ");
760 keywords = keywords->next;
764 /* imap_free_header_data: free IMAP_HEADER structure */
765 void imap_free_header_data (void **data)
767 /* this should be safe even if the list wasn't used */
768 mutt_free_list (&(((IMAP_HEADER_DATA *) * data)->keywords));
773 /* imap_set_flags: fill out the message header according to the flags from
774 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
775 char *imap_set_flags (IMAP_DATA * idata, HEADER * h, char *s)
777 CONTEXT *ctx = idata->ctx;
779 unsigned char readonly;
781 memset (&newh, 0, sizeof (newh));
782 newh.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
784 debug_print (2, ("parsing FLAGS\n"));
785 if ((s = msg_parse_flags (&newh, s)) == NULL) {
790 /* YAUH (yet another ugly hack): temporarily set context to
791 * read-write even if it's read-only, so *server* updates of
792 * flags can be processed by mutt_set_flag. ctx->changed must
793 * be restored afterwards */
794 readonly = ctx->readonly;
797 mutt_set_flag (ctx, h, M_NEW, !(newh.read || newh.old));
798 mutt_set_flag (ctx, h, M_OLD, newh.old);
799 mutt_set_flag (ctx, h, M_READ, newh.read);
800 mutt_set_flag (ctx, h, M_DELETE, newh.deleted);
801 mutt_set_flag (ctx, h, M_FLAG, newh.flagged);
802 mutt_set_flag (ctx, h, M_REPLIED, newh.replied);
804 /* this message is now definitively *not* changed (mutt_set_flag
805 * marks things changed as a side-effect) */
807 ctx->changed &= ~readonly;
808 ctx->readonly = readonly;
810 mutt_free_list (&(HEADER_DATA (h)->keywords));
811 HEADER_DATA (h)->keywords = newh.data->keywords;
818 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
819 * Expects string beginning with * n FETCH.
822 * -1 if the string is not a fetch response
823 * -2 if the string is a corrupt fetch response */
824 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
829 int rc = -1; /* default now is that string isn't FETCH response */
831 idata = (IMAP_DATA *) ctx->data;
836 /* skip to message number */
837 buf = imap_next_word (buf);
841 buf = imap_next_word (buf);
842 if (ascii_strncasecmp ("FETCH", buf, 5))
845 rc = -2; /* we've got a FETCH response, for better or worse */
846 if (!(buf = strchr (buf, '(')))
850 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
851 * read header lines and call it again. Silly. */
852 if (msg_parse_fetch (h, buf) != -2)
855 if (imap_get_literal_count (buf, &bytes) == 0) {
856 imap_read_literal (fp, idata, bytes);
858 /* we may have other fields of the FETCH _after_ the literal
859 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
860 * This all has to go - we should accept literals and nonliterals
861 * interchangeably at any time. */
862 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
865 if (msg_parse_fetch (h, idata->cmd.buf) == -1)
869 rc = 0; /* success */
871 /* subtract headers from message size - unfortunately only the subset of
872 * headers we've requested. */
873 h->content_length -= bytes;
879 static size_t imap_hcache_keylen (const char *fn)
881 return mutt_strlen (fn);
884 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
885 * Expects string beginning with * n FETCH.
888 * -1 if the string is not a fetch response
889 * -2 if the string is a corrupt fetch response */
890 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
894 int rc = -1; /* default now is that string isn't FETCH response */
896 idata = (IMAP_DATA *) ctx->data;
901 /* skip to message number */
902 buf = imap_next_word (buf);
906 buf = imap_next_word (buf);
907 if (ascii_strncasecmp ("FETCH", buf, 5))
910 rc = -2; /* we've got a FETCH response, for better or worse */
911 if (!(buf = strchr (buf, '(')))
915 if (msg_parse_fetch (h, buf) < 0) {
919 if (!(buf = strchr (buf, ')')))
925 #endif /* USE_HCACHE */
928 /* msg_has_flag: do a caseless comparison of the flag against a flag list,
929 * return 1 if found or flag list has '\*', 0 otherwise */
930 static int msg_has_flag (LIST * flag_list, const char *flag)
935 flag_list = flag_list->next;
937 if (!ascii_strncasecmp (flag_list->data, flag, mutt_strlen (flag_list->data)))
940 flag_list = flag_list->next;
946 /* msg_parse_fetch: handle headers returned from header fetch */
947 static int msg_parse_fetch (IMAP_HEADER * h, char *s)
949 char tmp[SHORT_STRING];
958 if (ascii_strncasecmp ("FLAGS", s, 5) == 0) {
959 if ((s = msg_parse_flags (h, s)) == NULL)
962 else if (ascii_strncasecmp ("UID", s, 3) == 0) {
965 h->data->uid = (unsigned int) atoi (s);
967 s = imap_next_word (s);
969 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0) {
973 debug_print (1, ("bogus INTERNALDATE entry: %s\n", s));
978 while (*s && *s != '\"')
982 s++; /* skip past the trailing " */
984 h->received = imap_parse_date (tmp);
986 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0) {
990 while (isdigit ((unsigned char) *s))
993 h->content_length = atoi (tmp);
995 else if (!ascii_strncasecmp ("BODY", s, 4) ||
996 !ascii_strncasecmp ("RFC822.HEADER", s, 13)) {
997 /* handle above, in msg_fetch_header */
1001 s++; /* end of request */
1003 /* got something i don't understand */
1004 imap_error ("msg_parse_fetch", s);
1012 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1013 static char *msg_parse_flags (IMAP_HEADER * h, char *s)
1017 /* sanity-check string */
1018 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
1019 debug_print (1, ("not a FLAGS response: %s\n", s));
1025 debug_print (1, ("bogus FLAGS response: %s\n", s));
1031 while (*s && *s != ')') {
1032 if (ascii_strncasecmp ("\\deleted", s, 8) == 0) {
1036 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0) {
1040 else if (ascii_strncasecmp ("\\answered", s, 9) == 0) {
1044 else if (ascii_strncasecmp ("\\seen", s, 5) == 0) {
1048 else if (ascii_strncasecmp ("\\recent", s, 5) == 0) {
1053 /* store custom flags as well */
1055 char *flag_word = s;
1057 if (!h->data->keywords)
1058 h->data->keywords = mutt_new_list ();
1060 while (*s && !ISSPACE (*s) && *s != ')')
1064 mutt_add_list (h->data->keywords, flag_word);
1070 /* wrap up, or note bad flags response */
1072 /* if a message is neither seen nor recent, it is OLD. */
1073 if (option (OPTMARKOLD) && !recent && !(h->read))
1078 debug_print (1, ("Unterminated FLAGS response: %s\n", s));
1085 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn)
1088 mutt_socket_write (conn, buf);