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