1 # Copyright (C) 2014-2018 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
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,
31 my ($class, $msgid) = @_;
32 $class->new($msgid, mid_escape($msgid));
36 my ($class, $raw) = @_;
37 $raw = '' unless defined $raw;
38 $raw =~ tr/\t\n / /s; # squeeze spaces
39 $raw =~ tr/\r//d; # kill CR
43 # some of these overrides are standard C escapes so they're
44 # easy-to-understand when rendered.
45 my %escape_sequence = (
46 "\x00" => '\\0', # NUL
47 "\x07" => '\\a', # bell
48 "\x08" => '\\b', # backspace
49 "\x09" => "\t", # obvious to show as-is
50 "\x0a" => "\n", # obvious to show as-is
51 "\x0b" => '\\v', # vertical tab
52 "\x0c" => '\\f', # form feed
53 "\x0d" => '\\r', # carriage ret (not preceding \n)
54 "\x1b" => '^[', # ASCII escape (mutt seems to escape this way)
55 "\x7f" => '\\x7f', # DEL
66 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
67 %xhtml_map = (%xhtml_map, %escape_sequence);
70 $_[0] =~ s/\r\n/\n/sg;
71 $_[0] =~ s/([\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
72 $_[0] = $enc_ascii->encode($_[0], Encode::HTMLCREF);
77 $s =~ s/\r\n/\n/sg; # fixup bad line endings
78 $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
79 $enc_ascii->encode($s, Encode::HTMLCREF);
82 sub as_html { ascii_html($_[0]->{raw}) }
94 index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
97 # for misguided people who believe in this stuff, give them a
98 # substitution for '.'
99 # ․ · and ͺ were also candidates:
100 # https://public-inbox.org/meta/20170615015250.GA6484@starla/
101 # However, • was chosen to make copy+paste errors more obvious
102 sub obfuscate_addrs ($$;$) {
104 my $repl = $_[2] || '•';
105 my $re = $ibx->{-no_obfuscate_re}; # regex of domains
106 my $addrs = $ibx->{-no_obfuscate}; # { adddress => 1 }
107 $_[1] =~ s/(([\w\.\+=\-]+)\@([\w\-]+\.[\w\.\-]+))/
108 my ($addr, $user, $domain) = ($1, $2, $3);
109 if ($addrs->{$addr} || ((defined $re && $domain =~ $re))) {
112 $domain =~ s!([^\.]+)\.!$1$repl!;
113 $user . '@' . $domain
118 # like format_sanitized_subject in git.git pretty.c with '%f' format string
119 sub to_filename ($) {
120 my ($s, undef) = split(/\n/, $_[0]);
121 $s =~ s/[^A-Za-z0-9_\.]+/-/g;
128 # convert a filename (or any string) to HTML attribute
130 my %ESCAPES = map { chr($_) => sprintf('::%02x', $_) } (0..255);
131 $ESCAPES{'/'} = ':'; # common
136 # git would never do this to us:
137 return if index($str, '//') >= 0;
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;
147 # reverse the result of to_attr
151 if ($str =~ s/\AZ([a-f0-9]{2})//ms) {
152 $first = chr(hex($1));
154 $str =~ s!::([a-f0-9]{2})!chr(hex($1))!egms;