package PublicInbox::View;
use strict;
use warnings;
-use PublicInbox::Hval;
use URI::Escape qw/uri_escape_utf8/;
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_compressed/;
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 => '<pre style="white-space:pre-wrap">';
+use constant PRE_WRAP => "<pre\nstyle=\"white-space:pre-wrap\">";
*ascii_html = *PublicInbox::Hval::ascii_html;
my $enc_utf8 = find_encoding('UTF-8');
-my $enc_mime = find_encoding('MIME-Header');
# public functions:
sub msg_html {
# this is already inside a <pre>
sub index_entry {
- my ($class, $mime, $now, $level, $seen) = @_;
- my $rv = "";
+ my ($class, $mime, $level, $state) = @_;
+ my ($now, $seen, $first) = @$state;
+ my $midx = $state->[3]++;
+ my ($prev, $next) = ($midx - 1, $midx + 1);
+ my $rv = '';
my $part_nr = 0;
my $enc_msg = enc_for($mime->header("Content-Type"));
my $subj = $mime->header('Subject');
my $header_obj = $mime->header_obj;
my $mid_raw = $header_obj->header_raw('Message-ID');
- my $name = anchor_for($mid_raw);
- $seen->{$name} = "#$name"; # save the anchor for later
+ my $id = anchor_for($mid_raw);
+ $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 $pfx = (' ' x $level);
my $ts = $mime->header('X-PI-Date');
- my $fmt = '%H:%M';
- if ($now > ($ts + (365 * 24 * 60 * 60))) {
- # doesn't have to be exactly 1 year
- $fmt = '%Y/%m/%d';
- } elsif ($now > ($ts + (24 * 60 * 60))) {
- $fmt = '%m/%d';
- }
+ my $fmt = '%Y-%m-%d %H:%M UTC';
$ts = POSIX::strftime($fmt, gmtime($ts));
- $rv .= "$pfx<a name=\"$name\"><b>$subj</b> $from - $ts</a>\n\n";
+ $rv .= "$pfx<b\nid=\"$id\">$subj</b>\n$pfx";
+ $rv .= "- by $from @ $ts - ";
+ $rv .= "<a\nid=\"s$midx\"\nhref=\"#s$next\">next</a>";
+ if ($prev >= 0) {
+ $rv .= "/<a\nhref=\"#s$prev\">prev</a>";
+ }
+ $rv .= "\n\n";
+ my $irp = $header_obj->header_raw('In-Reply-To');
+ my ($anchor_idx, $anchor);
+ if (defined $irp) {
+ $anchor_idx = anchor_for($irp);
+ $anchor = $seen->{$anchor_idx};
+ }
+ my $href = $mid->as_href;
+ my $mhref = "m/$href.html";
+ my $fhref = "f/$href.html";
+ my $more = 'message';
# scan through all parts, looking for displayable text
$mime->walk_parts(sub {
my ($part) = @_;
return if $part->subparts; # walk_parts already recurses
- my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
- my $more = '';
+ my $ct = $part->content_type;
+
+ # account for filter bugs...
+ return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
+
+ my $enc = enc_for($ct, $enc_msg);
if ($part_nr > 0) {
my $fn = $part->filename;
$rv .= $pfx . add_filename_line($enc->decode($fn));
}
- my $s = ascii_html($enc->decode($part->body));
-
- # drop quotes, including the "so-and-so wrote:" line
- $s =~ s/(?:^[^\n]*:\s*\n)?(?:^>[^\n]*\n)+(?:^\s*\n)?//mg;
-
- # Drop signatures
- $s =~ s/\n*-- \n.*\z//s;
+ my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
# drop the remainder of git patches, they're usually better
# to review when the full message is viewed
- if ($s =~ s/\n*---\n.*\z//s) {
- $more = "$pfx...\n";
- }
+ $s =~ s!^---+\n.*\z!!ms and $more = 'more...';
+
+ # Drop signatures
+ $s =~ s/^-- \n.*\z//ms and $more = 'more...';
- # kill any trailing whitespace
+ # kill any leading or trailing whitespace
+ $s =~ s/\A\s+//s;
$s =~ s/\s+\z//s;
- # add prefix:
- $s =~ s/^/$pfx/sgm;
+ if (length $s) {
+ # add prefix:
+ $s =~ s/^/$pfx/sgm;
- $rv .= $s . "\n$more";
+ $rv .= $s . "\n";
+ }
++$part_nr;
});
- my $href = 'm/' . $mid->as_href . '.html';
- $rv .= "$pfx<a\nhref=\"$href\">more</a> ";
- my $txt = 'm/' . $mid->as_href . '.txt';
+ $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
+ my $txt = "m/$href.txt";
$rv .= "<a\nhref=\"$txt\">raw</a> ";
$rv .= html_footer($mime, 0);
- my $irp = $header_obj->header_raw('In-Reply-To');
if (defined $irp) {
- my $anchor_idx = anchor_for($irp);
- my $anchor = $seen->{$anchor_idx};
unless (defined $anchor) {
my $v = PublicInbox::Hval->new_msgid($irp);
my $html = $v->as_html;
}
$rv .= " <a\nhref=\"$anchor\">parent</a>";
}
+ $rv .= " <a\nhref=\"?r=$first#$id\">threadlink</a>";
$rv . "\n\n";
}
# only private functions below.
sub enc_for {
- my ($ct) = @_;
- defined $ct or return $enc_utf8;
+ 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}) {
return $enc if $enc;
}
}
- $enc_utf8;
+ $default;
}
sub multipart_text_as_html {
$mime->walk_parts(sub {
my ($part) = @_;
return if $part->subparts; # walk_parts already recurses
- my $enc = enc_for($part->content_type) || $enc_msg || $enc_utf8;
+ my $ct = $part->content_type;
+
+ # account for filter bugs...
+ return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
+
+ my $enc = enc_for($ct, $enc_msg);
if ($part_nr > 0) {
my $fn = $part->filename;
"$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!<a href="$1">$1</a>!g;
+}
+
sub add_text_body_short {
my ($enc, $part, $part_nr, $full_pfx) = @_;
my $n = 0;
my $s = ascii_html($enc->decode($part->body));
- # TODO: fold the "so-and-so wrote:" attribute line here, too:
+ linkify($s);
$s =~ s!^((?:(?:>[^\n]*)\n)+)!
my $cur = $1;
my @lines = split(/\n/, $cur);
}
} while (@sum && length($cur) < MAX_TRUNC_LEN);
$cur =~ s/ \z/ .../;
- "> <<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
+ "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
"\">$cur<\/a>>\n";
} else {
$cur;
my ($enc, $part, $part_nr) = @_;
my $n = 0;
my $s = ascii_html($enc->decode($part->body));
+ linkify($s);
$s =~ s!^((?:(?:>[^\n]*)\n)+)!
my $cur = $1;
my @lines = split(/\n/, $cur);
if (@lines > MAX_INLINE_QUOTED) {
- "<a name=q${part_nr}_" . $n++ . ">$cur</a>";
+ "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
} else {
$cur;
}
$rv .= 'Message-ID: <' . $mid->as_html . '> ';
my $href = $mid->as_href;
$href = "../m/$href" unless $full_pfx;
- $rv .= "(<a href=\"$href.txt\">original</a>)\n";
+ $rv .= "(<a\nhref=\"$href.txt\">raw</a>)\n";
}
my $irp = $header_obj->header_raw('In-Reply-To');
if (defined $irp) {
- my $v = PublicInbox::Hval->new_msgid(my $tmp = $irp);
+ my $v = PublicInbox::Hval->new_msgid($irp);
my $html = $v->as_html;
my $href = $v->as_href;
$rv .= "In-Reply-To: <";
- $rv .= "<a href=\"$href.html\">$html</a>>\n";
+ $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
}
my $refs = $header_obj->header_raw('References');
}
sub html_footer {
- my ($mime, $purge) = @_;
+ my ($mime, $standalone) = @_;
my %cc; # everyone else
my $to; # this is the From address
$to ||= $dst;
}
}
- Email::Address->purge_cache if $purge;
+ Email::Address->purge_cache if $standalone;
my $subj = $mime->header('Subject') || '';
$subj = "Re: $subj" unless $subj =~ /\bRe:/;
$to = uri_escape_utf8($to);
$subj = uri_escape_utf8($subj);
- my $cc = uri_escape_utf8(join(',', values %cc));
+ my $cc = uri_escape_utf8(join(',', sort values %cc));
my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
- "<a\nhref=\"" . ascii_html($href) . '">reply</a>';
+ my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
+
+ "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
}
sub linkify_refs {
my $v = PublicInbox::Hval->new_msgid($_);
my $html = $v->as_html;
my $href = $v->as_href;
- "<<a href=\"$href.html\">$html</a>>";
+ "<<a\nhref=\"$href.html\">$html</a>>";
} @_);
}
-require Digest::SHA;
sub anchor_for {
my ($msgid) = @_;
- $msgid =~ s/\A\s*<?//;
- $msgid =~ s/>?\s*\z//;
- Digest::SHA::sha1_hex($msgid);
+ 'm' . mid_compressed(mid_clean($msgid));
}
1;