]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Hval.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / Hval.pm
1 # Copyright (C) 2014-2021 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 prurl mid_href fmt_ts ts2str/;
14 use POSIX qw(strftime);
15 my $enc_ascii = find_encoding('us-ascii');
16
17 # safe-ish acceptable filename pattern for portability
18 our $FN = '[a-zA-Z0-9][a-zA-Z0-9_\-\.]+[a-zA-Z0-9]'; # needs \z anchor
19
20 sub mid_href { ascii_html(mid_escape($_[0])) }
21
22 # some of these overrides are standard C escapes so they're
23 # easy-to-understand when rendered.
24 my %escape_sequence = (
25         "\x00" => '\\0', # NUL
26         "\x07" => '\\a', # bell
27         "\x08" => '\\b', # backspace
28         "\x09" => "\t", # obvious to show as-is
29         "\x0a" => "\n", # obvious to show as-is
30         "\x0b" => '\\v', # vertical tab
31         "\x0c" => '\\f', # form feed
32         "\x0d" => '\\r', # carriage ret (not preceding \n)
33         "\x1b" => '^[', # ASCII escape (mutt seems to escape this way)
34         "\x7f" => '\\x7f', # DEL
35 );
36
37 my %xhtml_map = (
38         '"' => '&#34;',
39         '&' => '&#38;',
40         "'" => '&#39;',
41         '<' => '&lt;',
42         '>' => '&gt;',
43 );
44
45 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
46 %xhtml_map = (%xhtml_map, %escape_sequence);
47
48 # for post-processing the output of highlight.pm and perhaps other
49 # highlighers in the future
50 sub src_escape ($) {
51         $_[0] =~ s/\r\n/\n/sg;
52         $_[0] =~ s/&apos;/&#39;/sg; # workaround https://bugs.debian.org/927409
53         $_[0] =~ s/([\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
54         $_[0] = $enc_ascii->encode($_[0], Encode::HTMLCREF);
55 }
56
57 sub ascii_html {
58         my ($s) = @_;
59         $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
60         $enc_ascii->encode($s, Encode::HTMLCREF);
61 }
62
63 # returns a protocol-relative URL string
64 sub prurl ($$) {
65         my ($env, $u) = @_;
66         if (ref($u) eq 'ARRAY') {
67                 my $h = $env->{HTTP_HOST} // $env->{SERVER_NAME};
68                 my @host_match = grep(/\b\Q$h\E\b/, @$u);
69                 $u = $host_match[0] // $u->[0];
70                 # fall through to below:
71         }
72         index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
73 }
74
75 # for misguided people who believe in this stuff, give them a
76 # substitution for '.'
77 # &#8228; &#183; and &#890; were also candidates:
78 #   https://public-inbox.org/meta/20170615015250.GA6484@starla/
79 # However, &#8226; was chosen to make copy+paste errors more obvious
80 sub obfuscate_addrs ($$;$) {
81         my $ibx = $_[0];
82         my $repl = $_[2] // '&#8226;';
83         my $re = $ibx->{-no_obfuscate_re}; # regex of domains
84         my $addrs = $ibx->{-no_obfuscate}; # { $address => 1 }
85         $_[1] =~ s/(([\w\.\+=\-]+)\@([\w\-]+\.[\w\.\-]+))/
86                 my ($addr, $user, $domain) = ($1, $2, $3);
87                 if ($addrs->{$addr} || ((defined $re && $domain =~ $re))) {
88                         $addr;
89                 } else {
90                         $domain =~ s!([^\.]+)\.!$1$repl!;
91                         $user . '@' . $domain
92                 }
93                 /sge;
94 }
95
96 # like format_sanitized_subject in git.git pretty.c with '%f' format string
97 sub to_filename ($) {
98         my $s = (split(/\n/, $_[0]))[0] // return; # empty string => undef
99         $s =~ s/[^A-Za-z0-9_\.]+/-/g;
100         $s =~ tr/././s;
101         $s =~ s/[\.\-]+\z//;
102         $s =~ s/\A[\.\-]+//;
103         $s eq '' ? undef : $s;
104 }
105
106 # convert a filename (or any string) to HTML attribute
107
108 my %ESCAPES = map { chr($_) => sprintf('::%02x', $_) } (0..255);
109 $ESCAPES{'/'} = ':'; # common
110
111 sub to_attr ($) {
112         my ($str) = @_;
113
114         # git would never do this to us:
115         return if index($str, '//') >= 0;
116
117         my $first = '';
118         utf8::encode($str); # to octets
119         if ($str =~ s/\A([^A-Ya-z])//ms) { # start with a letter
120                   $first = sprintf('Z%02x', ord($1));
121         }
122         $str =~ s/([^A-Za-z0-9_\.\-])/$ESCAPES{$1}/egms;
123         utf8::decode($str); # allow wide chars
124         $first . $str;
125 }
126
127 # for the t= query parameter passed to overview DB
128 sub ts2str ($) { strftime('%Y%m%d%H%M%S', gmtime($_[0])) };
129
130 # human-friendly format
131 sub fmt_ts ($) { strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
132
133 1;