]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Hval.pm
a120a291bd4d85c1e37ce4c1b549a7cdd2d948eb
[public-inbox.git] / lib / PublicInbox / Hval.pm
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>
3 #
4 # represents a header value in various forms.  Used for HTML generation
5 # in our web interface(s)
6 package PublicInbox::Hval;
7 use strict;
8 use warnings;
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/;
13
14 # User-generated content (UGC) may have excessively long lines
15 # and screw up rendering on some browsers, so we use pre-wrap.
16 #
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.
24 use constant STYLE =>
25         '<style>pre{white-space:pre-wrap}' .
26         '*{font-size:100%;font-family:monospace}</style>';
27
28 my $enc_ascii = find_encoding('us-ascii');
29
30 sub new {
31         my ($class, $raw, $href) = @_;
32
33         # we never care about trailing whitespace
34         $raw =~ s/\s*\z//;
35         bless {
36                 raw => $raw,
37                 href => defined $href ? $href : $raw,
38         }, $class;
39 }
40
41 sub new_msgid {
42         my ($class, $msgid) = @_;
43         $class->new($msgid, mid_escape($msgid));
44 }
45
46 sub new_oneline {
47         my ($class, $raw) = @_;
48         $raw = '' unless defined $raw;
49         $raw =~ tr/\t\n / /s; # squeeze spaces
50         $raw =~ tr/\r//d; # kill CR
51         $class->new($raw);
52 }
53
54 my %xhtml_map = (
55         '"' => '&#34;',
56         '&' => '&#38;',
57         "'" => '&#39;',
58         '<' => '&lt;',
59         '>' => '&gt;',
60 );
61
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
75
76 sub ascii_html {
77         my ($s) = @_;
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);
81 }
82
83 sub as_html { ascii_html($_[0]->{raw}) }
84
85 sub raw {
86         if (defined $_[1]) {
87                 $_[0]->{raw} = $_[1];
88         } else {
89                 $_[0]->{raw};
90         }
91 }
92
93 sub prurl {
94         my ($env, $u) = @_;
95         index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
96 }
97
98 # for misguided people who believe in this stuff, give them a
99 # substitution for '.'
100 # &#8228; &#183; and &#890; were also candidates:
101 #   https://public-inbox.org/meta/20170615015250.GA6484@starla/
102 # However, &#8226; was chosen to make copy+paste errors more obvious
103 sub obfuscate_addrs ($$;$) {
104         my $ibx = $_[0];
105         my $repl = $_[2] || '&#8226;';
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))) {
111                         $addr;
112                 } else {
113                         $domain =~ s!([^\.]+)\.!$1$repl!;
114                         $user . '@' . $domain
115                 }
116                 /sge;
117 }
118
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;
123         $s =~ tr/././s;
124         $s =~ s/[\.\-]+\z//;
125         $s =~ s/\A[\.\-]+//;
126         $s
127 }
128
129 1;