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 */
25 #include <lib-lib/lib-lib.h>
26 #include <lib-ui/curses.h>
28 #include <lib-sys/mutt_signal.h>
39 /* struct used by mutt_sync_mailbox() to store new offsets */
49 static int mbox_open_new_message (MESSAGE * msg, CONTEXT * dest, HEADER * hdr __attribute__ ((unused)))
56 static int mbox_reopen_mailbox (CONTEXT*, int*);
59 * ctx - context to lock
60 * excl - exclusive lock?
61 * retry - should retry if unable to lock?
63 int mbox_lock_mailbox (CONTEXT * ctx, int excl, int retry)
67 if ((r = mx_lock_file (ctx->path, fileno (ctx->fp), excl, 1, retry)) == 0)
69 else if (retry && !excl) {
77 void mbox_unlock_mailbox (CONTEXT * ctx)
82 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
87 static int mmdf_parse_mailbox (CONTEXT * ctx)
89 char buf[HUGE_STRING];
90 char return_path[LONG_STRING];
91 int count = 0, oldmsgcount = ctx->msgcount;
98 if (stat (ctx->path, &sb) == -1) {
99 mutt_perror (ctx->path);
102 ctx->mtime = sb.st_mtime;
103 ctx->size = sb.st_size;
105 /* precompute the local timezone to speed up calculation of the
107 tz = mutt_local_tz (0);
109 buf[sizeof (buf) - 1] = 0;
112 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
115 if (m_strcmp(buf, MMDF_SEP) == 0) {
116 loc = ftello (ctx->fp);
119 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
120 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
121 (int) (loc / (ctx->size / 100 + 1)));
124 if (ctx->msgcount == ctx->hdrmax)
125 mx_alloc_memory (ctx);
126 ctx->hdrs[ctx->msgcount] = hdr = header_new();
128 hdr->index = ctx->msgcount;
130 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL) {
131 /* TODO: memory leak??? */
137 if (!is_from (buf, return_path, sizeof (return_path), &t)) {
138 if (fseeko (ctx->fp, loc, SEEK_SET) != 0) {
139 mutt_error _("Mailbox is corrupt!");
145 hdr->received = t - tz;
147 hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0);
149 loc = ftello (ctx->fp);
151 if (hdr->content->length > 0 && hdr->lines > 0) {
152 tmploc = loc + hdr->content->length;
154 if (0 < tmploc && tmploc < ctx->size) {
155 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
156 fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL ||
157 m_strcmp(MMDF_SEP, buf) != 0) {
158 fseeko (ctx->fp, loc, SEEK_SET);
159 hdr->content->length = -1;
163 hdr->content->length = -1;
166 hdr->content->length = -1;
168 if (hdr->content->length < 0) {
171 loc = ftello (ctx->fp);
172 if (fgets (buf, sizeof (buf) - 1, ctx->fp) == NULL)
175 } while (m_strcmp(buf, MMDF_SEP) != 0);
178 hdr->content->length = loc - hdr->content->offset;
181 if (!hdr->env->return_path && return_path[0])
182 hdr->env->return_path =
183 rfc822_parse_adrlist (hdr->env->return_path, return_path);
186 hdr->env->from = address_list_dup (hdr->env->return_path);
191 mutt_error _("Mailbox is corrupt!");
197 if (ctx->msgcount > oldmsgcount)
198 mx_update_context (ctx, ctx->msgcount - oldmsgcount);
203 /* Note that this function is also called when new mail is appended to the
204 * currently open folder, and NOT just when the mailbox is initially read.
206 * NOTE: it is assumed that the mailbox being read has been locked before
207 * this routine gets called. Strange things could happen if it's not!
209 static int mbox_parse_mailbox (CONTEXT * ctx)
212 char buf[HUGE_STRING], return_path[STRING];
215 int count = 0, lines = 0;
218 /* Save information about the folder at the time we opened it. */
219 if (stat (ctx->path, &sb) == -1) {
220 mutt_perror (ctx->path);
224 ctx->size = sb.st_size;
225 ctx->mtime = sb.st_mtime;
228 ctx->readonly = access (ctx->path, W_OK) ? 1 : 0;
230 /* precompute the local timezone to speed up calculation of the
232 tz = mutt_local_tz (0);
234 loc = ftello (ctx->fp);
235 while (fgets (buf, sizeof (buf), ctx->fp) != NULL) {
236 if (is_from (buf, return_path, sizeof (return_path), &t)) {
237 /* Save the Content-Length of the previous message */
239 #define PREV ctx->hdrs[ctx->msgcount-1]
241 if (PREV->content->length < 0) {
242 PREV->content->length = loc - PREV->content->offset - 1;
243 if (PREV->content->length < 0)
244 PREV->content->length = 0;
247 PREV->lines = lines ? lines - 1 : 0;
252 if (!ctx->quiet && ReadInc && ((count % ReadInc == 0) || count == 1))
253 mutt_message (_("Reading %s... %d (%d%%)"), ctx->path, count,
254 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
256 if (ctx->msgcount == ctx->hdrmax)
257 mx_alloc_memory (ctx);
259 curhdr = ctx->hdrs[ctx->msgcount] = header_new();
260 curhdr->received = t - tz;
261 curhdr->offset = loc;
262 curhdr->index = ctx->msgcount;
264 curhdr->env = mutt_read_rfc822_header (ctx->fp, curhdr, 0, 0);
266 /* if we know how long this message is, either just skip over the body,
267 * or if we don't know how many lines there are, count them now (this will
268 * save time by not having to search for the next message marker).
270 if (curhdr->content->length > 0) {
273 loc = ftello (ctx->fp);
274 tmploc = loc + curhdr->content->length + 1;
276 if (0 < tmploc && tmploc < ctx->size) {
278 * check to see if the content-length looks valid. we expect to
279 * to see a valid message separator at this point in the stream
281 if (fseeko (ctx->fp, tmploc, SEEK_SET) != 0 ||
282 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
283 m_strncmp("From ", buf, 5) != 0) {
284 fseeko (ctx->fp, loc, SEEK_SET); /* nope, return the previous position */
285 curhdr->content->length = -1;
288 else if (tmploc != ctx->size) {
289 /* content-length would put us past the end of the file, so it
292 curhdr->content->length = -1;
295 if (curhdr->content->length != -1) {
296 /* good content-length. check to see if we know how many lines
297 * are in this message.
299 if (curhdr->lines == 0) {
300 int cl = curhdr->content->length;
302 /* count the number of lines in this message */
303 fseeko (ctx->fp, loc, SEEK_SET);
305 if (fgetc (ctx->fp) == '\n')
310 /* return to the offset of the next message separator */
311 fseeko(ctx->fp, tmploc, SEEK_SET);
317 if (!curhdr->env->return_path && return_path[0])
318 curhdr->env->return_path =
319 rfc822_parse_adrlist (curhdr->env->return_path, return_path);
321 if (!curhdr->env->from)
322 curhdr->env->from = address_list_dup (curhdr->env->return_path);
329 loc = ftello (ctx->fp);
333 * Only set the content-length of the previous message if we have read more
334 * than one message during _this_ invocation. If this routine is called
335 * when new mail is received, we need to make sure not to clobber what
336 * previously was the last message since the headers may be sorted.
339 if (PREV->content->length < 0) {
340 PREV->content->length = ftello (ctx->fp) - PREV->content->offset - 1;
341 if (PREV->content->length < 0)
342 PREV->content->length = 0;
346 PREV->lines = lines ? lines - 1 : 0;
348 mx_update_context (ctx, count);
356 /* open a mbox or mmdf style mailbox */
357 static int mbox_open_mailbox (CONTEXT * ctx)
361 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
362 mutt_perror (ctx->path);
365 mutt_block_signals ();
366 if (mbox_lock_mailbox (ctx, 0, 1) == -1) {
367 mutt_unblock_signals ();
371 if (ctx->magic == M_MBOX)
372 rc = mbox_parse_mailbox (ctx);
373 else if (ctx->magic == M_MMDF)
374 rc = mmdf_parse_mailbox (ctx);
378 mbox_unlock_mailbox (ctx);
379 mutt_unblock_signals ();
383 /* check to see if the mailbox has changed on disk.
386 * M_REOPENED mailbox has been reopened
387 * M_NEW_MAIL new mail has arrived!
388 * M_LOCKED couldn't lock the file
392 static int _mbox_check_mailbox (CONTEXT * ctx, int *index_hint)
395 char buffer[LONG_STRING];
399 if (stat (ctx->path, &st) == 0) {
400 if (st.st_mtime == ctx->mtime && st.st_size == ctx->size)
403 if (st.st_size == ctx->size) {
404 /* the file was touched, but it is still the same length, so just exit */
405 ctx->mtime = st.st_mtime;
409 if (st.st_size > ctx->size) {
410 /* lock the file if it isn't already */
412 mutt_block_signals ();
413 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
414 mutt_unblock_signals ();
415 /* we couldn't lock the mailbox, but nothing serious happened:
416 * probably the new mail arrived: no reason to wait till we can
417 * parse it: we'll get it on the next pass
425 * Check to make sure that the only change to the mailbox is that
426 * message(s) were appended to this file. My heuristic is that we should
427 * see the message separator at *exactly* what used to be the end of the
430 fseeko (ctx->fp, ctx->size, SEEK_SET);
431 if (fgets (buffer, sizeof (buffer), ctx->fp) != NULL) {
432 if ((ctx->magic == M_MBOX && m_strncmp("From ", buffer, 5) == 0)
433 || (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buffer) == 0)) {
434 fseeko (ctx->fp, ctx->size, SEEK_SET);
435 if (ctx->magic == M_MBOX)
436 mbox_parse_mailbox (ctx);
438 mmdf_parse_mailbox (ctx);
440 /* Only unlock the folder if it was locked inside of this routine.
441 * It may have been locked elsewhere, like in
442 * mutt_checkpoint_mailbox().
446 mbox_unlock_mailbox (ctx);
447 mutt_unblock_signals ();
450 return (M_NEW_MAIL); /* signal that new mail arrived */
463 if (mbox_reopen_mailbox (ctx, index_hint) != -1) {
465 mbox_unlock_mailbox (ctx);
466 mutt_unblock_signals ();
474 mbox_unlock_mailbox (ctx);
475 mx_fastclose_mailbox (ctx);
476 mutt_unblock_signals ();
477 mutt_error _("Mailbox was corrupted!");
482 static int mbox_check_mailbox (CONTEXT* ctx, int* index_hint, int lock) {
486 mutt_block_signals ();
487 if (mbox_lock_mailbox (ctx, 0, 0) == -1) {
488 mutt_unblock_signals ();
493 rc = _mbox_check_mailbox (ctx, index_hint);
496 mutt_unblock_signals ();
497 mbox_unlock_mailbox (ctx);
506 static int _mbox_sync_mailbox (CONTEXT * ctx, int unused __attribute__ ((unused)), int *index_hint)
508 char tempfile[_POSIX_PATH_MAX];
510 int i, j, save_sort = SORT_ORDER;
512 int need_sort = 0; /* flag to resort mailbox if new mail arrives */
513 int first = -1; /* first message to be written */
514 off_t offset; /* location in mailbox to write changed messages */
516 struct utimbuf utimebuf;
517 struct m_update_t *newOffset = NULL;
518 struct m_update_t *oldOffset = NULL;
521 /* sort message by their position in the mailbox on disk */
522 if (Sort != SORT_ORDER) {
525 mutt_sort_headers (ctx, 0);
530 /* need to open the file for writing in such a way that it does not truncate
531 * the file, so use read-write mode.
533 if ((ctx->fp = freopen (ctx->path, "r+", ctx->fp)) == NULL) {
534 mx_fastclose_mailbox (ctx);
535 mutt_error _("Fatal error! Could not reopen mailbox!");
540 mutt_block_signals ();
542 if (mbox_lock_mailbox (ctx, 1, 1) == -1) {
543 mutt_unblock_signals ();
544 mutt_error _("Unable to lock mailbox!");
549 /* Check to make sure that the file hasn't changed on disk */
550 if ((i = _mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL
551 || i == M_REOPENED) {
552 /* new mail arrived, or mailbox reopened */
561 /* Create a temporary file to write the new version of the mailbox in. */
562 mutt_mktemp (tempfile);
563 if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 ||
564 (fp = fdopen (i, "w")) == NULL) {
569 mutt_error _("Could not create temporary file!");
575 /* find the first deleted/changed message. we save a lot of time by only
576 * rewriting the mailbox from the point where it has actually changed.
578 for (i = 0; i < ctx->msgcount && !ctx->hdrs[i]->deleted &&
579 !ctx->hdrs[i]->changed && !ctx->hdrs[i]->attach_del; i++);
580 if (i == ctx->msgcount) {
581 /* this means ctx->changed or ctx->deleted was set, but no
582 * messages were found to be changed or deleted. This should
583 * never happen, is we presume it is a bug in mutt.
586 _("sync: mbox modified, but no modified messages! (report this bug)");
587 mutt_sleep (5); /* the mutt_error /will/ get cleared! */
592 /* save the index of the first changed/deleted message */
594 /* where to start overwriting */
595 offset = ctx->hdrs[i]->offset;
597 /* the offset stored in the header does not include the MMDF_SEP, so make
598 * sure we seek to the correct location
600 if (ctx->magic == M_MMDF)
601 offset -= (sizeof MMDF_SEP - 1);
603 /* allocate space for the new offsets */
604 newOffset = p_new(struct m_update_t, ctx->msgcount - first);
605 oldOffset = p_new(struct m_update_t, ctx->msgcount - first);
607 for (i = first, j = 0; i < ctx->msgcount; i++) {
609 * back up some information which is needed to restore offsets when
613 oldOffset[i - first].valid = 1;
614 oldOffset[i - first].hdr = ctx->hdrs[i]->offset;
615 oldOffset[i - first].body = ctx->hdrs[i]->content->offset;
616 oldOffset[i - first].lines = ctx->hdrs[i]->lines;
617 oldOffset[i - first].length = ctx->hdrs[i]->content->length;
619 if (!ctx->hdrs[i]->deleted) {
621 if (!ctx->quiet && WriteInc && ((i % WriteInc) == 0 || j == 1))
622 mutt_message (_("Writing messages... %d (%d%%)"), i,
623 (int) (ftello (ctx->fp) / (ctx->size / 100 + 1)));
625 if (ctx->magic == M_MMDF) {
626 if (fputs (MMDF_SEP, fp) == EOF) {
627 mutt_perror (tempfile);
635 /* save the new offset for this message. we add `offset' because the
636 * temporary file only contains saved message which are located after
637 * `offset' in the real mailbox
639 newOffset[i - first].hdr = ftello (fp) + offset;
641 if (mutt_copy_message
642 (fp, ctx, ctx->hdrs[i], M_CM_UPDATE,
643 CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) {
644 mutt_perror (tempfile);
650 /* Since messages could have been deleted, the offsets stored in memory
651 * will be wrong, so update what we can, which is the offset of this
652 * message, and the offset of the body. If this is a multipart message,
653 * we just flush the in memory cache so that the message will be reparsed
654 * if the user accesses it later.
656 newOffset[i - first].body =
657 ftello (fp) - ctx->hdrs[i]->content->length + offset;
658 body_list_wipe(&ctx->hdrs[i]->content->parts);
660 switch (ctx->magic) {
662 if (fputs (MMDF_SEP, fp) == EOF) {
663 mutt_perror (tempfile);
670 if (fputs ("\n", fp) == EOF) {
671 mutt_perror (tempfile);
680 if (fclose (fp) != 0) {
683 mutt_perror (tempfile);
689 /* Save the state of this folder. */
690 if (stat (ctx->path, &statbuf) == -1) {
691 mutt_perror (ctx->path);
697 if ((fp = fopen (tempfile, "r")) == NULL) {
698 mutt_unblock_signals ();
699 mx_fastclose_mailbox (ctx);
700 mutt_perror (tempfile);
705 if (fseeko (ctx->fp, offset, SEEK_SET) != 0 || /* seek the append location */
706 /* do a sanity check to make sure the mailbox looks ok */
707 fgets (buf, sizeof (buf), ctx->fp) == NULL ||
708 (ctx->magic == M_MBOX && m_strncmp("From ", buf, 5) != 0) ||
709 (ctx->magic == M_MMDF && m_strcmp(MMDF_SEP, buf) != 0)) {
713 if (fseeko (ctx->fp, offset, SEEK_SET) != 0) { /* return to proper offset */
716 /* copy the temp mailbox back into place starting at the first
717 * change/deleted message
719 mutt_message _("Committing changes...");
721 i = mutt_copy_stream (fp, ctx->fp);
723 if (ferror (ctx->fp))
727 ctx->size = ftello (ctx->fp); /* update the size of the mailbox */
728 ftruncate (fileno (ctx->fp), ctx->size);
734 mbox_unlock_mailbox (ctx);
736 if (fclose (ctx->fp) != 0 || i == -1) {
737 /* error occured while writing the mailbox back, so keep the temp copy
741 char savefile[_POSIX_PATH_MAX];
743 snprintf (savefile, sizeof (savefile), "%s/mutt.%s-%s-%u",
744 NONULL (Tempdir), NONULL (Username), NONULL (Hostname),
745 (unsigned int) getpid ());
746 rename (tempfile, savefile);
747 mutt_unblock_signals ();
748 mx_fastclose_mailbox (ctx);
749 mutt_pretty_mailbox (savefile);
750 mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile);
755 /* Restore the previous access/modification times */
756 utimebuf.actime = statbuf.st_atime;
757 utimebuf.modtime = statbuf.st_mtime;
758 utime (ctx->path, &utimebuf);
760 /* reopen the mailbox in read-only mode */
761 if ((ctx->fp = fopen (ctx->path, "r")) == NULL) {
763 mutt_unblock_signals ();
764 mx_fastclose_mailbox (ctx);
765 mutt_error _("Fatal error! Could not reopen mailbox!");
769 /* update the offsets of the rewritten messages */
770 for (i = first, j = first; i < ctx->msgcount; i++) {
771 if (!ctx->hdrs[i]->deleted) {
772 ctx->hdrs[i]->offset = newOffset[i - first].hdr;
773 ctx->hdrs[i]->content->hdr_offset = newOffset[i - first].hdr;
774 ctx->hdrs[i]->content->offset = newOffset[i - first].body;
775 ctx->hdrs[i]->index = j++;
778 p_delete(&newOffset);
779 p_delete(&oldOffset);
780 unlink (tempfile); /* remove partial copy of the mailbox */
781 mutt_unblock_signals ();
783 return (0); /* signal success */
785 bail: /* Come here in case of disaster */
789 /* restore offsets, as far as they are valid */
790 if (first >= 0 && oldOffset) {
791 for (i = first; i < ctx->msgcount && oldOffset[i - first].valid; i++) {
792 ctx->hdrs[i]->offset = oldOffset[i - first].hdr;
793 ctx->hdrs[i]->content->hdr_offset = oldOffset[i - first].hdr;
794 ctx->hdrs[i]->content->offset = oldOffset[i - first].body;
795 ctx->hdrs[i]->lines = oldOffset[i - first].lines;
796 ctx->hdrs[i]->content->length = oldOffset[i - first].length;
800 /* this is ok to call even if we haven't locked anything */
801 mbox_unlock_mailbox (ctx);
803 mutt_unblock_signals ();
804 p_delete(&newOffset);
805 p_delete(&oldOffset);
807 if ((ctx->fp = freopen (ctx->path, "r", ctx->fp)) == NULL) {
808 mutt_error _("Could not reopen mailbox!");
810 mx_fastclose_mailbox (ctx);
815 /* if the mailbox was reopened, the thread tree will be invalid so make
816 * sure to start threading from scratch. */
817 mutt_sort_headers (ctx, (need_sort == M_REOPENED));
822 static int mbox_sync_mailbox (CONTEXT * ctx, int unused, int *index_hint) {
826 int rc = _mbox_sync_mailbox (ctx, unused, index_hint);
829 if ((tmp = buffy_find_mailbox (ctx->path)) && tmp->new == 0)
830 buffy_update_mailbox (tmp);
835 /* close a mailbox opened in write-mode */
836 int mbox_close_mailbox (CONTEXT * ctx)
838 mx_unlock_file (ctx->path, fileno (ctx->fp), 1);
840 if (ctx->compressinfo)
841 mutt_slow_close_compressed (ctx);
843 mutt_unblock_signals ();
844 mx_fastclose_mailbox (ctx);
848 static int mbox_reopen_mailbox (CONTEXT * ctx, int *index_hint)
850 int (*cmp_headers) (const HEADER *, const HEADER *) = NULL;
858 /* silent operations */
861 mutt_message _("Reopening mailbox...");
863 /* our heuristics require the old mailbox to be unsorted */
864 if (Sort != SORT_ORDER) {
869 mutt_sort_headers (ctx, 1);
876 /* simulate a close */
878 hash_destroy (&ctx->id_hash, NULL);
880 hash_destroy (&ctx->subj_hash, NULL);
881 mutt_clear_threads (ctx);
884 for (i = 0; i < ctx->msgcount; i++)
885 header_delete(&(ctx->hdrs[i])); /* nothing to do! */
886 p_delete(&ctx->hdrs);
889 /* save the old headers */
890 old_msgcount = ctx->msgcount;
891 old_hdrs = ctx->hdrs;
895 ctx->hdrmax = 0; /* force allocation of new headers */
905 ctx->subj_hash = NULL;
907 switch (ctx->magic) {
910 if (fseeko (ctx->fp, 0, SEEK_SET) != 0) {
914 cmp_headers = mutt_cmp_header;
915 if (ctx->magic == M_MBOX)
916 rc = mbox_parse_mailbox (ctx);
918 rc = mmdf_parse_mailbox (ctx);
928 /* free the old headers */
929 for (j = 0; j < old_msgcount; j++)
930 header_delete(&(old_hdrs[j]));
937 /* now try to recover the old flags */
939 index_hint_set = (index_hint == NULL);
941 if (!ctx->readonly) {
942 for (i = 0; i < ctx->msgcount; i++) {
945 /* some messages have been deleted, and new messages have been
946 * appended at the end; the heuristic is that old messages have then
947 * "advanced" towards the beginning of the folder, so we begin the
948 * search at index "i"
950 for (j = i; j < old_msgcount; j++) {
951 if (old_hdrs[j] == NULL)
953 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
959 for (j = 0; j < i && j < old_msgcount; j++) {
960 if (old_hdrs[j] == NULL)
962 if (cmp_headers (ctx->hdrs[i], old_hdrs[j])) {
970 /* this is best done here */
971 if (!index_hint_set && *index_hint == j)
974 if (old_hdrs[j]->changed) {
975 /* Only update the flags if the old header was changed;
976 * otherwise, the header may have been modified externally,
977 * and we don't want to lose _those_ changes
979 mutt_set_flag (ctx, ctx->hdrs[i], M_FLAG, old_hdrs[j]->flagged);
980 mutt_set_flag (ctx, ctx->hdrs[i], M_REPLIED, old_hdrs[j]->replied);
981 mutt_set_flag (ctx, ctx->hdrs[i], M_OLD, old_hdrs[j]->old);
982 mutt_set_flag (ctx, ctx->hdrs[i], M_READ, old_hdrs[j]->read);
984 mutt_set_flag (ctx, ctx->hdrs[i], M_DELETE, old_hdrs[j]->deleted);
985 mutt_set_flag (ctx, ctx->hdrs[i], M_TAG, old_hdrs[j]->tagged);
987 /* we don't need this header any more */
988 header_delete(&(old_hdrs[j]));
992 /* free the remaining old headers */
993 for (j = 0; j < old_msgcount; j++) {
995 header_delete(&(old_hdrs[j]));
1004 return ((ctx->changed || msg_mod) ? M_REOPENED : M_NEW_MAIL);
1009 * 1 if the mailbox is not empty
1010 * 0 if the mailbox is empty
1013 int mbox_check_empty (const char *path)
1017 if (stat (path, &st) == -1)
1020 return ((st.st_size == 0));
1023 int mbox_is_magic (const char* path, struct stat* st) {
1026 char tmp[_POSIX_PATH_MAX];
1028 if (S_ISDIR(st->st_mode))
1031 if (st->st_size == 0) {
1032 /* hard to tell what zero-length files are, so assume the default magic */
1033 if (DefaultMagic == M_MBOX || DefaultMagic == M_MMDF)
1034 return (DefaultMagic);
1038 else if ((f = fopen (path, "r")) != NULL) {
1040 struct utimbuf times;
1042 fgets (tmp, sizeof (tmp), f);
1043 if (m_strncmp("From ", tmp, 5) == 0)
1045 else if (m_strcmp(MMDF_SEP, tmp) == 0)
1049 /* need to restore the times here, the file was not really accessed,
1050 * only the type was accessed. This is important, because detection
1051 * of "new mail" depends on those times set correctly.
1053 times.actime = st->st_atime;
1054 times.modtime = st->st_mtime;
1055 utime (path, ×);
1059 return (-1); /* fopen failed */
1062 if (magic == -1 && mutt_can_read_compressed (path))
1063 return (M_COMPRESSED);
1067 static int commit_message (MESSAGE* msg, CONTEXT* ctx __attribute__ ((unused)), int mbox) {
1068 if ((mbox && fputc ('\n', msg->fp) == EOF) ||
1069 (!mbox && fputs (MMDF_SEP, msg->fp) == EOF))
1071 if ((fflush (msg->fp) == EOF || fsync (fileno (msg->fp)) == -1)) {
1072 mutt_perror (_("Can't write message"));
1078 static int mbox_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1079 return (commit_message (msg, ctx, 1));
1082 static int mmdf_commit_message (MESSAGE* msg, CONTEXT* ctx) {
1083 return (commit_message (msg, ctx, 0));
1086 static mx_t* reg_mx (void) {
1087 mx_t* fmt = p_new(mx_t, 1);
1089 fmt->mx_check_empty = mbox_check_empty;
1090 fmt->mx_is_magic = mbox_is_magic;
1091 fmt->mx_access = access;
1092 fmt->mx_open_mailbox = mbox_open_mailbox;
1093 fmt->mx_open_new_message = mbox_open_new_message;
1094 fmt->mx_sync_mailbox = mbox_sync_mailbox;
1095 fmt->mx_check_mailbox = mbox_check_mailbox;
1099 mx_t* mbox_reg_mx (void) {
1100 mx_t* fmt = reg_mx ();
1102 fmt->mx_commit_message = mbox_commit_message;
1105 mx_t* mmdf_reg_mx (void) {
1106 mx_t* fmt = reg_mx ();
1108 fmt->mx_commit_message = mmdf_commit_message;