imap-fetch.h revision f32c6ed9db6f4c535f97a2020401572efc8abf86
a8c5a86d183db25a57bf193c06b41e092ec2e151Timo Sirainen#ifndef IMAP_FETCH_H
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen#define IMAP_FETCH_H
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen
46552a931924c2d743f045e95b08c3ce6beda91aTimo Sirainenstruct imap_fetch_context;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenenum imap_fetch_handler_flags {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen IMAP_FETCH_HANDLER_FLAG_BUFFERED = 0x01,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen IMAP_FETCH_HANDLER_FLAG_WANT_DEINIT = 0x02
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen};
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen/* Returns 1 = ok, 0 = client output buffer full, call again, -1 = error.
de754cb78f75e8b3b994cddafe41c9ed1467c33dTimo Sirainen mail = NULL for deinit. */
f29756821a4c6b12b73e4a2a3e1c230117a43773Timo Sirainentypedef int imap_fetch_handler_t(struct imap_fetch_context *ctx,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mail *mail, void *context);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstruct imap_fetch_init_context {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct imap_fetch_context *fetch_ctx;
53dfcefa9440a49d703e49193819a79be99c9ba6Timo Sirainen pool_t pool;
f0d93763f210ecdb85a115fdd0210a16cfc5ff5cTimo Sirainen
53dfcefa9440a49d703e49193819a79be99c9ba6Timo Sirainen const char *name;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const struct imap_arg *args;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const char *error;
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen};
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainenstruct imap_fetch_handler {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const char *name;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
2e78f05b11df23ec2731afaf8f19d5b5240cb29fTimo Sirainen /* Returns FALSE and sets ctx->error if arg is invalid */
2e78f05b11df23ec2731afaf8f19d5b5240cb29fTimo Sirainen bool (*init)(struct imap_fetch_init_context *ctx);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen};
d1e7425048c61d71f41f737ba947687198842dc2Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstruct imap_fetch_context_handler {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen imap_fetch_handler_t *handler;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen void *context;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const char *name;
660b99a7059824676b2b8d6f79b8e15d47df25a2Timo Sirainen const char *nil_reply;
660b99a7059824676b2b8d6f79b8e15d47df25a2Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool buffered:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool want_deinit:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen};
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainenstruct imap_fetch_qresync_args {
b9c76fe9d9ca194816606342da1ddbd9be6bc8abTimo Sirainen const ARRAY_TYPE(uint32_t) *qresync_sample_seqset;
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen const ARRAY_TYPE(uint32_t) *qresync_sample_uidset;
b9c76fe9d9ca194816606342da1ddbd9be6bc8abTimo Sirainen};
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainenstruct imap_fetch_state {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mailbox_transaction_context *trans;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mail_search_context *search_ctx;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mail *cur_mail;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen unsigned int cur_handler;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const char *cur_human_name;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen uoff_t cur_size;
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen enum mail_fetch_field cur_size_field;
9f627b360ed38fdc54cb02ec5e67246c3f0d5b0fTimo Sirainen string_t *cur_str;
b9c76fe9d9ca194816606342da1ddbd9be6bc8abTimo Sirainen size_t cur_str_prefix_size;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct istream *cur_input;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool skip_cr;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen int (*cont_handler)(struct imap_fetch_context *ctx);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen uint64_t *cur_stats_sizep;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool fetching:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool seen_flags_changed:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* TRUE if the first FETCH parameter result hasn't yet been sent to
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen the IMAP client. Note that this doesn't affect buffered content in
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen cur_str until it gets flushed out. */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool cur_first:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen /* TRUE if the cur_str prefix has been flushed. More data may still
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen be added to it. */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool line_partial:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool skipped_expunged_msgs:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool failed:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen};
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenstruct imap_fetch_context {
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct client *client;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen pool_t ctx_pool;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const char *reason;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen enum mail_fetch_field fetch_data;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ARRAY_TYPE(const_string) all_headers;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ARRAY(struct imap_fetch_context_handler) handlers;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen unsigned int buffered_handlers_count;
ca843e046e98b12f4730f4b87ee2e1a659c26e78Timo Sirainen
ca843e046e98b12f4730f4b87ee2e1a659c26e78Timo Sirainen ARRAY_TYPE(keywords) tmp_keywords;
ca843e046e98b12f4730f4b87ee2e1a659c26e78Timo Sirainen
ca843e046e98b12f4730f4b87ee2e1a659c26e78Timo Sirainen struct imap_fetch_state state;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen ARRAY_TYPE(seq_range) fetch_failed_uids;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen unsigned int fetched_mails_count;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen enum mail_error error;
e7dd5065d21c569e5e6ddd713bd345dadd1cf51dTimo Sirainen const char *errstr;
e7dd5065d21c569e5e6ddd713bd345dadd1cf51dTimo Sirainen
d694a52bce62c52080c3f87a56dcf77030fd2712Timo Sirainen bool initialized:1;
d694a52bce62c52080c3f87a56dcf77030fd2712Timo Sirainen bool failures:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool flags_have_handler:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool flags_update_seen:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool flags_show_only_seen_changes:1;
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen};
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenvoid imap_fetch_handlers_register(const struct imap_fetch_handler *handlers,
2598b2f36365b52d9754b9348a5be29569293e46Timo Sirainen size_t count);
2598b2f36365b52d9754b9348a5be29569293e46Timo Sirainenvoid imap_fetch_handler_unregister(const char *name);
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainen
6303191abcb37164f435ccdc56e9dbddf1288851Timo Sirainenvoid imap_fetch_add_handler(struct imap_fetch_init_context *ctx,
1093de32efb2a231949566d4bd8aa55a8f43fb70Timo Sirainen enum imap_fetch_handler_flags flags,
1093de32efb2a231949566d4bd8aa55a8f43fb70Timo Sirainen const char *nil_reply,
ab281fc992907b6cf6c730f672dc3aa4c6685015Timo Sirainen imap_fetch_handler_t *handler, void *context)
ab281fc992907b6cf6c730f672dc3aa4c6685015Timo Sirainen ATTR_NULL(3, 5);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen#define imap_fetch_add_handler(ctx, flags, nil_reply, handler, context) \
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen imap_fetch_add_handler(ctx, flags, nil_reply + \
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen CALLBACK_TYPECHECK(handler, int (*)( \
1bf5c6c20f3d51f13d3240cfb46e471074c86276Timo Sirainen struct imap_fetch_context *, struct mail *, \
1bf5c6c20f3d51f13d3240cfb46e471074c86276Timo Sirainen typeof(context))), \
1bf5c6c20f3d51f13d3240cfb46e471074c86276Timo Sirainen (imap_fetch_handler_t *)handler, context)
1bf5c6c20f3d51f13d3240cfb46e471074c86276Timo Sirainen
5a250816ffc4cc5db203f9410ea99b6601c7b91aTimo Sirainenint imap_fetch_att_list_parse(struct client *client, pool_t pool,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const struct imap_arg *list,
1bf5c6c20f3d51f13d3240cfb46e471074c86276Timo Sirainen struct imap_fetch_context **fetch_ctx_r,
1bf5c6c20f3d51f13d3240cfb46e471074c86276Timo Sirainen const char **error_r);
1bf5c6c20f3d51f13d3240cfb46e471074c86276Timo Sirainen
1bf5c6c20f3d51f13d3240cfb46e471074c86276Timo Sirainenstruct imap_fetch_context *
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenimap_fetch_alloc(struct client *client, pool_t pool, const char *reason);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenvoid imap_fetch_free(struct imap_fetch_context **ctx);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenbool imap_fetch_init_handler(struct imap_fetch_init_context *init_ctx);
e5acc283bf030b0b5c79ca4e52d315c516a299faPascal Volkvoid imap_fetch_init_nofail_handler(struct imap_fetch_context *ctx,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen bool (*init)(struct imap_fetch_init_context *));
2e2a1d720ed53490e8e5c5031e773d395bd5683dTimo Sirainenconst struct imap_fetch_handler *imap_fetch_handler_lookup(const char *name);
5a250816ffc4cc5db203f9410ea99b6601c7b91aTimo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenvoid imap_fetch_begin(struct imap_fetch_context *ctx, struct mailbox *box,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct mail_search_args *search_args);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenint imap_fetch_send_vanished(struct client *client, struct mailbox *box,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const struct mail_search_args *search_args,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen const struct imap_fetch_qresync_args *qresync_args);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen/* Returns 1 if finished, 0 if more data is needed, -1 if error.
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen When 0 is returned, line_partial=TRUE if literal is open and must be
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen finished before anything else to client. */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenint imap_fetch_more(struct imap_fetch_context *ctx,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct client_command_context *cmd);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen/* Like imap_fetch_more(), but don't check/update output_lock.
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen The caller must handle this itself. */
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenint imap_fetch_more_no_lock_update(struct imap_fetch_context *ctx);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenint imap_fetch_end(struct imap_fetch_context *ctx);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenint imap_fetch_more(struct imap_fetch_context *ctx,
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen struct client_command_context *cmd);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainenbool imap_fetch_flags_init(struct imap_fetch_init_context *ctx);
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainenbool imap_fetch_modseq_init(struct imap_fetch_init_context *ctx);
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainenbool imap_fetch_uid_init(struct imap_fetch_init_context *ctx);
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainen
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainenbool imap_fetch_body_section_init(struct imap_fetch_init_context *ctx);
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainenbool imap_fetch_rfc822_init(struct imap_fetch_init_context *ctx);
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainenbool imap_fetch_binary_init(struct imap_fetch_init_context *ctx);
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainen
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainenvoid imap_fetch_handlers_init(void);
b8afdaa1bffe2f27cd4b02bf3bfbd2d297c8e648Timo Sirainenvoid imap_fetch_handlers_deinit(void);
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen#endif
e248fe370c4047cee921a91b48edc37944ab0526Timo Sirainen