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