imap-sync.c revision 17b03c9db961e1c004284907d969eb11b08a795e
89a126810703c666309310d0f3189e9834d70b5bTimo Sirainen/* Copyright (c) 2002-2007 Dovecot authors, see the included COPYING file */
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainenstatic void imap_sync_send_changed_keywords(struct imap_sync_context *ctx)
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen ctx->keyword_announce_count = array_count(ctx->keywords);
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen if (client_save_keywords(&ctx->client->keywords, ctx->keywords))
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen client_send_mailbox_flags(ctx->client, ctx->box, ctx->keywords);
c4267cf4c40fb1f866b5958ff122ef836b8c5dfbTimo Sirainenimap_sync_init(struct client *client, struct mailbox *box,
39e6fcc3e8b1ccb13087c232cb6bdea04d1a20a4Timo Sirainen enum imap_sync_flags imap_flags, enum mailbox_sync_flags flags)
c4267cf4c40fb1f866b5958ff122ef836b8c5dfbTimo Sirainen ctx->sync_ctx = mailbox_sync_init(box, flags);
fdc557286bc9f92c5f3bb49096ff6e2bcec0ea79Timo Sirainen ctx->mail = mail_alloc(ctx->t, MAIL_FETCH_FLAGS, 0);
88187ee880b4829443e0d55ea7d145d9d5880217Timo Sirainen mailbox_get_status(ctx->box, STATUS_KEYWORDS, &status);
c4267cf4c40fb1f866b5958ff122ef836b8c5dfbTimo Sirainenint imap_sync_deinit(struct imap_sync_context *ctx)
bd74402ca1a39ec303075fefb1212d7e18a71531Timo Sirainen if (mailbox_sync_deinit(&ctx->sync_ctx, STATUS_UIDVALIDITY |
5fb3bff645380804c9db2510940c41db6b8fdb01Timo Sirainen STATUS_MESSAGES | STATUS_RECENT, &status) < 0 ||
bd74402ca1a39ec303075fefb1212d7e18a71531Timo Sirainen if (status.uidvalidity != ctx->client->uidvalidity) {
bd74402ca1a39ec303075fefb1212d7e18a71531Timo Sirainen /* most clients would get confused by this. disconnect them. */
bd74402ca1a39ec303075fefb1212d7e18a71531Timo Sirainen "Mailbox UIDVALIDITY changed");
150e64c376365becf1ec5c9d45912ecb840eea96Timo Sirainen ctx->client->messages_count = status.messages;
ef11d3930c3602fc86349a4e3a53442df470b601Timo Sirainen t_strdup_printf("* %u EXISTS", status.messages));
17b03c9db961e1c004284907d969eb11b08a795eTimo Sirainen if (status.recent != ctx->client->recent_count &&
ef11d3930c3602fc86349a4e3a53442df470b601Timo Sirainen t_strdup_printf("* %u RECENT", status.recent));
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainenstatic int imap_sync_send_flags(struct imap_sync_context *ctx, string_t *str,
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen const unsigned int *kw_indexes;
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen const char *const *all_names;
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen all_names = array_get(ctx->keywords, &all_count);
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen keywords = mail_get_keyword_indexes(ctx->mail);
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen t_array_init(keyword_names, I_MAX(kw_count, 16));
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen /* convert indexes to names */
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen for (i = 0; i < kw_count; i++) {
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen if (kw_indexes[i] >= ctx->keyword_announce_count) {
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen /* a new keyword. notify the client. */
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen all_names = array_get(ctx->keywords, &all_count);
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen array_append(keyword_names, &all_names[kw_indexes[i]], 1);
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen if (ctx->imap_flags & IMAP_SYNC_FLAG_SEND_UID)
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen imap_write_flags(str, flags, array_idx(keyword_names, 0));
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen return client_send_line(ctx->client, str_c(str));
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainenint imap_sync_more(struct imap_sync_context *ctx)
a4bc2c3962b94f83c7bb7bebe7af364f4dee7883Timo Sirainen ARRAY_TYPE(keywords) keyword_names = ARRAY_INIT;
c4267cf4c40fb1f866b5958ff122ef836b8c5dfbTimo Sirainen /* get next one */
c979eeda1f46483d9c963e265786b701d7683d77Timo Sirainen if (!mailbox_sync_next(ctx->sync_ctx, &ctx->sync_rec)) {
c979eeda1f46483d9c963e265786b701d7683d77Timo Sirainen /* finished */
a54be2bd26d6f0860d194d3aeedfa6b7fc14d24cTimo Sirainen if (ctx->sync_rec.seq2 > ctx->messages_count) {
a54be2bd26d6f0860d194d3aeedfa6b7fc14d24cTimo Sirainen /* don't send change notifications of messages we
a54be2bd26d6f0860d194d3aeedfa6b7fc14d24cTimo Sirainen haven't even announced to client yet */
10cfe8a2bdc5ccfc05380689c71c27209327538fTimo Sirainen if (ctx->sync_rec.seq1 > ctx->messages_count) {
c4267cf4c40fb1f866b5958ff122ef836b8c5dfbTimo Sirainen for (; ctx->seq <= ctx->sync_rec.seq2; ctx->seq++) {
c4267cf4c40fb1f866b5958ff122ef836b8c5dfbTimo Sirainen for (; ctx->seq >= ctx->sync_rec.seq1; ctx->seq--) {
68efcccb384f2d6871164b072457e87473502c51Timo Sirainen ret = client_send_line(ctx->client, str_c(str));
d42c9a8f362b76740418c4f9f9441eb7fc661e57Timo Sirainen /* update only after we're finished, so that
d42c9a8f362b76740418c4f9f9441eb7fc661e57Timo Sirainen the seq2 > messages_count check above
d42c9a8f362b76740418c4f9f9441eb7fc661e57Timo Sirainen doesn't break */
88286b0527bcc0711e312e9db65ca121a45213e3Timo Sirainen /* failure / buffer full */
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenstatic bool cmd_sync_continue(struct client_command_context *cmd)
42507d758b053bb483de58fba55c73a9eb5d3fbaTimo Sirainen if ((ret = imap_sync_more(ctx->sync_ctx)) == 0)
e60a349c641bb2f4723e4a395a25f55531682d2bTimo Sirainen client_send_untagged_storage_error(cmd->client,
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainenbool cmd_sync(struct client_command_context *cmd, enum mailbox_sync_flags flags,
6ef7e31619edfaa17ed044b45861d106a86191efTimo Sirainen enum imap_sync_flags imap_flags, const char *tagline)
42507d758b053bb483de58fba55c73a9eb5d3fbaTimo Sirainen i_assert(client->output_lock == cmd || client->output_lock == NULL);
42507d758b053bb483de58fba55c73a9eb5d3fbaTimo Sirainen mailbox_transaction_get_count(client->mailbox) > 0) {
150e64c376365becf1ec5c9d45912ecb840eea96Timo Sirainen no_newmail = (client_workarounds & WORKAROUND_DELAY_NEWMAIL) != 0 &&
150e64c376365becf1ec5c9d45912ecb840eea96Timo Sirainen /* expunges might break the client just as badly as new mail
150e64c376365becf1ec5c9d45912ecb840eea96Timo Sirainen notifications. */
e60a349c641bb2f4723e4a395a25f55531682d2bTimo Sirainen ctx = p_new(cmd->pool, struct cmd_sync_context, 1);