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 */
26 #include "mutt_curses.h"
27 #include "imap_private.h"
35 static void flush_buffer(char* buf, size_t* len, CONNECTION* conn);
36 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf,
38 static int msg_has_flag (LIST* flag_list, const char* flag);
39 static int msg_parse_fetch (IMAP_HEADER* h, char* s);
40 static char* msg_parse_flags (IMAP_HEADER* h, char* s);
43 static int msg_fetch_header_fetch (CONTEXT* ctx, IMAP_HEADER* h, char* buf,
45 static size_t imap_hcache_keylen (const char *fn);
46 #endif /* USE_HCACHE */
49 * Changed to read many headers instead of just one. It will return the
50 * msgno of the last message read. It will return a value other than
51 * msgend if mail comes in while downloading headers (in theory).
53 int imap_read_headers (IMAP_DATA* idata, int msgbegin, int msgend)
56 char buf[LONG_STRING];
59 char tempfile[_POSIX_PATH_MAX];
62 int rc, mfhrc, oldmsgcount;
64 const char *want_headers = "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE IN-REPLY-TO REPLY-TO LINES X-LABEL";
68 unsigned long long *uid_validity = NULL;
70 #endif /* USE_HCACHE */
75 hc = mutt_hcache_open (HeaderCache, ctx->path);
76 #endif /* USE_HCACHE */
78 if (mutt_bit_isset (idata->capabilities,IMAP4REV1))
80 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s)]",
83 else if (mutt_bit_isset (idata->capabilities,IMAP4))
85 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s)",
89 { /* Unable to fetch headers for lower versions */
90 mutt_error _("Unable to fetch headers from this IMAP server version.");
91 mutt_sleep (2); /* pause a moment to let the user see the error */
93 mutt_hcache_close (hc);
94 #endif /* USE_HCACHE */
98 /* instead of downloading all headers and then parsing them, we parse them
100 mutt_mktemp (tempfile);
101 if (!(fp = safe_fopen (tempfile, "w+")))
103 mutt_error (_("Could not create temporary file %s"), tempfile);
106 mutt_hcache_close (hc);
107 #endif /* USE_HCACHE */
112 /* make sure context has room to hold the mailbox */
113 while ((msgend) >= idata->ctx->hdrmax)
114 mx_alloc_memory (idata->ctx);
116 oldmsgcount = ctx->msgcount;
117 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
118 idata->newMailCount = 0;
121 snprintf (buf, sizeof (buf),
122 "FETCH %d:%d (UID FLAGS)", msgbegin + 1, msgend + 1);
123 fetchlast = msgend + 1;
125 imap_cmd_start (idata, buf);
127 for (msgno = msgbegin; msgno <= msgend ; msgno++)
129 if (ReadInc && (!msgno || ((msgno+1) % ReadInc == 0)))
130 mutt_message (_("Evaluating cache... [%d/%d]"), msgno + 1,
134 memset (&h, 0, sizeof (h));
135 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
140 rc = imap_cmd_step (idata);
141 if (rc != IMAP_CMD_CONTINUE)
144 if ((mfhrc = msg_fetch_header_fetch (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
149 /* make sure we don't get remnants from older larger message headers */
152 sprintf(uid_buf, "/%u", h.data->uid); /* XXX --tg 21:41 04-07-11 */
153 uid_validity = (unsigned long long *) mutt_hcache_fetch (hc, uid_buf, &imap_hcache_keylen);
155 if (uid_validity != NULL
156 && *uid_validity == idata->uid_validity) {
157 ctx->hdrs[msgno] = mutt_hcache_restore((unsigned char *) uid_validity, 0);
158 ctx->hdrs[msgno]->index = h.sid - 1;
159 if (h.sid != ctx->msgcount + 1)
160 dprint (1, (debugfile, "imap_read_headers: msgcount and sequence ID are inconsistent!"));
161 /* messages which have not been expunged are ACTIVE (borrowed from mh
163 ctx->hdrs[msgno]->active = 1;
164 ctx->hdrs[msgno]->read = h.read;
165 ctx->hdrs[msgno]->old = h.old;
166 ctx->hdrs[msgno]->deleted = h.deleted;
167 ctx->hdrs[msgno]->flagged = h.flagged;
168 ctx->hdrs[msgno]->replied = h.replied;
169 ctx->hdrs[msgno]->changed = h.changed;
170 /* ctx->hdrs[msgno]->received is restored from mutt_hcache_restore */
171 ctx->hdrs[msgno]->data = (void *) (h.data);
180 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
181 ((msgno + 1) >= fetchlast)));
183 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
185 imap_free_header_data ((void**) &h.data);
187 mutt_hcache_close (hc);
192 fetchlast = msgbegin;
193 #endif /* USE_HCACHE */
195 for (msgno = msgbegin; msgno <= msgend ; msgno++)
197 if (ReadInc && (!msgno || ((msgno+1) % ReadInc == 0)))
198 mutt_message (_("Fetching message headers... [%d/%d]"), msgno + 1,
201 if (ctx->hdrs[msgno])
204 if (msgno + 1 > fetchlast)
206 fetchlast = msgno + 1;
207 while((fetchlast <= msgend) && (! ctx->hdrs[fetchlast]))
211 * Make one request for everything. This makes fetching headers an
212 * order of magnitude faster if you have a large mailbox.
214 * If we get more messages while doing this, we make another
215 * request for all the new messages.
217 snprintf (buf, sizeof (buf),
218 "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)", msgno + 1,
221 imap_cmd_start (idata, buf);
226 memset (&h, 0, sizeof (h));
227 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
229 /* this DO loop does two things:
230 * 1. handles untagged messages, so we can try again on the same msg
231 * 2. fetches the tagged response at the end of the last message.
237 rc = imap_cmd_step (idata);
238 if (rc != IMAP_CMD_CONTINUE)
241 if ((mfhrc = msg_fetch_header (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
246 /* make sure we don't get remnants from older larger message headers */
249 /* update context with message header */
250 ctx->hdrs[msgno] = mutt_new_header ();
252 ctx->hdrs[msgno]->index = h.sid - 1;
253 if (h.sid != ctx->msgcount + 1)
254 dprint (1, (debugfile, "imap_read_headers: msgcount and sequence ID are inconsistent!"));
255 /* messages which have not been expunged are ACTIVE (borrowed from mh
257 ctx->hdrs[msgno]->active = 1;
258 ctx->hdrs[msgno]->read = h.read;
259 ctx->hdrs[msgno]->old = h.old;
260 ctx->hdrs[msgno]->deleted = h.deleted;
261 ctx->hdrs[msgno]->flagged = h.flagged;
262 ctx->hdrs[msgno]->replied = h.replied;
263 ctx->hdrs[msgno]->changed = h.changed;
264 ctx->hdrs[msgno]->received = h.received;
265 ctx->hdrs[msgno]->data = (void *) (h.data);
268 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
269 * on h.received being set */
270 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno],
272 /* content built as a side-effect of mutt_read_rfc822_header */
273 ctx->hdrs[msgno]->content->length = h.content_length;
276 sprintf(uid_buf, "/%u", h.data->uid);
277 mutt_hcache_store(hc, uid_buf, ctx->hdrs[msgno], idata->uid_validity, &imap_hcache_keylen);
278 #endif /* USE_HCACHE */
282 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
283 ((msgno + 1) >= fetchlast)));
285 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
287 imap_free_header_data ((void**) &h.data);
290 mutt_hcache_close (hc);
291 #endif /* USE_HCACHE */
295 /* in case we get new mail while fetching the headers */
296 if (idata->reopen & IMAP_NEWMAIL_PENDING)
298 msgend = idata->newMailCount - 1;
299 while ((msgend) >= ctx->hdrmax)
300 mx_alloc_memory (ctx);
301 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
302 idata->newMailCount = 0;
307 mutt_hcache_close (hc);
308 #endif /* USE_HCACHE */
312 if (ctx->msgcount > oldmsgcount)
313 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
318 int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
322 char buf[LONG_STRING];
323 char path[_POSIX_PATH_MAX];
331 /* Sam's weird courier server returns an OK response even when FETCH
332 * fails. Thanks Sam. */
335 idata = (IMAP_DATA*) ctx->data;
336 h = ctx->hdrs[msgno];
338 /* see if we already have the message in our cache */
339 cacheno = HEADER_DATA(h)->uid % IMAP_CACHE_LEN;
340 cache = &idata->cache[cacheno];
344 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")))
349 mutt_perror (cache->path);
356 /* clear the previous entry */
357 unlink (cache->path);
363 mutt_message _("Fetching message...");
365 cache->uid = HEADER_DATA(h)->uid;
367 cache->path = safe_strdup (path);
368 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 %d %s", HEADER_DATA(h)->uid,
380 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
381 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") :
384 imap_cmd_start (idata, buf);
387 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
391 pc = imap_next_word (pc);
392 pc = imap_next_word (pc);
394 if (!ascii_strncasecmp ("FETCH", pc, 5))
398 pc = imap_next_word (pc);
401 if (ascii_strncasecmp ("UID", pc, 3) == 0)
403 pc = imap_next_word (pc);
405 if (uid != HEADER_DATA(h)->uid)
406 mutt_error (_("The message index is incorrect. Try reopening the mailbox."));
408 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
409 (ascii_strncasecmp ("BODY[]", pc, 6) == 0))
411 pc = imap_next_word (pc);
412 if (imap_get_literal_count(pc, &bytes) < 0)
414 imap_error ("imap_fetch_message()", buf);
417 if (imap_read_literal (msg->fp, idata, bytes) < 0)
419 /* pick up trailing line */
420 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
426 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
427 * change (eg from \Unseen to \Seen).
428 * Uncommitted changes in mutt take precedence. If we decide to
429 * incrementally update flags later, this won't stop us syncing */
430 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed)
432 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
438 while (rc == IMAP_CMD_CONTINUE);
440 /* see comment before command start. */
443 if (rc != IMAP_CMD_OK)
446 if (!fetched || !imap_code (idata->cmd.buf))
449 /* Update the header information. Previously, we only downloaded a
450 * portion of the headers, those required for the main display.
453 /* It may be that the Status header indicates a message is read, but the
454 * IMAP server doesn't know the message has been \Seen. So we capture
455 * the server's notion of 'read' and if it differs from the message info
456 * picked up in mutt_read_rfc822_header, we mark the message (and context
457 * changed). Another possiblity: ignore Status on IMAP?*/
459 /* I hate do this here, since it's so low-level, but I'm not sure where
460 * I can abstract it. Problem: the id and subj hashes lose their keys when
461 * mutt_free_envelope gets called, but keep their spots in the hash. This
462 * confuses threading. Alternatively we could try to merge the new
463 * envelope into the old one. Also messy and lowlevel. */
464 if (ctx->id_hash && h->env->message_id)
465 hash_delete (ctx->id_hash, h->env->message_id, h, NULL);
466 if (ctx->subj_hash && h->env->real_subj)
467 hash_delete (ctx->subj_hash, h->env->real_subj, h, NULL);
468 mutt_free_envelope (&h->env);
469 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
470 if (ctx->id_hash && h->env->message_id)
471 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
472 if (ctx->subj_hash && h->env->real_subj)
473 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
475 /* see above. We want the new status in h->read, so we unset it manually
476 * and let mutt_set_flag set it correctly, updating context. */
480 mutt_set_flag (ctx, h, M_NEW, read);
484 fgets (buf, sizeof (buf), msg->fp);
485 while (!feof (msg->fp))
488 fgets (buf, sizeof (buf), msg->fp);
491 h->content->length = ftell (msg->fp) - h->content->offset;
493 /* This needs to be done in case this is a multipart message */
494 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
495 h->security = crypt_query (h->content);
504 safe_fclose (&msg->fp);
507 unlink (cache->path);
514 int imap_append_message (CONTEXT *ctx, MESSAGE *msg)
518 char buf[LONG_STRING];
519 char mbox[LONG_STRING];
520 char mailbox[LONG_STRING];
526 idata = (IMAP_DATA*) ctx->data;
528 if (imap_parse_path (ctx->path, &mx))
531 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
533 if ((fp = fopen (msg->path, "r")) == NULL)
535 mutt_perror (msg->path);
539 /* currently we set the \Seen flag on all messages, but probably we
540 * should scan the message Status header for flag info. Since we're
541 * already rereading the whole file for length it isn't any more
542 * expensive (it'd be nice if we had the file size passed in already
543 * by the code that writes the file, but that's a lot of changes.
544 * Ideally we'd have a HEADER structure with flag info here... */
545 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
547 if(c == '\n' && last != '\r')
554 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
555 snprintf (buf, sizeof (buf), "APPEND %s (\\Seen) {%d}", mbox, len);
557 imap_cmd_start (idata, buf);
560 rc = imap_cmd_step (idata);
561 while (rc == IMAP_CMD_CONTINUE);
563 if (rc != IMAP_CMD_RESPOND)
567 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
570 pc = idata->cmd.buf + SEQLEN;
572 pc = imap_next_word (pc);
573 mutt_error ("%s", pc);
579 mutt_message _("Uploading message ...");
581 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
583 if (c == '\n' && last != '\r')
588 if (len > sizeof(buf) - 3)
589 flush_buffer(buf, &len, idata->conn);
593 flush_buffer(buf, &len, idata->conn);
595 mutt_socket_write (idata->conn, "\r\n");
599 rc = imap_cmd_step (idata);
600 while (rc == IMAP_CMD_CONTINUE);
602 if (!imap_code (idata->cmd.buf))
606 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
608 pc = idata->cmd.buf + SEQLEN;
610 pc = imap_next_word (pc);
611 mutt_error ("%s", pc);
624 /* imap_copy_messages: use server COPY command to copy messages to another
629 * 1: non-fatal error - try fetch/append */
630 int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
635 char mbox[LONG_STRING];
636 char mmbox[LONG_STRING];
641 idata = (IMAP_DATA*) ctx->data;
643 if (imap_parse_path (dest, &mx))
645 dprint (1, (debugfile, "imap_copy_messages: bad destination %s\n", dest));
649 /* check that the save-to folder is in the same account */
650 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account)))
652 dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n",
657 if (h && h->attach_del)
659 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
663 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
665 memset (&cmd, 0, sizeof (cmd));
666 mutt_buffer_addstr (&cmd, "UID COPY ");
668 /* Null HEADER* means copy tagged messages */
671 /* if any messages have attachments to delete, fall through to FETCH
672 * and APPEND. TODO: Copy what we can with COPY, fall through for the
674 for (n = 0; n < ctx->msgcount; n++)
676 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
678 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
683 rc = imap_make_msg_set (idata, &cmd, M_TAG, 0);
686 dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
689 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);
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);
706 /* bail out if command failed for reasons other than nonexistent target */
707 if (ascii_strncasecmp (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11))
709 imap_error ("imap_copy_messages", idata->cmd.buf);
712 dprint (2, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
713 snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
714 if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1)
719 if (imap_create_mailbox (idata, mbox) < 0)
723 rc = imap_exec (idata, cmd.data, 0);
727 imap_error ("imap_copy_messages", idata->cmd.buf);
735 for (n = 0; n < ctx->msgcount; n++)
737 if (ctx->hdrs[n]->tagged)
739 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
740 mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
741 if (option (OPTDELETEUNTAG))
742 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
747 mutt_set_flag (ctx, h, M_DELETE, 1);
748 mutt_set_flag (ctx, h, M_APPENDED, 1);
749 if (option (OPTDELETEUNTAG))
750 mutt_set_flag (ctx, h, M_TAG, 0);
766 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
767 * appear in the folder flags list. Why wouldn't they? */
768 void imap_add_keywords (char* s, HEADER* h, LIST* mailbox_flags, size_t slen)
772 if (!mailbox_flags || !HEADER_DATA(h) || !HEADER_DATA(h)->keywords)
775 keywords = HEADER_DATA(h)->keywords->next;
779 if (msg_has_flag (mailbox_flags, keywords->data))
781 strncat (s, keywords->data, slen);
782 strncat (s, " ", slen);
784 keywords = keywords->next;
788 /* imap_free_header_data: free IMAP_HEADER structure */
789 void imap_free_header_data (void** data)
791 /* this should be safe even if the list wasn't used */
792 mutt_free_list (&(((IMAP_HEADER_DATA*) *data)->keywords));
797 /* imap_set_flags: fill out the message header according to the flags from
798 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
799 char* imap_set_flags (IMAP_DATA* idata, HEADER* h, char* s)
801 CONTEXT* ctx = idata->ctx;
803 unsigned char readonly;
805 memset (&newh, 0, sizeof (newh));
806 newh.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
808 dprint (2, (debugfile, "imap_fetch_message: parsing FLAGS\n"));
809 if ((s = msg_parse_flags (&newh, s)) == NULL)
815 /* YAUH (yet another ugly hack): temporarily set context to
816 * read-write even if it's read-only, so *server* updates of
817 * flags can be processed by mutt_set_flag. ctx->changed must
818 * be restored afterwards */
819 readonly = ctx->readonly;
822 mutt_set_flag (ctx, h, M_NEW, !(newh.read || newh.old));
823 mutt_set_flag (ctx, h, M_OLD, newh.old);
824 mutt_set_flag (ctx, h, M_READ, newh.read);
825 mutt_set_flag (ctx, h, M_DELETE, newh.deleted);
826 mutt_set_flag (ctx, h, M_FLAG, newh.flagged);
827 mutt_set_flag (ctx, h, M_REPLIED, newh.replied);
829 /* this message is now definitively *not* changed (mutt_set_flag
830 * marks things changed as a side-effect) */
832 ctx->changed &= ~readonly;
833 ctx->readonly = readonly;
835 mutt_free_list (&(HEADER_DATA(h)->keywords));
836 HEADER_DATA(h)->keywords = newh.data->keywords;
843 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
844 * Expects string beginning with * n FETCH.
847 * -1 if the string is not a fetch response
848 * -2 if the string is a corrupt fetch response */
849 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf, FILE* fp)
853 int rc = -1; /* default now is that string isn't FETCH response*/
855 idata = (IMAP_DATA*) ctx->data;
860 /* skip to message number */
861 buf = imap_next_word (buf);
865 buf = imap_next_word (buf);
866 if (ascii_strncasecmp ("FETCH", buf, 5))
869 rc = -2; /* we've got a FETCH response, for better or worse */
870 if (!(buf = strchr (buf, '(')))
874 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
875 * read header lines and call it again. Silly. */
876 if (msg_parse_fetch (h, buf) != -2)
879 if (imap_get_literal_count (buf, &bytes) < 0)
881 imap_read_literal (fp, idata, bytes);
883 /* we may have other fields of the FETCH _after_ the literal
884 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
885 * This all has to go - we should accept literals and nonliterals
886 * interchangeably at any time. */
887 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
890 if (msg_parse_fetch (h, idata->cmd.buf) == -1)
893 rc = 0; /* success */
895 /* subtract headers from message size - unfortunately only the subset of
896 * headers we've requested. */
897 h->content_length -= bytes;
903 static size_t imap_hcache_keylen (const char *fn)
905 return mutt_strlen(fn);
908 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
909 * Expects string beginning with * n FETCH.
912 * -1 if the string is not a fetch response
913 * -2 if the string is a corrupt fetch response */
914 static int msg_fetch_header_fetch (CONTEXT* ctx, IMAP_HEADER* h, char* buf, FILE* fp)
918 int rc = -1; /* default now is that string isn't FETCH response*/
920 idata = (IMAP_DATA*) ctx->data;
925 /* skip to message number */
926 buf = imap_next_word (buf);
930 buf = imap_next_word (buf);
931 if (ascii_strncasecmp ("FETCH", buf, 5))
934 rc = -2; /* we've got a FETCH response, for better or worse */
935 if (!(buf = strchr (buf, '(')))
939 if (msg_parse_fetch (h, buf) < 0) {
943 if (!(buf = strchr (buf, ')')))
949 #endif /* USE_HCACHE */
952 /* msg_has_flag: do a caseless comparison of the flag against a flag list,
953 * return 1 if found or flag list has '\*', 0 otherwise */
954 static int msg_has_flag (LIST* flag_list, const char* flag)
959 flag_list = flag_list->next;
962 if (!ascii_strncasecmp (flag_list->data, flag, strlen (flag_list->data)))
965 flag_list = flag_list->next;
971 /* msg_parse_fetch: handle headers returned from header fetch */
972 static int msg_parse_fetch (IMAP_HEADER *h, char *s)
974 char tmp[SHORT_STRING];
984 if (ascii_strncasecmp ("FLAGS", s, 5) == 0)
986 if ((s = msg_parse_flags (h, s)) == NULL)
989 else if (ascii_strncasecmp ("UID", s, 3) == 0)
993 h->data->uid = (unsigned int) atoi (s);
995 s = imap_next_word (s);
997 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0)
1003 dprint (1, (debugfile, "msg_parse_fetch(): bogus INTERNALDATE entry: %s\n", s));
1008 while (*s && *s != '\"')
1012 s++; /* skip past the trailing " */
1014 h->received = imap_parse_date (tmp);
1016 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0)
1021 while (isdigit ((unsigned char) *s))
1024 h->content_length = atoi (tmp);
1026 else if (!ascii_strncasecmp ("BODY", s, 4) ||
1027 !ascii_strncasecmp ("RFC822.HEADER", s, 13))
1029 /* handle above, in msg_fetch_header */
1033 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)
1053 dprint (1, (debugfile, "msg_parse_flags: not a FLAGS response: %s\n",
1061 dprint (1, (debugfile, "msg_parse_flags: bogus FLAGS response: %s\n",
1068 while (*s && *s != ')')
1070 if (ascii_strncasecmp ("\\deleted", s, 8) == 0)
1075 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0)
1080 else if (ascii_strncasecmp ("\\answered", s, 9) == 0)
1085 else if (ascii_strncasecmp ("\\seen", s, 5) == 0)
1090 else if (ascii_strncasecmp ("\\recent", s, 5) == 0)
1097 /* store custom flags as well */
1099 char* flag_word = s;
1101 if (!h->data->keywords)
1102 h->data->keywords = mutt_new_list ();
1104 while (*s && !ISSPACE (*s) && *s != ')')
1108 mutt_add_list (h->data->keywords, flag_word);
1114 /* wrap up, or note bad flags response */
1117 /* if a message is neither seen nor recent, it is OLD. */
1118 if (option (OPTMARKOLD) && !recent && !(h->read))
1124 dprint (1, (debugfile,
1125 "msg_parse_flags: Unterminated FLAGS response: %s\n", s));
1132 static void flush_buffer(char *buf, size_t *len, CONNECTION *conn)
1135 mutt_socket_write(conn, buf);