From 50af4232c16b883a2629c869c4df090caf0805b4 Mon Sep 17 00:00:00 2001 From: Marco Bettini Date: Tue, 9 May 2023 07:22:15 +0000 Subject: [PATCH] lib-storage: Add mail_get_message_id_no_validation() --- src/lib-storage/mail-storage.h | 3 +++ src/lib-storage/mail.c | 35 ++++++++++++++++++++++++++-------- 2 files changed, 30 insertions(+), 8 deletions(-) diff --git a/src/lib-storage/mail-storage.h b/src/lib-storage/mail-storage.h index 64e0e49a34..1abf5d4cbd 100644 --- a/src/lib-storage/mail-storage.h +++ b/src/lib-storage/mail-storage.h @@ -983,6 +983,9 @@ int mail_get_backend_mail(struct mail *mail, struct mail **real_mail_r); '>' in the *value_r return parameter or NULL if the header wasn't found or its value was invalid. */ int mail_get_message_id(struct mail *mail, const char **value_r); +/* Try to retrieve a properly parsed Message-ID header, if that fails return + the raw header value as it is. */ +int mail_get_message_id_no_validation(struct mail *mail, const char **value_r); /* Update message flags. */ void mail_update_flags(struct mail *mail, enum modify_type modify_type, diff --git a/src/lib-storage/mail.c b/src/lib-storage/mail.c index b3991d6f65..0054ba80df 100644 --- a/src/lib-storage/mail.c +++ b/src/lib-storage/mail.c @@ -427,24 +427,43 @@ int mail_get_backend_mail(struct mail *mail, struct mail **real_mail_r) return p->v.get_backend_mail(mail, real_mail_r); } -int mail_get_message_id(struct mail *mail, const char **value_r) +static int mail_get_message_id_full(struct mail *mail, + const char **value_r, + bool require_valid) { const char *hdr_value, *msgid_bare; int ret; - *value_r = NULL; - ret = mail_get_first_header(mail, "Message-ID", &hdr_value); - if (ret <= 0) + if (ret <= 0) { + *value_r = NULL; return ret; + } + *value_r = hdr_value; /* save it now as next function alters it */ msgid_bare = message_id_get_next(&hdr_value); - if (msgid_bare == NULL) + + if (msgid_bare != NULL) { + /* Complete the message ID with surrounding `<' and `>'. */ + *value_r = t_strconcat("<", msgid_bare, ">", NULL); + return 1; + } else if (!require_valid) { + /* *value_r already set above */ + return 1; + } else { + *value_r = NULL; return 0; + } +} + +int mail_get_message_id(struct mail *mail, const char **value_r) +{ + return mail_get_message_id_full(mail, value_r, TRUE/*require_valid*/); +} - /* Complete the message ID with surrounding `<' and `>'. */ - *value_r = t_strconcat("<", msgid_bare, ">", NULL); - return 1; +int mail_get_message_id_no_validation(struct mail *mail, const char **value_r) +{ + return mail_get_message_id_full(mail, value_r, FALSE/*require_valid*/); } void mail_update_flags(struct mail *mail, enum modify_type modify_type, -- 2.47.3