]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/ContentId.pm
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / ContentId.pm
1 # Copyright (C) 2018-2020 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::ContentId;
10 use strict;
11 use warnings;
12 use base qw/Exporter/;
13 our @EXPORT_OK = qw/content_id content_digest/;
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 =~ s/@([a-z0-9\_\.\-\(\)]*([A-Z])\S*)/'@'.lc($1)/ge;
24         utf8::encode($v);
25         $dig->add("$h\0$v\0");
26 }
27
28 sub content_dig_i {
29         my ($dig) = $_[1];
30         my ($part, $depth, @idx) = @{$_[0]};
31         $dig->add("\0$depth:".join('.', @idx)."\0");
32         my $fn = $part->filename;
33         if (defined $fn) {
34                 utf8::encode($fn);
35                 $dig->add("fn\0$fn\0");
36         }
37         my @d = $part->header('Content-Description');
38         foreach my $d (@d) {
39                 utf8::encode($d);
40                 $dig->add("d\0$d\0");
41         }
42         $dig->add("b\0");
43         my $ct = $part->content_type || 'text/plain';
44         my ($s, undef) = msg_part_text($part, $ct);
45         if (defined $s) {
46                 $s =~ s/\r\n/\n/gs;
47                 $s =~ s/\s*\z//s;
48                 utf8::encode($s);
49         } else {
50                 $s = $part->body;
51         }
52         $dig->add($s);
53 }
54
55 sub content_digest ($) {
56         my ($mime) = @_;
57         my $dig = Digest::SHA->new(256);
58         my $hdr = $mime->header_obj;
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_id
64         # if we got here, we've already got Message-ID reuse
65         my %seen = map { $_ => 1 } @{mids($hdr)};
66         foreach my $mid (@{references($hdr)}) {
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 = $hdr->header($h);
73                 if (@v) {
74                         digest_addr($dig, $h, $_) foreach @v;
75                 }
76         }
77         foreach my $h (qw(Subject Date)) {
78                 my @v = $hdr->header($h);
79                 foreach my $v (@v) {
80                         utf8::encode($v);
81                         $dig->add("$h\0$v\0");
82                 }
83         }
84         # Some mail processors will add " to unquoted names that were
85         # not in the original message.  For the purposes of deduplication,
86         # do not take it into account:
87         foreach my $h (qw(To Cc)) {
88                 my @v = $hdr->header($h);
89                 digest_addr($dig, $h, $_) foreach @v;
90         }
91         msg_iter($mime, \&content_dig_i, $dig);
92         $dig;
93 }
94
95 sub content_id ($) {
96         content_digest($_[0])->digest;
97 }
98
99 1;