imap-fetch.h revision 46e17407ed48155ac7290a992216a11b2b2b3cf4
5a580c3a38ced62d4bcc95b8ac7c4f2935b5d294Timo Sirainen#ifndef IMAP_FETCH_H
08b30498acefc69e223baf7eda6429be98cc3a10Timo Sirainen#define IMAP_FETCH_H
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
16f816d3f3c32ae3351834253f52ddd0212bcbf3Timo Sirainenstruct imap_fetch_context;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenenum imap_fetch_handler_flags {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen IMAP_FETCH_HANDLER_FLAG_BUFFERED = 0x01,
345212e8f61ebf14ff4f80df26df9e655eb5121eTimo Sirainen IMAP_FETCH_HANDLER_FLAG_WANT_DEINIT = 0x02
022412398e56a8f31ef111cfd7271498d64af9a9Timo Sirainen};
08b30498acefc69e223baf7eda6429be98cc3a10Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Returns 1 = ok, 0 = client output buffer full, call again, -1 = error.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen mail = NULL for deinit. */
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainentypedef int imap_fetch_handler_t(struct imap_fetch_context *ctx,
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen struct mail *mail, void *context);
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainenstruct imap_fetch_init_context {
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen struct imap_fetch_context *fetch_ctx;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen pool_t pool;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *name;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const struct imap_arg *args;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *error;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen};
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstruct imap_fetch_handler {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *name;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen /* Returns FALSE and sets ctx->error if arg is invalid */
d371507847d62ba311b4bcc23d18f45c3d0f1a38Timo Sirainen bool (*init)(struct imap_fetch_init_context *ctx);
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen};
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainenstruct imap_fetch_context_handler {
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen imap_fetch_handler_t *handler;
2d3aac5be07b96f72cf0551fac35ac74a4f07770Timo Sirainen void *context;
2d3aac5be07b96f72cf0551fac35ac74a4f07770Timo Sirainen
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen const char *name;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen const char *nil_reply;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen bool buffered:1;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen bool want_deinit:1;
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen};
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen
a205d315b0978985ba77d871f44e4a98273612e6Timo Sirainenstruct imap_fetch_qresync_args {
a205d315b0978985ba77d871f44e4a98273612e6Timo Sirainen const ARRAY_TYPE(uint32_t) *qresync_sample_seqset;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen const ARRAY_TYPE(uint32_t) *qresync_sample_uidset;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen};
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainenstruct imap_fetch_state {
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen struct mailbox_transaction_context *trans;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen struct mail_search_context *search_ctx;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen struct mail *cur_mail;
a6a2b38d806f3ab3198160e39240a8200775e525Timo Sirainen unsigned int cur_handler;
a6a2b38d806f3ab3198160e39240a8200775e525Timo Sirainen const char *cur_human_name;
a6a2b38d806f3ab3198160e39240a8200775e525Timo Sirainen uoff_t cur_size;
fb2e0bbb7737f3223b16aa41e4b40fb0cd5f288fTimo Sirainen enum mail_fetch_field cur_size_field;
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen string_t *cur_str;
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen size_t cur_str_prefix_size;
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen struct istream *cur_input;
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen bool skip_cr;
12d34ab79772e0748a1daef30fa749dfe3036608Timo Sirainen int (*cont_handler)(struct imap_fetch_context *ctx);
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen uint64_t *cur_stats_sizep;
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen bool fetching:1;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen bool seen_flags_changed:1;
f0569d9fbb25c8437760be69f194595a841ad711Timo Sirainen /* TRUE if the first FETCH parameter result hasn't yet been sent to
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen the IMAP client. Note that this doesn't affect buffered content in
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen cur_str until it gets flushed out. */
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen bool cur_first:1;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen /* TRUE if the cur_str prefix has been flushed. More data may still
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen be added to it. */
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen bool line_partial:1;
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen bool skipped_expunged_msgs:1;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen bool failed:1;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen};
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenstruct imap_fetch_context {
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen struct client *client;
821984ecb6c90696f35c32a8dc4c8a60f9e98f99Timo Sirainen pool_t ctx_pool;
821984ecb6c90696f35c32a8dc4c8a60f9e98f99Timo Sirainen
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen enum mail_fetch_field fetch_data;
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen ARRAY_TYPE(const_string) all_headers;
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ARRAY(struct imap_fetch_context_handler) handlers;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen unsigned int buffered_handlers_count;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ARRAY_TYPE(keywords) tmp_keywords;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen struct imap_fetch_state state;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ARRAY_TYPE(seq_range) fetch_failed_uids;
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen bool initialized:1;
e5759add4dc24b96606dccc4a989838e260f2a12Timo Sirainen bool failures:1;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen bool flags_have_handler:1;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen bool flags_update_seen:1;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen bool flags_show_only_seen_changes:1;
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen};
4efba37e4f27b93832f6147c3a353d6d22c855c7Timo Sirainen
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenvoid imap_fetch_handlers_register(const struct imap_fetch_handler *handlers,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen size_t count);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainenvoid imap_fetch_add_handler(struct imap_fetch_init_context *ctx,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen enum imap_fetch_handler_flags flags,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen const char *nil_reply,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen imap_fetch_handler_t *handler, void *context)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen ATTR_NULL(3, 5);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen#define imap_fetch_add_handler(ctx, flags, nil_reply, handler, context) \
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen imap_fetch_add_handler(ctx, flags, nil_reply + \
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen CALLBACK_TYPECHECK(handler, int (*)( \
abe8230dd1dd37d7ccf0163100e934bb5e658c20Timo Sirainen struct imap_fetch_context *, struct mail *, \
9aa52288a4b53186d81b0ec9afa7d9e0a8ee8753Timo Sirainen typeof(context))), \
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen (imap_fetch_handler_t *)handler, context)
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen
8bb360f9e5de1c25e4f875205bb06e8bf15dae14Timo Sirainenint imap_fetch_att_list_parse(struct client *client, pool_t pool,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen const struct imap_arg *list,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen struct imap_fetch_context **fetch_ctx_r,
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainen const char **error_r);
821984ecb6c90696f35c32a8dc4c8a60f9e98f99Timo Sirainen
821984ecb6c90696f35c32a8dc4c8a60f9e98f99Timo Sirainenstruct imap_fetch_context *
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenimap_fetch_alloc(struct client *client, pool_t pool);
313fe89df4d91cd0cd7f3558dc6d7fd21ad39eeeTimo Sirainenvoid imap_fetch_free(struct imap_fetch_context **ctx);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainenbool imap_fetch_init_handler(struct imap_fetch_init_context *init_ctx);
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainenvoid imap_fetch_init_nofail_handler(struct imap_fetch_context *ctx,
8d80659e504ffb34bb0c6a633184fece35751b18Timo Sirainen bool (*init)(struct imap_fetch_init_context *));
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
d22301419109ed4a38351715e6760011421dadecTimo Sirainenvoid imap_fetch_begin(struct imap_fetch_context *ctx, struct mailbox *box,
d22301419109ed4a38351715e6760011421dadecTimo Sirainen struct mail_search_args *search_args);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenint imap_fetch_send_vanished(struct client *client, struct mailbox *box,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const struct mail_search_args *search_args,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen const struct imap_fetch_qresync_args *qresync_args);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen/* Returns 1 if finished, 0 if more data is needed, -1 if error.
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen When 0 is returned, line_partial=TRUE if literal is open and must be
d22301419109ed4a38351715e6760011421dadecTimo Sirainen finished before anything else to client. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenint imap_fetch_more(struct imap_fetch_context *ctx,
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen struct client_command_context *cmd);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen/* Like imap_fetch_more(), but don't check/update output_lock.
197ad81605dc0f6d2ebc9ad99994db5ca6d76699Timo Sirainen The caller must handle this itself. */
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenint imap_fetch_more_no_lock_update(struct imap_fetch_context *ctx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenint imap_fetch_end(struct imap_fetch_context *ctx);
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainenint imap_fetch_more(struct imap_fetch_context *ctx,
5363a534097c170ef9cccbdde5ca802f581f5eb7Timo Sirainen struct client_command_context *cmd);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenbool imap_fetch_flags_init(struct imap_fetch_init_context *ctx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenbool imap_fetch_modseq_init(struct imap_fetch_init_context *ctx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenbool imap_fetch_uid_init(struct imap_fetch_init_context *ctx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenbool imap_fetch_body_section_init(struct imap_fetch_init_context *ctx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenbool imap_fetch_rfc822_init(struct imap_fetch_init_context *ctx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenbool imap_fetch_binary_init(struct imap_fetch_init_context *ctx);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenvoid imap_fetch_handlers_init(void);
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainenvoid imap_fetch_handlers_deinit(void);
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen
2054222e84cb972842cc4de88e16516bef41b542Timo Sirainen#endif
bb10ebcf076c959c752f583746d83805d7686df8Timo Sirainen