X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FContentId.pm;h=0c4a8678beb02fa4db436ca12c55eda6bc0edc79;hb=261f705649bf5bc9c1954b29cfdb3c894cf44be2;hp=b1d27eb8e48a8e835c0d088f77e84f3a7e38dd88;hpb=cfb8d16578e7f2f2e300f9f436205e4a8fc7f322;p=public-inbox.git
diff --git a/lib/PublicInbox/ContentId.pm b/lib/PublicInbox/ContentId.pm
index b1d27eb8..0c4a8678 100644
--- a/lib/PublicInbox/ContentId.pm
+++ b/lib/PublicInbox/ContentId.pm
@@ -1,6 +1,11 @@
-# Copyright (C) 2018 all contributors
+# Copyright (C) 2018-2019 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;
@@ -20,6 +25,33 @@ sub digest_addr ($$$) {
$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);
@@ -28,12 +60,9 @@ sub content_digest ($) {
# 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:
- my %seen;
- foreach my $mid (@{mids($hdr)}) {
- # do NOT consider the Message-ID as part of the content_id
- # if we got here, we've already got Message-ID reuse
- $seen{$mid} = 1;
- }
+ # 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)}) {
next if $seen{$mid};
$dig->add("ref\0$mid\0");
@@ -60,38 +89,7 @@ sub content_digest ($) {
my @v = $hdr->header($h);
digest_addr($dig, $h, $_) foreach @v;
}
- msg_iter($mime, sub {
- 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 = eval { $part->body_str };
- if ($@ && $ct =~ m!\btext/plain\b!i) {
- # Try to assume UTF-8 because Alpine
- # seems to do wacky things and set
- # charset=X-UNKNOWN
- $part->charset_set('UTF-8');
- $s = eval { $part->body_str };
- }
- if (defined $s) {
- $s =~ s/\r\n/\n/gs;
- $s =~ s/\s*\z//s;
- utf8::encode($s);
- } else {
- $s = $part->body;
- }
- $dig->add($s);
- });
+ msg_iter($mime, \&content_dig_i, $dig);
$dig;
}