X-Git-Url: https://git.llucax.com/software/mutt-debian.git/blobdiff_plain/14c29200cb58d3c4a0830265f2433849781858d0..5a2d8285f6bef8f940767a5f17780e998de2a19e:/mbox.c diff --git a/mbox.c b/mbox.c index c450172..afd1be4 100644 --- a/mbox.c +++ b/mbox.c @@ -85,7 +85,7 @@ int mmdf_parse_mailbox (CONTEXT *ctx) char return_path[LONG_STRING]; int count = 0, oldmsgcount = ctx->msgcount; int lines; - time_t t, tz; + time_t t; LOFF_T loc, tmploc; HEADER *hdr; struct stat sb; @@ -112,10 +112,6 @@ int mmdf_parse_mailbox (CONTEXT *ctx) } #endif - /* precompute the local timezone to speed up calculation of the - received time */ - tz = mutt_local_tz (0); - buf[sizeof (buf) - 1] = 0; if (!ctx->quiet) @@ -136,7 +132,7 @@ int mmdf_parse_mailbox (CONTEXT *ctx) count++; if (!ctx->quiet) mutt_progress_update (&progress, count, - (int)loc / (ctx->size / 100 + 1)); + (int) (loc / (ctx->size / 100 + 1))); if (ctx->msgcount == ctx->hdrmax) mx_alloc_memory (ctx); @@ -163,7 +159,7 @@ int mmdf_parse_mailbox (CONTEXT *ctx) } } else - hdr->received = t - tz; + hdr->received = t - mutt_local_tz (t); hdr->env = mutt_read_rfc822_header (ctx->fp, hdr, 0, 0); @@ -208,7 +204,7 @@ int mmdf_parse_mailbox (CONTEXT *ctx) hdr->env->return_path = rfc822_parse_adrlist (hdr->env->return_path, return_path); if (!hdr->env->from) - hdr->env->from = rfc822_cpy_adr (hdr->env->return_path); + hdr->env->from = rfc822_cpy_adr (hdr->env->return_path, 0); ctx->msgcount++; } @@ -237,7 +233,7 @@ int mbox_parse_mailbox (CONTEXT *ctx) struct stat sb; char buf[HUGE_STRING], return_path[STRING]; HEADER *curhdr; - time_t t, tz; + time_t t; int count = 0, lines = 0; LOFF_T loc; #ifdef NFS_ATTRIBUTE_HACK @@ -268,10 +264,6 @@ int mbox_parse_mailbox (CONTEXT *ctx) if (!ctx->readonly) ctx->readonly = access (ctx->path, W_OK) ? 1 : 0; - /* precompute the local timezone to speed up calculation of the - date received */ - tz = mutt_local_tz (0); - if (!ctx->quiet) { snprintf (msgbuf, sizeof (msgbuf), _("Reading %s..."), ctx->path); @@ -302,13 +294,13 @@ int mbox_parse_mailbox (CONTEXT *ctx) if (!ctx->quiet) mutt_progress_update (&progress, count, - (int)(ftell (ctx->fp) / (ctx->size / 100 + 1))); + (int)(ftello (ctx->fp) / (ctx->size / 100 + 1))); if (ctx->msgcount == ctx->hdrmax) mx_alloc_memory (ctx); curhdr = ctx->hdrs[ctx->msgcount] = mutt_new_header (); - curhdr->received = t - tz; + curhdr->received = t - mutt_local_tz (t); curhdr->offset = loc; curhdr->index = ctx->msgcount; @@ -383,7 +375,7 @@ int mbox_parse_mailbox (CONTEXT *ctx) curhdr->env->return_path = rfc822_parse_adrlist (curhdr->env->return_path, return_path); if (!curhdr->env->from) - curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path); + curhdr->env->from = rfc822_cpy_adr (curhdr->env->return_path, 0); lines = 0; } @@ -679,6 +671,48 @@ int mbox_check_mailbox (CONTEXT *ctx, int *index_hint) return (-1); } +/* + * Returns 1 if the mailbox has at least 1 new messages (not old) + * otherwise returns 0. + */ +static int mbox_has_new(CONTEXT *ctx) +{ + int i; + + for (i = 0; i < ctx->msgcount; i++) + if (!ctx->hdrs[i]->deleted && !ctx->hdrs[i]->read && !ctx->hdrs[i]->old) + return 1; + return 0; +} + +/* if mailbox has at least 1 new message, sets mtime > atime of mailbox + * so buffy check reports new mail */ +void mbox_reset_atime (CONTEXT *ctx, struct stat *st) +{ + struct utimbuf utimebuf; + int i; + struct stat _st; + + if (!st) + { + if (stat (ctx->path, &_st) < 0) + return; + st = &_st; + } + + utimebuf.actime = st->st_atime; + utimebuf.modtime = st->st_mtime; + + /* + * When $mbox_check_recent is set, existing new mail is ignored, so do not + * recent the atime to mtime-1 to signal new mail. + */ + if (!option(OPTMAILCHECKRECENT) && utimebuf.actime >= utimebuf.modtime && mbox_has_new(ctx)) + utimebuf.actime = utimebuf.modtime - 1; + + utime (ctx->path, &utimebuf); +} + /* return values: * 0 success * -1 failure @@ -693,7 +727,6 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint) int first = -1; /* first message to be written */ LOFF_T offset; /* location in mailbox to write changed messages */ struct stat statbuf; - struct utimbuf utimebuf; struct m_update_t *newOffset = NULL; struct m_update_t *oldOffset = NULL; FILE *fp = NULL; @@ -742,7 +775,7 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint) return (-1); /* Create a temporary file to write the new version of the mailbox in. */ - mutt_mktemp (tempfile); + mutt_mktemp (tempfile, sizeof (tempfile)); if ((i = open (tempfile, O_WRONLY | O_EXCL | O_CREAT, 0600)) == -1 || (fp = fdopen (i, "w")) == NULL) { @@ -799,7 +832,7 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint) for (i = first, j = 0; i < ctx->msgcount; i++) { if (!ctx->quiet) - mutt_progress_update (&progress, i, (int)(ftell (ctx->fp) / (ctx->size / 100 + 1))); + mutt_progress_update (&progress, i, (int)(ftello (ctx->fp) / (ctx->size / 100 + 1))); /* * back up some information which is needed to restore offsets when * something fails. @@ -833,7 +866,8 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint) */ newOffset[i - first].hdr = ftello (fp) + offset; - if (mutt_copy_message (fp, ctx, ctx->hdrs[i], M_CM_UPDATE, CH_FROM | CH_UPDATE | CH_UPDATE_LEN) == -1) + if (mutt_copy_message (fp, ctx, ctx->hdrs[i], M_CM_UPDATE, + CH_FROM | CH_UPDATE | CH_UPDATE_LEN) != 0) { mutt_perror (tempfile); mutt_sleep (5); @@ -876,7 +910,7 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint) if (fclose (fp) != 0) { fp = NULL; - dprint(1, (debugfile, "mbox_sync_mailbox: fclose() returned non-zero.\n")); + dprint(1, (debugfile, "mbox_sync_mailbox: safe_fclose (&) returned non-zero.\n")); unlink (tempfile); mutt_perror (tempfile); mutt_sleep (5); @@ -925,7 +959,8 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint) /* copy the temp mailbox back into place starting at the first * change/deleted message */ - mutt_message _("Committing changes..."); + if (!ctx->quiet) + mutt_message _("Committing changes..."); i = mutt_copy_stream (fp, ctx->fp); if (ferror (ctx->fp)) @@ -938,7 +973,7 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint) } } - fclose (fp); + safe_fclose (&fp); fp = NULL; mbox_unlock_mailbox (ctx); @@ -955,16 +990,14 @@ int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint) rename (tempfile, savefile); mutt_unblock_signals (); mx_fastclose_mailbox (ctx); - mutt_pretty_mailbox (savefile); + mutt_pretty_mailbox (savefile, sizeof (savefile)); mutt_error (_("Write failed! Saved partial mailbox to %s"), savefile); mutt_sleep (5); return (-1); } /* Restore the previous access/modification times */ - utimebuf.actime = statbuf.st_atime; - utimebuf.modtime = statbuf.st_mtime; - utime (ctx->path, &utimebuf); + mbox_reset_atime (ctx, &statbuf); /* reopen the mailbox in read-only mode */ if ((ctx->fp = fopen (ctx->path, "r")) == NULL) @@ -1055,7 +1088,8 @@ int mutt_reopen_mailbox (CONTEXT *ctx, int *index_hint) /* silent operations */ ctx->quiet = 1; - mutt_message _("Reopening mailbox..."); + if (!ctx->quiet) + mutt_message _("Reopening mailbox..."); /* our heuristics require the old mailbox to be unsorted */ if (Sort != SORT_ORDER) @@ -1108,17 +1142,13 @@ int mutt_reopen_mailbox (CONTEXT *ctx, int *index_hint) { case M_MBOX: case M_MMDF: - if (fseek (ctx->fp, 0, SEEK_SET) != 0) - { - dprint (1, (debugfile, "mutt_reopen_mailbox: fseek() failed\n")); - rc = -1; - } - else - { - cmp_headers = mbox_strict_cmp_headers; - rc = ((ctx->magic == M_MBOX) ? mbox_parse_mailbox - : mmdf_parse_mailbox) (ctx); - } + cmp_headers = mbox_strict_cmp_headers; + safe_fclose (&ctx->fp); + if (!(ctx->fp = safe_fopen (ctx->path, "r"))) + rc = -1; + else + rc = ((ctx->magic == M_MBOX) ? mbox_parse_mailbox + : mmdf_parse_mailbox) (ctx); break; default: