bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#include "lib.h"
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#include "istream.h"
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen#include "dsync-ibc.h"
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#include "dsync-mail.h"
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#include "dsync-mailbox-import.h"
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#include "dsync-mailbox-export.h"
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#include "dsync-brain-private.h"
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
ea245d7a9683e7bb9cd74fcdf1a26d049b2947ebTimo Sirainenconst char *dsync_box_state_names[DSYNC_BOX_STATE_DONE+1] = {
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen "mailbox",
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen "changes",
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen "attributes",
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen "mail_requests",
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen "mails",
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen "recv_last_common",
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen "done"
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen};
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic bool dsync_brain_master_sync_recv_mailbox(struct dsync_brain *brain)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen{
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const struct dsync_mailbox *dsync_box;
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen const char *resync_reason;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen enum dsync_ibc_recv_ret ret;
40440c0fee87be994ba7eb60fc3512a9355708aaTimo Sirainen bool resync;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen i_assert(brain->master_brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if ((ret = dsync_ibc_recv_mailbox(brain->ibc, &dsync_box)) == 0)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return FALSE;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if (ret == DSYNC_IBC_RECV_RET_FINISHED) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen i_error("Remote sent end-of-list instead of a mailbox");
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen brain->failed = TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (memcmp(dsync_box->mailbox_guid, brain->local_dsync_box.mailbox_guid,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen sizeof(dsync_box->mailbox_guid)) != 0) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen i_error("Remote sent mailbox with a wrong GUID");
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen brain->failed = TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
280bc7d8b07490dfa5cf0fc20aee8e9e2d15aa99Aki Tuomi if (dsync_box->mailbox_ignore) {
280bc7d8b07490dfa5cf0fc20aee8e9e2d15aa99Aki Tuomi /* ignore this box */
280bc7d8b07490dfa5cf0fc20aee8e9e2d15aa99Aki Tuomi if (brain->debug)
280bc7d8b07490dfa5cf0fc20aee8e9e2d15aa99Aki Tuomi i_debug("brain %c: Ignoring missing remote box GUID %s",
280bc7d8b07490dfa5cf0fc20aee8e9e2d15aa99Aki Tuomi brain->master_brain ? 'M' : 'S',
280bc7d8b07490dfa5cf0fc20aee8e9e2d15aa99Aki Tuomi guid_128_to_string(dsync_box->mailbox_guid));
280bc7d8b07490dfa5cf0fc20aee8e9e2d15aa99Aki Tuomi dsync_brain_sync_mailbox_deinit(brain);
280bc7d8b07490dfa5cf0fc20aee8e9e2d15aa99Aki Tuomi return TRUE;
280bc7d8b07490dfa5cf0fc20aee8e9e2d15aa99Aki Tuomi }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (dsync_box->mailbox_lost) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* remote lost the mailbox. it's probably already deleted, but
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen verify it on next sync just to be sure */
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen dsync_brain_set_changes_during_sync(brain, t_strdup_printf(
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen "Remote lost mailbox GUID %s (maybe it was just deleted?)",
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen guid_128_to_string(dsync_box->mailbox_guid)));
40440c0fee87be994ba7eb60fc3512a9355708aaTimo Sirainen brain->require_full_resync = TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen dsync_brain_sync_mailbox_deinit(brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
40440c0fee87be994ba7eb60fc3512a9355708aaTimo Sirainen resync = !dsync_brain_mailbox_update_pre(brain, brain->box,
40440c0fee87be994ba7eb60fc3512a9355708aaTimo Sirainen &brain->local_dsync_box,
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen dsync_box, &resync_reason);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
f48fdb57185ca68e8c079e174f3e04da36646880Timo Sirainen if (!dsync_boxes_need_sync(brain, &brain->local_dsync_box, dsync_box)) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* no fields appear to have changed, skip this mailbox */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen dsync_brain_sync_mailbox_deinit(brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
40440c0fee87be994ba7eb60fc3512a9355708aaTimo Sirainen if ((ret = dsync_brain_sync_mailbox_open(brain, dsync_box)) < 0)
c7be65f5adbc2990fbe6eeffb6df5054a8a49d9dTimo Sirainen return TRUE;
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen if (resync)
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen dsync_brain_set_changes_during_sync(brain, resync_reason);
40440c0fee87be994ba7eb60fc3512a9355708aaTimo Sirainen if (ret == 0 || resync) {
40440c0fee87be994ba7eb60fc3512a9355708aaTimo Sirainen brain->require_full_resync = TRUE;
464e82904c6670bd6c96b8793ceb294d776d6f44Timo Sirainen brain->failed = TRUE;
464e82904c6670bd6c96b8793ceb294d776d6f44Timo Sirainen dsync_brain_sync_mailbox_deinit(brain);
464e82904c6670bd6c96b8793ceb294d776d6f44Timo Sirainen return TRUE;
40440c0fee87be994ba7eb60fc3512a9355708aaTimo Sirainen }
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen dsync_brain_sync_init_box_states(brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen}
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainenstatic bool dsync_brain_recv_mailbox_attribute(struct dsync_brain *brain)
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen{
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen const struct dsync_mailbox_attribute *attr;
41e51b972f02e8b16c19fab9160294ea0a07c343Timo Sirainen struct istream *input;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen enum dsync_ibc_recv_ret ret;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen if ((ret = dsync_ibc_recv_mailbox_attribute(brain->ibc, &attr)) == 0)
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen return FALSE;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen if (ret == DSYNC_IBC_RECV_RET_FINISHED) {
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen brain->box_recv_state = DSYNC_BOX_STATE_CHANGES;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen return TRUE;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen }
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen if (dsync_mailbox_import_attribute(brain->box_importer, attr) < 0)
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen brain->failed = TRUE;
41e51b972f02e8b16c19fab9160294ea0a07c343Timo Sirainen input = attr->value_stream;
204ee6ed414f5e4eeb6f6c10763b55daf56f11acJosef 'Jeff' Sipek i_stream_unref(&input);
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen return TRUE;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen}
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen
189f639bdf2845b1e8587db5dfb2aedc1c573aa7Timo Sirainenstatic void dsync_brain_send_end_of_list(struct dsync_brain *brain,
189f639bdf2845b1e8587db5dfb2aedc1c573aa7Timo Sirainen enum dsync_ibc_eol_type type)
189f639bdf2845b1e8587db5dfb2aedc1c573aa7Timo Sirainen{
189f639bdf2845b1e8587db5dfb2aedc1c573aa7Timo Sirainen i_assert(!brain->failed);
189f639bdf2845b1e8587db5dfb2aedc1c573aa7Timo Sirainen dsync_ibc_send_end_of_list(brain->ibc, type);
189f639bdf2845b1e8587db5dfb2aedc1c573aa7Timo Sirainen}
189f639bdf2845b1e8587db5dfb2aedc1c573aa7Timo Sirainen
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainenstatic int dsync_brain_export_deinit(struct dsync_brain *brain)
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen{
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen const char *errstr;
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen enum mail_error error;
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen if (dsync_mailbox_export_deinit(&brain->box_exporter,
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen &errstr, &error) < 0) {
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen i_error("Exporting mailbox %s failed: %s",
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen mailbox_get_vname(brain->box), errstr);
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen brain->mail_error = error;
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen brain->failed = TRUE;
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen return -1;
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen }
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen return 0;
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen}
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainenstatic void dsync_brain_send_mailbox_attribute(struct dsync_brain *brain)
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen{
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen const struct dsync_mailbox_attribute *attr;
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen int ret;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen while ((ret = dsync_mailbox_export_next_attr(brain->box_exporter, &attr)) > 0) {
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen if (dsync_ibc_send_mailbox_attribute(brain->ibc, attr) == 0)
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen return;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen }
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen if (ret < 0) {
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen if (dsync_brain_export_deinit(brain) == 0)
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen i_unreached();
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen return;
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen }
189f639bdf2845b1e8587db5dfb2aedc1c573aa7Timo Sirainen dsync_brain_send_end_of_list(brain, DSYNC_IBC_EOL_MAILBOX_ATTRIBUTE);
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen brain->box_send_state = DSYNC_BOX_STATE_CHANGES;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen}
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic bool dsync_brain_recv_mail_change(struct dsync_brain *brain)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen{
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const struct dsync_mail_change *change;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen enum dsync_ibc_recv_ret ret;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if ((ret = dsync_ibc_recv_change(brain->ibc, &change)) == 0)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return FALSE;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if (ret == DSYNC_IBC_RECV_RET_FINISHED) {
69f6407b20f623193981d672c26fa722ee75d941Timo Sirainen if (dsync_mailbox_import_changes_finish(brain->box_importer) < 0)
69f6407b20f623193981d672c26fa722ee75d941Timo Sirainen brain->failed = TRUE;
887a9fbbb2ca6afd53365ba2ccae0ef8728d6948Timo Sirainen if (brain->mail_requests && brain->box_exporter != NULL)
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen brain->box_recv_state = DSYNC_BOX_STATE_MAIL_REQUESTS;
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen else
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen brain->box_recv_state = DSYNC_BOX_STATE_MAILS;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
0c47c2096714b50880d48d00ce0bf28349eb4aceTimo Sirainen if (dsync_mailbox_import_change(brain->box_importer, change) < 0)
0c47c2096714b50880d48d00ce0bf28349eb4aceTimo Sirainen brain->failed = TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen}
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic void dsync_brain_send_mail_change(struct dsync_brain *brain)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen{
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const struct dsync_mail_change *change;
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen int ret;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen while ((ret = dsync_mailbox_export_next(brain->box_exporter, &change)) > 0) {
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if (dsync_ibc_send_change(brain->ibc, change) == 0)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen if (ret < 0) {
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen if (dsync_brain_export_deinit(brain) == 0)
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen i_unreached();
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen return;
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen }
189f639bdf2845b1e8587db5dfb2aedc1c573aa7Timo Sirainen dsync_brain_send_end_of_list(brain, DSYNC_IBC_EOL_MAIL_CHANGES);
887a9fbbb2ca6afd53365ba2ccae0ef8728d6948Timo Sirainen if (brain->mail_requests && brain->box_importer != NULL)
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen brain->box_send_state = DSYNC_BOX_STATE_MAIL_REQUESTS;
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen else
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen brain->box_send_state = DSYNC_BOX_STATE_MAILS;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen}
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic bool dsync_brain_recv_mail_request(struct dsync_brain *brain)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen{
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const struct dsync_mail_request *request;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen enum dsync_ibc_recv_ret ret;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
887a9fbbb2ca6afd53365ba2ccae0ef8728d6948Timo Sirainen i_assert(brain->mail_requests);
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen i_assert(brain->box_exporter != NULL);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if ((ret = dsync_ibc_recv_mail_request(brain->ibc, &request)) == 0)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return FALSE;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if (ret == DSYNC_IBC_RECV_RET_FINISHED) {
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen brain->box_recv_state = brain->box_importer != NULL ?
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen DSYNC_BOX_STATE_MAILS :
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen DSYNC_BOX_STATE_RECV_LAST_COMMON;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen dsync_mailbox_export_want_mail(brain->box_exporter, request);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen}
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
b16de4d0b7d86168f0c6837ea5e65ca2870e2b3dTimo Sirainenstatic bool dsync_brain_send_mail_request(struct dsync_brain *brain)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen{
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const struct dsync_mail_request *request;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
887a9fbbb2ca6afd53365ba2ccae0ef8728d6948Timo Sirainen i_assert(brain->mail_requests);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen while ((request = dsync_mailbox_import_next_request(brain->box_importer)) != NULL) {
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if (dsync_ibc_send_mail_request(brain->ibc, request) == 0)
b16de4d0b7d86168f0c6837ea5e65ca2870e2b3dTimo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
4292c8c20c9740e42a214d190036b0cc2b618dbfTimo Sirainen if (brain->box_recv_state < DSYNC_BOX_STATE_MAIL_REQUESTS)
b16de4d0b7d86168f0c6837ea5e65ca2870e2b3dTimo Sirainen return FALSE;
b16de4d0b7d86168f0c6837ea5e65ca2870e2b3dTimo Sirainen
189f639bdf2845b1e8587db5dfb2aedc1c573aa7Timo Sirainen dsync_brain_send_end_of_list(brain, DSYNC_IBC_EOL_MAIL_REQUESTS);
b16de4d0b7d86168f0c6837ea5e65ca2870e2b3dTimo Sirainen if (brain->box_exporter != NULL)
b16de4d0b7d86168f0c6837ea5e65ca2870e2b3dTimo Sirainen brain->box_send_state = DSYNC_BOX_STATE_MAILS;
b16de4d0b7d86168f0c6837ea5e65ca2870e2b3dTimo Sirainen else {
b16de4d0b7d86168f0c6837ea5e65ca2870e2b3dTimo Sirainen i_assert(brain->box_recv_state != DSYNC_BOX_STATE_DONE);
b16de4d0b7d86168f0c6837ea5e65ca2870e2b3dTimo Sirainen brain->box_send_state = DSYNC_BOX_STATE_DONE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
b16de4d0b7d86168f0c6837ea5e65ca2870e2b3dTimo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen}
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic void dsync_brain_sync_half_finished(struct dsync_brain *brain)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen{
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct dsync_mailbox_state state;
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen const char *changes_during_sync;
805d7834412465268486c50711962407ad13fbf6Timo Sirainen bool require_full_resync;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (brain->box_recv_state < DSYNC_BOX_STATE_RECV_LAST_COMMON ||
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen brain->box_send_state < DSYNC_BOX_STATE_RECV_LAST_COMMON)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* finished with this mailbox */
efe78d3ba24fc866af1c79b9223dc0809ba26cadStephan Bosch i_zero(&state);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen memcpy(state.mailbox_guid, brain->local_dsync_box.mailbox_guid,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen sizeof(state.mailbox_guid));
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen state.last_uidvalidity = brain->local_dsync_box.uid_validity;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (brain->box_importer == NULL) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* this mailbox didn't exist on remote */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen state.last_common_uid = brain->local_dsync_box.uid_next-1;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen state.last_common_modseq =
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen brain->local_dsync_box.highest_modseq;
6abf66a3731d52889517bd644595c540e3a9b3ecTimo Sirainen state.last_common_pvt_modseq =
6abf66a3731d52889517bd644595c540e3a9b3ecTimo Sirainen brain->local_dsync_box.highest_pvt_modseq;
7d315281ae13a66e13da2b1ad006bdb883018278Timo Sirainen state.last_messages_count =
7d315281ae13a66e13da2b1ad006bdb883018278Timo Sirainen brain->local_dsync_box.messages_count;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen } else {
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen if (dsync_mailbox_import_deinit(&brain->box_importer,
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen !brain->failed,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen &state.last_common_uid,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen &state.last_common_modseq,
6abf66a3731d52889517bd644595c540e3a9b3ecTimo Sirainen &state.last_common_pvt_modseq,
7d315281ae13a66e13da2b1ad006bdb883018278Timo Sirainen &state.last_messages_count,
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen &changes_during_sync,
805d7834412465268486c50711962407ad13fbf6Timo Sirainen &require_full_resync,
ce0e25f26d6e67480ee39b5ca0ad634fa60c4605Timo Sirainen &brain->mail_error) < 0) {
805d7834412465268486c50711962407ad13fbf6Timo Sirainen if (require_full_resync) {
805d7834412465268486c50711962407ad13fbf6Timo Sirainen /* don't treat this as brain failure or the
805d7834412465268486c50711962407ad13fbf6Timo Sirainen state won't be sent to the other brain.
805d7834412465268486c50711962407ad13fbf6Timo Sirainen this also means we'll continue syncing the
805d7834412465268486c50711962407ad13fbf6Timo Sirainen following mailboxes. */
805d7834412465268486c50711962407ad13fbf6Timo Sirainen brain->require_full_resync = TRUE;
805d7834412465268486c50711962407ad13fbf6Timo Sirainen } else {
805d7834412465268486c50711962407ad13fbf6Timo Sirainen brain->failed = TRUE;
805d7834412465268486c50711962407ad13fbf6Timo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen if (changes_during_sync != NULL) {
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen state.changes_during_sync = TRUE;
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen dsync_brain_set_changes_during_sync(brain, changes_during_sync);
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
805d7834412465268486c50711962407ad13fbf6Timo Sirainen if (brain->require_full_resync) {
805d7834412465268486c50711962407ad13fbf6Timo Sirainen state.last_uidvalidity = 0;
805d7834412465268486c50711962407ad13fbf6Timo Sirainen state.changes_during_sync = TRUE;
805d7834412465268486c50711962407ad13fbf6Timo Sirainen }
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen brain->mailbox_state = state;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen dsync_ibc_send_mailbox_state(brain->ibc, &state);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen}
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic bool dsync_brain_recv_mail(struct dsync_brain *brain)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen{
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct dsync_mail *mail;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen enum dsync_ibc_recv_ret ret;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if ((ret = dsync_ibc_recv_mail(brain->ibc, &mail)) == 0)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return FALSE;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if (ret == DSYNC_IBC_RECV_RET_FINISHED) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen brain->box_recv_state = DSYNC_BOX_STATE_RECV_LAST_COMMON;
66e5a5a1bee411991ad08b6ee938e14d75941943Timo Sirainen if (brain->box_exporter != NULL &&
66e5a5a1bee411991ad08b6ee938e14d75941943Timo Sirainen brain->box_send_state >= DSYNC_BOX_STATE_RECV_LAST_COMMON) {
33409f3448d16690d151aa86e13cfcb4704286b5Timo Sirainen if (dsync_brain_export_deinit(brain) < 0)
33409f3448d16690d151aa86e13cfcb4704286b5Timo Sirainen return TRUE;
33409f3448d16690d151aa86e13cfcb4704286b5Timo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen dsync_brain_sync_half_finished(brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen if (brain->debug) {
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen i_debug("brain %c: import mail uid %u guid %s",
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen brain->master_brain ? 'M' : 'S', mail->uid, mail->guid);
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainen }
7ae6552482fe6e6a613f883335fdafdf9afbc7b7Timo Sirainen if (dsync_mailbox_import_mail(brain->box_importer, mail) < 0)
7ae6552482fe6e6a613f883335fdafdf9afbc7b7Timo Sirainen brain->failed = TRUE;
204ee6ed414f5e4eeb6f6c10763b55daf56f11acJosef 'Jeff' Sipek i_stream_unref(&mail->input);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen}
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic bool dsync_brain_send_mail(struct dsync_brain *brain)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen{
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const struct dsync_mail *mail;
2fdcbdc25e34c7d83bb744f25b2d28335822c4a7Timo Sirainen
2fdcbdc25e34c7d83bb744f25b2d28335822c4a7Timo Sirainen if (brain->mail_requests &&
2fdcbdc25e34c7d83bb744f25b2d28335822c4a7Timo Sirainen brain->box_recv_state < DSYNC_BOX_STATE_MAILS) {
2fdcbdc25e34c7d83bb744f25b2d28335822c4a7Timo Sirainen /* wait for mail requests to finish. we could already start
2fdcbdc25e34c7d83bb744f25b2d28335822c4a7Timo Sirainen exporting, but then we're going to do quite a lot of
2fdcbdc25e34c7d83bb744f25b2d28335822c4a7Timo Sirainen separate searches. especially with pipe backend we'd do
2fdcbdc25e34c7d83bb744f25b2d28335822c4a7Timo Sirainen a separate search for each mail. */
2fdcbdc25e34c7d83bb744f25b2d28335822c4a7Timo Sirainen return FALSE;
2fdcbdc25e34c7d83bb744f25b2d28335822c4a7Timo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
e1bef591ed45c5baf6b5bdc69fb7fa5ff05df2b6Timo Sirainen while (dsync_mailbox_export_next_mail(brain->box_exporter, &mail) > 0) {
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if (dsync_ibc_send_mail(brain->ibc, mail) == 0)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
33409f3448d16690d151aa86e13cfcb4704286b5Timo Sirainen if (dsync_brain_export_deinit(brain) < 0)
7abb6c8948a45a64ef0a37616099339ac241db40Timo Sirainen return TRUE;
7abb6c8948a45a64ef0a37616099339ac241db40Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen brain->box_send_state = DSYNC_BOX_STATE_DONE;
189f639bdf2845b1e8587db5dfb2aedc1c573aa7Timo Sirainen dsync_brain_send_end_of_list(brain, DSYNC_IBC_EOL_MAILS);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen dsync_brain_sync_half_finished(brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen}
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstatic bool dsync_brain_recv_last_common(struct dsync_brain *brain)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen{
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen enum dsync_ibc_recv_ret ret;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct dsync_mailbox_state state;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if ((ret = dsync_ibc_recv_mailbox_state(brain->ibc, &state)) == 0)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return FALSE;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen if (ret == DSYNC_IBC_RECV_RET_FINISHED) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen i_error("Remote sent end-of-list instead of a mailbox state");
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen brain->failed = TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen i_assert(brain->box_send_state == DSYNC_BOX_STATE_DONE);
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen i_assert(memcmp(state.mailbox_guid, brain->local_dsync_box.mailbox_guid,
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen sizeof(state.mailbox_guid)) == 0);
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen /* normally the last_common_* values should be the same in local and
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen remote, but during unexpected changes they may differ. use the
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen values that are lower as the final state. */
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen if (brain->mailbox_state.last_common_uid > state.last_common_uid)
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen brain->mailbox_state.last_common_uid = state.last_common_uid;
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen if (brain->mailbox_state.last_common_modseq > state.last_common_modseq)
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen brain->mailbox_state.last_common_modseq = state.last_common_modseq;
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen if (brain->mailbox_state.last_common_pvt_modseq > state.last_common_pvt_modseq)
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen brain->mailbox_state.last_common_pvt_modseq = state.last_common_pvt_modseq;
34421746925a2e1850549fa0fa07ddeeb1a271e2Timo Sirainen if (state.changes_during_sync)
4f7951e71128c120d8a502d6406cc603fcc8eb0bTimo Sirainen brain->changes_during_remote_sync = TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen dsync_brain_sync_mailbox_deinit(brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return TRUE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen}
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenbool dsync_brain_sync_mails(struct dsync_brain *brain)
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen{
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen bool changed = FALSE;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen i_assert(brain->box != NULL);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen switch (brain->box_recv_state) {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen case DSYNC_BOX_STATE_MAILBOX:
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen changed = dsync_brain_master_sync_recv_mailbox(brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen break;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen case DSYNC_BOX_STATE_ATTRIBUTES:
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen changed = dsync_brain_recv_mailbox_attribute(brain);
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen break;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen case DSYNC_BOX_STATE_CHANGES:
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen changed = dsync_brain_recv_mail_change(brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen break;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen case DSYNC_BOX_STATE_MAIL_REQUESTS:
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen changed = dsync_brain_recv_mail_request(brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen break;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen case DSYNC_BOX_STATE_MAILS:
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen changed = dsync_brain_recv_mail(brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen break;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen case DSYNC_BOX_STATE_RECV_LAST_COMMON:
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen changed = dsync_brain_recv_last_common(brain);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen break;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen case DSYNC_BOX_STATE_DONE:
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen break;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen if (!dsync_ibc_is_send_queue_full(brain->ibc) && !brain->failed) {
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen switch (brain->box_send_state) {
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen case DSYNC_BOX_STATE_MAILBOX:
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen /* wait for mailbox to be received first */
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen break;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen case DSYNC_BOX_STATE_ATTRIBUTES:
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen dsync_brain_send_mailbox_attribute(brain);
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen changed = TRUE;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen break;
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen case DSYNC_BOX_STATE_CHANGES:
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen dsync_brain_send_mail_change(brain);
b16de4d0b7d86168f0c6837ea5e65ca2870e2b3dTimo Sirainen changed = TRUE;
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen break;
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen case DSYNC_BOX_STATE_MAIL_REQUESTS:
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen if (dsync_brain_send_mail_request(brain))
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen changed = TRUE;
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen break;
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen case DSYNC_BOX_STATE_MAILS:
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen if (dsync_brain_send_mail(brain))
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen changed = TRUE;
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen break;
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen case DSYNC_BOX_STATE_RECV_LAST_COMMON:
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen i_unreached();
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen case DSYNC_BOX_STATE_DONE:
66088eb6144f98adbbd9799f902c87cbdd0a4eebTimo Sirainen break;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen }
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen return changed;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen}