imap-sync.c revision 8039af9679af6fb56116b353fe44f7dd4c08f031
c25356d5978632df6203437e1953bcb29e0c736fTimo Sirainen/* Copyright (c) 2002-2007 Dovecot authors, see the included COPYING file */
04ab375449dd97eed50ada88dd0df2abab01cfeeTimo Sirainenimap_sync_init(struct client *client, struct mailbox *box,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen enum imap_sync_flags imap_flags, enum mailbox_sync_flags flags)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen ctx->sync_ctx = mailbox_sync_init(box, flags);
a27e065f1a1f91c7fbdf7c2ea1c387441af0cbb3Timo Sirainen ctx->mail = mail_alloc(ctx->t, MAIL_FETCH_FLAGS, 0);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen i_array_init(&ctx->tmp_keywords, client->keywords.announce_count + 8);
ff7056842f14fd3b30a2d327dfab165b9d15dd30Timo Sirainenint imap_sync_deinit(struct imap_sync_context *ctx)
cb05ecbd96ddb5e53c1850d27434541138a3f284Timo Sirainen if (mailbox_sync_deinit(&ctx->sync_ctx, STATUS_UIDVALIDITY |
cb05ecbd96ddb5e53c1850d27434541138a3f284Timo Sirainen STATUS_MESSAGES | STATUS_RECENT, &status) < 0 ||
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if (status.uidvalidity != ctx->client->uidvalidity) {
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen /* most clients would get confused by this. disconnect them. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen "Mailbox UIDVALIDITY changed");
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen ctx->client->messages_count = status.messages;
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen t_strdup_printf("* %u EXISTS", status.messages));
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen if (status.recent != ctx->client->recent_count &&
d3442384ca53d4b18a493db7dd0b000f470419cfTimo Sirainen t_strdup_printf("* %u RECENT", status.recent));
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainenstatic int imap_sync_send_flags(struct imap_sync_context *ctx, string_t *str)
4b41116563110d00330896a568eff1078c382827Timo Sirainen const char *const *keywords;
5137d2d80255938a0f5fb8f3c1a21b34cf11ada3Timo Sirainen keywords = client_get_keyword_names(ctx->client, &ctx->tmp_keywords,
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if (ctx->imap_flags & IMAP_SYNC_FLAG_SEND_UID)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return client_send_line(ctx->client, str_c(str));
cb05ecbd96ddb5e53c1850d27434541138a3f284Timo Sirainenint imap_sync_more(struct imap_sync_context *ctx)
923eb3dde28e4d8841c14fd6b4a69635b7070c3eTimo Sirainen /* get next one */
923eb3dde28e4d8841c14fd6b4a69635b7070c3eTimo Sirainen if (!mailbox_sync_next(ctx->sync_ctx, &ctx->sync_rec)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* finished */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (ctx->sync_rec.seq2 > ctx->messages_count) {
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen /* don't send change notifications of messages we
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen haven't even announced to client yet */
e015e2f7e7f48874495f9df8b0dd192b7ffcb5ccTimo Sirainen if (ctx->sync_rec.seq1 > ctx->messages_count) {
4981827cb5e32cf767b7b0e3070137e6b36f42afTimo Sirainen for (; ctx->seq <= ctx->sync_rec.seq2; ctx->seq++) {
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen for (; ctx->seq >= ctx->sync_rec.seq1; ctx->seq--) {
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen ret = client_send_line(ctx->client, str_c(str));
8e371a3ce32bd64288786855b8ce0cb63f19f7d1Timo Sirainen /* update only after we're finished, so that
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen the seq2 > messages_count check above
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen doesn't break */
a2f250a332dfc1e6cd4ffd196c621eb9dbf7b8a1Timo Sirainen /* failure / buffer full */
f81f4bc282cd1944cec187bae89c0701a416ed2aTimo Sirainenstatic bool cmd_sync_continue(struct client_command_context *cmd)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen if ((ret = imap_sync_more(ctx->sync_ctx)) == 0)
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen client_send_untagged_storage_error(cmd->client,
a757f31393b9d6fc7760a9dec8363404ab3ae576Timo Sirainenbool cmd_sync(struct client_command_context *cmd, enum mailbox_sync_flags flags,
a757f31393b9d6fc7760a9dec8363404ab3ae576Timo Sirainen enum imap_sync_flags imap_flags, const char *tagline)
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen i_assert(client->output_lock == cmd || client->output_lock == NULL);
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen mailbox_transaction_get_count(client->mailbox) > 0) {
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen no_newmail = (client_workarounds & WORKAROUND_DELAY_NEWMAIL) != 0 &&
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen /* expunges might break the client just as badly as new mail
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen notifications. */
83bb013a99f0936995f9c7a1077822662d8fefdbTimo Sirainen ctx = p_new(cmd->pool, struct cmd_sync_context, 1);