mbox-sync-update.c revision bb10ebcf076c959c752f583746d83805d7686df8
e59faf65ce864fe95dc00f5d52b8323cdbd0608aTimo Sirainen/* Copyright (C) 2004 Timo Sirainen */
08b30498acefc69e223baf7eda6429be98cc3a10Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "lib.h"
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainen#include "ioloop.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "buffer.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "str.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen#include "message-parser.h"
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen#include "index-storage.h"
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen#include "mbox-storage.h"
08b30498acefc69e223baf7eda6429be98cc3a10Timo Sirainen#include "mbox-sync-private.h"
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void status_flags_append(struct mbox_sync_mail_context *ctx,
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen const struct mbox_flag_type *flags_list)
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen{
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen int i;
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen /* kludgy kludgy */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->mail.flags ^= MBOX_NONRECENT_KLUDGE;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen for (i = 0; flags_list[i].chr != 0; i++) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if ((ctx->mail.flags & flags_list[i].flag) != 0)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_append_c(ctx->header, flags_list[i].chr);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->mail.flags ^= MBOX_NONRECENT_KLUDGE;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mbox_sync_move_buffer(struct mbox_sync_mail_context *ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen size_t pos, size_t need, size_t have)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen{
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen ssize_t diff = (ssize_t)need - (ssize_t)have;
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen int i;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen i_assert(have < SSIZE_T_MAX);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if (diff == 0) {
2d3aac5be07b96f72cf0551fac35ac74a4f07770Timo Sirainen if (ctx->header_last_change < pos + have ||
2d3aac5be07b96f72cf0551fac35ac74a4f07770Timo Sirainen ctx->header_last_change == (size_t)-1)
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen ctx->header_last_change = pos + have;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen } else {
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen /* FIXME: if (diff < ctx->space && pos < ctx->offset) then
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen move the data only up to space offset and give/take the
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen space from there. update header_last_change accordingly.
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen (except pos and offset can't be compared directly) */
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen ctx->header_last_change = (size_t)-1;
a205d315b0978985ba77d871f44e4a98273612e6Timo Sirainen for (i = 0; i < MBOX_HDR_COUNT; i++) {
a205d315b0978985ba77d871f44e4a98273612e6Timo Sirainen if (ctx->hdr_pos[i] > pos &&
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen ctx->hdr_pos[i] != (size_t)-1)
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen ctx->hdr_pos[i] += diff;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen }
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen if (diff < 0)
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen str_delete(ctx->header, pos, -diff);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen else {
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen ctx->header_last_change = (size_t)-1;
a6a2b38d806f3ab3198160e39240a8200775e525Timo Sirainen buffer_copy(ctx->header, pos + diff,
a6a2b38d806f3ab3198160e39240a8200775e525Timo Sirainen ctx->header, pos, (size_t)-1);
a6a2b38d806f3ab3198160e39240a8200775e525Timo Sirainen }
fb2e0bbb7737f3223b16aa41e4b40fb0cd5f288fTimo Sirainen }
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen}
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainenstatic void status_flags_replace(struct mbox_sync_mail_context *ctx, size_t pos,
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen const struct mbox_flag_type *flags_list)
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen{
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen unsigned char *data;
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen size_t size;
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen int i, need, have;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen if (ctx->header_first_change > pos)
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen ctx->header_first_change = pos;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen /* how many bytes do we need? */
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen for (i = 0, need = 0; flags_list[i].chr != 0; i++) {
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen if ((ctx->mail.flags & flags_list[i].flag) != 0)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen need++;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen }
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* how many bytes do we have now? */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen data = buffer_get_modifyable_data(ctx->header, &size);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen for (have = 0; pos < size; pos++) {
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen if (data[pos] == '\n')
821984ecb6c90696f35c32a8dc4c8a60f9e98f99Timo Sirainen break;
821984ecb6c90696f35c32a8dc4c8a60f9e98f99Timo Sirainen
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen /* see if this is unknown flag for us */
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen for (i = 0; flags_list[i].chr != 0; i++) {
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen if (flags_list[i].chr == data[pos])
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen break;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen }
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen if (flags_list[i].chr != 0)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen have++;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen else {
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* save this one */
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen data[pos-have] = data[pos];
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen }
e5759add4dc24b96606dccc4a989838e260f2a12Timo Sirainen }
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen pos -= have;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen mbox_sync_move_buffer(ctx, pos, need, have);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* @UNSAFE */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen data = buffer_get_space_unsafe(ctx->header, pos, need);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen for (i = 0, need = 0; flags_list[i].chr != 0; i++) {
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen if ((ctx->mail.flags & flags_list[i].flag) != 0)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen *data++ = flags_list[i].chr;
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen }
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen}
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenstatic void keywords_append(struct mbox_sync_context *sync_ctx, string_t *dest,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen const array_t *keyword_indexes_arr)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen{
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ARRAY_SET_TYPE(keyword_indexes_arr, unsigned int);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen const char *const *keyword_names;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen const unsigned int *keyword_indexes;
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen unsigned int i, idx_count, keywords_count;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen size_t last_break;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen keyword_names = array_get(sync_ctx->ibox->keyword_names,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen &keywords_count);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen keyword_indexes = array_get(keyword_indexes_arr, &idx_count);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
821984ecb6c90696f35c32a8dc4c8a60f9e98f99Timo Sirainen for (i = 0, last_break = 0; i < idx_count; i++) {
821984ecb6c90696f35c32a8dc4c8a60f9e98f99Timo Sirainen i_assert(keyword_indexes[i] < keywords_count);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* try avoid overly long lines but cutting them
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen every 70 chars or so */
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen if (str_len(dest) - last_break < 70) {
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen if (i > 0)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_append_c(dest, ' ');
d22301419109ed4a38351715e6760011421dadecTimo Sirainen } else {
d22301419109ed4a38351715e6760011421dadecTimo Sirainen str_append(dest, "\n\t");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen last_break = str_len(dest);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_append(dest, keyword_names[keyword_indexes[i]]);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
d22301419109ed4a38351715e6760011421dadecTimo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenkeywords_append_all(struct mbox_sync_mail_context *ctx, string_t *dest)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
197ad81605dc0f6d2ebc9ad99994db5ca6d76699Timo Sirainen const char *const *names;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const unsigned char *p;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen unsigned int i, count;
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen size_t last_break;
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen p = str_data(dest);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (str_len(dest) < 70)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen last_break = 0;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen else {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* set last_break to beginning of line */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen for (last_break = str_len(dest); last_break > 0; last_break--) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (p[last_break-1] == '\n')
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen break;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen names = array_get(ctx->sync_ctx->ibox->keyword_names, &count);
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen for (i = 0; i < count; i++) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* try avoid overly long lines but cutting them
d22301419109ed4a38351715e6760011421dadecTimo Sirainen every 70 chars or so */
d22301419109ed4a38351715e6760011421dadecTimo Sirainen if (str_len(dest) - last_break < 70)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_append_c(dest, ' ');
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen else {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_append(dest, "\n\t");
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen last_break = str_len(dest);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_append(dest, names[i]);
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen }
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void mbox_sync_add_missing_headers(struct mbox_sync_mail_context *ctx)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen{
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen size_t old_hdr_size, new_hdr_size;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen old_hdr_size = ctx->body_offset - ctx->hdr_offset;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen new_hdr_size = str_len(ctx->header);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainen if (new_hdr_size > 0 &&
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_data(ctx->header)[new_hdr_size-1] != '\n') {
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen /* broken header - doesn't end with \n. fix it. */
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen str_append_c(ctx->header, '\n');
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen }
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (ctx->sync_ctx->dest_first_mail &&
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_IMAPBASE] == (size_t)-1) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (ctx->sync_ctx->base_uid_validity == 0) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ctx->sync_ctx->base_uid_validity =
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ctx->sync_ctx->hdr->uid_validity == 0 ?
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen (uint32_t)ioloop_time :
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ctx->sync_ctx->hdr->uid_validity;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen str_append(ctx->header, "X-IMAPbase: ");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_IMAPBASE] = str_len(ctx->header);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_printfa(ctx->header, "%u %010u",
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen ctx->sync_ctx->base_uid_validity,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->sync_ctx->next_uid-1);
7f773564b94e6054a40d3785cb63c29f1e4d4deeTimo Sirainen keywords_append_all(ctx, ctx->header);
7f773564b94e6054a40d3785cb63c29f1e4d4deeTimo Sirainen str_append_c(ctx->header, '\n');
7f773564b94e6054a40d3785cb63c29f1e4d4deeTimo Sirainen }
7f773564b94e6054a40d3785cb63c29f1e4d4deeTimo Sirainen
7f773564b94e6054a40d3785cb63c29f1e4d4deeTimo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_UID] == (size_t)-1 && !ctx->pseudo) {
7f773564b94e6054a40d3785cb63c29f1e4d4deeTimo Sirainen str_append(ctx->header, "X-UID: ");
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_UID] = str_len(ctx->header);
33ae95df45c9b5ec51332a6b39eb5322038686b9Timo Sirainen str_printfa(ctx->header, "%u\n", ctx->mail.uid);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
dda2c506c8fc8ac2f88272de4523ded42baa0aa0Timo Sirainen
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen if (ctx->hdr_pos[MBOX_HDR_STATUS] == (size_t)-1 &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (ctx->mail.flags & STATUS_FLAGS_MASK) != 0) {
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen str_append(ctx->header, "Status: ");
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen ctx->hdr_pos[MBOX_HDR_STATUS] = str_len(ctx->header);
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen status_flags_append(ctx, mbox_status_flags);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen str_append_c(ctx->header, '\n');
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
bf301a34ffbfd049be583094019b2644884b6d0bTimo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_STATUS] == (size_t)-1 &&
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen (ctx->mail.flags & XSTATUS_FLAGS_MASK) != 0) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen str_append(ctx->header, "X-Status: ");
58eb2cb24dbeadd94500670acad7ceb1c8b0d9b4Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_STATUS] = str_len(ctx->header);
bf301a34ffbfd049be583094019b2644884b6d0bTimo Sirainen status_flags_append(ctx, mbox_xstatus_flags);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen str_append_c(ctx->header, '\n');
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_KEYWORDS] == (size_t)-1 &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen array_is_created(&ctx->mail.keywords) &&
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen array_count(&ctx->mail.keywords) > 0) {
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen str_append(ctx->header, "X-Keywords: ");
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_KEYWORDS] = str_len(ctx->header);
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen keywords_append(ctx->sync_ctx, ctx->header,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen &ctx->mail.keywords);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_append_c(ctx->header, '\n');
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen }
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->content_length == (uoff_t)-1 &&
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ctx->mail.body_size >= MBOX_MIN_CONTENT_LENGTH_SIZE) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_printfa(ctx->header, "Content-Length: %"PRIuUOFF_T"\n",
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen ctx->mail.body_size);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (str_len(ctx->header) != new_hdr_size) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->header_first_change == (size_t)-1)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->header_first_change = new_hdr_size;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ctx->header_last_change = (size_t)-1;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->have_eoh)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_append_c(ctx->header, '\n');
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainenstatic void mbox_sync_update_status(struct mbox_sync_mail_context *ctx)
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen{
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (ctx->hdr_pos[MBOX_HDR_STATUS] != (size_t)-1) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen status_flags_replace(ctx, ctx->hdr_pos[MBOX_HDR_STATUS],
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mbox_status_flags);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen }
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void mbox_sync_update_xstatus(struct mbox_sync_mail_context *ctx)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_STATUS] != (size_t)-1) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen status_flags_replace(ctx, ctx->hdr_pos[MBOX_HDR_X_STATUS],
08b30498acefc69e223baf7eda6429be98cc3a10Timo Sirainen mbox_xstatus_flags);
08b30498acefc69e223baf7eda6429be98cc3a10Timo Sirainen }
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void mbox_sync_update_line(struct mbox_sync_mail_context *ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen size_t pos, string_t *new_line)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen{
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *hdr, *p;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen uoff_t file_pos;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->header_first_change > pos)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->header_first_change = pos;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen hdr = str_c(ctx->header) + pos;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen p = strchr(hdr, '\n');
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (p == NULL) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* shouldn't really happen, but allow anyway.. */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen p = hdr + strlen(hdr);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen }
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen file_pos = pos + ctx->hdr_offset;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->mail.space > 0 && ctx->mail.offset >= file_pos &&
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->mail.offset < file_pos + (p - hdr)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* extra space points to this line. remove it. */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->mail.offset = ctx->hdr_offset;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ctx->mail.space = 0;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen }
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen mbox_sync_move_buffer(ctx, pos, str_len(new_line), p - hdr + 1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen buffer_copy(ctx->header, pos, new_line, 0, (size_t)-1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainenstatic void mbox_sync_update_xkeywords(struct mbox_sync_mail_context *ctx)
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen{
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen string_t *str;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_KEYWORDS] == (size_t)-1)
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen return;
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen t_push();
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen str = t_str_new(256);
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen keywords_append(ctx->sync_ctx, str, &ctx->mail.keywords);
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen str_append_c(str, '\n');
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen mbox_sync_update_line(ctx, ctx->hdr_pos[MBOX_HDR_X_KEYWORDS], str);
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen t_pop();
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen}
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainenstatic void mbox_sync_update_x_imap_base(struct mbox_sync_mail_context *ctx)
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen{
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen struct mbox_sync_context *sync_ctx = ctx->sync_ctx;
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen string_t *str;
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen if (!sync_ctx->dest_first_mail ||
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_IMAPBASE] == (size_t)-1)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (sync_ctx->update_base_uid_last <= sync_ctx->base_uid_last)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen sync_ctx->update_base_uid_last = 0;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* see if anything changed */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (!(ctx->update_imapbase_keywords ||
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen sync_ctx->update_base_uid_last != 0))
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen return;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* update uid-last field in X-IMAPbase */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen t_push();
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str = t_str_new(200);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_printfa(str, "%u %010u", sync_ctx->base_uid_validity,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen sync_ctx->update_base_uid_last != 0 ?
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen sync_ctx->update_base_uid_last : sync_ctx->base_uid_last);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen keywords_append_all(ctx, str);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_append_c(str, '\n');
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen mbox_sync_update_line(ctx, ctx->hdr_pos[MBOX_HDR_X_IMAPBASE], str);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen t_pop();
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainenstatic void mbox_sync_update_x_uid(struct mbox_sync_mail_context *ctx)
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen{
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen string_t *str;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_UID] == (size_t)-1 ||
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen ctx->mail.uid == ctx->parsed_uid)
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen return;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen t_push();
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str = t_str_new(64);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_printfa(str, "%u\n", ctx->mail.uid);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mbox_sync_update_line(ctx, ctx->hdr_pos[MBOX_HDR_X_UID], str);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen t_pop();
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen}
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainenvoid mbox_sync_update_header(struct mbox_sync_mail_context *ctx)
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen{
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen uint8_t old_flags;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen int keywords_changed;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen i_assert(ctx->mail.uid != 0 || ctx->pseudo);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen old_flags = ctx->mail.flags;
bf301a34ffbfd049be583094019b2644884b6d0bTimo Sirainen
51ff0538ab38def8045b3f7feb43e1e069cbe037Timo Sirainen if (array_count(&ctx->sync_ctx->syncs) > 0) {
58eb2cb24dbeadd94500670acad7ceb1c8b0d9b4Timo Sirainen mbox_sync_apply_index_syncs(ctx->sync_ctx, &ctx->mail,
bf301a34ffbfd049be583094019b2644884b6d0bTimo Sirainen &keywords_changed);
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen if ((old_flags & XSTATUS_FLAGS_MASK) !=
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen (ctx->mail.flags & XSTATUS_FLAGS_MASK))
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen mbox_sync_update_xstatus(ctx);
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen if (keywords_changed)
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen mbox_sync_update_xkeywords(ctx);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen }
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen if (!ctx->sync_ctx->ibox->keep_recent)
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen ctx->mail.flags &= ~MAIL_RECENT;
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen if ((old_flags & STATUS_FLAGS_MASK) !=
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen (ctx->mail.flags & STATUS_FLAGS_MASK))
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen mbox_sync_update_status(ctx);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen mbox_sync_update_x_imap_base(ctx);
a60c1c1fca85402e6fccbf3ae0784b7179ae186cTimo Sirainen mbox_sync_update_x_uid(ctx);
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen mbox_sync_add_missing_headers(ctx);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->updated = TRUE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen}
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenvoid mbox_sync_update_header_from(struct mbox_sync_mail_context *ctx,
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen const struct mbox_sync_mail *mail)
7a6b45405fb1544ac476e6eb1402a70cc1ddcdcfTimo Sirainen{
ced118ac5caf6fe83d34339c2c65c63b2aa768acTimo Sirainen if ((ctx->mail.flags & STATUS_FLAGS_MASK) !=
d0d7fcf3ce44f26fdf34c1542a25cec644c5c4c7Timo Sirainen (mail->flags & STATUS_FLAGS_MASK) ||
08fa343b3aace9343da3195686c65c5326eda207Timo Sirainen (ctx->mail.flags & MAIL_RECENT) != 0) {
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen ctx->mail.flags = (ctx->mail.flags & ~STATUS_FLAGS_MASK) |
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen (mail->flags & STATUS_FLAGS_MASK);
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen if (!ctx->sync_ctx->ibox->keep_recent)
dc049c5e83d947aaf1b97c26ae819cc9577e0475Timo Sirainen ctx->mail.flags &= ~MAIL_RECENT;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen mbox_sync_update_status(ctx);
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen }
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen if ((ctx->mail.flags & XSTATUS_FLAGS_MASK) !=
33ae95df45c9b5ec51332a6b39eb5322038686b9Timo Sirainen (mail->flags & XSTATUS_FLAGS_MASK)) {
33ae95df45c9b5ec51332a6b39eb5322038686b9Timo Sirainen ctx->mail.flags = (ctx->mail.flags & ~XSTATUS_FLAGS_MASK) |
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (mail->flags & XSTATUS_FLAGS_MASK);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen mbox_sync_update_xstatus(ctx);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen }
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen if (!array_is_created(&mail->keywords) ||
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen array_count(&mail->keywords) == 0) {
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen /* no keywords for this mail */
19e8adccba16ff419f5675b1575358c2956dce83Timo Sirainen if (array_is_created(&ctx->mail.keywords)) {
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen array_clear(&ctx->mail.keywords);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen mbox_sync_update_xkeywords(ctx);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen }
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen } else if (!array_is_created(&ctx->mail.keywords)) {
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* adding first keywords */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ARRAY_CREATE(&ctx->mail.keywords,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ctx->sync_ctx->mail_keyword_pool,
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen unsigned int,
d0d7fcf3ce44f26fdf34c1542a25cec644c5c4c7Timo Sirainen array_count(&mail->keywords));
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen array_append_array(&ctx->mail.keywords,
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen &mail->keywords);
ced118ac5caf6fe83d34339c2c65c63b2aa768acTimo Sirainen mbox_sync_update_xkeywords(ctx);
d0d7fcf3ce44f26fdf34c1542a25cec644c5c4c7Timo Sirainen } else if (!buffer_cmp(ctx->mail.keywords.buffer,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen mail->keywords.buffer)) {
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* keywords changed. */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen array_clear(&ctx->mail.keywords);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen array_append_array(&ctx->mail.keywords,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen &mail->keywords);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen mbox_sync_update_xkeywords(ctx);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen }
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen i_assert(ctx->mail.uid == 0 || ctx->mail.uid == mail->uid);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ctx->mail.uid = mail->uid;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mbox_sync_update_x_imap_base(ctx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mbox_sync_update_x_uid(ctx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mbox_sync_add_missing_headers(ctx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen}
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen