cmd-select.c revision 115cf0320f679e4e63db25e7f44f47977b8338de
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen/* Copyright (c) 2002-2015 Dovecot authors, see the included COPYING file */
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainenstatic int select_qresync_get_uids(struct imap_select_context *ctx,
d5cebe7f98e63d4e2822863ef2faa4971e8b3a5dTimo Sirainen /* change all n:m ranges to n,m and store the results */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_array_init(&ctx->qresync_sample_uidset, uid_count);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen i_array_init(&ctx->qresync_sample_seqset, uid_count);
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen for (i = 0; i < uid_count; i++) {
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen if (!seq_range_array_iter_nth(&seq_iter, n++, &seq))
5ac0b0bf32898c63da086ae169674ecac151a31eTimo Sirainen array_append(&ctx->qresync_sample_seqset, &seq, 1);
c28f6aa0b70af4811c9ace9114fe827c2f503455Timo Sirainen if (!seq_range_array_iter_nth(&seq_iter, n++, &seq))
c28f6aa0b70af4811c9ace9114fe827c2f503455Timo Sirainen array_append(&ctx->qresync_sample_seqset, &seq, 1);
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen if (seq_range_array_iter_nth(&seq_iter, n, &seq))
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenselect_parse_qresync_known_set(struct imap_select_context *ctx,
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen const char *str;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen imap_seq_set_nostar_parse(str, &seqset) < 0) {
7662010b03ffe5f2a6ecf4b4eb220d1c65efea76Timo Sirainen "Invalid QRESYNC known-sequence-set");
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen imap_seq_set_nostar_parse(str, &uidset) < 0) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen "Invalid QRESYNC known-uid-set");
0a49b316fc729e5d57268ffa63c7122ac73f994cTimo Sirainen if (select_qresync_get_uids(ctx, &seqset, &uidset) < 0) {
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen client_send_command_error(ctx->cmd, "Invalid QRESYNC sets");
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen "Too many parameters to QRESYNC known set");
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainenselect_parse_qresync(struct imap_select_context *ctx,
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainen const char *str;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen unsigned int count;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen client_send_command_error(ctx->cmd, "QRESYNC not enabled");
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (!imap_arg_get_list_full(args, &args, &count)) {
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen "QRESYNC parameters missing");
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen str_to_uint32(str, &ctx->qresync_uid_validity) < 0 ||
0892446b45c195461bb7be6599f02d97e1e2c9b2Timo Sirainen str_to_uint64(str, &ctx->qresync_modseq) < 0) {
0a0cd45a633112a2ae6aad801c1e6afe53ab95deTimo Sirainen "Invalid QRESYNC parameters");
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen if (imap_seq_set_nostar_parse(str, &ctx->qresync_known_uids) < 0) {
d66ef20c30fee728899ee168c75fcc5ff8fbdac1Timo Sirainen "Invalid QRESYNC known-uids");
0a0cd45a633112a2ae6aad801c1e6afe53ab95deTimo Sirainen seq_range_array_add_range(&ctx->qresync_known_uids,
e4c81823af1fc43ca3f2ce9eb4af7fc8f57b13a5Timo Sirainen if (!select_parse_qresync_known_set(ctx, list_args))
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen "Invalid QRESYNC parameters");
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainenselect_parse_options(struct imap_select_context *ctx,
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen "SELECT options contain non-atoms.");
61e6367a259e2473f33df42fda8ceeb3b8b48416Timo Sirainen "Unknown FETCH modifier");
c0435c854a0e7246373b9752d163095cc4fbe985Timo Sirainenstatic void select_context_free(struct imap_select_context *ctx)
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen if (array_is_created(&ctx->qresync_known_uids))
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (array_is_created(&ctx->qresync_sample_seqset))
20344c0e814139e3c365fbb9287478f91512089eTimo Sirainen if (array_is_created(&ctx->qresync_sample_uidset))
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainenstatic void cmd_select_finish(struct imap_select_context *ctx, int ret)
c09f9f95db314e7482c95e502e1c56ed6c555797Timo Sirainen client_send_tagline(ctx->cmd, t_strdup_printf(
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ctx->cmd->client->mailbox_examined ? "Examine" : "Select"));
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainenstatic bool cmd_select_continue(struct client_command_context *cmd)
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen struct imap_select_context *ctx = cmd->context;
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen if (imap_fetch_more(ctx->fetch_ctx, cmd) == 0) {
e3fc1874694a8ddba9552ec23f9952f74f33d1d5Timo Sirainen /* unfinished */
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainenstatic int select_qresync(struct imap_select_context *ctx)
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen search_args->args = p_new(search_args->pool, struct mail_search_arg, 1);
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen search_args->args->value.seqset = ctx->qresync_known_uids;
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen imap_search_add_changed_since(search_args, ctx->qresync_modseq);
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen memset(&qresync_args, 0, sizeof(qresync_args));
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen qresync_args.qresync_sample_seqset = &ctx->qresync_sample_seqset;
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen qresync_args.qresync_sample_uidset = &ctx->qresync_sample_uidset;
602a0434db30d8e3292d1c161a803d96a879a74fTimo Sirainen if (imap_fetch_send_vanished(ctx->cmd->client, ctx->box,
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen fetch_ctx = imap_fetch_alloc(ctx->cmd->client, ctx->cmd->pool);
dd62b77c932d1b518f2a3e4bf80e36542becc256Timo Sirainen imap_fetch_init_nofail_handler(fetch_ctx, imap_fetch_uid_init);
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen imap_fetch_init_nofail_handler(fetch_ctx, imap_fetch_flags_init);
c9bf63e9094761767a63ac6b189bcf60bcffdc44Timo Sirainen imap_fetch_init_nofail_handler(fetch_ctx, imap_fetch_modseq_init);
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen imap_fetch_begin(fetch_ctx, ctx->box, search_args);
4334b9b032298defd4d3906f5357698ff016ead0Timo Sirainen if (imap_fetch_more(fetch_ctx, ctx->cmd) == 0) {
07e4875d250e7a7157cd99132aafc773cf3cdf83Timo Sirainen /* unfinished */
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen ctx->cmd->state = CLIENT_COMMAND_STATE_WAIT_OUTPUT;
4b9f99761df5014c659cd87fddaf6854af428cfcTimo Sirainenselect_open(struct imap_select_context *ctx, const char *mailbox, bool readonly)
7e1f68ad71d3485f1882142837b01f7a98ca8467Timo Sirainen ctx->box = mailbox_alloc(ctx->ns->list, mailbox, flags);
a3c197999dfe2b0c8ea38cb77cfa5e95026005c0Timo Sirainen ret = mailbox_enable(ctx->box, client->enabled_features);
a3c197999dfe2b0c8ea38cb77cfa5e95026005c0Timo Sirainen mailbox_sync(ctx->box, MAILBOX_SYNC_FLAG_FULL_READ) < 0) {
923115fd382904fa13bb09bf307bf2835b52df60Timo Sirainen mailbox_get_open_status(ctx->box, STATUS_MESSAGES | STATUS_RECENT |
923115fd382904fa13bb09bf307bf2835b52df60Timo Sirainen STATUS_FIRST_UNSEEN_SEQ | STATUS_UIDVALIDITY |
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen client_update_mailbox_flags(client, status.keywords);
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen t_strdup_printf("* %u EXISTS", status.messages));
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen t_strdup_printf("* %u RECENT", status.recent));
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen t_strdup_printf("* OK [UNSEEN %u] First unseen.",
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen t_strdup_printf("* OK [UIDVALIDITY %u] UIDs valid",
e0c3d5460d1cc0c440cb7723c8c2eef8d0afe9b9Timo Sirainen t_strdup_printf("* OK [UIDNEXT %u] Predicted next UID",
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen client->nonpermanent_modseqs = status.nonpermanent_modseqs;
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen "* OK [NOMODSEQ] No permanent modsequences");
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen t_strdup_printf("* OK [HIGHESTMODSEQ %llu] Highest",
51e1a1c280ccb461a15827f7987d09cb9708b6e3Timo Sirainen client->sync_last_full_modseq = status.highest_modseq;
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen if (ctx->qresync_uid_validity == status.uidvalidity &&
252db51b6c0a605163326b3ea5d09e9936ca3b29Timo Sirainen status.uidvalidity != 0 && !client->nonpermanent_modseqs) {
7b42d6cbee8186195d8c5e66078043a0fa1f25c1Timo Sirainenstatic void close_selected_mailbox(struct client *client)
89e195dfb5c4b0efd9b9f459771a4467674e5b1fTimo Sirainen /* CLOSED response is required by QRESYNC */
1036ad17ac837a451f6b045cac504d3efa2edb8eTimo Sirainen client_send_line(client, "* OK [CLOSED] Previous mailbox closed.");
1036ad17ac837a451f6b045cac504d3efa2edb8eTimo Sirainenbool cmd_select_full(struct client_command_context *cmd, bool readonly)
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* <mailbox> [(optional parameters)] */
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen client_send_command_error(cmd, "Invalid arguments.");
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen ctx = p_new(cmd->pool, struct imap_select_context, 1);
3785910c303507db5f629684e6dde2cc7f83668eTimo Sirainen ctx->ns = client_find_namespace_full(cmd->client, &mailbox, &error);
3785910c303507db5f629684e6dde2cc7f83668eTimo Sirainen if (imap_arg_get_list(&args[1], &list_args)) {
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen i_assert(client->mailbox_change_lock == NULL);
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen /* Enable while no mailbox is opened to avoid sending
ecc81625167ed96c04c02aa190a1ea5baa65b474Timo Sirainen HIGHESTMODSEQ for previously opened mailbox */
c06f4017027263cf3a08becc551f5126409e2a83Timo Sirainen (void)client_enable(client, MAILBOX_FEATURE_CONDSTORE);