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 * Changed to read many headers instead of just one. It will return the
44 * msgno of the last message read. It will return a value other than
45 * msgend if mail comes in while downloading headers (in theory).
47 int imap_read_headers (IMAP_DATA* idata, int msgbegin, int msgend)
50 char buf[LONG_STRING];
53 char tempfile[_POSIX_PATH_MAX];
56 int rc, mfhrc, oldmsgcount;
58 const char *want_headers = "DATE FROM SUBJECT TO CC MESSAGE-ID REFERENCES CONTENT-TYPE IN-REPLY-TO REPLY-TO LINES X-LABEL";
62 if (mutt_bit_isset (idata->capabilities,IMAP4REV1))
64 snprintf (hdrreq, sizeof (hdrreq), "BODY.PEEK[HEADER.FIELDS (%s)]",
67 else if (mutt_bit_isset (idata->capabilities,IMAP4))
69 snprintf (hdrreq, sizeof (hdrreq), "RFC822.HEADER.LINES (%s)",
73 { /* Unable to fetch headers for lower versions */
74 mutt_error _("Unable to fetch headers from this IMAP server version.");
75 mutt_sleep (2); /* pause a moment to let the user see the error */
79 /* instead of downloading all headers and then parsing them, we parse them
81 mutt_mktemp (tempfile);
82 if (!(fp = safe_fopen (tempfile, "w+")))
84 mutt_error (_("Could not create temporary file %s"), tempfile);
90 /* make sure context has room to hold the mailbox */
91 while ((msgend) >= idata->ctx->hdrmax)
92 mx_alloc_memory (idata->ctx);
94 oldmsgcount = ctx->msgcount;
95 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
96 idata->newMailCount = 0;
98 for (msgno = msgbegin; msgno <= msgend ; msgno++)
100 if (ReadInc && (!msgno || ((msgno+1) % ReadInc == 0)))
101 mutt_message (_("Fetching message headers... [%d/%d]"), msgno + 1,
104 if (msgno + 1 > fetchlast)
107 * Make one request for everything. This makes fetching headers an
108 * order of magnitude faster if you have a large mailbox.
110 * If we get more messages while doing this, we make another
111 * request for all the new messages.
113 snprintf (buf, sizeof (buf),
114 "FETCH %d:%d (UID FLAGS INTERNALDATE RFC822.SIZE %s)", msgno + 1,
117 imap_cmd_start (idata, buf);
119 fetchlast = msgend + 1;
124 memset (&h, 0, sizeof (h));
125 h.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
127 /* this DO loop does two things:
128 * 1. handles untagged messages, so we can try again on the same msg
129 * 2. fetches the tagged response at the end of the last message.
135 rc = imap_cmd_step (idata);
136 if (rc != IMAP_CMD_CONTINUE)
139 if ((mfhrc = msg_fetch_header (idata->ctx, &h, idata->cmd.buf, fp)) == -1)
144 /* make sure we don't get remnants from older larger message headers */
147 /* update context with message header */
148 ctx->hdrs[msgno] = mutt_new_header ();
150 ctx->hdrs[msgno]->index = h.sid - 1;
151 if (h.sid != ctx->msgcount + 1)
152 dprint (1, (debugfile, "imap_read_headers: msgcount and sequence ID are inconsistent!"));
153 /* messages which have not been expunged are ACTIVE (borrowed from mh
155 ctx->hdrs[msgno]->active = 1;
156 ctx->hdrs[msgno]->read = h.read;
157 ctx->hdrs[msgno]->old = h.old;
158 ctx->hdrs[msgno]->deleted = h.deleted;
159 ctx->hdrs[msgno]->flagged = h.flagged;
160 ctx->hdrs[msgno]->replied = h.replied;
161 ctx->hdrs[msgno]->changed = h.changed;
162 ctx->hdrs[msgno]->received = h.received;
163 ctx->hdrs[msgno]->data = (void *) (h.data);
166 /* NOTE: if Date: header is missing, mutt_read_rfc822_header depends
167 * on h.received being set */
168 ctx->hdrs[msgno]->env = mutt_read_rfc822_header (fp, ctx->hdrs[msgno],
170 /* content built as a side-effect of mutt_read_rfc822_header */
171 ctx->hdrs[msgno]->content->length = h.content_length;
175 while ((rc != IMAP_CMD_OK) && ((mfhrc == -1) ||
176 ((msgno + 1) >= fetchlast)));
178 if ((mfhrc < -1) || ((rc != IMAP_CMD_CONTINUE) && (rc != IMAP_CMD_OK)))
180 imap_free_header_data ((void**) &h.data);
186 /* in case we get new mail while fetching the headers */
187 if (idata->reopen & IMAP_NEWMAIL_PENDING)
189 msgend = idata->newMailCount - 1;
190 while ((msgend) >= ctx->hdrmax)
191 mx_alloc_memory (ctx);
192 idata->reopen &= ~IMAP_NEWMAIL_PENDING;
193 idata->newMailCount = 0;
199 if (ctx->msgcount > oldmsgcount)
200 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
205 int imap_fetch_message (MESSAGE *msg, CONTEXT *ctx, int msgno)
209 char buf[LONG_STRING];
210 char path[_POSIX_PATH_MAX];
218 /* Sam's weird courier server returns an OK response even when FETCH
219 * fails. Thanks Sam. */
222 idata = (IMAP_DATA*) ctx->data;
223 h = ctx->hdrs[msgno];
225 /* see if we already have the message in our cache */
226 cacheno = HEADER_DATA(h)->uid % IMAP_CACHE_LEN;
227 cache = &idata->cache[cacheno];
231 if (cache->uid == HEADER_DATA(h)->uid)
233 /* yes, so just return a pointer to the message */
234 if (!(msg->fp = fopen (cache->path, "r")))
236 mutt_perror (cache->path);
243 /* clear the previous entry */
244 unlink (cache->path);
250 mutt_message _("Fetching message...");
252 cache->uid = HEADER_DATA(h)->uid;
254 cache->path = safe_strdup (path);
255 if (!(msg->fp = safe_fopen (path, "w+")))
261 /* mark this header as currently inactive so the command handler won't
262 * also try to update it. HACK until all this code can be moved into the
266 snprintf (buf, sizeof (buf), "UID FETCH %d %s", HEADER_DATA(h)->uid,
267 (mutt_bit_isset (idata->capabilities, IMAP4REV1) ?
268 (option (OPTIMAPPEEK) ? "BODY.PEEK[]" : "BODY[]") :
271 imap_cmd_start (idata, buf);
274 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
278 pc = imap_next_word (pc);
279 pc = imap_next_word (pc);
281 if (!ascii_strncasecmp ("FETCH", pc, 5))
285 pc = imap_next_word (pc);
288 if (ascii_strncasecmp ("UID", pc, 3) == 0)
290 pc = imap_next_word (pc);
292 if (uid != HEADER_DATA(h)->uid)
293 mutt_error (_("The message index is incorrect. Try reopening the mailbox."));
295 else if ((ascii_strncasecmp ("RFC822", pc, 6) == 0) ||
296 (ascii_strncasecmp ("BODY[]", pc, 6) == 0))
298 pc = imap_next_word (pc);
299 if (imap_get_literal_count(pc, &bytes) < 0)
301 imap_error ("imap_fetch_message()", buf);
304 if (imap_read_literal (msg->fp, idata, bytes) < 0)
306 /* pick up trailing line */
307 if ((rc = imap_cmd_step (idata)) != IMAP_CMD_CONTINUE)
313 /* UW-IMAP will provide a FLAGS update here if the FETCH causes a
314 * change (eg from \Unseen to \Seen).
315 * Uncommitted changes in mutt take precedence. If we decide to
316 * incrementally update flags later, this won't stop us syncing */
317 else if ((ascii_strncasecmp ("FLAGS", pc, 5) == 0) && !h->changed)
319 if ((pc = imap_set_flags (idata, h, pc)) == NULL)
325 while (rc == IMAP_CMD_CONTINUE);
327 /* see comment before command start. */
330 if (rc != IMAP_CMD_OK)
333 if (!fetched || !imap_code (idata->cmd.buf))
336 /* Update the header information. Previously, we only downloaded a
337 * portion of the headers, those required for the main display.
340 /* It may be that the Status header indicates a message is read, but the
341 * IMAP server doesn't know the message has been \Seen. So we capture
342 * the server's notion of 'read' and if it differs from the message info
343 * picked up in mutt_read_rfc822_header, we mark the message (and context
344 * changed). Another possiblity: ignore Status on IMAP?*/
346 /* I hate do this here, since it's so low-level, but I'm not sure where
347 * I can abstract it. Problem: the id and subj hashes lose their keys when
348 * mutt_free_envelope gets called, but keep their spots in the hash. This
349 * confuses threading. Alternatively we could try to merge the new
350 * envelope into the old one. Also messy and lowlevel. */
351 if (ctx->id_hash && h->env->message_id)
352 hash_delete (ctx->id_hash, h->env->message_id, h, NULL);
353 if (ctx->subj_hash && h->env->real_subj)
354 hash_delete (ctx->subj_hash, h->env->real_subj, h, NULL);
355 mutt_free_envelope (&h->env);
356 h->env = mutt_read_rfc822_header (msg->fp, h, 0, 0);
357 if (ctx->id_hash && h->env->message_id)
358 hash_insert (ctx->id_hash, h->env->message_id, h, 0);
359 if (ctx->subj_hash && h->env->real_subj)
360 hash_insert (ctx->subj_hash, h->env->real_subj, h, 1);
362 /* see above. We want the new status in h->read, so we unset it manually
363 * and let mutt_set_flag set it correctly, updating context. */
367 mutt_set_flag (ctx, h, M_NEW, read);
371 fgets (buf, sizeof (buf), msg->fp);
372 while (!feof (msg->fp))
375 fgets (buf, sizeof (buf), msg->fp);
378 h->content->length = ftell (msg->fp) - h->content->offset;
380 /* This needs to be done in case this is a multipart message */
381 #if defined(HAVE_PGP) || defined(HAVE_SMIME)
382 h->security = crypt_query (h->content);
391 safe_fclose (&msg->fp);
394 unlink (cache->path);
401 int imap_append_message (CONTEXT *ctx, MESSAGE *msg)
405 char buf[LONG_STRING];
406 char mbox[LONG_STRING];
407 char mailbox[LONG_STRING];
413 idata = (IMAP_DATA*) ctx->data;
415 if (imap_parse_path (ctx->path, &mx))
418 imap_fix_path (idata, mx.mbox, mailbox, sizeof (mailbox));
420 if ((fp = fopen (msg->path, "r")) == NULL)
422 mutt_perror (msg->path);
426 /* currently we set the \Seen flag on all messages, but probably we
427 * should scan the message Status header for flag info. Since we're
428 * already rereading the whole file for length it isn't any more
429 * expensive (it'd be nice if we had the file size passed in already
430 * by the code that writes the file, but that's a lot of changes.
431 * Ideally we'd have a HEADER structure with flag info here... */
432 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
434 if(c == '\n' && last != '\r')
441 imap_munge_mbox_name (mbox, sizeof (mbox), mailbox);
442 snprintf (buf, sizeof (buf), "APPEND %s (\\Seen) {%d}", mbox, len);
444 imap_cmd_start (idata, buf);
447 rc = imap_cmd_step (idata);
448 while (rc == IMAP_CMD_CONTINUE);
450 if (rc != IMAP_CMD_RESPOND)
454 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
457 pc = idata->cmd.buf + SEQLEN;
459 pc = imap_next_word (pc);
460 mutt_error ("%s", pc);
466 mutt_message _("Uploading message ...");
468 for (last = EOF, len = 0; (c = fgetc(fp)) != EOF; last = c)
470 if (c == '\n' && last != '\r')
475 if (len > sizeof(buf) - 3)
476 flush_buffer(buf, &len, idata->conn);
480 flush_buffer(buf, &len, idata->conn);
482 mutt_socket_write (idata->conn, "\r\n");
486 rc = imap_cmd_step (idata);
487 while (rc == IMAP_CMD_CONTINUE);
489 if (!imap_code (idata->cmd.buf))
493 dprint (1, (debugfile, "imap_append_message(): command failed: %s\n",
495 pc = idata->cmd.buf + SEQLEN;
497 pc = imap_next_word (pc);
498 mutt_error ("%s", pc);
511 /* imap_copy_messages: use server COPY command to copy messages to another
516 * 1: non-fatal error - try fetch/append */
517 int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete)
522 char mbox[LONG_STRING];
523 char mmbox[LONG_STRING];
528 idata = (IMAP_DATA*) ctx->data;
530 if (imap_parse_path (dest, &mx))
532 dprint (1, (debugfile, "imap_copy_messages: bad destination %s\n", dest));
536 /* check that the save-to folder is in the same account */
537 if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account)))
539 dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n",
544 if (h && h->attach_del)
546 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
550 imap_fix_path (idata, mx.mbox, mbox, sizeof (mbox));
552 memset (&cmd, 0, sizeof (cmd));
553 mutt_buffer_addstr (&cmd, "UID COPY ");
555 /* Null HEADER* means copy tagged messages */
558 /* if any messages have attachments to delete, fall through to FETCH
559 * and APPEND. TODO: Copy what we can with COPY, fall through for the
561 for (n = 0; n < ctx->msgcount; n++)
563 if (ctx->hdrs[n]->tagged && ctx->hdrs[n]->attach_del)
565 dprint (3, (debugfile, "imap_copy_messages: Message contains attachments to be deleted\n"));
570 rc = imap_make_msg_set (idata, &cmd, M_TAG, 0);
573 dprint (1, (debugfile, "imap_copy_messages: No messages tagged\n"));
576 mutt_message (_("Copying %d messages to %s..."), rc, mbox);
580 mutt_message (_("Copying message %d to %s..."), h->index+1, mbox);
581 snprintf (uid, sizeof (uid), "%u", HEADER_DATA (h)->uid);
582 mutt_buffer_addstr (&cmd, uid);
585 /* let's get it on */
586 mutt_buffer_addstr (&cmd, " ");
587 imap_munge_mbox_name (mmbox, sizeof (mmbox), mbox);
588 mutt_buffer_addstr (&cmd, mmbox);
590 rc = imap_exec (idata, cmd.data, IMAP_CMD_FAIL_OK);
593 /* bail out if command failed for reasons other than nonexistent target */
594 if (ascii_strncasecmp (imap_get_qualifier (idata->cmd.buf), "[TRYCREATE]", 11))
596 imap_error ("imap_copy_messages", idata->cmd.buf);
599 dprint (2, (debugfile, "imap_copy_messages: server suggests TRYCREATE\n"));
600 snprintf (mmbox, sizeof (mmbox), _("Create %s?"), mbox);
601 if (option (OPTCONFIRMCREATE) && mutt_yesorno (mmbox, 1) < 1)
606 if (imap_create_mailbox (idata, mbox) < 0)
610 rc = imap_exec (idata, cmd.data, 0);
614 imap_error ("imap_copy_messages", idata->cmd.buf);
622 for (n = 0; n < ctx->msgcount; n++)
624 if (ctx->hdrs[n]->tagged)
626 mutt_set_flag (ctx, ctx->hdrs[n], M_DELETE, 1);
627 mutt_set_flag (ctx, ctx->hdrs[n], M_APPENDED, 1);
628 if (option (OPTDELETEUNTAG))
629 mutt_set_flag (ctx, ctx->hdrs[n], M_TAG, 0);
634 mutt_set_flag (ctx, h, M_DELETE, 1);
635 mutt_set_flag (ctx, h, M_APPENDED, 1);
636 if (option (OPTDELETEUNTAG))
637 mutt_set_flag (ctx, h, M_TAG, 0);
653 /* imap_add_keywords: concatenate custom IMAP tags to list, if they
654 * appear in the folder flags list. Why wouldn't they? */
655 void imap_add_keywords (char* s, HEADER* h, LIST* mailbox_flags, size_t slen)
659 if (!mailbox_flags || !HEADER_DATA(h) || !HEADER_DATA(h)->keywords)
662 keywords = HEADER_DATA(h)->keywords->next;
666 if (msg_has_flag (mailbox_flags, keywords->data))
668 strncat (s, keywords->data, slen);
669 strncat (s, " ", slen);
671 keywords = keywords->next;
675 /* imap_free_header_data: free IMAP_HEADER structure */
676 void imap_free_header_data (void** data)
678 /* this should be safe even if the list wasn't used */
679 mutt_free_list (&(((IMAP_HEADER_DATA*) *data)->keywords));
684 /* imap_set_flags: fill out the message header according to the flags from
685 * the server. Expects a flags line of the form "FLAGS (flag flag ...)" */
686 char* imap_set_flags (IMAP_DATA* idata, HEADER* h, char* s)
688 CONTEXT* ctx = idata->ctx;
690 unsigned char readonly;
692 memset (&newh, 0, sizeof (newh));
693 newh.data = safe_calloc (1, sizeof (IMAP_HEADER_DATA));
695 dprint (2, (debugfile, "imap_fetch_message: parsing FLAGS\n"));
696 if ((s = msg_parse_flags (&newh, s)) == NULL)
702 /* YAUH (yet another ugly hack): temporarily set context to
703 * read-write even if it's read-only, so *server* updates of
704 * flags can be processed by mutt_set_flag. ctx->changed must
705 * be restored afterwards */
706 readonly = ctx->readonly;
709 mutt_set_flag (ctx, h, M_NEW, !(newh.read || newh.old));
710 mutt_set_flag (ctx, h, M_OLD, newh.old);
711 mutt_set_flag (ctx, h, M_READ, newh.read);
712 mutt_set_flag (ctx, h, M_DELETE, newh.deleted);
713 mutt_set_flag (ctx, h, M_FLAG, newh.flagged);
714 mutt_set_flag (ctx, h, M_REPLIED, newh.replied);
716 /* this message is now definitively *not* changed (mutt_set_flag
717 * marks things changed as a side-effect) */
719 ctx->changed &= ~readonly;
720 ctx->readonly = readonly;
722 mutt_free_list (&(HEADER_DATA(h)->keywords));
723 HEADER_DATA(h)->keywords = newh.data->keywords;
729 /* msg_fetch_header: import IMAP FETCH response into an IMAP_HEADER.
730 * Expects string beginning with * n FETCH.
733 * -1 if the string is not a fetch response
734 * -2 if the string is a corrupt fetch response */
735 static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf, FILE* fp)
739 int rc = -1; /* default now is that string isn't FETCH response*/
741 idata = (IMAP_DATA*) ctx->data;
746 /* skip to message number */
747 buf = imap_next_word (buf);
751 buf = imap_next_word (buf);
752 if (ascii_strncasecmp ("FETCH", buf, 5))
755 rc = -2; /* we've got a FETCH response, for better or worse */
756 if (!(buf = strchr (buf, '(')))
760 /* FIXME: current implementation - call msg_parse_fetch - if it returns -2,
761 * read header lines and call it again. Silly. */
762 if (msg_parse_fetch (h, buf) != -2)
765 if (imap_get_literal_count (buf, &bytes) < 0)
767 imap_read_literal (fp, idata, bytes);
769 /* we may have other fields of the FETCH _after_ the literal
770 * (eg Domino puts FLAGS here). Nothing wrong with that, either.
771 * This all has to go - we should accept literals and nonliterals
772 * interchangeably at any time. */
773 if (imap_cmd_step (idata) != IMAP_CMD_CONTINUE)
776 if (msg_parse_fetch (h, idata->cmd.buf) == -1)
779 rc = 0; /* success */
781 /* subtract headers from message size - unfortunately only the subset of
782 * headers we've requested. */
783 h->content_length -= bytes;
788 /* msg_has_flag: do a caseless comparison of the flag against a flag list,
789 * return 1 if found or flag list has '\*', 0 otherwise */
790 static int msg_has_flag (LIST* flag_list, const char* flag)
795 flag_list = flag_list->next;
798 if (!ascii_strncasecmp (flag_list->data, flag, strlen (flag_list->data)))
801 flag_list = flag_list->next;
807 /* msg_parse_fetch: handle headers returned from header fetch */
808 static int msg_parse_fetch (IMAP_HEADER *h, char *s)
810 char tmp[SHORT_STRING];
820 if (ascii_strncasecmp ("FLAGS", s, 5) == 0)
822 if ((s = msg_parse_flags (h, s)) == NULL)
825 else if (ascii_strncasecmp ("UID", s, 3) == 0)
829 h->data->uid = (unsigned int) atoi (s);
831 s = imap_next_word (s);
833 else if (ascii_strncasecmp ("INTERNALDATE", s, 12) == 0)
839 dprint (1, (debugfile, "msg_parse_fetch(): bogus INTERNALDATE entry: %s\n", s));
844 while (*s && *s != '\"')
848 s++; /* skip past the trailing " */
850 h->received = imap_parse_date (tmp);
852 else if (ascii_strncasecmp ("RFC822.SIZE", s, 11) == 0)
857 while (isdigit ((unsigned char) *s))
860 h->content_length = atoi (tmp);
862 else if (!ascii_strncasecmp ("BODY", s, 4) ||
863 !ascii_strncasecmp ("RFC822.HEADER", s, 13))
865 /* handle above, in msg_fetch_header */
869 s++; /* end of request */
872 /* got something i don't understand */
873 imap_error ("msg_parse_fetch", s);
881 /* msg_parse_flags: read a FLAGS token into an IMAP_HEADER */
882 static char* msg_parse_flags (IMAP_HEADER* h, char* s)
886 /* sanity-check string */
887 if (ascii_strncasecmp ("FLAGS", s, 5) != 0)
889 dprint (1, (debugfile, "msg_parse_flags: not a FLAGS response: %s\n",
897 dprint (1, (debugfile, "msg_parse_flags: bogus FLAGS response: %s\n",
904 while (*s && *s != ')')
906 if (ascii_strncasecmp ("\\deleted", s, 8) == 0)
911 else if (ascii_strncasecmp ("\\flagged", s, 8) == 0)
916 else if (ascii_strncasecmp ("\\answered", s, 9) == 0)
921 else if (ascii_strncasecmp ("\\seen", s, 5) == 0)
926 else if (ascii_strncasecmp ("\\recent", s, 5) == 0)
933 /* store custom flags as well */
937 if (!h->data->keywords)
938 h->data->keywords = mutt_new_list ();
940 while (*s && !ISSPACE (*s) && *s != ')')
944 mutt_add_list (h->data->keywords, flag_word);
950 /* wrap up, or note bad flags response */
953 /* if a message is neither seen nor recent, it is OLD. */
954 if (option (OPTMARKOLD) && !recent && !(h->read))
960 dprint (1, (debugfile,
961 "msg_parse_flags: Unterminated FLAGS response: %s\n", s));
968 static void flush_buffer(char *buf, size_t *len, CONNECTION *conn)
971 mutt_socket_write(conn, buf);