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/;
13 use constant MID_MAX => 40; # SHA-1 hex length
17 defined($mid) or die "no Message-ID";
18 # MDA->precheck did more checking for us
19 if ($mid =~ /<([^>]+)>/) {
27 my ($id, $force) = @_;
29 if ($force || $id =~ /[^\w\-]/ || length($id) > MID_MAX) {
38 my ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
40 unless (defined $x38) {
41 # compatibility with old links (or short Message-IDs :)
42 $mid = mid_clean($mid);
44 $mid = sha1_hex($mid);
45 ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
50 sub mid_mime ($) { $_[0]->header_obj->header_raw('Message-ID') }
53 my ($hdr, @fields) = @_;
56 foreach my $f (@fields) {
57 push @raw, $hdr->header_raw($f);
59 my @mids = (join(' ', @raw) =~ /<([^>]+)>/g);
60 my $mids = scalar(@mids) == 0 ? \@raw: \@mids;
70 sub mids { uniq_mids($_[0], 'Message-Id') }
72 # last References should be IRT, but some mail clients do things
73 # out of order, so trust IRT over References iff IRT exists
74 sub references { uniq_mids($_[0], 'References', 'In-Reply-To') }
76 # RFC3986, section 3.3:
77 sub MID_ESC () { '^A-Za-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@' }
78 sub mid_escape ($) { uri_escape_utf8($_[0], MID_ESC) }