2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-9 Brandon Long <blong@fiction.net>
4 * Copyright (C) 1999-2002 Brendan Cully <brendan@kublai.com>
6 * This file is part of mutt-ng, see http://www.muttng.org/.
7 * It's licensed under the GNU General Public License,
8 * please see the file GPL in the top level source directory.
11 /* message parsing/updating functions */
21 #include "mutt_curses.h"
22 #include "imap_private.h"
40 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn);
41 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
43 static int msg_has_flag (LIST * flag_list, const char *flag);
44 static int msg_parse_fetch (IMAP_HEADER * h, char *s);
45 static char *msg_parse_flags (IMAP_HEADER * h, char *s);
48 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
50 static size_t imap_hcache_keylen (const char *fn);
51 #endif /* USE_HCACHE */
54 * Changed to read many headers instead of just one. It will return the
55 * msgno of the last message read. It will return a value other than
56 * msgend if mail comes in while downloading headers (in theory).
58 int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend)
61 char buf[LONG_STRING];
64 char tempfile[_POSIX_PATH_MAX];
67 int rc, mfhrc, oldmsgcount;
69 const char *want_headers =
70 "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO LINES LIST-POST X-LABEL";
74 unsigned long *uid_validity = NULL;
76 #endif /* USE_HCACHE */
81 hc = mutt_hcache_open (HeaderCache, ctx->path);
82 #endif /* USE_HCACHE */
84 if (mutt_bit_isset (idata->capabilities, IMAP4REV1)) {
85 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s%s%s)]",
86 want_headers, ImapHeaders ? " " : "",
87 ImapHeaders ? ImapHeaders : "");
89 else if (mutt_bit_isset (idata->capabilities, IMAP4)) {
90 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s%s%s)",
91 want_headers, ImapHeaders ? " " : "",
92 ImapHeaders ? ImapHeaders : "");
94 else { /* Unable to fetch headers for lower versions */
95 mutt_error _("Unable to fetch headers from this IMAP server version.");
97 mutt_sleep (2); /* pause a moment to let the user see the error */
99 mutt_hcache_close (hc);
100 #endif /* USE_HCACHE */
104 /* instead of downloading all headers and then parsing them, we parse them
105 * as they come in. */
106 mutt_mktemp (tempfile);
107 if (!(fp = safe_fopen (tempfile, "w+"))) {
108 mutt_error (_("Could not create temporary file %s"), tempfile);
111 mutt_hcache_close (hc);
112 #endif /* USE_HCACHE */
117 /* make sure context has room to hold the mailbox */
118 while ((msgend) >= idata->ctx->hdrmax)
119 mx_alloc_memory (idata->ctx);
121 oldmsgcount = ctx->msgcount;
122 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
123 idata->newMailCount = 0;
126 snprintf (buf, sizeof (buf),
127 "FETCH %d:%d (UID FLAGS)", msgbegin + 1, msgend + 1);
128 fetchlast = msgend + 1;
130 imap_cmd_start (idata, buf);
132 for (msgno = msgbegin; msgno <= msgend; msgno++) {
133 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
134 mutt_message (_("Evaluating cache... [%d/%d]"), msgno + 1, msgend + 1);
137 memset (&h, 0, sizeof (h));
138 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
142 rc = imap_cmd_step (idata);
143 if (rc != IMAP_CMD_CONTINUE)
147 msg_fetch_header_fetch (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
152 /* make sure we don't get remnants from older larger message headers */
155 sprintf (uid_buf, "/%u", h.data->uid); /* XXX --tg 21:41 04-07-11 */
157 (unsigned long *) mutt_hcache_fetch (hc, uid_buf, &imap_hcache_keylen);
159 if (uid_validity != NULL && *uid_validity == idata->uid_validity) {
161 mutt_hcache_restore ((unsigned char *) uid_validity, 0);
162 ctx->hdrs[msgno]->index = h.sid - 1;
163 if (h.sid != ctx->msgcount + 1)
166 "imap_read_headers: msgcount and sequence ID are inconsistent!"));
167 /* messages which have not been expunged are ACTIVE (borrowed from mh
169 ctx->hdrs[msgno]->active = 1;
170 ctx->hdrs[msgno]->read = h.read;
171 ctx->hdrs[msgno]->old = h.old;
172 ctx->hdrs[msgno]->deleted = h.deleted;
173 ctx->hdrs[msgno]->flagged = h.flagged;
174 ctx->hdrs[msgno]->replied = h.replied;
175 ctx->hdrs[msgno]->changed = h.changed;
176 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
177 ctx->hdrs[msgno]->data = (void *) (h.data);
183 FREE (&uid_validity);
186 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
187 ((msgno + 1) >= fetchlast)));
189 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
190 imap_free_header_data ((void **) &h.data);
192 mutt_hcache_close (hc);
197 fetchlast = msgbegin;
198 #endif /* USE_HCACHE */
200 for (msgno = msgbegin; msgno <= msgend; msgno++) {
201 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
202 mutt_message (_("Fetching message headers... [%d/%d]"), msgno + 1,
205 if (ctx->hdrs[msgno])
208 if (msgno + 1 > fetchlast) {
209 fetchlast = msgno + 1;
210 while ((fetchlast <= msgend) && (!ctx->hdrs[fetchlast]))
214 * Make one request for everything. This makes fetching headers an
215 * order of magnitude faster if you have a large mailbox.
217 * If we get more messages while doing this, we make another
218 * request for all the new messages.
220 snprintf (buf, sizeof (buf),
221 "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)",
222 msgno + 1, fetchlast, hdrreq);
224 imap_cmd_start (idata, buf);
229 memset (&h, 0, sizeof (h));
230 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
232 /* this DO loop does two things:
233 * 1. handles untagged messages, so we can try again on the same msg
234 * 2. fetches the tagged response at the end of the last message.
239 rc = imap_cmd_step (idata);
240 if (rc != IMAP_CMD_CONTINUE)
244 msg_fetch_header (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
249 /* make sure we don't get remnants from older larger message headers */
252 /* update context with message header */
253 ctx->hdrs[msgno] = mutt_new_header ();
255 ctx->hdrs[msgno]->index = h.sid - 1;
256 if (h.sid != ctx->msgcount + 1)
259 "imap_read_headers: msgcount and sequence ID are inconsistent!"));
260 /* messages which have not been expunged are ACTIVE (borrowed from mh
262 ctx->hdrs[msgno]->active = 1;
263 ctx->hdrs[msgno]->read = h.read;
264 ctx->hdrs[msgno]->old = h.old;
265 ctx->hdrs[msgno]->deleted = h.deleted;
266 ctx->hdrs[msgno]->flagged = h.flagged;
267 ctx->hdrs[msgno]->replied = h.replied;
268 ctx->hdrs[msgno]->changed = h.changed;
269 ctx->hdrs[msgno]->received = h.received;
270 ctx->hdrs[msgno]->data = (void *) (h.data);
273 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
274 * on h.received being set */
275 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno],
277 /* content built as a side-effect of mutt_read_rfc822_header */
278 ctx->hdrs[msgno]->content->length = h.content_length;
281 sprintf (uid_buf, "/%u", h.data->uid);
282 mutt_hcache_store (hc, uid_buf, ctx->hdrs[msgno], idata->uid_validity,
283 &imap_hcache_keylen);
284 #endif /* USE_HCACHE */
288 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
289 ((msgno + 1) >= fetchlast)));
291 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
292 imap_free_header_data ((void **) &h.data);
295 mutt_hcache_close (hc);
296 #endif /* USE_HCACHE */
300 /* in case we get new mail while fetching the headers */
301 if (idata->reopen & IMAP_NEWMAIL_PENDING) {
302 msgend = idata->newMailCount - 1;
303 while ((msgend) >= ctx->hdrmax)
304 mx_alloc_memory (ctx);
305 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
306 idata->newMailCount = 0;
311 mutt_hcache_close (hc);
312 #endif /* USE_HCACHE */
316 if (ctx->msgcount > oldmsgcount)
317 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
322 int imap_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
326 char buf[LONG_STRING];
327 char path[_POSIX_PATH_MAX];
336 /* Sam's weird courier server returns an OK response even when FETCH
337 * fails. Thanks Sam. */
340 idata = (IMAP_DATA *) ctx->data;
341 h = ctx->hdrs[msgno];
343 /* see if we already have the message in our cache */
344 cacheno = HEADER_DATA (h)->uid % IMAP_CACHE_LEN;
345 cache = &idata->cache[cacheno];
348 if (cache->uid == HEADER_DATA (h)->uid) {
349 /* yes, so just return a pointer to the message */
350 if (!(msg->fp = fopen (cache->path, "r"))) {
351 mutt_perror (cache->path);
357 /* clear the previous entry */
358 unlink (cache->path);
364 mutt_message _("Fetching message...");
366 cache->uid = HEADER_DATA (h)->uid;
368 cache->path = safe_strdup (path);
369 if (!(msg->fp = safe_fopen (path, "w+"))) {
374 /* mark this header as currently inactive so the command handler won't
375 * also try to update it. HACK until all this code can be moved into the
379 snprintf (buf, sizeof (buf), "UID FETCH %u %s", HEADER_DATA (h)->uid,
380 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
381 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") : "RFC822"));
383 imap_cmd_start (idata, buf);
385 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
389 pc = imap_next_word (pc);
390 pc = imap_next_word (pc);
392 if (!ascii_strncasecmp ("FETCH", pc, 5)) {
394 pc = imap_next_word (pc);
397 if (ascii_strncasecmp ("UID", pc, 3) == 0) {
398 pc = imap_next_word (pc);
400 if (uid != HEADER_DATA (h)->uid)
402 ("The message index is incorrect. Try reopening the mailbox."));
404 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
405 (ascii_strncasecmp ("BODY[]", pc, 6) == 0)) {
406 pc = imap_next_word (pc);
407 if (imap_get_literal_count (pc, &bytes) < 0) {
408 imap_error ("imap_fetch_message()", buf);
411 if (imap_read_literal (msg->fp, idata, bytes) < 0)
413 /* pick up trailing line */
414 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
420 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
421 * change (eg from \Unseen to \Seen).
422 * Uncommitted changes in mutt take precedence. If we decide to
423 * incrementally update flags later, this won't stop us syncing */
424 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed) {
425 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
431 while (rc == IMAP_CMD_CONTINUE);
433 /* see comment before command start. */
436 if (rc != IMAP_CMD_OK)
439 if (!fetched || !imap_code (idata->cmd.buf))
442 /* Update the header information. Previously, we only downloaded a
443 * portion of the headers, those required for the main display.
446 /* It may be that the Status header indicates a message is read, but the
447 * IMAP server doesn't know the message has been \Seen. So we capture
448 * the server's notion of 'read' and if it differs from the message info
449 * picked up in mutt_read_rfc822_header, we mark the message (and context
450 * changed). Another possiblity: ignore Status on IMAP?*/
452 /* I hate do this here, since it's so low-level, but I'm not sure where
453 * I can abstract it. Problem: the id and subj hashes lose their keys when
454 * mutt_free_envelope gets called, but keep their spots in the hash. This
455 * confuses threading. Alternatively we could try to merge the new
456 * envelope into the old one. Also messy and lowlevel. */
457 if (ctx->id_hash && h->env->message_id)
458 hash_delete (ctx->id_hash, h->env->message_id, h, NULL);
459 if (ctx->subj_hash && h->env->real_subj)
460 hash_delete (ctx->subj_hash, h->env->real_subj, h, NULL);
461 mutt_free_envelope (&h->env);
462 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
463 if (ctx->id_hash && h->env->message_id)
464 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
465 if (ctx->subj_hash && h->env->real_subj)
466 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
468 /* see above. We want the new status in h->read, so we unset it manually
469 * and let mutt_set_flag set it correctly, updating context. */
470 if (read != h->read) {
472 mutt_set_flag (ctx, h, M_NEW, read);
476 fgets (buf, sizeof (buf), msg->fp);
477 while (!feof (msg->fp)) {
479 fgets (buf, sizeof (buf), msg->fp);
482 h->content->length = ftell (msg->fp) - h->content->offset;
484 /* This needs to be done in case this is a multipart message */
485 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
486 h->security = crypt_query (h->content);
495 safe_fclose (&msg->fp);
497 unlink (cache->path);
504 int imap_append_message (CONTEXT * ctx, MESSAGE * msg)
508 char buf[LONG_STRING];
509 char mbox[LONG_STRING];
510 char mailbox[LONG_STRING];
516 idata = (IMAP_DATA *) ctx->data;
518 if (imap_parse_path (ctx->path, &mx))
521 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
523 if ((fp = fopen (msg->path, "r")) == NULL) {
524 mutt_perror (msg->path);
528 /* currently we set the \Seen flag on all messages, but probably we
529 * should scan the message Status header for flag info. Since we're
530 * already rereading the whole file for length it isn't any more
531 * expensive (it'd be nice if we had the file size passed in already
532 * by the code that writes the file, but that's a lot of changes.
533 * Ideally we'd have a HEADER structure with flag info here... */
534 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
535 if (c == '\n' && last != '\r')
542 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
543 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) {%lu}", mbox,
544 msg->flags.read ? "\\Seen" : "",
545 msg->flags.read && (msg->flags.replied
546 || msg->flags.flagged) ? " " : "",
547 msg->flags.replied ? "\\Answered" : "", msg->flags.replied
548 && msg->flags.flagged ? " " : "",
549 msg->flags.flagged ? "\\Flagged" : "", (unsigned long) len);
551 imap_cmd_start (idata, buf);
554 rc = imap_cmd_step (idata);
555 while (rc == IMAP_CMD_CONTINUE);
557 if (rc != IMAP_CMD_RESPOND) {
560 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
563 pc = idata->cmd.buf + SEQLEN;
565 pc = imap_next_word (pc);
566 mutt_error ("%s", pc);
572 mutt_message _("Uploading message ...");
574 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
575 if (c == '\n' && last != '\r')
580 if (len > sizeof (buf) - 3)
581 flush_buffer (buf, &len, idata->conn);
585 flush_buffer (buf, &len, idata->conn);
587 mutt_socket_write (idata->conn, "\r\n");
591 rc = imap_cmd_step (idata);
592 while (rc == IMAP_CMD_CONTINUE);
594 if (!imap_code (idata->cmd.buf)) {
597 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
599 pc = idata->cmd.buf + SEQLEN;
601 pc = imap_next_word (pc);
602 mutt_error ("%s", pc);
615 /* imap_copy_messages: use server COPY command to copy messages to another
620 * 1: non-fatal error - try fetch/append */
621 int imap_copy_messages (CONTEXT * ctx, HEADER * h, char *dest, int delete)
624 BUFFER cmd, sync_cmd;
626 char mbox[LONG_STRING];
627 char mmbox[LONG_STRING];
631 int err_continue = M_NO;
633 idata = (IMAP_DATA *) ctx->data;
635 if (imap_parse_path (dest, &mx)) {
636 dprint (1, (debugfile, "imap_copy_messages: bad destination %s\n", dest));
640 /* check that the save-to folder is in the same account */
641 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account))) {
642 dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n",
647 if (h && h->attach_del) {
650 "imap_copy_messages: Message contains attachments to be deleted\n"));
654 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
656 memset (&sync_cmd, 0, sizeof (sync_cmd));
657 memset (&cmd, 0, sizeof (cmd));
658 mutt_buffer_addstr (&cmd, "UID COPY ");
660 /* Null HEADER* means copy tagged messages */
662 /* if any messages have attachments to delete, fall through to FETCH
663 * and APPEND. TODO: Copy what we can with COPY, fall through for the
665 for (n = 0; n < ctx->msgcount; n++) {
666 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del) {
669 "imap_copy_messages: Message contains attachments to be deleted\n"));
673 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
674 ctx->hdrs[n]->changed)
676 rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
679 dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
685 rc = imap_make_msg_set (idata, &cmd, M_TAG, 0);
687 dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
690 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
693 mutt_message (_("Copying message %d to %s..."), h->index + 1, mbox);
694 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
695 mutt_buffer_addstr (&cmd, uid);
697 if (h->active && h->changed)
699 rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
702 dprint (1, (debugfile, "imap_copy_messages: could not sync\n"));
708 /* let's get it on */
709 mutt_buffer_addstr (&cmd, " ");
710 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
711 mutt_buffer_addstr (&cmd, mmbox);
713 rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
715 /* bail out if command failed for reasons other than nonexistent target */
716 if (ascii_strncasecmp
717 (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11)) {
718 imap_error ("imap_copy_messages", idata->cmd.buf);
722 (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
723 snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
724 if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1) {
728 if (imap_create_mailbox (idata, mbox) < 0)
732 rc = imap_exec (idata, cmd.data, 0);
735 imap_error ("imap_copy_messages", idata->cmd.buf);
742 for (n = 0; n < ctx->msgcount; n++) {
743 if (ctx->hdrs[n]->tagged) {
744 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
745 mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
746 if (option (OPTDELETEUNTAG))
747 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
751 mutt_set_flag (ctx, h, M_DELETE, 1);
752 mutt_set_flag (ctx, h, M_APPENDED, 1);
753 if (option (OPTDELETEUNTAG))
754 mutt_set_flag (ctx, h, M_TAG, 0);
761 FREE (&sync_cmd.data);
769 FREE (&sync_cmd.data);
774 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
775 * appear in the folder flags list. Why wouldn't they? */
776 void imap_add_keywords (char *s, HEADER * h, LIST * mailbox_flags,
781 if (!mailbox_flags || !HEADER_DATA (h) || !HEADER_DATA (h)->keywords)
784 keywords = HEADER_DATA (h)->keywords->next;
787 if (msg_has_flag (mailbox_flags, keywords->data)) {
788 safe_strcat (s, slen, keywords->data);
789 safe_strcat (s, slen, " ");
791 keywords = keywords->next;
795 /* imap_free_header_data: free IMAP_HEADER structure */
796 void imap_free_header_data (void **data)
798 /* this should be safe even if the list wasn't used */
799 mutt_free_list (&(((IMAP_HEADER_DATA *) * data)->keywords));
804 /* imap_set_flags: fill out the message header according to the flags from
805 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
806 char *imap_set_flags (IMAP_DATA * idata, HEADER * h, char *s)
808 CONTEXT *ctx = idata->ctx;
810 unsigned char readonly;
812 memset (&newh, 0, sizeof (newh));
813 newh.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
815 dprint (2, (debugfile, "imap_fetch_message: parsing FLAGS\n"));
816 if ((s = msg_parse_flags (&newh, s)) == NULL) {
821 /* YAUH (yet another ugly hack): temporarily set context to
822 * read-write even if it's read-only, so *server* updates of
823 * flags can be processed by mutt_set_flag. ctx->changed must
824 * be restored afterwards */
825 readonly = ctx->readonly;
828 mutt_set_flag (ctx, h, M_NEW, !(newh.read || newh.old));
829 mutt_set_flag (ctx, h, M_OLD, newh.old);
830 mutt_set_flag (ctx, h, M_READ, newh.read);
831 mutt_set_flag (ctx, h, M_DELETE, newh.deleted);
832 mutt_set_flag (ctx, h, M_FLAG, newh.flagged);
833 mutt_set_flag (ctx, h, M_REPLIED, newh.replied);
835 /* this message is now definitively *not* changed (mutt_set_flag
836 * marks things changed as a side-effect) */
838 ctx->changed &= ~readonly;
839 ctx->readonly = readonly;
841 mutt_free_list (&(HEADER_DATA (h)->keywords));
842 HEADER_DATA (h)->keywords = newh.data->keywords;
849 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
850 * Expects string beginning with * n FETCH.
853 * -1 if the string is not a fetch response
854 * -2 if the string is a corrupt fetch response */
855 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
860 int rc = -1; /* default now is that string isn't FETCH response */
862 idata = (IMAP_DATA *) ctx->data;
867 /* skip to message number */
868 buf = imap_next_word (buf);
872 buf = imap_next_word (buf);
873 if (ascii_strncasecmp ("FETCH", buf, 5))
876 rc = -2; /* we've got a FETCH response, for better or worse */
877 if (!(buf = strchr (buf, '(')))
881 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
882 * read header lines and call it again. Silly. */
883 if (msg_parse_fetch (h, buf) != -2)
886 if (imap_get_literal_count (buf, &bytes) == 0) {
887 imap_read_literal (fp, idata, bytes);
889 /* we may have other fields of the FETCH _after_ the literal
890 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
891 * This all has to go - we should accept literals and nonliterals
892 * interchangeably at any time. */
893 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
896 if (msg_parse_fetch (h, idata->cmd.buf) == -1)
900 rc = 0; /* success */
902 /* subtract headers from message size - unfortunately only the subset of
903 * headers we've requested. */
904 h->content_length -= bytes;
910 static size_t imap_hcache_keylen (const char *fn)
912 return safe_strlen (fn);
915 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
916 * Expects string beginning with * n FETCH.
919 * -1 if the string is not a fetch response
920 * -2 if the string is a corrupt fetch response */
921 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
925 int rc = -1; /* default now is that string isn't FETCH response */
927 idata = (IMAP_DATA *) ctx->data;
932 /* skip to message number */
933 buf = imap_next_word (buf);
937 buf = imap_next_word (buf);
938 if (ascii_strncasecmp ("FETCH", buf, 5))
941 rc = -2; /* we've got a FETCH response, for better or worse */
942 if (!(buf = strchr (buf, '(')))
946 if (msg_parse_fetch (h, buf) < 0) {
950 if (!(buf = strchr (buf, ')')))
956 #endif /* USE_HCACHE */
959 /* msg_has_flag: do a caseless comparison of the flag against a flag list,
960 * return 1 if found or flag list has '\*', 0 otherwise */
961 static int msg_has_flag (LIST * flag_list, const char *flag)
966 flag_list = flag_list->next;
968 if (!ascii_strncasecmp (flag_list->data, flag, safe_strlen (flag_list->data)))
971 flag_list = flag_list->next;
977 /* msg_parse_fetch: handle headers returned from header fetch */
978 static int msg_parse_fetch (IMAP_HEADER * h, char *s)
980 char tmp[SHORT_STRING];
989 if (ascii_strncasecmp ("FLAGS", s, 5) == 0) {
990 if ((s = msg_parse_flags (h, s)) == NULL)
993 else if (ascii_strncasecmp ("UID", s, 3) == 0) {
996 h->data->uid = (unsigned int) atoi (s);
998 s = imap_next_word (s);
1000 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0) {
1006 "msg_parse_fetch(): bogus INTERNALDATE entry: %s\n", s));
1011 while (*s && *s != '\"')
1015 s++; /* skip past the trailing " */
1017 h->received = imap_parse_date (tmp);
1019 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0) {
1023 while (isdigit ((unsigned char) *s))
1026 h->content_length = atoi (tmp);
1028 else if (!ascii_strncasecmp ("BODY", s, 4) ||
1029 !ascii_strncasecmp ("RFC822.HEADER", s, 13)) {
1030 /* handle above, in msg_fetch_header */
1034 s++; /* end of request */
1036 /* got something i don't understand */
1037 imap_error ("msg_parse_fetch", s);
1045 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1046 static char *msg_parse_flags (IMAP_HEADER * h, char *s)
1050 /* sanity-check string */
1051 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
1052 dprint (1, (debugfile, "msg_parse_flags: not a FLAGS response: %s\n", s));
1058 dprint (1, (debugfile, "msg_parse_flags: bogus FLAGS response: %s\n", s));
1064 while (*s && *s != ')') {
1065 if (ascii_strncasecmp ("\\deleted", s, 8) == 0) {
1069 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0) {
1073 else if (ascii_strncasecmp ("\\answered", s, 9) == 0) {
1077 else if (ascii_strncasecmp ("\\seen", s, 5) == 0) {
1081 else if (ascii_strncasecmp ("\\recent", s, 5) == 0) {
1086 /* store custom flags as well */
1088 char *flag_word = s;
1090 if (!h->data->keywords)
1091 h->data->keywords = mutt_new_list ();
1093 while (*s && !ISSPACE (*s) && *s != ')')
1097 mutt_add_list (h->data->keywords, flag_word);
1103 /* wrap up, or note bad flags response */
1105 /* if a message is neither seen nor recent, it is OLD. */
1106 if (option (OPTMARKOLD) && !recent && !(h->read))
1111 dprint (1, (debugfile,
1112 "msg_parse_flags: Unterminated FLAGS response: %s\n", s));
1119 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn)
1122 mutt_socket_write (conn, buf);