2 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
3 * Copyright (C) 1999-2002 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
20 /* message parsing/updating functions */
30 #include "mutt_curses.h"
31 #include "imap_private.h"
45 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn);
46 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
48 static int msg_has_flag (LIST * flag_list, const char *flag);
49 static int msg_parse_fetch (IMAP_HEADER * h, char *s);
50 static char *msg_parse_flags (IMAP_HEADER * h, char *s);
53 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
55 static size_t imap_hcache_keylen (const char *fn);
56 #endif /* USE_HCACHE */
59 * Changed to read many headers instead of just one. It will return the
60 * msgno of the last message read. It will return a value other than
61 * msgend if mail comes in while downloading headers (in theory).
63 int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend)
66 char buf[LONG_STRING];
69 char tempfile[_POSIX_PATH_MAX];
72 int rc, mfhrc, oldmsgcount;
74 const char *want_headers =
75 "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO LINES LIST-POST X-LABEL";
79 uint64_t *uid_validity = NULL;
81 #endif /* USE_HCACHE */
86 hc = mutt_hcache_open (HeaderCache, ctx->path);
87 #endif /* USE_HCACHE */
89 if (mutt_bit_isset (idata->capabilities, IMAP4REV1)) {
90 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s%s%s)]",
91 want_headers, ImapHeaders ? " " : "",
92 ImapHeaders ? ImapHeaders : "");
94 else if (mutt_bit_isset (idata->capabilities, IMAP4)) {
95 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s%s%s)",
96 want_headers, ImapHeaders ? " " : "",
97 ImapHeaders ? ImapHeaders : "");
99 else { /* Unable to fetch headers for lower versions */
100 mutt_error _("Unable to fetch headers from this IMAP server version.");
102 mutt_sleep (2); /* pause a moment to let the user see the error */
104 mutt_hcache_close (hc);
105 #endif /* USE_HCACHE */
109 /* instead of downloading all headers and then parsing them, we parse them
110 * as they come in. */
111 mutt_mktemp (tempfile);
112 if (!(fp = safe_fopen (tempfile, "w+"))) {
113 mutt_error (_("Could not create temporary file %s"), tempfile);
116 mutt_hcache_close (hc);
117 #endif /* USE_HCACHE */
122 /* make sure context has room to hold the mailbox */
123 while ((msgend) >= idata->ctx->hdrmax)
124 mx_alloc_memory (idata->ctx);
126 oldmsgcount = ctx->msgcount;
127 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
128 idata->newMailCount = 0;
131 snprintf (buf, sizeof (buf),
132 "FETCH %d:%d (UID FLAGS)", msgbegin + 1, msgend + 1);
133 fetchlast = msgend + 1;
135 imap_cmd_start (idata, buf);
137 for (msgno = msgbegin; msgno <= msgend; msgno++) {
138 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
139 mutt_message (_("Evaluating cache... [%d/%d]"), msgno + 1, msgend + 1);
142 memset (&h, 0, sizeof (h));
143 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
147 rc = imap_cmd_step (idata);
148 if (rc != IMAP_CMD_CONTINUE)
152 msg_fetch_header_fetch (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
157 /* make sure we don't get remnants from older larger message headers */
160 sprintf (uid_buf, "/%u", h.data->uid); /* XXX --tg 21:41 04-07-11 */
162 (uint64_t *) mutt_hcache_fetch (hc, uid_buf, &imap_hcache_keylen);
164 if (uid_validity != NULL && *uid_validity == idata->uid_validity) {
166 mutt_hcache_restore ((unsigned char *) uid_validity, 0);
167 ctx->hdrs[msgno]->index = h.sid - 1;
168 if (h.sid != ctx->msgcount + 1)
171 "imap_read_headers: msgcount and sequence ID are inconsistent!"));
172 /* messages which have not been expunged are ACTIVE (borrowed from mh
174 ctx->hdrs[msgno]->active = 1;
175 ctx->hdrs[msgno]->read = h.read;
176 ctx->hdrs[msgno]->old = h.old;
177 ctx->hdrs[msgno]->deleted = h.deleted;
178 ctx->hdrs[msgno]->flagged = h.flagged;
179 ctx->hdrs[msgno]->replied = h.replied;
180 ctx->hdrs[msgno]->changed = h.changed;
181 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
182 ctx->hdrs[msgno]->data = (void *) (h.data);
188 FREE (&uid_validity);
191 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
192 ((msgno + 1) >= fetchlast)));
194 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
195 imap_free_header_data ((void **) &h.data);
197 mutt_hcache_close (hc);
202 fetchlast = msgbegin;
203 #endif /* USE_HCACHE */
205 for (msgno = msgbegin; msgno <= msgend; msgno++) {
206 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
207 mutt_message (_("Fetching message headers... [%d/%d]"), msgno + 1,
210 if (ctx->hdrs[msgno])
213 if (msgno + 1 > fetchlast) {
214 fetchlast = msgno + 1;
215 while ((fetchlast <= msgend) && (!ctx->hdrs[fetchlast]))
219 * Make one request for everything. This makes fetching headers an
220 * order of magnitude faster if you have a large mailbox.
222 * If we get more messages while doing this, we make another
223 * request for all the new messages.
225 snprintf (buf, sizeof (buf),
226 "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)",
227 msgno + 1, fetchlast, hdrreq);
229 imap_cmd_start (idata, buf);
234 memset (&h, 0, sizeof (h));
235 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
237 /* this DO loop does two things:
238 * 1. handles untagged messages, so we can try again on the same msg
239 * 2. fetches the tagged response at the end of the last message.
244 rc = imap_cmd_step (idata);
245 if (rc != IMAP_CMD_CONTINUE)
249 msg_fetch_header (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
254 /* make sure we don't get remnants from older larger message headers */
257 /* update context with message header */
258 ctx->hdrs[msgno] = mutt_new_header ();
260 ctx->hdrs[msgno]->index = h.sid - 1;
261 if (h.sid != ctx->msgcount + 1)
264 "imap_read_headers: msgcount and sequence ID are inconsistent!"));
265 /* messages which have not been expunged are ACTIVE (borrowed from mh
267 ctx->hdrs[msgno]->active = 1;
268 ctx->hdrs[msgno]->read = h.read;
269 ctx->hdrs[msgno]->old = h.old;
270 ctx->hdrs[msgno]->deleted = h.deleted;
271 ctx->hdrs[msgno]->flagged = h.flagged;
272 ctx->hdrs[msgno]->replied = h.replied;
273 ctx->hdrs[msgno]->changed = h.changed;
274 ctx->hdrs[msgno]->received = h.received;
275 ctx->hdrs[msgno]->data = (void *) (h.data);
278 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
279 * on h.received being set */
280 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno],
282 /* content built as a side-effect of mutt_read_rfc822_header */
283 ctx->hdrs[msgno]->content->length = h.content_length;
286 sprintf (uid_buf, "/%u", h.data->uid);
287 mutt_hcache_store (hc, uid_buf, ctx->hdrs[msgno], idata->uid_validity,
288 &imap_hcache_keylen);
289 #endif /* USE_HCACHE */
293 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
294 ((msgno + 1) >= fetchlast)));
296 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
297 imap_free_header_data ((void **) &h.data);
300 mutt_hcache_close (hc);
301 #endif /* USE_HCACHE */
305 /* in case we get new mail while fetching the headers */
306 if (idata->reopen & IMAP_NEWMAIL_PENDING) {
307 msgend = idata->newMailCount - 1;
308 while ((msgend) >= ctx->hdrmax)
309 mx_alloc_memory (ctx);
310 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
311 idata->newMailCount = 0;
316 mutt_hcache_close (hc);
317 #endif /* USE_HCACHE */
321 if (ctx->msgcount > oldmsgcount)
322 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
327 int imap_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
331 char buf[LONG_STRING];
332 char path[_POSIX_PATH_MAX];
341 /* Sam's weird courier server returns an OK response even when FETCH
342 * fails. Thanks Sam. */
345 idata = (IMAP_DATA *) ctx->data;
346 h = ctx->hdrs[msgno];
348 /* see if we already have the message in our cache */
349 cacheno = HEADER_DATA (h)->uid % IMAP_CACHE_LEN;
350 cache = &idata->cache[cacheno];
353 if (cache->uid == HEADER_DATA (h)->uid) {
354 /* yes, so just return a pointer to the message */
355 if (!(msg->fp = fopen (cache->path, "r"))) {
356 mutt_perror (cache->path);
362 /* clear the previous entry */
363 unlink (cache->path);
369 mutt_message _("Fetching message...");
371 cache->uid = HEADER_DATA (h)->uid;
373 cache->path = safe_strdup (path);
374 if (!(msg->fp = safe_fopen (path, "w+"))) {
379 /* mark this header as currently inactive so the command handler won't
380 * also try to update it. HACK until all this code can be moved into the
384 snprintf (buf, sizeof (buf), "UID FETCH %u %s", HEADER_DATA (h)->uid,
385 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
386 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") : "RFC822"));
388 imap_cmd_start (idata, buf);
390 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
394 pc = imap_next_word (pc);
395 pc = imap_next_word (pc);
397 if (!ascii_strncasecmp ("FETCH", pc, 5)) {
399 pc = imap_next_word (pc);
402 if (ascii_strncasecmp ("UID", pc, 3) == 0) {
403 pc = imap_next_word (pc);
405 if (uid != HEADER_DATA (h)->uid)
407 ("The message index is incorrect. Try reopening the mailbox."));
409 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
410 (ascii_strncasecmp ("BODY[]", pc, 6) == 0)) {
411 pc = imap_next_word (pc);
412 if (imap_get_literal_count (pc, &bytes) < 0) {
413 imap_error ("imap_fetch_message()", buf);
416 if (imap_read_literal (msg->fp, idata, bytes) < 0)
418 /* pick up trailing line */
419 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
425 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
426 * change (eg from \Unseen to \Seen).
427 * Uncommitted changes in mutt take precedence. If we decide to
428 * incrementally update flags later, this won't stop us syncing */
429 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed) {
430 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
436 while (rc == IMAP_CMD_CONTINUE);
438 /* see comment before command start. */
441 if (rc != IMAP_CMD_OK)
444 if (!fetched || !imap_code (idata->cmd.buf))
447 /* Update the header information. Previously, we only downloaded a
448 * portion of the headers, those required for the main display.
451 /* It may be that the Status header indicates a message is read, but the
452 * IMAP server doesn't know the message has been \Seen. So we capture
453 * the server's notion of 'read' and if it differs from the message info
454 * picked up in mutt_read_rfc822_header, we mark the message (and context
455 * changed). Another possiblity: ignore Status on IMAP?*/
457 /* I hate do this here, since it's so low-level, but I'm not sure where
458 * I can abstract it. Problem: the id and subj hashes lose their keys when
459 * mutt_free_envelope gets called, but keep their spots in the hash. This
460 * confuses threading. Alternatively we could try to merge the new
461 * envelope into the old one. Also messy and lowlevel. */
462 if (ctx->id_hash && h->env->message_id)
463 hash_delete (ctx->id_hash, h->env->message_id, h, NULL);
464 if (ctx->subj_hash && h->env->real_subj)
465 hash_delete (ctx->subj_hash, h->env->real_subj, h, NULL);
466 mutt_free_envelope (&h->env);
467 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
468 if (ctx->id_hash && h->env->message_id)
469 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
470 if (ctx->subj_hash && h->env->real_subj)
471 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
473 /* see above. We want the new status in h->read, so we unset it manually
474 * and let mutt_set_flag set it correctly, updating context. */
475 if (read != h->read) {
477 mutt_set_flag (ctx, h, M_NEW, read);
481 fgets (buf, sizeof (buf), msg->fp);
482 while (!feof (msg->fp)) {
484 fgets (buf, sizeof (buf), msg->fp);
487 h->content->length = ftell (msg->fp) - h->content->offset;
489 /* This needs to be done in case this is a multipart message */
490 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
491 h->security = crypt_query (h->content);
500 safe_fclose (&msg->fp);
502 unlink (cache->path);
509 int imap_append_message (CONTEXT * ctx, MESSAGE * msg)
513 char buf[LONG_STRING];
514 char mbox[LONG_STRING];
515 char mailbox[LONG_STRING];
521 idata = (IMAP_DATA *) ctx->data;
523 if (imap_parse_path (ctx->path, &mx))
526 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
528 if ((fp = fopen (msg->path, "r")) == NULL) {
529 mutt_perror (msg->path);
533 /* currently we set the \Seen flag on all messages, but probably we
534 * should scan the message Status header for flag info. Since we're
535 * already rereading the whole file for length it isn't any more
536 * expensive (it'd be nice if we had the file size passed in already
537 * by the code that writes the file, but that's a lot of changes.
538 * Ideally we'd have a HEADER structure with flag info here... */
539 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
540 if (c == '\n' && last != '\r')
547 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
548 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) {%lu}", mbox,
549 msg->flags.read ? "\\Seen" : "",
550 msg->flags.read && (msg->flags.replied
551 || msg->flags.flagged) ? " " : "",
552 msg->flags.replied ? "\\Answered" : "", msg->flags.replied
553 && msg->flags.flagged ? " " : "",
554 msg->flags.flagged ? "\\Flagged" : "", (unsigned long) len);
556 imap_cmd_start (idata, buf);
559 rc = imap_cmd_step (idata);
560 while (rc == IMAP_CMD_CONTINUE);
562 if (rc != IMAP_CMD_RESPOND) {
565 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
568 pc = idata->cmd.buf + SEQLEN;
570 pc = imap_next_word (pc);
571 mutt_error ("%s", pc);
577 mutt_message _("Uploading message ...");
579 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
580 if (c == '\n' && last != '\r')
585 if (len > sizeof (buf) - 3)
586 flush_buffer (buf, &len, idata->conn);
590 flush_buffer (buf, &len, idata->conn);
592 mutt_socket_write (idata->conn, "\r\n");
596 rc = imap_cmd_step (idata);
597 while (rc == IMAP_CMD_CONTINUE);
599 if (!imap_code (idata->cmd.buf)) {
602 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
604 pc = idata->cmd.buf + SEQLEN;
606 pc = imap_next_word (pc);
607 mutt_error ("%s", pc);
620 /* imap_copy_messages: use server COPY command to copy messages to another
625 * 1: non-fatal error - try fetch/append */
626 int imap_copy_messages (CONTEXT * ctx, HEADER * h, char *dest, int delete)
631 char mbox[LONG_STRING];
632 char mmbox[LONG_STRING];
637 idata = (IMAP_DATA *) ctx->data;
639 if (imap_parse_path (dest, &mx)) {
640 dprint (1, (debugfile, "imap_copy_messages: bad destination %s\n", dest));
644 /* check that the save-to folder is in the same account */
645 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account))) {
646 dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n",
651 if (h && h->attach_del) {
654 "imap_copy_messages: Message contains attachments to be deleted\n"));
658 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
660 memset (&cmd, 0, sizeof (cmd));
661 mutt_buffer_addstr (&cmd, "UID COPY ");
663 /* Null HEADER* means copy tagged messages */
665 /* if any messages have attachments to delete, fall through to FETCH
666 * and APPEND. TODO: Copy what we can with COPY, fall through for the
668 for (n = 0; n < ctx->msgcount; n++) {
669 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del) {
672 "imap_copy_messages: Message contains attachments to be deleted\n"));
677 rc = imap_make_msg_set (idata, &cmd, M_TAG, 0);
679 dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
682 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
685 mutt_message (_("Copying message %d to %s..."), h->index + 1, mbox);
686 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
687 mutt_buffer_addstr (&cmd, uid);
690 /* let's get it on */
691 mutt_buffer_addstr (&cmd, " ");
692 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
693 mutt_buffer_addstr (&cmd, mmbox);
695 rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
697 /* bail out if command failed for reasons other than nonexistent target */
698 if (ascii_strncasecmp
699 (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11)) {
700 imap_error ("imap_copy_messages", idata->cmd.buf);
704 (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
705 snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
706 if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1) {
710 if (imap_create_mailbox (idata, mbox) < 0)
714 rc = imap_exec (idata, cmd.data, 0);
717 imap_error ("imap_copy_messages", idata->cmd.buf);
724 for (n = 0; n < ctx->msgcount; n++) {
725 if (ctx->hdrs[n]->tagged) {
726 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
727 mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
728 if (option (OPTDELETEUNTAG))
729 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
733 mutt_set_flag (ctx, h, M_DELETE, 1);
734 mutt_set_flag (ctx, h, M_APPENDED, 1);
735 if (option (OPTDELETEUNTAG))
736 mutt_set_flag (ctx, h, M_TAG, 0);
752 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
753 * appear in the folder flags list. Why wouldn't they? */
754 void imap_add_keywords (char *s, HEADER * h, LIST * mailbox_flags,
759 if (!mailbox_flags || !HEADER_DATA (h) || !HEADER_DATA (h)->keywords)
762 keywords = HEADER_DATA (h)->keywords->next;
765 if (msg_has_flag (mailbox_flags, keywords->data)) {
766 safe_strcat (s, slen, keywords->data);
767 safe_strcat (s, slen, " ");
769 keywords = keywords->next;
773 /* imap_free_header_data: free IMAP_HEADER structure */
774 void imap_free_header_data (void **data)
776 /* this should be safe even if the list wasn't used */
777 mutt_free_list (&(((IMAP_HEADER_DATA *) * data)->keywords));
782 /* imap_set_flags: fill out the message header according to the flags from
783 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
784 char *imap_set_flags (IMAP_DATA * idata, HEADER * h, char *s)
786 CONTEXT *ctx = idata->ctx;
788 unsigned char readonly;
790 memset (&newh, 0, sizeof (newh));
791 newh.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
793 dprint (2, (debugfile, "imap_fetch_message: parsing FLAGS\n"));
794 if ((s = msg_parse_flags (&newh, s)) == NULL) {
799 /* YAUH (yet another ugly hack): temporarily set context to
800 * read-write even if it's read-only, so *server* updates of
801 * flags can be processed by mutt_set_flag. ctx->changed must
802 * be restored afterwards */
803 readonly = ctx->readonly;
806 mutt_set_flag (ctx, h, M_NEW, !(newh.read || newh.old));
807 mutt_set_flag (ctx, h, M_OLD, newh.old);
808 mutt_set_flag (ctx, h, M_READ, newh.read);
809 mutt_set_flag (ctx, h, M_DELETE, newh.deleted);
810 mutt_set_flag (ctx, h, M_FLAG, newh.flagged);
811 mutt_set_flag (ctx, h, M_REPLIED, newh.replied);
813 /* this message is now definitively *not* changed (mutt_set_flag
814 * marks things changed as a side-effect) */
816 ctx->changed &= ~readonly;
817 ctx->readonly = readonly;
819 mutt_free_list (&(HEADER_DATA (h)->keywords));
820 HEADER_DATA (h)->keywords = newh.data->keywords;
827 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
828 * Expects string beginning with * n FETCH.
831 * -1 if the string is not a fetch response
832 * -2 if the string is a corrupt fetch response */
833 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
838 int rc = -1; /* default now is that string isn't FETCH response */
840 idata = (IMAP_DATA *) ctx->data;
845 /* skip to message number */
846 buf = imap_next_word (buf);
850 buf = imap_next_word (buf);
851 if (ascii_strncasecmp ("FETCH", buf, 5))
854 rc = -2; /* we've got a FETCH response, for better or worse */
855 if (!(buf = strchr (buf, '(')))
859 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
860 * read header lines and call it again. Silly. */
861 if (msg_parse_fetch (h, buf) != -2)
864 if (imap_get_literal_count (buf, &bytes) == 0) {
865 imap_read_literal (fp, idata, bytes);
867 /* we may have other fields of the FETCH _after_ the literal
868 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
869 * This all has to go - we should accept literals and nonliterals
870 * interchangeably at any time. */
871 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
874 if (msg_parse_fetch (h, idata->cmd.buf) == -1)
878 rc = 0; /* success */
880 /* subtract headers from message size - unfortunately only the subset of
881 * headers we've requested. */
882 h->content_length -= bytes;
888 static size_t imap_hcache_keylen (const char *fn)
890 return mutt_strlen (fn);
893 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
894 * Expects string beginning with * n FETCH.
897 * -1 if the string is not a fetch response
898 * -2 if the string is a corrupt fetch response */
899 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
903 int rc = -1; /* default now is that string isn't FETCH response */
905 idata = (IMAP_DATA *) ctx->data;
910 /* skip to message number */
911 buf = imap_next_word (buf);
915 buf = imap_next_word (buf);
916 if (ascii_strncasecmp ("FETCH", buf, 5))
919 rc = -2; /* we've got a FETCH response, for better or worse */
920 if (!(buf = strchr (buf, '(')))
924 if (msg_parse_fetch (h, buf) < 0) {
928 if (!(buf = strchr (buf, ')')))
934 #endif /* USE_HCACHE */
937 /* msg_has_flag: do a caseless comparison of the flag against a flag list,
938 * return 1 if found or flag list has '\*', 0 otherwise */
939 static int msg_has_flag (LIST * flag_list, const char *flag)
944 flag_list = flag_list->next;
946 if (!ascii_strncasecmp (flag_list->data, flag, strlen (flag_list->data)))
949 flag_list = flag_list->next;
955 /* msg_parse_fetch: handle headers returned from header fetch */
956 static int msg_parse_fetch (IMAP_HEADER * h, char *s)
958 char tmp[SHORT_STRING];
967 if (ascii_strncasecmp ("FLAGS", s, 5) == 0) {
968 if ((s = msg_parse_flags (h, s)) == NULL)
971 else if (ascii_strncasecmp ("UID", s, 3) == 0) {
974 h->data->uid = (unsigned int) atoi (s);
976 s = imap_next_word (s);
978 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0) {
984 "msg_parse_fetch(): bogus INTERNALDATE entry: %s\n", s));
989 while (*s && *s != '\"')
993 s++; /* skip past the trailing " */
995 h->received = imap_parse_date (tmp);
997 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0) {
1001 while (isdigit ((unsigned char) *s))
1004 h->content_length = atoi (tmp);
1006 else if (!ascii_strncasecmp ("BODY", s, 4) ||
1007 !ascii_strncasecmp ("RFC822.HEADER", s, 13)) {
1008 /* handle above, in msg_fetch_header */
1012 s++; /* end of request */
1014 /* got something i don't understand */
1015 imap_error ("msg_parse_fetch", s);
1023 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1024 static char *msg_parse_flags (IMAP_HEADER * h, char *s)
1028 /* sanity-check string */
1029 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
1030 dprint (1, (debugfile, "msg_parse_flags: not a FLAGS response: %s\n", s));
1036 dprint (1, (debugfile, "msg_parse_flags: bogus FLAGS response: %s\n", s));
1042 while (*s && *s != ')') {
1043 if (ascii_strncasecmp ("\\deleted", s, 8) == 0) {
1047 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0) {
1051 else if (ascii_strncasecmp ("\\answered", s, 9) == 0) {
1055 else if (ascii_strncasecmp ("\\seen", s, 5) == 0) {
1059 else if (ascii_strncasecmp ("\\recent", s, 5) == 0) {
1064 /* store custom flags as well */
1066 char *flag_word = s;
1068 if (!h->data->keywords)
1069 h->data->keywords = mutt_new_list ();
1071 while (*s && !ISSPACE (*s) && *s != ')')
1075 mutt_add_list (h->data->keywords, flag_word);
1081 /* wrap up, or note bad flags response */
1083 /* if a message is neither seen nor recent, it is OLD. */
1084 if (option (OPTMARKOLD) && !recent && !(h->read))
1089 dprint (1, (debugfile,
1090 "msg_parse_flags: Unterminated FLAGS response: %s\n", s));
1097 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn)
1100 mutt_socket_write (conn, buf);