mbox-sync-private.h revision 3343a61404603b21c246783a7963b77833095f31
5f5870385cff47efd2f58e7892f251cf13761528Timo Sirainen#ifndef __MBOX_SYNC_PRIVATE_H
1d4f710106fb498750456724628da6063e012e6dTimo Sirainen#define __MBOX_SYNC_PRIVATE_H
46552a931924c2d743f045e95b08c3ce6beda91aTimo Sirainen
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen#include "md5.h"
d3d769026fae5d21c2d29614d3bc4579e8d79e81Timo Sirainen#include "mail-index.h"
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenenum mbox_sync_flags {
baebb412a9a5a44b1756e01cfa3b99f5d8a846b6Timo Sirainen MBOX_SYNC_LAST_COMMIT = 0x01,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen MBOX_SYNC_HEADER = 0x02,
b55f914c0ade77252cfd798ea8eb9a84bda56315Timo Sirainen MBOX_SYNC_LOCK_READING = 0x04,
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen MBOX_SYNC_UNDIRTY = 0x08
b55f914c0ade77252cfd798ea8eb9a84bda56315Timo Sirainen};
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstruct mbox_flag_type {
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen char chr;
92c49f3005f4dff1a6f576fffa8112ef6d1cae7fTimo Sirainen enum mail_flags flag;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen};
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenenum header_position {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen MBOX_HDR_STATUS,
4ead43ecc06d10047998966c4dc0b142ecce4b66Timo Sirainen MBOX_HDR_X_IMAPBASE,
1d4f710106fb498750456724628da6063e012e6dTimo Sirainen MBOX_HDR_X_KEYWORDS,
1d4f710106fb498750456724628da6063e012e6dTimo Sirainen MBOX_HDR_X_STATUS,
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen MBOX_HDR_X_UID,
9439bed2f07d6475febd8a247cd2f0990fb32a13Timo Sirainen
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen MBOX_HDR_COUNT
ab0d9eecd85f74acae18fe88529302e0776cc500Timo Sirainen};
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen#define MBOX_NONRECENT MAIL_RECENT /* kludgy */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen#define MBOX_EXPUNGED 0x40
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen#define MBOX_DIRTY_SPACE 0x80
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen#define STATUS_FLAGS_MASK (MAIL_SEEN|MBOX_NONRECENT)
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen#define XSTATUS_FLAGS_MASK (MAIL_ANSWERED|MAIL_FLAGGED|MAIL_DRAFT|MAIL_DELETED)
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainenextern struct mbox_flag_type mbox_status_flags[];
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainenextern struct mbox_flag_type mbox_xstatus_flags[];
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainenstruct mbox_sync_mail {
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen uint32_t uid;
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen uint8_t flags;
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen keywords_mask_t keywords;
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen uoff_t from_offset;
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen uoff_t body_size;
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen /* following variables have a bit overloaded functionality:
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen a) space <= 0 : offset points to beginning of headers. space is the
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen amount of space missing that is required to be able to rewrite
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen the headers
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen b) space > 0 : offset points to beginning of whitespace that can
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen be removed. space is the amount of data that can be removed from
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen there. note that the message may contain more whitespace
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen elsewhere. */
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen uoff_t offset;
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen off_t space;
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen};
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen
ed16ab579bd058ec5e2b5d02bb41fdadd9e05b31Timo Sirainenstruct mbox_sync_mail_context {
7a94f950fd1dcc81537acfc8adb030b5e703d722Timo Sirainen struct mbox_sync_context *sync_ctx;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mbox_sync_mail mail;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen uint32_t seq;
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainen uoff_t from_offset, hdr_offset, body_offset;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen size_t header_first_change, header_last_change;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen string_t *header, *uidl;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e2a88d59c0d47d63ce1ad5b1fd95e487124a3fd4Timo Sirainen struct md5_context hdr_md5_ctx;
baebb412a9a5a44b1756e01cfa3b99f5d8a846b6Timo Sirainen unsigned char hdr_md5_sum[16];
baebb412a9a5a44b1756e01cfa3b99f5d8a846b6Timo Sirainen
baebb412a9a5a44b1756e01cfa3b99f5d8a846b6Timo Sirainen uoff_t content_length;
baebb412a9a5a44b1756e01cfa3b99f5d8a846b6Timo Sirainen
baebb412a9a5a44b1756e01cfa3b99f5d8a846b6Timo Sirainen size_t hdr_pos[MBOX_HDR_COUNT];
baebb412a9a5a44b1756e01cfa3b99f5d8a846b6Timo Sirainen uint32_t parsed_uid;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen unsigned int have_eoh:1;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen unsigned int need_rewrite:1;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen unsigned int seen_imapbase:1;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen unsigned int pseudo:1;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen unsigned int updated:1;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen unsigned int seen_received_hdr:1;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen};
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainenstruct mbox_sync_context {
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen struct index_mailbox *ibox;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen struct istream *input, *file_input;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen int fd;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen struct mail_index_sync_ctx *index_sync_ctx;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen struct mail_index_view *sync_view;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen struct mail_index_transaction *t;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen const struct mail_index_header *hdr;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen string_t *header, *uidl, *from_line;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen /* header state: */
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen uint32_t base_uid_validity, base_uid_last;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen uint32_t update_base_uid_last;
12c6ef6f1268ed4d5b63709bb4215c481b4f078cTimo Sirainen
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Sirainen /* mail state: */
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Sirainen buffer_t *mails, *syncs;
f29756821a4c6b12b73e4a2a3e1c230117a43773Timo Sirainen struct mail_index_sync_rec sync_rec;
f29756821a4c6b12b73e4a2a3e1c230117a43773Timo Sirainen
f29756821a4c6b12b73e4a2a3e1c230117a43773Timo Sirainen uint32_t prev_msg_uid, next_uid;
f059a046515f4b2b15a6c2a10a6f12f6166e39a5Timo Sirainen uint32_t seq, idx_seq, need_space_seq, need_space_idx_seq;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen off_t expunged_space, space_diff;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen unsigned int dest_first_mail:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen unsigned int seen_first_mail:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen};
5a250816ffc4cc5db203f9410ea99b6601c7b91aTimo Sirainen
5a250816ffc4cc5db203f9410ea99b6601c7b91aTimo Sirainenint mbox_sync(struct index_mailbox *ibox, enum mbox_sync_flags flags);
46ec792dd4ccf6c34706c4774228301fafde6aa9Timo Sirainenint mbox_sync_has_changed(struct index_mailbox *ibox, int leave_dirty);
46ec792dd4ccf6c34706c4774228301fafde6aa9Timo Sirainen
46ec792dd4ccf6c34706c4774228301fafde6aa9Timo Sirainenvoid mbox_sync_parse_next_mail(struct istream *input,
46ec792dd4ccf6c34706c4774228301fafde6aa9Timo Sirainen struct mbox_sync_mail_context *ctx);
4c6ddf2491104f917d00e6900e833e80ea02c7b6Timo Sirainenint mbox_sync_parse_match_mail(struct index_mailbox *ibox,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mail_index_view *view, uint32_t seq);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenvoid mbox_sync_update_header(struct mbox_sync_mail_context *ctx,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen buffer_t *syncs_buf);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenvoid mbox_sync_update_header_from(struct mbox_sync_mail_context *ctx,
f29756821a4c6b12b73e4a2a3e1c230117a43773Timo Sirainen const struct mbox_sync_mail *mail);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenint mbox_sync_try_rewrite(struct mbox_sync_mail_context *ctx, off_t move_diff,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen int leave_space_hole);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenint mbox_sync_rewrite(struct mbox_sync_context *sync_ctx, uoff_t extra_space,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen uint32_t first_seq, uint32_t last_seq);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenint mbox_sync_seek(struct mbox_sync_context *sync_ctx, uoff_t from_offset);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenint mbox_move(struct mbox_sync_context *sync_ctx,
ddbdc644a15f56f4b43596f1b8c0fc196c101445Timo Sirainen uoff_t dest, uoff_t source, uoff_t size);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenvoid mbox_sync_move_buffer(struct mbox_sync_mail_context *ctx,
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen size_t pos, size_t need, size_t have);
6fdfa4d4cf14d1d7764d7faa8258f112e39c8dbeTimo Sirainen
d3d769026fae5d21c2d29614d3bc4579e8d79e81Timo Sirainen#endif
ad004e44be109684521494b5af2ad1da39b8bb27Timo Sirainen