]> granicus.if.org Git - neomutt/commitdiff
Change imap_conn_find() to always return an authenticated conn.
authorKevin McCarthy <kevin@8t8.us>
Thu, 13 Sep 2018 01:23:00 +0000 (18:23 -0700)
committerRichard Russon <rich@flatcap.org>
Tue, 25 Sep 2018 12:23:50 +0000 (13:23 +0100)
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().

imap/imap.c

index eb46f7f8e58e5a74528692038e0d8c227e9aba33..ed88c06407150f77ea2cec510bbadcaef4a05ebb 100644 (file)
@@ -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;
 }