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'' and ``mmdf'' style mailboxes */
30 #include "lib/debug.h"
41 /* struct used by mutt_sync_mailbox() to store new offsets */
51 static int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr)
58 static int mbox_reopen_mailbox (CONTEXT*, int*);
61 * ctx - context to lock
62 * excl - exclusive lock?
63 * retry - should retry if unable to lock?
65 int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
69 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
71 else if (retry && !excl) {
79 void mbox_unlock_mailbox (CONTEXT * ctx)
84 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
89 static int mmdf_parse_mailbox (CONTEXT * ctx)
91 char buf[HUGE_STRING];
92 char return_path[LONG_STRING];
93 int count = 0, oldmsgcount = ctx->msgcount;
100 #ifdef NFS_ATTRIBUTE_HACK
101 struct utimbuf newtime;
104 if (stat (ctx->path, &sb) == -1) {
105 mutt_perror (ctx->path);
108 ctx->mtime = sb.st_mtime;
109 ctx->size = sb.st_size;
111 #ifdef NFS_ATTRIBUTE_HACK
112 if (sb.st_mtime > sb.st_atime) {
113 newtime.modtime = sb.st_mtime;
114 newtime.actime = time (NULL);
115 utime (ctx->path, &newtime);
119 /* precompute the local timezone to speed up calculation of the
121 tz = mutt_local_tz (0);
123 buf[sizeof (buf) - 1] = 0;
126 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
129 if (safe_strcmp (buf, MMDF_SEP) == 0) {
130 loc = ftell (ctx->fp);
133 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
134 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
135 (int) (loc / (ctx->size / 100 + 1)));
138 if (ctx->msgcount == ctx->hdrmax)
139 mx_alloc_memory (ctx);
140 ctx->hdrs[ctx->msgcount] = hdr = mutt_new_header ();
142 hdr->index = ctx->msgcount;
144 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
145 /* TODO: memory leak??? */
146 debug_print (1, ("unexpected EOF\n"));
152 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
153 if (fseek (ctx->fp, loc, SEEK_SET) != 0) {
154 debug_print (1, ("fseek() failed\n"));
155 mutt_error _("Mailbox is corrupt!");
161 hdr->received = t - tz;
163 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
165 loc = ftell (ctx->fp);
167 if (hdr->content->length > 0 && hdr->lines > 0) {
168 tmploc = loc + hdr->content->length;
170 if (0 < tmploc && tmploc < ctx->size) {
171 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
172 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
173 safe_strcmp (MMDF_SEP, buf) != 0) {
174 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
175 debug_print (1, ("fseek() failed\n"));
176 hdr->content->length = -1;
180 hdr->content->length = -1;
183 hdr->content->length = -1;
185 if (hdr->content->length < 0) {
188 loc = ftell (ctx->fp);
189 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
192 } while (safe_strcmp (buf, MMDF_SEP) != 0);
195 hdr->content->length = loc - hdr->content->offset;
198 if (!hdr->env->return_path && return_path[0])
199 hdr->env->return_path =
200 rfc822_parse_adrlist (hdr->env->return_path, return_path);
203 hdr->env->from = rfc822_cpy_adr (hdr->env->return_path);
208 debug_print (1, ("corrupt mailbox!\n"));
209 mutt_error _("Mailbox is corrupt!");
215 if (ctx->msgcount > oldmsgcount)
216 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
221 /* Note that this function is also called when new mail is appended to the
222 * currently open folder, and NOT just when the mailbox is initially read.
224 * NOTE: it is assumed that the mailbox being read has been locked before
225 * this routine gets called. Strange things could happen if it's not!
227 static int mbox_parse_mailbox (CONTEXT * ctx)
230 char buf[HUGE_STRING], return_path[STRING];
233 int count = 0, lines = 0;
236 #ifdef NFS_ATTRIBUTE_HACK
237 struct utimbuf newtime;
240 /* Save information about the folder at the time we opened it. */
241 if (stat (ctx->path, &sb) == -1) {
242 mutt_perror (ctx->path);
246 ctx->size = sb.st_size;
247 ctx->mtime = sb.st_mtime;
249 #ifdef NFS_ATTRIBUTE_HACK
250 if (sb.st_mtime > sb.st_atime) {
251 newtime.modtime = sb.st_mtime;
252 newtime.actime = time (NULL);
253 utime (ctx->path, &newtime);
258 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
260 /* precompute the local timezone to speed up calculation of the
262 tz = mutt_local_tz (0);
264 loc = ftell (ctx->fp);
265 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
266 if (is_from (buf, return_path, sizeof (return_path), &t)) {
267 /* Save the Content-Length of the previous message */
269 #define PREV ctx->hdrs[ctx->msgcount-1]
271 if (PREV->content->length < 0) {
272 PREV->content->length = loc - PREV->content->offset - 1;
273 if (PREV->content->length < 0)
274 PREV->content->length = 0;
277 PREV->lines = lines ? lines - 1 : 0;
282 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
283 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
284 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
286 if (ctx->msgcount == ctx->hdrmax)
287 mx_alloc_memory (ctx);
289 curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header ();
290 curhdr->received = t - tz;
291 curhdr->offset = loc;
292 curhdr->index = ctx->msgcount;
294 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
296 /* if we know how long this message is, either just skip over the body,
297 * or if we don't know how many lines there are, count them now (this will
298 * save time by not having to search for the next message marker).
300 if (curhdr->content->length > 0) {
303 loc = ftell (ctx->fp);
304 tmploc = loc + curhdr->content->length + 1;
306 if (0 < tmploc && tmploc < ctx->size) {
308 * check to see if the content-length looks valid. we expect to
309 * to see a valid message separator at this point in the stream
311 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0 ||
312 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
313 safe_strncmp ("From ", buf, 5) != 0) {
314 debug_print (1, ("bad content-length in message %d (cl=%ld)\n",
315 curhdr->index, curhdr->content->length));
316 debug_print (1, ("LINE: %s\n", buf));
317 if (fseek (ctx->fp, loc, SEEK_SET) != 0) { /* nope, return the previous position */
318 debug_print (1, ("fseek() failed\n"));
320 curhdr->content->length = -1;
323 else if (tmploc != ctx->size) {
324 /* content-length would put us past the end of the file, so it
327 curhdr->content->length = -1;
330 if (curhdr->content->length != -1) {
331 /* good content-length. check to see if we know how many lines
332 * are in this message.
334 if (curhdr->lines == 0) {
335 int cl = curhdr->content->length;
337 /* count the number of lines in this message */
338 if (fseek (ctx->fp, loc, SEEK_SET) != 0)
339 debug_print (1, ("fseek() failed\n"));
341 if (fgetc (ctx->fp) == '\n')
346 /* return to the offset of the next message separator */
347 if (fseek (ctx->fp, tmploc, SEEK_SET) != 0)
348 debug_print (1, ("fseek() failed\n"));
354 if (!curhdr->env->return_path && return_path[0])
355 curhdr->env->return_path =
356 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
358 if (!curhdr->env->from)
359 curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path);
366 loc = ftell (ctx->fp);
370 * Only set the content-length of the previous message if we have read more
371 * than one message during _this_ invocation. If this routine is called
372 * when new mail is received, we need to make sure not to clobber what
373 * previously was the last message since the headers may be sorted.
376 if (PREV->content->length < 0) {
377 PREV->content->length = ftell (ctx->fp) - PREV->content->offset - 1;
378 if (PREV->content->length < 0)
379 PREV->content->length = 0;
383 PREV->lines = lines ? lines - 1 : 0;
385 mx_update_context (ctx, count);
393 /* open a mbox or mmdf style mailbox */
394 static int mbox_open_mailbox (CONTEXT * ctx)
398 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
399 mutt_perror (ctx->path);
402 mutt_block_signals ();
403 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
404 mutt_unblock_signals ();
408 if (ctx->magic == M_MBOX)
409 rc = mbox_parse_mailbox (ctx);
410 else if (ctx->magic == M_MMDF)
411 rc = mmdf_parse_mailbox (ctx);
415 mbox_unlock_mailbox (ctx);
416 mutt_unblock_signals ();
420 /* return 1 if address lists are strictly identical */
421 static int strict_addrcmp (const ADDRESS * a, const ADDRESS * b)
424 if (safe_strcmp (a->mailbox, b->mailbox) ||
425 safe_strcmp (a->personal, b->personal))
437 static int strict_cmp_lists (const LIST * a, const LIST * b)
440 if (safe_strcmp (a->data, b->data))
452 static int strict_cmp_envelopes (const ENVELOPE * e1, const ENVELOPE * e2)
455 if (safe_strcmp (e1->message_id, e2->message_id) ||
456 safe_strcmp (e1->subject, e2->subject) ||
457 !strict_cmp_lists (e1->references, e2->references) ||
458 !strict_addrcmp (e1->from, e2->from) ||
459 !strict_addrcmp (e1->sender, e2->sender) ||
460 !strict_addrcmp (e1->reply_to, e2->reply_to) ||
461 !strict_addrcmp (e1->to, e2->to) ||
462 !strict_addrcmp (e1->cc, e2->cc) ||
463 !strict_addrcmp (e1->return_path, e2->return_path))
469 if (e1 == NULL && e2 == NULL)
476 static int strict_cmp_parameters (const PARAMETER * p1, const PARAMETER * p2)
479 if (safe_strcmp (p1->attribute, p2->attribute) ||
480 safe_strcmp (p1->value, p2->value))
492 static int strict_cmp_bodies (const BODY * b1, const BODY * b2)
494 if (b1->type != b2->type ||
495 b1->encoding != b2->encoding ||
496 safe_strcmp (b1->subtype, b2->subtype) ||
497 safe_strcmp (b1->description, b2->description) ||
498 !strict_cmp_parameters (b1->parameter, b2->parameter) ||
499 b1->length != b2->length)
504 /* return 1 if headers are strictly identical */
505 int mbox_strict_cmp_headers (const HEADER * h1, const HEADER * h2)
508 if (h1->received != h2->received ||
509 h1->date_sent != h2->date_sent ||
510 h1->content->length != h2->content->length ||
511 h1->lines != h2->lines ||
512 h1->zhours != h2->zhours ||
513 h1->zminutes != h2->zminutes ||
514 h1->zoccident != h2->zoccident ||
515 h1->mime != h2->mime ||
516 !strict_cmp_envelopes (h1->env, h2->env) ||
517 !strict_cmp_bodies (h1->content, h2->content))
523 if (h1 == NULL && h2 == NULL)
530 /* check to see if the mailbox has changed on disk.
533 * M_REOPENED mailbox has been reopened
534 * M_NEW_MAIL new mail has arrived!
535 * M_LOCKED couldn't lock the file
539 int mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
542 char buffer[LONG_STRING];
546 if (stat (ctx->path, &st) == 0) {
547 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
550 if (st.st_size == ctx->size) {
551 /* the file was touched, but it is still the same length, so just exit */
552 ctx->mtime = st.st_mtime;
556 if (st.st_size > ctx->size) {
557 /* lock the file if it isn't already */
559 mutt_block_signals ();
560 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
561 mutt_unblock_signals ();
562 /* we couldn't lock the mailbox, but nothing serious happened:
563 * probably the new mail arrived: no reason to wait till we can
564 * parse it: we'll get it on the next pass
572 * Check to make sure that the only change to the mailbox is that
573 * message(s) were appended to this file. My heuristic is that we should
574 * see the message separator at *exactly* what used to be the end of the
577 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
578 debug_print (1, ("fseek() failed\n"));
579 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
580 if ((ctx->magic == M_MBOX && safe_strncmp ("From ", buffer, 5) == 0)
581 || (ctx->magic == M_MMDF && safe_strcmp (MMDF_SEP, buffer) == 0)) {
582 if (fseek (ctx->fp, ctx->size, SEEK_SET) != 0)
583 debug_print (1, ("fseek() failed\n"));
584 if (ctx->magic == M_MBOX)
585 mbox_parse_mailbox (ctx);
587 mmdf_parse_mailbox (ctx);
589 /* Only unlock the folder if it was locked inside of this routine.
590 * It may have been locked elsewhere, like in
591 * mutt_checkpoint_mailbox().
595 mbox_unlock_mailbox (ctx);
596 mutt_unblock_signals ();
599 return (M_NEW_MAIL); /* signal that new mail arrived */
605 debug_print (1, ("fgets returned NULL.\n"));
614 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
616 mbox_unlock_mailbox (ctx);
617 mutt_unblock_signals ();
625 mbox_unlock_mailbox (ctx);
626 mx_fastclose_mailbox (ctx);
627 mutt_unblock_signals ();
628 mutt_error _("Mailbox was corrupted!");
637 static int _mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint)
639 char tempfile[_POSIX_PATH_MAX];
641 int i, j, save_sort = SORT_ORDER;
643 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
644 int first = -1; /* first message to be written */
645 long offset; /* location in mailbox to write changed messages */
647 struct utimbuf utimebuf;
648 struct m_update_t *newOffset = NULL;
649 struct m_update_t *oldOffset = NULL;
652 /* sort message by their position in the mailbox on disk */
653 if (Sort != SORT_ORDER) {
656 mutt_sort_headers (ctx, 0);
659 /* need to open the file for writing in such a way that it does not truncate
660 * the file, so use read-write mode.
662 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
663 mx_fastclose_mailbox (ctx);
664 mutt_error _("Fatal error! Could not reopen mailbox!");
669 mutt_block_signals ();
671 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
672 mutt_unblock_signals ();
673 mutt_error _("Unable to lock mailbox!");
678 /* Check to make sure that the file hasn't changed on disk */
679 if ((i = mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
680 || i == M_REOPENED) {
681 /* new mail arrived, or mailbox reopened */
692 /* Create a temporary file to write the new version of the mailbox in. */
693 mutt_mktemp (tempfile);
694 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
695 (fp = fdopen (i, "w")) == NULL) {
700 mutt_error _("Could not create temporary file!");
706 /* find the first deleted/changed message. we save a lot of time by only
707 * rewriting the mailbox from the point where it has actually changed.
709 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
710 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
711 if (i == ctx->msgcount) {
712 /* this means ctx->changed or ctx->deleted was set, but no
713 * messages were found to be changed or deleted. This should
714 * never happen, is we presume it is a bug in mutt.
717 _("sync: mbox modified, but no modified messages! (report this bug)");
718 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
719 debug_print (1, ("no modified messages.\n"));
724 /* save the index of the first changed/deleted message */
726 /* where to start overwriting */
727 offset = ctx->hdrs[i]->offset;
729 /* the offset stored in the header does not include the MMDF_SEP, so make
730 * sure we seek to the correct location
732 if (ctx->magic == M_MMDF)
733 offset -= (sizeof MMDF_SEP - 1);
735 /* allocate space for the new offsets */
736 newOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
737 oldOffset = safe_calloc (ctx->msgcount - first, sizeof (struct m_update_t));
739 for (i = first, j = 0; i < ctx->msgcount; i++) {
741 * back up some information which is needed to restore offsets when
745 oldOffset[i - first].valid = 1;
746 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
747 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
748 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
749 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
751 if (!ctx->hdrs[i]->deleted) {
753 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
754 mutt_message (_("Writing messages... %d (%d%%)"), i,
755 (int) (ftell (ctx->fp) / (ctx->size / 100 + 1)));
757 if (ctx->magic == M_MMDF) {
758 if (fputs (MMDF_SEP, fp) == EOF) {
759 mutt_perror (tempfile);
767 /* save the new offset for this message. we add `offset' because the
768 * temporary file only contains saved message which are located after
769 * `offset' in the real mailbox
771 newOffset[i - first].hdr = ftell (fp) + offset;
773 if (mutt_copy_message
774 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
775 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
776 mutt_perror (tempfile);
782 /* Since messages could have been deleted, the offsets stored in memory
783 * will be wrong, so update what we can, which is the offset of this
784 * message, and the offset of the body. If this is a multipart message,
785 * we just flush the in memory cache so that the message will be reparsed
786 * if the user accesses it later.
788 newOffset[i - first].body =
789 ftell (fp) - ctx->hdrs[i]->content->length + offset;
790 mutt_free_body (&ctx->hdrs[i]->content->parts);
792 switch (ctx->magic) {
794 if (fputs (MMDF_SEP, fp) == EOF) {
795 mutt_perror (tempfile);
802 if (fputs ("\n", fp) == EOF) {
803 mutt_perror (tempfile);
812 if (fclose (fp) != 0) {
814 debug_print (1, ("fclose() returned non-zero.\n"));
816 mutt_perror (tempfile);
822 /* Save the state of this folder. */
823 if (stat (ctx->path, &statbuf) == -1) {
824 mutt_perror (ctx->path);
830 if ((fp = fopen (tempfile, "r")) == NULL) {
831 mutt_unblock_signals ();
832 mx_fastclose_mailbox (ctx);
833 debug_print (1, ("unable to reopen temp copy of mailbox!\n"));
834 mutt_perror (tempfile);
839 if (fseek (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
840 /* do a sanity check to make sure the mailbox looks ok */
841 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
842 (ctx->magic == M_MBOX && safe_strncmp ("From ", buf, 5) != 0) ||
843 (ctx->magic == M_MMDF && safe_strcmp (MMDF_SEP, buf) != 0)) {
844 debug_print (1, ("message not in expected position.\n"));
845 debug_print (1, ("LINE: %s\n", buf));
849 if (fseek (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
851 debug_print (1, ("fseek() failed\n"));
854 /* copy the temp mailbox back into place starting at the first
855 * change/deleted message
857 mutt_message _("Committing changes...");
859 i = mutt_copy_stream (fp, ctx->fp);
861 if (ferror (ctx->fp))
865 ctx->size = ftell (ctx->fp); /* update the size of the mailbox */
866 ftruncate (fileno (ctx->fp), ctx->size);
872 mbox_unlock_mailbox (ctx);
874 if (fclose (ctx->fp) != 0 || i == -1) {
875 /* error occured while writing the mailbox back, so keep the temp copy
879 char savefile[_POSIX_PATH_MAX];
881 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
882 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
883 (unsigned int) getpid ());
884 rename (tempfile, savefile);
885 mutt_unblock_signals ();
886 mx_fastclose_mailbox (ctx);
887 mutt_pretty_mailbox (savefile);
888 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
893 /* Restore the previous access/modification times */
894 utimebuf.actime = statbuf.st_atime;
895 utimebuf.modtime = statbuf.st_mtime;
896 utime (ctx->path, &utimebuf);
898 /* reopen the mailbox in read-only mode */
899 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
901 mutt_unblock_signals ();
902 mx_fastclose_mailbox (ctx);
903 mutt_error _("Fatal error! Could not reopen mailbox!");
909 /* update the offsets of the rewritten messages */
910 for (i = first, j = first; i < ctx->msgcount; i++) {
911 if (!ctx->hdrs[i]->deleted) {
912 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
913 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
914 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
915 ctx->hdrs[i]->index = j++;
920 unlink (tempfile); /* remove partial copy of the mailbox */
921 mutt_unblock_signals ();
922 Sort = save_sort; /* Restore the default value. */
924 return (0); /* signal success */
926 bail: /* Come here in case of disaster */
930 /* restore offsets, as far as they are valid */
931 if (first >= 0 && oldOffset) {
932 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
933 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
934 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
935 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
936 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
937 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
941 /* this is ok to call even if we haven't locked anything */
942 mbox_unlock_mailbox (ctx);
944 mutt_unblock_signals ();
948 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
949 mutt_error _("Could not reopen mailbox!");
951 mx_fastclose_mailbox (ctx);
955 if (need_sort || save_sort != Sort) {
957 /* if the mailbox was reopened, the thread tree will be invalid so make
958 * sure to start threading from scratch. */
959 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
965 static int mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint) {
969 int rc = _mbox_sync_mailbox (ctx, unused, index_hint);
972 if ((tmp = buffy_find_mailbox (ctx->path)) && tmp->new == 0)
973 buffy_update_mailbox (tmp);
978 /* close a mailbox opened in write-mode */
979 int mbox_close_mailbox (CONTEXT * ctx)
981 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
983 #ifdef USE_COMPRESSED
984 if (ctx->compressinfo)
985 mutt_slow_close_compressed (ctx);
988 mutt_unblock_signals ();
989 mx_fastclose_mailbox (ctx);
993 static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
995 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
1003 /* silent operations */
1006 mutt_message _("Reopening mailbox...");
1008 /* our heuristics require the old mailbox to be unsorted */
1009 if (Sort != SORT_ORDER) {
1014 mutt_sort_headers (ctx, 1);
1021 /* simulate a close */
1023 hash_destroy (&ctx->id_hash, NULL);
1025 hash_destroy (&ctx->subj_hash, NULL);
1026 mutt_clear_threads (ctx);
1028 if (ctx->readonly) {
1029 for (i = 0; i < ctx->msgcount; i++)
1030 mutt_free_header (&(ctx->hdrs[i])); /* nothing to do! */
1034 /* save the old headers */
1035 old_msgcount = ctx->msgcount;
1036 old_hdrs = ctx->hdrs;
1040 ctx->hdrmax = 0; /* force allocation of new headers */
1049 ctx->id_hash = NULL;
1050 ctx->subj_hash = NULL;
1052 switch (ctx->magic) {
1055 if (fseek (ctx->fp, 0, SEEK_SET) != 0) {
1056 debug_print (1, ("fseek() failed\n"));
1060 cmp_headers = mbox_strict_cmp_headers;
1061 if (ctx->magic == M_MBOX)
1062 rc = mbox_parse_mailbox (ctx);
1064 rc = mmdf_parse_mailbox (ctx);
1074 /* free the old headers */
1075 for (j = 0; j < old_msgcount; j++)
1076 mutt_free_header (&(old_hdrs[j]));
1083 /* now try to recover the old flags */
1085 index_hint_set = (index_hint == NULL);
1087 if (!ctx->readonly) {
1088 for (i = 0; i < ctx->msgcount; i++) {
1091 /* some messages have been deleted, and new messages have been
1092 * appended at the end; the heuristic is that old messages have then
1093 * "advanced" towards the beginning of the folder, so we begin the
1094 * search at index "i"
1096 for (j = i; j < old_msgcount; j++) {
1097 if (old_hdrs[j] == NULL)
1099 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1105 for (j = 0; j < i && j < old_msgcount; j++) {
1106 if (old_hdrs[j] == NULL)
1108 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
1116 /* this is best done here */
1117 if (!index_hint_set && *index_hint == j)
1120 if (old_hdrs[j]->changed) {
1121 /* Only update the flags if the old header was changed;
1122 * otherwise, the header may have been modified externally,
1123 * and we don't want to lose _those_ changes
1125 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
1126 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
1127 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
1128 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
1130 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
1131 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
1133 /* we don't need this header any more */
1134 mutt_free_header (&(old_hdrs[j]));
1138 /* free the remaining old headers */
1139 for (j = 0; j < old_msgcount; j++) {
1141 mutt_free_header (&(old_hdrs[j]));
1150 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1155 * 1 if the mailbox is not empty
1156 * 0 if the mailbox is empty
1159 int mbox_check_empty (const char *path)
1163 if (stat (path, &st) == -1)
1166 return ((st.st_size == 0));
1169 int mbox_is_magic (const char* path, struct stat* st) {
1172 char tmp[_POSIX_PATH_MAX];
1174 if (S_ISDIR(st->st_mode))
1177 if (st->st_size == 0) {
1178 /* hard to tell what zero-length files are, so assume the default magic */
1179 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
1180 return (DefaultMagic);
1184 else if ((f = fopen (path, "r")) != NULL) {
1186 struct utimbuf times;
1188 fgets (tmp, sizeof (tmp), f);
1189 if (safe_strncmp ("From ", tmp, 5) == 0)
1191 else if (safe_strcmp (MMDF_SEP, tmp) == 0)
1195 /* need to restore the times here, the file was not really accessed,
1196 * only the type was accessed. This is important, because detection
1197 * of "new mail" depends on those times set correctly.
1199 times.actime = st->st_atime;
1200 times.modtime = st->st_mtime;
1201 utime (path, ×);
1205 return (-1); /* fopen failed */
1208 #ifdef USE_COMPRESSED
1209 if (magic == -1 && mutt_can_read_compressed (path))
1210 return (M_COMPRESSED);
1215 static mx_t* reg_mx (void) {
1216 mx_t* fmt = safe_calloc (1, sizeof (mx_t));
1218 fmt->mx_check_empty = mbox_check_empty;
1219 fmt->mx_is_magic = mbox_is_magic;
1220 fmt->mx_access = access;
1221 fmt->mx_open_mailbox = mbox_open_mailbox;
1222 fmt->mx_open_new_message = mbox_open_new_message;
1223 fmt->mx_sync_mailbox = mbox_sync_mailbox;
1227 mx_t* mbox_reg_mx (void) {
1228 mx_t* fmt = reg_mx ();
1232 mx_t* mmdf_reg_mx (void) {
1233 mx_t* fmt = reg_mx ();