]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ContentHash.pm
imap+nntp: share COMPRESS implementation
[public-inbox.git] / lib / PublicInbox / ContentHash.pm
1 # Copyright (C) 2018-2021 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 Digest::SHA;
19
20 sub digest_addr ($$$) {
21         my ($dig, $h, $v) = @_;
22         $v =~ tr/"//d;
23         $v =~ tr/\r\n\t / /s;
24         $v =~ s/@([a-z0-9\_\.\-\(\)]*([A-Z])\S*)/'@'.lc($1)/ge;
25         utf8::encode($v);
26         $dig->add("$h\0$v\0");
27 }
28
29 sub content_dig_i {
30         my ($dig) = $_[1];
31         my ($part, $depth, @idx) = @{$_[0]};
32         $dig->add("\0$depth:".join('.', @idx)."\0");
33         my $fn = $part->filename;
34         if (defined $fn) {
35                 utf8::encode($fn);
36                 $dig->add("fn\0$fn\0");
37         }
38         my @d = $part->header('Content-Description');
39         foreach my $d (@d) {
40                 utf8::encode($d);
41                 $dig->add("d\0$d\0");
42         }
43         $dig->add("b\0");
44         my $ct = $part->content_type || 'text/plain';
45         my ($s, undef) = msg_part_text($part, $ct);
46         if (defined $s) {
47                 $s =~ s/\r\n/\n/gs;
48                 $s =~ s/\s*\z//s;
49                 utf8::encode($s);
50         } else {
51                 $s = $part->body;
52         }
53         $dig->add($s);
54 }
55
56 sub content_digest ($;$) {
57         my ($eml, $dig) = @_;
58         $dig //= Digest::SHA->new(256);
59
60         # References: and In-Reply-To: get used interchangeably
61         # in some "duplicates" in LKML.  We treat them the same
62         # in SearchIdx, so treat them the same for this:
63         # do NOT consider the Message-ID as part of the content_hash
64         # if we got here, we've already got Message-ID reuse
65         my %seen = map { $_ => 1 } @{mids($eml)};
66         foreach my $mid (@{references($eml)}) {
67                 $dig->add("ref\0$mid\0") unless $seen{$mid}++;
68         }
69
70         # Only use Sender: if From is not present
71         foreach my $h (qw(From Sender)) {
72                 my @v = $eml->header($h) or next;
73                 digest_addr($dig, $h, $_) foreach @v;
74                 last;
75         }
76         foreach my $h (qw(Subject Date)) {
77                 my @v = $eml->header($h);
78                 foreach my $v (@v) {
79                         utf8::encode($v);
80                         $dig->add("$h\0$v\0");
81                 }
82         }
83         # Some mail processors will add " to unquoted names that were
84         # not in the original message.  For the purposes of deduplication,
85         # do not take it into account:
86         foreach my $h (qw(To Cc)) {
87                 my @v = $eml->header($h);
88                 digest_addr($dig, $h, $_) foreach @v;
89         }
90         msg_iter($eml, \&content_dig_i, $dig);
91         $dig;
92 }
93
94 sub content_hash ($) {
95         content_digest($_[0])->digest;
96 }
97
98 sub git_sha ($$) {
99         my ($n, $eml) = @_;
100         my $dig = Digest::SHA->new($n);
101         my $bref = ref($eml) eq 'SCALAR' ? $eml : \($eml->as_string);
102         $dig->add('blob '.length($$bref)."\0");
103         $dig->add($$bref);
104         $dig;
105 }
106
107 1;