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