1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 package PublicInbox::ContentId;
8 our @EXPORT_OK = qw/content_id/;
9 use PublicInbox::MID qw(mids references);
11 # not sure if less-widely supported hash families are worth bothering with
14 # Content-* headers are often no-ops, so maybe we don't need them
15 my @ID_HEADERS = qw(Subject From Date To Cc);
17 sub content_id ($;$) {
18 my ($mime, $alg) = @_;
20 my $dig = Digest::SHA->new($alg);
21 my $hdr = $mime->header_obj;
23 # References: and In-Reply-To: get used interchangeably
24 # in some "duplicates" in LKML. We treat them the same
25 # in SearchIdx, so treat them the same for this:
27 foreach my $mid (@{mids($hdr)}) {
28 $dig->add('mid: '.$mid);
31 foreach my $mid (@{references($hdr)}) {
33 $dig->add('ref: '.$mid);
35 foreach my $h (@ID_HEADERS) {
36 my @v = $hdr->header_raw($h);
37 $dig->add("$h: $_") foreach @v;
39 $dig->add($mime->body_raw);
40 'SHA-' . $dig->algorithm . ':' . $dig->hexdigest;