about summary refs log tree commit homepage
path: root/lib/PublicInbox/ContentHash.pm
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-05-10 22:37:12 +0000
committerEric Wong <e@yhbt.net>2020-05-12 06:15:59 +0000
commit098fecd1fe516a00fbfd49622b82be382ebcdab6 (patch)
tree0b38bb0910e6f9b8c3600ba6ef105aac43bafed6 /lib/PublicInbox/ContentHash.pm
parentcd8cda10c9687533949a8a358fd7b858f704da6e (diff)
downloadpublic-inbox-098fecd1fe516a00fbfd49622b82be382ebcdab6.tar.gz
The old name may be confused with "Content-ID" as described in
RFC 2392, so use an alternate name to avoid confusing future
readers.
Diffstat (limited to 'lib/PublicInbox/ContentHash.pm')
-rw-r--r--lib/PublicInbox/ContentHash.pm99
1 files changed, 99 insertions, 0 deletions
diff --git a/lib/PublicInbox/ContentHash.pm b/lib/PublicInbox/ContentHash.pm
new file mode 100644
index 00000000..420dc5e7
--- /dev/null
+++ b/lib/PublicInbox/ContentHash.pm
@@ -0,0 +1,99 @@
+# Copyright (C) 2018-2020 all contributors <meta@public-inbox.org>
+# License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
+
+# Unstable internal API.
+# Used for on-the-fly duplicate detection in V2 inboxes.
+# This is not stored in any database anywhere and may change
+# as changes in duplicate detection are needed.
+# See L<public-inbox-v2-format(5)> manpage for more details.
+package PublicInbox::ContentHash;
+use strict;
+use warnings;
+use base qw/Exporter/;
+our @EXPORT_OK = qw/content_hash content_digest/;
+use PublicInbox::MID qw(mids references);
+use PublicInbox::MsgIter;
+
+# not sure if less-widely supported hash families are worth bothering with
+use Digest::SHA;
+
+sub digest_addr ($$$) {
+        my ($dig, $h, $v) = @_;
+        $v =~ tr/"//d;
+        $v =~ s/@([a-z0-9\_\.\-\(\)]*([A-Z])\S*)/'@'.lc($1)/ge;
+        utf8::encode($v);
+        $dig->add("$h\0$v\0");
+}
+
+sub content_dig_i {
+        my ($dig) = $_[1];
+        my ($part, $depth, @idx) = @{$_[0]};
+        $dig->add("\0$depth:".join('.', @idx)."\0");
+        my $fn = $part->filename;
+        if (defined $fn) {
+                utf8::encode($fn);
+                $dig->add("fn\0$fn\0");
+        }
+        my @d = $part->header('Content-Description');
+        foreach my $d (@d) {
+                utf8::encode($d);
+                $dig->add("d\0$d\0");
+        }
+        $dig->add("b\0");
+        my $ct = $part->content_type || 'text/plain';
+        my ($s, undef) = msg_part_text($part, $ct);
+        if (defined $s) {
+                $s =~ s/\r\n/\n/gs;
+                $s =~ s/\s*\z//s;
+                utf8::encode($s);
+        } else {
+                $s = $part->body;
+        }
+        $dig->add($s);
+}
+
+sub content_digest ($) {
+        my ($mime) = @_;
+        my $dig = Digest::SHA->new(256);
+        my $hdr = $mime->header_obj;
+
+        # References: and In-Reply-To: get used interchangeably
+        # in some "duplicates" in LKML.  We treat them the same
+        # in SearchIdx, so treat them the same for this:
+        # do NOT consider the Message-ID as part of the content_hash
+        # if we got here, we've already got Message-ID reuse
+        my %seen = map { $_ => 1 } @{mids($hdr)};
+        foreach my $mid (@{references($hdr)}) {
+                $dig->add("ref\0$mid\0") unless $seen{$mid}++;
+        }
+
+        # Only use Sender: if From is not present
+        foreach my $h (qw(From Sender)) {
+                my @v = $hdr->header($h);
+                if (@v) {
+                        digest_addr($dig, $h, $_) foreach @v;
+                }
+        }
+        foreach my $h (qw(Subject Date)) {
+                my @v = $hdr->header($h);
+                foreach my $v (@v) {
+                        utf8::encode($v);
+                        $dig->add("$h\0$v\0");
+                }
+        }
+        # Some mail processors will add " to unquoted names that were
+        # not in the original message.  For the purposes of deduplication,
+        # do not take it into account:
+        foreach my $h (qw(To Cc)) {
+                my @v = $hdr->header($h);
+                digest_addr($dig, $h, $_) foreach @v;
+        }
+        msg_iter($mime, \&content_dig_i, $dig);
+        $dig;
+}
+
+sub content_hash ($) {
+        content_digest($_[0])->digest;
+}
+
+1;