bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2014-2018 Dovecot authors, see the included COPYING file */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen uint32_t keywords_count, keywords_crc32, uids_crc32;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenexport_seq_range(buffer_t *dest, const ARRAY_TYPE(seq_range) *range)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen for (i = 0; i < count; i++) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_encode(dest, (uids[i].seq1 - next_uid) << 1);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_encode(dest, 1 | ((uids[i].seq1 - next_uid) << 1));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_encode(dest, uids[i].seq2 - uids[i].seq1 - 1);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimport_seq_range(const unsigned char **data, const unsigned char *end,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen for (i = 0; i < count; i++) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenint imap_state_export_internal(struct client *client, buffer_t *dest,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const char **error_r)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* the only IMAP command we allow running is IDLE or X-STATE */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen strcasecmp(client->command_queue->name, "IDLE") != 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* this would require saving the seq <-> uid mapping
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen and restore it on import. quite a lot of trouble if
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen messages have been expunged in the mean time. */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen *error_r = "Non-IDLE connections not supported currently";
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return client->v.state_export(client, TRUE, dest, error_r);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenint imap_state_export_external(struct client *client, buffer_t *dest,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const char **error_r)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen i_assert(strcmp(client->command_queue->name, "X-STATE") == 0);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return client->v.state_export(client, FALSE, dest, error_r);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimap_state_import(struct client *client, bool internal,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const unsigned char *data, size_t size, const char **error_r)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen while (size > 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen ret = client->v.state_import(client, internal,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenint imap_state_import_internal(struct client *client,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const char **error_r)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return imap_state_import(client, TRUE, data, size, error_r);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenint imap_state_import_external(struct client *client,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const char **error_r)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return imap_state_import(client, FALSE, data, size, error_r);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimap_state_export_mailbox_mails(buffer_t *dest, struct mailbox *box,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const char **error_r)
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi trans = mailbox_transaction_begin(box, 0,
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi "unhibernate imap_state_export_mailbox_mails");
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen search_ctx = mailbox_search_init(trans, search_args, NULL, 0, NULL);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen while (mailbox_search_next(search_ctx, &mail)) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen crc = crc32_data_more(crc, &mail->uid, sizeof(mail->uid));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if ((mail_get_flags(mail) & MAIL_RECENT) != 0)
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi *error_r = mailbox_get_last_internal_error(box, NULL);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenmailbox_status_keywords_crc32(const struct mailbox_status *status)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimap_state_export_mailbox(buffer_t *dest, struct client *client,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen mailbox_get_open_status(box, STATUS_UIDVALIDITY | STATUS_UIDNEXT |
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (mailbox_get_metadata(box, MAILBOX_METADATA_GUID, &metadata) < 0) {
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi *error_r = mailbox_get_last_internal_error(box, &mail_error);
f3fa33be556fd5199dcc856b02b58128bdf693a5Timo Sirainen /* if the selected mailbox can't have a GUID, fail silently */
f3fa33be556fd5199dcc856b02b58128bdf693a5Timo Sirainen return mail_error == MAIL_ERROR_NOTPOSSIBLE ? 0 : -1;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen buffer_append_c(dest, IMAP_STATE_TYPE_MAILBOX);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen buffer_append(dest, metadata.guid, sizeof(metadata.guid));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen buffer_append_c(dest, client->mailbox_examined ? 1 : 0);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if ((client->enabled_features & MAILBOX_FEATURE_QRESYNC) != 0 &&
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_encode(dest, client->sync_last_full_modseq);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* keywords count + CRC32 should be enough to figure out if it
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen needs to be resent */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_encode(dest, array_count(status.keywords));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_encode(dest, mailbox_status_keywords_crc32(&status));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* we're now basically done, but just in case there's a bug add a
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen checksum of the currently existing UIDs and verify it when
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen importing. this also writes the list of recent UIDs. */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen return imap_state_export_mailbox_mails(dest, box, error_r);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenint imap_state_export_base(struct client *client, bool internal,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (array_is_created(&client->search_updates) &&
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* these could be tricky */
bda082b4c7d06cd8ed83794318930385673b7eeeTeemu Huovila *error_r = "CONTEXT=SEARCH updates not supported currently";
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* FIXME: this really should be supported. also IDLE wouldn't
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen be needed if NOTIFY allows sending EXPUNGEs to selected
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* IMAP features */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen i_assert((client->enabled_features & ~(MAILBOX_FEATURE_CONDSTORE |
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen buffer_append_c(dest, IMAP_STATE_TYPE_ENABLED_FEATURES);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if ((client->enabled_features & MAILBOX_FEATURE_CONDSTORE) != 0)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen buffer_append_c(dest, IMAP_STATE_FEATURE_CONDSTORE);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if ((client->enabled_features & MAILBOX_FEATURE_QRESYNC) != 0)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen buffer_append_c(dest, IMAP_STATE_FEATURE_QRESYNC);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen buffer_append_c(dest, IMAP_STATE_TYPE_ID_LOGGED);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen buffer_append_c(dest, IMAP_STATE_TYPE_TLS_COMPRESSION);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* IMAP SEARCHRES extension */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (array_is_created(&client->search_saved_uidset) &&
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen array_count(&client->search_saved_uidset) > 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen buffer_append_c(dest, IMAP_STATE_TYPE_SEARCHRES);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen export_seq_range(dest, &client->search_saved_uidset);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimport_string(const unsigned char **data, const unsigned char *end,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const char **str_r)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const unsigned char *p;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const char **error_r)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen ARRAY_TYPE(seq_range) uids_filter, expunged_uids;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* the mailbox was empty originally - there couldn't be any
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen pending expunges. */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* get all the message UIDs expunged since the last known modseq */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen seq_range_array_add_range(&uids_filter, 1, state->uidnext-1);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (!mailbox_get_expunged_uids(client->mailbox, state->highest_modseq,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen "Couldn't get recently expunged UIDs "
47a5a7e8296f3b8f2fac9a0659d4de3f2723ba4aMartti Rannanjärvi "(uidnext=%u highest_modseq=%"PRIu64")",
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen seq_range_array_iter_init(&iter, &expunged_uids);
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi trans = mailbox_transaction_begin(client->mailbox, 0,
0dab9cb35a976c49b28a11e28d5570f5191f1a7aMartti Rannanjärvi "unhibernate import_send_expunges");
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen search_ctx = mailbox_search_init(trans, search_args, NULL, 0, NULL);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* find sequence numbers for the expunged UIDs */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen t_array_init(&expunged_seqs, array_count(&expunged_uids)+1); seq = 0;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen while (mailbox_search_next(search_ctx, &mail)) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen while (seq_range_array_iter_nth(&iter, n, &expunged_uid) &&
18efe79858a7b4a794c11bc1867fc313c7c07bbeTimo Sirainen expunged_uid < mail->uid && seq < state->messages) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen crc = crc32_data_more(crc, &mail->uid, sizeof(mail->uid));
18efe79858a7b4a794c11bc1867fc313c7c07bbeTimo Sirainen while (seq_range_array_iter_nth(&iter, n, &expunged_uid) &&
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi *error_r = mailbox_get_last_internal_error(client->mailbox, NULL);
6028ef7bd1a453212b815b608e1c7b1b21ebca49Aki Tuomi *error_r = t_strdup_printf("Message count mismatch after "
6028ef7bd1a453212b815b608e1c7b1b21ebca49Aki Tuomi "handling expunges (%u != %u)",
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen seqs = array_get(&expunged_seqs, &expunge_count);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (client->messages_count + expunge_count < state->messages) {
6028ef7bd1a453212b815b608e1c7b1b21ebca49Aki Tuomi *error_r = t_strdup_printf("Message count too low after "
6028ef7bd1a453212b815b608e1c7b1b21ebca49Aki Tuomi "handling expunges (%u < %u)",
6028ef7bd1a453212b815b608e1c7b1b21ebca49Aki Tuomi *error_r = t_strdup_printf("Message UIDs CRC32 mismatch (%u != %u)",
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if ((client->enabled_features & MAILBOX_FEATURE_QRESYNC) == 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen for (i = expunge_count; i > 0; i--) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen o_stream_nsend(client->output, str_data(str), str_len(str));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimport_send_flag_changes(struct client *client,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen seq_range_array_add_range(&old_uids, 1, state->uidnext-1);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen search_args->args = p_new(search_args->pool, struct mail_search_arg, 1);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen imap_search_add_changed_since(search_args, state->highest_modseq);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen pool = pool_alloconly_create("imap state flag changes", 1024);
cddfd1355db6b60c71d7ee3c0b4f23b3efcc9ad1Timo Sirainen fetch_ctx = imap_fetch_alloc(client, pool, "unhibernate");
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen imap_fetch_init_nofail_handler(fetch_ctx, imap_fetch_flags_init);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if ((client->enabled_features & MAILBOX_FEATURE_QRESYNC) != 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen imap_fetch_init_nofail_handler(fetch_ctx, imap_fetch_uid_init);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen imap_fetch_init_nofail_handler(fetch_ctx, imap_fetch_modseq_init);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen imap_fetch_begin(fetch_ctx, client->mailbox, search_args);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* FIXME: ideally do this asynchronously.. */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen while (imap_fetch_more_no_lock_update(fetch_ctx) == 0) ;
f32c6ed9db6f4c535f97a2020401572efc8abf86Timo Sirainen *flag_change_count_r = fetch_ctx->fetched_mails_count;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimport_state_mailbox_struct(const unsigned char *data, size_t size,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const char **error_r)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const unsigned char *p = data, *end = data + size;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (import_string(&p, end, &state_r->vname) < 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (end-p < (int)sizeof(state_r->mailbox_guid)) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen memcpy(state_r->mailbox_guid, p, sizeof(state_r->mailbox_guid));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (guid_128_is_empty(state_r->mailbox_guid)) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* EXAMINEd vs SELECTed */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen *error_r = "Mailbox state truncated at examined-flag";
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* mailbox state */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (numpack_decode32(&p, end, &state_r->uidvalidity) < 0 ||
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_decode32(&p, end, &state_r->uidnext) < 0 ||
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_decode32(&p, end, &state_r->messages) < 0 ||
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_decode(&p, end, &state_r->highest_modseq) < 0 ||
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_decode32(&p, end, &state_r->keywords_count) < 0 ||
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_decode32(&p, end, &state_r->keywords_crc32) < 0 ||
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen numpack_decode32(&p, end, &state_r->uids_crc32) < 0 ||
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen import_seq_range(&p, end, &state_r->recent_uids) < 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimport_state_mailbox_open(struct client *client,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const char **error_r)
f32c6ed9db6f4c535f97a2020401572efc8abf86Timo Sirainen unsigned int expunge_count, new_mails_count = 0, flag_change_count = 0;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen ns = mail_namespace_find(client->user->namespaces, state->vname);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen box = mailbox_alloc(ns->list, state->vname, flags);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen *error_r = t_strdup_printf("Couldn't open mailbox: %s",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen ret = mailbox_enable(box, client->enabled_features);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen *error_r = t_strdup_printf("Couldn't sync mailbox: %s",
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi mailbox_get_last_internal_error(box, NULL));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* verify that this still looks like the same mailbox */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (mailbox_get_metadata(box, MAILBOX_METADATA_GUID, &metadata) < 0) {
bf7dc750b95039981c0e9d728f313d50cf38a156Martti Rannanjärvi *error_r = mailbox_get_last_internal_error(box, NULL);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (!guid_128_equals(metadata.guid, state->mailbox_guid)) {
6028ef7bd1a453212b815b608e1c7b1b21ebca49Aki Tuomi *error_r = t_strdup_printf("Mailbox GUID has changed %s->%s",
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen mailbox_get_open_status(box, STATUS_UIDVALIDITY | STATUS_UIDNEXT |
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (status.uidvalidity != state->uidvalidity) {
6028ef7bd1a453212b815b608e1c7b1b21ebca49Aki Tuomi *error_r = t_strdup_printf("Mailbox UIDVALIDITY has changed %u->%u",
6028ef7bd1a453212b815b608e1c7b1b21ebca49Aki Tuomi *error_r = t_strdup_printf("Mailbox UIDNEXT shrank %u -> %u",
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (status.highest_modseq < state->highest_modseq) {
47a5a7e8296f3b8f2fac9a0659d4de3f2723ba4aMartti Rannanjärvi *error_r = t_strdup_printf("Mailbox HIGHESTMODSEQ shrank %"PRIu64" -> %"PRIu64,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (import_send_expunges(client, state, &expunge_count, error_r) < 0)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (state->messages - expunge_count > client->messages_count) {
6028ef7bd1a453212b815b608e1c7b1b21ebca49Aki Tuomi *error_r = t_strdup_printf("Mailbox message count shrank %u -> %u",
551fef69c0633ae3c7738038e047a7c0762d9599Timo Sirainen client_update_mailbox_flags(client, status.keywords);
551fef69c0633ae3c7738038e047a7c0762d9599Timo Sirainen for (uid = range->seq1; uid <= range->seq2; uid++) {
551fef69c0633ae3c7738038e047a7c0762d9599Timo Sirainen if (mail_index_lookup_seq(box->view, uid, &seq))
551fef69c0633ae3c7738038e047a7c0762d9599Timo Sirainen mailbox_recent_flags_set_uid_forced(box, uid);
551fef69c0633ae3c7738038e047a7c0762d9599Timo Sirainen client->recent_count = mailbox_recent_flags_count(box);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (state->messages - expunge_count < client->messages_count) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* new messages arrived */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen t_strdup_printf("* %u EXISTS", client->messages_count));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen t_strdup_printf("* %u RECENT", client->recent_count));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (array_count(status.keywords) == state->keywords_count &&
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen mailbox_status_keywords_crc32(&status) == state->keywords_crc32) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* no changes to keywords */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client->keywords.announce_count = state->keywords_count;
f32c6ed9db6f4c535f97a2020401572efc8abf86Timo Sirainen if (import_send_flag_changes(client, state, &flag_change_count) < 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if ((client->enabled_features & MAILBOX_FEATURE_QRESYNC) != 0 &&
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen status.highest_modseq != state->highest_modseq) {
47a5a7e8296f3b8f2fac9a0659d4de3f2723ba4aMartti Rannanjärvi "* OK [HIGHESTMODSEQ %"PRIu64"] Highest",
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client->sync_last_full_modseq = status.highest_modseq;
f32c6ed9db6f4c535f97a2020401572efc8abf86Timo Sirainen i_debug("Unhibernation sync: %u expunges, %u new messages, %u flag changes, %"PRIu64" modseq changes",
f32c6ed9db6f4c535f97a2020401572efc8abf86Timo Sirainen expunge_count, new_mails_count, flag_change_count,
f32c6ed9db6f4c535f97a2020401572efc8abf86Timo Sirainen status.highest_modseq - state->highest_modseq);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimport_state_mailbox(struct client *client, const unsigned char *data,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen ret = import_state_mailbox_struct(data, size, &state, error_r);
7f2cb2e5b258280af90ae99b6d07dfa44109e5beTimo Sirainen if (import_state_mailbox_open(client, &state, error_r) < 0) {
7f2cb2e5b258280af90ae99b6d07dfa44109e5beTimo Sirainen *error_r = t_strdup_printf("Mailbox %s: %s", state.vname, *error_r);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimport_state_enabled_features(struct client *client, const unsigned char *data,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen for (i = 0; i < size; i++) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client->enabled_features |= MAILBOX_FEATURE_CONDSTORE;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client->enabled_features |= MAILBOX_FEATURE_QRESYNC;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimport_state_searchres(struct client *client, const unsigned char *data,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const unsigned char *p = data;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen i_array_init(&client->search_saved_uidset, 128);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (import_seq_range(&p, data+size, &client->search_saved_uidset) < 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimport_state_tls_compression(struct client *client,
f24edebe360d3effe584a884aa7d119daf3fd371Aki Tuomivoid imap_state_import_idle_cmd_tag(struct client *client, const char *tag)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* IDLE command continues */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* IDLE terminated because of an external change, but
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen DONE was already buffered */
2933174a1d7af52678eb5d2bf581e8c2a995f6dfTimo Sirainen i_assert(cmd->state == CLIENT_COMMAND_STATE_WAIT_INPUT ||
2933174a1d7af52678eb5d2bf581e8c2a995f6dfTimo Sirainen cmd->state == CLIENT_COMMAND_STATE_WAIT_OUTPUT);
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen /* we're finishing IDLE command */
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen client->state_import_bad_idle_done ? "BAD" : "OK"));
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenstatic struct {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen ssize_t (*import)(struct client *client, const unsigned char *data,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen { IMAP_STATE_TYPE_MAILBOX, import_state_mailbox },
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen { IMAP_STATE_TYPE_ENABLED_FEATURES, import_state_enabled_features },
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen { IMAP_STATE_TYPE_SEARCHRES, import_state_searchres }
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenstatic struct {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen ssize_t (*import)(struct client *client, const unsigned char *data,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen { IMAP_STATE_TYPE_ID_LOGGED, import_state_id_logged },
f24edebe360d3effe584a884aa7d119daf3fd371Aki Tuomi { IMAP_STATE_TYPE_TLS_COMPRESSION, import_state_tls_compression }
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimap_state_try_import_public(struct client *client, const unsigned char *data,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen unsigned int i;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen for (i = 0; i < N_ELEMENTS(imap_states_public); i++) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenimap_state_try_import_internal(struct client *client, const unsigned char *data,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen unsigned int i;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen for (i = 0; i < N_ELEMENTS(imap_states_internal); i++) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (imap_states_internal[i].type == data[0]) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainenssize_t imap_state_import_base(struct client *client, bool internal,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const char **error_r)
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen const unsigned char *p;
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen if (size < 5 || memcmp(data, "base\n", 5) != 0) {
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen *error_r = t_strdup_printf("Unknown state block '%s'",
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen ret = imap_state_try_import_public(client, data+pos,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen ret = imap_state_try_import_internal(client, data+pos,
5ef28f68edef46f69961b19b7c1dcd8ec5a955e8Timo Sirainen *error_r = t_strdup_printf("Unknown type '%c'",