a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen#ifndef DSYNC_IBC_H
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen#define DSYNC_IBC_H
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen/* dsync inter-brain communicator */
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen#include "ioloop.h"
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen#include "guid.h"
ce0e25f26d6e67480ee39b5ca0ad634fa60c4605Timo Sirainen#include "mail-error.h"
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen#include "dsync-brain.h"
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenstruct dsync_mailbox;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenstruct dsync_mailbox_state;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenstruct dsync_mailbox_node;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenstruct dsync_mailbox_delete;
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainenstruct dsync_mailbox_attribute;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenstruct dsync_mail;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenstruct dsync_mail_change;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenstruct dsync_mail_request;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_send_ret {
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen DSYNC_IBC_SEND_RET_OK = 1,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen /* send queue is full, stop sending more */
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen DSYNC_IBC_SEND_RET_FULL = 0
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen};
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_recv_ret {
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen DSYNC_IBC_RECV_RET_FINISHED = -1,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen /* try again / error (the error handling delayed until io callback) */
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen DSYNC_IBC_RECV_RET_TRYAGAIN = 0,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen DSYNC_IBC_RECV_RET_OK = 1
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen};
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
b8e6e314eb2f9f1fc8ce2999034321bfeb7a2269Timo Sirainenenum dsync_ibc_eol_type {
b8e6e314eb2f9f1fc8ce2999034321bfeb7a2269Timo Sirainen DSYNC_IBC_EOL_MAILBOX_STATE,
b8e6e314eb2f9f1fc8ce2999034321bfeb7a2269Timo Sirainen DSYNC_IBC_EOL_MAILBOX_TREE,
b8e6e314eb2f9f1fc8ce2999034321bfeb7a2269Timo Sirainen DSYNC_IBC_EOL_MAILBOX_ATTRIBUTE,
b8e6e314eb2f9f1fc8ce2999034321bfeb7a2269Timo Sirainen DSYNC_IBC_EOL_MAILBOX,
b8e6e314eb2f9f1fc8ce2999034321bfeb7a2269Timo Sirainen DSYNC_IBC_EOL_MAIL_CHANGES,
b8e6e314eb2f9f1fc8ce2999034321bfeb7a2269Timo Sirainen DSYNC_IBC_EOL_MAIL_REQUESTS,
b8e6e314eb2f9f1fc8ce2999034321bfeb7a2269Timo Sirainen DSYNC_IBC_EOL_MAILS
b8e6e314eb2f9f1fc8ce2999034321bfeb7a2269Timo Sirainen};
b8e6e314eb2f9f1fc8ce2999034321bfeb7a2269Timo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenstruct dsync_ibc_settings {
0e03baa885b89cd42fb76fe0282f272f035638cdTimo Sirainen /* Server hostname. Used for determining which server does the
0e03baa885b89cd42fb76fe0282f272f035638cdTimo Sirainen locking. */
0e03baa885b89cd42fb76fe0282f272f035638cdTimo Sirainen const char *hostname;
d487aa885845c33fb358d5b3b514eece6791db0eTimo Sirainen /* if non-NULL, sync only these namespaces (LF-separated) */
d487aa885845c33fb358d5b3b514eece6791db0eTimo Sirainen const char *sync_ns_prefixes;
707f172cd4e4c91af86ff5bd82a60a873dec6f70Timo Sirainen /* if non-NULL, sync only this mailbox name */
707f172cd4e4c91af86ff5bd82a60a873dec6f70Timo Sirainen const char *sync_box;
70df8f39fb3db7c49b18c855178f8172176a037aTimo Sirainen /* if non-NULL, use this mailbox for finding messages with GUIDs and
70df8f39fb3db7c49b18c855178f8172176a037aTimo Sirainen copying them instead of saving them again. */
70df8f39fb3db7c49b18c855178f8172176a037aTimo Sirainen const char *virtual_all_box;
9f99b5c3e607c41c16a6380203d401250d9e2603Timo Sirainen /* if non-empty, sync only this mailbox GUID */
9f99b5c3e607c41c16a6380203d401250d9e2603Timo Sirainen guid_128_t sync_box_guid;
fd32c46c360e61de2c957c3d2241eaacab7b3eecTimo Sirainen /* Exclude these mailboxes from the sync. They can contain '*'
fd32c46c360e61de2c957c3d2241eaacab7b3eecTimo Sirainen wildcards and be \special-use flags. */
fd32c46c360e61de2c957c3d2241eaacab7b3eecTimo Sirainen const char *const *exclude_mailboxes;
3561c7bb472a78af74d755219cc0fc71c85ff5c2Timo Sirainen /* Sync only mails with received timestamp at least this high. */
3561c7bb472a78af74d755219cc0fc71c85ff5c2Timo Sirainen time_t sync_since_timestamp;
ee8294dbc7bb549557f6ba1264d66b55fbef69b6Aki Tuomi /* Sync only mails with received timestamp less or equal than this */
ee8294dbc7bb549557f6ba1264d66b55fbef69b6Aki Tuomi time_t sync_until_timestamp;
ae949831f1f668b5501b4b125e7f7b1767fb109bTimo Sirainen /* Don't sync mails larger than this. */
ae949831f1f668b5501b4b125e7f7b1767fb109bTimo Sirainen uoff_t sync_max_size;
2e652d2651b2800f99a17dcb3014a009fe4660d3Timo Sirainen /* Sync only mails with specified flags. */
2e652d2651b2800f99a17dcb3014a009fe4660d3Timo Sirainen const char *sync_flags;
69a71891361b2b27ff68ed84b29278486628464aAki Tuomi /* Hashed headers */
69a71891361b2b27ff68ed84b29278486628464aAki Tuomi const char *const *hashed_headers;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen enum dsync_brain_sync_type sync_type;
e83126866761632b437e532dfdc30be01d14039dTimo Sirainen enum dsync_brain_flags brain_flags;
45af47783693b3ba2768c5ad34eeff68132382d0Timo Sirainen bool hdr_hash_v2;
f3c24c2c92802cb773315eba1132254932d8709bTimo Sirainen bool hdr_hash_v3;
0e03baa885b89cd42fb76fe0282f272f035638cdTimo Sirainen unsigned int lock_timeout;
a76faea3eb26c4cd67886fbe02c604f74d54be8cTimo Sirainen unsigned int import_commit_msgs_interval;
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen};
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenvoid dsync_ibc_init_pipe(struct dsync_ibc **ibc1_r,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen struct dsync_ibc **ibc2_r);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenstruct dsync_ibc *
5332128f69ad688cc024897c2a92f6b37ef8d05cTimo Sirainendsync_ibc_init_stream(struct istream *input, struct ostream *output,
1ff34185c13dffaab10e7691844ad7aec7c716d2Timo Sirainen const char *name, const char *temp_path_prefix,
1ff34185c13dffaab10e7691844ad7aec7c716d2Timo Sirainen unsigned int timeout_secs);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenvoid dsync_ibc_deinit(struct dsync_ibc **ibc);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen/* I/O callback is called whenever new data is available. It's also called on
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen errors, so check first the error status. */
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenvoid dsync_ibc_set_io_callback(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen io_callback_t *callback, void *context);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenvoid dsync_ibc_send_handshake(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_ibc_settings *set);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_recv_ret
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_recv_handshake(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_ibc_settings **set_r);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_send_ret ATTR_NOWARN_UNUSED_RESULT
b8e6e314eb2f9f1fc8ce2999034321bfeb7a2269Timo Sirainendsync_ibc_send_end_of_list(struct dsync_ibc *ibc, enum dsync_ibc_eol_type type);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_send_ret ATTR_NOWARN_UNUSED_RESULT
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_send_mailbox_state(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_mailbox_state *state);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_recv_ret
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_recv_mailbox_state(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen struct dsync_mailbox_state *state_r);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_send_ret ATTR_NOWARN_UNUSED_RESULT
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_send_mailbox_tree_node(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const char *const *name,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_mailbox_node *node);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_recv_ret
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_recv_mailbox_tree_node(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const char *const **name_r,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_mailbox_node **node_r);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_send_ret ATTR_NOWARN_UNUSED_RESULT
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_send_mailbox_deletes(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_mailbox_delete *deletes,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen unsigned int count, char hierarchy_sep);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_recv_ret
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_recv_mailbox_deletes(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_mailbox_delete **deletes_r,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen unsigned int *count_r, char *hierarchy_sep_r);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_send_ret ATTR_NOWARN_UNUSED_RESULT
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_send_mailbox(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_mailbox *dsync_box);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_recv_ret
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_recv_mailbox(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_mailbox **dsync_box_r);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainenenum dsync_ibc_send_ret ATTR_NOWARN_UNUSED_RESULT
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainendsync_ibc_send_mailbox_attribute(struct dsync_ibc *ibc,
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen const struct dsync_mailbox_attribute *attr);
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainenenum dsync_ibc_recv_ret
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainendsync_ibc_recv_mailbox_attribute(struct dsync_ibc *ibc,
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen const struct dsync_mailbox_attribute **attr_r);
55d33f807765482eb47374aaaced1fe714e0b256Timo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_send_ret ATTR_NOWARN_UNUSED_RESULT
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_send_change(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_mail_change *change);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_recv_ret
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_recv_change(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_mail_change **change_r);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_send_ret ATTR_NOWARN_UNUSED_RESULT
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_send_mail_request(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_mail_request *request);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_recv_ret
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_recv_mail_request(struct dsync_ibc *ibc,
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen const struct dsync_mail_request **request_r);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_send_ret ATTR_NOWARN_UNUSED_RESULT
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_send_mail(struct dsync_ibc *ibc, const struct dsync_mail *mail);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenenum dsync_ibc_recv_ret
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainendsync_ibc_recv_mail(struct dsync_ibc *ibc, struct dsync_mail **mail_r);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
ce0e25f26d6e67480ee39b5ca0ad634fa60c4605Timo Sirainenvoid dsync_ibc_send_finish(struct dsync_ibc *ibc, const char *error,
e229fe84553486b4ab37584f2a896a33384d0d70Timo Sirainen enum mail_error mail_error,
e229fe84553486b4ab37584f2a896a33384d0d70Timo Sirainen bool require_full_resync);
24bd831901b8fd59718e353b36eaef6a950f09a2Timo Sirainenenum dsync_ibc_recv_ret
ce0e25f26d6e67480ee39b5ca0ad634fa60c4605Timo Sirainendsync_ibc_recv_finish(struct dsync_ibc *ibc, const char **error_r,
e229fe84553486b4ab37584f2a896a33384d0d70Timo Sirainen enum mail_error *mail_error_r,
e229fe84553486b4ab37584f2a896a33384d0d70Timo Sirainen bool *require_full_resync_r);
24bd831901b8fd59718e353b36eaef6a950f09a2Timo Sirainen
f476a2abe41082176e65425358bf01bdcc86a41cTimo Sirainen/* Close any mail input streams that are kept open. This needs to be called
f476a2abe41082176e65425358bf01bdcc86a41cTimo Sirainen before the mail is attempted to be freed (usually on error conditions). */
f476a2abe41082176e65425358bf01bdcc86a41cTimo Sirainenvoid dsync_ibc_close_mail_streams(struct dsync_ibc *ibc);
f476a2abe41082176e65425358bf01bdcc86a41cTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenbool dsync_ibc_has_failed(struct dsync_ibc *ibc);
ea245d7a9683e7bb9cd74fcdf1a26d049b2947ebTimo Sirainenbool dsync_ibc_has_timed_out(struct dsync_ibc *ibc);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenbool dsync_ibc_is_send_queue_full(struct dsync_ibc *ibc);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainenbool dsync_ibc_has_pending_data(struct dsync_ibc *ibc);
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen
a85473f7c11c8734bdee9c2cbe4b767f144a18aaTimo Sirainen#endif