bcb4e51a409d94ae670de96afb8483a4f7855294Stephan Bosch/* Copyright (c) 2004-2018 Dovecot authors, see the included COPYING file */
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen/* Line length when to wrap X-IMAP, X-IMAPbase and X-Keywords headers to next
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen line. Keep this pretty long, as after we wrap we lose compatibility with
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void status_flags_append(struct mbox_sync_mail_context *ctx,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen if ((ctx->mail.flags & flags_list[i].flag) != 0)
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainenvoid mbox_sync_move_buffer(struct mbox_sync_mail_context *ctx,
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen /* FIXME: if (diff < ctx->space && pos < ctx->offset) then
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen move the data only up to space offset and give/take the
a205d315b0978985ba77d871f44e4a98273612e6Timo Sirainen space from there. update header_last_change accordingly.
a205d315b0978985ba77d871f44e4a98273612e6Timo Sirainen (except pos and offset can't be compared directly) */
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen for (i = 0; i < MBOX_HDR_COUNT; i++) {
a6a2b38d806f3ab3198160e39240a8200775e525Timo Sirainen i_assert(ctx->mail.offset + ctx->mail.space <=
a6a2b38d806f3ab3198160e39240a8200775e525Timo Sirainen ctx->mail.offset > ctx->hdr_offset + pos + have);
fb2e0bbb7737f3223b16aa41e4b40fb0cd5f288fTimo Sirainen if (ctx->mail.offset > ctx->hdr_offset + pos) {
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen /* free space offset moves */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenstatic void status_flags_replace(struct mbox_sync_mail_context *ctx, size_t pos,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* how many bytes do we 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 /* how many bytes do we have now? */
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen data = buffer_get_modifiable_data(ctx->header, &size);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* see if this is unknown flag for us */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* save this one */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen /* @UNSAFE */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen data = buffer_get_space_unsafe(ctx->header, pos, need);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen if ((ctx->mail.flags & flags_list[i].flag) != 0)
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainenkeywords_append(struct mbox_sync_context *sync_ctx, string_t *dest,
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen const ARRAY_TYPE(keyword_indexes) *keyword_indexes_arr)
d22301419109ed4a38351715e6760011421dadecTimo Sirainen keyword_names = array_get(ibox->keyword_names,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen keyword_indexes = array_get(keyword_indexes_arr, &idx_count);
197ad81605dc0f6d2ebc9ad99994db5ca6d76699Timo Sirainen for (i = 0, last_break = str_len(dest); i < idx_count; i++) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen i_assert(keyword_indexes[i] < keywords_count);
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen /* wrap the line whenever it gets too long */
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen if (str_len(dest) - last_break < KEYWORD_WRAP_LINE_LENGTH) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen str_append(dest, keyword_names[keyword_indexes[i]]);
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainenkeywords_append_all(struct mbox_sync_mail_context *ctx, string_t *dest,
d22301419109ed4a38351715e6760011421dadecTimo Sirainen INDEX_STORAGE_CONTEXT(&ctx->sync_ctx->mbox->box);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const unsigned char *p;
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen if (str_len(dest) - startpos < KEYWORD_WRAP_LINE_LENGTH)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* set last_break to beginning of line */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen for (last_break = str_len(dest); last_break > 0; last_break--) {
d22301419109ed4a38351715e6760011421dadecTimo Sirainen names = array_get(ibox->keyword_names, &count);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen for (i = 0; i < count; i++) {
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen /* wrap the line whenever it gets too long */
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen if (str_len(dest) - last_break < KEYWORD_WRAP_LINE_LENGTH)
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void mbox_sync_add_missing_headers(struct mbox_sync_mail_context *ctx)
7f773564b94e6054a40d3785cb63c29f1e4d4deeTimo Sirainen str_data(ctx->header)[new_hdr_size-1] != '\n') {
7f773564b94e6054a40d3785cb63c29f1e4d4deeTimo Sirainen /* broken header - doesn't end with \n. fix it. */
33ae95df45c9b5ec51332a6b39eb5322038686b9Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_IMAPBASE] == (size_t)-1) {
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen i_assert(ctx->sync_ctx->base_uid_validity != 0);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_IMAPBASE] = str_len(ctx->header);
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen /* startpos must start from identical position as when
bf301a34ffbfd049be583094019b2644884b6d0bTimo Sirainen ctx->last_uid_updated_value = ctx->sync_ctx->next_uid-1;
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen ctx->last_uid_value_start_pos = str_len(ctx->header) -
bf301a34ffbfd049be583094019b2644884b6d0bTimo Sirainen str_printfa(ctx->header, "%010u", ctx->last_uid_updated_value);
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen keywords_append_all(ctx, ctx->header, startpos);
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_UID] == (size_t)-1 && !ctx->mail.pseudo) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_UID] = str_len(ctx->header);
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen str_printfa(ctx->header, "%u\n", ctx->mail.uid);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->hdr_pos[MBOX_HDR_STATUS] == (size_t)-1 &&
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen ctx->hdr_pos[MBOX_HDR_STATUS] = str_len(ctx->header);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_STATUS] == (size_t)-1 &&
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen (ctx->mail.flags & XSTATUS_FLAGS_MASK) != 0) {
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_STATUS] = str_len(ctx->header);
16c89b1260c9d07c01c83a9219424d3727069b2eTimo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_KEYWORDS] == (size_t)-1 &&
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_KEYWORDS] = str_len(ctx->header);
08b30498acefc69e223baf7eda6429be98cc3a10Timo Sirainen ctx->mail.body_size >= MBOX_MIN_CONTENT_LENGTH_SIZE) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen str_printfa(ctx->header, "Content-Length: %"PRIuUOFF_T"\n",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void mbox_sync_update_status(struct mbox_sync_mail_context *ctx)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen if (ctx->hdr_pos[MBOX_HDR_STATUS] != (size_t)-1) {
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen status_flags_replace(ctx, ctx->hdr_pos[MBOX_HDR_STATUS],
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic void mbox_sync_update_xstatus(struct mbox_sync_mail_context *ctx)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_STATUS] != (size_t)-1) {
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen status_flags_replace(ctx, ctx->hdr_pos[MBOX_HDR_X_STATUS],
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainenstatic void mbox_sync_update_line(struct mbox_sync_mail_context *ctx,
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen /* set p = end of header, handle also wrapped headers */
92139717fd109c34692670df54d157d8c4df9b71Timo Sirainen /* shouldn't really happen, but allow anyway.. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (ctx->mail.space > 0 && ctx->mail.offset >= file_pos &&
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* extra space points to this line. remove it. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mbox_sync_move_buffer(ctx, pos, str_len(new_line), p - hdr + 1);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen buffer_copy(ctx->header, pos, new_line, 0, (size_t)-1);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenstatic void mbox_sync_update_xkeywords(struct mbox_sync_mail_context *ctx)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_KEYWORDS] == (size_t)-1)
1cd97699af9c77d8f5920832ec3374884544fd68Timo Sirainen keywords_append(ctx->sync_ctx, str, &ctx->mail.keywords);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen mbox_sync_update_line(ctx, ctx->hdr_pos[MBOX_HDR_X_KEYWORDS], str);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainenstatic void mbox_sync_update_x_imap_base(struct mbox_sync_mail_context *ctx)
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen struct mbox_sync_context *sync_ctx = ctx->sync_ctx;
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen ctx->hdr_pos[MBOX_HDR_X_IMAPBASE] == (size_t)-1)
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen /* uid-last might need updating, but we'll do it later by
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen writing it directly where needed. */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen /* a) keyword list changed, b) uid-last didn't use 10 digits */
84e1634acc701d14e358e27f1beff5ad74f5004aTimo Sirainen str_printfa(str, "%u ", sync_ctx->base_uid_validity);
bf301a34ffbfd049be583094019b2644884b6d0bTimo Sirainen ctx->last_uid_updated_value = sync_ctx->next_uid-1;
bf301a34ffbfd049be583094019b2644884b6d0bTimo Sirainen str_printfa(str, "%010u", ctx->last_uid_updated_value);
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen mbox_sync_update_line(ctx, ctx->hdr_pos[MBOX_HDR_X_IMAPBASE], str);
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainenstatic void mbox_sync_update_x_uid(struct mbox_sync_mail_context *ctx)
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen if (ctx->hdr_pos[MBOX_HDR_X_UID] == (size_t)-1 ||
6843896c40bee4f9b6680ca7ced598c446e9f999Timo Sirainen mbox_sync_update_line(ctx, ctx->hdr_pos[MBOX_HDR_X_UID], str);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainenstatic void mbox_sync_update_header_real(struct mbox_sync_mail_context *ctx)
1e76a5b92f9d82d557f81f080f3dfad1c9d8f200Timo Sirainen i_assert(ctx->mail.uid != 0 || ctx->mail.pseudo);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainenvoid mbox_sync_update_header(struct mbox_sync_mail_context *ctx)
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainenmbox_sync_update_header_from_real(struct mbox_sync_mail_context *ctx,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ctx->mail.flags = (ctx->mail.flags & ~STATUS_FLAGS_MASK) |
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ctx->mail.flags = (ctx->mail.flags & ~XSTATUS_FLAGS_MASK) |
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* no keywords for this mail */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen } else if (!array_is_created(&ctx->mail.keywords)) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* adding first keywords */
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen } else if (!array_cmp(&ctx->mail.keywords, &mail->keywords)) {
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen /* keywords changed. */
dc52b16795b56589923ff586e5cdd0c0f1fd5931Timo Sirainen i_assert(ctx->mail.uid == 0 || ctx->mail.uid == mail->uid);