message: flag list-unsubscribe as MailingList

Flag message that merely have a List-Unsubscribe header with
Flags::MailingList too (some marketing message have this header, yet
miss "List-Id".

Add a test as well.
This commit is contained in:
Dirk-Jan C. Binnema 2024-03-15 19:23:35 +02:00
parent 9fd0d2be12
commit 52826aa758
2 changed files with 31 additions and 2 deletions

View File

@ -326,8 +326,16 @@ get_mailing_list(const MimeMessage& mime_msg)
const char *b, *e;
const auto hdr{mime_msg.header("List-Id")};
if (!hdr)
return {};
if (!hdr) {
/* some marketing messages don't have a List-Id, but _do_ have a
* List-Unsubscribe; if so, return an empty string here, so this
* message is still flagged as "MailingList"
*/
if (const auto lu = mime_msg.header("List-Unsubscribe"); !!lu)
return "";
else
return Nothing;
}
dechdr = g_mime_utils_header_decode_phrase(NULL, hdr->c_str());
if (!dechdr)

View File

@ -1065,8 +1065,27 @@ Hello!
"Le poids économique de la chasse : la dette cachée de la chasse !");
assert_equal(msg->header("Subject").value_or(""),
"Le poids économique de la chasse : \n\nla dette cachée de la chasse !");
g_assert_true(none_of(msg->flags() & Flags::MailingList));
}
static void
test_message_list_unsubscribe()
{
constexpr const auto txt =
R"(From: "Mu Test" <mu@djcbsoftware.nl>
To: mu@djcbsoftware.nl
Subject: Test
Message-ID: <87lew9xddt.fsf@djcbsoftware.nl>
List-Unsubscribe: <mailto:unsubscribe-T7BC8RRQMK-booking-email-9@booking.com>
abcdef
)";
const auto msg{Message::make_from_text(txt, "/xxx/m123:2,S")};
assert_valid_result(msg);
assert_equal(msg->mailing_list(), "");
g_assert_true(any_of(msg->flags() & Flags::MailingList));
}
int
main(int argc, char* argv[])
@ -1099,6 +1118,8 @@ main(int argc, char* argv[])
test_message_fail);
g_test_add_func("/message/message/sanitize-maildir",
test_message_sanitize_maildir);
g_test_add_func("/message/message/message-list-unsubscribe",
test_message_list_unsubscribe);
return g_test_run();
}