]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ContentId.pm
9082b7694002168467ab3d2610a36631348116d2
[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 sub content_digest ($) {
15         my ($mime) = @_;
16         my $dig = Digest::SHA->new(256);
17         my $hdr = $mime->header_obj;
18
19         # References: and In-Reply-To: get used interchangeably
20         # in some "duplicates" in LKML.  We treat them the same
21         # in SearchIdx, so treat them the same for this:
22         my %seen;
23         foreach my $mid (@{mids($hdr)}) {
24                 $dig->add('mid: '.$mid);
25                 $seen{$mid} = 1;
26         }
27         foreach my $mid (@{references($hdr)}) {
28                 next if $seen{$mid};
29                 $dig->add('ref: '.$mid);
30         }
31
32         # Only use Sender: if From is not present
33         foreach my $h (qw(From Sender)) {
34                 my @v = $hdr->header_raw($h);
35                 if (@v) {
36                         $dig->add("$h: $_") foreach @v;
37                         last;
38                 }
39         }
40
41         # Content-* headers are often no-ops, so maybe we don't need them
42         foreach my $h (qw(Subject Date To Cc)) {
43                 my @v = $hdr->header_raw($h);
44                 $dig->add("$h: $_") foreach @v;
45         }
46         $dig->add($mime->body_raw);
47         $dig;
48 }
49
50 sub content_id ($) {
51         content_digest($_[0])->digest;
52 }
53
54 1;