]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ContentId.pm
8347de2d2846654ef2042c4d34aa6d4595e92dc1
[public-inbox.git] / lib / PublicInbox / ContentId.pm
1 # Copyright (C) 2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 package PublicInbox::ContentId;
5 use strict;
6 use warnings;
7 use base qw/Exporter/;
8 our @EXPORT_OK = qw/content_id content_digest/;
9 use PublicInbox::MID qw(mids references);
10
11 # not sure if less-widely supported hash families are worth bothering with
12 use Digest::SHA;
13
14 # Content-* headers are often no-ops, so maybe we don't need them
15 my @ID_HEADERS = qw(Subject From Date To Cc);
16
17 sub content_digest ($) {
18         my ($mime) = @_;
19         my $dig = Digest::SHA->new(256);
20         my $hdr = $mime->header_obj;
21
22         # References: and In-Reply-To: get used interchangeably
23         # in some "duplicates" in LKML.  We treat them the same
24         # in SearchIdx, so treat them the same for this:
25         my %seen;
26         foreach my $mid (@{mids($hdr)}) {
27                 $dig->add('mid: '.$mid);
28                 $seen{$mid} = 1;
29         }
30         foreach my $mid (@{references($hdr)}) {
31                 next if $seen{$mid};
32                 $dig->add('ref: '.$mid);
33         }
34         foreach my $h (@ID_HEADERS) {
35                 my @v = $hdr->header_raw($h);
36                 $dig->add("$h: $_") foreach @v;
37         }
38         $dig->add($mime->body_raw);
39         $dig;
40 }
41
42 sub content_id ($) {
43         content_digest($_[0])->digest;
44 }
45
46 1;