]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ContentHash.pm
No ext_urls
[public-inbox.git] / lib / PublicInbox / ContentHash.pm
1 # Copyright (C) all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3
4 # Unstable internal API.
5 # Used for on-the-fly duplicate detection in V2 inboxes.
6 # This is not stored in any database anywhere and may change
7 # as changes in duplicate detection are needed.
8 # See L<public-inbox-v2-format(5)> manpage for more details.
9 package PublicInbox::ContentHash;
10 use strict;
11 use v5.10.1;
12 use parent qw(Exporter);
13 our @EXPORT_OK = qw(content_hash content_digest git_sha);
14 use PublicInbox::MID qw(mids references);
15 use PublicInbox::MsgIter;
16
17 # not sure if less-widely supported hash families are worth bothering with
18 use PublicInbox::SHA; # faster, but no ->clone
19 use Digest::SHA; # we still need this for ->clone
20
21 sub digest_addr ($$$) {
22         my ($dig, $h, $v) = @_;
23         $v =~ tr/"//d;
24         $v =~ tr/\r\n\t / /s;
25         $v =~ s/@([a-z0-9\_\.\-\(\)]*([A-Z])\S*)/'@'.lc($1)/ge;
26         utf8::encode($v);
27         $dig->add("$h\0$v\0");
28 }
29
30 sub content_dig_i {
31         my ($dig) = $_[1];
32         my ($part, $depth, @idx) = @{$_[0]};
33         $dig->add("\0$depth:".join('.', @idx)."\0");
34         my $fn = $part->filename;
35         if (defined $fn) {
36                 utf8::encode($fn);
37                 $dig->add("fn\0$fn\0");
38         }
39         my @d = $part->header('Content-Description');
40         foreach my $d (@d) {
41                 utf8::encode($d);
42                 $dig->add("d\0$d\0");
43         }
44         $dig->add("b\0");
45         my $ct = $part->content_type || 'text/plain';
46         my ($s, undef) = msg_part_text($part, $ct);
47         if (defined $s) {
48                 $s =~ s/\r\n/\n/gs;
49                 $s =~ s/\s*\z//s;
50                 utf8::encode($s);
51         } else {
52                 $s = $part->body;
53         }
54         $dig->add($s);
55 }
56
57 sub content_digest ($;$) {
58         my ($eml, $dig) = @_;
59         $dig //= Digest::SHA->new(256);
60
61         # References: and In-Reply-To: get used interchangeably
62         # in some "duplicates" in LKML.  We treat them the same
63         # in SearchIdx, so treat them the same for this:
64         # do NOT consider the Message-ID as part of the content_hash
65         # if we got here, we've already got Message-ID reuse
66         my %seen = map { $_ => 1 } @{mids($eml)};
67         for (grep { !$seen{$_}++ } @{references($eml)}) {
68                 utf8::encode($_);
69                 $dig->add("ref\0$_\0");
70         }
71
72         # Only use Sender: if From is not present
73         foreach my $h (qw(From Sender)) {
74                 my @v = $eml->header($h) or next;
75                 digest_addr($dig, $h, $_) foreach @v;
76                 last;
77         }
78         foreach my $h (qw(Subject Date)) {
79                 my @v = $eml->header($h);
80                 foreach my $v (@v) {
81                         utf8::encode($v);
82                         $dig->add("$h\0$v\0");
83                 }
84         }
85         # Some mail processors will add " to unquoted names that were
86         # not in the original message.  For the purposes of deduplication,
87         # do not take it into account:
88         foreach my $h (qw(To Cc)) {
89                 my @v = $eml->header($h);
90                 digest_addr($dig, $h, $_) foreach @v;
91         }
92         msg_iter($eml, \&content_dig_i, $dig);
93         $dig;
94 }
95
96 sub content_hash ($) {
97         content_digest($_[0], PublicInbox::SHA->new(256))->digest;
98 }
99
100 # don't clone the result of this
101 sub git_sha ($$) {
102         my ($n, $eml) = @_;
103         my $dig = PublicInbox::SHA->new($n);
104         my $bref = ref($eml) eq 'SCALAR' ? $eml : \($eml->as_string);
105         $dig->add('blob '.length($$bref)."\0", $$bref);
106         $dig;
107 }
108
109 1;