]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MID.pm
update copyright headers and email addresses
[public-inbox.git] / lib / PublicInbox / MID.pm
1 # Copyright (C) 2015 all contributors <meta@public-inbox.org>
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::MID;
4 use strict;
5 use warnings;
6 use base qw/Exporter/;
7 our @EXPORT_OK = qw/mid_clean mid_compress mid2path/;
8 use Digest::SHA qw/sha1_hex/;
9 use constant MID_MAX => 40; # SHA-1 hex length
10
11 sub mid_clean {
12         my ($mid) = @_;
13         defined($mid) or die "no Message-ID";
14         # MDA->precheck did more checking for us
15         if ($mid =~ /<([^>]+)>/) {
16                 $mid = $1;
17         }
18         $mid;
19 }
20
21 # this is idempotent
22 sub mid_compress {
23         my ($mid, $force) = @_;
24
25         # XXX dirty hack! FIXME!
26         # Some HTTP servers (apache2 2.2.22-13+deb7u5 on my system)
27         # apparently do not handle "%25" in the URL path component correctly.
28         # I'm not yet sure if it's something weird with my rewrite rules
29         # or what; will need to debug...
30         return sha1_hex($mid) if (index($mid, '%') >= 0);
31
32         return $mid if (!$force && length($mid) <= MID_MAX);
33         sha1_hex($mid);
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 = sha1_hex(mid_clean($mid));
43                 ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
44         }
45         "$x2/$x38";
46 }
47
48 1;