+/* queues command. If the queue is full, attempts to drain it. */
+static int cmd_queue (IMAP_DATA* idata, const char* cmdstr)
+{
+ IMAP_COMMAND* cmd;
+ int rc;
+
+ if (cmd_queue_full (idata))
+ {
+ dprint (3, (debugfile, "Draining IMAP command pipeline\n"));
+
+ rc = imap_exec (idata, NULL, IMAP_CMD_FAIL_OK);
+
+ if (rc < 0 && rc != -2)
+ return rc;
+ }
+
+ if (!(cmd = cmd_new (idata)))
+ return IMAP_CMD_BAD;
+
+ if (mutt_buffer_printf (idata->cmdbuf, "%s %s\r\n", cmd->seq, cmdstr) < 0)
+ return IMAP_CMD_BAD;
+
+ return 0;
+}
+
+static int cmd_start (IMAP_DATA* idata, const char* cmdstr, int flags)
+{
+ int rc;
+
+ if (idata->status == IMAP_FATAL)
+ {
+ cmd_handle_fatal (idata);
+ return -1;
+ }
+
+ if (cmdstr && ((rc = cmd_queue (idata, cmdstr)) < 0))
+ return rc;
+
+ if (flags & IMAP_CMD_QUEUE)
+ return 0;
+
+ if (idata->cmdbuf->dptr == idata->cmdbuf->data)
+ return IMAP_CMD_BAD;
+
+ rc = mutt_socket_write_d (idata->conn, idata->cmdbuf->data, -1,
+ flags & IMAP_CMD_PASS ? IMAP_LOG_PASS : IMAP_LOG_CMD);
+ idata->cmdbuf->dptr = idata->cmdbuf->data;
+
+ /* unidle when command queue is flushed */
+ if (idata->state == IMAP_IDLE)
+ idata->state = IMAP_SELECTED;
+
+ return (rc < 0) ? IMAP_CMD_BAD : 0;
+}
+