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