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 */
23 #include "mutt_curses.h"
24 #include "imap_private.h"
36 #include "lib/debug.h"
44 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn);
45 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
47 static int msg_has_flag (LIST * flag_list, const char *flag);
48 static int msg_parse_fetch (IMAP_HEADER * h, char *s);
49 static char *msg_parse_flags (IMAP_HEADER * h, char *s);
52 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
54 static size_t imap_hcache_keylen (const char *fn);
55 #endif /* USE_HCACHE */
58 * Changed to read many headers instead of just one. It will return the
59 * msgno of the last message read. It will return a value other than
60 * msgend if mail comes in while downloading headers (in theory).
62 int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend)
65 char buf[LONG_STRING];
68 char tempfile[_POSIX_PATH_MAX];
71 int rc, mfhrc, oldmsgcount;
73 const char *want_headers =
74 "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO LINES LIST-POST X-LABEL";
78 unsigned long *uid_validity = NULL;
80 #endif /* USE_HCACHE */
84 if (mutt_bit_isset (idata->capabilities, IMAP4REV1)) {
85 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s%s%s)]",
86 want_headers, ImapHeaders ? " " : "",
87 ImapHeaders ? ImapHeaders : "");
89 else if (mutt_bit_isset (idata->capabilities, IMAP4)) {
90 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s%s%s)",
91 want_headers, ImapHeaders ? " " : "",
92 ImapHeaders ? ImapHeaders : "");
94 else { /* Unable to fetch headers for lower versions */
95 mutt_error _("Unable to fetch headers from this IMAP server version.");
97 mutt_sleep (2); /* pause a moment to let the user see the error */
101 /* instead of downloading all headers and then parsing them, we parse them
102 * as they come in. */
103 mutt_mktemp (tempfile);
104 if (!(fp = safe_fopen (tempfile, "w+"))) {
105 mutt_error (_("Could not create temporary file %s"), tempfile);
111 /* make sure context has room to hold the mailbox */
112 while ((msgend) >= idata->ctx->hdrmax)
113 mx_alloc_memory (idata->ctx);
115 oldmsgcount = ctx->msgcount;
116 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
117 idata->newMailCount = 0;
120 hc = mutt_hcache_open (HeaderCache, ctx->path);
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 = mem_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 (unsigned long *) mutt_hcache_fetch (hc, uid_buf, &imap_hcache_keylen);
155 if (uid_validity != NULL && *uid_validity == idata->uid_validity) {
156 ctx->hdrs[msgno] = mutt_hcache_restore((unsigned char *) uid_validity, 0);
157 ctx->hdrs[msgno]->index = h.sid - 1;
158 if (h.sid != ctx->msgcount + 1)
159 debug_print (1, ("imap_read_headers: msgcount and sequence ID are inconsistent!"));
160 /* messages which have not been expunged are ACTIVE (borrowed from mh
162 ctx->hdrs[msgno]->active = 1;
163 ctx->hdrs[msgno]->read = h.read;
164 ctx->hdrs[msgno]->old = h.old;
165 ctx->hdrs[msgno]->deleted = h.deleted;
166 ctx->hdrs[msgno]->flagged = h.flagged;
167 ctx->hdrs[msgno]->replied = h.replied;
168 ctx->hdrs[msgno]->changed = h.changed;
169 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
170 ctx->hdrs[msgno]->data = (void *) (h.data);
177 mem_free (&uid_validity);
180 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
181 ((msgno + 1) >= fetchlast)));
183 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
184 imap_free_header_data ((void **) &h.data);
186 mutt_hcache_close (hc);
191 fetchlast = msgbegin;
192 #endif /* USE_HCACHE */
194 for (msgno = msgbegin; msgno <= msgend; msgno++) {
195 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
196 mutt_message (_("Fetching message headers... [%d/%d]"), msgno + 1,
199 if (ctx->hdrs[msgno])
202 if (msgno + 1 > fetchlast) {
203 fetchlast = msgno + 1;
204 while ((fetchlast <= msgend) && (!ctx->hdrs[fetchlast]))
208 * Make one request for everything. This makes fetching headers an
209 * order of magnitude faster if you have a large mailbox.
211 * If we get more messages while doing this, we make another
212 * request for all the new messages.
214 snprintf (buf, sizeof (buf),
215 "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)",
216 msgno + 1, fetchlast, hdrreq);
218 imap_cmd_start (idata, buf);
223 memset (&h, 0, sizeof (h));
224 h.data = mem_calloc (1, sizeof (IMAP_HEADER_DATA));
226 /* this DO loop does two things:
227 * 1. handles untagged messages, so we can try again on the same msg
228 * 2. fetches the tagged response at the end of the last message.
233 rc = imap_cmd_step (idata);
234 if (rc != IMAP_CMD_CONTINUE)
238 msg_fetch_header (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
243 /* make sure we don't get remnants from older larger message headers */
246 /* update context with message header */
247 ctx->hdrs[msgno] = mutt_new_header ();
249 ctx->hdrs[msgno]->index = h.sid - 1;
250 if (h.sid != ctx->msgcount + 1)
251 debug_print (1, ("msgcount and sequence ID are inconsistent!\n"));
252 /* messages which have not been expunged are ACTIVE (borrowed from mh
254 ctx->hdrs[msgno]->active = 1;
255 ctx->hdrs[msgno]->read = h.read;
256 ctx->hdrs[msgno]->old = h.old;
257 ctx->hdrs[msgno]->deleted = h.deleted;
258 ctx->hdrs[msgno]->flagged = h.flagged;
259 ctx->hdrs[msgno]->replied = h.replied;
260 ctx->hdrs[msgno]->changed = h.changed;
261 ctx->hdrs[msgno]->received = h.received;
262 ctx->hdrs[msgno]->data = (void *) (h.data);
265 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
266 * on h.received being set */
267 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno],
269 /* content built as a side-effect of mutt_read_rfc822_header */
270 ctx->hdrs[msgno]->content->length = h.content_length;
273 sprintf (uid_buf, "/%u", h.data->uid);
274 mutt_hcache_store (hc, uid_buf, ctx->hdrs[msgno], idata->uid_validity,
275 &imap_hcache_keylen);
276 #endif /* USE_HCACHE */
280 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
281 ((msgno + 1) >= fetchlast)));
283 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
284 imap_free_header_data ((void **) &h.data);
287 mutt_hcache_close (hc);
288 #endif /* USE_HCACHE */
292 /* in case we get new mail while fetching the headers */
293 if (idata->reopen & IMAP_NEWMAIL_PENDING) {
294 msgend = idata->newMailCount - 1;
295 while ((msgend) >= ctx->hdrmax)
296 mx_alloc_memory (ctx);
297 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
298 idata->newMailCount = 0;
303 mutt_hcache_close (hc);
304 #endif /* USE_HCACHE */
308 if (ctx->msgcount > oldmsgcount)
309 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
314 int imap_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
319 char buf[LONG_STRING];
320 char path[_POSIX_PATH_MAX];
330 /* Sam's weird courier server returns an OK response even when FETCH
331 * fails. Thanks Sam. */
334 idata = (IMAP_DATA *) ctx->data;
335 h = ctx->hdrs[msgno];
337 /* see if we already have the message in our cache */
338 cacheno = HEADER_DATA (h)->uid % IMAP_CACHE_LEN;
339 cache = &idata->cache[cacheno];
342 /* don't treat cache errors as fatal, just fall back. */
343 if (cache->uid == HEADER_DATA(h)->uid && (msg->fp = fopen (cache->path, "r")))
346 unlink (cache->path);
347 mem_free (&cache->path);
352 mutt_message _("Fetching message...");
354 cache->uid = HEADER_DATA (h)->uid;
356 cache->path = str_dup (path);
357 if (!(msg->fp = safe_fopen (path, "w+"))) {
358 mem_free (&cache->path);
362 /* mark this header as currently inactive so the command handler won't
363 * also try to update it. HACK until all this code can be moved into the
367 snprintf (buf, sizeof (buf), "UID FETCH %u %s", HEADER_DATA (h)->uid,
368 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
369 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") : "RFC822"));
371 imap_cmd_start (idata, buf);
373 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
377 pc = imap_next_word (pc);
378 pc = imap_next_word (pc);
380 if (!ascii_strncasecmp ("FETCH", pc, 5)) {
382 pc = imap_next_word (pc);
385 if (ascii_strncasecmp ("UID", pc, 3) == 0) {
386 pc = imap_next_word (pc);
388 if (uid != HEADER_DATA (h)->uid)
390 ("The message index is incorrect. Try reopening the mailbox."));
392 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
393 (ascii_strncasecmp ("BODY[]", pc, 6) == 0)) {
394 pc = imap_next_word (pc);
395 if (imap_get_literal_count (pc, &bytes) < 0) {
396 imap_error ("imap_fetch_message()", buf);
400 bar.msg = _("Fetching message...");
401 mutt_progress_bar (&bar, 0);
402 if (imap_read_literal (msg->fp, idata, bytes, &bar) < 0)
404 /* pick up trailing line */
405 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
411 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
412 * change (eg from \Unseen to \Seen).
413 * Uncommitted changes in mutt take precedence. If we decide to
414 * incrementally update flags later, this won't stop us syncing */
415 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed) {
416 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
422 while (rc == IMAP_CMD_CONTINUE);
424 /* see comment before command start. */
428 if (ferror (msg->fp)) {
429 mutt_perror (cache->path);
433 if (rc != IMAP_CMD_OK)
436 if (!fetched || !imap_code (idata->cmd.buf))
439 /* Update the header information. Previously, we only downloaded a
440 * portion of the headers, those required for the main display.
443 /* It may be that the Status header indicates a message is read, but the
444 * IMAP server doesn't know the message has been \Seen. So we capture
445 * the server's notion of 'read' and if it differs from the message info
446 * picked up in mutt_read_rfc822_header, we mark the message (and context
447 * changed). Another possiblity: ignore Status on IMAP?*/
449 newenv = mutt_read_rfc822_header (msg->fp, h, 0, 0);
450 mutt_merge_envelopes(h->env, &newenv);
452 /* see above. We want the new status in h->read, so we unset it manually
453 * and let mutt_set_flag set it correctly, updating context. */
454 if (read != h->read) {
456 mutt_set_flag (ctx, h, M_NEW, read);
460 fgets (buf, sizeof (buf), msg->fp);
461 while (!feof (msg->fp)) {
463 fgets (buf, sizeof (buf), msg->fp);
466 h->content->length = ftell (msg->fp) - h->content->offset;
468 /* This needs to be done in case this is a multipart message */
469 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
470 h->security = crypt_query (h->content);
479 safe_fclose (&msg->fp);
481 unlink (cache->path);
482 mem_free (&cache->path);
488 int imap_append_message (CONTEXT * ctx, MESSAGE * msg)
492 char buf[LONG_STRING];
493 char mbox[LONG_STRING];
494 char mailbox[LONG_STRING];
502 idata = (IMAP_DATA *) ctx->data;
504 if (imap_parse_path (ctx->path, &mx))
507 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
509 if ((fp = fopen (msg->path, "r")) == NULL) {
510 mutt_perror (msg->path);
514 /* currently we set the \Seen flag on all messages, but probably we
515 * should scan the message Status header for flag info. Since we're
516 * already rereading the whole file for length it isn't any more
517 * expensive (it'd be nice if we had the file size passed in already
518 * by the code that writes the file, but that's a lot of changes.
519 * Ideally we'd have a HEADER structure with flag info here... */
520 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
521 if (c == '\n' && last != '\r')
528 bar.msg = _("Uploading message...");
530 mutt_progress_bar (&bar, 0);
532 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
533 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) {%lu}", mbox,
534 msg->flags.read ? "\\Seen" : "",
535 msg->flags.read && (msg->flags.replied
536 || msg->flags.flagged) ? " " : "",
537 msg->flags.replied ? "\\Answered" : "", msg->flags.replied
538 && msg->flags.flagged ? " " : "",
539 msg->flags.flagged ? "\\Flagged" : "", (unsigned long) len);
541 imap_cmd_start (idata, buf);
544 rc = imap_cmd_step (idata);
545 while (rc == IMAP_CMD_CONTINUE);
547 if (rc != IMAP_CMD_RESPOND) {
550 debug_print (1, ("command failed: %s\n", idata->cmd.buf));
552 pc = idata->cmd.buf + SEQLEN;
554 pc = imap_next_word (pc);
555 mutt_error ("%s", pc);
561 for (last = EOF, sent = len = 0; (c = fgetc (fp)) != EOF; last = c) {
562 if (c == '\n' && last != '\r')
567 if (len > sizeof (buf) - 3) {
569 flush_buffer (buf, &len, idata->conn);
570 mutt_progress_bar (&bar, sent);
575 flush_buffer (buf, &len, idata->conn);
577 mutt_socket_write (idata->conn, "\r\n");
581 rc = imap_cmd_step (idata);
582 while (rc == IMAP_CMD_CONTINUE);
584 if (!imap_code (idata->cmd.buf)) {
587 debug_print (1, ("command failed: %s\n", idata->cmd.buf));
588 pc = idata->cmd.buf + SEQLEN;
590 pc = imap_next_word (pc);
591 mutt_error ("%s", pc);
604 /* imap_copy_messages: use server COPY command to copy messages to another
609 * 1: non-fatal error - try fetch/append */
610 int imap_copy_messages (CONTEXT * ctx, HEADER * h, char *dest, int delete)
613 BUFFER cmd, sync_cmd;
615 char mbox[LONG_STRING];
616 char mmbox[LONG_STRING];
620 int err_continue = M_NO;
622 idata = (IMAP_DATA *) ctx->data;
624 if (imap_parse_path (dest, &mx)) {
625 debug_print (1, ("bad destination %s\n", dest));
629 /* check that the save-to folder is in the same account */
630 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account))) {
631 debug_print (3, ("%s not same server as %s\n", dest, ctx->path));
635 if (h && h->attach_del) {
636 debug_print (3, ("Message contains attachments to be deleted\n"));
640 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
642 memset (&sync_cmd, 0, sizeof (sync_cmd));
643 memset (&cmd, 0, sizeof (cmd));
644 mutt_buffer_addstr (&cmd, "UID COPY ");
646 /* Null HEADER* means copy tagged messages */
648 /* if any messages have attachments to delete, fall through to FETCH
649 * and APPEND. TODO: Copy what we can with COPY, fall through for the
651 for (n = 0; n < ctx->msgcount; n++) {
652 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del) {
653 debug_print (3, ("Message contains attachments to be deleted\n"));
657 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
658 ctx->hdrs[n]->changed)
660 rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
663 debug_print (1, ("could not sync\n"));
669 rc = imap_make_msg_set (idata, &cmd, M_TAG, 0);
671 debug_print (1, ("No messages tagged\n"));
674 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
677 mutt_message (_("Copying message %d to %s..."), h->index + 1, mbox);
678 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
679 mutt_buffer_addstr (&cmd, uid);
681 if (h->active && h->changed)
683 rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
686 debug_print (1, ("could not sync\n"));
692 /* let's get it on */
693 mutt_buffer_addstr (&cmd, " ");
694 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
695 mutt_buffer_addstr (&cmd, mmbox);
697 rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
699 /* bail out if command failed for reasons other than nonexistent target */
700 if (ascii_strncasecmp
701 (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11)) {
702 imap_error ("imap_copy_messages", idata->cmd.buf);
705 debug_print (2, ("server suggests TRYCREATE\n"));
706 snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
707 if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1) {
711 if (imap_create_mailbox (idata, mbox) < 0)
715 rc = imap_exec (idata, cmd.data, 0);
718 imap_error ("imap_copy_messages", idata->cmd.buf);
725 for (n = 0; n < ctx->msgcount; n++) {
726 if (ctx->hdrs[n]->tagged) {
727 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
728 mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
729 if (option (OPTDELETEUNTAG))
730 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
734 mutt_set_flag (ctx, h, M_DELETE, 1);
735 mutt_set_flag (ctx, h, M_APPENDED, 1);
736 if (option (OPTDELETEUNTAG))
737 mutt_set_flag (ctx, h, M_TAG, 0);
742 mem_free (&cmd.data);
744 mem_free (&sync_cmd.data);
750 mem_free (&cmd.data);
752 mem_free (&sync_cmd.data);
757 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
758 * appear in the folder flags list. Why wouldn't they? */
759 void imap_add_keywords (char *s, HEADER * h, LIST * mailbox_flags,
764 if (!mailbox_flags || !HEADER_DATA (h) || !HEADER_DATA (h)->keywords)
767 keywords = HEADER_DATA (h)->keywords->next;
770 if (msg_has_flag (mailbox_flags, keywords->data)) {
771 str_cat (s, slen, keywords->data);
772 str_cat (s, slen, " ");
774 keywords = keywords->next;
778 /* imap_free_header_data: free IMAP_HEADER structure */
779 void imap_free_header_data (void **data)
781 /* this should be safe even if the list wasn't used */
782 mutt_free_list (&(((IMAP_HEADER_DATA *) * data)->keywords));
787 /* imap_set_flags: fill out the message header according to the flags from
788 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
789 char *imap_set_flags (IMAP_DATA * idata, HEADER * h, char *s)
791 CONTEXT *ctx = idata->ctx;
793 unsigned char readonly;
795 memset (&newh, 0, sizeof (newh));
796 newh.data = mem_calloc (1, sizeof (IMAP_HEADER_DATA));
798 debug_print (2, ("parsing FLAGS\n"));
799 if ((s = msg_parse_flags (&newh, s)) == NULL) {
800 mem_free (&newh.data);
804 /* YAUH (yet another ugly hack): temporarily set context to
805 * read-write even if it's read-only, so *server* updates of
806 * flags can be processed by mutt_set_flag. ctx->changed must
807 * be restored afterwards */
808 readonly = ctx->readonly;
811 mutt_set_flag (ctx, h, M_NEW, !(newh.read || newh.old));
812 mutt_set_flag (ctx, h, M_OLD, newh.old);
813 mutt_set_flag (ctx, h, M_READ, newh.read);
814 mutt_set_flag (ctx, h, M_DELETE, newh.deleted);
815 mutt_set_flag (ctx, h, M_FLAG, newh.flagged);
816 mutt_set_flag (ctx, h, M_REPLIED, newh.replied);
818 /* this message is now definitively *not* changed (mutt_set_flag
819 * marks things changed as a side-effect) */
821 ctx->changed &= ~readonly;
822 ctx->readonly = readonly;
824 mutt_free_list (&(HEADER_DATA (h)->keywords));
825 HEADER_DATA (h)->keywords = newh.data->keywords;
826 mem_free (&newh.data);
832 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
833 * Expects string beginning with * n FETCH.
836 * -1 if the string is not a fetch response
837 * -2 if the string is a corrupt fetch response */
838 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
843 int rc = -1; /* default now is that string isn't FETCH response */
845 idata = (IMAP_DATA *) ctx->data;
850 /* skip to message number */
851 buf = imap_next_word (buf);
855 buf = imap_next_word (buf);
856 if (ascii_strncasecmp ("FETCH", buf, 5))
859 rc = -2; /* we've got a FETCH response, for better or worse */
860 if (!(buf = strchr (buf, '(')))
864 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
865 * read header lines and call it again. Silly. */
866 if (msg_parse_fetch (h, buf) != -2)
869 if (imap_get_literal_count (buf, &bytes) == 0) {
870 imap_read_literal (fp, idata, bytes, NULL);
872 /* we may have other fields of the FETCH _after_ the literal
873 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
874 * This all has to go - we should accept literals and nonliterals
875 * interchangeably at any time. */
876 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
879 if (msg_parse_fetch (h, idata->cmd.buf) == -1)
883 rc = 0; /* success */
885 /* subtract headers from message size - unfortunately only the subset of
886 * headers we've requested. */
887 h->content_length -= bytes;
893 static size_t imap_hcache_keylen (const char *fn)
898 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
899 * Expects string beginning with * n FETCH.
902 * -1 if the string is not a fetch response
903 * -2 if the string is a corrupt fetch response */
904 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
908 int rc = -1; /* default now is that string isn't FETCH response */
910 idata = (IMAP_DATA *) ctx->data;
915 /* skip to message number */
916 buf = imap_next_word (buf);
920 buf = imap_next_word (buf);
921 if (ascii_strncasecmp ("FETCH", buf, 5))
924 rc = -2; /* we've got a FETCH response, for better or worse */
925 if (!(buf = strchr (buf, '(')))
929 if (msg_parse_fetch (h, buf) < 0) {
933 if (!(buf = strchr (buf, ')')))
939 #endif /* USE_HCACHE */
942 /* msg_has_flag: do a caseless comparison of the flag against a flag list,
943 * return 1 if found or flag list has '\*', 0 otherwise */
944 static int msg_has_flag (LIST * flag_list, const char *flag)
949 flag_list = flag_list->next;
951 if (!ascii_strncasecmp (flag_list->data, flag, str_len (flag_list->data)))
954 flag_list = flag_list->next;
960 /* msg_parse_fetch: handle headers returned from header fetch */
961 static int msg_parse_fetch (IMAP_HEADER * h, char *s)
963 char tmp[SHORT_STRING];
972 if (ascii_strncasecmp ("FLAGS", s, 5) == 0) {
973 if ((s = msg_parse_flags (h, s)) == NULL)
976 else if (ascii_strncasecmp ("UID", s, 3) == 0) {
979 h->data->uid = (unsigned int) atoi (s);
981 s = imap_next_word (s);
983 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0) {
987 debug_print (1, ("bogus INTERNALDATE entry: %s\n", s));
992 while (*s && *s != '\"')
996 s++; /* skip past the trailing " */
998 h->received = imap_parse_date (tmp);
1000 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0) {
1004 while (isdigit ((unsigned char) *s))
1007 h->content_length = atoi (tmp);
1009 else if (!ascii_strncasecmp ("BODY", s, 4) ||
1010 !ascii_strncasecmp ("RFC822.HEADER", s, 13)) {
1011 /* handle above, in msg_fetch_header */
1015 s++; /* end of request */
1017 /* got something i don't understand */
1018 imap_error ("msg_parse_fetch", s);
1026 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1027 static char *msg_parse_flags (IMAP_HEADER * h, char *s)
1031 /* sanity-check string */
1032 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
1033 debug_print (1, ("not a FLAGS response: %s\n", s));
1039 debug_print (1, ("bogus FLAGS response: %s\n", s));
1045 while (*s && *s != ')') {
1046 if (ascii_strncasecmp ("\\deleted", s, 8) == 0) {
1050 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0) {
1054 else if (ascii_strncasecmp ("\\answered", s, 9) == 0) {
1058 else if (ascii_strncasecmp ("\\seen", s, 5) == 0) {
1062 else if (ascii_strncasecmp ("\\recent", s, 5) == 0) {
1067 /* store custom flags as well */
1069 char *flag_word = s;
1071 if (!h->data->keywords)
1072 h->data->keywords = mutt_new_list ();
1074 while (*s && !ISSPACE (*s) && *s != ')')
1078 mutt_add_list (h->data->keywords, flag_word);
1084 /* wrap up, or note bad flags response */
1086 /* if a message is neither seen nor recent, it is OLD. */
1087 if (option (OPTMARKOLD) && !recent && !(h->read))
1092 debug_print (1, ("Unterminated FLAGS response: %s\n", s));
1099 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn)
1102 mutt_socket_write (conn, buf);