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 # User-generated content (UGC) may have excessively long lines
15 # and screw up rendering on some browsers, so we use pre-wrap.
17 # We also force everything to the same scaled font-size because GUI
18 # browsers (tested both Firefox and surf (webkit)) uses a larger font
19 # for the Search <form> element than the rest of the page. Font size
20 # uniformity is important to people who rely on gigantic fonts.
21 # Finally, we use monospace to ensure the Search field and button
22 # has the same size and spacing as everything else which is
23 # <pre>-formatted anyways.
25 '<style>pre{white-space:pre-wrap}' .
26 '*{font-size:100%;font-family:monospace}</style>';
28 my $enc_ascii = find_encoding('us-ascii');
31 my ($class, $raw, $href) = @_;
33 # we never care about trailing whitespace
37 href => defined $href ? $href : $raw,
42 my ($class, $msgid) = @_;
43 $class->new($msgid, mid_escape($msgid));
47 my ($class, $raw) = @_;
48 $raw = '' unless defined $raw;
49 $raw =~ tr/\t\n / /s; # squeeze spaces
50 $raw =~ tr/\r//d; # kill CR
62 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
63 # some of these overrides are standard C escapes so they're
64 # easy-to-understand when rendered.
65 $xhtml_map{"\x00"} = '\\0'; # NUL
66 $xhtml_map{"\x07"} = '\\a'; # bell
67 $xhtml_map{"\x08"} = '\\b'; # backspace
68 $xhtml_map{"\x09"} = "\t"; # obvious to show as-is
69 $xhtml_map{"\x0a"} = "\n"; # obvious to show as-is
70 $xhtml_map{"\x0b"} = '\\v'; # vertical tab
71 $xhtml_map{"\x0c"} = '\\f'; # form feed
72 $xhtml_map{"\x0d"} = '\\r'; # carriage ret (not preceding \n)
73 $xhtml_map{"\x1b"} = '^['; # ASCII escape (mutt seems to escape this way)
74 $xhtml_map{"\x7f"} = '\\x7f'; # DEL
78 $s =~ s/\r\n/\n/sg; # fixup bad line endings
79 $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
80 $enc_ascii->encode($s, Encode::HTMLCREF);
83 sub as_html { ascii_html($_[0]->{raw}) }
95 index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
98 # for misguided people who believe in this stuff, give them a
99 # substitution for '.'
100 # ․ · and ͺ were also candidates:
101 # https://public-inbox.org/meta/20170615015250.GA6484@starla/
102 # However, • was chosen to make copy+paste errors more obvious
103 sub obfuscate_addrs ($$;$) {
105 my $repl = $_[2] || '•';
106 my $re = $ibx->{-no_obfuscate_re}; # regex of domains
107 my $addrs = $ibx->{-no_obfuscate}; # { adddress => 1 }
108 $_[1] =~ s/(([\w\.\+=\-]+)\@([\w\-]+\.[\w\.\-]+))/
109 my ($addr, $user, $domain) = ($1, $2, $3);
110 if ($addrs->{$addr} || ((defined $re && $domain =~ $re))) {
113 $domain =~ s!([^\.]+)\.!$1$repl!;
114 $user . '@' . $domain
119 # like format_sanitized_subject in git.git pretty.c with '%f' format string
120 sub to_filename ($) {
121 my ($s, undef) = split(/\n/, $_[0]);
122 $s =~ s/[^A-Za-z0-9_\.]+/-/g;