From: Pietro Cerutti Date: Mon, 6 Feb 2017 15:03:41 +0000 (+0000) Subject: Simplify imap_keepalive + some cleanup (#354) X-Git-Tag: neomutt-20170225~45 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=71fb7cc8314d00e2a14dc0e5d9888f9ebc6c8b5a;p=neomutt Simplify imap_keepalive + some cleanup (#354) --- diff --git a/curs_main.c b/curs_main.c index 947d1dcff..ac2f02211 100644 --- a/curs_main.c +++ b/curs_main.c @@ -1666,7 +1666,7 @@ int mutt_index_menu (void) #ifdef USE_IMAP case OP_MAIN_IMAP_FETCH: if (Context && Context->magic == MUTT_IMAP) - imap_check_mailbox (Context, &index_hint, 1); + imap_check_mailbox (Context, 1); break; case OP_MAIN_IMAP_LOGOUT_ALL: diff --git a/imap/imap.c b/imap/imap.c index 673e5e80a..f7b6c61bc 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -165,7 +165,7 @@ int imap_delete_mailbox (CONTEXT* ctx, IMAP_MBOX mx) imap_munge_mbox_name (idata, mbox, sizeof (mbox), mx.mbox); snprintf (buf, sizeof (buf), "DELETE %s", mbox); - if (imap_exec ((IMAP_DATA*) idata, buf, 0) != 0) + if (imap_exec (idata, buf, 0) != 0) return -1; return 0; @@ -331,7 +331,7 @@ IMAP_DATA* imap_conn_find (const ACCOUNT* account, int flags) else memcpy (&conn->account, creds, sizeof (ACCOUNT)); - idata = (IMAP_DATA*)conn->data; + idata = conn->data; if (flags & MUTT_IMAP_CONN_NONEW) { if (!idata) @@ -1167,7 +1167,7 @@ static int sync_helper (IMAP_DATA* idata, int right, int flag, const char* name) * ctx: the current context * expunge: 0 or 1 - do expunge? */ -int imap_sync_mailbox (CONTEXT* ctx, int expunge, int* index_hint) +int imap_sync_mailbox (CONTEXT* ctx, int expunge) { IMAP_DATA* idata; CONTEXT* appendctx = NULL; @@ -1177,7 +1177,7 @@ int imap_sync_mailbox (CONTEXT* ctx, int expunge, int* index_hint) int n; int rc; - idata = (IMAP_DATA*) ctx->data; + idata = ctx->data; if (idata->state < IMAP_SELECTED) { @@ -1189,7 +1189,7 @@ int imap_sync_mailbox (CONTEXT* ctx, int expunge, int* index_hint) * to be changed. */ imap_allow_reopen (ctx); - if ((rc = imap_check_mailbox (ctx, index_hint, 0)) != 0) + if ((rc = imap_check (idata, 0)) != 0) return rc; /* if we are expunging anyway, we can do deleted messages very quickly... */ @@ -1366,7 +1366,7 @@ int imap_close_mailbox (CONTEXT* ctx) IMAP_DATA* idata; int i; - idata = (IMAP_DATA*) ctx->data; + idata = ctx->data; /* Check to see if the mailbox is actually open */ if (!idata) return 0; @@ -1416,15 +1416,17 @@ int imap_close_mailbox (CONTEXT* ctx) * 0 no change * -1 error */ -int imap_check_mailbox (CONTEXT *ctx, int *index_hint, int force) +int imap_check_mailbox (CONTEXT *ctx, int force) +{ + return imap_check (ctx->data, force); +} + +int imap_check (IMAP_DATA *idata, int force) { /* overload keyboard timeout to avoid many mailbox checks in a row. * Most users don't like having to wait exactly when they press a key. */ - IMAP_DATA* idata; int result = 0; - idata = (IMAP_DATA*) ctx->data; - /* try IDLE first, unless force is set */ if (!force && option (OPTIMAPIDLE) && mutt_bit_isset (idata->capabilities, IDLE) && (idata->state != IMAP_IDLE || time(NULL) >= idata->lastread + ImapKeepalive)) @@ -1473,9 +1475,10 @@ int imap_check_mailbox (CONTEXT *ctx, int *index_hint, int force) static int imap_check_mailbox_reopen (CONTEXT *ctx, int *index_hint) { int rc; + (void)index_hint; imap_allow_reopen (ctx); - rc = imap_check_mailbox (ctx, index_hint, 0); + rc = imap_check (ctx->data, 0); imap_disallow_reopen (ctx); return rc; @@ -1838,7 +1841,7 @@ static int imap_compile_search (const pattern_t* pat, BUFFER* buf) int imap_search (CONTEXT* ctx, const pattern_t* pat) { BUFFER buf; - IMAP_DATA* idata = (IMAP_DATA*)ctx->data; + IMAP_DATA* idata = ctx->data; int i; for (i = 0; i < ctx->msgcount; i++) @@ -2094,7 +2097,7 @@ int imap_fast_trash (CONTEXT* ctx, char* dest) IMAP_MBOX mx; int triedcreate = 0; - idata = (IMAP_DATA*) ctx->data; + idata = ctx->data; if (imap_parse_path (dest, &mx)) { @@ -2103,7 +2106,7 @@ int imap_fast_trash (CONTEXT* ctx, char* dest) } /* check that the save-to folder is in the same account */ - if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account))) + if (!mutt_account_match (&(idata->conn->account), &(mx.account))) { dprint (3, (debugfile, "imap_fast_trash: %s not same server as %s\n", dest, ctx->path)); diff --git a/imap/imap.h b/imap/imap.h index 584982d5f..1419270ac 100644 --- a/imap/imap.h +++ b/imap/imap.h @@ -33,9 +33,9 @@ typedef struct /* imap.c */ int imap_access (const char*, int); -int imap_check_mailbox (CONTEXT *ctx, int *index_hint, int force); -int imap_delete_mailbox (CONTEXT* idata, IMAP_MBOX mx); -int imap_sync_mailbox (CONTEXT *ctx, int expunge, int *index_hint); +int imap_check_mailbox (CONTEXT* ctx, int force); +int imap_delete_mailbox (CONTEXT* ctx, IMAP_MBOX mx); +int imap_sync_mailbox (CONTEXT *ctx, int expunge); int imap_close_mailbox (CONTEXT *ctx); int imap_buffy_check (int force, int check_stats); int imap_status (char *path, int queue); diff --git a/imap/imap_private.h b/imap/imap_private.h index 67fea27e4..79f75e2ee 100644 --- a/imap/imap_private.h +++ b/imap/imap_private.h @@ -226,11 +226,9 @@ typedef struct } IMAP_DATA; /* I wish that were called IMAP_CONTEXT :( */ -/* -- macros -- */ -#define CTX_DATA ((IMAP_DATA *) ctx->data) - /* -- private IMAP functions -- */ /* imap.c */ +int imap_check (IMAP_DATA* idata, int force); int imap_create_mailbox (IMAP_DATA* idata, char* mailbox); int imap_rename_mailbox (IMAP_DATA* idata, IMAP_MBOX* mx, const char* newname); IMAP_STATUS* imap_mboxcache_get (IMAP_DATA* idata, const char* mbox, diff --git a/imap/message.c b/imap/message.c index 1b28d16a9..2e014b682 100644 --- a/imap/message.c +++ b/imap/message.c @@ -413,7 +413,7 @@ int imap_fetch_message (CONTEXT *ctx, MESSAGE *msg, int msgno) * fails. Thanks Sam. */ short fetched = 0; - idata = (IMAP_DATA*) ctx->data; + idata = ctx->data; h = ctx->hdrs[msgno]; if ((msg->fp = msg_cache_get (idata, h))) @@ -629,7 +629,7 @@ int imap_append_message (CONTEXT *ctx, MESSAGE *msg) IMAP_MBOX mx; int rc; - idata = (IMAP_DATA*) ctx->data; + idata = ctx->data; if (imap_parse_path (ctx->path, &mx)) return -1; @@ -768,7 +768,7 @@ int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete) int err_continue = MUTT_NO; int triedcreate = 0; - idata = (IMAP_DATA*) ctx->data; + idata = ctx->data; if (imap_parse_path (dest, &mx)) { @@ -777,7 +777,7 @@ int imap_copy_messages (CONTEXT* ctx, HEADER* h, char* dest, int delete) } /* check that the save-to folder is in the same account */ - if (!mutt_account_match (&(CTX_DATA->conn->account), &(mx.account))) + if (!mutt_account_match (&(idata->conn->account), &(mx.account))) { dprint (3, (debugfile, "imap_copy_messages: %s not same server as %s\n", dest, ctx->path)); @@ -994,7 +994,7 @@ int imap_cache_del (IMAP_DATA* idata, HEADER* h) static int msg_cache_clean_cb (const char* id, body_cache_t* bcache, void* data) { unsigned int uv, uid, n; - IMAP_DATA* idata = (IMAP_DATA*)data; + IMAP_DATA* idata = data; if (sscanf (id, "%u-%u", &uv, &uid) != 2) return 0; @@ -1108,7 +1108,7 @@ static int msg_fetch_header (CONTEXT* ctx, IMAP_HEADER* h, char* buf, FILE* fp) long bytes; int rc = -1; /* default now is that string isn't FETCH response*/ - idata = (IMAP_DATA*) ctx->data; + idata = ctx->data; if (buf[0] != '*') return rc; diff --git a/imap/util.c b/imap/util.c index c2ae103bb..8eb7a1e0d 100644 --- a/imap/util.c +++ b/imap/util.c @@ -844,43 +844,20 @@ static void alrm_handler (int sig) void imap_keepalive (void) { CONNECTION *conn; - CONTEXT *ctx = NULL; IMAP_DATA *idata; + time_t now = time(NULL); - conn = mutt_socket_head (); - while (conn) + for (conn = mutt_socket_head(); conn; conn = conn->next) { if (conn->account.type == MUTT_ACCT_TYPE_IMAP) { - int need_free = 0; - - idata = (IMAP_DATA*) conn->data; - + idata = conn->data; if (idata->state >= IMAP_AUTHENTICATED - && time(NULL) >= idata->lastread + ImapKeepalive) + && now >= idata->lastread + ImapKeepalive) { - if (idata->ctx) - ctx = idata->ctx; - else - { - ctx = safe_calloc (1, sizeof (CONTEXT)); - ctx->data = idata; - /* imap_close_mailbox will set ctx->iadata->ctx to NULL, so we can't - * rely on the value of iadata->ctx to determine if this placeholder - * context needs to be freed. - */ - need_free = 1; - } - /* if the imap connection closes during this call, ctx may be invalid - * after this point, and thus should not be read. - */ - imap_check_mailbox (ctx, NULL, 1); - if (need_free) - FREE (&ctx); + imap_check(idata, 1); } } - - conn = conn->next; } } @@ -932,14 +909,16 @@ int imap_wait_keepalive (pid_t pid) void imap_allow_reopen (CONTEXT *ctx) { - if (ctx && ctx->magic == MUTT_IMAP && CTX_DATA->ctx == ctx) - CTX_DATA->reopen |= IMAP_REOPEN_ALLOW; + IMAP_DATA* idata = ctx->data; + if (ctx && ctx->magic == MUTT_IMAP && idata->ctx == ctx) + idata->reopen |= IMAP_REOPEN_ALLOW; } void imap_disallow_reopen (CONTEXT *ctx) { - if (ctx && ctx->magic == MUTT_IMAP && CTX_DATA->ctx == ctx) - CTX_DATA->reopen &= ~IMAP_REOPEN_ALLOW; + IMAP_DATA* idata = ctx->data; + if (ctx && ctx->magic == MUTT_IMAP && idata->ctx == ctx) + idata->reopen &= ~IMAP_REOPEN_ALLOW; } int imap_account_match (const ACCOUNT* a1, const ACCOUNT* a2) diff --git a/mx.c b/mx.c index 7666e5408..a8899b57c 100644 --- a/mx.c +++ b/mx.c @@ -1018,7 +1018,7 @@ int mx_close_mailbox (CONTEXT *ctx, int *index_hint) /* allow IMAP to preserve the deleted flag across sessions */ if (ctx->magic == MUTT_IMAP) { - if ((check = imap_sync_mailbox (ctx, purge, index_hint)) != 0) + if ((check = imap_sync_mailbox (ctx, purge)) != 0) { ctx->closing = 0; return check; @@ -1242,7 +1242,7 @@ int mx_sync_mailbox (CONTEXT *ctx, int *index_hint) #ifdef USE_IMAP if (ctx->magic == MUTT_IMAP) - rc = imap_sync_mailbox (ctx, purge, index_hint); + rc = imap_sync_mailbox (ctx, purge); else #endif rc = sync_mailbox (ctx, index_hint);