]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MID.pm
mid: be strict with References, but loose on Message-Id
[public-inbox.git] / lib / PublicInbox / MID.pm
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>
3 #
4 # Various Message-ID-related functions.
5 package PublicInbox::MID;
6 use strict;
7 use warnings;
8 use base qw/Exporter/;
9 our @EXPORT_OK = qw/mid_clean id_compress mid2path mid_mime mid_escape MID_ESC
10         mids references/;
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
14
15 sub mid_clean {
16         my ($mid) = @_;
17         defined($mid) or die "no Message-ID";
18         # MDA->precheck did more checking for us
19         if ($mid =~ /<([^>]+)>/) {
20                 $mid = $1;
21         }
22         $mid;
23 }
24
25 # this is idempotent
26 sub id_compress {
27         my ($id, $force) = @_;
28
29         if ($force || $id =~ /[^\w\-]/ || length($id) > MID_MAX) {
30                 utf8::encode($id);
31                 return sha1_hex($id);
32         }
33         $id;
34 }
35
36 sub mid2path {
37         my ($mid) = @_;
38         my ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
39
40         unless (defined $x38) {
41                 # compatibility with old links (or short Message-IDs :)
42                 $mid = mid_clean($mid);
43                 utf8::encode($mid);
44                 $mid = sha1_hex($mid);
45                 ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
46         }
47         "$x2/$x38";
48 }
49
50 sub mid_mime ($) { $_[0]->header_obj->header_raw('Message-ID') }
51
52 sub mids ($) {
53         my ($hdr) = @_;
54         my @mids;
55         my @v = $hdr->header_raw('Message-Id');
56         foreach my $v (@v) {
57                 my @cur = ($v =~ /<([^>]+)>/sg);
58                 if (@cur) {
59                         push(@mids, @cur);
60                 } else {
61                         push(@mids, $v);
62                 }
63         }
64         uniq_mids(\@mids);
65 }
66
67 # last References should be IRT, but some mail clients do things
68 # out of order, so trust IRT over References iff IRT exists
69 sub references ($) {
70         my ($hdr) = @_;
71         my @mids;
72         foreach my $f (qw(References In-Reply-To)) {
73                 my @v = $hdr->header_raw($f);
74                 foreach my $v (@v) {
75                         push(@mids, ($v =~ /<([^>]+)>/sg));
76                 }
77         }
78         uniq_mids(\@mids);
79 }
80
81 sub uniq_mids ($) {
82         my ($mids) = @_;
83         my @ret;
84         my %seen;
85         foreach (@$mids) {
86                 next if $seen{$_};
87                 push @ret, $_;
88                 $seen{$_} = 1;
89         }
90         \@ret;
91 }
92
93 # RFC3986, section 3.3:
94 sub MID_ESC () { '^A-Za-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@' }
95 sub mid_escape ($) { uri_escape_utf8($_[0], MID_ESC) }
96
97 1;