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