2 * Copyright notice from original mutt:
3 * Copyright (C) 1996-2002 Michael R. Elkins <me@mutt.org>
5 * This file is part of mutt-ng, see http://www.muttng.org/.
6 * It's licensed under the GNU General Public License,
7 * please see the file GPL in the top level source directory.
10 /* This file contains code to parse ``mbox'' style mailboxes */
12 #include <lib-lib/lib-lib.h>
14 #include <lib-ui/lib-ui.h>
15 #include <lib-sys/mutt_signal.h>
26 /* struct used by mutt_sync_mailbox() to store new offsets */
35 static int mbox_open_new_message(MESSAGE *msg, CONTEXT *dest, HEADER *hdr)
41 static int mbox_open_message(MESSAGE *msg, CONTEXT *ctx, int msgno)
48 static int mbox_reopen_mailbox (CONTEXT*, int*);
51 * ctx - context to lock
52 * excl - exclusive lock?
53 * retry - should retry if unable to lock?
55 int mbox_lock_mailbox(CONTEXT *ctx, int excl, int retry)
59 if ((r = mx_lock_file(ctx->path, fileno(ctx->fp), excl, retry)) == 0)
61 else if (retry && !excl) {
69 static void mbox_unlock_mailbox (CONTEXT * ctx)
74 mx_unlock_file(ctx->path, fileno(ctx->fp));
79 /* Note that this function is also called when new mail is appended to the
80 * currently open folder, and NOT just when the mailbox is initially read.
82 * NOTE: it is assumed that the mailbox being read has been locked before
83 * this routine gets called. Strange things could happen if it's not!
85 static int mbox_parse_mailbox (CONTEXT * ctx)
88 char buf[HUGE_STRING], return_path[STRING];
91 int count = 0, lines = 0;
94 /* Save information about the folder at the time we opened it. */
95 if (stat (ctx->path, &sb) == -1) {
96 mutt_perror (ctx->path);
100 ctx->size = sb.st_size;
101 ctx->mtime = sb.st_mtime;
104 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
106 /* precompute the local timezone to speed up calculation of the
109 tz = localtime(&t)->tm_gmtoff;
111 loc = ftello (ctx->fp);
112 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
113 if (is_from (buf, return_path, sizeof (return_path), &t)) {
114 /* Save the Content-Length of the previous message */
116 #define PREV ctx->hdrs[ctx->msgcount-1]
118 if (PREV->content->length < 0) {
119 PREV->content->length = loc - PREV->content->offset - 1;
120 if (PREV->content->length < 0)
121 PREV->content->length = 0;
124 PREV->lines = lines ? lines - 1 : 0;
129 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
130 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
131 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
133 if (ctx->msgcount == ctx->hdrmax)
134 mx_alloc_memory (ctx);
136 curhdr = ctx->hdrs[ctx->msgcount] = header_new();
137 curhdr->received = t - tz;
138 curhdr->offset = loc;
139 curhdr->index = ctx->msgcount;
141 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
143 /* if we know how long this message is, either just skip over the body,
144 * or if we don't know how many lines there are, count them now (this will
145 * save time by not having to search for the next message marker).
147 if (curhdr->content->length > 0) {
150 loc = ftello (ctx->fp);
151 tmploc = loc + curhdr->content->length + 1;
153 if (0 < tmploc && tmploc < ctx->size) {
155 * check to see if the content-length looks valid. we expect to
156 * to see a valid message separator at this point in the stream
158 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
159 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
160 m_strncmp("From ", buf, 5) != 0) {
161 fseeko (ctx->fp, loc, SEEK_SET); /* nope, return the previous position */
162 curhdr->content->length = -1;
165 else if (tmploc != ctx->size) {
166 /* content-length would put us past the end of the file, so it
169 curhdr->content->length = -1;
172 if (curhdr->content->length != -1) {
173 /* good content-length. check to see if we know how many lines
174 * are in this message.
176 if (curhdr->lines == 0) {
177 int cl = curhdr->content->length;
179 /* count the number of lines in this message */
180 fseeko (ctx->fp, loc, SEEK_SET);
182 if (fgetc (ctx->fp) == '\n')
187 /* return to the offset of the next message separator */
188 fseeko(ctx->fp, tmploc, SEEK_SET);
194 if (!curhdr->env->return_path && return_path[0])
195 curhdr->env->return_path =
196 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
198 if (!curhdr->env->from)
199 curhdr->env->from = address_list_dup (curhdr->env->return_path);
206 loc = ftello (ctx->fp);
210 * Only set the content-length of the previous message if we have read more
211 * than one message during _this_ invocation. If this routine is called
212 * when new mail is received, we need to make sure not to clobber what
213 * previously was the last message since the headers may be sorted.
216 if (PREV->content->length < 0) {
217 PREV->content->length = ftello (ctx->fp) - PREV->content->offset - 1;
218 if (PREV->content->length < 0)
219 PREV->content->length = 0;
223 PREV->lines = lines ? lines - 1 : 0;
225 mx_update_context (ctx, count);
233 /* open a mbox style mailbox */
234 static int mbox_open_mailbox (CONTEXT * ctx)
238 if (!(ctx->fp = fopen(ctx->path, "r"))) {
239 mutt_perror(ctx->path);
243 mutt_block_signals();
244 if (mbox_lock_mailbox(ctx, 0, 1) < 0) {
245 mutt_unblock_signals();
249 rc = ctx->magic == M_MBOX ? mbox_parse_mailbox(ctx) : -1;
251 mbox_unlock_mailbox(ctx);
252 mutt_unblock_signals();
256 /* check to see if the mailbox has changed on disk.
259 * M_REOPENED mailbox has been reopened
260 * M_NEW_MAIL new mail has arrived!
261 * M_LOCKED couldn't lock the file
265 static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
268 char buffer[LONG_STRING];
272 if (stat (ctx->path, &st) == 0) {
273 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
276 if (st.st_size == ctx->size) {
277 /* the file was touched, but it is still the same length, so just exit */
278 ctx->mtime = st.st_mtime;
282 if (st.st_size > ctx->size) {
283 /* lock the file if it isn't already */
285 mutt_block_signals ();
286 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
287 mutt_unblock_signals ();
288 /* we couldn't lock the mailbox, but nothing serious happened:
289 * probably the new mail arrived: no reason to wait till we can
290 * parse it: we'll get it on the next pass
298 * Check to make sure that the only change to the mailbox is that
299 * message(s) were appended to this file. My heuristic is that we should
300 * see the message separator at *exactly* what used to be the end of the
303 fseeko (ctx->fp, ctx->size, SEEK_SET);
304 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
305 if (ctx->magic == M_MBOX && m_strncmp("From ", buffer, 5) == 0) {
306 fseeko (ctx->fp, ctx->size, SEEK_SET);
307 mbox_parse_mailbox (ctx);
309 /* Only unlock the folder if it was locked inside of this routine.
310 * It may have been locked elsewhere, like in
311 * mutt_checkpoint_mailbox().
315 mbox_unlock_mailbox (ctx);
316 mutt_unblock_signals ();
319 return M_NEW_MAIL; /* signal that new mail arrived */
332 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
334 mbox_unlock_mailbox (ctx);
335 mutt_unblock_signals ();
343 mbox_unlock_mailbox (ctx);
344 mx_fastclose_mailbox (ctx);
345 mutt_unblock_signals ();
346 mutt_error _("Mailbox was corrupted!");
351 static int mbox_check_mailbox(CONTEXT *ctx, int *index_hint, int lock)
356 mutt_block_signals();
357 if (mbox_lock_mailbox(ctx, 0, 0) < 0) {
358 mutt_unblock_signals();
363 rc = _mbox_check_mailbox(ctx, index_hint);
366 mutt_unblock_signals ();
367 mbox_unlock_mailbox (ctx);
376 static int mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)), int *index_hint)
378 char tempfile[_POSIX_PATH_MAX];
380 int i, j, save_sort = SORT_ORDER;
382 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
383 int first = -1; /* first message to be written */
384 off_t offset; /* location in mailbox to write changed messages */
386 struct utimbuf utimebuf;
387 struct m_update_t *newOffset = NULL;
388 struct m_update_t *oldOffset = NULL;
391 /* sort message by their position in the mailbox on disk */
392 if (Sort != SORT_ORDER) {
395 mutt_sort_headers (ctx, 0);
400 /* need to open the file for writing in such a way that it does not truncate
401 * the file, so use read-write mode.
403 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
404 mx_fastclose_mailbox (ctx);
405 mutt_error _("Fatal error! Could not reopen mailbox!");
410 mutt_block_signals ();
412 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
413 mutt_unblock_signals ();
414 mutt_error _("Unable to lock mailbox!");
419 /* Check to make sure that the file hasn't changed on disk */
420 if ((i = _mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
421 || i == M_REOPENED) {
422 /* new mail arrived, or mailbox reopened */
431 /* Create a temporary file to write the new version of the mailbox in. */
432 fp = m_tempfile(tempfile, _POSIX_PATH_MAX, NONULL(mod_core.tmpdir), NULL);
434 mutt_error _("Could not create temporary file!");
439 /* find the first deleted/changed message. we save a lot of time by only
440 * rewriting the mailbox from the point where it has actually changed.
442 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
443 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
444 if (i == ctx->msgcount) {
445 /* this means ctx->changed or ctx->deleted was set, but no
446 * messages were found to be changed or deleted. This should
447 * never happen, is we presume it is a bug in mutt.
450 _("sync: mbox modified, but no modified messages! (report this bug)");
451 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
456 /* save the index of the first changed/deleted message */
458 /* where to start overwriting */
459 offset = ctx->hdrs[i]->offset;
461 /* allocate space for the new offsets */
462 newOffset = p_new(struct m_update_t, ctx->msgcount - first);
463 oldOffset = p_new(struct m_update_t, ctx->msgcount - first);
465 for (i = first, j = 0; i < ctx->msgcount; i++) {
467 * back up some information which is needed to restore offsets when
471 oldOffset[i - first].valid = 1;
472 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
473 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
474 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
475 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
477 if (!ctx->hdrs[i]->deleted) {
479 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
480 mutt_message (_("Writing messages... %d (%d%%)"), i,
481 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
483 /* save the new offset for this message. we add `offset' because the
484 * temporary file only contains saved message which are located after
485 * `offset' in the real mailbox
487 newOffset[i - first].hdr = ftello (fp) + offset;
489 if (mutt_copy_message
490 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
491 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
492 mutt_perror (_("Can't create temporary file"));
498 /* Since messages could have been deleted, the offsets stored in memory
499 * will be wrong, so update what we can, which is the offset of this
500 * message, and the offset of the body. If this is a multipart message,
501 * we just flush the in memory cache so that the message will be reparsed
502 * if the user accesses it later.
504 newOffset[i - first].body =
505 ftello (fp) - ctx->hdrs[i]->content->length + offset;
506 body_list_wipe(&ctx->hdrs[i]->content->parts);
508 if (fputs ("\n", fp) == EOF) {
509 mutt_perror (_("Can't create temporary file"));
517 if (m_fclose(&fp) != 0) {
519 mutt_perror (_("Can't create temporary file"));
524 /* Save the state of this folder. */
525 if (stat (ctx->path, &statbuf) == -1) {
526 mutt_perror (ctx->path);
532 if ((fp = fopen (tempfile, "r")) == NULL) {
533 mutt_unblock_signals ();
534 mx_fastclose_mailbox (ctx);
535 mutt_perror (_("Can't create temporary file"));
540 if (fseeko (ctx->fp, offset, SEEK_SET) != 0 /* seek the append location */
541 /* do a sanity check to make sure the mailbox looks ok */
542 || fgets (buf, sizeof (buf), ctx->fp) == NULL
543 || (ctx->magic == M_MBOX && m_strncmp("From ", buf, 5) != 0))
548 if (fseeko (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
551 /* copy the temp mailbox back into place starting at the first
552 * change/deleted message
554 mutt_message _("Committing changes...");
556 i = mutt_copy_stream (fp, ctx->fp);
558 if (ferror (ctx->fp))
562 ctx->size = ftello (ctx->fp); /* update the size of the mailbox */
563 ftruncate (fileno (ctx->fp), ctx->size);
568 mbox_unlock_mailbox (ctx);
570 if (m_fclose(&ctx->fp) != 0 || i == -1) {
571 /* error occured while writing the mailbox back, so keep the temp copy
575 char savefile[_POSIX_PATH_MAX];
577 snprintf(savefile, sizeof (savefile), "%s/mutt.%s-%u",
578 NONULL(mod_core.tmpdir), NONULL(mod_core.username), (unsigned int)getpid());
579 rename (tempfile, savefile);
580 mutt_unblock_signals ();
581 mx_fastclose_mailbox (ctx);
582 mutt_pretty_mailbox (savefile);
583 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
588 /* Restore the previous access/modification times */
589 utimebuf.actime = statbuf.st_atime;
590 utimebuf.modtime = statbuf.st_mtime;
591 utime (ctx->path, &utimebuf);
593 /* reopen the mailbox in read-only mode */
594 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
596 mutt_unblock_signals ();
597 mx_fastclose_mailbox (ctx);
598 mutt_error _("Fatal error! Could not reopen mailbox!");
602 /* update the offsets of the rewritten messages */
603 for (i = first, j = first; i < ctx->msgcount; i++) {
604 if (!ctx->hdrs[i]->deleted) {
605 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
606 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
607 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
608 ctx->hdrs[i]->index = j++;
611 p_delete(&newOffset);
612 p_delete(&oldOffset);
613 unlink (tempfile); /* remove partial copy of the mailbox */
614 mutt_unblock_signals ();
616 return 0; /* signal success */
618 bail: /* Come here in case of disaster */
622 /* restore offsets, as far as they are valid */
623 if (first >= 0 && oldOffset) {
624 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
625 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
626 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
627 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
628 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
629 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
633 /* this is ok to call even if we haven't locked anything */
634 mbox_unlock_mailbox (ctx);
636 mutt_unblock_signals ();
637 p_delete(&newOffset);
638 p_delete(&oldOffset);
640 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
641 mutt_error _("Could not reopen mailbox!");
643 mx_fastclose_mailbox (ctx);
648 /* if the mailbox was reopened, the thread tree will be invalid so make
649 * sure to start threading from scratch. */
650 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
655 /* close a mailbox opened in write-mode */
656 int mbox_close_mailbox (CONTEXT * ctx)
658 mx_unlock_file(ctx->path, fileno(ctx->fp));
661 mutt_slow_close_compressed (ctx);
663 mutt_unblock_signals ();
664 mx_fastclose_mailbox (ctx);
668 static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
670 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
678 /* silent operations */
681 mutt_message _("Reopening mailbox...");
683 /* our heuristics require the old mailbox to be unsorted */
684 if (Sort != SORT_ORDER) {
689 mutt_sort_headers (ctx, 1);
696 /* simulate a close */
698 hash_delete (&ctx->id_hash, NULL);
700 hash_delete (&ctx->subj_hash, NULL);
701 mutt_clear_threads (ctx);
704 for (i = 0; i < ctx->msgcount; i++)
705 header_delete(&(ctx->hdrs[i])); /* nothing to do! */
706 p_delete(&ctx->hdrs);
709 /* save the old headers */
710 old_msgcount = ctx->msgcount;
711 old_hdrs = ctx->hdrs;
715 ctx->hdrmax = 0; /* force allocation of new headers */
725 ctx->subj_hash = NULL;
727 if (ctx->magic == M_MBOX && !fseeko(ctx->fp, 0, SEEK_SET)) {
728 cmp_headers = mutt_cmp_header;
729 rc = mbox_parse_mailbox (ctx);
735 /* free the old headers */
736 for (j = 0; j < old_msgcount; j++)
737 header_delete(&(old_hdrs[j]));
744 /* now try to recover the old flags */
746 index_hint_set = (index_hint == NULL);
748 if (!ctx->readonly) {
749 for (i = 0; i < ctx->msgcount; i++) {
752 /* some messages have been deleted, and new messages have been
753 * appended at the end; the heuristic is that old messages have then
754 * "advanced" towards the beginning of the folder, so we begin the
755 * search at index "i"
757 for (j = i; j < old_msgcount; j++) {
758 if (old_hdrs[j] == NULL)
760 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
766 for (j = 0; j < i && j < old_msgcount; j++) {
767 if (old_hdrs[j] == NULL)
769 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
777 /* this is best done here */
778 if (!index_hint_set && *index_hint == j)
781 if (old_hdrs[j]->changed) {
782 /* Only update the flags if the old header was changed;
783 * otherwise, the header may have been modified externally,
784 * and we don't want to lose _those_ changes
786 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
787 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
788 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
789 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
791 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
792 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
794 /* we don't need this header any more */
795 header_delete(&(old_hdrs[j]));
799 /* free the remaining old headers */
800 for (j = 0; j < old_msgcount; j++) {
802 header_delete(&(old_hdrs[j]));
811 return (ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL;
816 * 1 if the mailbox is not empty
817 * 0 if the mailbox is empty
820 int mbox_check_empty (const char *path)
824 if (stat (path, &st) == -1)
827 return st.st_size == 0;
830 int mbox_is_magic (const char* path, struct stat* st)
834 char tmp[_POSIX_PATH_MAX];
836 if (S_ISDIR(st->st_mode))
839 if (st->st_size == 0) {
842 else if ((f = fopen (path, "r")) != NULL) {
843 struct utimbuf times;
845 fgets (tmp, sizeof (tmp), f);
846 if (m_strncmp("From ", tmp, 5) == 0)
850 /* need to restore the times here, the file was not really accessed,
851 * only the type was accessed. This is important, because detection
852 * of "new mail" depends on those times set correctly.
854 times.actime = st->st_atime;
855 times.modtime = st->st_mtime;
856 utime (path, ×);
859 return -1; /* fopen failed */
862 if (magic == -1 && mutt_can_read_compressed (path))
867 static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) {
868 if (fputc ('\n', msg->fp) == EOF)
870 if ((fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
871 mutt_perror (_("Can't write message"));
877 mx_t const mbox_mx = {
884 mbox_open_new_message,