From: Timo Sirainen Date: Wed, 17 Mar 2021 15:28:03 +0000 (+0200) Subject: imap: Add reason_code=imap:fetch_* X-Git-Tag: 2.3.18~347 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=bb1df6771968d9da0e6de4451f425714f5dfc3dc;p=thirdparty%2Fdovecot%2Fcore.git imap: Add reason_code=imap:fetch_* The main reason for these is to allow understanding why mails are being opened. The possibilities: * imap:fetch_body * imap:fetch_header * imap:fetch_header_fields * imap:fetch_bodystructure * imap:fetch_size --- diff --git a/src/imap/cmd-fetch.c b/src/imap/cmd-fetch.c index d05d8d1975..35e85d15d3 100644 --- a/src/imap/cmd-fetch.c +++ b/src/imap/cmd-fetch.c @@ -283,6 +283,46 @@ static bool cmd_fetch_continue(struct client_command_context *cmd) return cmd_fetch_finish(ctx, cmd); } +static void cmd_fetch_set_reason_codes(struct client_command_context *cmd, + struct imap_fetch_context *ctx) +{ + /* Fetching body or header always causes the message to be opened. + Use them as the primary reason. */ + if ((ctx->fetch_data & MAIL_FETCH_STREAM_BODY) != 0) { + event_strlist_append(cmd->global_event, EVENT_REASON_CODE, + "imap:fetch_body"); + return; + } + if ((ctx->fetch_data & MAIL_FETCH_STREAM_HEADER) != 0) { + event_strlist_append(cmd->global_event, EVENT_REASON_CODE, + "imap:fetch_header"); + return; + } + + /* The rest of these can come from cache. Since especially with + prefetching we can't really know which one of them specifically + triggered opening the mail, just use all of them as the reasons. */ + if (ctx->fetch_header_fields || + HAS_ANY_BITS(ctx->fetch_data, + MAIL_FETCH_IMAP_ENVELOPE | + MAIL_FETCH_DATE)) { + event_strlist_append(cmd->global_event, EVENT_REASON_CODE, + "imap:fetch_header_fields"); + } + if (HAS_ANY_BITS(ctx->fetch_data, + MAIL_FETCH_IMAP_BODY | + MAIL_FETCH_IMAP_BODYSTRUCTURE)) { + event_strlist_append(cmd->global_event, EVENT_REASON_CODE, + "imap:fetch_bodystructure"); + } + if (HAS_ANY_BITS(ctx->fetch_data, + MAIL_FETCH_PHYSICAL_SIZE | + MAIL_FETCH_VIRTUAL_SIZE)) { + event_strlist_append(cmd->global_event, EVENT_REASON_CODE, + "imap:fetch_size"); + } +} + bool cmd_fetch(struct client_command_context *cmd) { struct client *client = cmd->client; @@ -335,6 +375,7 @@ bool cmd_fetch(struct client_command_context *cmd) } } + cmd_fetch_set_reason_codes(cmd, ctx); imap_fetch_begin(ctx, client->mailbox, search_args); mail_search_args_unref(&search_args); diff --git a/src/imap/imap-fetch-body.c b/src/imap/imap-fetch-body.c index 39e66f093d..b467334ec3 100644 --- a/src/imap/imap-fetch-body.c +++ b/src/imap/imap-fetch-body.c @@ -251,6 +251,8 @@ body_header_fields_parse(struct imap_fetch_init_context *ctx, const char *value; size_t i; + ctx->fetch_ctx->fetch_header_fields = TRUE; + str = str_new(ctx->pool, 128); str_append(str, prefix); str_append(str, " ("); diff --git a/src/imap/imap-fetch.h b/src/imap/imap-fetch.h index 39a0b9d4fa..ba8cfc80a9 100644 --- a/src/imap/imap-fetch.h +++ b/src/imap/imap-fetch.h @@ -100,6 +100,8 @@ struct imap_fetch_context { bool flags_have_handler:1; bool flags_update_seen:1; bool flags_show_only_seen_changes:1; + /* HEADER.FIELDS or HEADER.FIELDS.NOT is fetched */ + bool fetch_header_fields:1; }; void imap_fetch_handlers_register(const struct imap_fetch_handler *handlers,