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";
79 unsigned int *uid_validity = NULL;
80 unsigned int *puidnext = NULL;
81 unsigned int uidnext = 0;
83 #endif /* USE_HCACHE */
87 if (mutt_bit_isset (idata->capabilities,IMAP4REV1))
89 safe_asprintf (&hdrreq, "BODY.PEEK[HEADER.FIELDS (%s%s%s)]",
90 want_headers, ImapHeaders ? " " : "", NONULL (ImapHeaders));
92 else if (mutt_bit_isset (idata->capabilities,IMAP4))
94 safe_asprintf (&hdrreq, "RFC822.HEADER.LINES (%s%s%s)",
95 want_headers, ImapHeaders ? " " : "", NONULL (ImapHeaders));
98 { /* Unable to fetch headers for lower versions */
99 mutt_error _("Unable to fetch headers from this IMAP server version.");
100 mutt_sleep (2); /* pause a moment to let the user see the error */
104 /* instead of downloading all headers and then parsing them, we parse them
105 * as they come in. */
106 mutt_mktemp (tempfile, sizeof (tempfile));
107 if (!(fp = safe_fopen (tempfile, "w+")))
109 mutt_error (_("Could not create temporary file %s"), tempfile);
115 /* make sure context has room to hold the mailbox */
116 while ((msgend) >= idata->ctx->hdrmax)
117 mx_alloc_memory (idata->ctx);
119 oldmsgcount = ctx->msgcount;
120 idata->reopen &= ~(IMAP_REOPEN_ALLOW|IMAP_NEWMAIL_PENDING);
121 idata->newMailCount = 0;
124 idata->hcache = imap_hcache_open (idata, NULL);
126 if (idata->hcache && !msgbegin)
128 uid_validity = mutt_hcache_fetch_raw (idata->hcache, "/UIDVALIDITY", imap_hcache_keylen);
129 puidnext = mutt_hcache_fetch_raw (idata->hcache, "/UIDNEXT", imap_hcache_keylen);
135 if (uid_validity && uidnext && *uid_validity == idata->uid_validity)
137 FREE (&uid_validity);
141 mutt_progress_init (&progress, _("Evaluating cache..."),
142 M_PROGRESS_MSG, ReadInc, msgend + 1);
144 snprintf (buf, sizeof (buf),
145 "UID FETCH 1:%u (UID FLAGS)", uidnext - 1);
147 imap_cmd_start (idata, buf);
149 rc = IMAP_CMD_CONTINUE;
150 for (msgno = msgbegin; rc == IMAP_CMD_CONTINUE; msgno++)
152 mutt_progress_update (&progress, msgno + 1, -1);
154 memset (&h, 0, sizeof (h));
155 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
160 rc = imap_cmd_step (idata);
161 if (rc != IMAP_CMD_CONTINUE)
163 /* suppress GCC aliasing warning */
164 imap_free_header_data ((void**) (void*) &h.data);
168 /* hole in the header cache */
172 if ((mfhrc = msg_fetch_header (ctx, &h, idata->buf, NULL)) == -1)
176 imap_free_header_data ((void**) (void*) &h.data);
182 dprint (2, (debugfile, "imap_read_headers: skipping hcache FETCH "
183 "response for unknown message number %d\n", h.sid));
189 ctx->hdrs[idx] = imap_hcache_get (idata, h.data->uid);
192 ctx->hdrs[idx]->index = idx;
193 /* messages which have not been expunged are ACTIVE (borrowed from mh
195 ctx->hdrs[idx]->active = 1;
196 ctx->hdrs[idx]->read = h.data->read;
197 ctx->hdrs[idx]->old = h.data->old;
198 ctx->hdrs[idx]->deleted = h.data->deleted;
199 ctx->hdrs[idx]->flagged = h.data->flagged;
200 ctx->hdrs[idx]->replied = h.data->replied;
201 ctx->hdrs[idx]->changed = h.data->changed;
202 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
203 ctx->hdrs[idx]->data = (void *) (h.data);
206 ctx->size += ctx->hdrs[idx]->content->length;
210 /* bad header in the cache, we'll have to refetch. */
211 dprint (3, (debugfile, "bad cache entry at %d, giving up\n", h.sid - 1));
212 imap_free_header_data((void**) (void*) &h.data);
216 while (rc != IMAP_CMD_OK && mfhrc == -1);
217 if (rc == IMAP_CMD_OK)
219 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
222 imap_free_header_data ((void**) (void*) &h.data);
223 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)
244 fetchlast = msgend + 1;
245 safe_asprintf (&cmd, "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)",
246 msgno + 1, fetchlast, hdrreq);
247 imap_cmd_start (idata, cmd);
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);
344 /* in case we get new mail while fetching the headers */
345 if (idata->reopen & IMAP_NEWMAIL_PENDING)
347 msgend = idata->newMailCount - 1;
348 while ((msgend) >= ctx->hdrmax)
349 mx_alloc_memory (ctx);
350 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
351 idata->newMailCount = 0;
355 if (maxuid && (status = imap_mboxcache_get (idata, idata->mailbox, 0)))
356 status->uidnext = maxuid + 1;
359 mutt_hcache_store_raw (idata->hcache, "/UIDVALIDITY", &idata->uid_validity,
360 sizeof (idata->uid_validity), imap_hcache_keylen);
361 if (maxuid && idata->uidnext < maxuid + 1)
363 dprint (2, (debugfile, "Overriding UIDNEXT: %u -> %u\n", idata->uidnext, maxuid + 1));
364 idata->uidnext = maxuid + 1;
366 if (idata->uidnext > 1)
367 mutt_hcache_store_raw (idata->hcache, "/UIDNEXT", &idata->uidnext,
368 sizeof (idata->uidnext), imap_hcache_keylen);
370 imap_hcache_close (idata);
371 #endif /* USE_HCACHE */
373 if (ctx->msgcount > oldmsgcount)
375 mx_alloc_memory(ctx);
376 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
379 idata->reopen |= IMAP_REOPEN_ALLOW;
392 int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
397 char buf[LONG_STRING];
398 char path[_POSIX_PATH_MAX];
401 progress_t progressbar;
407 /* Sam's weird courier server returns an OK response even when FETCH
408 * fails. Thanks Sam. */
411 idata = (IMAP_DATA*) ctx->data;
412 h = ctx->hdrs[msgno];
414 if ((msg->fp = msg_cache_get (idata, h)))
416 if (HEADER_DATA(h)->parsed)
422 /* we still do some caching even if imap_cachedir is unset */
423 /* see if we already have the message in our cache */
424 cacheno = HEADER_DATA(h)->uid % IMAP_CACHE_LEN;
425 cache = &idata->cache[cacheno];
429 /* don't treat cache errors as fatal, just fall back. */
430 if (cache->uid == HEADER_DATA(h)->uid &&
431 (msg->fp = fopen (cache->path, "r")))
435 unlink (cache->path);
441 mutt_message _("Fetching message...");
443 if (!(msg->fp = msg_cache_put (idata, h)))
445 cache->uid = HEADER_DATA(h)->uid;
446 mutt_mktemp (path, sizeof (path));
447 cache->path = safe_strdup (path);
448 if (!(msg->fp = safe_fopen (path, "w+")))
455 /* mark this header as currently inactive so the command handler won't
456 * also try to update it. HACK until all this code can be moved into the
460 snprintf (buf, sizeof (buf), "UID FETCH %u %s", HEADER_DATA(h)->uid,
461 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
462 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") :
465 imap_cmd_start (idata, buf);
468 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
472 pc = imap_next_word (pc);
473 pc = imap_next_word (pc);
475 if (!ascii_strncasecmp ("FETCH", pc, 5))
479 pc = imap_next_word (pc);
482 if (ascii_strncasecmp ("UID", pc, 3) == 0)
484 pc = imap_next_word (pc);
486 if (uid != HEADER_DATA(h)->uid)
487 mutt_error (_("The message index is incorrect. Try reopening the mailbox."));
489 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
490 (ascii_strncasecmp ("BODY[]", pc, 6) == 0))
492 pc = imap_next_word (pc);
493 if (imap_get_literal_count(pc, &bytes) < 0)
495 imap_error ("imap_fetch_message()", buf);
498 mutt_progress_init (&progressbar, _("Fetching message..."),
499 M_PROGRESS_SIZE, NetInc, bytes);
500 if (imap_read_literal (msg->fp, idata, bytes, &progressbar) < 0)
502 /* pick up trailing line */
503 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
509 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
510 * change (eg from \Unseen to \Seen).
511 * Uncommitted changes in mutt take precedence. If we decide to
512 * incrementally update flags later, this won't stop us syncing */
513 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed)
515 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
521 while (rc == IMAP_CMD_CONTINUE);
523 /* see comment before command start. */
527 if (ferror (msg->fp))
529 mutt_perror (cache->path);
533 if (rc != IMAP_CMD_OK)
536 if (!fetched || !imap_code (idata->buf))
539 msg_cache_commit (idata, h);
542 /* Update the header information. Previously, we only downloaded a
543 * portion of the headers, those required for the main display.
546 /* It may be that the Status header indicates a message is read, but the
547 * IMAP server doesn't know the message has been \Seen. So we capture
548 * the server's notion of 'read' and if it differs from the message info
549 * picked up in mutt_read_rfc822_header, we mark the message (and context
550 * changed). Another possiblity: ignore Status on IMAP?*/
552 newenv = mutt_read_rfc822_header (msg->fp, h, 0, 0);
553 mutt_merge_envelopes(h->env, &newenv);
555 /* see above. We want the new status in h->read, so we unset it manually
556 * and let mutt_set_flag set it correctly, updating context. */
560 mutt_set_flag (ctx, h, M_NEW, read);
564 fgets (buf, sizeof (buf), msg->fp);
565 while (!feof (msg->fp))
568 fgets (buf, sizeof (buf), msg->fp);
571 h->content->length = ftell (msg->fp) - h->content->offset;
573 /* This needs to be done in case this is a multipart message */
574 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
575 h->security = crypt_query (h->content);
580 HEADER_DATA(h)->parsed = 1;
585 safe_fclose (&msg->fp);
586 imap_cache_del (idata, h);
589 unlink (cache->path);
596 int imap_append_message (CONTEXT *ctx, MESSAGE *msg)
600 char buf[LONG_STRING];
601 char mbox[LONG_STRING];
602 char mailbox[LONG_STRING];
603 char internaldate[IMAP_DATELEN];
605 progress_t progressbar;
611 idata = (IMAP_DATA*) ctx->data;
613 if (imap_parse_path (ctx->path, &mx))
616 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
618 strfcpy (mailbox, "INBOX", sizeof (mailbox));
620 if ((fp = fopen (msg->path, "r")) == NULL)
622 mutt_perror (msg->path);
626 /* currently we set the \Seen flag on all messages, but probably we
627 * should scan the message Status header for flag info. Since we're
628 * already rereading the whole file for length it isn't any more
629 * expensive (it'd be nice if we had the file size passed in already
630 * by the code that writes the file, but that's a lot of changes.
631 * Ideally we'd have a HEADER structure with flag info here... */
632 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
634 if(c == '\n' && last != '\r')
641 mutt_progress_init (&progressbar, _("Uploading message..."),
642 M_PROGRESS_SIZE, NetInc, len);
644 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
645 imap_make_date (internaldate, msg->received);
646 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) \"%s\" {%lu}", mbox,
647 msg->flags.read ? "\\Seen" : "",
648 msg->flags.read && (msg->flags.replied || msg->flags.flagged) ? " " : "",
649 msg->flags.replied ? "\\Answered" : "",
650 msg->flags.replied && msg->flags.flagged ? " " : "",
651 msg->flags.flagged ? "\\Flagged" : "",
653 (unsigned long) len);
655 imap_cmd_start (idata, buf);
658 rc = imap_cmd_step (idata);
659 while (rc == IMAP_CMD_CONTINUE);
661 if (rc != IMAP_CMD_RESPOND)
665 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
668 pc = idata->buf + SEQLEN;
670 pc = imap_next_word (pc);
671 mutt_error ("%s", pc);
677 for (last = EOF, sent = len = 0; (c = fgetc(fp)) != EOF; last = c)
679 if (c == '\n' && last != '\r')
684 if (len > sizeof(buf) - 3)
687 flush_buffer(buf, &len, idata->conn);
688 mutt_progress_update (&progressbar, sent, -1);
693 flush_buffer(buf, &len, idata->conn);
695 mutt_socket_write (idata->conn, "\r\n");
699 rc = imap_cmd_step (idata);
700 while (rc == IMAP_CMD_CONTINUE);
702 if (!imap_code (idata->buf))
706 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
708 pc = idata->buf + SEQLEN;
710 pc = imap_next_word (pc);
711 mutt_error ("%s", pc);
724 /* imap_copy_messages: use server COPY command to copy messages to another
729 * 1: non-fatal error - try fetch/append */
730 int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
733 BUFFER cmd, sync_cmd;
734 char mbox[LONG_STRING];
735 char mmbox[LONG_STRING];
736 char prompt[LONG_STRING];
740 int err_continue = M_NO;
743 idata = (IMAP_DATA*) ctx->data;
745 if (imap_parse_path (dest, &mx))
747 dprint (1, (debugfile, "imap_copy_messages: bad destination %s\n", dest));
751 /* check that the save-to folder is in the same account */
752 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account)))
754 dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n",
759 if (h && h->attach_del)
761 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
765 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
767 strfcpy (mbox, "INBOX", sizeof (mbox));
768 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
770 /* loop in case of TRYCREATE */
773 memset (&sync_cmd, 0, sizeof (sync_cmd));
774 memset (&cmd, 0, sizeof (cmd));
776 /* Null HEADER* means copy tagged messages */
779 /* if any messages have attachments to delete, fall through to FETCH
780 * and APPEND. TODO: Copy what we can with COPY, fall through for the
782 for (n = 0; n < ctx->msgcount; n++)
784 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
786 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
790 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
791 ctx->hdrs[n]->changed)
793 rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
796 dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
802 rc = imap_exec_msgset (idata, "UID COPY", mmbox, M_TAG, 0, 0);
805 dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
811 dprint (1, (debugfile, "could not queue copy\n"));
815 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
819 mutt_message (_("Copying message %d to %s..."), h->index+1, mbox);
820 mutt_buffer_printf (&cmd, "UID COPY %u %s", HEADER_DATA (h)->uid, mmbox);
822 if (h->active && h->changed)
824 rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
827 dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
831 if ((rc = imap_exec (idata, cmd.data, IMAP_CMD_QUEUE)) < 0)
833 dprint (1, (debugfile, "could not queue copy\n"));
838 /* let's get it on */
839 rc = imap_exec (idata, NULL, IMAP_CMD_FAIL_OK);
844 dprint (1, (debugfile, "Already tried to create mailbox %s\n", mbox));
847 /* bail out if command failed for reasons other than nonexistent target */
848 if (ascii_strncasecmp (imap_get_qualifier (idata->buf), "[TRYCREATE]", 11))
850 dprint (3, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
851 snprintf (prompt, sizeof (prompt), _("Create %s?"), mbox);
852 if (option (OPTCONFIRMCREATE) && mutt_yesorno (prompt, 1) < 1)
857 if (imap_create_mailbox (idata, mbox) < 0)
866 imap_error ("imap_copy_messages", idata->buf);
874 for (n = 0; n < ctx->msgcount; n++)
876 if (ctx->hdrs[n]->tagged)
878 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
879 if (option (OPTDELETEUNTAG))
880 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
885 mutt_set_flag (ctx, h, M_DELETE, 1);
886 if (option (OPTDELETEUNTAG))
887 mutt_set_flag (ctx, h, M_TAG, 0);
897 FREE (&sync_cmd.data);
900 return rc < 0 ? -1 : rc;
903 static body_cache_t *msg_cache_open (IMAP_DATA *idata)
905 char mailbox[_POSIX_PATH_MAX];
908 return idata->bcache;
910 imap_cachepath (idata, idata->mailbox, mailbox, sizeof (mailbox));
912 return mutt_bcache_open (&idata->conn->account, mailbox);
915 static FILE* msg_cache_get (IMAP_DATA* idata, HEADER* h)
917 char id[_POSIX_PATH_MAX];
922 idata->bcache = msg_cache_open (idata);
923 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
924 return mutt_bcache_get (idata->bcache, id);
927 static FILE* msg_cache_put (IMAP_DATA* idata, HEADER* h)
929 char id[_POSIX_PATH_MAX];
934 idata->bcache = msg_cache_open (idata);
935 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
936 return mutt_bcache_put (idata->bcache, id, 1);
939 static int msg_cache_commit (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);
949 return mutt_bcache_commit (idata->bcache, id);
952 int imap_cache_del (IMAP_DATA* idata, HEADER* h)
954 char id[_POSIX_PATH_MAX];
959 idata->bcache = msg_cache_open (idata);
960 snprintf (id, sizeof (id), "%u-%u", idata->uid_validity, HEADER_DATA(h)->uid);
961 return mutt_bcache_del (idata->bcache, id);
964 static int msg_cache_clean_cb (const char* id, body_cache_t* bcache, void* data)
966 unsigned int uv, uid, n;
967 IMAP_DATA* idata = (IMAP_DATA*)data;
969 if (sscanf (id, "%u-%u", &uv, &uid) != 2)
973 if (uv != idata->uid_validity)
974 mutt_bcache_del (bcache, id);
976 /* TODO: presort UIDs, walk in order */
977 for (n = 0; n < idata->ctx->msgcount; n++)
979 if (uid == HEADER_DATA(idata->ctx->hdrs[n])->uid)
982 mutt_bcache_del (bcache, id);
987 int imap_cache_clean (IMAP_DATA* idata)
989 idata->bcache = msg_cache_open (idata);
990 mutt_bcache_list (idata->bcache, msg_cache_clean_cb, idata);
995 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
996 * appear in the folder flags list. Why wouldn't they? */
997 void imap_add_keywords (char* s, HEADER* h, LIST* mailbox_flags, size_t slen)
1001 if (!mailbox_flags || !HEADER_DATA(h) || !HEADER_DATA(h)->keywords)
1004 keywords = HEADER_DATA(h)->keywords->next;
1008 if (imap_has_flag (mailbox_flags, keywords->data))
1010 safe_strcat (s, slen, keywords->data);
1011 safe_strcat (s, slen, " ");
1013 keywords = keywords->next;
1017 /* imap_free_header_data: free IMAP_HEADER structure */
1018 void imap_free_header_data (void** data)
1020 /* this should be safe even if the list wasn't used */
1021 mutt_free_list (&(((IMAP_HEADER_DATA*) *data)->keywords));
1023 FREE (data); /* __FREE_CHECKED__ */
1026 /* imap_set_flags: fill out the message header according to the flags from
1027 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
1028 char* imap_set_flags (IMAP_DATA* idata, HEADER* h, char* s)
1030 CONTEXT* ctx = idata->ctx;
1032 IMAP_HEADER_DATA* hd;
1033 unsigned char readonly;
1035 memset (&newh, 0, sizeof (newh));
1039 dprint (2, (debugfile, "imap_fetch_message: parsing FLAGS\n"));
1040 if ((s = msg_parse_flags (&newh, s)) == NULL)
1043 /* YAUH (yet another ugly hack): temporarily set context to
1044 * read-write even if it's read-only, so *server* updates of
1045 * flags can be processed by mutt_set_flag. ctx->changed must
1046 * be restored afterwards */
1047 readonly = ctx->readonly;
1050 mutt_set_flag (ctx, h, M_NEW, !(hd->read || hd->old));
1051 mutt_set_flag (ctx, h, M_OLD, hd->old);
1052 mutt_set_flag (ctx, h, M_READ, hd->read);
1053 mutt_set_flag (ctx, h, M_DELETE, hd->deleted);
1054 mutt_set_flag (ctx, h, M_FLAG, hd->flagged);
1055 mutt_set_flag (ctx, h, M_REPLIED, hd->replied);
1057 /* this message is now definitively *not* changed (mutt_set_flag
1058 * marks things changed as a side-effect) */
1060 ctx->changed &= ~readonly;
1061 ctx->readonly = readonly;
1067 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
1068 * Expects string beginning with * n FETCH.
1071 * -1 if the string is not a fetch response
1072 * -2 if the string is a corrupt fetch response */
1073 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf, FILE* fp)
1077 int rc = -1; /* default now is that string isn't FETCH response*/
1079 idata = (IMAP_DATA*) ctx->data;
1084 /* skip to message number */
1085 buf = imap_next_word (buf);
1086 h->sid = atoi (buf);
1088 /* find FETCH tag */
1089 buf = imap_next_word (buf);
1090 if (ascii_strncasecmp ("FETCH", buf, 5))
1093 rc = -2; /* we've got a FETCH response, for better or worse */
1094 if (!(buf = strchr (buf, '(')))
1098 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
1099 * read header lines and call it again. Silly. */
1100 if ((rc = msg_parse_fetch (h, buf)) != -2 || !fp)
1103 if (imap_get_literal_count (buf, &bytes) == 0)
1105 imap_read_literal (fp, idata, bytes, NULL);
1107 /* we may have other fields of the FETCH _after_ the literal
1108 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
1109 * This all has to go - we should accept literals and nonliterals
1110 * interchangeably at any time. */
1111 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
1114 if (msg_parse_fetch (h, idata->buf) == -1)
1118 rc = 0; /* success */
1120 /* subtract headers from message size - unfortunately only the subset of
1121 * headers we've requested. */
1122 h->content_length -= bytes;
1127 /* msg_parse_fetch: handle headers returned from header fetch */
1128 static int msg_parse_fetch (IMAP_HEADER *h, char *s)
1130 char tmp[SHORT_STRING];
1140 if (ascii_strncasecmp ("FLAGS", s, 5) == 0)
1142 if ((s = msg_parse_flags (h, s)) == NULL)
1145 else if (ascii_strncasecmp ("UID", s, 3) == 0)
1149 h->data->uid = (unsigned int) atoi (s);
1151 s = imap_next_word (s);
1153 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0)
1159 dprint (1, (debugfile, "msg_parse_fetch(): bogus INTERNALDATE entry: %s\n", s));
1164 while (*s && *s != '\"')
1168 s++; /* skip past the trailing " */
1170 h->received = imap_parse_date (tmp);
1172 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0)
1177 while (isdigit ((unsigned char) *s))
1180 h->content_length = atoi (tmp);
1182 else if (!ascii_strncasecmp ("BODY", s, 4) ||
1183 !ascii_strncasecmp ("RFC822.HEADER", s, 13))
1185 /* handle above, in msg_fetch_header */
1189 s++; /* end of request */
1192 /* got something i don't understand */
1193 imap_error ("msg_parse_fetch", s);
1201 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1202 static char* msg_parse_flags (IMAP_HEADER* h, char* s)
1204 IMAP_HEADER_DATA* hd = h->data;
1206 /* sanity-check string */
1207 if (ascii_strncasecmp ("FLAGS", s, 5) != 0)
1209 dprint (1, (debugfile, "msg_parse_flags: not a FLAGS response: %s\n",
1217 dprint (1, (debugfile, "msg_parse_flags: bogus FLAGS response: %s\n",
1223 mutt_free_list (&hd->keywords);
1224 hd->deleted = hd->flagged = hd->replied = hd->read = hd->old = 0;
1227 while (*s && *s != ')')
1229 if (ascii_strncasecmp ("\\deleted", s, 8) == 0)
1234 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0)
1239 else if (ascii_strncasecmp ("\\answered", s, 9) == 0)
1244 else if (ascii_strncasecmp ("\\seen", s, 5) == 0)
1249 else if (ascii_strncasecmp ("\\recent", s, 7) == 0)
1251 else if (ascii_strncasecmp ("old", s, 3) == 0)
1258 /* store custom flags as well */
1260 char* flag_word = s;
1263 hd->keywords = mutt_new_list ();
1265 while (*s && !ISSPACE (*s) && *s != ')')
1269 mutt_add_list (hd->keywords, flag_word);
1275 /* wrap up, or note bad flags response */
1280 dprint (1, (debugfile,
1281 "msg_parse_flags: Unterminated FLAGS response: %s\n", s));
1288 static void flush_buffer(char *buf, size_t *len, CONNECTION *conn)
1291 mutt_socket_write_n(conn, buf, *len);