]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/View.pm
52e8f0b290b59692f50693af0999c525493af6c9
[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_utf8/;
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 msg_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><hr /><pre>' .
25                 html_footer($mime) .
26                 '</pre></body></html>';
27 }
28
29 sub 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 ($mime, $full_pfx) = @_;
142
143         my $rv = "";
144         my @title;
145         foreach my $h (qw(From To Cc Subject Date)) {
146                 my $v = $mime->header($h);
147                 defined($v) && length($v) or next;
148                 $v = PublicInbox::Hval->new_oneline($v);
149                 $rv .= "$h: " . $v->as_html . "\n";
150
151                 if ($h eq 'From') {
152                         my @from = Email::Address->parse($v->raw);
153                         $v = $from[0]->name;
154                         unless (defined($v) && length($v)) {
155                                 $v = '<' . $from[0]->address . '>';
156                         }
157                         $title[1] = ascii_html($v);
158                 } elsif ($h eq 'Subject') {
159                         $title[0] = $v->as_html;
160                 }
161         }
162
163         my $header_obj = $mime->header_obj;
164         my $mid = $header_obj->header_raw('Message-ID');
165         if (defined $mid) {
166                 $mid = PublicInbox::Hval->new_msgid($mid);
167                 $rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
168                 my $href = $mid->as_href;
169                 $href = "../m/$href" unless $full_pfx;
170                 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
171         }
172
173         my $irp = $header_obj->header_raw('In-Reply-To');
174         if (defined $irp) {
175                 my $v = PublicInbox::Hval->new_msgid(my $tmp = $irp);
176                 my $html = $v->as_html;
177                 my $href = $v->as_href;
178                 $rv .= "In-Reply-To: &lt;";
179                 $rv .= "<a href=\"$href.html\">$html</a>&gt;\n";
180         }
181
182         my $refs = $header_obj->header_raw('References');
183         if ($refs) {
184                 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
185                 my @refs = ($refs =~ /<([^>]+)>/g);
186                 if (@refs) {
187                         $rv .= 'References: '. linkify_refs(@refs) . "\n";
188                 }
189         }
190
191         $rv .= "\n";
192
193         ("<html><head><title>".  join(' - ', @title) .
194          '</title></head><body><pre style="white-space:pre-wrap">' .  $rv);
195 }
196
197 sub html_footer {
198         my ($mime) = @_;
199         my %cc; # everyone else
200         my $to; # this is the From address
201
202         foreach my $h (qw(From To Cc)) {
203                 my $v = $mime->header($h);
204                 defined($v) && length($v) or next;
205                 my @addrs = Email::Address->parse($v);
206                 foreach my $recip (@addrs) {
207                         my $address = $recip->address;
208                         my $dst = lc($address);
209                         $cc{$dst} ||= $address;
210                         $to ||= $dst;
211                 }
212         }
213         Email::Address->purge_cache;
214
215         my $subj = $mime->header('Subject') || '';
216         $subj = "Re: $subj" unless $subj =~ /\bRe:/;
217         my $irp = uri_escape_utf8(
218                         $mime->header_obj->header_raw('Message-ID') || '');
219         delete $cc{$to};
220         $to = uri_escape_utf8($to);
221         $subj = uri_escape_utf8($subj);
222
223         my $cc = uri_escape_utf8(join(',', values %cc));
224         my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
225
226         '<a href="' . ascii_html($href) . '">reply</a>';
227 }
228
229 sub linkify_refs {
230         join(' ', map {
231                 my $v = PublicInbox::Hval->new_msgid($_);
232                 my $html = $v->as_html;
233                 my $href = $v->as_href;
234                 "&lt;<a href=\"$href.html\">$html</a>&gt;";
235         } @_);
236 }
237
238 1;