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"
36 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn);
37 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
39 static int msg_has_flag (LIST * flag_list, const char *flag);
40 static int msg_parse_fetch (IMAP_HEADER * h, char *s);
41 static char *msg_parse_flags (IMAP_HEADER * h, char *s);
44 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
46 static size_t imap_hcache_keylen (const char *fn);
47 #endif /* USE_HCACHE */
50 * Changed to read many headers instead of just one. It will return the
51 * msgno of the last message read. It will return a value other than
52 * msgend if mail comes in while downloading headers (in theory).
54 int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend)
57 char buf[LONG_STRING];
60 char tempfile[_POSIX_PATH_MAX];
63 int rc, mfhrc, oldmsgcount;
65 const char *want_headers =
66 "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO LINES LIST-POST X-LABEL";
70 uint64_t *uid_validity = NULL;
72 #endif /* USE_HCACHE */
77 hc = mutt_hcache_open (HeaderCache, ctx->path);
78 #endif /* USE_HCACHE */
80 if (mutt_bit_isset (idata->capabilities, IMAP4REV1)) {
81 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s%s%s)]",
82 want_headers, ImapHeaders ? " " : "",
83 ImapHeaders ? ImapHeaders : "");
85 else if (mutt_bit_isset (idata->capabilities, IMAP4)) {
86 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s%s%s)",
87 want_headers, ImapHeaders ? " " : "",
88 ImapHeaders ? ImapHeaders : "");
90 else { /* Unable to fetch headers for lower versions */
91 mutt_error _("Unable to fetch headers from this IMAP server version.");
93 mutt_sleep (2); /* pause a moment to let the user see the error */
95 mutt_hcache_close (hc);
96 #endif /* USE_HCACHE */
100 /* instead of downloading all headers and then parsing them, we parse them
101 * as they come in. */
102 mutt_mktemp (tempfile);
103 if (!(fp = safe_fopen (tempfile, "w+"))) {
104 mutt_error (_("Could not create temporary file %s"), tempfile);
107 mutt_hcache_close (hc);
108 #endif /* USE_HCACHE */
113 /* make sure context has room to hold the mailbox */
114 while ((msgend) >= idata->ctx->hdrmax)
115 mx_alloc_memory (idata->ctx);
117 oldmsgcount = ctx->msgcount;
118 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
119 idata->newMailCount = 0;
122 snprintf (buf, sizeof (buf),
123 "FETCH %d:%d (UID FLAGS)", msgbegin + 1, msgend + 1);
124 fetchlast = msgend + 1;
126 imap_cmd_start (idata, buf);
128 for (msgno = msgbegin; msgno <= msgend; msgno++) {
129 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
130 mutt_message (_("Evaluating cache... [%d/%d]"), msgno + 1, msgend + 1);
133 memset (&h, 0, sizeof (h));
134 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
138 rc = imap_cmd_step (idata);
139 if (rc != IMAP_CMD_CONTINUE)
143 msg_fetch_header_fetch (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
148 /* make sure we don't get remnants from older larger message headers */
151 sprintf (uid_buf, "/%u", h.data->uid); /* XXX --tg 21:41 04-07-11 */
153 (uint64_t *) mutt_hcache_fetch (hc, uid_buf, &imap_hcache_keylen);
155 if (uid_validity != NULL && *uid_validity == idata->uid_validity) {
157 mutt_hcache_restore ((unsigned char *) uid_validity, 0);
158 ctx->hdrs[msgno]->index = h.sid - 1;
159 if (h.sid != ctx->msgcount + 1)
162 "imap_read_headers: msgcount and sequence ID are inconsistent!"));
163 /* messages which have not been expunged are ACTIVE (borrowed from mh
165 ctx->hdrs[msgno]->active = 1;
166 ctx->hdrs[msgno]->read = h.read;
167 ctx->hdrs[msgno]->old = h.old;
168 ctx->hdrs[msgno]->deleted = h.deleted;
169 ctx->hdrs[msgno]->flagged = h.flagged;
170 ctx->hdrs[msgno]->replied = h.replied;
171 ctx->hdrs[msgno]->changed = h.changed;
172 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
173 ctx->hdrs[msgno]->data = (void *) (h.data);
179 FREE (&uid_validity);
182 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
183 ((msgno + 1) >= fetchlast)));
185 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
186 imap_free_header_data ((void **) &h.data);
188 mutt_hcache_close (hc);
193 fetchlast = msgbegin;
194 #endif /* USE_HCACHE */
196 for (msgno = msgbegin; msgno <= msgend; msgno++) {
197 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
198 mutt_message (_("Fetching message headers... [%d/%d]"), msgno + 1,
201 if (ctx->hdrs[msgno])
204 if (msgno + 1 > fetchlast) {
205 fetchlast = msgno + 1;
206 while ((fetchlast <= msgend) && (!ctx->hdrs[fetchlast]))
210 * Make one request for everything. This makes fetching headers an
211 * order of magnitude faster if you have a large mailbox.
213 * If we get more messages while doing this, we make another
214 * request for all the new messages.
216 snprintf (buf, sizeof (buf),
217 "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)",
218 msgno + 1, fetchlast, hdrreq);
220 imap_cmd_start (idata, buf);
225 memset (&h, 0, sizeof (h));
226 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
228 /* this DO loop does two things:
229 * 1. handles untagged messages, so we can try again on the same msg
230 * 2. fetches the tagged response at the end of the last message.
235 rc = imap_cmd_step (idata);
236 if (rc != IMAP_CMD_CONTINUE)
240 msg_fetch_header (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
245 /* make sure we don't get remnants from older larger message headers */
248 /* update context with message header */
249 ctx->hdrs[msgno] = mutt_new_header ();
251 ctx->hdrs[msgno]->index = h.sid - 1;
252 if (h.sid != ctx->msgcount + 1)
255 "imap_read_headers: msgcount and sequence ID are inconsistent!"));
256 /* messages which have not been expunged are ACTIVE (borrowed from mh
258 ctx->hdrs[msgno]->active = 1;
259 ctx->hdrs[msgno]->read = h.read;
260 ctx->hdrs[msgno]->old = h.old;
261 ctx->hdrs[msgno]->deleted = h.deleted;
262 ctx->hdrs[msgno]->flagged = h.flagged;
263 ctx->hdrs[msgno]->replied = h.replied;
264 ctx->hdrs[msgno]->changed = h.changed;
265 ctx->hdrs[msgno]->received = h.received;
266 ctx->hdrs[msgno]->data = (void *) (h.data);
269 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
270 * on h.received being set */
271 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno],
273 /* content built as a side-effect of mutt_read_rfc822_header */
274 ctx->hdrs[msgno]->content->length = h.content_length;
277 sprintf (uid_buf, "/%u", h.data->uid);
278 mutt_hcache_store (hc, uid_buf, ctx->hdrs[msgno], idata->uid_validity,
279 &imap_hcache_keylen);
280 #endif /* USE_HCACHE */
284 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
285 ((msgno + 1) >= fetchlast)));
287 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
288 imap_free_header_data ((void **) &h.data);
291 mutt_hcache_close (hc);
292 #endif /* USE_HCACHE */
296 /* in case we get new mail while fetching the headers */
297 if (idata->reopen & IMAP_NEWMAIL_PENDING) {
298 msgend = idata->newMailCount - 1;
299 while ((msgend) >= ctx->hdrmax)
300 mx_alloc_memory (ctx);
301 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
302 idata->newMailCount = 0;
307 mutt_hcache_close (hc);
308 #endif /* USE_HCACHE */
312 if (ctx->msgcount > oldmsgcount)
313 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
318 int imap_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
322 char buf[LONG_STRING];
323 char path[_POSIX_PATH_MAX];
332 /* Sam's weird courier server returns an OK response even when FETCH
333 * fails. Thanks Sam. */
336 idata = (IMAP_DATA *) ctx->data;
337 h = ctx->hdrs[msgno];
339 /* see if we already have the message in our cache */
340 cacheno = HEADER_DATA (h)->uid % IMAP_CACHE_LEN;
341 cache = &idata->cache[cacheno];
344 if (cache->uid == HEADER_DATA (h)->uid) {
345 /* yes, so just return a pointer to the message */
346 if (!(msg->fp = fopen (cache->path, "r"))) {
347 mutt_perror (cache->path);
353 /* clear the previous entry */
354 unlink (cache->path);
360 mutt_message _("Fetching message...");
362 cache->uid = HEADER_DATA (h)->uid;
364 cache->path = safe_strdup (path);
365 if (!(msg->fp = safe_fopen (path, "w+"))) {
370 /* mark this header as currently inactive so the command handler won't
371 * also try to update it. HACK until all this code can be moved into the
375 snprintf (buf, sizeof (buf), "UID FETCH %u %s", HEADER_DATA (h)->uid,
376 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
377 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") : "RFC822"));
379 imap_cmd_start (idata, buf);
381 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
385 pc = imap_next_word (pc);
386 pc = imap_next_word (pc);
388 if (!ascii_strncasecmp ("FETCH", pc, 5)) {
390 pc = imap_next_word (pc);
393 if (ascii_strncasecmp ("UID", pc, 3) == 0) {
394 pc = imap_next_word (pc);
396 if (uid != HEADER_DATA (h)->uid)
398 ("The message index is incorrect. Try reopening the mailbox."));
400 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
401 (ascii_strncasecmp ("BODY[]", pc, 6) == 0)) {
402 pc = imap_next_word (pc);
403 if (imap_get_literal_count (pc, &bytes) < 0) {
404 imap_error ("imap_fetch_message()", buf);
407 if (imap_read_literal (msg->fp, idata, bytes) < 0)
409 /* pick up trailing line */
410 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
416 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
417 * change (eg from \Unseen to \Seen).
418 * Uncommitted changes in mutt take precedence. If we decide to
419 * incrementally update flags later, this won't stop us syncing */
420 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed) {
421 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
427 while (rc == IMAP_CMD_CONTINUE);
429 /* see comment before command start. */
432 if (rc != IMAP_CMD_OK)
435 if (!fetched || !imap_code (idata->cmd.buf))
438 /* Update the header information. Previously, we only downloaded a
439 * portion of the headers, those required for the main display.
442 /* It may be that the Status header indicates a message is read, but the
443 * IMAP server doesn't know the message has been \Seen. So we capture
444 * the server's notion of 'read' and if it differs from the message info
445 * picked up in mutt_read_rfc822_header, we mark the message (and context
446 * changed). Another possiblity: ignore Status on IMAP?*/
448 /* I hate do this here, since it's so low-level, but I'm not sure where
449 * I can abstract it. Problem: the id and subj hashes lose their keys when
450 * mutt_free_envelope gets called, but keep their spots in the hash. This
451 * confuses threading. Alternatively we could try to merge the new
452 * envelope into the old one. Also messy and lowlevel. */
453 if (ctx->id_hash && h->env->message_id)
454 hash_delete (ctx->id_hash, h->env->message_id, h, NULL);
455 if (ctx->subj_hash && h->env->real_subj)
456 hash_delete (ctx->subj_hash, h->env->real_subj, h, NULL);
457 mutt_free_envelope (&h->env);
458 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
459 if (ctx->id_hash && h->env->message_id)
460 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
461 if (ctx->subj_hash && h->env->real_subj)
462 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
464 /* see above. We want the new status in h->read, so we unset it manually
465 * and let mutt_set_flag set it correctly, updating context. */
466 if (read != h->read) {
468 mutt_set_flag (ctx, h, M_NEW, read);
472 fgets (buf, sizeof (buf), msg->fp);
473 while (!feof (msg->fp)) {
475 fgets (buf, sizeof (buf), msg->fp);
478 h->content->length = ftell (msg->fp) - h->content->offset;
480 /* This needs to be done in case this is a multipart message */
481 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
482 h->security = crypt_query (h->content);
491 safe_fclose (&msg->fp);
493 unlink (cache->path);
500 int imap_append_message (CONTEXT * ctx, MESSAGE * msg)
504 char buf[LONG_STRING];
505 char mbox[LONG_STRING];
506 char mailbox[LONG_STRING];
512 idata = (IMAP_DATA *) ctx->data;
514 if (imap_parse_path (ctx->path, &mx))
517 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
519 if ((fp = fopen (msg->path, "r")) == NULL) {
520 mutt_perror (msg->path);
524 /* currently we set the \Seen flag on all messages, but probably we
525 * should scan the message Status header for flag info. Since we're
526 * already rereading the whole file for length it isn't any more
527 * expensive (it'd be nice if we had the file size passed in already
528 * by the code that writes the file, but that's a lot of changes.
529 * Ideally we'd have a HEADER structure with flag info here... */
530 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
531 if (c == '\n' && last != '\r')
538 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
539 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) {%lu}", mbox,
540 msg->flags.read ? "\\Seen" : "",
541 msg->flags.read && (msg->flags.replied
542 || msg->flags.flagged) ? " " : "",
543 msg->flags.replied ? "\\Answered" : "", msg->flags.replied
544 && msg->flags.flagged ? " " : "",
545 msg->flags.flagged ? "\\Flagged" : "", (unsigned long) len);
547 imap_cmd_start (idata, buf);
550 rc = imap_cmd_step (idata);
551 while (rc == IMAP_CMD_CONTINUE);
553 if (rc != IMAP_CMD_RESPOND) {
556 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
559 pc = idata->cmd.buf + SEQLEN;
561 pc = imap_next_word (pc);
562 mutt_error ("%s", pc);
568 mutt_message _("Uploading message ...");
570 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
571 if (c == '\n' && last != '\r')
576 if (len > sizeof (buf) - 3)
577 flush_buffer (buf, &len, idata->conn);
581 flush_buffer (buf, &len, idata->conn);
583 mutt_socket_write (idata->conn, "\r\n");
587 rc = imap_cmd_step (idata);
588 while (rc == IMAP_CMD_CONTINUE);
590 if (!imap_code (idata->cmd.buf)) {
593 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
595 pc = idata->cmd.buf + SEQLEN;
597 pc = imap_next_word (pc);
598 mutt_error ("%s", pc);
611 /* imap_copy_messages: use server COPY command to copy messages to another
616 * 1: non-fatal error - try fetch/append */
617 int imap_copy_messages (CONTEXT * ctx, HEADER * h, char *dest, int delete)
622 char mbox[LONG_STRING];
623 char mmbox[LONG_STRING];
628 idata = (IMAP_DATA *) ctx->data;
630 if (imap_parse_path (dest, &mx)) {
631 dprint (1, (debugfile, "imap_copy_messages: bad destination %s\n", dest));
635 /* check that the save-to folder is in the same account */
636 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account))) {
637 dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n",
642 if (h && h->attach_del) {
645 "imap_copy_messages: Message contains attachments to be deleted\n"));
649 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
651 memset (&cmd, 0, sizeof (cmd));
652 mutt_buffer_addstr (&cmd, "UID COPY ");
654 /* Null HEADER* means copy tagged messages */
656 /* if any messages have attachments to delete, fall through to FETCH
657 * and APPEND. TODO: Copy what we can with COPY, fall through for the
659 for (n = 0; n < ctx->msgcount; n++) {
660 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del) {
663 "imap_copy_messages: Message contains attachments to be deleted\n"));
668 rc = imap_make_msg_set (idata, &cmd, M_TAG, 0);
670 dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
673 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
676 mutt_message (_("Copying message %d to %s..."), h->index + 1, mbox);
677 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
678 mutt_buffer_addstr (&cmd, uid);
681 /* let's get it on */
682 mutt_buffer_addstr (&cmd, " ");
683 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
684 mutt_buffer_addstr (&cmd, mmbox);
686 rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
688 /* bail out if command failed for reasons other than nonexistent target */
689 if (ascii_strncasecmp
690 (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11)) {
691 imap_error ("imap_copy_messages", idata->cmd.buf);
695 (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
696 snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
697 if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1) {
701 if (imap_create_mailbox (idata, mbox) < 0)
705 rc = imap_exec (idata, cmd.data, 0);
708 imap_error ("imap_copy_messages", idata->cmd.buf);
715 for (n = 0; n < ctx->msgcount; n++) {
716 if (ctx->hdrs[n]->tagged) {
717 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
718 mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
719 if (option (OPTDELETEUNTAG))
720 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
724 mutt_set_flag (ctx, h, M_DELETE, 1);
725 mutt_set_flag (ctx, h, M_APPENDED, 1);
726 if (option (OPTDELETEUNTAG))
727 mutt_set_flag (ctx, h, M_TAG, 0);
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 dprint (2, (debugfile, "imap_fetch_message: 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) {
975 "msg_parse_fetch(): bogus INTERNALDATE entry: %s\n", s));
980 while (*s && *s != '\"')
984 s++; /* skip past the trailing " */
986 h->received = imap_parse_date (tmp);
988 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0) {
992 while (isdigit ((unsigned char) *s))
995 h->content_length = atoi (tmp);
997 else if (!ascii_strncasecmp ("BODY", s, 4) ||
998 !ascii_strncasecmp ("RFC822.HEADER", s, 13)) {
999 /* handle above, in msg_fetch_header */
1003 s++; /* end of request */
1005 /* got something i don't understand */
1006 imap_error ("msg_parse_fetch", s);
1014 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1015 static char *msg_parse_flags (IMAP_HEADER * h, char *s)
1019 /* sanity-check string */
1020 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
1021 dprint (1, (debugfile, "msg_parse_flags: not a FLAGS response: %s\n", s));
1027 dprint (1, (debugfile, "msg_parse_flags: bogus FLAGS response: %s\n", s));
1033 while (*s && *s != ')') {
1034 if (ascii_strncasecmp ("\\deleted", s, 8) == 0) {
1038 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0) {
1042 else if (ascii_strncasecmp ("\\answered", s, 9) == 0) {
1046 else if (ascii_strncasecmp ("\\seen", s, 5) == 0) {
1050 else if (ascii_strncasecmp ("\\recent", s, 5) == 0) {
1055 /* store custom flags as well */
1057 char *flag_word = s;
1059 if (!h->data->keywords)
1060 h->data->keywords = mutt_new_list ();
1062 while (*s && !ISSPACE (*s) && *s != ')')
1066 mutt_add_list (h->data->keywords, flag_word);
1072 /* wrap up, or note bad flags response */
1074 /* if a message is neither seen nor recent, it is OLD. */
1075 if (option (OPTMARKOLD) && !recent && !(h->read))
1080 dprint (1, (debugfile,
1081 "msg_parse_flags: Unterminated FLAGS response: %s\n", s));
1088 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn)
1091 mutt_socket_write (conn, buf);