1 # Copyright (C) 2014-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # represents a header value in various forms. Used for HTML generation
5 # in our web interface(s)
6 package PublicInbox::Hval;
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/;
14 my $enc_ascii = find_encoding('us-ascii');
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
20 my ($class, $raw, $href) = @_;
22 # we never care about trailing whitespace
26 href => defined $href ? $href : $raw,
30 sub mid_href { ascii_html(mid_escape($_[0])) }
32 # some of these overrides are standard C escapes so they're
33 # easy-to-understand when rendered.
34 my %escape_sequence = (
35 "\x00" => '\\0', # NUL
36 "\x07" => '\\a', # bell
37 "\x08" => '\\b', # backspace
38 "\x09" => "\t", # obvious to show as-is
39 "\x0a" => "\n", # obvious to show as-is
40 "\x0b" => '\\v', # vertical tab
41 "\x0c" => '\\f', # form feed
42 "\x0d" => '\\r', # carriage ret (not preceding \n)
43 "\x1b" => '^[', # ASCII escape (mutt seems to escape this way)
44 "\x7f" => '\\x7f', # DEL
55 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
56 %xhtml_map = (%xhtml_map, %escape_sequence);
58 # for post-processing the output of highlight.pm and perhaps other
59 # highlighers in the future
61 $_[0] =~ s/\r\n/\n/sg;
62 $_[0] =~ s/'/'/sg; # workaround https://bugs.debian.org/927409
63 $_[0] =~ s/([\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
64 $_[0] = $enc_ascii->encode($_[0], Encode::HTMLCREF);
69 $s =~ s/\r\n/\n/sg; # fixup bad line endings
70 $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
71 $enc_ascii->encode($s, Encode::HTMLCREF);
74 sub as_html { ascii_html($_[0]->{raw}) }
84 # returns a protocol-relative URL string
87 if (ref($u) eq 'ARRAY') {
88 my $h = $env->{HTTP_HOST} // $env->{SERVER_NAME};
89 my @host_match = grep(/\b\Q$h\E\b/, @$u);
90 $u = $host_match[0] // $u->[0];
91 # fall through to below:
93 index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
96 # for misguided people who believe in this stuff, give them a
97 # substitution for '.'
98 # ․ · and ͺ were also candidates:
99 # https://public-inbox.org/meta/20170615015250.GA6484@starla/
100 # However, • was chosen to make copy+paste errors more obvious
101 sub obfuscate_addrs ($$;$) {
103 my $repl = $_[2] || '•';
104 my $re = $ibx->{-no_obfuscate_re}; # regex of domains
105 my $addrs = $ibx->{-no_obfuscate}; # { adddress => 1 }
106 $_[1] =~ s/(([\w\.\+=\-]+)\@([\w\-]+\.[\w\.\-]+))/
107 my ($addr, $user, $domain) = ($1, $2, $3);
108 if ($addrs->{$addr} || ((defined $re && $domain =~ $re))) {
111 $domain =~ s!([^\.]+)\.!$1$repl!;
112 $user . '@' . $domain
117 # like format_sanitized_subject in git.git pretty.c with '%f' format string
118 sub to_filename ($) {
119 my ($s, undef) = split(/\n/, $_[0]);
120 $s =~ s/[^A-Za-z0-9_\.]+/-/g;
127 # convert a filename (or any string) to HTML attribute
129 my %ESCAPES = map { chr($_) => sprintf('::%02x', $_) } (0..255);
130 $ESCAPES{'/'} = ':'; # common
135 # git would never do this to us:
136 return if index($str, '//') >= 0;
139 utf8::encode($str); # to octets
140 if ($str =~ s/\A([^A-Ya-z])//ms) { # start with a letter
141 $first = sprintf('Z%02x', ord($1));
143 $str =~ s/([^A-Za-z0-9_\.\-])/$ESCAPES{$1}/egms;
144 utf8::decode($str); # allow wide chars