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;
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/;
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\">";
18 *ascii_html = *PublicInbox::Hval::ascii_html;
20 my $enc_utf8 = find_encoding('UTF-8');
24 my ($class, $mime, $full_pfx, $footer) = @_;
25 if (defined $footer) {
26 $footer = "\n" . $footer;
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>';
38 my ($class, $mime, $full_pfx) = @_;
40 PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . '</pre>';
43 # this is already inside a <pre>
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);
51 my $enc_msg = enc_for($mime->header("Content-Type"));
52 my $subj = $mime->header('Subject');
53 my $header_obj = $mime->header_obj;
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
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;
65 $from = PublicInbox::Hval->new_oneline($from)->as_html;
66 $subj = PublicInbox::Hval->new_oneline($subj)->as_html;
67 my $pfx = (' ' x $level);
69 my $ts = $mime->header('X-PI-Date');
70 my $fmt = '%Y-%m-%d %H:%M UTC';
71 $ts = POSIX::strftime($fmt, gmtime($ts));
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>";
77 $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
81 my $irp = $header_obj->header_raw('In-Reply-To');
82 my ($anchor_idx, $anchor);
84 $anchor_idx = anchor_for($irp);
85 $anchor = $seen->{$anchor_idx};
87 my $href = $mid->as_href;
88 my $mhref = "m/$href.html";
89 my $fhref = "f/$href.html";
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,
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);
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;
110 $rv .= " <a\nhref=\"$anchor\">parent</a>";
112 $rv .= " <a\nhref=\"?r=$first#$id\">threadlink</a>";
117 # only private functions below.
120 my ($part, $pfx, $enc_msg, $part_nr, $fhref, $more) = @_;
122 return $rv if $part->subparts; # walk_parts already recurses
123 my $ct = $part->content_type;
125 # account for filter bugs...
126 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
128 my $enc = enc_for($ct, $enc_msg);
131 my $fn = $part->filename;
132 defined($fn) or $fn = "part #" . ($part_nr + 1);
133 $rv .= $pfx . add_filename_line($enc->decode($fn));
136 my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
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...';
143 $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
145 # kill any leading or trailing whitespace
159 my ($ct, $default) = @_;
160 $default ||= $enc_utf8;
161 defined $ct or return $default;
162 my $ct_parsed = parse_content_type($ct);
164 if (my $charset = $ct_parsed->{attributes}->{charset}) {
165 my $enc = find_encoding($charset);
172 sub multipart_text_as_html {
173 my ($mime, $full_pfx) = @_;
176 my $enc_msg = enc_for($mime->header("Content-Type"));
178 # scan through all parts, looking for displayable text
179 $mime->walk_parts(sub {
181 return if $part->subparts; # walk_parts already recurses
182 my $ct = $part->content_type;
184 # account for filter bugs...
185 return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
187 my $enc = enc_for($ct, $enc_msg);
190 my $fn = $part->filename;
191 defined($fn) or $fn = "part #" . ($part_nr + 1);
192 $rv .= add_filename_line($enc->decode($fn));
195 if (defined $full_pfx) {
196 $rv .= add_text_body_short($enc, $part, $part_nr,
199 $rv .= add_text_body_full($enc, $part, $part_nr);
201 $rv .= "\n" unless $rv =~ /\n\z/s;
207 sub add_filename_line {
213 $pad x= ($len/2) if ($len > 0);
214 "$pad " . ascii_html($fn) . " $pad\n";
217 my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
220 # no newlines added here since it'd break the splitting we do
222 $_[0] =~ s!$LINK_RE!<a href="$1">$1</a>!g;
225 sub add_text_body_short {
226 my ($enc, $part, $part_nr, $full_pfx) = @_;
228 my $s = ascii_html($enc->decode($part->body));
230 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
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/^>\s*//;
238 my @sum = split(/\s+/, $cur);
241 my $tmp = shift(@sum);
242 my $len = length($tmp) + length($cur);
243 if ($len > MAX_TRUNC_LEN) {
248 } while (@sum && length($cur) < MAX_TRUNC_LEN);
250 "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
251 "\">$cur<\/a>>\n";
259 sub add_text_body_full {
260 my ($enc, $part, $part_nr) = @_;
262 my $s = ascii_html($enc->decode($part->body));
264 $s =~ s!^((?:(?:>[^\n]*)\n)+)!
266 my @lines = split(/\n/, $cur);
267 if (@lines > MAX_INLINE_QUOTED) {
268 "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
276 sub headers_to_html_header {
277 my ($mime, $full_pfx) = @_;
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";
288 my @from = Email::Address->parse($v->raw);
290 unless (defined($v) && length($v)) {
291 $v = '<' . $from[0]->address . '>';
293 $title[1] = ascii_html($v);
294 } elsif ($h eq 'Subject') {
295 $title[0] = $v->as_html;
299 my $header_obj = $mime->header_obj;
300 my $mid = $header_obj->header_raw('Message-ID');
302 $mid = PublicInbox::Hval->new_msgid($mid);
303 $rv .= 'Message-ID: <' . $mid->as_html . '> ';
304 my $href = $mid->as_href;
305 $href = "../m/$href" unless $full_pfx;
306 $rv .= "(<a\nhref=\"$href.txt\">raw</a>)\n";
309 my $irp = $header_obj->header_raw('In-Reply-To');
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: <";
315 $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
318 my $refs = $header_obj->header_raw('References');
320 $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
321 my @refs = ($refs =~ /<([^>]+)>/g);
323 $rv .= 'References: '. linkify_refs(@refs) . "\n";
329 ("<html><head><title>". join(' - ', @title) .
330 '</title></head><body>' . PRE_WRAP . $rv);
334 my ($mime, $standalone) = @_;
335 my %cc; # everyone else
336 my $to; # this is the From address
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;
349 Email::Address->purge_cache if $standalone;
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') || '');
356 $to = uri_escape_utf8($to);
357 $subj = uri_escape_utf8($subj);
359 my $cc = uri_escape_utf8(join(',', sort values %cc));
360 my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
362 my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
364 "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
369 my $v = PublicInbox::Hval->new_msgid($_);
370 my $html = $v->as_html;
371 my $href = $v->as_href;
372 "<<a\nhref=\"$href.html\">$html</a>>";
379 $msgid =~ s/\A\s*<?//;
380 $msgid =~ s/>?\s*\z//;
381 'm' . Digest::SHA::sha1_hex($msgid);