]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MID.pm
369bb034c8dd9d5c03ce4beb2e35d1c5fc632a9c
[public-inbox.git] / lib / PublicInbox / MID.pm
1 # Copyright (C) 2015-2020 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_escape MID_ESC
10         mids references mids_for_index $MID_EXTRACT);
11 use URI::Escape qw(uri_escape_utf8);
12 use Digest::SHA qw/sha1_hex/;
13 require PublicInbox::Address;
14 use constant {
15         ID_MAX => 40, # SHA-1 hex length for HTML id anchors
16         MAX_MID_SIZE => 244, # max term size (Xapian limitation) - length('Q')
17 };
18
19 our $MID_EXTRACT = qr/<([^>]+)>/s;
20
21 sub mid_clean {
22         my ($mid) = @_;
23         defined($mid) or die "no Message-ID";
24         # MDA->precheck did more checking for us
25         if ($mid =~ $MID_EXTRACT) {
26                 $mid = $1;
27         }
28         $mid;
29 }
30
31 # this is idempotent, used for HTML anchor/ids and such
32 sub id_compress {
33         my ($id, $force) = @_;
34
35         if ($force || $id =~ /[^a-zA-Z0-9_\-]/ || length($id) > ID_MAX) {
36                 utf8::encode($id);
37                 return sha1_hex($id);
38         }
39         $id;
40 }
41
42 sub mid2path {
43         my ($mid) = @_;
44         my ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
45
46         unless (defined $x38) {
47                 # compatibility with old links (or short Message-IDs :)
48                 $mid = mid_clean($mid);
49                 utf8::encode($mid);
50                 $mid = sha1_hex($mid);
51                 ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
52         }
53         "$x2/$x38";
54 }
55
56 # only intended for Message-ID and X-Alt-Message-ID
57 sub extract_mids {
58         my @mids;
59         for my $v (@_) {
60                 my @cur = ($v =~ /$MID_EXTRACT/g);
61                 if (@cur) {
62                         push(@mids, @cur);
63                 } else {
64                         push(@mids, $v);
65                 }
66         }
67         \@mids;
68 }
69
70 sub mids ($) {
71         my ($hdr) = @_;
72         my @mids = $hdr->header_raw('Message-ID');
73         uniq_mids(extract_mids(@mids));
74 }
75
76 # we allow searching on X-Alt-Message-ID since PublicInbox::NNTP uses them
77 # to placate some clients, and we want to ensure NNTP-only clients can
78 # import and index without relying on HTTP endpoints
79 sub mids_for_index ($) {
80         my ($hdr) = @_;
81         my @mids = $hdr->header_raw('Message-ID');
82         my @alts = $hdr->header_raw('X-Alt-Message-ID');
83         uniq_mids(extract_mids(@mids, @alts));
84 }
85
86 # last References should be IRT, but some mail clients do things
87 # out of order, so trust IRT over References iff IRT exists
88 sub references ($) {
89         my ($hdr) = @_;
90         my @mids;
91         foreach my $f (qw(References In-Reply-To)) {
92                 my @v = $hdr->header_raw($f);
93                 foreach my $v (@v) {
94                         push(@mids, ($v =~ /$MID_EXTRACT/g));
95                 }
96         }
97
98         # old versions of git-send-email would prompt users for
99         # In-Reply-To and users' muscle memory would use 'y' or 'n'
100         # as responses:
101         my %addr = ( y => 1, n => 1 );
102
103         foreach my $f (qw(To From Cc)) {
104                 my @v = $hdr->header_raw($f);
105                 foreach my $v (@v) {
106                         $addr{$_} = 1 for (PublicInbox::Address::emails($v));
107                 }
108         }
109         uniq_mids(\@mids, \%addr);
110 }
111
112 sub uniq_mids ($;$) {
113         my ($mids, $seen) = @_;
114         my @ret;
115         $seen ||= {};
116         foreach my $mid (@$mids) {
117                 $mid =~ tr/\n\t\r//d;
118                 if (length($mid) > MAX_MID_SIZE) {
119                         warn "Message-ID: <$mid> too long, truncating\n";
120                         $mid = substr($mid, 0, MAX_MID_SIZE);
121                 }
122                 push(@ret, $mid) unless $seen->{$mid}++;
123         }
124         \@ret;
125 }
126
127 # RFC3986, section 3.3:
128 sub MID_ESC () { '^A-Za-z0-9\-\._~!\$\&\';\(\)\*\+,;=:@' }
129 sub mid_escape ($) { uri_escape_utf8($_[0], MID_ESC) }
130
131 1;