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/;
14 my $enc_ascii = find_encoding('us-ascii');
17 my ($class, $raw, $href) = @_;
19 # we never care about trailing whitespace
23 href => defined $href ? $href : $raw,
28 my ($class, $msgid) = @_;
29 $class->new($msgid, mid_escape($msgid));
33 my ($class, $raw) = @_;
34 $raw = '' unless defined $raw;
35 $raw =~ tr/\t\n / /s; # squeeze spaces
36 $raw =~ tr/\r//d; # kill CR
40 # some of these overrides are standard C escapes so they're
41 # easy-to-understand when rendered.
42 my %escape_sequence = (
43 "\x00" => '\\0', # NUL
44 "\x07" => '\\a', # bell
45 "\x08" => '\\b', # backspace
46 "\x09" => "\t", # obvious to show as-is
47 "\x0a" => "\n", # obvious to show as-is
48 "\x0b" => '\\v', # vertical tab
49 "\x0c" => '\\f', # form feed
50 "\x0d" => '\\r', # carriage ret (not preceding \n)
51 "\x1b" => '^[', # ASCII escape (mutt seems to escape this way)
52 "\x7f" => '\\x7f', # DEL
63 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
64 %xhtml_map = (%xhtml_map, %escape_sequence);
68 $s =~ s/\r\n/\n/sg; # fixup bad line endings
69 $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
70 $enc_ascii->encode($s, Encode::HTMLCREF);
73 sub as_html { ascii_html($_[0]->{raw}) }
85 index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
88 # for misguided people who believe in this stuff, give them a
89 # substitution for '.'
90 # ․ · and ͺ were also candidates:
91 # https://public-inbox.org/meta/20170615015250.GA6484@starla/
92 # However, • was chosen to make copy+paste errors more obvious
93 sub obfuscate_addrs ($$;$) {
95 my $repl = $_[2] || '•';
96 my $re = $ibx->{-no_obfuscate_re}; # regex of domains
97 my $addrs = $ibx->{-no_obfuscate}; # { adddress => 1 }
98 $_[1] =~ s/(([\w\.\+=\-]+)\@([\w\-]+\.[\w\.\-]+))/
99 my ($addr, $user, $domain) = ($1, $2, $3);
100 if ($addrs->{$addr} || ((defined $re && $domain =~ $re))) {
103 $domain =~ s!([^\.]+)\.!$1$repl!;
104 $user . '@' . $domain
109 # like format_sanitized_subject in git.git pretty.c with '%f' format string
110 sub to_filename ($) {
111 my ($s, undef) = split(/\n/, $_[0]);
112 $s =~ s/[^A-Za-z0-9_\.]+/-/g;