From aa8246b973dbc27754d889fa4235cdaa510f2f53 Mon Sep 17 00:00:00 2001
From: Brendan Cully <brendan@kublai.com>
Date: Tue, 3 Jan 2006 17:28:51 +0000
Subject: [PATCH] Pipeline postponed mail check on mailbox open if appropriate.

---
 imap/imap.c | 43 ++++++++++++++++++++++++-------------------
 imap/imap.h |  2 +-
 postpone.c  |  2 +-
 3 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/imap/imap.c b/imap/imap.c
index 7e3fc3465..d5826b577 100644
--- a/imap/imap.c
+++ b/imap/imap.c
@@ -542,7 +542,7 @@ int imap_open_mailbox (CONTEXT* ctx)
   char buf[LONG_STRING];
   char bufout[LONG_STRING];
   int count = 0;
-  IMAP_MBOX mx;
+  IMAP_MBOX mx, pmx;
   int rc;
   
   if (imap_parse_path (ctx->path, &mx))
@@ -599,7 +599,11 @@ int imap_open_mailbox (CONTEXT* ctx)
     mutt_bit_set (idata->rights, IMAP_ACL_CREATE);
     mutt_bit_set (idata->rights, IMAP_ACL_DELETE);
   }
-  
+  /* pipeline the postponed count if possible */
+  if (mx_is_imap (Postponed) && !imap_parse_path (Postponed, &pmx)
+      && mutt_account_match (&pmx.account, &mx.account))
+    imap_status (Postponed, 1);
+
   snprintf (bufout, sizeof (bufout), "%s %s",
     ctx->readonly ? "EXAMINE" : "SELECT", buf);
 
@@ -1495,15 +1499,17 @@ int imap_buffy_check (int force)
   return buffies;
 }
 
-/* imap_status: returns count of messages in mailbox, or -1 on error */
-int imap_status (char* path)
+/* imap_status: returns count of messages in mailbox, or -1 on error.
+ * if queue != 0, queue the command and expect it to have been run
+ * on the next call (for pipelining the postponed count) */
+int imap_status (char* path, int queue)
 {
+  static int queued = 0;
+
   IMAP_DATA *idata;
   char buf[LONG_STRING];
   char mbox[LONG_STRING];
-  IMAP_STATUS status;
-  int rc;
-  int messages = 0;
+  IMAP_STATUS* status;
 
   if (imap_get_mailbox (path, &idata, buf, sizeof (buf)) < 0)
     return -1;
@@ -1523,21 +1529,20 @@ int imap_status (char* path)
      * There is no lightweight way to check recent arrivals */
     return -1;
 
-  idata->cmddata = &status;
-  imap_cmd_start (idata, buf);
-  do
+  if (queue)
   {
-    status.name = NULL;
-    if ((rc = imap_cmd_step (idata)) == IMAP_CMD_CONTINUE)
-    {
-      if (status.name && !mutt_strcmp (mbox, status.name))
-        messages = status.messages;
-    }
+    imap_cmd_queue (idata, buf);
+    queued = 1;
+    return 0;
   }
-  while (rc == IMAP_CMD_CONTINUE);
-  idata->cmddata = NULL;
+  else if (!queued)
+    imap_exec (idata, buf, 0);
 
-  return messages;
+  queued = 0;
+  if ((status = imap_mboxcache_get (idata, mbox)))
+    return status->messages;
+  
+  return 0;
 }
 
 /* return cached mailbox stats or NULL */
diff --git a/imap/imap.h b/imap/imap.h
index 145fdfa18..78d931966 100644
--- a/imap/imap.h
+++ b/imap/imap.h
@@ -40,7 +40,7 @@ int imap_open_mailbox_append (CONTEXT *ctx);
 int imap_sync_mailbox (CONTEXT *ctx, int expunge, int *index_hint);
 void imap_close_mailbox (CONTEXT *ctx);
 int imap_buffy_check (int force);
-int imap_status (char *path);
+int imap_status (char *path, int queue);
 int imap_search (CONTEXT* ctx, const pattern_t* pat);
 int imap_subscribe (char *path, int subscribe);
 int imap_complete (char* dest, size_t dlen, char* path);
diff --git a/postpone.c b/postpone.c
index 2795af328..a261d1f30 100644
--- a/postpone.c
+++ b/postpone.c
@@ -89,7 +89,7 @@ int mutt_num_postponed (int force)
     {
       short newpc;
 
-      newpc = imap_status (Postponed);
+      newpc = imap_status (Postponed, 0);
       if (newpc >= 0)
       {
 	PostCount = newpc;
-- 
2.40.0