X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FContentId.pm;h=8d77934f20a51665f974da408d7195f26509ec15;hb=95bdac7f09c69036efed537a4d03d5bdd2ae4eb6;hp=65d5a76c09b43f4dec2edb08998c22ffe6d6e95e;hpb=5b491c0b308e576897cfcc270647ba4e35c6cc8a;p=public-inbox.git diff --git a/lib/PublicInbox/ContentId.pm b/lib/PublicInbox/ContentId.pm index 65d5a76c..8d77934f 100644 --- a/lib/PublicInbox/ContentId.pm +++ b/lib/PublicInbox/ContentId.pm @@ -1,30 +1,99 @@ -# Copyright (C) 2018 all contributors +# Copyright (C) 2018-2020 all contributors # License: AGPL-3.0+ +# 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 manpage for more details. package PublicInbox::ContentId; use strict; use warnings; use base qw/Exporter/; -our @EXPORT_OK = qw/content_id/; +our @EXPORT_OK = qw/content_id 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; -# Content-* headers are often no-ops, so maybe we don't need them -my @ID_HEADERS = qw(Subject From Date Message-ID References To Cc In-Reply-To); +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_id ($;$) { - my ($mime, $alg) = @_; - $alg ||= 256; - my $dig = Digest::SHA->new($alg); +sub content_digest ($) { + my ($mime) = @_; + my $dig = Digest::SHA->new(256); my $hdr = $mime->header_obj; - foreach my $h (@ID_HEADERS) { - my @v = $hdr->header_raw($h); - $dig->add($_) foreach @v; + # 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_id + # 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}++; } - $dig->add($mime->body_raw); - 'SHA-' . $dig->algorithm . ':' . $dig->hexdigest; + + # 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_id ($) { + content_digest($_[0])->digest; } 1;