From: Kevin McCarthy Date: Thu, 13 Sep 2018 01:23:00 +0000 (-0700) Subject: Change imap_conn_find() to always return an authenticated conn. X-Git-Tag: 2019-10-25~635^2~5 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d031f408163ff787b0222fd324fd170e73249103;p=neomutt Change imap_conn_find() to always return an authenticated conn. With the flag MUTT_IMAP_CONN_NONEW, it was already ensured the connection would be authenticated. However, without that flag, an error in opening the connection or authentication would still return an idata. The callers that didn't bother to check the state were still assuming authenticated, because they were all subseqeuently issuing an "authenticated state" command to the server. Rather than add state checks to every caller, just change the function to return NULL if the idata did not end up in an authenticated state. Remove the now redundant state checks in imap_open_mailbox() and imap_get_mailbox(). --- diff --git a/imap/imap.c b/imap/imap.c index eb46f7f8e..ed88c0640 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -343,8 +343,9 @@ static int get_mailbox(const char *path, struct ImapMboxData **hidata, char *buf mutt_debug(1, "Error parsing %s\n", path); return -1; } - if (!(*hidata = imap_conn_find(&(mx.account), ImapPassive ? MUTT_IMAP_CONN_NONEW : 0)) || - (*hidata)->state < IMAP_AUTHENTICATED) + + *hidata = imap_conn_find(&(mx.account), ImapPassive ? MUTT_IMAP_CONN_NONEW : 0); + if (!*hidata) { FREE(&mx.mbox); return -1; @@ -912,8 +913,8 @@ void imap_expunge_mailbox(struct ImapMboxData *mdata) * imap_conn_find - Find an open IMAP connection * @param account ConnAccount to search * @param flags Flags, e.g. #MUTT_IMAP_CONN_NONEW - * @retval ptr Matching connection - * @retval NULL Failure + * @retval ptr Authenticated connection + * @retval NULL Failure, or no matching authenticated connections * * Find an open IMAP connection matching account, or open a new one if none can * be found. @@ -953,16 +954,11 @@ struct ImapMboxData *imap_conn_find(const struct ConnAccount *account, int flags if (!conn) return NULL; /* this happens when the initial connection fails */ + /* The current connection is a new connection */ if (!mdata) { /* The current connection is a new connection */ mdata = imap_mdata_new(); - if (!mdata) - { - mutt_socket_free(conn); - return NULL; - } - conn->data = mdata; mdata->conn = conn; new = true; @@ -1009,6 +1005,9 @@ struct ImapMboxData *imap_conn_find(const struct ConnAccount *account, int flags imap_exec(mdata, NULL, IMAP_CMD_FAIL_OK); } + if (mdata->state < IMAP_AUTHENTICATED) + return NULL; + return mdata; }