cmd-select.c revision 878d69326d44013f83641d7a81ed8ae3e4996f15
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Copyright (C) 2002 Timo Sirainen */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "common.h"
18398a5d21c88cbb34c601c6b6c1f9dea502e1caTimo Sirainen#include "commands.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "imap-sync.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenint _cmd_select_full(struct client_command_context *cmd, int readonly)
7e235b3a5f622813121cd18f351e036650aaf8f8Timo Sirainen{
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen struct client *client = cmd->client;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen struct mail_storage *storage;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen struct mailbox *box;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen struct mailbox_status status;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen const char *mailbox;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen /* <mailbox> */
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if (!client_read_string_args(cmd, 1, &mailbox))
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen return FALSE;
7e235b3a5f622813121cd18f351e036650aaf8f8Timo Sirainen
7e235b3a5f622813121cd18f351e036650aaf8f8Timo Sirainen if (client->mailbox != NULL) {
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen box = client->mailbox;
fadd878cd6098f5b873c21c121209a922679dae4Timo Sirainen client->mailbox = NULL;
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen storage = mailbox_get_storage(box);
4b231ca0bbe3b536acbd350101e183441ce0247aTimo Sirainen if (mailbox_close(box) < 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_send_untagged_storage_error(client, storage);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen storage = client_find_storage(cmd, &mailbox);
bbce20cb4e5739e9a06058cf8ee1f38a7f6884f6Timo Sirainen if (storage == NULL)
bbce20cb4e5739e9a06058cf8ee1f38a7f6884f6Timo Sirainen return TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen box = mailbox_open(storage, mailbox, NULL, !readonly ? 0 :
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (MAILBOX_OPEN_READONLY | MAILBOX_OPEN_KEEP_RECENT));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (box == NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_send_storage_error(cmd, storage);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
55a210942dc7da58b2fd0b11bed8da6b030af5c1Timo Sirainen
fc7b17677ac1a5fa3f7fe13d5ef7dcfea8d9b4a1Timo Sirainen if (imap_sync_nonselected(box, MAILBOX_SYNC_FLAG_FULL_READ) < 0) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_send_storage_error(cmd, storage);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen mailbox_close(box);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (mailbox_get_status(box, STATUS_MESSAGES | STATUS_RECENT |
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen STATUS_FIRST_UNSEEN_SEQ | STATUS_UIDVALIDITY |
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen STATUS_UIDNEXT | STATUS_KEYWORDS,
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen &status) < 0) {
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen client_send_storage_error(cmd, storage);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen mailbox_close(box);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen return TRUE;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen }
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen client_save_keywords(&client->keywords, status.keywords);
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen client->messages_count = status.messages;
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen client->recent_count = status.recent;
d9fdacd5fb3e07997e5c389739d2054f0c8441d8Timo Sirainen
44ff75ca53188056ff5a3e50428e3f2078800b3cTimo Sirainen /* set client's mailbox only after getting status to make sure
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen we're not sending any expunge/exists replies too early to client */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client->mailbox = box;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client->select_counter++;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_send_mailbox_flags(client, box, status.keywords);
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
45e62043058738e294f89504c319d852e25943ccTimo Sirainen client_send_line(client,
45e62043058738e294f89504c319d852e25943ccTimo Sirainen t_strdup_printf("* %u EXISTS", status.messages));
45e62043058738e294f89504c319d852e25943ccTimo Sirainen client_send_line(client,
45e62043058738e294f89504c319d852e25943ccTimo Sirainen t_strdup_printf("* %u RECENT", status.recent));
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen if (status.first_unseen_seq != 0) {
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen client_send_line(client,
d5960ce1c0adda5c9e259bc429123ebc29c60baeTimo Sirainen t_strdup_printf("* OK [UNSEEN %u] First unseen.",
d5960ce1c0adda5c9e259bc429123ebc29c60baeTimo Sirainen status.first_unseen_seq));
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen }
13c6532dc104d23061e6901783ceb1ff8872c206Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen client_send_line(client,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen t_strdup_printf("* OK [UIDVALIDITY %u] UIDs valid",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen status.uidvalidity));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
6eb30032b4a50c383dea4c9c74342d906de6ad36Timo Sirainen client_send_line(client,
6eb30032b4a50c383dea4c9c74342d906de6ad36Timo Sirainen t_strdup_printf("* OK [UIDNEXT %u] Predicted next UID",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen status.uidnext));
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen client_send_tagline(cmd, mailbox_is_readonly(box) ?
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen "OK [READ-ONLY] Select completed." :
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen "OK [READ-WRITE] Select completed.");
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen return TRUE;
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen}
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainenint cmd_select(struct client_command_context *cmd)
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen{
2a6af811ea3de3cf9e2f15e446674dd21b0705f3Timo Sirainen return _cmd_select_full(cmd, FALSE);
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen}
4bc96ba6f1d67a90a75fa131bcd2cd508ea5a05aTimo Sirainen