# Copyright (C) 2014, Eric Wong and all contributors # License: AGPLv3 or later (https://www.gnu.org/licenses/agpl-3.0.txt) package PublicInbox::View; use strict; use warnings; use URI::Escape qw/uri_escape_utf8/; use Date::Parse qw/str2time/; use Encode qw/find_encoding/; use Encode::MIME::Header; use Email::MIME::ContentType qw/parse_content_type/; use PublicInbox::Hval; use PublicInbox::MID qw/mid_clean mid_compress mid2path/; use Digest::SHA; require POSIX; # TODO: make these constants tunable use constant MAX_INLINE_QUOTED => 12; # half an 80x24 terminal use constant MAX_TRUNC_LEN => 72; use constant PRE_WRAP => ""; use constant T_ANCHOR => '#u'; *ascii_html = *PublicInbox::Hval::ascii_html; my $enc_utf8 = find_encoding('UTF-8'); # public functions: sub msg_html { my ($ctx, $mime, $full_pfx, $footer) = @_; if (defined $footer) { $footer = "\n" . $footer; } else { $footer = ''; } my $srch = $ctx->{srch} if $ctx; headers_to_html_header($mime, $full_pfx, $srch) . multipart_text_as_html($mime, $full_pfx) . '
' .
		html_footer($mime, 1, $full_pfx, $ctx) .
		$footer .
		'
'; } sub feed_entry { my ($class, $mime, $full_pfx) = @_; PRE_WRAP . multipart_text_as_html($mime, $full_pfx) . ''; } # this is already inside a
sub index_entry {
	my ($fh, $mime, $level, $state) = @_;
	my $midx = $state->{anchor_idx}++;
	my $ctx = $state->{ctx};
	my $srch = $ctx->{srch};
	my ($prev, $next) = ($midx - 1, $midx + 1);
	my $part_nr = 0;
	my $enc = enc_for($mime->header("Content-Type"));
	my $subj = $mime->header('Subject');
	my $header_obj = $mime->header_obj;

	my $mid_raw = $header_obj->header('Message-ID');
	my $id = anchor_for($mid_raw);
	my $seen = $state->{seen};
	$seen->{$id} = "#$id"; # save the anchor for later

	my $mid = PublicInbox::Hval->new_msgid($mid_raw);
	my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
	my @from = Email::Address->parse($from);
	$from = $from[0]->name;

	$from = PublicInbox::Hval->new_oneline($from)->as_html;
	$subj = PublicInbox::Hval->new_oneline($subj)->as_html;
	my $more = 'permalink';
	my $root_anchor = $state->{root_anchor};
	my $path = $root_anchor ? '../../' : '';
	my $href = $mid->as_href;
	my $irt = $header_obj->header('In-Reply-To');
	my ($anchor_idx, $anchor, $t_anchor);
	if (defined $irt) {
		$anchor_idx = anchor_for($irt);
		$anchor = $seen->{$anchor_idx};
		$t_anchor = T_ANCHOR;
	} else {
		$t_anchor = '';
	}
	if ($srch) {
		$subj = "$subj";
	}
	if ($root_anchor && $root_anchor eq $id) {
		$subj = "$subj";
	}

	my $ts = $mime->header('X-PI-TS');
	unless (defined $ts) {
		$ts = msg_timestamp($mime);
	}
	$ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));

	my $rv = "";
	if ($level) {
		$rv .= '
' . ('  ' x $level) . '
'; } $rv .= "" . PRE_WRAP; $rv .= "$subj\n"; $rv .= "- by $from @ $ts UTC - "; $rv .= "next"; if ($prev >= 0) { $rv .= "/prev"; } $fh->write($rv .= "\n\n"); my ($fhref, $more_ref); my $mhref = "${path}m/$href/"; if ($level > 0) { $fhref = "${path}f/$href/"; $more_ref = \$more; } # scan through all parts, looking for displayable text $mime->walk_parts(sub { index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref); }); $mime->body_set(''); my $txt = "${path}m/$href/raw"; $rv = "\n$more raw "; $rv .= html_footer($mime, 0, undef, $ctx); if (defined $irt) { unless (defined $anchor) { my $v = PublicInbox::Hval->new_msgid($irt); $v = $v->as_href; $anchor = "${path}m/$v/"; $seen->{$anchor_idx} = $anchor; } $rv .= " parent"; } if ($srch) { $rv .= " " . "threadlink"; } $fh->write($rv .= '
'); } sub thread_html { my ($ctx, $foot, $srch) = @_; sub { emit_thread_html($_[0], $ctx, $foot, $srch) } } # only private functions below. sub emit_thread_html { my ($cb, $ctx, $foot, $srch) = @_; my $mid = mid_compress($ctx->{mid}); my $res = $srch->get_thread($mid); my $msgs = load_results($res); my $nr = scalar @$msgs; return missing_thread($cb) if $nr == 0; my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]); my $th = thread_results($msgs); my $state = { ctx => $ctx, seen => {}, root_anchor => anchor_for($mid), anchor_idx => 0, }; { require PublicInbox::GitCatFile; my $git = PublicInbox::GitCatFile->new($ctx->{git_dir}); thread_entry($fh, $git, $state, $_, 0) for $th->rootset; } my $final_anchor = $state->{anchor_idx}; my $next = ""; $next .= $final_anchor == 1 ? 'only message in' : 'end of'; $next .= " thread, back to index\n"; # $mid = PublicInbox::Hval->new_msgid($mid)->as_href; $next .= "download: mbox.gz\n\n"; $fh->write("
" . PRE_WRAP . $next . $foot . ""); $fh->close; } sub index_walk { my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_; my $s = add_text_body($enc, $part, $part_nr, $fhref); if ($more) { # drop the remainder of git patches, they're usually better # to review when the full message is viewed $s =~ s!^---+\n.*\z!!ms and $$more = 'more...'; # Drop signatures $s =~ s/^-- \n.*\z//ms and $$more = 'more...'; } # kill any leading or trailing whitespace lines $s =~ s/^\s*$//sgm; $s =~ s/\s+\z//s; if ($s ne '') { # kill per-line trailing whitespace $s =~ s/[ \t]+$//sgm; $s .= "\n" unless $s =~ /\n\z/s; } $fh->write($s); } sub enc_for { my ($ct, $default) = @_; $default ||= $enc_utf8; defined $ct or return $default; my $ct_parsed = parse_content_type($ct); if ($ct_parsed) { if (my $charset = $ct_parsed->{attributes}->{charset}) { my $enc = find_encoding($charset); return $enc if $enc; } } $default; } sub multipart_text_as_html { my ($mime, $full_pfx, $srch) = @_; my $rv = ""; my $part_nr = 0; my $enc = enc_for($mime->header("Content-Type")); # scan through all parts, looking for displayable text $mime->walk_parts(sub { my ($part) = @_; $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx); }); $mime->body_set(''); $rv; } sub add_filename_line { my ($enc, $fn) = @_; my $len = 72; my $pad = "-"; $fn = $enc->decode($fn); $len -= length($fn); $pad x= ($len/2) if ($len > 0); "$pad " . ascii_html($fn) . " $pad\n"; } my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!; sub linkify { # no newlines added here since it'd break the splitting we do # to fold quotes $_[0] =~ s!$LINK_RE!$1!g; } sub flush_quote { my ($quot, $n, $part_nr, $full_pfx, $final) = @_; if ($full_pfx) { if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) { # show quote inline my $rv = join('', map { linkify($_); $_ } @$quot); @$quot = (); return $rv; } # show a short snippet of quoted text and link to full version: @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot; my $cur = join(' ', @$quot); @$quot = split(/\s+/, $cur); $cur = ''; do { my $tmp = shift(@$quot); my $len = length($tmp) + length($cur); if ($len > MAX_TRUNC_LEN) { @$quot = (); } else { $cur .= $tmp . ' '; } } while (@$quot && length($cur) < MAX_TRUNC_LEN); @$quot = (); $cur =~ s/ \z/ .../s; my $nr = ++$$n; "> [$cur]\n"; } else { # show everything in the full version with anchor from # short version (see above) my $nr = ++$$n; my $rv = ""; $rv .= join('', map { linkify($_); $_ } @$quot); @$quot = (); $rv; } } sub add_text_body { my ($enc_msg, $part, $part_nr, $full_pfx) = @_; return '' if $part->subparts; my $ct = $part->content_type; # account for filter bugs... if (defined $ct && $ct =~ m!\btext/[xh]+tml\b!i) { $part->body_set(''); return ''; } my $enc = enc_for($ct, $enc_msg); my $n = 0; my $nr = 0; my $s = $part->body; $part->body_set(''); $s = $enc->decode($s); $s = ascii_html($s); my @lines = split(/^/m, $s); $s = ''; if ($$part_nr > 0) { my $fn = $part->filename; defined($fn) or $fn = "part #" . ($$part_nr + 1); $s .= add_filename_line($enc, $fn); } my @quot; while (defined(my $cur = shift @lines)) { if ($cur !~ /^>/) { # show the previously buffered quote inline if (scalar @quot) { $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 0); } # regular line, OK linkify($cur); $s .= $cur; } else { push @quot, $cur; } } $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1) if scalar @quot; $s .= "\n" unless $s =~ /\n\z/s; ++$$part_nr; $s; } sub headers_to_html_header { my ($mime, $full_pfx, $srch) = @_; my $rv = ""; my @title; my $header_obj = $mime->header_obj; my $mid = $header_obj->header('Message-ID'); $mid = PublicInbox::Hval->new_msgid($mid); my $mid_href = $mid->as_href; foreach my $h (qw(From To Cc Subject Date)) { my $v = $mime->header($h); defined($v) && ($v ne '') or next; $v = PublicInbox::Hval->new_oneline($v); if ($h eq 'From') { my @from = Email::Address->parse($v->raw); $title[1] = ascii_html($from[0]->name); } elsif ($h eq 'Subject') { $title[0] = $v->as_html; if ($srch) { $rv .= "$h: "; $rv .= $v->as_html . "\n"; next; } } $rv .= "$h: " . $v->as_html . "\n"; } $rv .= 'Message-ID: <' . $mid->as_html . '> '; my $raw_ref = $full_pfx ? 'raw' : "../../m/$mid_href/raw"; $rv .= "(raw)\n"; my $irt = $header_obj->header('In-Reply-To'); if (defined $irt) { my $v = PublicInbox::Hval->new_msgid($irt); my $html = $v->as_html; my $href = $v->as_href; $rv .= "In-Reply-To: <"; $rv .= "$html>\n"; } my $refs = $header_obj->header('References'); if ($refs) { # avoid redundant URLs wasting bandwidth my %seen; $seen{mid_clean($irt)} = 1 if defined $irt; my @refs; my @raw_refs = ($refs =~ /<([^>]+)>/g); foreach my $ref (@raw_refs) { next if $seen{$ref}; $seen{$ref} = 1; push @refs, linkify_ref($ref); } if (@refs) { $rv .= 'References: '. join(' ', @refs) . "\n"; } } $rv .= "\n"; ("". join(' - ', @title) . '' . PRE_WRAP . $rv); } sub html_footer { my ($mime, $standalone, $full_pfx, $ctx) = @_; my %cc; # everyone else my $to; # this is the From address foreach my $h (qw(From To Cc)) { my $v = $mime->header($h); defined($v) && ($v ne '') or next; my @addrs = Email::Address->parse($v); foreach my $recip (@addrs) { my $address = $recip->address; my $dst = lc($address); $cc{$dst} ||= $address; $to ||= $dst; } } Email::Address->purge_cache if $standalone; my $subj = $mime->header('Subject') || ''; $subj = "Re: $subj" unless $subj =~ /\bRe:/i; my $mid = $mime->header('Message-ID'); my $irt = uri_escape_utf8($mid); delete $cc{$to}; $to = uri_escape_utf8($to); $subj = uri_escape_utf8($subj); my $cc = uri_escape_utf8(join(',', sort values %cc)); my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj"; my $srch = $ctx->{srch} if $ctx; my $idx = $standalone ? " index" : ''; if ($idx && $srch) { $mid = mid_compress(mid_clean($mid)); my $t_anchor = defined $irt ? T_ANCHOR : ''; $irt = $mime->header('In-Reply-To'); $idx = " ". "threadlink$idx"; my $res = $srch->get_followups($mid); if (my $c = $res->{total}) { my $nr = scalar @{$res->{msgs}}; if ($nr < $c) { $c = "$nr of $c followups"; } else { $c = $c == 1 ? '1 followup' : "$c followups"; } $idx .= "\n$c:\n"; $res->{srch} = $srch; thread_followups(\$idx, $mime, $res); } else { $idx .= "\n(no followups, yet)\n"; } if (defined $irt) { $irt = PublicInbox::Hval->new_msgid($irt); $irt = $irt->as_href; $irt = "parent "; } else { $irt = ' ' x length('parent '); } } else { $irt = ''; } "$irtreply' . $idx; } sub linkify_ref { my $v = PublicInbox::Hval->new_msgid($_[0]); my $html = $v->as_html; my $href = $v->as_href; "<$html>"; } sub anchor_for { my ($msgid) = @_; my $id = $msgid; if ($id !~ /\A[a-f0-9]{40}\z/) { $id = mid_compress(mid_clean($id), 1); } 'm' . $id; } sub simple_dump { my ($dst, $root, $node, $level) = @_; return unless $node; # $root = [ undef, \%seen, $srch ]; if (my $x = $node->message) { my $f = $x->header('X-PI-From'); my $d = $x->header('X-PI-Date'); if (defined $f && defined $d) { my $mid = $x->header('Message-ID'); my $pfx = ' ' x $level; $$dst .= $pfx; # Subject is never undef, this mail was loaded from # our Xapian which would've resulted in '' if it were # really missing (and Filter rejects empty subjects) my $s = $x->header('Subject'); my $h = $root->[2]->subject_path($s); if ($root->[1]->{$h}) { $s = undef; } else { $root->[1]->{$h} = 1; $s = PublicInbox::Hval->new($s); $s = $s->as_html; } my $m = PublicInbox::Hval->new_msgid($mid); $f = PublicInbox::Hval->new($f); $d = PublicInbox::Hval->new($d); $m = '../' . $m->as_href . '/'; $f = $f->as_html; $d = $d->as_html . ' UTC'; if (defined $s) { $$dst .= "` $s\n" . "$pfx by $f @ $d\n"; } else { $$dst .= "` $f @ $d\n"; } } } simple_dump($dst, $root, $node->child, $level+1); simple_dump($dst, $root, $node->next, $level); } sub thread_followups { my ($dst, $root, $res) = @_; $root->header_set('X-PI-TS', '0'); my $msgs = load_results($res); push @$msgs, $root; my $th = thread_results($msgs); my $srch = $res->{srch}; my $subj = $srch->subject_path($root->header('Subject')); my %seen = ($subj => 1); $root = [ undef, \%seen, $srch ]; simple_dump($dst, $root, $_, 0) for $th->rootset; } sub thread_html_head { my ($mime) = @_; my $s = PublicInbox::Hval->new_oneline($mime->header('Subject')); $s = $s->as_html; "$s"; } sub thread_entry { my ($fh, $git, $state, $node, $level) = @_; return unless $node; if (my $mime = $node->message) { # lazy load the full message from mini_mime: my $path = mid2path(mid_clean($mime->header('Message-ID'))); $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) }; if ($mime) { if ($state->{anchor_idx} == 0) { $fh->write(thread_html_head($mime)); } index_entry($fh, $mime, $level, $state); } } thread_entry($fh, $git, $state, $node->child, $level + 1); thread_entry($fh, $git, $state, $node->next, $level); } sub load_results { my ($res) = @_; [ map { $_->mini_mime } @{delete $res->{msgs}} ]; } sub msg_timestamp { my ($mime) = @_; my $ts = eval { str2time($mime->header('Date')) }; defined($ts) ? $ts : 0; } sub thread_results { my ($msgs) = @_; require PublicInbox::Thread; my $th = PublicInbox::Thread->new(@$msgs); $th->thread; no warnings 'once'; $th->order(*PublicInbox::Thread::sort_ts); $th } sub missing_thread { my ($cb) = @_; my $title = 'Thread does not exist'; $cb->([404, ['Content-Type' => 'text/html']])->write(<$title
$title
Return to index
EOF } 1;