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"
33 #include "lib/debug.h"
41 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn);
42 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
44 static int msg_has_flag (LIST * flag_list, const char *flag);
45 static int msg_parse_fetch (IMAP_HEADER * h, char *s);
46 static char *msg_parse_flags (IMAP_HEADER * h, char *s);
49 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
51 static size_t imap_hcache_keylen (const char *fn);
52 #endif /* USE_HCACHE */
55 * Changed to read many headers instead of just one. It will return the
56 * msgno of the last message read. It will return a value other than
57 * msgend if mail comes in while downloading headers (in theory).
59 int imap_read_headers (IMAP_DATA * idata, int msgbegin, int msgend)
62 char buf[LONG_STRING];
65 char tempfile[_POSIX_PATH_MAX];
68 int rc, mfhrc, oldmsgcount;
70 const char *want_headers =
71 "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE CONTENT-DESCRIPTION IN-REPLY-TO REPLY-TO LINES LIST-POST X-LABEL";
75 unsigned long *uid_validity = NULL;
77 #endif /* USE_HCACHE */
82 hc = mutt_hcache_open (HeaderCache, ctx->path);
83 #endif /* USE_HCACHE */
85 if (mutt_bit_isset (idata->capabilities, IMAP4REV1)) {
86 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s%s%s)]",
87 want_headers, ImapHeaders ? " " : "",
88 ImapHeaders ? ImapHeaders : "");
90 else if (mutt_bit_isset (idata->capabilities, IMAP4)) {
91 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s%s%s)",
92 want_headers, ImapHeaders ? " " : "",
93 ImapHeaders ? ImapHeaders : "");
95 else { /* Unable to fetch headers for lower versions */
96 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 */
100 mutt_hcache_close (hc);
101 #endif /* USE_HCACHE */
105 /* instead of downloading all headers and then parsing them, we parse them
106 * as they come in. */
107 mutt_mktemp (tempfile);
108 if (!(fp = safe_fopen (tempfile, "w+"))) {
109 mutt_error (_("Could not create temporary file %s"), tempfile);
112 mutt_hcache_close (hc);
113 #endif /* USE_HCACHE */
118 /* make sure context has room to hold the mailbox */
119 while ((msgend) >= idata->ctx->hdrmax)
120 mx_alloc_memory (idata->ctx);
122 oldmsgcount = ctx->msgcount;
123 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
124 idata->newMailCount = 0;
127 snprintf (buf, sizeof (buf),
128 "FETCH %d:%d (UID FLAGS)", msgbegin + 1, msgend + 1);
129 fetchlast = msgend + 1;
131 imap_cmd_start (idata, buf);
133 for (msgno = msgbegin; msgno <= msgend; msgno++) {
134 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
135 mutt_message (_("Evaluating cache... [%d/%d]"), msgno + 1, msgend + 1);
138 memset (&h, 0, sizeof (h));
139 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
143 rc = imap_cmd_step (idata);
144 if (rc != IMAP_CMD_CONTINUE)
148 msg_fetch_header_fetch (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
153 /* make sure we don't get remnants from older larger message headers */
156 sprintf (uid_buf, "/%u", h.data->uid); /* XXX --tg 21:41 04-07-11 */
158 (unsigned long *) mutt_hcache_fetch (hc, uid_buf, &imap_hcache_keylen);
160 if (uid_validity != NULL && *uid_validity == idata->uid_validity) {
162 mutt_hcache_restore ((unsigned char *) uid_validity, 0);
163 ctx->hdrs[msgno]->index = h.sid - 1;
164 if (h.sid != ctx->msgcount + 1)
165 debug_print (1, ("msgcount and sequence ID are inconsistent!\n"));
166 /* messages which have not been expunged are ACTIVE (borrowed from mh
168 ctx->hdrs[msgno]->active = 1;
169 ctx->hdrs[msgno]->read = h.read;
170 ctx->hdrs[msgno]->old = h.old;
171 ctx->hdrs[msgno]->deleted = h.deleted;
172 ctx->hdrs[msgno]->flagged = h.flagged;
173 ctx->hdrs[msgno]->replied = h.replied;
174 ctx->hdrs[msgno]->changed = h.changed;
175 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
176 ctx->hdrs[msgno]->data = (void *) (h.data);
182 FREE (&uid_validity);
185 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
186 ((msgno + 1) >= fetchlast)));
188 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
189 imap_free_header_data ((void **) &h.data);
191 mutt_hcache_close (hc);
196 fetchlast = msgbegin;
197 #endif /* USE_HCACHE */
199 for (msgno = msgbegin; msgno <= msgend; msgno++) {
200 if (ReadInc && (!msgno || ((msgno + 1) % ReadInc == 0)))
201 mutt_message (_("Fetching message headers... [%d/%d]"), msgno + 1,
204 if (ctx->hdrs[msgno])
207 if (msgno + 1 > fetchlast) {
208 fetchlast = msgno + 1;
209 while ((fetchlast <= msgend) && (!ctx->hdrs[fetchlast]))
213 * Make one request for everything. This makes fetching headers an
214 * order of magnitude faster if you have a large mailbox.
216 * If we get more messages while doing this, we make another
217 * request for all the new messages.
219 snprintf (buf, sizeof (buf),
220 "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)",
221 msgno + 1, fetchlast, hdrreq);
223 imap_cmd_start (idata, buf);
228 memset (&h, 0, sizeof (h));
229 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
231 /* this DO loop does two things:
232 * 1. handles untagged messages, so we can try again on the same msg
233 * 2. fetches the tagged response at the end of the last message.
238 rc = imap_cmd_step (idata);
239 if (rc != IMAP_CMD_CONTINUE)
243 msg_fetch_header (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
248 /* make sure we don't get remnants from older larger message headers */
251 /* update context with message header */
252 ctx->hdrs[msgno] = mutt_new_header ();
254 ctx->hdrs[msgno]->index = h.sid - 1;
255 if (h.sid != ctx->msgcount + 1)
256 debug_print (1, ("msgcount and sequence ID are inconsistent!\n"));
257 /* messages which have not been expunged are ACTIVE (borrowed from mh
259 ctx->hdrs[msgno]->active = 1;
260 ctx->hdrs[msgno]->read = h.read;
261 ctx->hdrs[msgno]->old = h.old;
262 ctx->hdrs[msgno]->deleted = h.deleted;
263 ctx->hdrs[msgno]->flagged = h.flagged;
264 ctx->hdrs[msgno]->replied = h.replied;
265 ctx->hdrs[msgno]->changed = h.changed;
266 ctx->hdrs[msgno]->received = h.received;
267 ctx->hdrs[msgno]->data = (void *) (h.data);
270 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
271 * on h.received being set */
272 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno],
274 /* content built as a side-effect of mutt_read_rfc822_header */
275 ctx->hdrs[msgno]->content->length = h.content_length;
278 sprintf (uid_buf, "/%u", h.data->uid);
279 mutt_hcache_store (hc, uid_buf, ctx->hdrs[msgno], idata->uid_validity,
280 &imap_hcache_keylen);
281 #endif /* USE_HCACHE */
285 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
286 ((msgno + 1) >= fetchlast)));
288 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK))) {
289 imap_free_header_data ((void **) &h.data);
292 mutt_hcache_close (hc);
293 #endif /* USE_HCACHE */
297 /* in case we get new mail while fetching the headers */
298 if (idata->reopen & IMAP_NEWMAIL_PENDING) {
299 msgend = idata->newMailCount - 1;
300 while ((msgend) >= ctx->hdrmax)
301 mx_alloc_memory (ctx);
302 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
303 idata->newMailCount = 0;
308 mutt_hcache_close (hc);
309 #endif /* USE_HCACHE */
313 if (ctx->msgcount > oldmsgcount)
314 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
319 int imap_fetch_message (MESSAGE * msg, CONTEXT * ctx, int msgno)
323 char buf[LONG_STRING];
324 char path[_POSIX_PATH_MAX];
333 /* Sam's weird courier server returns an OK response even when FETCH
334 * fails. Thanks Sam. */
337 idata = (IMAP_DATA *) ctx->data;
338 h = ctx->hdrs[msgno];
340 /* see if we already have the message in our cache */
341 cacheno = HEADER_DATA (h)->uid % IMAP_CACHE_LEN;
342 cache = &idata->cache[cacheno];
345 if (cache->uid == HEADER_DATA (h)->uid) {
346 /* yes, so just return a pointer to the message */
347 if (!(msg->fp = fopen (cache->path, "r"))) {
348 mutt_perror (cache->path);
354 /* clear the previous entry */
355 unlink (cache->path);
361 mutt_message _("Fetching message...");
363 cache->uid = HEADER_DATA (h)->uid;
365 cache->path = safe_strdup (path);
366 if (!(msg->fp = safe_fopen (path, "w+"))) {
371 /* mark this header as currently inactive so the command handler won't
372 * also try to update it. HACK until all this code can be moved into the
376 snprintf (buf, sizeof (buf), "UID FETCH %u %s", HEADER_DATA (h)->uid,
377 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
378 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") : "RFC822"));
380 imap_cmd_start (idata, buf);
382 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
386 pc = imap_next_word (pc);
387 pc = imap_next_word (pc);
389 if (!ascii_strncasecmp ("FETCH", pc, 5)) {
391 pc = imap_next_word (pc);
394 if (ascii_strncasecmp ("UID", pc, 3) == 0) {
395 pc = imap_next_word (pc);
397 if (uid != HEADER_DATA (h)->uid)
399 ("The message index is incorrect. Try reopening the mailbox."));
401 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
402 (ascii_strncasecmp ("BODY[]", pc, 6) == 0)) {
403 pc = imap_next_word (pc);
404 if (imap_get_literal_count (pc, &bytes) < 0) {
405 imap_error ("imap_fetch_message()", buf);
408 if (imap_read_literal (msg->fp, idata, bytes) < 0)
410 /* pick up trailing line */
411 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
417 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
418 * change (eg from \Unseen to \Seen).
419 * Uncommitted changes in mutt take precedence. If we decide to
420 * incrementally update flags later, this won't stop us syncing */
421 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed) {
422 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
428 while (rc == IMAP_CMD_CONTINUE);
430 /* see comment before command start. */
433 if (rc != IMAP_CMD_OK)
436 if (!fetched || !imap_code (idata->cmd.buf))
439 /* Update the header information. Previously, we only downloaded a
440 * portion of the headers, those required for the main display.
443 /* It may be that the Status header indicates a message is read, but the
444 * IMAP server doesn't know the message has been \Seen. So we capture
445 * the server's notion of 'read' and if it differs from the message info
446 * picked up in mutt_read_rfc822_header, we mark the message (and context
447 * changed). Another possiblity: ignore Status on IMAP?*/
449 /* I hate do this here, since it's so low-level, but I'm not sure where
450 * I can abstract it. Problem: the id and subj hashes lose their keys when
451 * mutt_free_envelope gets called, but keep their spots in the hash. This
452 * confuses threading. Alternatively we could try to merge the new
453 * envelope into the old one. Also messy and lowlevel. */
454 if (ctx->id_hash && h->env->message_id)
455 hash_delete (ctx->id_hash, h->env->message_id, h, NULL);
456 if (ctx->subj_hash && h->env->real_subj)
457 hash_delete (ctx->subj_hash, h->env->real_subj, h, NULL);
458 mutt_free_envelope (&h->env);
459 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
460 if (ctx->id_hash && h->env->message_id)
461 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
462 if (ctx->subj_hash && h->env->real_subj)
463 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
465 /* see above. We want the new status in h->read, so we unset it manually
466 * and let mutt_set_flag set it correctly, updating context. */
467 if (read != h->read) {
469 mutt_set_flag (ctx, h, M_NEW, read);
473 fgets (buf, sizeof (buf), msg->fp);
474 while (!feof (msg->fp)) {
476 fgets (buf, sizeof (buf), msg->fp);
479 h->content->length = ftell (msg->fp) - h->content->offset;
481 /* This needs to be done in case this is a multipart message */
482 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
483 h->security = crypt_query (h->content);
492 safe_fclose (&msg->fp);
494 unlink (cache->path);
501 int imap_append_message (CONTEXT * ctx, MESSAGE * msg)
505 char buf[LONG_STRING];
506 char mbox[LONG_STRING];
507 char mailbox[LONG_STRING];
513 idata = (IMAP_DATA *) ctx->data;
515 if (imap_parse_path (ctx->path, &mx))
518 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
520 if ((fp = fopen (msg->path, "r")) == NULL) {
521 mutt_perror (msg->path);
525 /* currently we set the \Seen flag on all messages, but probably we
526 * should scan the message Status header for flag info. Since we're
527 * already rereading the whole file for length it isn't any more
528 * expensive (it'd be nice if we had the file size passed in already
529 * by the code that writes the file, but that's a lot of changes.
530 * Ideally we'd have a HEADER structure with flag info here... */
531 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
532 if (c == '\n' && last != '\r')
539 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
540 snprintf (buf, sizeof (buf), "APPEND %s (%s%s%s%s%s) {%lu}", mbox,
541 msg->flags.read ? "\\Seen" : "",
542 msg->flags.read && (msg->flags.replied
543 || msg->flags.flagged) ? " " : "",
544 msg->flags.replied ? "\\Answered" : "", msg->flags.replied
545 && msg->flags.flagged ? " " : "",
546 msg->flags.flagged ? "\\Flagged" : "", (unsigned long) len);
548 imap_cmd_start (idata, buf);
551 rc = imap_cmd_step (idata);
552 while (rc == IMAP_CMD_CONTINUE);
554 if (rc != IMAP_CMD_RESPOND) {
557 debug_print (1, ("command failed: %s\n", idata->cmd.buf));
559 pc = idata->cmd.buf + SEQLEN;
561 pc = imap_next_word (pc);
562 mutt_error ("%s", pc);
568 mutt_message _("Uploading message ...");
570 for (last = EOF, len = 0; (c = fgetc (fp)) != EOF; last = c) {
571 if (c == '\n' && last != '\r')
576 if (len > sizeof (buf) - 3)
577 flush_buffer (buf, &len, idata->conn);
581 flush_buffer (buf, &len, idata->conn);
583 mutt_socket_write (idata->conn, "\r\n");
587 rc = imap_cmd_step (idata);
588 while (rc == IMAP_CMD_CONTINUE);
590 if (!imap_code (idata->cmd.buf)) {
593 debug_print (1, ("command failed: %s\n", idata->cmd.buf));
594 pc = idata->cmd.buf + SEQLEN;
596 pc = imap_next_word (pc);
597 mutt_error ("%s", pc);
610 /* imap_copy_messages: use server COPY command to copy messages to another
615 * 1: non-fatal error - try fetch/append */
616 int imap_copy_messages (CONTEXT * ctx, HEADER * h, char *dest, int delete)
619 BUFFER cmd, sync_cmd;
621 char mbox[LONG_STRING];
622 char mmbox[LONG_STRING];
626 int err_continue = M_NO;
628 idata = (IMAP_DATA *) ctx->data;
630 if (imap_parse_path (dest, &mx)) {
631 debug_print (1, ("bad destination %s\n", dest));
635 /* check that the save-to folder is in the same account */
636 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account))) {
637 debug_print (3, ("%s not same server as %s\n", dest, ctx->path));
641 if (h && h->attach_del) {
642 debug_print (3, ("Message contains attachments to be deleted\n"));
646 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
648 memset (&sync_cmd, 0, sizeof (sync_cmd));
649 memset (&cmd, 0, sizeof (cmd));
650 mutt_buffer_addstr (&cmd, "UID COPY ");
652 /* Null HEADER* means copy tagged messages */
654 /* if any messages have attachments to delete, fall through to FETCH
655 * and APPEND. TODO: Copy what we can with COPY, fall through for the
657 for (n = 0; n < ctx->msgcount; n++) {
658 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del) {
659 debug_print (3, ("Message contains attachments to be deleted\n"));
663 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->active &&
664 ctx->hdrs[n]->changed)
666 rc = imap_sync_message (idata, ctx->hdrs[n], &sync_cmd, &err_continue);
669 debug_print (1, ("could not sync\n"));
675 rc = imap_make_msg_set (idata, &cmd, M_TAG, 0);
677 debug_print (1, ("No messages tagged\n"));
680 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
683 mutt_message (_("Copying message %d to %s..."), h->index + 1, mbox);
684 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
685 mutt_buffer_addstr (&cmd, uid);
687 if (h->active && h->changed)
689 rc = imap_sync_message (idata, h, &sync_cmd, &err_continue);
692 debug_print (1, ("could not sync\n"));
698 /* let's get it on */
699 mutt_buffer_addstr (&cmd, " ");
700 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
701 mutt_buffer_addstr (&cmd, mmbox);
703 rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
705 /* bail out if command failed for reasons other than nonexistent target */
706 if (ascii_strncasecmp
707 (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11)) {
708 imap_error ("imap_copy_messages", idata->cmd.buf);
711 debug_print (2, ("server suggests TRYCREATE\n"));
712 snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
713 if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1) {
717 if (imap_create_mailbox (idata, mbox) < 0)
721 rc = imap_exec (idata, cmd.data, 0);
724 imap_error ("imap_copy_messages", idata->cmd.buf);
731 for (n = 0; n < ctx->msgcount; n++) {
732 if (ctx->hdrs[n]->tagged) {
733 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
734 mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
735 if (option (OPTDELETEUNTAG))
736 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
740 mutt_set_flag (ctx, h, M_DELETE, 1);
741 mutt_set_flag (ctx, h, M_APPENDED, 1);
742 if (option (OPTDELETEUNTAG))
743 mutt_set_flag (ctx, h, M_TAG, 0);
750 FREE (&sync_cmd.data);
758 FREE (&sync_cmd.data);
763 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
764 * appear in the folder flags list. Why wouldn't they? */
765 void imap_add_keywords (char *s, HEADER * h, LIST * mailbox_flags,
770 if (!mailbox_flags || !HEADER_DATA (h) || !HEADER_DATA (h)->keywords)
773 keywords = HEADER_DATA (h)->keywords->next;
776 if (msg_has_flag (mailbox_flags, keywords->data)) {
777 safe_strcat (s, slen, keywords->data);
778 safe_strcat (s, slen, " ");
780 keywords = keywords->next;
784 /* imap_free_header_data: free IMAP_HEADER structure */
785 void imap_free_header_data (void **data)
787 /* this should be safe even if the list wasn't used */
788 mutt_free_list (&(((IMAP_HEADER_DATA *) * data)->keywords));
793 /* imap_set_flags: fill out the message header according to the flags from
794 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
795 char *imap_set_flags (IMAP_DATA * idata, HEADER * h, char *s)
797 CONTEXT *ctx = idata->ctx;
799 unsigned char readonly;
801 memset (&newh, 0, sizeof (newh));
802 newh.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
804 debug_print (2, ("parsing FLAGS\n"));
805 if ((s = msg_parse_flags (&newh, s)) == NULL) {
810 /* YAUH (yet another ugly hack): temporarily set context to
811 * read-write even if it's read-only, so *server* updates of
812 * flags can be processed by mutt_set_flag. ctx->changed must
813 * be restored afterwards */
814 readonly = ctx->readonly;
817 mutt_set_flag (ctx, h, M_NEW, !(newh.read || newh.old));
818 mutt_set_flag (ctx, h, M_OLD, newh.old);
819 mutt_set_flag (ctx, h, M_READ, newh.read);
820 mutt_set_flag (ctx, h, M_DELETE, newh.deleted);
821 mutt_set_flag (ctx, h, M_FLAG, newh.flagged);
822 mutt_set_flag (ctx, h, M_REPLIED, newh.replied);
824 /* this message is now definitively *not* changed (mutt_set_flag
825 * marks things changed as a side-effect) */
827 ctx->changed &= ~readonly;
828 ctx->readonly = readonly;
830 mutt_free_list (&(HEADER_DATA (h)->keywords));
831 HEADER_DATA (h)->keywords = newh.data->keywords;
838 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
839 * Expects string beginning with * n FETCH.
842 * -1 if the string is not a fetch response
843 * -2 if the string is a corrupt fetch response */
844 static int msg_fetch_header (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
849 int rc = -1; /* default now is that string isn't FETCH response */
851 idata = (IMAP_DATA *) ctx->data;
856 /* skip to message number */
857 buf = imap_next_word (buf);
861 buf = imap_next_word (buf);
862 if (ascii_strncasecmp ("FETCH", buf, 5))
865 rc = -2; /* we've got a FETCH response, for better or worse */
866 if (!(buf = strchr (buf, '(')))
870 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
871 * read header lines and call it again. Silly. */
872 if (msg_parse_fetch (h, buf) != -2)
875 if (imap_get_literal_count (buf, &bytes) == 0) {
876 imap_read_literal (fp, idata, bytes);
878 /* we may have other fields of the FETCH _after_ the literal
879 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
880 * This all has to go - we should accept literals and nonliterals
881 * interchangeably at any time. */
882 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
885 if (msg_parse_fetch (h, idata->cmd.buf) == -1)
889 rc = 0; /* success */
891 /* subtract headers from message size - unfortunately only the subset of
892 * headers we've requested. */
893 h->content_length -= bytes;
899 static size_t imap_hcache_keylen (const char *fn)
901 return safe_strlen (fn);
904 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
905 * Expects string beginning with * n FETCH.
908 * -1 if the string is not a fetch response
909 * -2 if the string is a corrupt fetch response */
910 static int msg_fetch_header_fetch (CONTEXT * ctx, IMAP_HEADER * h, char *buf,
914 int rc = -1; /* default now is that string isn't FETCH response */
916 idata = (IMAP_DATA *) ctx->data;
921 /* skip to message number */
922 buf = imap_next_word (buf);
926 buf = imap_next_word (buf);
927 if (ascii_strncasecmp ("FETCH", buf, 5))
930 rc = -2; /* we've got a FETCH response, for better or worse */
931 if (!(buf = strchr (buf, '(')))
935 if (msg_parse_fetch (h, buf) < 0) {
939 if (!(buf = strchr (buf, ')')))
945 #endif /* USE_HCACHE */
948 /* msg_has_flag: do a caseless comparison of the flag against a flag list,
949 * return 1 if found or flag list has '\*', 0 otherwise */
950 static int msg_has_flag (LIST * flag_list, const char *flag)
955 flag_list = flag_list->next;
957 if (!ascii_strncasecmp (flag_list->data, flag, safe_strlen (flag_list->data)))
960 flag_list = flag_list->next;
966 /* msg_parse_fetch: handle headers returned from header fetch */
967 static int msg_parse_fetch (IMAP_HEADER * h, char *s)
969 char tmp[SHORT_STRING];
978 if (ascii_strncasecmp ("FLAGS", s, 5) == 0) {
979 if ((s = msg_parse_flags (h, s)) == NULL)
982 else if (ascii_strncasecmp ("UID", s, 3) == 0) {
985 h->data->uid = (unsigned int) atoi (s);
987 s = imap_next_word (s);
989 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0) {
993 debug_print (1, ("bogus INTERNALDATE entry: %s\n", s));
998 while (*s && *s != '\"')
1002 s++; /* skip past the trailing " */
1004 h->received = imap_parse_date (tmp);
1006 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0) {
1010 while (isdigit ((unsigned char) *s))
1013 h->content_length = atoi (tmp);
1015 else if (!ascii_strncasecmp ("BODY", s, 4) ||
1016 !ascii_strncasecmp ("RFC822.HEADER", s, 13)) {
1017 /* handle above, in msg_fetch_header */
1021 s++; /* end of request */
1023 /* got something i don't understand */
1024 imap_error ("msg_parse_fetch", s);
1032 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
1033 static char *msg_parse_flags (IMAP_HEADER * h, char *s)
1037 /* sanity-check string */
1038 if (ascii_strncasecmp ("FLAGS", s, 5) != 0) {
1039 debug_print (1, ("not a FLAGS response: %s\n", s));
1045 debug_print (1, ("bogus FLAGS response: %s\n", s));
1051 while (*s && *s != ')') {
1052 if (ascii_strncasecmp ("\\deleted", s, 8) == 0) {
1056 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0) {
1060 else if (ascii_strncasecmp ("\\answered", s, 9) == 0) {
1064 else if (ascii_strncasecmp ("\\seen", s, 5) == 0) {
1068 else if (ascii_strncasecmp ("\\recent", s, 5) == 0) {
1073 /* store custom flags as well */
1075 char *flag_word = s;
1077 if (!h->data->keywords)
1078 h->data->keywords = mutt_new_list ();
1080 while (*s && !ISSPACE (*s) && *s != ')')
1084 mutt_add_list (h->data->keywords, flag_word);
1090 /* wrap up, or note bad flags response */
1092 /* if a message is neither seen nor recent, it is OLD. */
1093 if (option (OPTMARKOLD) && !recent && !(h->read))
1098 debug_print (1, ("Unterminated FLAGS response: %s\n", s));
1105 static void flush_buffer (char *buf, size_t * len, CONNECTION * conn)
1108 mutt_socket_write (conn, buf);