58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen#ifndef MAIL_DELIVER_H
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen#define MAIL_DELIVER_H
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
de62ce819d59a529530da4b57be1b8d6dad13d6bTimo Sirainen#include "guid.h"
a8fe899601735459641edae975c0fa08be8482e2Timo Sirainen#include "mail-types.h"
a8fe899601735459641edae975c0fa08be8482e2Timo Sirainen#include "mail-error.h"
de0971aff3009fe6906c3631e8322908658a5e93Stephan Bosch#include "smtp-params.h"
dbffb9e8b87b771851700a1acc353aa62b5ab7d7Timo Sirainen
629cb309f6f1bfce4ce0037c0ef089b66281f482Timo Sirainen#include <sys/time.h>
629cb309f6f1bfce4ce0037c0ef089b66281f482Timo Sirainen
3d7828efd92ecc0d08049f727d9be0154d1d681bStephan Boschstruct smtp_address;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenstruct mail_storage;
44f3f472a49078312432b785fddcfe7b95928391Timo Sirainenstruct mail_save_context;
f9db221d0793f05c4631885e71f98145428a7e1bTimo Sirainenstruct mailbox;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
44f3f472a49078312432b785fddcfe7b95928391Timo Sirainenstruct mail_deliver_session {
44f3f472a49078312432b785fddcfe7b95928391Timo Sirainen pool_t pool;
44f3f472a49078312432b785fddcfe7b95928391Timo Sirainen
dbffb9e8b87b771851700a1acc353aa62b5ab7d7Timo Sirainen /* List of INBOX GUIDs where this mail has already been saved to */
4ee00532a265bdfb38539d811fcd12d51210ac35Timo Sirainen ARRAY(guid_128_t) inbox_guids;
44f3f472a49078312432b785fddcfe7b95928391Timo Sirainen};
44f3f472a49078312432b785fddcfe7b95928391Timo Sirainen
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenstruct mail_deliver_context {
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen pool_t pool;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen const struct lda_settings *set;
0f72f253d5a117dad2f28b2ce336190a5e6b79c8Stephan Bosch const struct smtp_submit_settings *smtp_set;
44f3f472a49078312432b785fddcfe7b95928391Timo Sirainen struct mail_deliver_session *session;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
6b6011c2242e470b41316f92512b282b5e306dacTimo Sirainen unsigned int session_time_msecs;
6b6011c2242e470b41316f92512b282b5e306dacTimo Sirainen struct timeval delivery_time_started;
6b6011c2242e470b41316f92512b282b5e306dacTimo Sirainen
f41f6294f438bd16a35be64cef3bc90d2b66e0eaStephan Bosch struct mail_duplicate_db *dup_db;
4afaedfcbd43896befbe1fd5c10eba42246f3fdeTimo Sirainen
eed03830015b7138b9d4522e72bef650aa24b45fTimo Sirainen /* Session ID, used as log line prefix if non-NULL. */
eed03830015b7138b9d4522e72bef650aa24b45fTimo Sirainen const char *session_id;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen /* Mail to save */
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen struct mail *src_mail;
a77f136d3603a36f6cdd2e3685a5b18dd3712d79Stephan Bosch
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen /* Envelope sender, if known. */
3d7828efd92ecc0d08049f727d9be0154d1d681bStephan Bosch const struct smtp_address *mail_from;
de0971aff3009fe6906c3631e8322908658a5e93Stephan Bosch /* MAIL parameters */
de0971aff3009fe6906c3631e8322908658a5e93Stephan Bosch struct smtp_params_mail mail_params;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
a77f136d3603a36f6cdd2e3685a5b18dd3712d79Stephan Bosch /* Envelope recipient (final recipient) */
3d7828efd92ecc0d08049f727d9be0154d1d681bStephan Bosch const struct smtp_address *rcpt_to;
de0971aff3009fe6906c3631e8322908658a5e93Stephan Bosch /* RCPT parameters (can contain original recipient) */
de0971aff3009fe6906c3631e8322908658a5e93Stephan Bosch struct smtp_params_rcpt rcpt_params;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen /* Destination user */
a77f136d3603a36f6cdd2e3685a5b18dd3712d79Stephan Bosch struct mail_user *rcpt_user;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen /* Mailbox where mail should be saved, unless e.g. Sieve does
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen something to it. */
a77f136d3603a36f6cdd2e3685a5b18dd3712d79Stephan Bosch const char *rcpt_default_mailbox;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
2ef5254ab6b446b93ce7733bc96eeefa6f731ee4Timo Sirainen /* Filled with destination mail, if save_dest_mail=TRUE.
2ef5254ab6b446b93ce7733bc96eeefa6f731ee4Timo Sirainen The caller must free the mail, its transaction and close
2ef5254ab6b446b93ce7733bc96eeefa6f731ee4Timo Sirainen the mailbox. */
2ef5254ab6b446b93ce7733bc96eeefa6f731ee4Timo Sirainen struct mail *dest_mail;
2ef5254ab6b446b93ce7733bc96eeefa6f731ee4Timo Sirainen
12e28ffc2655b6d0efce628eb7d93fa74fbd345bTimo Sirainen /* mail_deliver_log() caches the var expand table values here */
12e28ffc2655b6d0efce628eb7d93fa74fbd345bTimo Sirainen struct mail_deliver_cache *cache;
291f1e54dcb8c7f38e5b78afc1eaf518e3756692Timo Sirainen
b9dce659b9135c87c7708b2bb0f14e8742db7e15Timo Sirainen /* Error message for a temporary failure. This is necessary only when
b9dce659b9135c87c7708b2bb0f14e8742db7e15Timo Sirainen there is no storage where to get the error message from. */
b9dce659b9135c87c7708b2bb0f14e8742db7e15Timo Sirainen const char *tempfail_error;
b9dce659b9135c87c7708b2bb0f14e8742db7e15Timo Sirainen
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen bool tried_default_save;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen bool saved_mail;
2ef5254ab6b446b93ce7733bc96eeefa6f731ee4Timo Sirainen bool save_dest_mail;
6682872f15fc74c1aa8898d4dc5745ff6a5dc51eTimo Sirainen /* Delivery failed because user is out of quota / disk space */
6682872f15fc74c1aa8898d4dc5745ff6a5dc51eTimo Sirainen bool mailbox_full;
6cc815e209fa75989ad2a309119d4f286a6ea00fTimo Sirainen /* Send DSN instead of MDN */
6cc815e209fa75989ad2a309119d4f286a6ea00fTimo Sirainen bool dsn;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen};
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
f9db221d0793f05c4631885e71f98145428a7e1bTimo Sirainenstruct mail_deliver_save_open_context {
f9db221d0793f05c4631885e71f98145428a7e1bTimo Sirainen struct mail_user *user;
f9db221d0793f05c4631885e71f98145428a7e1bTimo Sirainen bool lda_mailbox_autocreate;
f9db221d0793f05c4631885e71f98145428a7e1bTimo Sirainen bool lda_mailbox_autosubscribe;
f9db221d0793f05c4631885e71f98145428a7e1bTimo Sirainen};
f9db221d0793f05c4631885e71f98145428a7e1bTimo Sirainen
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainentypedef int deliver_mail_func_t(struct mail_deliver_context *ctx,
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen struct mail_storage **storage_r);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenextern deliver_mail_func_t *deliver_mail;
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
6b6011c2242e470b41316f92512b282b5e306dacTimo Sirainenconst struct var_expand_table *
6b6011c2242e470b41316f92512b282b5e306dacTimo Sirainenmail_deliver_ctx_get_log_var_expand_table(struct mail_deliver_context *ctx,
6b6011c2242e470b41316f92512b282b5e306dacTimo Sirainen const char *message);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenvoid mail_deliver_log(struct mail_deliver_context *ctx, const char *fmt, ...)
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen ATTR_FORMAT(2, 3);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
3d7828efd92ecc0d08049f727d9be0154d1d681bStephan Boschconst struct smtp_address *
3d7828efd92ecc0d08049f727d9be0154d1d681bStephan Boschmail_deliver_get_address(struct mail *mail, const char *header);
3d7828efd92ecc0d08049f727d9be0154d1d681bStephan Boschconst struct smtp_address *
3d7828efd92ecc0d08049f727d9be0154d1d681bStephan Boschmail_deliver_get_return_address(struct mail_deliver_context *ctx);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenconst char *mail_deliver_get_new_message_id(struct mail_deliver_context *ctx);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
44f3f472a49078312432b785fddcfe7b95928391Timo Sirainenstruct mail_deliver_session *mail_deliver_session_init(void);
44f3f472a49078312432b785fddcfe7b95928391Timo Sirainenvoid mail_deliver_session_deinit(struct mail_deliver_session **session);
44f3f472a49078312432b785fddcfe7b95928391Timo Sirainen
f9db221d0793f05c4631885e71f98145428a7e1bTimo Sirainen/* Try to open mailbox for saving. Returns 0 if ok, -1 if error. The box may
f9db221d0793f05c4631885e71f98145428a7e1bTimo Sirainen be returned even with -1, and the caller must free it then. */
f9db221d0793f05c4631885e71f98145428a7e1bTimo Sirainenint mail_deliver_save_open(struct mail_deliver_save_open_context *ctx,
f9db221d0793f05c4631885e71f98145428a7e1bTimo Sirainen const char *name, struct mailbox **box_r,
258adfa09081ea8600a39759d486e678b5aa5f60Timo Sirainen enum mail_error *error_r, const char **error_str_r);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenint mail_deliver_save(struct mail_deliver_context *ctx, const char *mailbox,
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen enum mail_flags flags, const char *const *keywords,
a10ed8c47534b4c6b6bf2711ccfe577e720a47b4Timo Sirainen struct mail_storage **storage_r) ATTR_NULL(4);
21fdf5394fbc307673b6c4b33f2497751a9ca1feTimo Sirainenvoid mail_deliver_deduplicate_guid_if_needed(struct mail_deliver_session *session,
21fdf5394fbc307673b6c4b33f2497751a9ca1feTimo Sirainen struct mail_save_context *save_ctx);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainenint mail_deliver(struct mail_deliver_context *ctx,
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen struct mail_storage **storage_r);
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen
92e6bb6497f8c9d57bff334a5c9f31bc2f040394Timo Sirainen/* Sets the deliver_mail hook and returns the previous hook,
92e6bb6497f8c9d57bff334a5c9f31bc2f040394Timo Sirainen which the new_hook should call if it's non-NULL. */
92e6bb6497f8c9d57bff334a5c9f31bc2f040394Timo Sirainendeliver_mail_func_t *mail_deliver_hook_set(deliver_mail_func_t *new_hook);
92e6bb6497f8c9d57bff334a5c9f31bc2f040394Timo Sirainen
eed56fe22f1fc61f2003d5e66373d03dacbc9aaaTimo Sirainen/* Must be called before any storage is created. */
eed56fe22f1fc61f2003d5e66373d03dacbc9aaaTimo Sirainenvoid mail_deliver_hooks_init(void);
eed56fe22f1fc61f2003d5e66373d03dacbc9aaaTimo Sirainen
58c61ac5650583d21c891e61e051c614290d31fbTimo Sirainen#endif