]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/View.pm
view: reduce redundant linkage in index
[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 require POSIX;
12
13 # TODO: make these constants tunable
14 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
15 use constant MAX_TRUNC_LEN => 72;
16 use constant PRE_WRAP => '<pre style="white-space:pre-wrap">';
17
18 *ascii_html = *PublicInbox::Hval::ascii_html;
19
20 my $enc_utf8 = find_encoding('UTF-8');
21 my $enc_mime = find_encoding('MIME-Header');
22
23 # public functions:
24 sub msg_html {
25         my ($class, $mime, $full_pfx, $footer) = @_;
26         if (defined $footer) {
27                 $footer = "\n" . $footer;
28         } else {
29                 $footer = '';
30         }
31         headers_to_html_header($mime, $full_pfx) .
32                 multipart_text_as_html($mime, $full_pfx) .
33                 '</pre><hr />' . PRE_WRAP .
34                 html_footer($mime, 1) . $footer .
35                 '</pre></body></html>';
36 }
37
38 sub feed_entry {
39         my ($class, $mime, $full_pfx) = @_;
40
41         PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
42 }
43
44 # this is already inside a <pre>
45 sub index_entry {
46         my ($class, $mime, $now, $level, $seen) = @_;
47         my $rv = "";
48         my $part_nr = 0;
49         my $enc_msg = enc_for($mime->header("Content-Type"));
50         my $subj = $mime->header('Subject');
51         my $header_obj = $mime->header_obj;
52
53         my $mid_raw = $header_obj->header_raw('Message-ID');
54         my $name = anchor_for($mid_raw);
55         $seen->{$name} = "#$name"; # save the anchor for later
56
57         my $mid = PublicInbox::Hval->new_msgid($mid_raw);
58         my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
59         my @from = Email::Address->parse($from);
60         $from = $from[0]->name;
61         (defined($from) && length($from)) or $from = $from[0]->address;
62
63         $from = PublicInbox::Hval->new_oneline($from)->as_html;
64         $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
65         my $pfx = ('  ' x $level);
66
67         my $ts = $mime->header('X-PI-Date');
68         my $fmt = '%H:%M';
69         if ($now > ($ts + (365 * 24 * 60 * 60))) {
70                 # doesn't have to be exactly 1 year
71                 $fmt = '%Y/%m/%d';
72         } elsif ($now > ($ts + (24 * 60 * 60))) {
73                 $fmt = '%m/%d';
74         }
75         $ts = POSIX::strftime($fmt, gmtime($ts));
76
77         $rv .= "$pfx<a name=\"$name\"><b>$subj</b> $from - $ts</a>\n\n";
78
79         my $irp = $header_obj->header_raw('In-Reply-To');
80         my ($anchor_idx, $anchor);
81         if (defined $irp) {
82                 $anchor_idx = anchor_for($irp);
83                 $anchor = $seen->{$anchor_idx};
84         }
85         my $href = $mid->as_href;
86         my $mhref = "m/$href.html";
87         my $fhref = "f/$href.html";
88         my $more = 'link';
89         # scan through all parts, looking for displayable text
90         $mime->walk_parts(sub {
91                 my ($part) = @_;
92                 return if $part->subparts; # walk_parts already recurses
93                 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
94
95                 if ($part_nr > 0) {
96                         my $fn = $part->filename;
97                         defined($fn) or $fn = "part #" . ($part_nr + 1);
98                         $rv .= $pfx . add_filename_line($enc->decode($fn));
99                 }
100
101                 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
102
103                 # keep signatures for now?  They shold usually be short,
104                 # and sometimes footnotes/"P.S." appear there.
105
106                 # drop the remainder of git patches, they're usually better
107                 # to review when the full message is viewed
108                 $s =~ s!^---\n.*\z!!ms and $more = 'more...';
109
110                 # kill any leading or trailing whitespace
111                 $s =~ s/\A\s+//s;
112                 $s =~ s/\s+\z//s;
113
114                 # add prefix:
115                 $s =~ s/^/$pfx/sgm;
116
117                 $rv .= $s . "\n";
118                 ++$part_nr;
119         });
120
121         $rv .= "$pfx<a\nhref=\"$mhref\">$more</a> ";
122         my $txt = "m/$href.txt";
123         $rv .= "<a\nhref=\"$txt\">raw</a> ";
124         $rv .= html_footer($mime, 0);
125
126         if (defined $irp) {
127                 unless (defined $anchor) {
128                         my $v = PublicInbox::Hval->new_msgid($irp);
129                         my $html = $v->as_html;
130                         $anchor = 'm/' . $v->as_href . '.html';
131                         $seen->{$anchor_idx} = $anchor;
132                 }
133                 $rv .= " <a\nhref=\"$anchor\">parent</a>";
134         }
135
136         $rv . "\n\n";
137 }
138
139 # only private functions below.
140
141 sub enc_for {
142         my ($ct) = @_;
143         defined $ct or return $enc_utf8;
144         my $ct_parsed = parse_content_type($ct);
145         if ($ct_parsed) {
146                 if (my $charset = $ct_parsed->{attributes}->{charset}) {
147                         my $enc = find_encoding($charset);
148                         return $enc if $enc;
149                 }
150         }
151         $enc_utf8;
152 }
153
154 sub multipart_text_as_html {
155         my ($mime, $full_pfx) = @_;
156         my $rv = "";
157         my $part_nr = 0;
158         my $enc_msg = enc_for($mime->header("Content-Type"));
159
160         # scan through all parts, looking for displayable text
161         $mime->walk_parts(sub {
162                 my ($part) = @_;
163                 return if $part->subparts; # walk_parts already recurses
164                 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
165
166                 if ($part_nr > 0) {
167                         my $fn = $part->filename;
168                         defined($fn) or $fn = "part #" . ($part_nr + 1);
169                         $rv .= add_filename_line($enc->decode($fn));
170                 }
171
172                 if (defined $full_pfx) {
173                         $rv .= add_text_body_short($enc, $part, $part_nr,
174                                                 $full_pfx);
175                 } else {
176                         $rv .= add_text_body_full($enc, $part, $part_nr);
177                 }
178                 $rv .= "\n" unless $rv =~ /\n\z/s;
179                 ++$part_nr;
180         });
181         $rv;
182 }
183
184 sub add_filename_line {
185         my ($fn) = @_;
186         my $len = 72;
187         my $pad = "-";
188
189         $len -= length($fn);
190         $pad x= ($len/2) if ($len > 0);
191         "$pad " . ascii_html($fn) . " $pad\n";
192 }
193
194 sub add_text_body_short {
195         my ($enc, $part, $part_nr, $full_pfx) = @_;
196         my $n = 0;
197         my $s = ascii_html($enc->decode($part->body));
198         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
199                 my $cur = $1;
200                 my @lines = split(/\n/, $cur);
201                 if (@lines > MAX_INLINE_QUOTED) {
202                         # show a short snippet of quoted text
203                         $cur = join(' ', @lines);
204                         $cur =~ s/^&gt;\s*//;
205
206                         my @sum = split(/\s+/, $cur);
207                         $cur = '';
208                         do {
209                                 my $tmp = shift(@sum);
210                                 my $len = length($tmp) + length($cur);
211                                 if ($len > MAX_TRUNC_LEN) {
212                                         @sum = ();
213                                 } else {
214                                         $cur .= $tmp . ' ';
215                                 }
216                         } while (@sum && length($cur) < MAX_TRUNC_LEN);
217                         $cur =~ s/ \z/ .../;
218                         "&gt; &lt;<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
219                                 "\">$cur<\/a>&gt;\n";
220                 } else {
221                         $cur;
222                 }
223         !emg;
224         $s;
225 }
226
227 sub add_text_body_full {
228         my ($enc, $part, $part_nr) = @_;
229         my $n = 0;
230         my $s = ascii_html($enc->decode($part->body));
231         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
232                 my $cur = $1;
233                 my @lines = split(/\n/, $cur);
234                 if (@lines > MAX_INLINE_QUOTED) {
235                         "<a name=q${part_nr}_" . $n++ . ">$cur</a>";
236                 } else {
237                         $cur;
238                 }
239         !emg;
240         $s;
241 }
242
243 sub headers_to_html_header {
244         my ($mime, $full_pfx) = @_;
245
246         my $rv = "";
247         my @title;
248         foreach my $h (qw(From To Cc Subject Date)) {
249                 my $v = $mime->header($h);
250                 defined($v) && length($v) or next;
251                 $v = PublicInbox::Hval->new_oneline($v);
252                 $rv .= "$h: " . $v->as_html . "\n";
253
254                 if ($h eq 'From') {
255                         my @from = Email::Address->parse($v->raw);
256                         $v = $from[0]->name;
257                         unless (defined($v) && length($v)) {
258                                 $v = '<' . $from[0]->address . '>';
259                         }
260                         $title[1] = ascii_html($v);
261                 } elsif ($h eq 'Subject') {
262                         $title[0] = $v->as_html;
263                 }
264         }
265
266         my $header_obj = $mime->header_obj;
267         my $mid = $header_obj->header_raw('Message-ID');
268         if (defined $mid) {
269                 $mid = PublicInbox::Hval->new_msgid($mid);
270                 $rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
271                 my $href = $mid->as_href;
272                 $href = "../m/$href" unless $full_pfx;
273                 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
274         }
275
276         my $irp = $header_obj->header_raw('In-Reply-To');
277         if (defined $irp) {
278                 my $v = PublicInbox::Hval->new_msgid(my $tmp = $irp);
279                 my $html = $v->as_html;
280                 my $href = $v->as_href;
281                 $rv .= "In-Reply-To: &lt;";
282                 $rv .= "<a href=\"$href.html\">$html</a>&gt;\n";
283         }
284
285         my $refs = $header_obj->header_raw('References');
286         if ($refs) {
287                 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
288                 my @refs = ($refs =~ /<([^>]+)>/g);
289                 if (@refs) {
290                         $rv .= 'References: '. linkify_refs(@refs) . "\n";
291                 }
292         }
293
294         $rv .= "\n";
295
296         ("<html><head><title>".  join(' - ', @title) .
297          '</title></head><body>' . PRE_WRAP . $rv);
298 }
299
300 sub html_footer {
301         my ($mime, $purge) = @_;
302         my %cc; # everyone else
303         my $to; # this is the From address
304
305         foreach my $h (qw(From To Cc)) {
306                 my $v = $mime->header($h);
307                 defined($v) && length($v) or next;
308                 my @addrs = Email::Address->parse($v);
309                 foreach my $recip (@addrs) {
310                         my $address = $recip->address;
311                         my $dst = lc($address);
312                         $cc{$dst} ||= $address;
313                         $to ||= $dst;
314                 }
315         }
316         Email::Address->purge_cache if $purge;
317
318         my $subj = $mime->header('Subject') || '';
319         $subj = "Re: $subj" unless $subj =~ /\bRe:/;
320         my $irp = uri_escape_utf8(
321                         $mime->header_obj->header_raw('Message-ID') || '');
322         delete $cc{$to};
323         $to = uri_escape_utf8($to);
324         $subj = uri_escape_utf8($subj);
325
326         my $cc = uri_escape_utf8(join(',', values %cc));
327         my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
328
329         "<a\nhref=\"" . ascii_html($href) . '">reply</a>';
330 }
331
332 sub linkify_refs {
333         join(' ', map {
334                 my $v = PublicInbox::Hval->new_msgid($_);
335                 my $html = $v->as_html;
336                 my $href = $v->as_href;
337                 "&lt;<a href=\"$href.html\">$html</a>&gt;";
338         } @_);
339 }
340
341 require Digest::SHA;
342 sub anchor_for {
343         my ($msgid) = @_;
344         $msgid =~ s/\A\s*<?//;
345         $msgid =~ s/>?\s*\z//;
346         Digest::SHA::sha1_hex($msgid);
347 }
348
349 1;