1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # Various Message-ID-related functions.
5 package PublicInbox::MID;
9 our @EXPORT_OK = qw/mid_clean id_compress mid2path mid_mime mid_escape MID_ESC
11 use URI::Escape qw(uri_escape_utf8);
12 use Digest::SHA qw/sha1_hex/;
14 MID_MAX => 40, # SHA-1 hex length # TODO: get rid of this
15 MAX_MID_SIZE => 244, # max term size (Xapian limitation) - length('Q')
20 defined($mid) or die "no Message-ID";
21 # MDA->precheck did more checking for us
22 if ($mid =~ /<([^>]+)>/) {
30 my ($id, $force) = @_;
32 if ($force || $id =~ /[^\w\-]/ || length($id) > MID_MAX) {
41 my ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
43 unless (defined $x38) {
44 # compatibility with old links (or short Message-IDs :)
45 $mid = mid_clean($mid);
47 $mid = sha1_hex($mid);
48 ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
53 sub mid_mime ($) { $_[0]->header_obj->header_raw('Message-ID') }
58 my @v = $hdr->header_raw('Message-Id');
60 my @cur = ($v =~ /<([^>]+)>/sg);
67 foreach my $i (0..$#mids) {
68 next if length($mids[$i]) <= MAX_MID_SIZE;
69 warn "Message-ID: <$mids[$i]> too long, truncating\n";
70 $mids[$i] = substr($mids[$i], 0, MAX_MID_SIZE);
76 # last References should be IRT, but some mail clients do things
77 # out of order, so trust IRT over References iff IRT exists
81 foreach my $f (qw(References In-Reply-To)) {
82 my @v = $hdr->header_raw($f);
84 push(@mids, ($v =~ /<([^>]+)>/sg));
102 # RFC3986, section 3.3:
103 sub MID_ESC () { '^A-Za-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@' }
104 sub mid_escape ($) { uri_escape_utf8($_[0], MID_ESC) }