]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/View.pm
extract redundant Message-ID handling code
[public-inbox.git] / lib / PublicInbox / View.pm
index 5076cf923baa671f6a6e1b8375f6e6ca395e6e6c..c2dbb7ed42fd9d1048fd3491e6c2fbf705e4447f 100644 (file)
@@ -3,22 +3,22 @@
 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 {
@@ -43,16 +43,19 @@ sub feed_entry {
 
 # 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;
@@ -65,16 +68,16 @@ sub index_entry {
        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);
@@ -85,12 +88,17 @@ sub index_entry {
        my $href = $mid->as_href;
        my $mhref = "m/$href.html";
        my $fhref = "f/$href.html";
-       my $more = 'link';
+       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 $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;
@@ -100,12 +108,12 @@ sub index_entry {
 
                my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
 
-               # keep signatures for now?  They shold usually be short,
-               # and sometimes footnotes/"P.S." appear there.
-
                # 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...';
+               $s =~ s!^---+\n.*\z!!ms and $more = 'more...';
+
+               # Drop signatures
+               $s =~ s/^-- \n.*\z//ms and $more = 'more...';
 
                # kill any leading or trailing whitespace
                $s =~ s/\A\s+//s;
@@ -115,12 +123,12 @@ sub index_entry {
                        # add prefix:
                        $s =~ s/^/$pfx/sgm;
 
-                       $rv .= $s;
+                       $rv .= $s . "\n";
                }
                ++$part_nr;
        });
 
-       $rv .= "$pfx<a\nhref=\"$mhref\">$more</a> ";
+       $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
        my $txt = "m/$href.txt";
        $rv .= "<a\nhref=\"$txt\">raw</a> ";
        $rv .= html_footer($mime, 0);
@@ -134,6 +142,7 @@ sub index_entry {
                }
                $rv .= " <a\nhref=\"$anchor\">parent</a>";
        }
+       $rv .= " <a\nhref=\"?r=$first#$id\">threadlink</a>";
 
        $rv . "\n\n";
 }
@@ -141,8 +150,9 @@ sub index_entry {
 # 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}) {
@@ -150,7 +160,7 @@ sub enc_for {
                        return $enc if $enc;
                }
        }
-       $enc_utf8;
+       $default;
 }
 
 sub multipart_text_as_html {
@@ -163,7 +173,12 @@ 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;
@@ -193,10 +208,19 @@ sub add_filename_line {
        "$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));
+       linkify($s);
        $s =~ s!^((?:(?:&gt;[^\n]*)\n)+)!
                my $cur = $1;
                my @lines = split(/\n/, $cur);
@@ -217,7 +241,7 @@ sub add_text_body_short {
                                }
                        } while (@sum && length($cur) < MAX_TRUNC_LEN);
                        $cur =~ s/ \z/ .../;
-                       "&gt; &lt;<a href=\"${full_pfx}#q${part_nr}_" . $n++ .
+                       "&gt; &lt;<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
                                "\">$cur<\/a>&gt;\n";
                } else {
                        $cur;
@@ -230,11 +254,12 @@ sub add_text_body_full {
        my ($enc, $part, $part_nr) = @_;
        my $n = 0;
        my $s = ascii_html($enc->decode($part->body));
+       linkify($s);
        $s =~ s!^((?:(?:&gt;[^\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;
                }
@@ -272,16 +297,16 @@ sub headers_to_html_header {
                $rv .= 'Message-ID: &lt;' . $mid->as_html . '&gt; ';
                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: &lt;";
-               $rv .= "<a href=\"$href.html\">$html</a>&gt;\n";
+               $rv .= "<a\nhref=\"$href.html\">$html</a>&gt;\n";
        }
 
        my $refs = $header_obj->header_raw('References');
@@ -300,7 +325,7 @@ sub headers_to_html_header {
 }
 
 sub html_footer {
-       my ($mime, $purge) = @_;
+       my ($mime, $standalone) = @_;
        my %cc; # everyone else
        my $to; # this is the From address
 
@@ -315,7 +340,7 @@ sub html_footer {
                        $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:/;
@@ -325,10 +350,12 @@ sub html_footer {
        $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 {
@@ -336,16 +363,13 @@ sub linkify_refs {
                my $v = PublicInbox::Hval->new_msgid($_);
                my $html = $v->as_html;
                my $href = $v->as_href;
-               "&lt;<a href=\"$href.html\">$html</a>&gt;";
+               "&lt;<a\nhref=\"$href.html\">$html</a>&gt;";
        } @_);
 }
 
-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;