]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/MID.pm
truncate Message-IDs and References consistently
[public-inbox.git] / lib / PublicInbox / MID.pm
index 96085399b6d2f2d96ba9bef3d06376a953132953..c82e84013ec0d68730423ab4b691074c5b690dc5 100644 (file)
@@ -10,7 +10,10 @@ 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/;
-use constant MID_MAX => 40; # SHA-1 hex length
+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')
+};
 
 sub mid_clean {
        my ($mid) = @_;
@@ -47,7 +50,8 @@ sub mid2path {
        "$x2/$x38";
 }
 
-sub mid_mime ($) { $_[0]->header_obj->header_raw('Message-ID') }
+# Only for v1 code paths:
+sub mid_mime ($) { mids($_[0]->header_obj)->[0] }
 
 sub mids ($) {
        my ($hdr) = @_;
@@ -82,10 +86,14 @@ sub uniq_mids ($) {
        my ($mids) = @_;
        my @ret;
        my %seen;
-       foreach (@$mids) {
-               next if $seen{$_};
-               push @ret, $_;
-               $seen{$_} = 1;
+       foreach my $mid (@$mids) {
+               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};
+               push @ret, $mid;
+               $seen{$mid} = 1;
        }
        \@ret;
 }