]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
lib-imap-client: Track connection's selected mailbox
authorMarco Bettini <marco.bettini@open-xchange.com>
Thu, 13 Nov 2025 11:26:46 +0000 (11:26 +0000)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Mon, 24 Nov 2025 12:20:11 +0000 (12:20 +0000)
src/lib-imap-client/imapc-client.c
src/lib-imap-client/imapc-client.h
src/lib-imap-client/imapc-connection.c
src/lib-imap-client/imapc-connection.h

index 054f0c57f9108556a15e18019503701cc9118ae2..4afa1fcba300e6e94e4038bc4b01b6452eb97f77 100644 (file)
@@ -277,6 +277,14 @@ imapc_client_cmd(struct imapc_client *client,
        return imapc_connection_cmd(conn, callback, context);
 }
 
+bool imapc_client_is_server_selected(struct imapc_client *client,
+                                    const char *name)
+{
+       struct imapc_connection *conn = imapc_client_find_connection(client);
+       const char *conn_name = imapc_connection_get_selected_mailbox_name(conn);
+       return null_strcmp(conn_name, name) == 0;
+}
+
 static struct imapc_client_connection *
 imapc_client_get_unboxed_connection(struct imapc_client *client)
 {
index b7ccd89db943fd57ad3f01d000530cbe2a855b4c..34ab1a2d75313b29b633853b4a8bc9ce732f1952 100644 (file)
@@ -212,5 +212,7 @@ void imapc_client_register_state_change_callback(struct imapc_client *client,
                                                 void *context);
 
 bool imapc_client_is_ssl(struct imapc_client *client);
+bool imapc_client_is_server_selected(struct imapc_client *client,
+                                    const char *name);
 
 #endif
index e0cc616e5e7b93615e3a4d772307902eaa710e6e..fc6dbd357a998f564873e2218bca16f7ec9f020c 100644 (file)
@@ -158,10 +158,10 @@ struct imapc_connection {
        bool idle_plus_waiting:1;
        bool imap4rev2_enabled:1;
        bool select_waiting_reply:1;
-       /* TRUE if IMAP server is in SELECTED state. select_box may be NULL
+       /* The name of the folder if in SELECTED state. select_box may be NULL
           though, if we already closed the mailbox from client point of
           view. */
-       bool selected_on_server:1;
+       char *selected_on_server;
 };
 
 static void imapc_connection_capability_cb(const struct imapc_command_reply *reply,
@@ -262,6 +262,7 @@ static void imapc_connection_unref(struct imapc_connection **_conn)
        array_free(&conn->aborted_cmd_tags);
        imapc_client_unref(&conn->client);
        event_unref(&conn->event);
+       i_free(conn->selected_on_server);
        i_free(conn->ips);
        i_free(conn);
 }
@@ -422,7 +423,7 @@ static void imapc_connection_set_state(struct imapc_connection *conn,
                conn->select_waiting_reply = FALSE;
                conn->qresync_selecting_box = NULL;
                conn->selected_box = NULL;
-               conn->selected_on_server = FALSE;
+               i_free_and_null(conn->selected_on_server);
                /* fall through */
        case IMAPC_CONNECTION_STATE_DONE:
                /* if we came from imapc_client_get_capabilities(), stop so
@@ -838,7 +839,7 @@ imapc_connection_handle_resp_text_code(struct imapc_connection *conn,
                        conn->selected_box = conn->qresync_selecting_box;
                        conn->qresync_selecting_box = NULL;
                } else {
-                       conn->selected_on_server = FALSE;
+                       i_free_and_null(conn->selected_on_server);
                }
        }
        return 0;
@@ -2218,7 +2219,7 @@ static void imapc_connection_set_selecting(struct imapc_client_mailbox *box)
 
        i_assert(conn->qresync_selecting_box == NULL);
 
-       if (conn->selected_on_server &&
+       if (conn->selected_on_server != NULL &&
            (conn->capabilities & IMAPC_CAPABILITY_QRESYNC) != 0) {
                /* server will send a [CLOSED] once selected mailbox is
                   closed */
@@ -2226,8 +2227,9 @@ static void imapc_connection_set_selecting(struct imapc_client_mailbox *box)
        } else {
                /* we'll have to assume that all the future untagged messages
                   are for the mailbox we're selecting */
+               i_free_and_null(conn->selected_on_server);
+               conn->selected_on_server = i_strdup(box->name);
                conn->selected_box = box;
-               conn->selected_on_server = TRUE;
        }
        conn->select_waiting_reply = TRUE;
 }
@@ -2605,7 +2607,7 @@ void imapc_connection_mailbox_closed(struct imapc_client_mailbox *box,
                conn->qresync_selecting_box = NULL;
                conn->selected_box = NULL;
                if (via_tagged_reply)
-                       conn->selected_on_server = FALSE;
+                       i_free_and_null(conn->selected_on_server);
                else {
                        /* We didn't actually send UNSELECT command, so don't
                           touch selected_on_server state. */
@@ -2684,3 +2686,9 @@ struct timeval imapc_command_get_start_time(struct imapc_command *cmd)
 {
        return cmd->start_time;
 }
+
+const char *
+imapc_connection_get_selected_mailbox_name(struct imapc_connection *conn)
+{
+       return conn->selected_on_server;
+}
index f3fbafe8919b651ef498dee37b7194b42dc89066..4e1b5f46cbc5eccf31015842fbdb6ba86d2f0231 100644 (file)
@@ -61,5 +61,7 @@ imapc_connection_get_mailbox(struct imapc_connection *conn);
 
 void imapc_connection_idle(struct imapc_connection *conn);
 struct event *imapc_connection_get_event(struct imapc_connection *conn);
+const char *
+imapc_connection_get_selected_mailbox_name(struct imapc_connection *conn);
 
 #endif