]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Hval.pm
www: admin-configurable CSS via "publicinbox.css"
[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 my $enc_ascii = find_encoding('us-ascii');
15
16 sub new {
17         my ($class, $raw, $href) = @_;
18
19         # we never care about trailing whitespace
20         $raw =~ s/\s*\z//;
21         bless {
22                 raw => $raw,
23                 href => defined $href ? $href : $raw,
24         }, $class;
25 }
26
27 sub new_msgid {
28         my ($class, $msgid) = @_;
29         $class->new($msgid, mid_escape($msgid));
30 }
31
32 sub new_oneline {
33         my ($class, $raw) = @_;
34         $raw = '' unless defined $raw;
35         $raw =~ tr/\t\n / /s; # squeeze spaces
36         $raw =~ tr/\r//d; # kill CR
37         $class->new($raw);
38 }
39
40 my %xhtml_map = (
41         '"' => '&#34;',
42         '&' => '&#38;',
43         "'" => '&#39;',
44         '<' => '&lt;',
45         '>' => '&gt;',
46 );
47
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
61
62 sub ascii_html {
63         my ($s) = @_;
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);
67 }
68
69 sub as_html { ascii_html($_[0]->{raw}) }
70
71 sub raw {
72         if (defined $_[1]) {
73                 $_[0]->{raw} = $_[1];
74         } else {
75                 $_[0]->{raw};
76         }
77 }
78
79 sub prurl {
80         my ($env, $u) = @_;
81         index($u, '//') == 0 ? "$env->{'psgi.url_scheme'}:$u" : $u;
82 }
83
84 # for misguided people who believe in this stuff, give them a
85 # substitution for '.'
86 # &#8228; &#183; and &#890; were also candidates:
87 #   https://public-inbox.org/meta/20170615015250.GA6484@starla/
88 # However, &#8226; was chosen to make copy+paste errors more obvious
89 sub obfuscate_addrs ($$;$) {
90         my $ibx = $_[0];
91         my $repl = $_[2] || '&#8226;';
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))) {
97                         $addr;
98                 } else {
99                         $domain =~ s!([^\.]+)\.!$1$repl!;
100                         $user . '@' . $domain
101                 }
102                 /sge;
103 }
104
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;
109         $s =~ tr/././s;
110         $s =~ s/[\.\-]+\z//;
111         $s =~ s/\A[\.\-]+//;
112         $s
113 }
114
115 1;