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));
291 ctx->hdrs[idx] = mutt_new_header ();
293 ctx->hdrs[idx]->index = h.sid - 1;
294 /* messages which have not been expunged are ACTIVE (borrowed from mh
296 ctx->hdrs[idx]->active = 1;
297 ctx->hdrs[idx]->read = h.data->read;
298 ctx->hdrs[idx]->old = h.data->old;
299 ctx->hdrs[idx]->deleted = h.data->deleted;
300 ctx->hdrs[idx]->flagged = h.data->flagged;
301 ctx->hdrs[idx]->replied = h.data->replied;
302 ctx->hdrs[idx]->changed = h.data->changed;
303 ctx->hdrs[idx]->received = h.received;
304 ctx->hdrs[idx]->data = (void *) (h.data);
306 if (maxuid < h.data->uid)
307 maxuid = h.data->uid;
310 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
311 * on h.received being set */
312 ctx->hdrs[idx]->env = mutt_read_rfc822_header (fp, ctx->hdrs[idx],
314 /* content built as a side-effect of mutt_read_rfc822_header */
315 ctx->hdrs[idx]->content->length = h.content_length;
316 ctx->size += h.content_length;
319 imap_hcache_put (idata, ctx->hdrs[idx]);
320 #endif /* USE_HCACHE */
324 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
325 ((msgno + 1) >= fetchlast)));
327 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
330 imap_free_header_data ((void**) (void*) &h.data);
332 imap_hcache_close (idata);
338 /* in case we get new mail while fetching the headers */
339 if (idata->reopen & IMAP_NEWMAIL_PENDING)
341 msgend = idata->newMailCount - 1;
342 while ((msgend) >= ctx->hdrmax)
343 mx_alloc_memory (ctx);
344 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
345 idata->newMailCount = 0;
349 if (maxuid && (status = imap_mboxcache_get (idata, idata->mailbox, 0)))
350 status->uidnext = maxuid + 1;
353 mutt_hcache_store_raw (idata->hcache, "/UIDVALIDITY", &idata->uid_validity,
354 sizeof (idata->uid_validity), imap_hcache_keylen);
355 if (maxuid && idata->uidnext < maxuid + 1)
357 dprint (2, (debugfile, "Overriding UIDNEXT: %u -> %u\n", idata->uidnext, maxuid + 1));
358 idata->uidnext = maxuid + 1;
360 if (idata->uidnext > 1)
361 mutt_hcache_store_raw (idata->hcache, "/UIDNEXT", &idata->uidnext,
362 sizeof (idata->uidnext), imap_hcache_keylen);
364 imap_hcache_close (idata);
365 #endif /* USE_HCACHE */
369 if (ctx->msgcount > oldmsgcount)
371 mx_alloc_memory(ctx);
372 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
375 idata->reopen |= IMAP_REOPEN_ALLOW;
379 int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
384 char buf[LONG_STRING];
385 char path[_POSIX_PATH_MAX];
388 progress_t progressbar;
394 /* Sam's weird courier server returns an OK response even when FETCH
395 * fails. Thanks Sam. */
398 idata = (IMAP_DATA*) ctx->data;
399 h = ctx->hdrs[msgno];
401 if ((msg->fp = msg_cache_get (idata, h)))
403 if (HEADER_DATA(h)->parsed)
409 /* we still do some caching even if imap_cachedir is unset */
410 /* see if we already have the message in our cache */
411 cacheno = HEADER_DATA(h)->uid % IMAP_CACHE_LEN;
412 cache = &idata->cache[cacheno];
416 /* don't treat cache errors as fatal, just fall back. */
417 if (cache->uid == HEADER_DATA(h)->uid &&
418 (msg->fp = fopen (cache->path, "r")))
422 unlink (cache->path);
428 mutt_message _("Fetching message...");
430 if (!(msg->fp = msg_cache_put (idata, h)))
432 cache->uid = HEADER_DATA(h)->uid;
434 cache->path = safe_strdup (path);
435 if (!(msg->fp = safe_fopen (path, "w+")))
442 /* mark this header as currently inactive so the command handler won't
443 * also try to update it. HACK until all this code can be moved into the
447 snprintf (buf, sizeof (buf), "UID FETCH %u %s", HEADER_DATA(h)->uid,
448 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
449 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") :
452 imap_cmd_start (idata, buf);
455 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
459 pc = imap_next_word (pc);
460 pc = imap_next_word (pc);
462 if (!ascii_strncasecmp ("FETCH", pc, 5))
466 pc = imap_next_word (pc);
469 if (ascii_strncasecmp ("UID", pc, 3) == 0)
471 pc = imap_next_word (pc);
473 if (uid != HEADER_DATA(h)->uid)
474 mutt_error (_("The message index is incorrect. Try reopening the mailbox."));
476 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
477 (ascii_strncasecmp ("BODY[]", pc, 6) == 0))
479 pc = imap_next_word (pc);
480 if (imap_get_literal_count(pc, &bytes) < 0)
482 imap_error ("imap_fetch_message()", buf);
485 mutt_progress_init (&progressbar, _("Fetching message..."),
486 M_PROGRESS_SIZE, NetInc, bytes);
487 if (imap_read_literal (msg->fp, idata, bytes, &progressbar) < 0)
489 /* pick up trailing line */
490 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
496 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
497 * change (eg from \Unseen to \Seen).
498 * Uncommitted changes in mutt take precedence. If we decide to
499 * incrementally update flags later, this won't stop us syncing */
500 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed)
502 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
508 while (rc == IMAP_CMD_CONTINUE);
510 /* see comment before command start. */
514 if (ferror (msg->fp))
516 mutt_perror (cache->path);
520 if (rc != IMAP_CMD_OK)
523 if (!fetched || !imap_code (idata->buf))
526 msg_cache_commit (idata, h);
529 /* Update the header information. Previously, we only downloaded a
530 * portion of the headers, those required for the main display.
533 /* It may be that the Status header indicates a message is read, but the
534 * IMAP server doesn't know the message has been \Seen. So we capture
535 * the server's notion of 'read' and if it differs from the message info
536 * picked up in mutt_read_rfc822_header, we mark the message (and context
537 * changed). Another possiblity: ignore Status on IMAP?*/
539 newenv = mutt_read_rfc822_header (msg->fp, h, 0, 0);
540 mutt_merge_envelopes(h->env, &newenv);
542 /* see above. We want the new status in h->read, so we unset it manually
543 * and let mutt_set_flag set it correctly, updating context. */
547 mutt_set_flag (ctx, h, M_NEW, read);
551 fgets (buf, sizeof (buf), msg->fp);
552 while (!feof (msg->fp))
555 fgets (buf, sizeof (buf), msg->fp);
558 h->content->length = ftell (msg->fp) - h->content->offset;
560 /* This needs to be done in case this is a multipart message */
561 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
562 h->security = crypt_query (h->content);
567 HEADER_DATA(h)->parsed = 1;
572 safe_fclose (&msg->fp);
573 imap_cache_del (idata, h);
576 unlink (cache->path);
583 int imap_append_message (CONTEXT *ctx, MESSAGE *msg)
587 char buf[LONG_STRING];
588 char mbox[LONG_STRING];
589 char mailbox[LONG_STRING];
591 progress_t progressbar;
597 idata = (IMAP_DATA*) ctx->data;
599 if (imap_parse_path (ctx->path, &mx))
602 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
604 strfcpy (mailbox, "INBOX", sizeof (mailbox));
606 if ((fp = fopen (msg->path, "r")) == NULL)
608 mutt_perror (msg->path);
612 /* currently we set the \Seen flag on all messages, but probably we
613 * should scan the message Status header for flag info. Since we're
614 * already rereading the whole file for length it isn't any more
615 * expensive (it'd be nice if we had the file size passed in already
616 * by the code that writes the file, but that's a lot of changes.
617 * Ideally we'd have a HEADER structure with flag info here... */
618 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
620 if(c == '\n' && last != '\r')
627 mutt_progress_init (&progressbar, _("Uploading message..."),
628 M_PROGRESS_SIZE, NetInc, len);
630 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
631 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) {%lu}", mbox,
632 msg->flags.read ? "\\Seen" : "",
633 msg->flags.read && (msg->flags.replied || msg->flags.flagged) ? " " : "",
634 msg->flags.replied ? "\\Answered" : "",
635 msg->flags.replied && msg->flags.flagged ? " " : "",
636 msg->flags.flagged ? "\\Flagged" : "",
637 (unsigned long) len);
639 imap_cmd_start (idata, buf);
642 rc = imap_cmd_step (idata);
643 while (rc == IMAP_CMD_CONTINUE);
645 if (rc != IMAP_CMD_RESPOND)
649 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
652 pc = idata->buf + SEQLEN;
654 pc = imap_next_word (pc);
655 mutt_error ("%s", pc);
661 for (last = EOF, sent = len = 0; (c = fgetc(fp)) != EOF; last = c)
663 if (c == '\n' && last != '\r')
668 if (len > sizeof(buf) - 3)
671 flush_buffer(buf, &len, idata->conn);
672 mutt_progress_update (&progressbar, sent, -1);
677 flush_buffer(buf, &len, idata->conn);
679 mutt_socket_write (idata->conn, "\r\n");
683 rc = imap_cmd_step (idata);
684 while (rc == IMAP_CMD_CONTINUE);
686 if (!imap_code (idata->buf))
690 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
692 pc = idata->buf + SEQLEN;
694 pc = imap_next_word (pc);
695 mutt_error ("%s", pc);
708 /* imap_copy_messages: use server COPY command to copy messages to another
713 * 1: non-fatal error - try fetch/append */
714 int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
717 BUFFER cmd, sync_cmd;
718 char mbox[LONG_STRING];
719 char mmbox[LONG_STRING];
720 char prompt[LONG_STRING];
724 int err_continue = M_NO;
727 idata = (IMAP_DATA*) ctx->data;
729 if (imap_parse_path (dest, &mx))
731 dprint (1, (debugfile, "imap_copy_messages: bad destination %s\n", dest));
735 /* check that the save-to folder is in the same account */
736 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account)))
738 dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n",
743 if (h && h->attach_del)
745 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
749 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
751 strfcpy (mbox, "INBOX", sizeof (mbox));
752 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
754 /* loop in case of TRYCREATE */
757 memset (&sync_cmd, 0, sizeof (sync_cmd));
758 memset (&cmd, 0, sizeof (cmd));
760 /* Null HEADER* means copy tagged messages */
763 /* if any messages have attachments to delete, fall through to FETCH
764 * and APPEND. TODO: Copy what we can with COPY, fall through for the
766 for (n = 0; n < ctx->msgcount; n++)
768 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
770 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
774 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
775 ctx->hdrs[n]->changed)
777 rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
780 dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
786 rc = imap_exec_msgset (idata, "UID COPY", mmbox, M_TAG, 0, 0);
789 dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
794 dprint (1, (debugfile, "could not queue copy\n"));
798 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
802 mutt_message (_("Copying message %d to %s..."), h->index+1, mbox);
803 mutt_buffer_printf (&cmd, "UID COPY %u %s", HEADER_DATA (h)->uid, mmbox);
805 if (h->active && h->changed)
807 rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
810 dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
814 if ((rc = imap_exec (idata, cmd.data, IMAP_CMD_QUEUE)) < 0)
816 dprint (1, (debugfile, "could not queue copy\n"));
821 /* let's get it on */
822 rc = imap_exec (idata, NULL, IMAP_CMD_FAIL_OK);
827 dprint (1, (debugfile, "Already tried to create mailbox %s\n", mbox));
830 /* bail out if command failed for reasons other than nonexistent target */
831 if (ascii_strncasecmp (imap_get_qualifier (idata->buf), "[TRYCREATE]", 11))
833 dprint (3, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
834 snprintf (prompt, sizeof (prompt), _("Create %s?"), mbox);
835 if (option (OPTCONFIRMCREATE) && mutt_yesorno (prompt, 1) < 1)
840 if (imap_create_mailbox (idata, mbox) < 0)
849 imap_error ("imap_copy_messages", idata->buf);
857 for (n = 0; n < ctx->msgcount; n++)
859 if (ctx->hdrs[n]->tagged)
861 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
862 if (option (OPTDELETEUNTAG))
863 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
868 mutt_set_flag (ctx, h, M_DELETE, 1);
869 if (option (OPTDELETEUNTAG))
870 mutt_set_flag (ctx, h, M_TAG, 0);
877 FREE (&sync_cmd.data);
885 FREE (&sync_cmd.data);
890 static body_cache_t *msg_cache_open (IMAP_DATA *idata)
892 char mailbox[_POSIX_PATH_MAX];
895 return idata->bcache;
897 imap_cachepath (idata, idata->mailbox, mailbox, sizeof (mailbox));
899 return mutt_bcache_open (&idata->conn->account, mailbox);
902 static FILE* msg_cache_get (IMAP_DATA* idata, HEADER* h)
904 char id[_POSIX_PATH_MAX];
909 idata->bcache = msg_cache_open (idata);
910 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
911 return mutt_bcache_get (idata->bcache, id);
914 static FILE* msg_cache_put (IMAP_DATA* idata, HEADER* h)
916 char id[_POSIX_PATH_MAX];
921 idata->bcache = msg_cache_open (idata);
922 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
923 return mutt_bcache_put (idata->bcache, id, 1);
926 static int msg_cache_commit (IMAP_DATA* idata, HEADER* h)
928 char id[_POSIX_PATH_MAX];
933 idata->bcache = msg_cache_open (idata);
934 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
936 return mutt_bcache_commit (idata->bcache, id);
939 int imap_cache_del (IMAP_DATA* idata, HEADER* h)
941 char id[_POSIX_PATH_MAX];
946 idata->bcache = msg_cache_open (idata);
947 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
948 return mutt_bcache_del (idata->bcache, id);
951 static int msg_cache_clean_cb (const char* id, body_cache_t* bcache, void* data)
953 unsigned int uv, uid, n;
954 IMAP_DATA* idata = (IMAP_DATA*)data;
956 if (sscanf (id, "%u-%u", &uv, &uid) != 2)
960 if (uv != idata->uid_validity)
961 mutt_bcache_del (bcache, id);
963 /* TODO: presort UIDs, walk in order */
964 for (n = 0; n < idata->ctx->msgcount; n++)
966 if (uid == HEADER_DATA(idata->ctx->hdrs[n])->uid)
969 mutt_bcache_del (bcache, id);
974 int imap_cache_clean (IMAP_DATA* idata)
976 idata->bcache = msg_cache_open (idata);
977 mutt_bcache_list (idata->bcache, msg_cache_clean_cb, idata);
982 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
983 * appear in the folder flags list. Why wouldn't they? */
984 void imap_add_keywords (char* s, HEADER* h, LIST* mailbox_flags, size_t slen)
988 if (!mailbox_flags || !HEADER_DATA(h) || !HEADER_DATA(h)->keywords)
991 keywords = HEADER_DATA(h)->keywords->next;
995 if (imap_has_flag (mailbox_flags, keywords->data))
997 safe_strcat (s, slen, keywords->data);
998 safe_strcat (s, slen, " ");
1000 keywords = keywords->next;
1004 /* imap_free_header_data: free IMAP_HEADER structure */
1005 void imap_free_header_data (void** data)
1007 /* this should be safe even if the list wasn't used */
1008 mutt_free_list (&(((IMAP_HEADER_DATA*) *data)->keywords));
1010 FREE (data); /* __FREE_CHECKED__ */
1013 /* imap_set_flags: fill out the message header according to the flags from
1014 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
1015 char* imap_set_flags (IMAP_DATA* idata, HEADER* h, char* s)
1017 CONTEXT* ctx = idata->ctx;
1019 IMAP_HEADER_DATA* hd;
1020 unsigned char readonly;
1022 memset (&newh, 0, sizeof (newh));
1026 dprint (2, (debugfile, "imap_fetch_message: parsing FLAGS\n"));
1027 if ((s = msg_parse_flags (&newh, s)) == NULL)
1030 /* YAUH (yet another ugly hack): temporarily set context to
1031 * read-write even if it's read-only, so *server* updates of
1032 * flags can be processed by mutt_set_flag. ctx->changed must
1033 * be restored afterwards */
1034 readonly = ctx->readonly;
1037 mutt_set_flag (ctx, h, M_NEW, !(hd->read || hd->old));
1038 mutt_set_flag (ctx, h, M_OLD, hd->old);
1039 mutt_set_flag (ctx, h, M_READ, hd->read);
1040 mutt_set_flag (ctx, h, M_DELETE, hd->deleted);
1041 mutt_set_flag (ctx, h, M_FLAG, hd->flagged);
1042 mutt_set_flag (ctx, h, M_REPLIED, hd->replied);
1044 /* this message is now definitively *not* changed (mutt_set_flag
1045 * marks things changed as a side-effect) */
1047 ctx->changed &= ~readonly;
1048 ctx->readonly = readonly;
1054 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
1055 * Expects string beginning with * n FETCH.
1058 * -1 if the string is not a fetch response
1059 * -2 if the string is a corrupt fetch response */
1060 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf, FILE* fp)
1064 int rc = -1; /* default now is that string isn't FETCH response*/
1066 idata = (IMAP_DATA*) ctx->data;
1071 /* skip to message number */
1072 buf = imap_next_word (buf);
1073 h->sid = atoi (buf);
1075 /* find FETCH tag */
1076 buf = imap_next_word (buf);
1077 if (ascii_strncasecmp ("FETCH", buf, 5))
1080 rc = -2; /* we've got a FETCH response, for better or worse */
1081 if (!(buf = strchr (buf, '(')))
1085 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
1086 * read header lines and call it again. Silly. */
1087 if ((rc = msg_parse_fetch (h, buf)) != -2 || !fp)
1090 if (imap_get_literal_count (buf, &bytes) == 0)
1092 imap_read_literal (fp, idata, bytes, NULL);
1094 /* we may have other fields of the FETCH _after_ the literal
1095 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
1096 * This all has to go - we should accept literals and nonliterals
1097 * interchangeably at any time. */
1098 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
1101 if (msg_parse_fetch (h, idata->buf) == -1)
1105 rc = 0; /* success */
1107 /* subtract headers from message size - unfortunately only the subset of
1108 * headers we've requested. */
1109 h->content_length -= bytes;
1114 /* msg_parse_fetch: handle headers returned from header fetch */
1115 static int msg_parse_fetch (IMAP_HEADER *h, char *s)
1117 char tmp[SHORT_STRING];
1127 if (ascii_strncasecmp ("FLAGS", s, 5) == 0)
1129 if ((s = msg_parse_flags (h, s)) == NULL)
1132 else if (ascii_strncasecmp ("UID", s, 3) == 0)
1136 h->data->uid = (unsigned int) atoi (s);
1138 s = imap_next_word (s);
1140 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0)
1146 dprint (1, (debugfile, "msg_parse_fetch(): bogus INTERNALDATE entry: %s\n", s));
1151 while (*s && *s != '\"')
1155 s++; /* skip past the trailing " */
1157 h->received = imap_parse_date (tmp);
1159 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0)
1164 while (isdigit ((unsigned char) *s))
1167 h->content_length = atoi (tmp);
1169 else if (!ascii_strncasecmp ("BODY", s, 4) ||
1170 !ascii_strncasecmp ("RFC822.HEADER", s, 13))
1172 /* handle above, in msg_fetch_header */
1176 s++; /* end of request */
1179 /* got something i don't understand */
1180 imap_error ("msg_parse_fetch", s);
1188 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1189 static char* msg_parse_flags (IMAP_HEADER* h, char* s)
1191 IMAP_HEADER_DATA* hd = h->data;
1193 /* sanity-check string */
1194 if (ascii_strncasecmp ("FLAGS", s, 5) != 0)
1196 dprint (1, (debugfile, "msg_parse_flags: not a FLAGS response: %s\n",
1204 dprint (1, (debugfile, "msg_parse_flags: bogus FLAGS response: %s\n",
1210 mutt_free_list (&hd->keywords);
1211 hd->deleted = hd->flagged = hd->replied = hd->read = hd->old = 0;
1214 while (*s && *s != ')')
1216 if (ascii_strncasecmp ("\\deleted", s, 8) == 0)
1221 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0)
1226 else if (ascii_strncasecmp ("\\answered", s, 9) == 0)
1231 else if (ascii_strncasecmp ("\\seen", s, 5) == 0)
1236 else if (ascii_strncasecmp ("\\recent", s, 7) == 0)
1238 else if (ascii_strncasecmp ("old", s, 3) == 0)
1245 /* store custom flags as well */
1247 char* flag_word = s;
1250 hd->keywords = mutt_new_list ();
1252 while (*s && !ISSPACE (*s) && *s != ')')
1256 mutt_add_list (hd->keywords, flag_word);
1262 /* wrap up, or note bad flags response */
1267 dprint (1, (debugfile,
1268 "msg_parse_flags: Unterminated FLAGS response: %s\n", s));
1275 static void flush_buffer(char *buf, size_t *len, CONNECTION *conn)
1278 mutt_socket_write_n(conn, buf, *len);