]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/View.pm
view: show quotes in index if parent is too old
[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
86         # scan through all parts, looking for displayable text
87         $mime->walk_parts(sub {
88                 my ($part) = @_;
89                 return if $part->subparts; # walk_parts already recurses
90                 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
91                 my $more = '';
92
93                 if ($part_nr > 0) {
94                         my $fn = $part->filename;
95                         defined($fn) or $fn = "part #" . ($part_nr + 1);
96                         $rv .= $pfx . add_filename_line($enc->decode($fn));
97                 }
98
99                 my $s = ascii_html($enc->decode($part->body));
100
101                 if (defined $anchor) {
102                         # drop quotes, including the "so-and-so wrote:" line
103                         $s =~ s/(?:^[^\n]*:\s*\n)?
104                                (?:^&gt;[^\n]*\n)+(?:^\s*\n)?//mgx;
105                 }
106
107                 # Drop signatures
108                 $s =~ s/^-- \n.*\z//ms;
109
110                 # drop the remainder of git patches, they're usually better
111                 # to review when the full message is viewed
112                 if ($s =~ s/^---\n.*\z//ms) {
113                         $more = "$pfx...\n";
114                 }
115
116                 # kill any trailing whitespace
117                 $s =~ s/\s+\z//s;
118
119                 # add prefix:
120                 $s =~ s/^/$pfx/sgm;
121
122                 $rv .= $s . "\n$more";
123                 ++$part_nr;
124         });
125
126         my $href = 'm/' . $mid->as_href . '.html';
127         $rv .= "$pfx<a\nhref=\"$href\">more</a> ";
128         my $txt = 'm/' . $mid->as_href . '.txt';
129         $rv .= "<a\nhref=\"$txt\">raw</a> ";
130         $rv .= html_footer($mime, 0);
131
132         if (defined $irp) {
133                 unless (defined $anchor) {
134                         my $v = PublicInbox::Hval->new_msgid($irp);
135                         my $html = $v->as_html;
136                         $anchor = 'm/' . $v->as_href . '.html';
137                         $seen->{$anchor_idx} = $anchor;
138                 }
139                 $rv .= " <a\nhref=\"$anchor\">parent</a>";
140         }
141
142         $rv . "\n\n";
143 }
144
145 # only private functions below.
146
147 sub enc_for {
148         my ($ct) = @_;
149         defined $ct or return $enc_utf8;
150         my $ct_parsed = parse_content_type($ct);
151         if ($ct_parsed) {
152                 if (my $charset = $ct_parsed->{attributes}->{charset}) {
153                         my $enc = find_encoding($charset);
154                         return $enc if $enc;
155                 }
156         }
157         $enc_utf8;
158 }
159
160 sub multipart_text_as_html {
161         my ($mime, $full_pfx) = @_;
162         my $rv = "";
163         my $part_nr = 0;
164         my $enc_msg = enc_for($mime->header("Content-Type"));
165
166         # scan through all parts, looking for displayable text
167         $mime->walk_parts(sub {
168                 my ($part) = @_;
169                 return if $part->subparts; # walk_parts already recurses
170                 my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
171
172                 if ($part_nr > 0) {
173                         my $fn = $part->filename;
174                         defined($fn) or $fn = "part #" . ($part_nr + 1);
175                         $rv .= add_filename_line($enc->decode($fn));
176                 }
177
178                 if (defined $full_pfx) {
179                         $rv .= add_text_body_short($enc, $part, $part_nr,
180                                                 $full_pfx);
181                 } else {
182                         $rv .= add_text_body_full($enc, $part, $part_nr);
183                 }
184                 $rv .= "\n" unless $rv =~ /\n\z/s;
185                 ++$part_nr;
186         });
187         $rv;
188 }
189
190 sub add_filename_line {
191         my ($fn) = @_;
192         my $len = 72;
193         my $pad = "-";
194
195         $len -= length($fn);
196         $pad x= ($len/2) if ($len > 0);
197         "$pad " . ascii_html($fn) . " $pad\n";
198 }
199
200 sub add_text_body_short {
201         my ($enc, $part, $part_nr, $full_pfx) = @_;
202         my $n = 0;
203         my $s = ascii_html($enc->decode($part->body));
204         # TODO: fold the "so-and-so wrote:" attribute line here, too:
205         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
206                 my $cur = $1;
207                 my @lines = split(/\n/, $cur);
208                 if (@lines > MAX_INLINE_QUOTED) {
209                         # show a short snippet of quoted text
210                         $cur = join(' ', @lines);
211                         $cur =~ s/^&gt;\s*//;
212
213                         my @sum = split(/\s+/, $cur);
214                         $cur = '';
215                         do {
216                                 my $tmp = shift(@sum);
217                                 my $len = length($tmp) + length($cur);
218                                 if ($len > MAX_TRUNC_LEN) {
219                                         @sum = ();
220                                 } else {
221                                         $cur .= $tmp . ' ';
222                                 }
223                         } while (@sum && length($cur) < MAX_TRUNC_LEN);
224                         $cur =~ s/ \z/ .../;
225                         "&gt; &lt;<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
226                                 "\">$cur<\/a>&gt;\n";
227                 } else {
228                         $cur;
229                 }
230         !emg;
231         $s;
232 }
233
234 sub add_text_body_full {
235         my ($enc, $part, $part_nr) = @_;
236         my $n = 0;
237         my $s = ascii_html($enc->decode($part->body));
238         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
239                 my $cur = $1;
240                 my @lines = split(/\n/, $cur);
241                 if (@lines > MAX_INLINE_QUOTED) {
242                         "<a name=q${part_nr}_" . $n++ . ">$cur</a>";
243                 } else {
244                         $cur;
245                 }
246         !emg;
247         $s;
248 }
249
250 sub headers_to_html_header {
251         my ($mime, $full_pfx) = @_;
252
253         my $rv = "";
254         my @title;
255         foreach my $h (qw(From To Cc Subject Date)) {
256                 my $v = $mime->header($h);
257                 defined($v) && length($v) or next;
258                 $v = PublicInbox::Hval->new_oneline($v);
259                 $rv .= "$h: " . $v->as_html . "\n";
260
261                 if ($h eq 'From') {
262                         my @from = Email::Address->parse($v->raw);
263                         $v = $from[0]->name;
264                         unless (defined($v) && length($v)) {
265                                 $v = '<' . $from[0]->address . '>';
266                         }
267                         $title[1] = ascii_html($v);
268                 } elsif ($h eq 'Subject') {
269                         $title[0] = $v->as_html;
270                 }
271         }
272
273         my $header_obj = $mime->header_obj;
274         my $mid = $header_obj->header_raw('Message-ID');
275         if (defined $mid) {
276                 $mid = PublicInbox::Hval->new_msgid($mid);
277                 $rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
278                 my $href = $mid->as_href;
279                 $href = "../m/$href" unless $full_pfx;
280                 $rv .= "(<a href=\"$href.txt\">original</a>)\n";
281         }
282
283         my $irp = $header_obj->header_raw('In-Reply-To');
284         if (defined $irp) {
285                 my $v = PublicInbox::Hval->new_msgid(my $tmp = $irp);
286                 my $html = $v->as_html;
287                 my $href = $v->as_href;
288                 $rv .= "In-Reply-To: &lt;";
289                 $rv .= "<a href=\"$href.html\">$html</a>&gt;\n";
290         }
291
292         my $refs = $header_obj->header_raw('References');
293         if ($refs) {
294                 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
295                 my @refs = ($refs =~ /<([^>]+)>/g);
296                 if (@refs) {
297                         $rv .= 'References: '. linkify_refs(@refs) . "\n";
298                 }
299         }
300
301         $rv .= "\n";
302
303         ("<html><head><title>".  join(' - ', @title) .
304          '</title></head><body>' . PRE_WRAP . $rv);
305 }
306
307 sub html_footer {
308         my ($mime, $purge) = @_;
309         my %cc; # everyone else
310         my $to; # this is the From address
311
312         foreach my $h (qw(From To Cc)) {
313                 my $v = $mime->header($h);
314                 defined($v) && length($v) or next;
315                 my @addrs = Email::Address->parse($v);
316                 foreach my $recip (@addrs) {
317                         my $address = $recip->address;
318                         my $dst = lc($address);
319                         $cc{$dst} ||= $address;
320                         $to ||= $dst;
321                 }
322         }
323         Email::Address->purge_cache if $purge;
324
325         my $subj = $mime->header('Subject') || '';
326         $subj = "Re: $subj" unless $subj =~ /\bRe:/;
327         my $irp = uri_escape_utf8(
328                         $mime->header_obj->header_raw('Message-ID') || '');
329         delete $cc{$to};
330         $to = uri_escape_utf8($to);
331         $subj = uri_escape_utf8($subj);
332
333         my $cc = uri_escape_utf8(join(',', values %cc));
334         my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
335
336         "<a\nhref=\"" . ascii_html($href) . '">reply</a>';
337 }
338
339 sub linkify_refs {
340         join(' ', map {
341                 my $v = PublicInbox::Hval->new_msgid($_);
342                 my $html = $v->as_html;
343                 my $href = $v->as_href;
344                 "&lt;<a href=\"$href.html\">$html</a>&gt;";
345         } @_);
346 }
347
348 require Digest::SHA;
349 sub anchor_for {
350         my ($msgid) = @_;
351         $msgid =~ s/\A\s*<?//;
352         $msgid =~ s/>?\s*\z//;
353         Digest::SHA::sha1_hex($msgid);
354 }
355
356 1;