2 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
3 * Copyright (C) 1999-2007 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 *uidnext = NULL;
81 #endif /* USE_HCACHE */
85 if (mutt_bit_isset (idata->capabilities,IMAP4REV1))
87 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s%s%s)]",
88 want_headers, ImapHeaders ? " " : "", ImapHeaders ? ImapHeaders : "");
90 else if (mutt_bit_isset (idata->capabilities,IMAP4))
92 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s%s%s)",
93 want_headers, ImapHeaders ? " " : "", ImapHeaders ? ImapHeaders : "");
96 { /* Unable to fetch headers for lower versions */
97 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+")))
107 mutt_error (_("Could not create temporary file %s"), tempfile);
113 /* make sure context has room to hold the mailbox */
114 while ((msgend) >= idata->ctx->hdrmax)
115 mx_alloc_memory (idata->ctx);
117 oldmsgcount = ctx->msgcount;
118 idata->reopen &= ~(IMAP_REOPEN_ALLOW|IMAP_NEWMAIL_PENDING);
119 idata->newMailCount = 0;
122 idata->hcache = imap_hcache_open (idata, NULL);
124 if (idata->hcache && !msgbegin)
126 uid_validity = mutt_hcache_fetch_raw (idata->hcache, "/UIDVALIDITY", imap_hcache_keylen);
127 uidnext = mutt_hcache_fetch_raw (idata->hcache, "/UIDNEXT", imap_hcache_keylen);
128 if (uid_validity && uidnext && *uid_validity == idata->uid_validity
133 FREE (&uid_validity);
137 mutt_progress_init (&progress, _("Evaluating cache..."),
138 M_PROGRESS_MSG, ReadInc, msgend + 1);
140 snprintf (buf, sizeof (buf),
141 "UID FETCH 1:%u (UID FLAGS)", *uidnext - 1);
144 imap_cmd_start (idata, buf);
146 for (msgno = msgbegin; msgno <= msgend ; msgno++)
148 mutt_progress_update (&progress, msgno + 1, -1);
150 memset (&h, 0, sizeof (h));
151 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
156 rc = imap_cmd_step (idata);
157 if (rc != IMAP_CMD_CONTINUE)
159 /* suppress GCC aliasing warning */
160 imap_free_header_data ((void**) (void*) &h.data);
164 /* hole in the header cache */
168 if ((mfhrc = msg_fetch_header (ctx, &h, idata->buf, NULL)) == -1)
172 imap_free_header_data ((void**) (void*) &h.data);
177 ctx->hdrs[idx] = imap_hcache_get (idata, h.data->uid);
180 ctx->hdrs[idx]->index = idx;
181 /* messages which have not been expunged are ACTIVE (borrowed from mh
183 ctx->hdrs[idx]->active = 1;
184 ctx->hdrs[idx]->read = h.data->read;
185 ctx->hdrs[idx]->old = h.data->old;
186 ctx->hdrs[idx]->deleted = h.data->deleted;
187 ctx->hdrs[idx]->flagged = h.data->flagged;
188 ctx->hdrs[idx]->replied = h.data->replied;
189 ctx->hdrs[idx]->changed = h.data->changed;
190 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
191 ctx->hdrs[idx]->data = (void *) (h.data);
194 ctx->size += ctx->hdrs[idx]->content->length;
198 /* bad header in the cache, we'll have to refetch. */
199 dprint (3, (debugfile, "bad cache entry at %d, giving up\n", h.sid - 1));
200 imap_free_header_data((void**) (void*) &h.data);
204 while (rc != IMAP_CMD_OK && mfhrc == -1);
205 if (rc == IMAP_CMD_OK)
207 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
210 imap_free_header_data ((void**) (void*) &h.data);
211 imap_hcache_close (idata);
216 /* could also look for first null header in case hcache is holey */
217 msgbegin = ctx->msgcount;
219 #endif /* USE_HCACHE */
221 mutt_progress_init (&progress, _("Fetching message headers..."),
222 M_PROGRESS_MSG, ReadInc, msgend + 1);
224 for (msgno = msgbegin; msgno <= msgend ; msgno++)
226 mutt_progress_update (&progress, msgno + 1, -1);
228 /* we may get notification of new mail while fetching headers */
229 if (msgno + 1 > fetchlast)
231 fetchlast = msgend + 1;
233 snprintf (buf, sizeof (buf),
234 "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)", msgno + 1,
237 imap_cmd_start (idata, buf);
241 memset (&h, 0, sizeof (h));
242 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
244 /* this DO loop does two things:
245 * 1. handles untagged messages, so we can try again on the same msg
246 * 2. fetches the tagged response at the end of the last message.
252 rc = imap_cmd_step (idata);
253 if (rc != IMAP_CMD_CONTINUE)
256 if ((mfhrc = msg_fetch_header (ctx, &h, idata->buf, fp)) == -1)
261 /* make sure we don't get remnants from older larger message headers */
265 ctx->hdrs[idx] = mutt_new_header ();
267 ctx->hdrs[idx]->index = h.sid - 1;
268 /* messages which have not been expunged are ACTIVE (borrowed from mh
270 ctx->hdrs[idx]->active = 1;
271 ctx->hdrs[idx]->read = h.data->read;
272 ctx->hdrs[idx]->old = h.data->old;
273 ctx->hdrs[idx]->deleted = h.data->deleted;
274 ctx->hdrs[idx]->flagged = h.data->flagged;
275 ctx->hdrs[idx]->replied = h.data->replied;
276 ctx->hdrs[idx]->changed = h.data->changed;
277 ctx->hdrs[idx]->received = h.received;
278 ctx->hdrs[idx]->data = (void *) (h.data);
280 if (maxuid < h.data->uid)
281 maxuid = h.data->uid;
284 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
285 * on h.received being set */
286 ctx->hdrs[idx]->env = mutt_read_rfc822_header (fp, ctx->hdrs[idx],
288 /* content built as a side-effect of mutt_read_rfc822_header */
289 ctx->hdrs[idx]->content->length = h.content_length;
290 ctx->size += h.content_length;
293 imap_hcache_put (idata, ctx->hdrs[idx]);
294 #endif /* USE_HCACHE */
298 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
299 ((msgno + 1) >= fetchlast)));
301 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
304 imap_free_header_data ((void**) (void*) &h.data);
306 imap_hcache_close (idata);
312 /* in case we get new mail while fetching the headers */
313 if (idata->reopen & IMAP_NEWMAIL_PENDING)
315 msgend = idata->newMailCount - 1;
316 while ((msgend) >= ctx->hdrmax)
317 mx_alloc_memory (ctx);
318 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
319 idata->newMailCount = 0;
323 if (maxuid && (status = imap_mboxcache_get (idata, idata->mailbox, 0)))
324 status->uidnext = maxuid + 1;
327 mutt_hcache_store_raw (idata->hcache, "/UIDVALIDITY", &idata->uid_validity,
328 sizeof (idata->uid_validity), imap_hcache_keylen);
329 if (maxuid && idata->uidnext < maxuid + 1)
331 dprint (2, (debugfile, "Overriding UIDNEXT: %u -> %u\n", idata->uidnext, maxuid + 1));
332 idata->uidnext = maxuid + 1;
334 if (idata->uidnext > 1)
335 mutt_hcache_store_raw (idata->hcache, "/UIDNEXT", &idata->uidnext,
336 sizeof (idata->uidnext), imap_hcache_keylen);
338 imap_hcache_close (idata);
339 #endif /* USE_HCACHE */
343 if (ctx->msgcount > oldmsgcount)
345 mx_alloc_memory(ctx);
346 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
349 idata->reopen |= IMAP_REOPEN_ALLOW;
353 int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
358 char buf[LONG_STRING];
359 char path[_POSIX_PATH_MAX];
362 progress_t progressbar;
368 /* Sam's weird courier server returns an OK response even when FETCH
369 * fails. Thanks Sam. */
372 idata = (IMAP_DATA*) ctx->data;
373 h = ctx->hdrs[msgno];
375 if ((msg->fp = msg_cache_get (idata, h)))
377 if (HEADER_DATA(h)->parsed)
383 /* we still do some caching even if imap_cachedir is unset */
384 /* see if we already have the message in our cache */
385 cacheno = HEADER_DATA(h)->uid % IMAP_CACHE_LEN;
386 cache = &idata->cache[cacheno];
390 /* don't treat cache errors as fatal, just fall back. */
391 if (cache->uid == HEADER_DATA(h)->uid &&
392 (msg->fp = fopen (cache->path, "r")))
396 unlink (cache->path);
402 mutt_message _("Fetching message...");
404 if (!(msg->fp = msg_cache_put (idata, h)))
406 cache->uid = HEADER_DATA(h)->uid;
408 cache->path = safe_strdup (path);
409 if (!(msg->fp = safe_fopen (path, "w+")))
416 /* mark this header as currently inactive so the command handler won't
417 * also try to update it. HACK until all this code can be moved into the
421 snprintf (buf, sizeof (buf), "UID FETCH %u %s", HEADER_DATA(h)->uid,
422 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
423 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") :
426 imap_cmd_start (idata, buf);
429 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
433 pc = imap_next_word (pc);
434 pc = imap_next_word (pc);
436 if (!ascii_strncasecmp ("FETCH", pc, 5))
440 pc = imap_next_word (pc);
443 if (ascii_strncasecmp ("UID", pc, 3) == 0)
445 pc = imap_next_word (pc);
447 if (uid != HEADER_DATA(h)->uid)
448 mutt_error (_("The message index is incorrect. Try reopening the mailbox."));
450 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
451 (ascii_strncasecmp ("BODY[]", pc, 6) == 0))
453 pc = imap_next_word (pc);
454 if (imap_get_literal_count(pc, &bytes) < 0)
456 imap_error ("imap_fetch_message()", buf);
459 mutt_progress_init (&progressbar, _("Fetching message..."),
460 M_PROGRESS_SIZE, NetInc, bytes);
461 if (imap_read_literal (msg->fp, idata, bytes, &progressbar) < 0)
463 /* pick up trailing line */
464 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
470 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
471 * change (eg from \Unseen to \Seen).
472 * Uncommitted changes in mutt take precedence. If we decide to
473 * incrementally update flags later, this won't stop us syncing */
474 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed)
476 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
482 while (rc == IMAP_CMD_CONTINUE);
484 /* see comment before command start. */
488 if (ferror (msg->fp))
490 mutt_perror (cache->path);
494 if (rc != IMAP_CMD_OK)
497 if (!fetched || !imap_code (idata->buf))
500 msg_cache_commit (idata, h);
503 /* Update the header information. Previously, we only downloaded a
504 * portion of the headers, those required for the main display.
507 /* It may be that the Status header indicates a message is read, but the
508 * IMAP server doesn't know the message has been \Seen. So we capture
509 * the server's notion of 'read' and if it differs from the message info
510 * picked up in mutt_read_rfc822_header, we mark the message (and context
511 * changed). Another possiblity: ignore Status on IMAP?*/
513 newenv = mutt_read_rfc822_header (msg->fp, h, 0, 0);
514 mutt_merge_envelopes(h->env, &newenv);
516 /* see above. We want the new status in h->read, so we unset it manually
517 * and let mutt_set_flag set it correctly, updating context. */
521 mutt_set_flag (ctx, h, M_NEW, read);
525 fgets (buf, sizeof (buf), msg->fp);
526 while (!feof (msg->fp))
529 fgets (buf, sizeof (buf), msg->fp);
532 h->content->length = ftell (msg->fp) - h->content->offset;
534 /* This needs to be done in case this is a multipart message */
535 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
536 h->security = crypt_query (h->content);
541 HEADER_DATA(h)->parsed = 1;
546 safe_fclose (&msg->fp);
547 imap_cache_del (idata, h);
550 unlink (cache->path);
557 int imap_append_message (CONTEXT *ctx, MESSAGE *msg)
561 char buf[LONG_STRING];
562 char mbox[LONG_STRING];
563 char mailbox[LONG_STRING];
565 progress_t progressbar;
571 idata = (IMAP_DATA*) ctx->data;
573 if (imap_parse_path (ctx->path, &mx))
576 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
578 if ((fp = fopen (msg->path, "r")) == NULL)
580 mutt_perror (msg->path);
584 /* currently we set the \Seen flag on all messages, but probably we
585 * should scan the message Status header for flag info. Since we're
586 * already rereading the whole file for length it isn't any more
587 * expensive (it'd be nice if we had the file size passed in already
588 * by the code that writes the file, but that's a lot of changes.
589 * Ideally we'd have a HEADER structure with flag info here... */
590 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
592 if(c == '\n' && last != '\r')
599 mutt_progress_init (&progressbar, _("Uploading message..."),
600 M_PROGRESS_SIZE, NetInc, len);
602 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
603 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) {%lu}", mbox,
604 msg->flags.read ? "\\Seen" : "",
605 msg->flags.read && (msg->flags.replied || msg->flags.flagged) ? " " : "",
606 msg->flags.replied ? "\\Answered" : "",
607 msg->flags.replied && msg->flags.flagged ? " " : "",
608 msg->flags.flagged ? "\\Flagged" : "",
609 (unsigned long) len);
611 imap_cmd_start (idata, buf);
614 rc = imap_cmd_step (idata);
615 while (rc == IMAP_CMD_CONTINUE);
617 if (rc != IMAP_CMD_RESPOND)
621 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
624 pc = idata->buf + SEQLEN;
626 pc = imap_next_word (pc);
627 mutt_error ("%s", pc);
633 for (last = EOF, sent = len = 0; (c = fgetc(fp)) != EOF; last = c)
635 if (c == '\n' && last != '\r')
640 if (len > sizeof(buf) - 3)
643 flush_buffer(buf, &len, idata->conn);
644 mutt_progress_update (&progressbar, sent, -1);
649 flush_buffer(buf, &len, idata->conn);
651 mutt_socket_write (idata->conn, "\r\n");
655 rc = imap_cmd_step (idata);
656 while (rc == IMAP_CMD_CONTINUE);
658 if (!imap_code (idata->buf))
662 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
664 pc = idata->buf + SEQLEN;
666 pc = imap_next_word (pc);
667 mutt_error ("%s", pc);
680 /* imap_copy_messages: use server COPY command to copy messages to another
685 * 1: non-fatal error - try fetch/append */
686 int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
689 BUFFER cmd, sync_cmd;
691 char mbox[LONG_STRING];
692 char mmbox[LONG_STRING];
696 int err_continue = M_NO;
698 idata = (IMAP_DATA*) ctx->data;
700 if (imap_parse_path (dest, &mx))
702 dprint (1, (debugfile, "imap_copy_messages: bad destination %s\n", dest));
706 /* check that the save-to folder is in the same account */
707 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account)))
709 dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n",
714 if (h && h->attach_del)
716 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
720 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
722 memset (&sync_cmd, 0, sizeof (sync_cmd));
723 memset (&cmd, 0, sizeof (cmd));
724 mutt_buffer_addstr (&cmd, "UID COPY ");
726 /* Null HEADER* means copy tagged messages */
729 /* if any messages have attachments to delete, fall through to FETCH
730 * and APPEND. TODO: Copy what we can with COPY, fall through for the
732 for (n = 0; n < ctx->msgcount; n++)
734 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
736 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
740 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
741 ctx->hdrs[n]->changed)
743 rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
746 dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
752 rc = imap_make_msg_set (idata, &cmd, M_TAG, 0, 0);
755 dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
758 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
762 mutt_message (_("Copying message %d to %s..."), h->index+1, mbox);
763 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
764 mutt_buffer_addstr (&cmd, uid);
766 if (h->active && h->changed)
768 rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
771 dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
777 /* let's get it on */
778 mutt_buffer_addstr (&cmd, " ");
779 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
780 mutt_buffer_addstr (&cmd, mmbox);
782 rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
785 /* bail out if command failed for reasons other than nonexistent target */
786 if (ascii_strncasecmp (imap_get_qualifier (idata->buf), "[TRYCREATE]", 11))
788 imap_error ("imap_copy_messages", idata->buf);
791 dprint (2, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
792 snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
793 if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1)
798 if (imap_create_mailbox (idata, mbox) < 0)
802 rc = imap_exec (idata, cmd.data, 0);
806 imap_error ("imap_copy_messages", idata->buf);
814 for (n = 0; n < ctx->msgcount; n++)
816 if (ctx->hdrs[n]->tagged)
818 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
819 if (option (OPTDELETEUNTAG))
820 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
825 mutt_set_flag (ctx, h, M_DELETE, 1);
826 if (option (OPTDELETEUNTAG))
827 mutt_set_flag (ctx, h, M_TAG, 0);
834 FREE (&sync_cmd.data);
842 FREE (&sync_cmd.data);
847 static body_cache_t *msg_cache_open (IMAP_DATA *idata)
849 char mailbox[_POSIX_PATH_MAX];
852 return idata->bcache;
854 imap_cachepath (idata, idata->mailbox, mailbox, sizeof (mailbox));
856 return mutt_bcache_open (&idata->conn->account, mailbox);
859 static FILE* msg_cache_get (IMAP_DATA* idata, HEADER* h)
861 char id[_POSIX_PATH_MAX];
866 idata->bcache = msg_cache_open (idata);
867 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
868 return mutt_bcache_get (idata->bcache, id);
871 static FILE* msg_cache_put (IMAP_DATA* idata, HEADER* h)
873 char id[_POSIX_PATH_MAX];
878 idata->bcache = msg_cache_open (idata);
879 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
880 return mutt_bcache_put (idata->bcache, id, 1);
883 static int msg_cache_commit (IMAP_DATA* idata, HEADER* h)
885 char id[_POSIX_PATH_MAX];
890 idata->bcache = msg_cache_open (idata);
891 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
893 return mutt_bcache_commit (idata->bcache, id);
896 int imap_cache_del (IMAP_DATA* idata, HEADER* h)
898 char id[_POSIX_PATH_MAX];
903 idata->bcache = msg_cache_open (idata);
904 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
905 return mutt_bcache_del (idata->bcache, id);
908 static int msg_cache_clean_cb (const char* id, body_cache_t* bcache, void* data)
910 unsigned int uv, uid, n;
911 IMAP_DATA* idata = (IMAP_DATA*)data;
913 if (sscanf (id, "%u-%u", &uv, &uid) != 2)
917 if (uv != idata->uid_validity)
918 mutt_bcache_del (bcache, id);
920 /* TODO: presort UIDs, walk in order */
921 for (n = 0; n < idata->ctx->msgcount; n++)
923 if (uid == HEADER_DATA(idata->ctx->hdrs[n])->uid)
926 mutt_bcache_del (bcache, id);
931 int imap_cache_clean (IMAP_DATA* idata)
933 idata->bcache = msg_cache_open (idata);
934 mutt_bcache_list (idata->bcache, msg_cache_clean_cb, idata);
939 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
940 * appear in the folder flags list. Why wouldn't they? */
941 void imap_add_keywords (char* s, HEADER* h, LIST* mailbox_flags, size_t slen)
945 if (!mailbox_flags || !HEADER_DATA(h) || !HEADER_DATA(h)->keywords)
948 keywords = HEADER_DATA(h)->keywords->next;
952 if (imap_has_flag (mailbox_flags, keywords->data))
954 safe_strcat (s, slen, keywords->data);
955 safe_strcat (s, slen, " ");
957 keywords = keywords->next;
961 /* imap_free_header_data: free IMAP_HEADER structure */
962 void imap_free_header_data (void** data)
964 /* this should be safe even if the list wasn't used */
965 mutt_free_list (&(((IMAP_HEADER_DATA*) *data)->keywords));
967 FREE (data); /* __FREE_CHECKED__ */
970 /* imap_set_flags: fill out the message header according to the flags from
971 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
972 char* imap_set_flags (IMAP_DATA* idata, HEADER* h, char* s)
974 CONTEXT* ctx = idata->ctx;
976 IMAP_HEADER_DATA* hd;
977 unsigned char readonly;
979 memset (&newh, 0, sizeof (newh));
983 dprint (2, (debugfile, "imap_fetch_message: parsing FLAGS\n"));
984 if ((s = msg_parse_flags (&newh, s)) == NULL)
987 /* YAUH (yet another ugly hack): temporarily set context to
988 * read-write even if it's read-only, so *server* updates of
989 * flags can be processed by mutt_set_flag. ctx->changed must
990 * be restored afterwards */
991 readonly = ctx->readonly;
994 mutt_set_flag (ctx, h, M_NEW, !(hd->read || hd->old));
995 mutt_set_flag (ctx, h, M_OLD, hd->old);
996 mutt_set_flag (ctx, h, M_READ, hd->read);
997 mutt_set_flag (ctx, h, M_DELETE, hd->deleted);
998 mutt_set_flag (ctx, h, M_FLAG, hd->flagged);
999 mutt_set_flag (ctx, h, M_REPLIED, hd->replied);
1001 /* this message is now definitively *not* changed (mutt_set_flag
1002 * marks things changed as a side-effect) */
1004 ctx->changed &= ~readonly;
1005 ctx->readonly = readonly;
1011 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
1012 * Expects string beginning with * n FETCH.
1015 * -1 if the string is not a fetch response
1016 * -2 if the string is a corrupt fetch response */
1017 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf, FILE* fp)
1021 int rc = -1; /* default now is that string isn't FETCH response*/
1023 idata = (IMAP_DATA*) ctx->data;
1028 /* skip to message number */
1029 buf = imap_next_word (buf);
1030 h->sid = atoi (buf);
1032 /* find FETCH tag */
1033 buf = imap_next_word (buf);
1034 if (ascii_strncasecmp ("FETCH", buf, 5))
1037 rc = -2; /* we've got a FETCH response, for better or worse */
1038 if (!(buf = strchr (buf, '(')))
1042 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
1043 * read header lines and call it again. Silly. */
1044 if ((rc = msg_parse_fetch (h, buf) != -2) || !fp)
1047 if (imap_get_literal_count (buf, &bytes) == 0)
1049 imap_read_literal (fp, idata, bytes, NULL);
1051 /* we may have other fields of the FETCH _after_ the literal
1052 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
1053 * This all has to go - we should accept literals and nonliterals
1054 * interchangeably at any time. */
1055 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
1058 if (msg_parse_fetch (h, idata->buf) == -1)
1062 rc = 0; /* success */
1064 /* subtract headers from message size - unfortunately only the subset of
1065 * headers we've requested. */
1066 h->content_length -= bytes;
1071 /* msg_parse_fetch: handle headers returned from header fetch */
1072 static int msg_parse_fetch (IMAP_HEADER *h, char *s)
1074 char tmp[SHORT_STRING];
1084 if (ascii_strncasecmp ("FLAGS", s, 5) == 0)
1086 if ((s = msg_parse_flags (h, s)) == NULL)
1089 else if (ascii_strncasecmp ("UID", s, 3) == 0)
1093 h->data->uid = (unsigned int) atoi (s);
1095 s = imap_next_word (s);
1097 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0)
1103 dprint (1, (debugfile, "msg_parse_fetch(): bogus INTERNALDATE entry: %s\n", s));
1108 while (*s && *s != '\"')
1112 s++; /* skip past the trailing " */
1114 h->received = imap_parse_date (tmp);
1116 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0)
1121 while (isdigit ((unsigned char) *s))
1124 h->content_length = atoi (tmp);
1126 else if (!ascii_strncasecmp ("BODY", s, 4) ||
1127 !ascii_strncasecmp ("RFC822.HEADER", s, 13))
1129 /* handle above, in msg_fetch_header */
1133 s++; /* end of request */
1136 /* got something i don't understand */
1137 imap_error ("msg_parse_fetch", s);
1145 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1146 static char* msg_parse_flags (IMAP_HEADER* h, char* s)
1148 IMAP_HEADER_DATA* hd = h->data;
1150 /* sanity-check string */
1151 if (ascii_strncasecmp ("FLAGS", s, 5) != 0)
1153 dprint (1, (debugfile, "msg_parse_flags: not a FLAGS response: %s\n",
1161 dprint (1, (debugfile, "msg_parse_flags: bogus FLAGS response: %s\n",
1167 mutt_free_list (&hd->keywords);
1168 hd->deleted = hd->flagged = hd->replied = hd->read = hd->old = 0;
1171 while (*s && *s != ')')
1173 if (ascii_strncasecmp ("\\deleted", s, 8) == 0)
1178 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0)
1183 else if (ascii_strncasecmp ("\\answered", s, 9) == 0)
1188 else if (ascii_strncasecmp ("\\seen", s, 5) == 0)
1193 else if (ascii_strncasecmp ("\\recent", s, 7) == 0)
1195 else if (ascii_strncasecmp ("old", s, 3) == 0)
1202 /* store custom flags as well */
1204 char* flag_word = s;
1207 hd->keywords = mutt_new_list ();
1209 while (*s && !ISSPACE (*s) && *s != ')')
1213 mutt_add_list (hd->keywords, flag_word);
1219 /* wrap up, or note bad flags response */
1224 dprint (1, (debugfile,
1225 "msg_parse_flags: Unterminated FLAGS response: %s\n", s));
1232 static void flush_buffer(char *buf, size_t *len, CONNECTION *conn)
1235 mutt_socket_write_n(conn, buf, *len);