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 */
20 #include <lib-lib/mem.h>
25 #include "mutt_curses.h"
26 #include "imap_private.h"
35 #include <lib-lib/macros.h>
37 #include "lib/debug.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 =
75 "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO LINES LIST-POST X-LABEL";
79 unsigned long *uid_validity = NULL;
81 #endif /* USE_HCACHE */
85 if (mutt_bit_isset (idata->capabilities, IMAP4REV1)) {
86 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s%s%s)]",
87 want_headers, ImapHeaders ? " " : "",
88 ImapHeaders ? ImapHeaders : "");
90 else if (mutt_bit_isset (idata->capabilities, IMAP4)) {
91 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s%s%s)",
92 want_headers, ImapHeaders ? " " : "",
93 ImapHeaders ? ImapHeaders : "");
95 else { /* Unable to fetch headers for lower versions */
96 mutt_error _("Unable to fetch headers from this IMAP server version.");
98 mutt_sleep (2); /* pause a moment to let the user see the error */
102 /* instead of downloading all headers and then parsing them, we parse them
103 * as they come in. */
104 mutt_mktemp (tempfile);
105 if (!(fp = safe_fopen (tempfile, "w+"))) {
106 mutt_error (_("Could not create temporary file %s"), tempfile);
112 /* make sure context has room to hold the mailbox */
113 while ((msgend) >= idata->ctx->hdrmax)
114 mx_alloc_memory (idata->ctx);
116 oldmsgcount = ctx->msgcount;
117 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
118 idata->newMailCount = 0;
121 if ((hc = mutt_hcache_open (HeaderCache, ctx->path))) {
123 snprintf (buf, sizeof (buf),
124 "FETCH %d:%d (UID FLAGS)", msgbegin + 1, msgend + 1);
125 fetchlast = msgend + 1;
127 imap_cmd_start (idata, buf);
129 for (msgno = msgbegin; msgno <= msgend; msgno++) {
130 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
131 mutt_message (_("Evaluating cache... [%d/%d]"), msgno + 1, msgend + 1);
134 memset (&h, 0, sizeof (h));
135 h.data = p_new(IMAP_HEADER_DATA, 1);
139 rc = imap_cmd_step (idata);
140 if (rc != IMAP_CMD_CONTINUE)
144 msg_fetch_header_fetch (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
149 /* make sure we don't get remnants from older larger message headers */
152 sprintf (uid_buf, "/%u", h.data->uid); /* XXX --tg 21:41 04-07-11 */
154 (unsigned long *) mutt_hcache_fetch (hc, uid_buf, &imap_hcache_keylen);
156 if (uid_validity != NULL && *uid_validity == idata->uid_validity) {
157 ctx->hdrs[msgno] = mutt_hcache_restore((unsigned char *) uid_validity, 0);
158 ctx->hdrs[msgno]->index = h.sid - 1;
159 if (h.sid != ctx->msgcount + 1)
160 debug_print (1, ("imap_read_headers: msgcount and sequence ID are inconsistent!"));
161 /* messages which have not been expunged are ACTIVE (borrowed from mh
163 ctx->hdrs[msgno]->active = 1;
164 ctx->hdrs[msgno]->read = h.read;
165 ctx->hdrs[msgno]->old = h.old;
166 ctx->hdrs[msgno]->deleted = h.deleted;
167 ctx->hdrs[msgno]->flagged = h.flagged;
168 ctx->hdrs[msgno]->replied = h.replied;
169 ctx->hdrs[msgno]->changed = h.changed;
170 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
171 ctx->hdrs[msgno]->data = (void *) (h.data);
178 p_delete(&uid_validity);
181 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
182 ((msgno + 1) >= fetchlast)));
184 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
185 imap_free_header_data ((void **) &h.data);
187 mutt_hcache_close (hc);
192 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 = p_new(IMAP_HEADER_DATA, 1);
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)
253 debug_print (1, ("msgcount and sequence ID are inconsistent!\n"));
254 /* messages which have not been expunged are ACTIVE (borrowed from mh
256 ctx->hdrs[msgno]->active = 1;
257 ctx->hdrs[msgno]->read = h.read;
258 ctx->hdrs[msgno]->old = h.old;
259 ctx->hdrs[msgno]->deleted = h.deleted;
260 ctx->hdrs[msgno]->flagged = h.flagged;
261 ctx->hdrs[msgno]->replied = h.replied;
262 ctx->hdrs[msgno]->changed = h.changed;
263 ctx->hdrs[msgno]->received = h.received;
264 ctx->hdrs[msgno]->data = (void *) (h.data);
267 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
268 * on h.received being set */
269 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno],
271 /* content built as a side-effect of mutt_read_rfc822_header */
272 ctx->hdrs[msgno]->content->length = h.content_length;
275 sprintf (uid_buf, "/%u", h.data->uid);
276 mutt_hcache_store (hc, uid_buf, ctx->hdrs[msgno], idata->uid_validity,
277 &imap_hcache_keylen);
278 #endif /* USE_HCACHE */
282 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
283 ((msgno + 1) >= fetchlast)));
285 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
286 imap_free_header_data ((void *)&h.data);
289 mutt_hcache_close (hc);
290 #endif /* USE_HCACHE */
294 /* in case we get new mail while fetching the headers */
295 if (idata->reopen & IMAP_NEWMAIL_PENDING) {
296 msgend = idata->newMailCount - 1;
297 while ((msgend) >= ctx->hdrmax)
298 mx_alloc_memory (ctx);
299 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
300 idata->newMailCount = 0;
305 mutt_hcache_close (hc);
306 #endif /* USE_HCACHE */
310 if (ctx->msgcount > oldmsgcount)
311 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
316 int imap_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
321 char buf[LONG_STRING];
322 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 /* don't treat cache errors as fatal, just fall back. */
345 if (cache->uid == HEADER_DATA(h)->uid && (msg->fp = fopen (cache->path, "r")))
348 unlink (cache->path);
349 p_delete(&cache->path);
354 mutt_message _("Fetching message...");
356 cache->uid = HEADER_DATA (h)->uid;
358 cache->path = str_dup (path);
359 if (!(msg->fp = safe_fopen (path, "w+"))) {
360 p_delete(&cache->path);
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);
402 bar.msg = _("Fetching message...");
403 mutt_progress_bar (&bar, 0);
404 if (imap_read_literal (msg->fp, idata, bytes, &bar) < 0)
406 /* pick up trailing line */
407 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
413 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
414 * change (eg from \Unseen to \Seen).
415 * Uncommitted changes in mutt take precedence. If we decide to
416 * incrementally update flags later, this won't stop us syncing */
417 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed) {
418 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
424 while (rc == IMAP_CMD_CONTINUE);
426 /* see comment before command start. */
430 if (ferror (msg->fp)) {
431 mutt_perror (cache->path);
435 if (rc != IMAP_CMD_OK)
438 if (!fetched || !imap_code (idata->cmd.buf))
441 /* Update the header information. Previously, we only downloaded a
442 * portion of the headers, those required for the main display.
445 /* It may be that the Status header indicates a message is read, but the
446 * IMAP server doesn't know the message has been \Seen. So we capture
447 * the server's notion of 'read' and if it differs from the message info
448 * picked up in mutt_read_rfc822_header, we mark the message (and context
449 * changed). Another possiblity: ignore Status on IMAP?*/
451 newenv = mutt_read_rfc822_header (msg->fp, h, 0, 0);
452 mutt_merge_envelopes(h->env, &newenv);
454 /* see above. We want the new status in h->read, so we unset it manually
455 * and let mutt_set_flag set it correctly, updating context. */
456 if (read != h->read) {
458 mutt_set_flag (ctx, h, M_NEW, read);
462 fgets (buf, sizeof (buf), msg->fp);
463 while (!feof (msg->fp)) {
465 fgets (buf, sizeof (buf), msg->fp);
468 h->content->length = ftello (msg->fp) - h->content->offset;
470 /* This needs to be done in case this is a multipart message */
471 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
472 h->security = crypt_query (h->content);
481 safe_fclose (&msg->fp);
483 unlink (cache->path);
484 p_delete(&cache->path);
490 int imap_append_message (CONTEXT * ctx, MESSAGE * msg)
494 char buf[LONG_STRING];
495 char mbox[LONG_STRING];
496 char mailbox[LONG_STRING];
504 idata = (IMAP_DATA *) ctx->data;
506 if (imap_parse_path (ctx->path, &mx))
509 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
511 if ((fp = fopen (msg->path, "r")) == NULL) {
512 mutt_perror (msg->path);
516 /* currently we set the \Seen flag on all messages, but probably we
517 * should scan the message Status header for flag info. Since we're
518 * already rereading the whole file for length it isn't any more
519 * expensive (it'd be nice if we had the file size passed in already
520 * by the code that writes the file, but that's a lot of changes.
521 * Ideally we'd have a HEADER structure with flag info here... */
522 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
523 if (c == '\n' && last != '\r')
530 bar.msg = _("Uploading message...");
532 mutt_progress_bar (&bar, 0);
534 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
535 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) {%lu}", mbox,
536 msg->flags.read ? "\\Seen" : "",
537 msg->flags.read && (msg->flags.replied
538 || msg->flags.flagged) ? " " : "",
539 msg->flags.replied ? "\\Answered" : "", msg->flags.replied
540 && msg->flags.flagged ? " " : "",
541 msg->flags.flagged ? "\\Flagged" : "", (unsigned long) len);
543 imap_cmd_start (idata, buf);
546 rc = imap_cmd_step (idata);
547 while (rc == IMAP_CMD_CONTINUE);
549 if (rc != IMAP_CMD_RESPOND) {
552 debug_print (1, ("command failed: %s\n", idata->cmd.buf));
554 pc = idata->cmd.buf + SEQLEN;
556 pc = imap_next_word (pc);
557 mutt_error ("%s", pc);
563 for (last = EOF, sent = len = 0; (c = fgetc (fp)) != EOF; last = c) {
564 if (c == '\n' && last != '\r')
569 if (len > sizeof (buf) - 3) {
571 flush_buffer (buf, &len, idata->conn);
572 mutt_progress_bar (&bar, sent);
577 flush_buffer (buf, &len, idata->conn);
579 mutt_socket_write (idata->conn, "\r\n");
583 rc = imap_cmd_step (idata);
584 while (rc == IMAP_CMD_CONTINUE);
586 if (!imap_code (idata->cmd.buf)) {
589 debug_print (1, ("command failed: %s\n", idata->cmd.buf));
590 pc = idata->cmd.buf + SEQLEN;
592 pc = imap_next_word (pc);
593 mutt_error ("%s", pc);
606 /* imap_copy_messages: use server COPY command to copy messages to another
611 * 1: non-fatal error - try fetch/append */
612 int imap_copy_messages (CONTEXT * ctx, HEADER * h, char *dest, int delete)
615 BUFFER cmd, sync_cmd;
617 char mbox[LONG_STRING];
618 char mmbox[LONG_STRING];
622 int err_continue = M_NO;
624 idata = (IMAP_DATA *) ctx->data;
626 if (imap_parse_path (dest, &mx)) {
627 debug_print (1, ("bad destination %s\n", dest));
631 /* check that the save-to folder is in the same account */
632 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account))) {
633 debug_print (3, ("%s not same server as %s\n", dest, ctx->path));
637 if (h && h->attach_del) {
638 debug_print (3, ("Message contains attachments to be deleted\n"));
642 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
644 memset (&sync_cmd, 0, sizeof (sync_cmd));
645 memset (&cmd, 0, sizeof (cmd));
646 mutt_buffer_addstr (&cmd, "UID COPY ");
648 /* Null HEADER* means copy tagged messages */
650 /* if any messages have attachments to delete, fall through to FETCH
651 * and APPEND. TODO: Copy what we can with COPY, fall through for the
653 for (n = 0; n < ctx->msgcount; n++) {
654 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del) {
655 debug_print (3, ("Message contains attachments to be deleted\n"));
659 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
660 ctx->hdrs[n]->changed)
662 rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
665 debug_print (1, ("could not sync\n"));
671 rc = imap_make_msg_set (idata, &cmd, M_TAG, 0);
673 debug_print (1, ("No messages tagged\n"));
676 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
679 mutt_message (_("Copying message %d to %s..."), h->index + 1, mbox);
680 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
681 mutt_buffer_addstr (&cmd, uid);
683 if (h->active && h->changed)
685 rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
688 debug_print (1, ("could not sync\n"));
694 /* let's get it on */
695 mutt_buffer_addstr (&cmd, " ");
696 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
697 mutt_buffer_addstr (&cmd, mmbox);
699 rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
701 /* bail out if command failed for reasons other than nonexistent target */
702 if (ascii_strncasecmp
703 (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11)) {
704 imap_error ("imap_copy_messages", idata->cmd.buf);
707 debug_print (2, ("server suggests TRYCREATE\n"));
708 snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
709 if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1) {
713 if (imap_create_mailbox (idata, mbox) < 0)
717 rc = imap_exec (idata, cmd.data, 0);
720 imap_error ("imap_copy_messages", idata->cmd.buf);
727 for (n = 0; n < ctx->msgcount; n++) {
728 if (ctx->hdrs[n]->tagged) {
729 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
730 mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
731 if (option (OPTDELETEUNTAG))
732 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
736 mutt_set_flag (ctx, h, M_DELETE, 1);
737 mutt_set_flag (ctx, h, M_APPENDED, 1);
738 if (option (OPTDELETEUNTAG))
739 mutt_set_flag (ctx, h, M_TAG, 0);
746 p_delete(&sync_cmd.data);
754 p_delete(&sync_cmd.data);
759 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
760 * appear in the folder flags list. Why wouldn't they? */
761 void imap_add_keywords (char *s, HEADER * h, LIST * mailbox_flags,
766 if (!mailbox_flags || !HEADER_DATA (h) || !HEADER_DATA (h)->keywords)
769 keywords = HEADER_DATA (h)->keywords->next;
772 if (msg_has_flag (mailbox_flags, keywords->data)) {
773 str_cat (s, slen, keywords->data);
774 str_cat (s, slen, " ");
776 keywords = keywords->next;
780 /* imap_free_header_data: free IMAP_HEADER structure */
781 void imap_free_header_data (void **data)
783 /* this should be safe even if the list wasn't used */
784 mutt_free_list (&(((IMAP_HEADER_DATA *) * data)->keywords));
789 /* imap_set_flags: fill out the message header according to the flags from
790 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
791 char *imap_set_flags (IMAP_DATA * idata, HEADER * h, char *s)
793 CONTEXT *ctx = idata->ctx;
795 unsigned char readonly;
797 memset (&newh, 0, sizeof (newh));
798 newh.data = p_new(IMAP_HEADER_DATA, 1);
800 debug_print (2, ("parsing FLAGS\n"));
801 if ((s = msg_parse_flags (&newh, s)) == NULL) {
802 p_delete(&newh.data);
806 /* YAUH (yet another ugly hack): temporarily set context to
807 * read-write even if it's read-only, so *server* updates of
808 * flags can be processed by mutt_set_flag. ctx->changed must
809 * be restored afterwards */
810 readonly = ctx->readonly;
813 mutt_set_flag (ctx, h, M_NEW, !(newh.read || newh.old));
814 mutt_set_flag (ctx, h, M_OLD, newh.old);
815 mutt_set_flag (ctx, h, M_READ, newh.read);
816 mutt_set_flag (ctx, h, M_DELETE, newh.deleted);
817 mutt_set_flag (ctx, h, M_FLAG, newh.flagged);
818 mutt_set_flag (ctx, h, M_REPLIED, newh.replied);
820 /* this message is now definitively *not* changed (mutt_set_flag
821 * marks things changed as a side-effect) */
823 ctx->changed &= ~readonly;
824 ctx->readonly = readonly;
826 mutt_free_list (&(HEADER_DATA (h)->keywords));
827 HEADER_DATA (h)->keywords = newh.data->keywords;
828 p_delete(&newh.data);
834 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
835 * Expects string beginning with * n FETCH.
838 * -1 if the string is not a fetch response
839 * -2 if the string is a corrupt fetch response */
840 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
845 int rc = -1; /* default now is that string isn't FETCH response */
847 idata = (IMAP_DATA *) ctx->data;
852 /* skip to message number */
853 buf = imap_next_word (buf);
857 buf = imap_next_word (buf);
858 if (ascii_strncasecmp ("FETCH", buf, 5))
861 rc = -2; /* we've got a FETCH response, for better or worse */
862 if (!(buf = strchr (buf, '(')))
866 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
867 * read header lines and call it again. Silly. */
868 if (msg_parse_fetch (h, buf) != -2)
871 if (imap_get_literal_count (buf, &bytes) == 0) {
872 imap_read_literal (fp, idata, bytes, NULL);
874 /* we may have other fields of the FETCH _after_ the literal
875 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
876 * This all has to go - we should accept literals and nonliterals
877 * interchangeably at any time. */
878 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
881 if (msg_parse_fetch (h, idata->cmd.buf) == -1)
885 rc = 0; /* success */
887 /* subtract headers from message size - unfortunately only the subset of
888 * headers we've requested. */
889 h->content_length -= bytes;
895 static size_t imap_hcache_keylen (const char *fn)
900 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
901 * Expects string beginning with * n FETCH.
904 * -1 if the string is not a fetch response
905 * -2 if the string is a corrupt fetch response */
906 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
910 int rc = -1; /* default now is that string isn't FETCH response */
912 idata = (IMAP_DATA *) ctx->data;
917 /* skip to message number */
918 buf = imap_next_word (buf);
922 buf = imap_next_word (buf);
923 if (ascii_strncasecmp ("FETCH", buf, 5))
926 rc = -2; /* we've got a FETCH response, for better or worse */
927 if (!(buf = strchr (buf, '(')))
931 if (msg_parse_fetch (h, buf) < 0) {
935 if (!(buf = strchr (buf, ')')))
941 #endif /* USE_HCACHE */
944 /* msg_has_flag: do a caseless comparison of the flag against a flag list,
945 * return 1 if found or flag list has '\*', 0 otherwise */
946 static int msg_has_flag (LIST * flag_list, const char *flag)
951 flag_list = flag_list->next;
953 if (!ascii_strncasecmp (flag_list->data, flag, str_len (flag_list->data)))
956 flag_list = flag_list->next;
962 /* msg_parse_fetch: handle headers returned from header fetch */
963 static int msg_parse_fetch (IMAP_HEADER * h, char *s)
965 char tmp[SHORT_STRING];
974 if (ascii_strncasecmp ("FLAGS", s, 5) == 0) {
975 if ((s = msg_parse_flags (h, s)) == NULL)
978 else if (ascii_strncasecmp ("UID", s, 3) == 0) {
981 h->data->uid = (unsigned int) atoi (s);
983 s = imap_next_word (s);
985 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0) {
989 debug_print (1, ("bogus INTERNALDATE entry: %s\n", s));
994 while (*s && *s != '\"')
998 s++; /* skip past the trailing " */
1000 h->received = imap_parse_date (tmp);
1002 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0) {
1006 while (isdigit ((unsigned char) *s))
1009 h->content_length = atoi (tmp);
1011 else if (!ascii_strncasecmp ("BODY", s, 4) ||
1012 !ascii_strncasecmp ("RFC822.HEADER", s, 13)) {
1013 /* handle above, in msg_fetch_header */
1017 s++; /* end of request */
1019 /* got something i don't understand */
1020 imap_error ("msg_parse_fetch", s);
1028 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1029 static char *msg_parse_flags (IMAP_HEADER * h, char *s)
1033 /* sanity-check string */
1034 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
1035 debug_print (1, ("not a FLAGS response: %s\n", s));
1041 debug_print (1, ("bogus FLAGS response: %s\n", s));
1047 while (*s && *s != ')') {
1048 if (ascii_strncasecmp ("\\deleted", s, 8) == 0) {
1052 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0) {
1056 else if (ascii_strncasecmp ("\\answered", s, 9) == 0) {
1060 else if (ascii_strncasecmp ("\\seen", s, 5) == 0) {
1064 else if (ascii_strncasecmp ("\\recent", s, 5) == 0) {
1069 /* store custom flags as well */
1071 char *flag_word = s;
1073 if (!h->data->keywords)
1074 h->data->keywords = mutt_new_list ();
1076 while (*s && !ISSPACE (*s) && *s != ')')
1080 mutt_add_list (h->data->keywords, flag_word);
1086 /* wrap up, or note bad flags response */
1088 /* if a message is neither seen nor recent, it is OLD. */
1089 if (option (OPTMARKOLD) && !recent && !(h->read))
1094 debug_print (1, ("Unterminated FLAGS response: %s\n", s));
1101 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn)
1104 mutt_socket_write (conn, buf);