There were some bug-fixes hidden in that code.
return -1;
if ((msg = mx_open_new_message (&ctx, hn, is_from (buf, NULL, 0) ? 0 : M_ADD_FROM)) == NULL)
{
- mx_close_mailbox(&ctx);
+ mx_close_mailbox(&ctx, NULL);
return -1;
}
if (ctx.magic == M_MBOX || ctx.magic == M_MMDF || ctx.magic == M_KENDRA)
r = -1;
mx_close_message (&msg);
- mx_close_mailbox (&ctx);
+ mx_close_mailbox (&ctx, NULL);
return r;
}
else
need_buffy_cleanup = (ctx.magic == M_MBOX || ctx.magic == M_MMDF || ctx.magic == M_KENDRA);
- mx_close_mailbox (&ctx);
+ mx_close_mailbox (&ctx, NULL);
if (need_buffy_cleanup)
{
if (!ctx->msgcount)
{
- mx_close_mailbox (ctx);
+ mx_close_mailbox (ctx, NULL);
safe_free ((void **) &ctx);
mutt_error _("No messages in that folder.");
break;
menu->redraw |= REDRAW_FULL;
if (close == OP_QUIT)
- mx_close_mailbox (Context);
+ mx_close_mailbox (Context, NULL);
else
mx_fastclose_mailbox (Context);
safe_free ((void **) &Context);
return 0;
}
+static void update_index (MUTTMENU *menu, CONTEXT *ctx, int check,
+ int oldcount, int index_hint)
+{
+ /* store pointers to the newly added messages */
+ HEADER **save_new = NULL;
+ int j;
+
+ /* take note of the current message */
+ if (oldcount)
+ {
+ if (menu->current < Context->vcount)
+ menu->oldcurrent = index_hint;
+ else
+ oldcount = 0; /* invalid message number! */
+ }
+
+ /* We are in a limited view. Check if the new message(s) satisfy
+ * the limit criteria. If they do, set their virtual msgno so that
+ * they will be visible in the limited view */
+ if (Context->pattern)
+ {
+#define THIS_BODY Context->hdrs[j]->content
+ if (oldcount || check == M_REOPENED)
+ {
+ for (j = (check == M_REOPENED) ? 0 : oldcount; j < Context->msgcount; j++)
+ {
+ if (mutt_pattern_exec (Context->limit_pattern,
+ M_MATCH_FULL_ADDRESS,
+ Context, Context->hdrs[j]))
+ {
+ Context->hdrs[j]->virtual = Context->vcount;
+ Context->v2r[Context->vcount] = j;
+ Context->hdrs[j]->limited = 1;
+ Context->vcount++;
+ Context->vsize += THIS_BODY->length + THIS_BODY->offset - THIS_BODY->hdr_offset;
+ }
+ }
+ }
+#undef THIS_BODY
+ }
+
+ /* save the list of new messages */
+ if (oldcount && check != M_REOPENED
+ && ((Sort & SORT_MASK) == SORT_THREADS))
+ {
+ save_new = (HEADER **) safe_malloc (sizeof (HEADER *) * (Context->msgcount - oldcount));
+ for (j = oldcount; j < Context->msgcount; j++)
+ save_new[j-oldcount] = Context->hdrs[j];
+ }
+
+ /* if the mailbox was reopened, need to rethread from scratch */
+ mutt_sort_headers (Context, (check == M_REOPENED));
+
+ /* uncollapse threads with new mail */
+ if ((Sort & SORT_MASK) == SORT_THREADS)
+ {
+ if (check == M_REOPENED)
+ {
+ HEADER *h;
+
+ h = Context->tree;
+ Context->collapsed = 0;
+
+ while (h)
+ {
+ mutt_uncollapse_thread (Context, h);
+ h = h->next;
+ }
+ mutt_set_virtual (Context);
+ }
+ else if (oldcount)
+ {
+ for (j = 0; j < Context->msgcount - oldcount; j++)
+ {
+ int k;
+
+ for (k = 0; k < Context->msgcount; k++)
+ {
+ HEADER *h = Context->hdrs[k];
+ if (h == save_new[j] && (!Context->pattern || h->limited))
+ mutt_uncollapse_thread (Context, h);
+ }
+ }
+ FREE (&save_new);
+ mutt_set_virtual (Context);
+ }
+ }
+
+ menu->current = -1;
+ if (oldcount)
+ {
+ /* restore the current message to the message it was pointing to */
+ for (j = 0; j < Context->vcount; j++)
+ {
+ if (Context->hdrs[Context->v2r[j]]->index == menu->oldcurrent)
+ {
+ menu->current = j;
+ break;
+ }
+ }
+ }
+
+ if (menu->current < 0)
+ menu->current = ci_first_message ();
+
+}
+
static void resort_index (MUTTMENU *menu)
{
int i;
int do_buffy_notify = 1;
int close = 0; /* did we OP_QUIT or OP_EXIT out of this menu? */
int attach_msg = option(OPTATTACHMSG);
- int check_lock = 0;
menu = mutt_new_menu ();
menu->menu = MENU_MAIN;
index_hint = (Context->vcount) ? CURHDR->index : 0;
- if ((check = mx_check_mailbox (Context, &index_hint, check_lock)) < 0)
+ if ((check = mx_check_mailbox (Context, &index_hint, 0)) < 0)
{
if (!Context->path)
{
}
else if (check == M_NEW_MAIL || check == M_REOPENED)
{
- /* store pointers to the newly added messages */
- HEADER **save_new = NULL;
-
- /* take note of the current message */
- if (oldcount)
- {
- if (menu->current < Context->vcount)
- menu->oldcurrent = index_hint;
- else
- oldcount = 0; /* invalid message number! */
- }
-
- /* We are in a limited view. Check if the new message(s) satisfy
- * the limit criteria. If they do, set their virtual msgno so that
- * they will be visible in the limited view */
- if (Context->pattern)
- {
-#define THIS_BODY Context->hdrs[j]->content
- if (oldcount || check == M_REOPENED)
- for (j = (check == M_REOPENED) ? 0 : oldcount; j < Context->msgcount; j++)
- {
- if (mutt_pattern_exec (Context->limit_pattern,
- M_MATCH_FULL_ADDRESS,
- Context, Context->hdrs[j]))
- {
- Context->hdrs[j]->virtual = Context->vcount;
- Context->v2r[Context->vcount] = j;
- Context->hdrs[j]->limited = 1;
- Context->vcount++;
- Context->vsize += THIS_BODY->length + THIS_BODY->offset - THIS_BODY->hdr_offset;
- }
- }
-#undef THIS_BODY
- }
-
- /* save the list of new messages */
- if (oldcount && check != M_REOPENED
- && ((Sort & SORT_MASK) == SORT_THREADS))
- {
- save_new = (HEADER **) safe_malloc (sizeof (HEADER *) * (Context->msgcount - oldcount));
- for (j = oldcount; j < Context->msgcount; j++)
- save_new[j-oldcount] = Context->hdrs[j];
- }
-
- /* if the mailbox was reopened, need to rethread from scratch */
- mutt_sort_headers (Context, (check == M_REOPENED));
-
- /* uncollapse threads with new mail */
- if ((Sort & SORT_MASK) == SORT_THREADS)
- {
- if (check == M_REOPENED)
- {
- HEADER *h;
-
- h = Context->tree;
- Context->collapsed = 0;
-
- while (h)
- {
- mutt_uncollapse_thread (Context, h);
- h = h->next;
- }
- mutt_set_virtual (Context);
- }
- else if (oldcount)
- {
- for (j = 0; j < Context->msgcount - oldcount; j++)
- {
- int k;
-
- for (k = 0; k < Context->msgcount; k++)
- {
- HEADER *h = Context->hdrs[k];
- if (h == save_new[j] && (!Context->pattern || h->limited))
- mutt_uncollapse_thread (Context, h);
- }
- }
- FREE (&save_new);
- mutt_set_virtual (Context);
- }
- }
-
- menu->current = -1;
- if (oldcount)
- {
- /* restore the current message to the message it was pointing to */
- for (j = 0; j < Context->vcount; j++)
- if (Context->hdrs[Context->v2r[j]]->index == menu->oldcurrent)
- {
- menu->current = j;
- break;
- }
- }
-
- if (menu->current < 0)
- menu->current = ci_first_message ();
-
+ update_index (menu, Context, check, oldcount, index_hint);
+
/* notify the user of new mail */
if (check == M_REOPENED)
mutt_error _("Mailbox was externally modified. Flags may be wrong.");
}
/* avoid the message being overwritten by buffy */
do_buffy_notify = 0;
-
+
menu->redraw = REDRAW_FULL;
menu->max = Context->vcount;
-
+
set_option (OPTSEARCHINVALID);
}
}
imap_disallow_reopen (Context);
#endif
- check_lock = 0;
-
if (!attach_msg)
{
/* check for new mail in the incoming folders */
}
if (query_quadoption (OPT_QUIT, _("Quit Mutt?")) == M_YES)
- {
+ {
+ int check;
+
#ifdef USE_IMAP
/* logout gracefully from the IMAP server */
if (Context && Context->magic == M_IMAP)
imap_set_logout (Context);
#endif
- if (!Context || mx_close_mailbox (Context) == 0)
+ oldcount = Context->msgcount;
+
+ if (!Context || (check = mx_close_mailbox (Context, &index_hint)) == 0)
done = 1;
else
+ {
+ if (check == M_NEW_MAIL || check == M_REOPENED)
+ update_index (menu, Context, check, oldcount, index_hint);
+
menu->redraw = REDRAW_FULL; /* new mail arrived? */
+ set_option (OPTSEARCHINVALID);
+ }
}
break;
case OP_MAIN_SYNC_FOLDER:
- check_lock = 1; /* call mx_check_mailbox() with locking the
- * nex time
- */
-
CHECK_MSGCOUNT;
CHECK_READONLY;
{
int oldvcount = Context->vcount;
+ int oldcount = Context->msgcount;
int dcount = 0;
+ int check;
/* calculate the number of messages _above_ the cursor,
* so we can keep the cursor on the current message
if (Context->hdrs[Context->v2r[j]]->deleted)
dcount++;
}
- if (mx_sync_mailbox (Context) == 0)
+
+ if ((check = mx_sync_mailbox (Context, &index_hint)) == 0)
{
if (Context->vcount != oldvcount)
menu->current -= dcount;
set_option (OPTSEARCHINVALID);
}
-
- /* do a sanity check even if mx_sync_mailbox failed.
+ else if (check == M_NEW_MAIL || check == M_REOPENED)
+ update_index (menu, Context, check, oldcount, index_hint);
+
+ /*
+ * do a sanity check even if mx_sync_mailbox failed.
*/
if (menu->current < 0 || menu->current >= Context->vcount)
if (Context)
{
+ int check;
+
mutt_str_replace (&LastFolder, Context->path);
+ oldcount = Context->msgcount;
- if (mx_close_mailbox (Context) != 0)
+ if ((check = mx_close_mailbox (Context, &index_hint)) != 0)
{
+ if (check == M_NEW_MAIL || check == M_REOPENED)
+ update_index (menu, Context, check, oldcount, index_hint);
+
+ set_option (OPTSEARCHINVALID);
menu->redraw = REDRAW_INDEX | REDRAW_STATUS;
break;
}
rc = mutt_append_message (&tmpctx, ctx, cur, 0, CH_NOLEN | CH_NOSTATUS); oerrno = errno;
- mx_close_mailbox (&tmpctx);
+ mx_close_mailbox (&tmpctx, NULL);
if (rc == -1)
{
if (msg == NULL)
{
mutt_error (_("Can't append to folder: %s"), strerror (errno));
- mx_close_mailbox (&tmpctx);
+ mx_close_mailbox (&tmpctx, NULL);
goto bail;
}
rc = mx_commit_message (msg, &tmpctx);
mx_close_message (&msg);
- mx_close_mailbox (&tmpctx);
+ mx_close_mailbox (&tmpctx, NULL);
bail:
if (fp) fclose (fp);
return -1;
}
- if (imap_sync_mailbox (ctx, 0) < 0)
+ if (imap_sync_mailbox (ctx, 0, NULL) < 0)
return -1;
idata = CTX_DATA;
* expunge: 0 or 1 - do expunge?
*/
-int imap_sync_mailbox (CONTEXT* ctx, int expunge)
+int imap_sync_mailbox (CONTEXT* ctx, int expunge, int *index_hint)
{
char buf[HUGE_STRING];
char flags[LONG_STRING];
* expects the context to be changed.
*/
- if ((rc = imap_check_mailbox (ctx, NULL)) != 0)
+ if ((rc = imap_check_mailbox (ctx, index_hint)) != 0)
return rc;
/* if we are expunging anyway, we can do deleted messages very quickly... */
int imap_open_mailbox_append (CONTEXT *ctx);
int imap_select_mailbox (CONTEXT *ctx, const char* path);
void imap_set_logout (CONTEXT *ctx);
-int imap_sync_mailbox (CONTEXT *ctx, int expunge);
+int imap_sync_mailbox (CONTEXT *ctx, int expunge, int *index_hint);
void imap_fastclose_mailbox (CONTEXT *ctx);
int imap_buffy_check (char *path);
int imap_mailbox_check (char *path, int new);
void mx_fastclose_mailbox (CONTEXT *);
-int mx_close_mailbox (CONTEXT *);
-int mx_sync_mailbox (CONTEXT *);
+int mx_close_mailbox (CONTEXT *, int *);
+int mx_sync_mailbox (CONTEXT *, int *);
int mx_commit_message (MESSAGE *, CONTEXT *);
int mx_close_message (MESSAGE **);
int mx_get_magic (const char *);
* 0 success
* -1 failure
*/
-int mbox_sync_mailbox (CONTEXT *ctx)
+int mbox_sync_mailbox (CONTEXT *ctx, int *index_hint)
{
char tempfile[_POSIX_PATH_MAX];
char buf[32];
int i, j, save_sort = SORT_ORDER;
+ int rc = -1;
int need_sort = 0; /* flag to resort mailbox if new mail arrives */
int first; /* first message to be written */
long offset; /* location in mailbox to write changed messages */
}
/* Check to make sure that the file hasn't changed on disk */
- if ((i = mbox_check_mailbox (ctx, NULL)) == M_NEW_MAIL || i == M_REOPENED)
+ if ((i = mbox_check_mailbox (ctx, index_hint)) == M_NEW_MAIL || i == M_REOPENED)
{
/* new mail arrived, or mailbox reopened */
need_sort = i;
+ rc = i;
goto bail;
}
else if (i < 0)
bail: /* Come here in case of disaster */
- /* Disadvantage of using "goto" */
- if (fp)
- fclose (fp);
+ safe_fclose (&fp);
/* this is ok to call even if we haven't locked anything */
mbox_unlock_mailbox (ctx);
mutt_sort_headers (ctx, (need_sort == M_REOPENED));
}
- return (-1);
+ return rc;
}
/* close a mailbox opened in write-mode */
return (0);
}
-int mh_sync_mailbox (CONTEXT * ctx)
+int mh_sync_mailbox (CONTEXT * ctx, int *index_hint)
{
char path[_POSIX_PATH_MAX], tmp[_POSIX_PATH_MAX];
int i, j;
- if (mh_check_mailbox(ctx, NULL) != 0)
- return -1;
+ if ((i = mh_check_mailbox(ctx, index_hint)) != 0)
+ return i;
for (i = 0; i < ctx->msgcount; i++)
{
}
/* save changes to disk */
-static int sync_mailbox (CONTEXT *ctx)
+static int sync_mailbox (CONTEXT *ctx, int *index_hint)
{
#ifdef BUFFY_SIZE
BUFFY *tmp = NULL;
{
case M_MBOX:
case M_MMDF:
- rc = mbox_sync_mailbox (ctx);
+ rc = mbox_sync_mailbox (ctx, index_hint);
#ifdef BUFFY_SIZE
tmp = mutt_find_mailbox (ctx->path);
#endif
case M_MH:
case M_MAILDIR:
- rc = mh_sync_mailbox (ctx);
+ rc = mh_sync_mailbox (ctx, index_hint);
break;
#ifdef USE_IMAP
case M_IMAP:
/* extra argument means EXPUNGE */
- rc = imap_sync_mailbox (ctx, 1);
+ rc = imap_sync_mailbox (ctx, 1, index_hint);
break;
#endif /* USE_IMAP */
}
}
/* save changes and close mailbox */
-int mx_close_mailbox (CONTEXT *ctx)
+int mx_close_mailbox (CONTEXT *ctx, int *index_hint)
{
int i, move_messages = 0, purge = 1, read_msgs = 0;
+ int check;
int isSpool = 0;
CONTEXT f;
char mbox[_POSIX_PATH_MAX];
}
else
{
- mx_close_mailbox (&f);
+ mx_close_mailbox (&f, NULL);
return -1;
}
}
}
- mx_close_mailbox (&f);
+ mx_close_mailbox (&f, NULL);
}
}
/* allow IMAP to preserve the deleted flag across sessions */
if (ctx->magic == M_IMAP)
{
- if (imap_sync_mailbox (ctx, purge) != 0)
- return -1;
+ if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0)
+ return check;
}
else
#endif
if (ctx->changed || ctx->deleted)
{
- if (sync_mailbox (ctx) != 0)
- return -1;
+ if ((check = sync_mailbox (ctx, index_hint)) != 0)
+ return check;
}
}
* 0 success
* -1 error
*/
-int mx_sync_mailbox (CONTEXT *ctx)
+int mx_sync_mailbox (CONTEXT *ctx, int *index_hint)
{
int rc, i;
int purge = 1;
#ifdef USE_IMAP
if (ctx->magic == M_IMAP)
- rc = imap_sync_mailbox (ctx, purge);
+ rc = imap_sync_mailbox (ctx, purge, index_hint);
else
#endif
- rc = sync_mailbox (ctx);
+ rc = sync_mailbox (ctx, index_hint);
if (rc == 0)
{
#ifdef USE_IMAP
#define KENDRA_SEP "\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001\001\n"
#define MAXLOCKATTEMPT 5
-int mbox_sync_mailbox (CONTEXT *);
+int mbox_sync_mailbox (CONTEXT *, int *);
int mbox_open_mailbox (CONTEXT *);
int mbox_check_mailbox (CONTEXT *, int *);
int mbox_close_mailbox (CONTEXT *);
void mbox_unlock_mailbox (CONTEXT *);
int mh_read_dir (CONTEXT *, const char *);
-int mh_sync_mailbox (CONTEXT *);
+int mh_sync_mailbox (CONTEXT *, int *);
int mh_check_mailbox (CONTEXT *, int *);
int mh_parse_sequences (CONTEXT *, const char *);
err = 1;
mx_close_message (&msg);
}
- mx_close_mailbox (&ctx);
+ mx_close_mailbox (&ctx, NULL);
if (err)
{
if (! PostContext->msgcount)
{
PostCount = 0;
- mx_close_mailbox (PostContext);
+ mx_close_mailbox (PostContext, NULL);
#ifdef USE_IMAP
if (need_reopen)
ctx = mx_open_mailbox (curpath, 0, PostContext);
}
else if ((h = select_msg ()) == NULL)
{
- mx_close_mailbox (PostContext);
+ mx_close_mailbox (PostContext, NULL);
#ifdef USE_IMAP
if (need_reopen)
ctx = mx_open_mailbox (curpath, 0, PostContext);
/* avoid the "purge deleted messages" prompt */
opt_delete = quadoption (OPT_DELETE);
set_quadoption (OPT_DELETE, M_YES);
- mx_close_mailbox (PostContext);
+ mx_close_mailbox (PostContext, NULL);
set_quadoption (OPT_DELETE, opt_delete);
#ifdef USE_IMAP
{
tmp = rfc822_cpy_adr_real (tmp);
if (!option (OPTREVREAL))
- safe_free (&tmp->personal);
+ FREE (&tmp->personal);
if (!tmp->personal)
tmp->personal = safe_strdup (Realname);
}
if ((tempfp = safe_fopen (tempfile, "w+")) == NULL)
{
mutt_perror (tempfile);
- mx_close_mailbox (&f);
+ mx_close_mailbox (&f, NULL);
return (-1);
}
}
hdr->read = !post; /* make sure to put it in the `cur' directory (maildir) */
if ((msg = mx_open_new_message (&f, hdr, M_ADD_FROM)) == NULL)
{
- mx_close_mailbox (&f);
+ mx_close_mailbox (&f, NULL);
return (-1);
}
unlink (tempfile);
mx_commit_message (msg, &f); /* XXX - really? */
mx_close_message (&msg);
- mx_close_mailbox (&f);
+ mx_close_mailbox (&f, NULL);
return -1;
}
if (mx_commit_message (msg, &f) != 0)
r = -1;
mx_close_message (&msg);
- mx_close_mailbox (&f);
+ mx_close_mailbox (&f, NULL);
return r;
}