cmd-select.c revision bd74402ca1a39ec303075fefb1212d7e18a71531
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen/* Copyright (C) 2002 Timo Sirainen */
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "common.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "commands.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen#include "imap-sync.h"
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainenbool _cmd_select_full(struct client_command_context *cmd, bool readonly)
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen{
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen struct client *client = cmd->client;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen struct mail_storage *storage;
18565c69efcd7db003dbf27cf625ed822e889fb1Timo Sirainen struct mailbox *box;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen struct mailbox_status status;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen const char *mailbox;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen /* <mailbox> */
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen if (!client_read_string_args(cmd, 1, &mailbox))
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen return FALSE;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
af1f4b17a92ca7b2661737e65c7849df289d3070Timo Sirainen if (client->mailbox != NULL) {
af1f4b17a92ca7b2661737e65c7849df289d3070Timo Sirainen box = client->mailbox;
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainen client->mailbox = NULL;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen storage = mailbox_get_storage(box);
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainen if (mailbox_close(&box) < 0)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen client_send_untagged_storage_error(client, storage);
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen }
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen storage = client_find_storage(cmd, &mailbox);
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen if (storage == NULL)
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen return TRUE;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen box = mailbox_open(storage, mailbox, NULL, !readonly ? 0 :
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen (MAILBOX_OPEN_READONLY | MAILBOX_OPEN_KEEP_RECENT));
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen if (box == NULL) {
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen client_send_storage_error(cmd, storage);
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen return TRUE;
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen }
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen if (imap_sync_nonselected(box, MAILBOX_SYNC_FLAG_FULL_READ) < 0) {
d30da25fb6be1f1c667d93767c9194000194b618Timo Sirainen client_send_storage_error(cmd, storage);
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen mailbox_close(&box);
af1f4b17a92ca7b2661737e65c7849df289d3070Timo Sirainen return TRUE;
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen }
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen if (mailbox_get_status(box, STATUS_MESSAGES | STATUS_RECENT |
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen STATUS_FIRST_UNSEEN_SEQ | STATUS_UIDVALIDITY |
63a61b7a739ae0f3f520215137d9c50f94d0f34fTimo Sirainen STATUS_UIDNEXT | STATUS_KEYWORDS,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen &status) < 0) {
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen client_send_storage_error(cmd, storage);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen mailbox_close(&box);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen return TRUE;
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen }
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen client_save_keywords(&client->keywords, status.keywords);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen client->messages_count = status.messages;
01cbf4ac5d44137ab434791be7f838d98d0fcf3bTimo Sirainen client->recent_count = status.recent;
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen client->uidvalidity = status.uidvalidity;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen /* set client's mailbox only after getting status to make sure
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen we're not sending any expunge/exists replies too early to client */
af1f4b17a92ca7b2661737e65c7849df289d3070Timo Sirainen client->mailbox = box;
af1f4b17a92ca7b2661737e65c7849df289d3070Timo Sirainen client->select_counter++;
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen client_send_mailbox_flags(client, box, status.keywords);
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen client_send_line(client,
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen t_strdup_printf("* %u EXISTS", status.messages));
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen client_send_line(client,
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen t_strdup_printf("* %u RECENT", status.recent));
7f773564b94e6054a40d3785cb63c29f1e4d4deeTimo Sirainen
f1901fd21906911f7be075c965ac882f6a87b4c3Timo Sirainen if (status.first_unseen_seq != 0) {
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen client_send_line(client,
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen t_strdup_printf("* OK [UNSEEN %u] First unseen.",
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen status.first_unseen_seq));
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen }
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen client_send_line(client,
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen t_strdup_printf("* OK [UIDVALIDITY %u] UIDs valid",
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen status.uidvalidity));
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen client_send_line(client,
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen t_strdup_printf("* OK [UIDNEXT %u] Predicted next UID",
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen status.uidnext));
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen client_send_tagline(cmd, mailbox_is_readonly(box) ?
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen "OK [READ-ONLY] Select completed." :
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen "OK [READ-WRITE] Select completed.");
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen return TRUE;
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen}
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainenbool cmd_select(struct client_command_context *cmd)
8fcff4c5b52f24d9c681805fdf06b486f1d0fcbeTimo Sirainen{
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen return _cmd_select_full(cmd, FALSE);
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen}
992a13add4eea0810e4db0f042a595dddf85536aTimo Sirainen