]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
imap: Add reason_code=imap:fetch_*
authorTimo Sirainen <timo.sirainen@open-xchange.com>
Wed, 17 Mar 2021 15:28:03 +0000 (17:28 +0200)
committeraki.tuomi <aki.tuomi@open-xchange.com>
Wed, 29 Sep 2021 10:09:58 +0000 (10:09 +0000)
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

src/imap/cmd-fetch.c
src/imap/imap-fetch-body.c
src/imap/imap-fetch.h

index d05d8d197561b85fd8b403a3ba9c8adc65f79f9f..35e85d15d366e37facb72684b52d8a1f81c2f105 100644 (file)
@@ -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);
 
index 39e66f093d2f349c8dd46442a590be1932e818bf..b467334ec309d88181c56040f0f4d46b0fd56c9a 100644 (file)
@@ -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, " (");
index 39a0b9d4faa2e24e4856b80f950d1caa2170efec..ba8cfc80a9851468ea321375c3c884703b181a54 100644 (file)
@@ -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,