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
48 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
49 # some of these overrides are standard C escapes so they're
50 # easy-to-understand when rendered.
51 $xhtml_map{"\x00"} = '\\0'; # NUL
52 $xhtml_map{"\x07"} = '\\a'; # bell
53 $xhtml_map{"\x08"} = '\\b'; # backspace
54 $xhtml_map{"\x09"} = "\t"; # obvious to show as-is
55 $xhtml_map{"\x0a"} = "\n"; # obvious to show as-is
56 $xhtml_map{"\x0b"} = '\\v'; # vertical tab
57 $xhtml_map{"\x0c"} = '\\f'; # form feed
58 $xhtml_map{"\x0d"} = '\\r'; # carriage ret (not preceding \n)
59 $xhtml_map{"\x1b"} = '^['; # ASCII escape (mutt seems to escape this way)
60 $xhtml_map{"\x7f"} = '\\x7f'; # DEL
64 $s =~ s/\r\n/\n/sg; # fixup bad line endings
65 $s =~ s/([<>&'"\x7f\x00-\x1f])/$xhtml_map{$1}/sge;
66 $enc_ascii->encode($s, Encode::HTMLCREF);
69 sub as_html { ascii_html($_[0]->{raw}) }
81 index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
84 # for misguided people who believe in this stuff, give them a
85 # substitution for '.'
86 # ․ · and ͺ were also candidates:
87 # https://public-inbox.org/meta/20170615015250.GA6484@starla/
88 # However, • was chosen to make copy+paste errors more obvious
89 sub obfuscate_addrs ($$;$) {
91 my $repl = $_[2] || '•';
92 my $re = $ibx->{-no_obfuscate_re}; # regex of domains
93 my $addrs = $ibx->{-no_obfuscate}; # { adddress => 1 }
94 $_[1] =~ s/(([\w\.\+=\-]+)\@([\w\-]+\.[\w\.\-]+))/
95 my ($addr, $user, $domain) = ($1, $2, $3);
96 if ($addrs->{$addr} || ((defined $re && $domain =~ $re))) {
99 $domain =~ s!([^\.]+)\.!$1$repl!;
100 $user . '@' . $domain
105 # like format_sanitized_subject in git.git pretty.c with '%f' format string
106 sub to_filename ($) {
107 my ($s, undef) = split(/\n/, $_[0]);
108 $s =~ s/[^A-Za-z0-9_\.]+/-/g;