7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#ifndef DSYNC_MAIL_H
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#define DSYNC_MAIL_H
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#include "mail-types.h"
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
f0d1de444d0a5849b42e0c1d680a566fcace8aacTimo Sirainenstruct md5_context;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstruct mail;
f97c983e7a742e1d54ec60e502093abbb3ad2907Timo Sirainenstruct mailbox;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstruct dsync_mail {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* either GUID="" or uid=0 */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const char *guid;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen uint32_t uid;
8bc87e22fecd20797112b86778a961b08dc1f5c8Timo Sirainen time_t saved_date;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
f5d3d7a0e1e517c6dff9bb1750334c8d9e4dce99Timo Sirainen /* If non-NULL, we're syncing within the dsync process using ibc-pipe.
f5d3d7a0e1e517c6dff9bb1750334c8d9e4dce99Timo Sirainen This mail can be used to mailbox_copy() the mail. */
f5d3d7a0e1e517c6dff9bb1750334c8d9e4dce99Timo Sirainen struct mail *input_mail;
f5d3d7a0e1e517c6dff9bb1750334c8d9e4dce99Timo Sirainen /* Verify that this equals to input_mail->uid */
f5d3d7a0e1e517c6dff9bb1750334c8d9e4dce99Timo Sirainen uint32_t input_mail_uid;
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen /* TRUE if the following fields aren't set, because minimal_fill=TRUE
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen parameter was used. */
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen bool minimal_fields;
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen const char *pop3_uidl;
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen unsigned int pop3_order;
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen time_t received_date;
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen /* Input stream containing the message text, or NULL if all instances
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen of the message were already expunged from this mailbox. */
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen struct istream *input;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen};
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstruct dsync_mail_request {
d8bdf558c7ba173fc47a194633d9bd97af1b9c74Timo Sirainen /* either GUID=NULL or uid=0 */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const char *guid;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen uint32_t uid;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen};
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenenum dsync_mail_change_type {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen DSYNC_MAIL_CHANGE_TYPE_SAVE,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen DSYNC_MAIL_CHANGE_TYPE_EXPUNGE,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen DSYNC_MAIL_CHANGE_TYPE_FLAG_CHANGE
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen};
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#define KEYWORD_CHANGE_ADD '+'
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#define KEYWORD_CHANGE_REMOVE '-'
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#define KEYWORD_CHANGE_FINAL '='
252f5c6a63878e7a8a7ffb5847eecbad7f8737e8Timo Sirainen#define KEYWORD_CHANGE_ADD_AND_FINAL '&'
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenstruct dsync_mail_change {
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen enum dsync_mail_change_type type;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen uint32_t uid;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* Message's GUID:
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen - for expunges either 128bit hex or NULL if unknown
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen - "" if backend doesn't support GUIDs */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const char *guid;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* If GUID is "", this contains hash of the message header,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen otherwise NULL */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen const char *hdr_hash;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* Message's current modseq (saves, flag changes) */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen uint64_t modseq;
6abf66a3731d52889517bd644595c540e3a9b3ecTimo Sirainen /* Message's current private modseq (for private flags in
6abf66a3731d52889517bd644595c540e3a9b3ecTimo Sirainen shared mailboxes, otherwise 0) */
6abf66a3731d52889517bd644595c540e3a9b3ecTimo Sirainen uint64_t pvt_modseq;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* List of flag/keyword changes: (saves, flag changes) */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* Flags added/removed since last sync, and final flags containing
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen flags that exist now but haven't changed */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen uint8_t add_flags, remove_flags, final_flags;
6abf66a3731d52889517bd644595c540e3a9b3ecTimo Sirainen uint8_t add_pvt_flags, remove_pvt_flags;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen /* Remove all keywords before applying changes. This is used only with
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen old transaction logs, new ones never reset keywords (just explicitly
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen remove unwanted keywords) */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen bool keywords_reset;
252f5c6a63878e7a8a7ffb5847eecbad7f8737e8Timo Sirainen /* +add, -remove, =final, &add_and_final. */
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen ARRAY_TYPE(const_string) keyword_changes;
3561c7bb472a78af74d755219cc0fc71c85ff5c2Timo Sirainen
ee8294dbc7bb549557f6ba1264d66b55fbef69b6Aki Tuomi /* Received timestamp for saves, if brain.sync_since/until_timestamp is set */
3561c7bb472a78af74d755219cc0fc71c85ff5c2Timo Sirainen time_t received_timestamp;
ae949831f1f668b5501b4b125e7f7b1767fb109bTimo Sirainen /* Mail's size for saves if brain.sync_max_size is set,
ae949831f1f668b5501b4b125e7f7b1767fb109bTimo Sirainen (uoff_t)-1 otherwise. */
ae949831f1f668b5501b4b125e7f7b1767fb109bTimo Sirainen uoff_t virtual_size;
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen};
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
f97c983e7a742e1d54ec60e502093abbb3ad2907Timo Sirainenstruct mailbox_header_lookup_ctx *
69a71891361b2b27ff68ed84b29278486628464aAki Tuomidsync_mail_get_hash_headers(struct mailbox *box, const char *const *hashed_headers);
f97c983e7a742e1d54ec60e502093abbb3ad2907Timo Sirainen
45af47783693b3ba2768c5ad34eeff68132382d0Timo Sirainenint dsync_mail_get_hdr_hash(struct mail *mail, unsigned int version,
69a71891361b2b27ff68ed84b29278486628464aAki Tuomi const char *const *hashed_headers, const char **hdr_hash_r);
afd6d387ea65843b59fb6051fb567719d2a5279cAki Tuomistatic inline bool dsync_mail_hdr_hash_is_empty(const char *hdr_hash)
afd6d387ea65843b59fb6051fb567719d2a5279cAki Tuomi{
afd6d387ea65843b59fb6051fb567719d2a5279cAki Tuomi /* md5(\n) */
afd6d387ea65843b59fb6051fb567719d2a5279cAki Tuomi return strcmp(hdr_hash, "68b329da9893e34099c7d8ad5cb9c940") == 0;
afd6d387ea65843b59fb6051fb567719d2a5279cAki Tuomi}
afd6d387ea65843b59fb6051fb567719d2a5279cAki Tuomi
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainenint dsync_mail_fill(struct mail *mail, bool minimal_fill,
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen struct dsync_mail *dmail_r, const char **error_field_r);
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainenint dsync_mail_fill_nonminimal(struct mail *mail, struct dsync_mail *dmail_r,
d519a0449d0e536a32db93305516fdbd7db6773dTimo Sirainen const char **error_field_r);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainenvoid dsync_mail_change_dup(pool_t pool, const struct dsync_mail_change *src,
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen struct dsync_mail_change *dest_r);
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen
7bd5b1c64cc987715bdaf8cc4907c3c37d5d7b29Timo Sirainen#endif