]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/View.pm
cgi: eliminate dead/redundant HTML escaping code
[public-inbox.git] / lib / PublicInbox / View.pm
1 # Copyright (C) 2014, Eric Wong <normalperson@yhbt.net> and all contributors
2 # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt)
3 package PublicInbox::View;
4 use strict;
5 use warnings;
6 use PublicInbox::Hval;
7 use URI::Escape qw/uri_escape/;
8 use CGI qw/escapeHTML/;
9 use Encode qw/find_encoding/;
10 use Encode::MIME::Header;
11 use Email::MIME::ContentType qw/parse_content_type/;
12 use constant MAX_INLINE_QUOTED => 5;
13 use constant MAX_TRUNC_LEN => 72;
14 *ascii_html = *PublicInbox::Hval::ascii_html;
15
16 my $enc_utf8 = find_encoding('utf8');
17 my $enc_ascii = find_encoding('us-ascii');
18 my $enc_mime = find_encoding('MIME-Header');
19
20 # public functions:
21 sub as_html {
22         my ($class, $mime, $full_pfx) = @_;
23
24         headers_to_html_header($mime, $full_pfx) .
25                 multipart_text_as_html($mime, $full_pfx) .
26                 '</pre></body></html>';
27 }
28
29 sub as_feed_entry {
30         my ($class, $mime, $full_pfx) = @_;
31
32         "<pre>" . multipart_text_as_html($mime, $full_pfx) . "</pre>";
33 }
34
35
36 # only private functions below.
37
38 sub enc_for {
39         my ($ct) = @_;
40         defined $ct or return $enc_utf8;
41         my $ct_parsed = parse_content_type($ct);
42         if ($ct_parsed) {
43                 if (my $charset = $ct_parsed->{attributes}->{charset}) {
44                         my $enc = find_encoding($charset);
45                         return $enc if $enc;
46                 }
47         }
48         $enc_utf8;
49 }
50
51 sub multipart_text_as_html {
52         my ($mime, $full_pfx) = @_;
53         my $rv = "";
54         my $part_nr = 0;
55         my $enc_msg = enc_for($mime->header("Content-Type"));
56
57         # scan through all parts, looking for displayable text
58         $mime->walk_parts(sub {
59                 my ($part) = @_;
60                 return if $part->subparts; # walk_parts already recurses
61                 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
62
63                 if ($part_nr > 0) {
64                         my $fn = $part->filename;
65                         defined($fn) or $fn = "part #" . ($part_nr + 1);
66                         $rv .= add_filename_line($enc->decode($fn));
67                 }
68
69                 if (defined $full_pfx) {
70                         $rv .= add_text_body_short($enc, $part, $part_nr,
71                                                 $full_pfx);
72                 } else {
73                         $rv .= add_text_body_full($enc, $part, $part_nr);
74                 }
75                 $rv .= "\n" unless $rv =~ /\n\z/s;
76                 ++$part_nr;
77         });
78         $rv;
79 }
80
81 sub add_filename_line {
82         my ($fn) = @_;
83         my $len = 72;
84         my $pad = "-";
85
86         $len -= length($fn);
87         $pad x= ($len/2) if ($len > 0);
88         "$pad " . ascii_html($fn) . " $pad\n";
89 }
90
91 sub add_text_body_short {
92         my ($enc, $part, $part_nr, $full_pfx) = @_;
93         my $n = 0;
94         my $s = ascii_html($enc->decode($part->body));
95         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
96                 my $cur = $1;
97                 my @lines = split(/\n/, $cur);
98                 if (@lines > MAX_INLINE_QUOTED) {
99                         # show a short snippet of quoted text
100                         $cur = join(' ', @lines);
101                         $cur =~ s/^&gt;\s*//;
102
103                         my @sum = split(/\s+/, $cur);
104                         $cur = '';
105                         do {
106                                 my $tmp = shift(@sum);
107                                 my $len = length($tmp) + length($cur);
108                                 if ($len > MAX_TRUNC_LEN) {
109                                         @sum = ();
110                                 } else {
111                                         $cur .= $tmp . ' ';
112                                 }
113                         } while (@sum && length($cur) < MAX_TRUNC_LEN);
114                         $cur =~ s/ \z/ .../;
115                         "&gt; &lt;<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
116                                 "\">$cur<\/a>&gt;\n";
117                 } else {
118                         $cur;
119                 }
120         !emg;
121         $s;
122 }
123
124 sub add_text_body_full {
125         my ($enc, $part, $part_nr) = @_;
126         my $n = 0;
127         my $s = ascii_html($enc->decode($part->body));
128         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
129                 my $cur = $1;
130                 my @lines = split(/\n/, $cur);
131                 if (@lines > MAX_INLINE_QUOTED) {
132                         "<a name=q${part_nr}_" . $n++ . ">$cur</a>";
133                 } else {
134                         $cur;
135                 }
136         !emg;
137         $s;
138 }
139
140 sub headers_to_html_header {
141         my ($simple, $full_pfx) = @_;
142
143         my $rv = "";
144         my @title;
145         foreach my $h (qw(From To Cc Subject Date)) {
146                 my $v = $simple->header($h);
147                 defined $v or next;
148                 $v =~ tr/\n/ /s;
149                 $v =~ tr/\r//d;
150                 my $raw = $enc_mime->decode($v);
151                 $v = ascii_html($raw);
152                 $rv .= "$h: $v\n";
153
154                 if ($h eq 'From') {
155                         my @from = Email::Address->parse($raw);
156                         $raw = $from[0]->name;
157                         unless (defined($raw) && length($raw)) {
158                                 $raw = '<' . $from[0]->address . '>';
159                         }
160                         $title[1] = ascii_html($raw);
161
162                 } elsif ($h eq 'Subject') {
163                         $title[0] = $v;
164                 }
165         }
166
167         my $mid = $simple->header('Message-ID');
168         if (defined $mid) {
169                 $mid = PublicInbox::Hval->new_msgid($mid);
170                 $rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
171                 my $href = $mid->as_href;
172                 $href = "../m/$href" unless $full_pfx;
173                 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
174         }
175
176         my $irp = $simple->header('In-Reply-To');
177         if (defined $irp) {
178                 $irp = PublicInbox::Hval->new_msgid($irp);
179                 my $html = $irp->as_html;
180                 my $href = $irp->as_href;
181                 $rv .= "In-Reply-To: &lt;";
182                 $rv .= "<a href=\"$href.html\">$html</a>&gt;\n";
183         }
184         $rv .= "\n";
185
186         ("<html><head><title>".  join(' - ', @title) .
187          '</title></head><body><pre style="white-space:pre-wrap">' .  $rv);
188 }
189
190 1;