]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Hval.pm
hval: export prurl and add prototype
[public-inbox.git] / lib / PublicInbox / Hval.pm
1 # Copyright (C) 2014-2019 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # represents a header value in various forms.  Used for HTML generation
5 # in our web interface(s)
6 package PublicInbox::Hval;
7 use strict;
8 use warnings;
9 use Encode qw(find_encoding);
10 use PublicInbox::MID qw/mid_clean mid_escape/;
11 use base qw/Exporter/;
12 our @EXPORT_OK = qw/ascii_html obfuscate_addrs to_filename src_escape
13                 to_attr from_attr prurl/;
14 my $enc_ascii = find_encoding('us-ascii');
15
16 # safe-ish acceptable filename pattern for portability
17 our $FN = '[a-zA-Z0-9][a-zA-Z0-9_\-\.]+[a-zA-Z0-9]'; # needs \z anchor
18
19 sub new {
20         my ($class, $raw, $href) = @_;
21
22         # we never care about trailing whitespace
23         $raw =~ s/\s*\z//;
24         bless {
25                 raw => $raw,
26                 href => defined $href ? $href : $raw,
27         }, $class;
28 }
29
30 sub new_msgid {
31         my ($class, $msgid) = @_;
32         $class->new($msgid, mid_escape($msgid));
33 }
34
35 # some of these overrides are standard C escapes so they're
36 # easy-to-understand when rendered.
37 my %escape_sequence = (
38         "\x00" => '\\0', # NUL
39         "\x07" => '\\a', # bell
40         "\x08" => '\\b', # backspace
41         "\x09" => "\t", # obvious to show as-is
42         "\x0a" => "\n", # obvious to show as-is
43         "\x0b" => '\\v', # vertical tab
44         "\x0c" => '\\f', # form feed
45         "\x0d" => '\\r', # carriage ret (not preceding \n)
46         "\x1b" => '^[', # ASCII escape (mutt seems to escape this way)
47         "\x7f" => '\\x7f', # DEL
48 );
49
50 my %xhtml_map = (
51         '"' => '&#34;',
52         '&' => '&#38;',
53         "'" => '&#39;',
54         '<' => '&lt;',
55         '>' => '&gt;',
56 );
57
58 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
59 %xhtml_map = (%xhtml_map, %escape_sequence);
60
61 # for post-processing the output of highlight.pm and perhaps other
62 # highlighers in the future
63 sub src_escape ($) {
64         $_[0] =~ s/\r\n/\n/sg;
65         $_[0] =~ s/&apos;/&#39;/sg; # workaround https://bugs.debian.org/927409
66         $_[0] =~ s/([\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
67         $_[0] = $enc_ascii->encode($_[0], Encode::HTMLCREF);
68 }
69
70 sub ascii_html {
71         my ($s) = @_;
72         $s =~ s/\r\n/\n/sg; # fixup bad line endings
73         $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
74         $enc_ascii->encode($s, Encode::HTMLCREF);
75 }
76
77 sub as_html { ascii_html($_[0]->{raw}) }
78
79 sub raw {
80         if (defined $_[1]) {
81                 $_[0]->{raw} = $_[1];
82         } else {
83                 $_[0]->{raw};
84         }
85 }
86
87 # returns a protocol-relative URL string
88 sub prurl ($$) {
89         my ($env, $u) = @_;
90         if (ref($u) eq 'ARRAY') {
91                 my $h = $env->{HTTP_HOST} // $env->{SERVER_NAME};
92                 my @host_match = grep(/\b\Q$h\E\b/, @$u);
93                 $u = $host_match[0] // $u->[0];
94                 # fall through to below:
95         }
96         index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
97 }
98
99 # for misguided people who believe in this stuff, give them a
100 # substitution for '.'
101 # &#8228; &#183; and &#890; were also candidates:
102 #   https://public-inbox.org/meta/20170615015250.GA6484@starla/
103 # However, &#8226; was chosen to make copy+paste errors more obvious
104 sub obfuscate_addrs ($$;$) {
105         my $ibx = $_[0];
106         my $repl = $_[2] || '&#8226;';
107         my $re = $ibx->{-no_obfuscate_re}; # regex of domains
108         my $addrs = $ibx->{-no_obfuscate}; # { adddress => 1 }
109         $_[1] =~ s/(([\w\.\+=\-]+)\@([\w\-]+\.[\w\.\-]+))/
110                 my ($addr, $user, $domain) = ($1, $2, $3);
111                 if ($addrs->{$addr} || ((defined $re && $domain =~ $re))) {
112                         $addr;
113                 } else {
114                         $domain =~ s!([^\.]+)\.!$1$repl!;
115                         $user . '@' . $domain
116                 }
117                 /sge;
118 }
119
120 # like format_sanitized_subject in git.git pretty.c with '%f' format string
121 sub to_filename ($) {
122         my ($s, undef) = split(/\n/, $_[0]);
123         $s =~ s/[^A-Za-z0-9_\.]+/-/g;
124         $s =~ tr/././s;
125         $s =~ s/[\.\-]+\z//;
126         $s =~ s/\A[\.\-]+//;
127         $s
128 }
129
130 # convert a filename (or any string) to HTML attribute
131
132 my %ESCAPES = map { chr($_) => sprintf('::%02x', $_) } (0..255);
133 $ESCAPES{'/'} = ':'; # common
134
135 sub to_attr ($) {
136         my ($str) = @_;
137
138         # git would never do this to us:
139         return if index($str, '//') >= 0;
140
141         my $first = '';
142         if ($str =~ s/\A([^A-Ya-z])//ms) { # start with a letter
143                   $first = sprintf('Z%02x', ord($1));
144         }
145         $str =~ s/([^A-Za-z0-9_\.\-])/$ESCAPES{$1}/egms;
146         $first . $str;
147 }
148
149 # reverse the result of to_attr
150 sub from_attr ($) {
151         my ($str) = @_;
152         my $first = '';
153         if ($str =~ s/\AZ([a-f0-9]{2})//ms) {
154                 $first = chr(hex($1));
155         }
156         $str =~ s!::([a-f0-9]{2})!chr(hex($1))!egms;
157         $str =~ tr!:!/!;
158         $first . $str;
159 }
160
161 1;