package PublicInbox::View;
use strict;
use warnings;
-use URI::Escape qw/uri_escape/;
-use CGI qw/escapeHTML/;
+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/;
+
+# TODO: make these constants tunable
use constant MAX_INLINE_QUOTED => 5;
+use constant MAX_TRUNC_LEN => 72;
+
+*ascii_html = *PublicInbox::Hval::ascii_html;
-my $enc_utf8 = find_encoding('utf8');
-my $enc_ascii = find_encoding('us-ascii');
+my $enc_utf8 = find_encoding('UTF-8');
my $enc_mime = find_encoding('MIME-Header');
# public functions:
-sub as_html {
+sub msg_html {
my ($class, $mime, $full_pfx) = @_;
- headers_to_html_header($mime) .
+ headers_to_html_header($mime, $full_pfx) .
multipart_text_as_html($mime, $full_pfx) .
+ '</pre><hr /><pre>' .
+ html_footer($mime) .
'</pre></body></html>';
}
-sub as_feed_entry {
+sub feed_entry {
my ($class, $mime, $full_pfx) = @_;
- "<pre>" . multipart_text_as_html($mime, $full_pfx) . "</pre>";
+ '<pre>' . multipart_text_as_html($mime, $full_pfx) . '</pre>';
}
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:
$s =~ s!^((?:(?:>[^\n]*)\n)+)!
my $cur = $1;
my @lines = split(/\n/, $cur);
if (@lines > MAX_INLINE_QUOTED) {
# show a short snippet of quoted text
$cur = join(' ', @lines);
- $cur =~ s/> ?//g;
+ $cur =~ s/^>\s*//;
my @sum = split(/\s+/, $cur);
$cur = '';
do {
- $cur .= shift(@sum) . ' ';
- } while (@sum && length($cur) < 64);
- $cur=~ s/ \z/ .../;
+ my $tmp = shift(@sum);
+ my $len = length($tmp) + length($cur);
+ if ($len > MAX_TRUNC_LEN) {
+ @sum = ();
+ } else {
+ $cur .= $tmp . ' ';
+ }
+ } while (@sum && length($cur) < MAX_TRUNC_LEN);
+ $cur =~ s/ \z/ .../;
"> <<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
"\">$cur<\/a>>\n";
} else {
$s;
}
-sub trim_message_id {
- my ($mid) = @_;
- $mid = $enc_mime->decode($mid);
- $mid =~ s/\A\s*<//;
- $mid =~ s/>\s*\z//;
- my $html = ascii_html($mid);
- my $href = ascii_html(uri_escape($mid));
-
- ($html, $href);
-}
-
-sub ascii_html {
- $enc_ascii->encode(escapeHTML($_[0]), Encode::HTMLCREF);
-}
-
sub headers_to_html_header {
- my ($simple) = @_;
+ my ($mime, $full_pfx) = @_;
my $rv = "";
my @title;
foreach my $h (qw(From To Cc Subject Date)) {
- my $v = $simple->header($h);
- defined $v or next;
- $v =~ tr/\n/ /s;
- $v =~ tr/\r//d;
- my $raw = $enc_mime->decode($v);
- $v = ascii_html($raw);
- $rv .= "$h: $v\n";
+ my $v = $mime->header($h);
+ defined($v) && length($v) or next;
+ $v = PublicInbox::Hval->new_oneline($v);
+ $rv .= "$h: " . $v->as_html . "\n";
if ($h eq 'From') {
- my @from = Email::Address->parse($raw);
- $raw = $from[0]->name;
- unless (defined($raw) && length($raw)) {
- $raw = '<' . $from[0]->address . '>';
+ my @from = Email::Address->parse($v->raw);
+ $v = $from[0]->name;
+ unless (defined($v) && length($v)) {
+ $v = '<' . $from[0]->address . '>';
}
- $title[1] = ascii_html($raw);
-
+ $title[1] = ascii_html($v);
} elsif ($h eq 'Subject') {
- $title[0] = $v;
+ $title[0] = $v->as_html;
}
}
- my $mid = $simple->header('Message-ID');
+ my $header_obj = $mime->header_obj;
+ my $mid = $header_obj->header_raw('Message-ID');
if (defined $mid) {
- my ($html, $href) = trim_message_id($mid);
- $rv .= "Message-ID: <<a href=\"$href.html\">$html</a>> ";
+ $mid = PublicInbox::Hval->new_msgid($mid);
+ $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";
}
- my $irp = $simple->header('In-Reply-To');
+ my $irp = $header_obj->header_raw('In-Reply-To');
if (defined $irp) {
- my ($html, $href) = trim_message_id($irp);
+ my $v = PublicInbox::Hval->new_msgid(my $tmp = $irp);
+ my $html = $v->as_html;
+ my $href = $v->as_href;
$rv .= "In-Reply-To: <";
$rv .= "<a href=\"$href.html\">$html</a>>\n";
}
+
+ my $refs = $header_obj->header_raw('References');
+ if ($refs) {
+ $refs =~ s/\s*\Q$irp\E\s*// if (defined $irp);
+ my @refs = ($refs =~ /<([^>]+)>/g);
+ if (@refs) {
+ $rv .= 'References: '. linkify_refs(@refs) . "\n";
+ }
+ }
+
$rv .= "\n";
("<html><head><title>". join(' - ', @title) .
'</title></head><body><pre style="white-space:pre-wrap">' . $rv);
}
+sub html_footer {
+ my ($mime) = @_;
+ 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) && length($v) 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;
+
+ my $subj = $mime->header('Subject') || '';
+ $subj = "Re: $subj" unless $subj =~ /\bRe:/;
+ my $irp = uri_escape_utf8(
+ $mime->header_obj->header_raw('Message-ID') || '');
+ delete $cc{$to};
+ $to = uri_escape_utf8($to);
+ $subj = uri_escape_utf8($subj);
+
+ my $cc = uri_escape_utf8(join(',', values %cc));
+ my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
+
+ '<a href="' . ascii_html($href) . '">reply</a>';
+}
+
+sub linkify_refs {
+ join(' ', map {
+ my $v = PublicInbox::Hval->new_msgid($_);
+ my $html = $v->as_html;
+ my $href = $v->as_href;
+ "<<a href=\"$href.html\">$html</a>>";
+ } @_);
+}
+
1;