]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Hval.pm
view: support SHA-1 of Message-IDs for message links
[public-inbox.git] / lib / PublicInbox / Hval.pm
1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 #
4 # represents a header value in various forms
5 package PublicInbox::Hval;
6 use strict;
7 use warnings;
8 use fields qw(raw href);
9 use Encode qw(find_encoding);
10 use URI::Escape qw(uri_escape_utf8);
11
12 my $enc_ascii = find_encoding('us-ascii');
13
14 sub new {
15         my ($class, $raw, $href) = @_;
16         my $self = fields::new($class);
17
18         # we never care about leading/trailing whitespace
19         $raw =~ s/\A\s*//;
20         $raw =~ s/\s*\z//;
21         $self->{raw} = $raw;
22         $self->{href} = defined $href ? $href : $raw;
23         $self;
24 }
25
26 sub new_msgid {
27         my ($class, $msgid) = @_;
28         $msgid =~ s/\A\s*<?//;
29         $msgid =~ s/>?\s*\z//;
30
31         if (length($msgid) <= 40) {
32                 $class->new($msgid);
33         } else {
34                 require Digest::SHA;
35                 my $hex = Digest::SHA::sha1_hex($msgid);
36                 $class->new($msgid, $hex);
37         }
38 }
39
40 sub new_oneline {
41         my ($class, $raw) = @_;
42         $raw = '' unless defined $raw;
43         $raw =~ tr/\t\n / /s; # squeeze spaces
44         $raw =~ tr/\r//d; # kill CR
45         $class->new($raw);
46 }
47
48 my %xhtml_map = (
49         '"' => '&#34;',
50         '&' => '&#38;',
51         "'" => '&#39;',
52         '<' => '&lt;',
53         '>' => '&gt;',
54 );
55
56 sub ascii_html {
57         my ($s) = @_;
58         $s =~ s/([<>&'"])/$xhtml_map{$1}/ge;
59         $enc_ascii->encode($s, Encode::HTMLCREF);
60 }
61
62 sub as_html { ascii_html($_[0]->{raw}) }
63 sub as_href { ascii_html(uri_escape_utf8($_[0]->{href})) }
64
65 sub raw {
66         if (defined $_[1]) {
67                 $_[0]->{raw} = $_[1];
68         } else {
69                 $_[0]->{raw};
70         }
71 }
72
73 1;