Skip to content

Commit

Permalink
Optionally keep an account alive on removal of its last mailbox
Browse files Browse the repository at this point in the history
  • Loading branch information
gahr authored and flatcap committed Dec 24, 2021
1 parent e945111 commit d9f13d4
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 27 deletions.
4 changes: 2 additions & 2 deletions compose/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ static int op_compose_attach_message(struct ComposeSharedData *shared, int op)
if (!mx_mbox_open(m_attach, MUTT_READONLY))
{
mutt_error(_("Unable to open mailbox %s"), mutt_buffer_string(fname));
mx_fastclose_mailbox(m_attach);
mx_fastclose_mailbox(m_attach, false);
m_attach = NULL;
mutt_buffer_pool_release(&fname);
return IR_ERROR;
Expand Down Expand Up @@ -641,7 +641,7 @@ static int op_compose_attach_message(struct ComposeSharedData *shared, int op)
{
m_attach->readonly = old_readonly;
}
mx_fastclose_mailbox(m_attach_new);
mx_fastclose_mailbox(m_attach_new, false);

/* Restore old $sort variables */
cs_subset_str_native_set(shared->sub, "sort", old_sort, NULL);
Expand Down
4 changes: 1 addition & 3 deletions imap/command.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,11 +173,9 @@ static void cmd_handle_fatal(struct ImapAccountData *adata)

if ((adata->state >= IMAP_SELECTED) && (mdata->reopen & IMAP_REOPEN_ALLOW))
{
mx_fastclose_mailbox(adata->mailbox);
mutt_socket_close(adata->conn);
mx_fastclose_mailbox(adata->mailbox, true);
mutt_error(_("Mailbox %s@%s closed"), adata->conn->account.user,
adata->conn->account.host);
adata->state = IMAP_DISCONNECTED;
}

imap_close_connection(adata);
Expand Down
2 changes: 1 addition & 1 deletion imap/imap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1251,7 +1251,7 @@ int imap_path_status(const char *path, bool queue)

if (is_temp)
{
mx_ac_remove(m);
mx_ac_remove(m, false);
mailbox_free(&m);
}

Expand Down
2 changes: 1 addition & 1 deletion index/functions.c
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ static int op_exit(struct IndexSharedData *shared, struct IndexPrivateData *priv
{
if (shared->ctx)
{
mx_fastclose_mailbox(shared->mailbox);
mx_fastclose_mailbox(shared->mailbox, false);
ctx_free(&shared->ctx);
}
return IR_DONE;
Expand Down
12 changes: 6 additions & 6 deletions mbox/mbox.c
Original file line number Diff line number Diff line change
Expand Up @@ -1150,7 +1150,7 @@ static enum MxStatus mbox_mbox_check(struct Mailbox *m)

error:
mbox_unlock_mailbox(m);
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);
mutt_sig_unblock();
mutt_error(_("Mailbox was corrupted"));
return MX_STATUS_ERROR;
Expand Down Expand Up @@ -1194,7 +1194,7 @@ static enum MxStatus mbox_mbox_sync(struct Mailbox *m)
adata->fp = freopen(mailbox_path(m), "r+", adata->fp);
if (!adata->fp)
{
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);
mutt_error(_("Fatal error! Could not reopen mailbox!"));
goto fatal;
}
Expand Down Expand Up @@ -1365,7 +1365,7 @@ static enum MxStatus mbox_mbox_sync(struct Mailbox *m)
if (!fp)
{
mutt_sig_unblock();
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);
mutt_debug(LL_DEBUG1, "unable to reopen temp copy of mailbox!\n");
mutt_perror(mutt_buffer_string(tempfile));
FREE(&new_offset);
Expand Down Expand Up @@ -1426,7 +1426,7 @@ static enum MxStatus mbox_mbox_sync(struct Mailbox *m)
NONULL(ShortHostname), (unsigned int) getpid());
rename(mutt_buffer_string(tempfile), mutt_buffer_string(savefile));
mutt_sig_unblock();
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);
mutt_buffer_pretty_mailbox(savefile);
mutt_error(_("Write failed! Saved partial mailbox to %s"), mutt_buffer_string(savefile));
mutt_buffer_pool_release(&savefile);
Expand All @@ -1448,7 +1448,7 @@ static enum MxStatus mbox_mbox_sync(struct Mailbox *m)
{
unlink(mutt_buffer_string(tempfile));
mutt_sig_unblock();
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);
mutt_error(_("Fatal error! Could not reopen mailbox!"));
FREE(&new_offset);
FREE(&old_offset);
Expand Down Expand Up @@ -1515,7 +1515,7 @@ static enum MxStatus mbox_mbox_sync(struct Mailbox *m)
if (!adata->fp)
{
mutt_error(_("Could not reopen mailbox"));
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);
goto fatal;
}

Expand Down
20 changes: 11 additions & 9 deletions mx.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
return true;

error:
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, newly_linked_account);
if (newly_linked_account)
account_mailbox_remove(m->account, m);
return false;
Expand All @@ -425,8 +425,9 @@ bool mx_mbox_open(struct Mailbox *m, OpenMailboxFlags flags)
/**
* mx_fastclose_mailbox - Free up memory associated with the Mailbox
* @param m Mailbox
* @param keep_account Make sure not to remove the mailbox's account
*/
void mx_fastclose_mailbox(struct Mailbox *m)
void mx_fastclose_mailbox(struct Mailbox *m, bool keep_account)
{
if (!m)
return;
Expand Down Expand Up @@ -459,7 +460,7 @@ void mx_fastclose_mailbox(struct Mailbox *m)

if (m->flags & MB_HIDDEN)
{
mx_ac_remove(m);
mx_ac_remove(m, keep_account);
}
}

Expand Down Expand Up @@ -624,7 +625,7 @@ enum MxStatus mx_mbox_close(struct Mailbox *m)

if (m->readonly || m->dontwrite || m->append || m->peekonly)
{
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);
return 0;
}

Expand Down Expand Up @@ -809,7 +810,7 @@ enum MxStatus mx_mbox_close(struct Mailbox *m)
mutt_message(_("Mailbox is unchanged"));
if ((m->type == MUTT_MBOX) || (m->type == MUTT_MMDF))
mbox_reset_atime(m, NULL);
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);
rc = MX_STATUS_OK;
goto cleanup;
}
Expand Down Expand Up @@ -901,7 +902,7 @@ enum MxStatus mx_mbox_close(struct Mailbox *m)
}
#endif

mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);

rc = MX_STATUS_OK;

Expand Down Expand Up @@ -1025,7 +1026,7 @@ enum MxStatus mx_mbox_sync(struct Mailbox *m)
!mutt_is_spool(mailbox_path(m)) && !c_save_empty)
{
unlink(mailbox_path(m));
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);
return MX_STATUS_OK;
}

Expand Down Expand Up @@ -1768,19 +1769,20 @@ bool mx_ac_add(struct Account *a, struct Mailbox *m)
/**
* mx_ac_remove - Remove a Mailbox from an Account and delete Account if empty
* @param m Mailbox to remove
* @param keep_account Make sure not to remove the mailbox's account
* @retval 0 Success
* @retval -1 Error
*
* @note The mailbox is NOT free'd
*/
int mx_ac_remove(struct Mailbox *m)
int mx_ac_remove(struct Mailbox *m, bool keep_account)
{
if (!m || !m->account)
return -1;

struct Account *a = m->account;
account_mailbox_remove(m->account, m);
if (STAILQ_EMPTY(&a->mailboxes))
if (!keep_account && STAILQ_EMPTY(&a->mailboxes))
{
neomutt_account_remove(NeoMutt, a);
}
Expand Down
4 changes: 2 additions & 2 deletions mx.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ struct Mailbox *mx_mbox_find (struct Account *a, const char *path);
struct Mailbox *mx_mbox_find2 (const char *path);
bool mx_mbox_ac_link(struct Mailbox *m);
bool mx_ac_add (struct Account *a, struct Mailbox *m);
int mx_ac_remove (struct Mailbox *m);
int mx_ac_remove (struct Mailbox *m, bool keep_account);

int mx_access (const char *path, int flags);
void mx_alloc_memory (struct Mailbox *m);
int mx_path_is_empty (const char *path);
void mx_fastclose_mailbox(struct Mailbox *m);
void mx_fastclose_mailbox(struct Mailbox *m, bool keep_account);
const struct MxOps *mx_get_ops (enum MailboxType type);
bool mx_tags_is_supported(struct Mailbox *m);
int mx_toggle_write (struct Mailbox *m);
Expand Down
6 changes: 3 additions & 3 deletions postpone.c
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ int mutt_num_postponed(struct Mailbox *m, bool force)
if (mx_mbox_open(m_post, MUTT_NOSORT | MUTT_QUIET))
{
PostCount = m_post->msg_count;
mx_fastclose_mailbox(m_post);
mx_fastclose_mailbox(m_post, false);
if (m_post->flags == MB_HIDDEN)
mailbox_free(&m_post);
}
Expand Down Expand Up @@ -198,7 +198,7 @@ static void hardclose(struct Mailbox *m)
if (rc != MX_STATUS_ERROR && rc != MX_STATUS_OK)
rc = mx_mbox_close(m);
if (rc != MX_STATUS_OK)
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);
}

/**
Expand Down Expand Up @@ -672,7 +672,7 @@ int mutt_get_postponed(struct Mailbox *m_cur, struct Email *hdr,
mutt_error(_("No postponed messages"));
if (m_cur != m)
{
mx_fastclose_mailbox(m);
mx_fastclose_mailbox(m, false);
if (m->flags == MB_HIDDEN)
mailbox_free(&m);
}
Expand Down

0 comments on commit d9f13d4

Please sign in to comment.