]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/MID.pm
mid: mid_compressed => mid_compress
[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         $mid =~ s/\A\s*<?//;
16         $mid =~ s/>?\s*\z//;
17         $mid;
18 }
19
20 # this is idempotent
21 sub mid_compress {
22         my ($mid, $force) = @_;
23
24         # XXX dirty hack! FIXME!
25         # Some HTTP servers (apache2 2.2.22-13+deb7u5 on my system)
26         # apparently do not handle "%25" in the URL path component correctly.
27         # I'm not yet sure if it's something weird with my rewrite rules
28         # or what; will need to debug...
29         return sha1_hex($mid) if (index($mid, '%') >= 0);
30
31         return $mid if (!$force && length($mid) <= MID_MAX);
32         sha1_hex($mid);
33 }
34
35 sub mid2path {
36         my ($mid) = @_;
37         my ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
38
39         unless (defined $x38) {
40                 # compatibility with old links (or short Message-IDs :)
41                 $mid = sha1_hex($mid);
42                 ($x2, $x38) = ($mid =~ /\A([a-f0-9]{2})([a-f0-9]{38})\z/);
43         }
44         "$x2/$x38";
45 }
46
47 1;