2 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
3 * Copyright (C) 1999-2009 Brendan Cully <brendan@kublai.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 /* message parsing/updating functions */
32 #include "imap_private.h"
46 static FILE* msg_cache_get (IMAP_DATA* idata, HEADER* h);
47 static FILE* msg_cache_put (IMAP_DATA* idata, HEADER* h);
48 static int msg_cache_commit (IMAP_DATA* idata, HEADER* h);
50 static void flush_buffer(char* buf, size_t* len, CONNECTION* conn);
51 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf,
53 static int msg_parse_fetch (IMAP_HEADER* h, char* s);
54 static char* msg_parse_flags (IMAP_HEADER* h, char* s);
57 * Changed to read many headers instead of just one. It will return the
58 * msgno of the last message read. It will return a value other than
59 * msgend if mail comes in while downloading headers (in theory).
61 int imap_read_headers (IMAP_DATA* idata, int msgbegin, int msgend)
64 char buf[LONG_STRING];
67 char tempfile[_POSIX_PATH_MAX];
71 int rc, mfhrc, oldmsgcount;
74 const char *want_headers = "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO LINES LIST-POST X-LABEL";
78 unsigned int *uid_validity = NULL;
79 unsigned int *puidnext = NULL;
80 unsigned int uidnext = 0;
82 #endif /* USE_HCACHE */
86 if (mutt_bit_isset (idata->capabilities,IMAP4REV1))
88 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s%s%s)]",
89 want_headers, ImapHeaders ? " " : "", ImapHeaders ? ImapHeaders : "");
91 else if (mutt_bit_isset (idata->capabilities,IMAP4))
93 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s%s%s)",
94 want_headers, ImapHeaders ? " " : "", ImapHeaders ? ImapHeaders : "");
97 { /* Unable to fetch headers for lower versions */
98 mutt_error _("Unable to fetch headers from this IMAP server version.");
99 mutt_sleep (2); /* pause a moment to let the user see the error */
103 /* instead of downloading all headers and then parsing them, we parse them
104 * as they come in. */
105 mutt_mktemp (tempfile);
106 if (!(fp = safe_fopen (tempfile, "w+")))
108 mutt_error (_("Could not create temporary file %s"), tempfile);
114 /* make sure context has room to hold the mailbox */
115 while ((msgend) >= idata->ctx->hdrmax)
116 mx_alloc_memory (idata->ctx);
118 oldmsgcount = ctx->msgcount;
119 idata->reopen &= ~(IMAP_REOPEN_ALLOW|IMAP_NEWMAIL_PENDING);
120 idata->newMailCount = 0;
123 idata->hcache = imap_hcache_open (idata, NULL);
125 if (idata->hcache && !msgbegin)
127 uid_validity = mutt_hcache_fetch_raw (idata->hcache, "/UIDVALIDITY", imap_hcache_keylen);
128 puidnext = mutt_hcache_fetch_raw (idata->hcache, "/UIDNEXT", imap_hcache_keylen);
134 if (uid_validity && uidnext && *uid_validity == idata->uid_validity)
136 FREE (&uid_validity);
140 mutt_progress_init (&progress, _("Evaluating cache..."),
141 M_PROGRESS_MSG, ReadInc, msgend + 1);
143 snprintf (buf, sizeof (buf),
144 "UID FETCH 1:%u (UID FLAGS)", uidnext - 1);
146 imap_cmd_start (idata, buf);
148 rc = IMAP_CMD_CONTINUE;
149 for (msgno = msgbegin; rc == IMAP_CMD_CONTINUE; msgno++)
151 mutt_progress_update (&progress, msgno + 1, -1);
153 memset (&h, 0, sizeof (h));
154 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
159 rc = imap_cmd_step (idata);
160 if (rc != IMAP_CMD_CONTINUE)
162 /* suppress GCC aliasing warning */
163 imap_free_header_data ((void**) (void*) &h.data);
167 /* hole in the header cache */
171 if ((mfhrc = msg_fetch_header (ctx, &h, idata->buf, NULL)) == -1)
175 imap_free_header_data ((void**) (void*) &h.data);
181 dprint (2, (debugfile, "imap_read_headers: skipping hcache FETCH "
182 "response for unknown message number %d\n", h.sid));
188 ctx->hdrs[idx] = imap_hcache_get (idata, h.data->uid);
191 ctx->hdrs[idx]->index = idx;
192 /* messages which have not been expunged are ACTIVE (borrowed from mh
194 ctx->hdrs[idx]->active = 1;
195 ctx->hdrs[idx]->read = h.data->read;
196 ctx->hdrs[idx]->old = h.data->old;
197 ctx->hdrs[idx]->deleted = h.data->deleted;
198 ctx->hdrs[idx]->flagged = h.data->flagged;
199 ctx->hdrs[idx]->replied = h.data->replied;
200 ctx->hdrs[idx]->changed = h.data->changed;
201 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
202 ctx->hdrs[idx]->data = (void *) (h.data);
205 ctx->size += ctx->hdrs[idx]->content->length;
209 /* bad header in the cache, we'll have to refetch. */
210 dprint (3, (debugfile, "bad cache entry at %d, giving up\n", h.sid - 1));
211 imap_free_header_data((void**) (void*) &h.data);
215 while (rc != IMAP_CMD_OK && mfhrc == -1);
216 if (rc == IMAP_CMD_OK)
218 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
221 imap_free_header_data ((void**) (void*) &h.data);
222 imap_hcache_close (idata);
227 /* could also look for first null header in case hcache is holey */
228 msgbegin = ctx->msgcount;
230 #endif /* USE_HCACHE */
232 mutt_progress_init (&progress, _("Fetching message headers..."),
233 M_PROGRESS_MSG, ReadInc, msgend + 1);
235 for (msgno = msgbegin; msgno <= msgend ; msgno++)
237 mutt_progress_update (&progress, msgno + 1, -1);
239 /* we may get notification of new mail while fetching headers */
240 if (msgno + 1 > fetchlast)
242 fetchlast = msgend + 1;
244 snprintf (buf, sizeof (buf),
245 "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)", msgno + 1,
248 imap_cmd_start (idata, buf);
252 memset (&h, 0, sizeof (h));
253 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
255 /* this DO loop does two things:
256 * 1. handles untagged messages, so we can try again on the same msg
257 * 2. fetches the tagged response at the end of the last message.
263 rc = imap_cmd_step (idata);
264 if (rc != IMAP_CMD_CONTINUE)
267 if ((mfhrc = msg_fetch_header (ctx, &h, idata->buf, fp)) == -1)
274 dprint (2, (debugfile, "msg_fetch_header: ignoring fetch response with no body\n"));
279 /* make sure we don't get remnants from older larger message headers */
285 dprint (1, (debugfile, "imap_read_headers: skipping FETCH response for "
286 "unknown message number %d\n", h.sid));
290 /* May receive FLAGS updates in a separate untagged response (#2935) */
291 if (idx < ctx->msgcount)
293 dprint (2, (debugfile, "imap_read_headers: message %d is not new\n",
298 ctx->hdrs[idx] = mutt_new_header ();
300 ctx->hdrs[idx]->index = h.sid - 1;
301 /* messages which have not been expunged are ACTIVE (borrowed from mh
303 ctx->hdrs[idx]->active = 1;
304 ctx->hdrs[idx]->read = h.data->read;
305 ctx->hdrs[idx]->old = h.data->old;
306 ctx->hdrs[idx]->deleted = h.data->deleted;
307 ctx->hdrs[idx]->flagged = h.data->flagged;
308 ctx->hdrs[idx]->replied = h.data->replied;
309 ctx->hdrs[idx]->changed = h.data->changed;
310 ctx->hdrs[idx]->received = h.received;
311 ctx->hdrs[idx]->data = (void *) (h.data);
313 if (maxuid < h.data->uid)
314 maxuid = h.data->uid;
317 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
318 * on h.received being set */
319 ctx->hdrs[idx]->env = mutt_read_rfc822_header (fp, ctx->hdrs[idx],
321 /* content built as a side-effect of mutt_read_rfc822_header */
322 ctx->hdrs[idx]->content->length = h.content_length;
323 ctx->size += h.content_length;
326 imap_hcache_put (idata, ctx->hdrs[idx]);
327 #endif /* USE_HCACHE */
331 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
332 ((msgno + 1) >= fetchlast)));
334 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
337 imap_free_header_data ((void**) (void*) &h.data);
339 imap_hcache_close (idata);
345 /* in case we get new mail while fetching the headers */
346 if (idata->reopen & IMAP_NEWMAIL_PENDING)
348 msgend = idata->newMailCount - 1;
349 while ((msgend) >= ctx->hdrmax)
350 mx_alloc_memory (ctx);
351 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
352 idata->newMailCount = 0;
356 if (maxuid && (status = imap_mboxcache_get (idata, idata->mailbox, 0)))
357 status->uidnext = maxuid + 1;
360 mutt_hcache_store_raw (idata->hcache, "/UIDVALIDITY", &idata->uid_validity,
361 sizeof (idata->uid_validity), imap_hcache_keylen);
362 if (maxuid && idata->uidnext < maxuid + 1)
364 dprint (2, (debugfile, "Overriding UIDNEXT: %u -> %u\n", idata->uidnext, maxuid + 1));
365 idata->uidnext = maxuid + 1;
367 if (idata->uidnext > 1)
368 mutt_hcache_store_raw (idata->hcache, "/UIDNEXT", &idata->uidnext,
369 sizeof (idata->uidnext), imap_hcache_keylen);
371 imap_hcache_close (idata);
372 #endif /* USE_HCACHE */
376 if (ctx->msgcount > oldmsgcount)
378 mx_alloc_memory(ctx);
379 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
382 idata->reopen |= IMAP_REOPEN_ALLOW;
386 int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
391 char buf[LONG_STRING];
392 char path[_POSIX_PATH_MAX];
395 progress_t progressbar;
401 /* Sam's weird courier server returns an OK response even when FETCH
402 * fails. Thanks Sam. */
405 idata = (IMAP_DATA*) ctx->data;
406 h = ctx->hdrs[msgno];
408 if ((msg->fp = msg_cache_get (idata, h)))
410 if (HEADER_DATA(h)->parsed)
416 /* we still do some caching even if imap_cachedir is unset */
417 /* see if we already have the message in our cache */
418 cacheno = HEADER_DATA(h)->uid % IMAP_CACHE_LEN;
419 cache = &idata->cache[cacheno];
423 /* don't treat cache errors as fatal, just fall back. */
424 if (cache->uid == HEADER_DATA(h)->uid &&
425 (msg->fp = fopen (cache->path, "r")))
429 unlink (cache->path);
435 mutt_message _("Fetching message...");
437 if (!(msg->fp = msg_cache_put (idata, h)))
439 cache->uid = HEADER_DATA(h)->uid;
441 cache->path = safe_strdup (path);
442 if (!(msg->fp = safe_fopen (path, "w+")))
449 /* mark this header as currently inactive so the command handler won't
450 * also try to update it. HACK until all this code can be moved into the
454 snprintf (buf, sizeof (buf), "UID FETCH %u %s", HEADER_DATA(h)->uid,
455 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
456 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") :
459 imap_cmd_start (idata, buf);
462 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
466 pc = imap_next_word (pc);
467 pc = imap_next_word (pc);
469 if (!ascii_strncasecmp ("FETCH", pc, 5))
473 pc = imap_next_word (pc);
476 if (ascii_strncasecmp ("UID", pc, 3) == 0)
478 pc = imap_next_word (pc);
480 if (uid != HEADER_DATA(h)->uid)
481 mutt_error (_("The message index is incorrect. Try reopening the mailbox."));
483 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
484 (ascii_strncasecmp ("BODY[]", pc, 6) == 0))
486 pc = imap_next_word (pc);
487 if (imap_get_literal_count(pc, &bytes) < 0)
489 imap_error ("imap_fetch_message()", buf);
492 mutt_progress_init (&progressbar, _("Fetching message..."),
493 M_PROGRESS_SIZE, NetInc, bytes);
494 if (imap_read_literal (msg->fp, idata, bytes, &progressbar) < 0)
496 /* pick up trailing line */
497 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
503 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
504 * change (eg from \Unseen to \Seen).
505 * Uncommitted changes in mutt take precedence. If we decide to
506 * incrementally update flags later, this won't stop us syncing */
507 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed)
509 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
515 while (rc == IMAP_CMD_CONTINUE);
517 /* see comment before command start. */
521 if (ferror (msg->fp))
523 mutt_perror (cache->path);
527 if (rc != IMAP_CMD_OK)
530 if (!fetched || !imap_code (idata->buf))
533 msg_cache_commit (idata, h);
536 /* Update the header information. Previously, we only downloaded a
537 * portion of the headers, those required for the main display.
540 /* It may be that the Status header indicates a message is read, but the
541 * IMAP server doesn't know the message has been \Seen. So we capture
542 * the server's notion of 'read' and if it differs from the message info
543 * picked up in mutt_read_rfc822_header, we mark the message (and context
544 * changed). Another possiblity: ignore Status on IMAP?*/
546 newenv = mutt_read_rfc822_header (msg->fp, h, 0, 0);
547 mutt_merge_envelopes(h->env, &newenv);
549 /* see above. We want the new status in h->read, so we unset it manually
550 * and let mutt_set_flag set it correctly, updating context. */
554 mutt_set_flag (ctx, h, M_NEW, read);
558 fgets (buf, sizeof (buf), msg->fp);
559 while (!feof (msg->fp))
562 fgets (buf, sizeof (buf), msg->fp);
565 h->content->length = ftell (msg->fp) - h->content->offset;
567 /* This needs to be done in case this is a multipart message */
568 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
569 h->security = crypt_query (h->content);
574 HEADER_DATA(h)->parsed = 1;
579 safe_fclose (&msg->fp);
580 imap_cache_del (idata, h);
583 unlink (cache->path);
590 int imap_append_message (CONTEXT *ctx, MESSAGE *msg)
594 char buf[LONG_STRING];
595 char mbox[LONG_STRING];
596 char mailbox[LONG_STRING];
598 progress_t progressbar;
604 idata = (IMAP_DATA*) ctx->data;
606 if (imap_parse_path (ctx->path, &mx))
609 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
611 strfcpy (mailbox, "INBOX", sizeof (mailbox));
613 if ((fp = fopen (msg->path, "r")) == NULL)
615 mutt_perror (msg->path);
619 /* currently we set the \Seen flag on all messages, but probably we
620 * should scan the message Status header for flag info. Since we're
621 * already rereading the whole file for length it isn't any more
622 * expensive (it'd be nice if we had the file size passed in already
623 * by the code that writes the file, but that's a lot of changes.
624 * Ideally we'd have a HEADER structure with flag info here... */
625 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
627 if(c == '\n' && last != '\r')
634 mutt_progress_init (&progressbar, _("Uploading message..."),
635 M_PROGRESS_SIZE, NetInc, len);
637 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
638 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) {%lu}", mbox,
639 msg->flags.read ? "\\Seen" : "",
640 msg->flags.read && (msg->flags.replied || msg->flags.flagged) ? " " : "",
641 msg->flags.replied ? "\\Answered" : "",
642 msg->flags.replied && msg->flags.flagged ? " " : "",
643 msg->flags.flagged ? "\\Flagged" : "",
644 (unsigned long) len);
646 imap_cmd_start (idata, buf);
649 rc = imap_cmd_step (idata);
650 while (rc == IMAP_CMD_CONTINUE);
652 if (rc != IMAP_CMD_RESPOND)
656 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
659 pc = idata->buf + SEQLEN;
661 pc = imap_next_word (pc);
662 mutt_error ("%s", pc);
668 for (last = EOF, sent = len = 0; (c = fgetc(fp)) != EOF; last = c)
670 if (c == '\n' && last != '\r')
675 if (len > sizeof(buf) - 3)
678 flush_buffer(buf, &len, idata->conn);
679 mutt_progress_update (&progressbar, sent, -1);
684 flush_buffer(buf, &len, idata->conn);
686 mutt_socket_write (idata->conn, "\r\n");
690 rc = imap_cmd_step (idata);
691 while (rc == IMAP_CMD_CONTINUE);
693 if (!imap_code (idata->buf))
697 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
699 pc = idata->buf + SEQLEN;
701 pc = imap_next_word (pc);
702 mutt_error ("%s", pc);
715 /* imap_copy_messages: use server COPY command to copy messages to another
720 * 1: non-fatal error - try fetch/append */
721 int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
724 BUFFER cmd, sync_cmd;
725 char mbox[LONG_STRING];
726 char mmbox[LONG_STRING];
727 char prompt[LONG_STRING];
731 int err_continue = M_NO;
734 idata = (IMAP_DATA*) ctx->data;
736 if (imap_parse_path (dest, &mx))
738 dprint (1, (debugfile, "imap_copy_messages: bad destination %s\n", dest));
742 /* check that the save-to folder is in the same account */
743 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account)))
745 dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n",
750 if (h && h->attach_del)
752 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
756 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
758 strfcpy (mbox, "INBOX", sizeof (mbox));
759 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
761 /* loop in case of TRYCREATE */
764 memset (&sync_cmd, 0, sizeof (sync_cmd));
765 memset (&cmd, 0, sizeof (cmd));
767 /* Null HEADER* means copy tagged messages */
770 /* if any messages have attachments to delete, fall through to FETCH
771 * and APPEND. TODO: Copy what we can with COPY, fall through for the
773 for (n = 0; n < ctx->msgcount; n++)
775 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
777 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
781 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
782 ctx->hdrs[n]->changed)
784 rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
787 dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
793 rc = imap_exec_msgset (idata, "UID COPY", mmbox, M_TAG, 0, 0);
796 dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
802 dprint (1, (debugfile, "could not queue copy\n"));
806 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
810 mutt_message (_("Copying message %d to %s..."), h->index+1, mbox);
811 mutt_buffer_printf (&cmd, "UID COPY %u %s", HEADER_DATA (h)->uid, mmbox);
813 if (h->active && h->changed)
815 rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
818 dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
822 if ((rc = imap_exec (idata, cmd.data, IMAP_CMD_QUEUE)) < 0)
824 dprint (1, (debugfile, "could not queue copy\n"));
829 /* let's get it on */
830 rc = imap_exec (idata, NULL, IMAP_CMD_FAIL_OK);
835 dprint (1, (debugfile, "Already tried to create mailbox %s\n", mbox));
838 /* bail out if command failed for reasons other than nonexistent target */
839 if (ascii_strncasecmp (imap_get_qualifier (idata->buf), "[TRYCREATE]", 11))
841 dprint (3, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
842 snprintf (prompt, sizeof (prompt), _("Create %s?"), mbox);
843 if (option (OPTCONFIRMCREATE) && mutt_yesorno (prompt, 1) < 1)
848 if (imap_create_mailbox (idata, mbox) < 0)
857 imap_error ("imap_copy_messages", idata->buf);
865 for (n = 0; n < ctx->msgcount; n++)
867 if (ctx->hdrs[n]->tagged)
869 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
870 if (option (OPTDELETEUNTAG))
871 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
876 mutt_set_flag (ctx, h, M_DELETE, 1);
877 if (option (OPTDELETEUNTAG))
878 mutt_set_flag (ctx, h, M_TAG, 0);
888 FREE (&sync_cmd.data);
891 return rc < 0 ? -1 : rc;
894 static body_cache_t *msg_cache_open (IMAP_DATA *idata)
896 char mailbox[_POSIX_PATH_MAX];
899 return idata->bcache;
901 imap_cachepath (idata, idata->mailbox, mailbox, sizeof (mailbox));
903 return mutt_bcache_open (&idata->conn->account, mailbox);
906 static FILE* msg_cache_get (IMAP_DATA* idata, HEADER* h)
908 char id[_POSIX_PATH_MAX];
913 idata->bcache = msg_cache_open (idata);
914 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
915 return mutt_bcache_get (idata->bcache, id);
918 static FILE* msg_cache_put (IMAP_DATA* idata, HEADER* h)
920 char id[_POSIX_PATH_MAX];
925 idata->bcache = msg_cache_open (idata);
926 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
927 return mutt_bcache_put (idata->bcache, id, 1);
930 static int msg_cache_commit (IMAP_DATA* idata, HEADER* h)
932 char id[_POSIX_PATH_MAX];
937 idata->bcache = msg_cache_open (idata);
938 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
940 return mutt_bcache_commit (idata->bcache, id);
943 int imap_cache_del (IMAP_DATA* idata, HEADER* h)
945 char id[_POSIX_PATH_MAX];
950 idata->bcache = msg_cache_open (idata);
951 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
952 return mutt_bcache_del (idata->bcache, id);
955 static int msg_cache_clean_cb (const char* id, body_cache_t* bcache, void* data)
957 unsigned int uv, uid, n;
958 IMAP_DATA* idata = (IMAP_DATA*)data;
960 if (sscanf (id, "%u-%u", &uv, &uid) != 2)
964 if (uv != idata->uid_validity)
965 mutt_bcache_del (bcache, id);
967 /* TODO: presort UIDs, walk in order */
968 for (n = 0; n < idata->ctx->msgcount; n++)
970 if (uid == HEADER_DATA(idata->ctx->hdrs[n])->uid)
973 mutt_bcache_del (bcache, id);
978 int imap_cache_clean (IMAP_DATA* idata)
980 idata->bcache = msg_cache_open (idata);
981 mutt_bcache_list (idata->bcache, msg_cache_clean_cb, idata);
986 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
987 * appear in the folder flags list. Why wouldn't they? */
988 void imap_add_keywords (char* s, HEADER* h, LIST* mailbox_flags, size_t slen)
992 if (!mailbox_flags || !HEADER_DATA(h) || !HEADER_DATA(h)->keywords)
995 keywords = HEADER_DATA(h)->keywords->next;
999 if (imap_has_flag (mailbox_flags, keywords->data))
1001 safe_strcat (s, slen, keywords->data);
1002 safe_strcat (s, slen, " ");
1004 keywords = keywords->next;
1008 /* imap_free_header_data: free IMAP_HEADER structure */
1009 void imap_free_header_data (void** data)
1011 /* this should be safe even if the list wasn't used */
1012 mutt_free_list (&(((IMAP_HEADER_DATA*) *data)->keywords));
1014 FREE (data); /* __FREE_CHECKED__ */
1017 /* imap_set_flags: fill out the message header according to the flags from
1018 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
1019 char* imap_set_flags (IMAP_DATA* idata, HEADER* h, char* s)
1021 CONTEXT* ctx = idata->ctx;
1023 IMAP_HEADER_DATA* hd;
1024 unsigned char readonly;
1026 memset (&newh, 0, sizeof (newh));
1030 dprint (2, (debugfile, "imap_fetch_message: parsing FLAGS\n"));
1031 if ((s = msg_parse_flags (&newh, s)) == NULL)
1034 /* YAUH (yet another ugly hack): temporarily set context to
1035 * read-write even if it's read-only, so *server* updates of
1036 * flags can be processed by mutt_set_flag. ctx->changed must
1037 * be restored afterwards */
1038 readonly = ctx->readonly;
1041 mutt_set_flag (ctx, h, M_NEW, !(hd->read || hd->old));
1042 mutt_set_flag (ctx, h, M_OLD, hd->old);
1043 mutt_set_flag (ctx, h, M_READ, hd->read);
1044 mutt_set_flag (ctx, h, M_DELETE, hd->deleted);
1045 mutt_set_flag (ctx, h, M_FLAG, hd->flagged);
1046 mutt_set_flag (ctx, h, M_REPLIED, hd->replied);
1048 /* this message is now definitively *not* changed (mutt_set_flag
1049 * marks things changed as a side-effect) */
1051 ctx->changed &= ~readonly;
1052 ctx->readonly = readonly;
1058 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
1059 * Expects string beginning with * n FETCH.
1062 * -1 if the string is not a fetch response
1063 * -2 if the string is a corrupt fetch response */
1064 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf, FILE* fp)
1068 int rc = -1; /* default now is that string isn't FETCH response*/
1070 idata = (IMAP_DATA*) ctx->data;
1075 /* skip to message number */
1076 buf = imap_next_word (buf);
1077 h->sid = atoi (buf);
1079 /* find FETCH tag */
1080 buf = imap_next_word (buf);
1081 if (ascii_strncasecmp ("FETCH", buf, 5))
1084 rc = -2; /* we've got a FETCH response, for better or worse */
1085 if (!(buf = strchr (buf, '(')))
1089 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
1090 * read header lines and call it again. Silly. */
1091 if ((rc = msg_parse_fetch (h, buf)) != -2 || !fp)
1094 if (imap_get_literal_count (buf, &bytes) == 0)
1096 imap_read_literal (fp, idata, bytes, NULL);
1098 /* we may have other fields of the FETCH _after_ the literal
1099 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
1100 * This all has to go - we should accept literals and nonliterals
1101 * interchangeably at any time. */
1102 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
1105 if (msg_parse_fetch (h, idata->buf) == -1)
1109 rc = 0; /* success */
1111 /* subtract headers from message size - unfortunately only the subset of
1112 * headers we've requested. */
1113 h->content_length -= bytes;
1118 /* msg_parse_fetch: handle headers returned from header fetch */
1119 static int msg_parse_fetch (IMAP_HEADER *h, char *s)
1121 char tmp[SHORT_STRING];
1131 if (ascii_strncasecmp ("FLAGS", s, 5) == 0)
1133 if ((s = msg_parse_flags (h, s)) == NULL)
1136 else if (ascii_strncasecmp ("UID", s, 3) == 0)
1140 h->data->uid = (unsigned int) atoi (s);
1142 s = imap_next_word (s);
1144 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0)
1150 dprint (1, (debugfile, "msg_parse_fetch(): bogus INTERNALDATE entry: %s\n", s));
1155 while (*s && *s != '\"')
1159 s++; /* skip past the trailing " */
1161 h->received = imap_parse_date (tmp);
1163 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0)
1168 while (isdigit ((unsigned char) *s))
1171 h->content_length = atoi (tmp);
1173 else if (!ascii_strncasecmp ("BODY", s, 4) ||
1174 !ascii_strncasecmp ("RFC822.HEADER", s, 13))
1176 /* handle above, in msg_fetch_header */
1180 s++; /* end of request */
1183 /* got something i don't understand */
1184 imap_error ("msg_parse_fetch", s);
1192 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1193 static char* msg_parse_flags (IMAP_HEADER* h, char* s)
1195 IMAP_HEADER_DATA* hd = h->data;
1197 /* sanity-check string */
1198 if (ascii_strncasecmp ("FLAGS", s, 5) != 0)
1200 dprint (1, (debugfile, "msg_parse_flags: not a FLAGS response: %s\n",
1208 dprint (1, (debugfile, "msg_parse_flags: bogus FLAGS response: %s\n",
1214 mutt_free_list (&hd->keywords);
1215 hd->deleted = hd->flagged = hd->replied = hd->read = hd->old = 0;
1218 while (*s && *s != ')')
1220 if (ascii_strncasecmp ("\\deleted", s, 8) == 0)
1225 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0)
1230 else if (ascii_strncasecmp ("\\answered", s, 9) == 0)
1235 else if (ascii_strncasecmp ("\\seen", s, 5) == 0)
1240 else if (ascii_strncasecmp ("\\recent", s, 7) == 0)
1242 else if (ascii_strncasecmp ("old", s, 3) == 0)
1249 /* store custom flags as well */
1251 char* flag_word = s;
1254 hd->keywords = mutt_new_list ();
1256 while (*s && !ISSPACE (*s) && *s != ')')
1260 mutt_add_list (hd->keywords, flag_word);
1266 /* wrap up, or note bad flags response */
1271 dprint (1, (debugfile,
1272 "msg_parse_flags: Unterminated FLAGS response: %s\n", s));
1279 static void flush_buffer(char *buf, size_t *len, CONNECTION *conn)
1282 mutt_socket_write_n(conn, buf, *len);