]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/View.pm
www: drop --subject from "git send-email" instructions
[public-inbox.git] / lib / PublicInbox / View.pm
index eee6ae33ea9f97a2f300f21afe18e3bd4f972f8e..fa96cca337d87f9113425a209c5b78aafa2b6dfa 100644 (file)
@@ -5,8 +5,7 @@
 # See Documentation/design_www.txt for this.
 package PublicInbox::View;
 use strict;
-use warnings;
-use bytes (); # only for bytes::length
+use v5.10.1;
 use List::Util qw(max);
 use PublicInbox::MsgTime qw(msg_datestamp);
 use PublicInbox::Hval qw(ascii_html obfuscate_addrs prurl mid_href
@@ -22,6 +21,7 @@ use PublicInbox::ViewDiff qw(flush_diff);
 use PublicInbox::Eml;
 use Time::Local qw(timegm);
 use PublicInbox::Smsg qw(subject_normalized);
+use PublicInbox::ContentHash qw(content_hash);
 use constant COLS => 72;
 use constant INDENT => '  ';
 use constant TCHILD => '` ';
@@ -31,13 +31,17 @@ sub msg_page_i {
        my ($ctx, $eml) = @_;
        if ($eml) { # called by WwwStream::async_eml or getline
                my $smsg = $ctx->{smsg};
-               $ctx->{smsg} = $ctx->{over}->next_by_mid(@{$ctx->{next_arg}});
+               my $over = $ctx->{ibx}->over;
+               $ctx->{smsg} = $over ? $over->next_by_mid(@{$ctx->{next_arg}})
+                               : $ctx->gone('over');
                $ctx->{mhref} = ($ctx->{nr} || $ctx->{smsg}) ?
                                "../${\mid_href($smsg->{mid})}/" : '';
                my $obuf = $ctx->{obuf} = _msg_page_prepare_obuf($eml, $ctx);
-               multipart_text_as_html($eml, $ctx);
+               if (length($$obuf)) {
+                       multipart_text_as_html($eml, $ctx);
+                       $$obuf .= '</pre><hr>';
+               }
                delete $ctx->{obuf};
-               $$obuf .= '</pre><hr>';
                $$obuf .= html_footer($ctx, $ctx->{first_hdr}) if !$ctx->{smsg};
                $$obuf;
        } else { # called by WwwStream::async_next or getline
@@ -53,9 +57,11 @@ sub no_over_html ($) {
        $ctx->{mhref} = '';
        PublicInbox::WwwStream::init($ctx);
        my $obuf = $ctx->{obuf} = _msg_page_prepare_obuf($eml, $ctx);
-       multipart_text_as_html($eml, $ctx);
+       if (length($$obuf)) {
+               multipart_text_as_html($eml, $ctx);
+               $$obuf .= '</pre><hr>';
+       }
        delete $ctx->{obuf};
-       $$obuf .= '</pre><hr>';
        eval { $$obuf .= html_footer($ctx, $eml) };
        html_oneshot($ctx, 200, $obuf);
 }
@@ -66,7 +72,7 @@ sub msg_page {
        my ($ctx) = @_;
        my $ibx = $ctx->{ibx};
        $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
-       my $over = $ctx->{over} = $ibx->over or return no_over_html($ctx);
+       my $over = $ibx->over or return no_over_html($ctx);
        my ($id, $prev);
        my $next_arg = $ctx->{next_arg} = [ $ctx->{mid}, \$id, \$prev ];
 
@@ -136,6 +142,9 @@ $info
   <a
 href="$se_url">$se_url</a>
 $link</pre>
+
+  Be sure your reply has a <b>Subject:</b> header at the top and a blank line
+  before the message body.
 EOF
 }
 
@@ -254,7 +263,6 @@ sub eml_entry {
        } elsif ($mapping) {
                my $nested = 'nested';
                my $flat = 'flat';
-               my $end = '';
                if ($ctx->{flat}) {
                        $hr = 1;
                        $flat = "<b>$flat</b>";
@@ -276,8 +284,15 @@ sub eml_entry {
 sub pad_link ($$;$) {
        my ($mid, $level, $s) = @_;
        $s ||= '...';
-       my $id = id_compress($mid, 1);
-       (' 'x19).indent_for($level).th_pfx($level)."<a\nhref=#r$id>($s)</a>\n";
+       my $href = defined($mid) ?
+               ("<a\nhref=#r".id_compress($mid, 1).">($s)</a>\n") :
+               "($s)\n";
+       (' 'x19).indent_for($level).th_pfx($level).$href;
+}
+
+sub _skel_hdr {
+       # my ($mapping, $mid) = @_;
+       ($_[0]->{$_[1] // \'bogus'} // [ "(?)\n" ])->[0];
 }
 
 sub _th_index_lite {
@@ -309,10 +324,11 @@ sub _th_index_lite {
                                my $s = ($idx - 1). ' preceding siblings ...';
                                $rv .= pad_link($pmid, $level, $s);
                        } elsif ($idx == 2) {
-                               my $ppmid = $siblings->[0]->{mid};
-                               $rv .= $pad . $mapping->{$ppmid}->[0];
+                               $rv .= $pad . _skel_hdr($mapping,
+                                               $siblings->[0] ?
+                                               $siblings->[0]->{mid} : undef);
                        }
-                       $rv .= $pad . $mapping->{$pmid}->[0];
+                       $rv .= $pad . _skel_hdr($mapping, $pmid);
                }
        }
        my $s_s = nr_to_s($nr_s, 'sibling', 'siblings');
@@ -322,26 +338,26 @@ sub _th_index_lite {
        $attr =~ s!<a\nhref=[^>]+>([^<]+)</a>!$1!s; # no point linking to self
        $rv .= "<b>@ $attr";
        if ($nr_c) {
-               my $cmid = $children->[0]->{mid};
-               $rv .= $pad . $mapping->{$cmid}->[0];
+               my $cmid = $children->[0] ? $children->[0]->{mid} : undef;
+               $rv .= $pad . _skel_hdr($mapping, $cmid);
                if ($nr_c > 2) {
                        my $s = ($nr_c - 1). ' more replies';
                        $rv .= pad_link($cmid, $level + 1, $s);
                } elsif (my $cn = $children->[1]) {
-                       $rv .= $pad . $mapping->{$cn->{mid}}->[0];
+                       $rv .= $pad . _skel_hdr($mapping, $cn->{mid});
                }
        }
 
        my $next = $siblings->[$idx+1] if $siblings && $idx >= 0;
        if ($next) {
                my $nmid = $next->{mid};
-               $rv .= $pad . $mapping->{$nmid}->[0];
+               $rv .= $pad . _skel_hdr($mapping, $nmid);
                my $nnext = $nr_s - $idx;
                if ($nnext > 2) {
                        my $s = ($nnext - 1).' subsequent siblings';
                        $rv .= pad_link($nmid, $level, $s);
                } elsif (my $nn = $siblings->[$idx + 2]) {
-                       $rv .= $pad . $mapping->{$nn->{mid}}->[0];
+                       $rv .= $pad . _skel_hdr($mapping, $nn->{mid});
                }
        }
        $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
@@ -420,6 +436,7 @@ sub stream_thread ($$) {
 # /$INBOX/$MSGID/t/ and /$INBOX/$MSGID/T/
 sub thread_html {
        my ($ctx) = @_;
+       $ctx->{-upfx} = '../../';
        my $mid = $ctx->{mid};
        my $ibx = $ctx->{ibx};
        my ($nr, $msgs) = $ibx->over->get_thread($mid);
@@ -443,13 +460,12 @@ EOF
        $skel .= " (download: <a\nhref=\"../t.mbox.gz\">mbox.gz</a>";
        $skel .= " / follow: <a\nhref=\"../t.atom\">Atom feed</a>)\n";
        $skel .= "-- links below jump to the message on this page --\n";
-       $ctx->{-upfx} = '../../';
        $ctx->{cur_level} = 0;
        $ctx->{skel} = \$skel;
        $ctx->{prev_attr} = '';
        $ctx->{prev_level} = 0;
-       $ctx->{root_anchor} = anchor_for($mid);
-       $ctx->{mapping} = {};
+       $ctx->{root_anchor} = 'm' . id_compress($mid, 1);
+       $ctx->{mapping} = {}; # mid -> [ header_summary, node, idx, level ]
        $ctx->{s_nr} = ($nr > 1 ? "$nr+ messages" : 'only message')
                       .' in thread';
 
@@ -518,7 +534,8 @@ sub attach_link ($$$$;$) {
        return unless $part->{bdy};
 
        my $nl = $idx eq '1' ? '' : "\n"; # like join("\n", ...)
-       my $size = bytes::length($part->body);
+       my $size = length($part->body);
+       delete $part->{bdy}; # save memory
 
        # hide attributes normally, unless we want to aid users in
        # spotting MUA problems:
@@ -571,7 +588,7 @@ sub add_text_body { # callback for each_part
 
        # makes no difference to browsers, and don't screw up filename
        # link generation in diffs with the extra '%0D'
-       $s =~ s/\r\n/\n/sg;
+       $s =~ s/\r+\n/\n/sg;
 
        # will be escaped to `&#8226;' in HTML
        obfuscate_addrs($ibx, $s, "\x{2022}") if $ibx->{obfuscate};
@@ -591,8 +608,9 @@ sub add_text_body { # callback for each_part
                $diff = 1;
                delete $ctx->{-long_path};
                my $spfx;
-               if ($ibx->{-repo_objs}) {
-                       if (index($upfx, '//') >= 0) { # absolute URL (Atom feeds)
+               # absolute URL (Atom feeds)
+               if ($ibx->{coderepo}) {
+                       if (index($upfx, '//') >= 0) {
                                $spfx = $upfx;
                                $spfx =~ s!/([^/]*)/\z!/!;
                        } else {
@@ -609,9 +627,6 @@ sub add_text_body { # callback for each_part
                $ctx->{-spfx} = $spfx;
        };
 
-       # some editors don't put trailing newlines at the end:
-       $s .= "\n" unless $s =~ /\n\z/s;
-
        # split off quoted and unquoted blocks:
        my @sections = PublicInbox::MsgIter::split_quotes($s);
        undef $s; # free memory
@@ -620,6 +635,7 @@ sub add_text_body { # callback for each_part
                attach_link($ctx, $ct, $p, $fn, $err);
                $$rv .= "\n";
        }
+       delete $part->{bdy}; # save memory
        foreach my $cur (@sections) {
                if ($cur =~ /\A>/) {
                        # we use a <span> here to allow users to specify
@@ -645,13 +661,16 @@ sub _msg_page_prepare_obuf {
        my $mids = mids_for_index($eml);
        my $nr = $ctx->{nr}++;
        if ($nr) { # unlikely
+               if ($ctx->{chash} eq content_hash($eml)) {
+                       warn "W: BUG? @$mids not deduplicated properly\n";
+                       return \$rv;
+               }
+               $rv .=
+"<pre>WARNING: multiple messages have this Message-ID\n</pre>";
                $rv .= '<pre>';
        } else {
                $ctx->{first_hdr} = $eml->header_obj;
-               if ($ctx->{smsg}) {
-                       $rv .=
-"<pre>WARNING: multiple messages have this Message-ID\n</pre>";
-               }
+               $ctx->{chash} = content_hash($eml) if $ctx->{smsg}; # reused MID
                $rv .= "<pre\nid=b>"; # anchor for body start
        }
        $ctx->{-upfx} = '../' if $over;
@@ -693,7 +712,7 @@ sub _msg_page_prepare_obuf {
        for my $v ($eml->header('Date')) {
                $v = ascii_html($v);
                obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P
-               $rv .= "Date: $v\n";
+               $rv .= qq{Date: $v\t<a\nhref="#r">[thread overview]</a>\n};
        }
        if (!$nr) { # first (and only) message, common case
                $ctx->{-title_html} = join(' - ', @title);
@@ -738,7 +757,8 @@ sub thread_skel ($$$) {
                        $$skel .= SKEL_EXPAND."\n ";
                        $$skel .= ghost_parent('../', $parent) . "\n";
                } else {
-                       $$skel .= '[no followups] '.SKEL_EXPAND."\n";
+                       $$skel .= "<a\nid=r>[no followups]</a> ".
+                                       SKEL_EXPAND."\n";
                }
                $ctx->{next_msg} = undef;
                $ctx->{parent_msg} = $parent;
@@ -859,11 +879,6 @@ sub linkify_ref_no_over {
        "&lt;<a\nhref=\"../$href/\">$html</a>&gt;";
 }
 
-sub anchor_for {
-       my ($msgid) = @_;
-       'm' . id_compress($msgid, 1);
-}
-
 sub ghost_parent {
        my ($upfx, $mid) = @_;
 
@@ -965,7 +980,7 @@ sub skel_dump { # walk_thread callback
                $$skel .= delete($ctx->{sl_note}) || '';
        }
 
-       my $f = ascii_html($smsg->{from_name});
+       my $f = ascii_html(delete $smsg->{from_name});
        my $obfs_ibx = $ctx->{-obfs_ibx};
        obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
 
@@ -1060,10 +1075,10 @@ sub _skel_ghost {
 }
 
 sub sort_ds {
-       [ sort {
+       @{$_[0]} = sort {
                (eval { $a->topmost->{ds} } || 0) <=>
                (eval { $b->topmost->{ds} } || 0)
-       } @{$_[0]} ];
+       } @{$_[0]};
 }
 
 # accumulate recent topics if search is supported
@@ -1141,10 +1156,8 @@ sub dump_topics {
                        $anchor = '#t'; # thread skeleton
                }
 
-               my $mbox = qq(<a\nhref="$href/t.mbox.gz">mbox.gz</a>);
-               my $atom = qq(<a\nhref="$href/t.atom">Atom</a>);
                my $s = "<a\nhref=\"$href/T/$anchor\">$top_subj</a>\n" .
-                       " $ds UTC $n - $mbox / $atom\n";
+                       " $ds UTC $n\n";
                for (my $i = 0; $i < scalar(@extra); $i += 2) {
                        my $level = $extra[$i];
                        my $subj = $extra[$i + 1]; # already normalized
@@ -1264,7 +1277,7 @@ sub thread_adj_level {
 sub ghost_index_entry {
        my ($ctx, $level, $node) = @_;
        my ($beg, $end) = thread_adj_level($ctx,  $level);
-       $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{mid})
+       $beg . '<pre>'. ghost_parent($ctx->{-upfx}, $node->{mid} // '?')
                . '</pre>' . $end;
 }