]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Hval.pm
run update-copyrights from gnulib for 2019
[public-inbox.git] / lib / PublicInbox / Hval.pm
1 # Copyright (C) 2014-2019 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 src_escape
13                 to_attr from_attr/;
14 my $enc_ascii = find_encoding('us-ascii');
15
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
18
19 sub new {
20         my ($class, $raw, $href) = @_;
21
22         # we never care about trailing whitespace
23         $raw =~ s/\s*\z//;
24         bless {
25                 raw => $raw,
26                 href => defined $href ? $href : $raw,
27         }, $class;
28 }
29
30 sub new_msgid {
31         my ($class, $msgid) = @_;
32         $class->new($msgid, mid_escape($msgid));
33 }
34
35 sub new_oneline {
36         my ($class, $raw) = @_;
37         $raw = '' unless defined $raw;
38         $raw =~ tr/\t\n / /s; # squeeze spaces
39         $raw =~ tr/\r//d; # kill CR
40         $class->new($raw);
41 }
42
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
56 );
57
58 my %xhtml_map = (
59         '"' => '&#34;',
60         '&' => '&#38;',
61         "'" => '&#39;',
62         '<' => '&lt;',
63         '>' => '&gt;',
64 );
65
66 $xhtml_map{chr($_)} = sprintf('\\x%02x', $_) for (0..31);
67 %xhtml_map = (%xhtml_map, %escape_sequence);
68
69 sub src_escape ($) {
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);
73 }
74
75 sub ascii_html {
76         my ($s) = @_;
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);
80 }
81
82 sub as_html { ascii_html($_[0]->{raw}) }
83
84 sub raw {
85         if (defined $_[1]) {
86                 $_[0]->{raw} = $_[1];
87         } else {
88                 $_[0]->{raw};
89         }
90 }
91
92 sub prurl {
93         my ($env, $u) = @_;
94         index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
95 }
96
97 # for misguided people who believe in this stuff, give them a
98 # substitution for '.'
99 # &#8228; &#183; and &#890; were also candidates:
100 #   https://public-inbox.org/meta/20170615015250.GA6484@starla/
101 # However, &#8226; was chosen to make copy+paste errors more obvious
102 sub obfuscate_addrs ($$;$) {
103         my $ibx = $_[0];
104         my $repl = $_[2] || '&#8226;';
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))) {
110                         $addr;
111                 } else {
112                         $domain =~ s!([^\.]+)\.!$1$repl!;
113                         $user . '@' . $domain
114                 }
115                 /sge;
116 }
117
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;
122         $s =~ tr/././s;
123         $s =~ s/[\.\-]+\z//;
124         $s =~ s/\A[\.\-]+//;
125         $s
126 }
127
128 # convert a filename (or any string) to HTML attribute
129
130 my %ESCAPES = map { chr($_) => sprintf('::%02x', $_) } (0..255);
131 $ESCAPES{'/'} = ':'; # common
132
133 sub to_attr ($) {
134         my ($str) = @_;
135
136         # git would never do this to us:
137         return if index($str, '//') >= 0;
138
139         my $first = '';
140         if ($str =~ s/\A([^A-Ya-z])//ms) { # start with a letter
141                   $first = sprintf('Z%02x', ord($1));
142         }
143         $str =~ s/([^A-Za-z0-9_\.\-])/$ESCAPES{$1}/egms;
144         $first . $str;
145 }
146
147 # reverse the result of to_attr
148 sub from_attr ($) {
149         my ($str) = @_;
150         my $first = '';
151         if ($str =~ s/\AZ([a-f0-9]{2})//ms) {
152                 $first = chr(hex($1));
153         }
154         $str =~ s!::([a-f0-9]{2})!chr(hex($1))!egms;
155         $str =~ tr!:!/!;
156         $first . $str;
157 }
158
159 1;