]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/View.pm
extract redundant Message-ID handling code
[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 URI::Escape qw/uri_escape_utf8/;
7 use Encode qw/find_encoding/;
8 use Encode::MIME::Header;
9 use Email::MIME::ContentType qw/parse_content_type/;
10 use PublicInbox::Hval;
11 use PublicInbox::MID qw/mid_clean mid_compressed/;
12 require POSIX;
13
14 # TODO: make these constants tunable
15 use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal
16 use constant MAX_TRUNC_LEN => 72;
17 use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
18
19 *ascii_html = *PublicInbox::Hval::ascii_html;
20
21 my $enc_utf8 = find_encoding('UTF-8');
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);
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, $default) = @_;
154         $default ||= $enc_utf8;
155         defined $ct or return $default;
156         my $ct_parsed = parse_content_type($ct);
157         if ($ct_parsed) {
158                 if (my $charset = $ct_parsed->{attributes}->{charset}) {
159                         my $enc = find_encoding($charset);
160                         return $enc if $enc;
161                 }
162         }
163         $default;
164 }
165
166 sub multipart_text_as_html {
167         my ($mime, $full_pfx) = @_;
168         my $rv = "";
169         my $part_nr = 0;
170         my $enc_msg = enc_for($mime->header("Content-Type"));
171
172         # scan through all parts, looking for displayable text
173         $mime->walk_parts(sub {
174                 my ($part) = @_;
175                 return if $part->subparts; # walk_parts already recurses
176                 my $ct = $part->content_type;
177
178                 # account for filter bugs...
179                 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
180
181                 my $enc = enc_for($ct, $enc_msg);
182
183                 if ($part_nr > 0) {
184                         my $fn = $part->filename;
185                         defined($fn) or $fn = "part #" . ($part_nr + 1);
186                         $rv .= add_filename_line($enc->decode($fn));
187                 }
188
189                 if (defined $full_pfx) {
190                         $rv .= add_text_body_short($enc, $part, $part_nr,
191                                                 $full_pfx);
192                 } else {
193                         $rv .= add_text_body_full($enc, $part, $part_nr);
194                 }
195                 $rv .= "\n" unless $rv =~ /\n\z/s;
196                 ++$part_nr;
197         });
198         $rv;
199 }
200
201 sub add_filename_line {
202         my ($fn) = @_;
203         my $len = 72;
204         my $pad = "-";
205
206         $len -= length($fn);
207         $pad x= ($len/2) if ($len > 0);
208         "$pad " . ascii_html($fn) . " $pad\n";
209 }
210
211 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
212
213 sub linkify {
214         # no newlines added here since it'd break the splitting we do
215         # to fold quotes
216         $_[0] =~ s!$LINK_RE!<a href="$1">$1</a>!g;
217 }
218
219 sub add_text_body_short {
220         my ($enc, $part, $part_nr, $full_pfx) = @_;
221         my $n = 0;
222         my $s = ascii_html($enc->decode($part->body));
223         linkify($s);
224         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
225                 my $cur = $1;
226                 my @lines = split(/\n/, $cur);
227                 if (@lines > MAX_INLINE_QUOTED) {
228                         # show a short snippet of quoted text
229                         $cur = join(' ', @lines);
230                         $cur =~ s/^&gt;\s*//;
231
232                         my @sum = split(/\s+/, $cur);
233                         $cur = '';
234                         do {
235                                 my $tmp = shift(@sum);
236                                 my $len = length($tmp) + length($cur);
237                                 if ($len > MAX_TRUNC_LEN) {
238                                         @sum = ();
239                                 } else {
240                                         $cur .= $tmp . ' ';
241                                 }
242                         } while (@sum && length($cur) < MAX_TRUNC_LEN);
243                         $cur =~ s/ \z/ .../;
244                         "&gt; &lt;<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
245                                 "\">$cur<\/a>&gt;\n";
246                 } else {
247                         $cur;
248                 }
249         !emg;
250         $s;
251 }
252
253 sub add_text_body_full {
254         my ($enc, $part, $part_nr) = @_;
255         my $n = 0;
256         my $s = ascii_html($enc->decode($part->body));
257         linkify($s);
258         $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
259                 my $cur = $1;
260                 my @lines = split(/\n/, $cur);
261                 if (@lines > MAX_INLINE_QUOTED) {
262                         "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
263                 } else {
264                         $cur;
265                 }
266         !emg;
267         $s;
268 }
269
270 sub headers_to_html_header {
271         my ($mime, $full_pfx) = @_;
272
273         my $rv = "";
274         my @title;
275         foreach my $h (qw(From To Cc Subject Date)) {
276                 my $v = $mime->header($h);
277                 defined($v) && length($v) or next;
278                 $v = PublicInbox::Hval->new_oneline($v);
279                 $rv .= "$h: " . $v->as_html . "\n";
280
281                 if ($h eq 'From') {
282                         my @from = Email::Address->parse($v->raw);
283                         $v = $from[0]->name;
284                         unless (defined($v) && length($v)) {
285                                 $v = '<' . $from[0]->address . '>';
286                         }
287                         $title[1] = ascii_html($v);
288                 } elsif ($h eq 'Subject') {
289                         $title[0] = $v->as_html;
290                 }
291         }
292
293         my $header_obj = $mime->header_obj;
294         my $mid = $header_obj->header_raw('Message-ID');
295         if (defined $mid) {
296                 $mid = PublicInbox::Hval->new_msgid($mid);
297                 $rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
298                 my $href = $mid->as_href;
299                 $href = "../m/$href" unless $full_pfx;
300                 $rv .= "(<a\nhref=\"$href.txt\">raw</a>)\n";
301         }
302
303         my $irp = $header_obj->header_raw('In-Reply-To');
304         if (defined $irp) {
305                 my $v = PublicInbox::Hval->new_msgid($irp);
306                 my $html = $v->as_html;
307                 my $href = $v->as_href;
308                 $rv .= "In-Reply-To: &lt;";
309                 $rv .= "<a\nhref=\"$href.html\">$html</a>&gt;\n";
310         }
311
312         my $refs = $header_obj->header_raw('References');
313         if ($refs) {
314                 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
315                 my @refs = ($refs =~ /<([^>]+)>/g);
316                 if (@refs) {
317                         $rv .= 'References: '. linkify_refs(@refs) . "\n";
318                 }
319         }
320
321         $rv .= "\n";
322
323         ("<html><head><title>".  join(' - ', @title) .
324          '</title></head><body>' . PRE_WRAP . $rv);
325 }
326
327 sub html_footer {
328         my ($mime, $standalone) = @_;
329         my %cc; # everyone else
330         my $to; # this is the From address
331
332         foreach my $h (qw(From To Cc)) {
333                 my $v = $mime->header($h);
334                 defined($v) && length($v) or next;
335                 my @addrs = Email::Address->parse($v);
336                 foreach my $recip (@addrs) {
337                         my $address = $recip->address;
338                         my $dst = lc($address);
339                         $cc{$dst} ||= $address;
340                         $to ||= $dst;
341                 }
342         }
343         Email::Address->purge_cache if $standalone;
344
345         my $subj = $mime->header('Subject') || '';
346         $subj = "Re: $subj" unless $subj =~ /\bRe:/;
347         my $irp = uri_escape_utf8(
348                         $mime->header_obj->header_raw('Message-ID') || '');
349         delete $cc{$to};
350         $to = uri_escape_utf8($to);
351         $subj = uri_escape_utf8($subj);
352
353         my $cc = uri_escape_utf8(join(',', sort values %cc));
354         my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
355
356         my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
357
358         "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
359 }
360
361 sub linkify_refs {
362         join(' ', map {
363                 my $v = PublicInbox::Hval->new_msgid($_);
364                 my $html = $v->as_html;
365                 my $href = $v->as_href;
366                 "&lt;<a\nhref=\"$href.html\">$html</a>&gt;";
367         } @_);
368 }
369
370 sub anchor_for {
371         my ($msgid) = @_;
372         'm' . mid_compressed(mid_clean($msgid));
373 }
374
375 1;