]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/ContentId.pm
treewide: run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / ContentId.pm
index 9baf0e76a3e33c3d32502061c93ceb15b4028e33..8d77934f20a51665f974da408d7195f26509ec15 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2018 all contributors <meta@public-inbox.org>
+# 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.
@@ -25,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);
@@ -33,15 +60,11 @@ 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");
+               $dig->add("ref\0$mid\0") unless $seen{$mid}++;
        }
 
        # Only use Sender: if From is not present
@@ -65,31 +88,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, 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);
-       });
+       msg_iter($mime, \&content_dig_i, $dig);
        $dig;
 }