]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/MID.pm
run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / MID.pm
index c82e84013ec0d68730423ab4b691074c5b690dc5..14089f914ab8923c129fd1249ddfc87c75989ac3 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2019 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # Various Message-ID-related functions.
@@ -10,6 +10,7 @@ our @EXPORT_OK = qw/mid_clean id_compress mid2path mid_mime mid_escape MID_ESC
        mids references/;
 use URI::Escape qw(uri_escape_utf8);
 use Digest::SHA qw/sha1_hex/;
+require PublicInbox::Address;
 use constant {
        MID_MAX => 40, # SHA-1 hex length # TODO: get rid of this
        MAX_MID_SIZE => 244, # max term size (Xapian limitation) - length('Q')
@@ -25,11 +26,11 @@ sub mid_clean {
        $mid;
 }
 
-# this is idempotent
+# this is idempotent, used for HTML anchor/ids and such
 sub id_compress {
        my ($id, $force) = @_;
 
-       if ($force || $id =~ /[^\w\-]/ || length($id) > MID_MAX) {
+       if ($force || $id =~ /[^a-zA-Z0-9_\-]/ || length($id) > MID_MAX) {
                utf8::encode($id);
                return sha1_hex($id);
        }
@@ -79,21 +80,34 @@ sub references ($) {
                        push(@mids, ($v =~ /<([^>]+)>/sg));
                }
        }
-       uniq_mids(\@mids);
+
+       # old versions of git-send-email would prompt users for
+       # In-Reply-To and users' muscle memory would use 'y' or 'n'
+       # as responses:
+       my %addr = ( y => 1, n => 1 );
+
+       foreach my $f (qw(To From Cc)) {
+               my @v = $hdr->header_raw($f);
+               foreach my $v (@v) {
+                       $addr{$_} = 1 for (PublicInbox::Address::emails($v));
+               }
+       }
+       uniq_mids(\@mids, \%addr);
 }
 
-sub uniq_mids ($) {
-       my ($mids) = @_;
+sub uniq_mids ($;$) {
+       my ($mids, $seen) = @_;
        my @ret;
-       my %seen;
+       $seen ||= {};
        foreach my $mid (@$mids) {
+               $mid =~ tr/\n\t\r//d;
                if (length($mid) > MAX_MID_SIZE) {
                        warn "Message-ID: <$mid> too long, truncating\n";
                        $mid = substr($mid, 0, MAX_MID_SIZE);
                }
-               next if $seen{$mid};
+               next if $seen->{$mid};
                push @ret, $mid;
-               $seen{$mid} = 1;
+               $seen->{$mid} = 1;
        }
        \@ret;
 }