]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/View.pm
view: do not offer links to 0-byte multipart attachments
[public-inbox.git] / lib / PublicInbox / View.pm
index 280d0ea5557024a790012d2bf0ab1e2d950f014b..a05ac4142f29385abeb9b741eb2911cc6cb17fd4 100644 (file)
@@ -17,6 +17,7 @@ use PublicInbox::Address;
 use PublicInbox::WwwStream;
 use PublicInbox::Reply;
 use PublicInbox::ViewDiff qw(flush_diff);
+use PublicInbox::Eml;
 use POSIX qw(strftime);
 use Time::Local qw(timegm);
 use PublicInbox::Smsg qw(subject_normalized);
@@ -56,7 +57,7 @@ sub msg_page {
        } else {
                $first = $ibx->msg_by_mid($mid) or return;
        }
-       my $mime = PublicInbox::MIME->new($first);
+       my $mime = PublicInbox::Eml->new($first);
        $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
        my $hdr = $ctx->{hdr} = $mime->header_obj;
        $ctx->{obuf} = _msg_page_prepare_obuf($hdr, $ctx, 0);
@@ -243,7 +244,7 @@ sub index_entry {
        # scan through all parts, looking for displayable text
        $ctx->{mhref} = $mhref;
        $ctx->{obuf} = \$rv;
-       msg_iter($mime, \&add_text_body, $ctx, 1);
+       $mime->each_part(\&add_text_body, $ctx, 1);
        delete $ctx->{obuf};
 
        # add the footer
@@ -474,17 +475,36 @@ sub thread_html_i { # PublicInbox::WwwStream::getline callback
 }
 
 sub multipart_text_as_html {
-       # ($mime, $ctx) = @_; # msg_iter will do "$_[0] = undef"
+       # ($mime, $ctx) = @_; # each_part may do "$_[0] = undef"
 
        # scan through all parts, looking for displayable text
-       msg_iter($_[0], \&add_text_body, $_[1], 1);
+       $_[0]->each_part(\&add_text_body, $_[1], 1);
+}
+
+sub submsg_hdr ($$) {
+       my ($ctx, $eml) = @_;
+       my $obfs_ibx = $ctx->{-obfs_ibx};
+       my $rv = $ctx->{obuf};
+       $$rv .= "\n";
+       for my $h (qw(From To Cc Subject Date Message-ID X-Alt-Message-ID)) {
+               my @v = $eml->header($h);
+               for my $v (@v) {
+                       obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx;
+                       $v = ascii_html($v);
+                       $$rv .= "$h: $v\n";
+               }
+       }
 }
 
 sub attach_link ($$$$;$) {
        my ($ctx, $ct, $p, $fn, $err) = @_;
-       my ($part, $depth, @idx) = @$p;
-       my $nl = $idx[-1] > 1 ? "\n" : '';
-       my $idx = join('.', @idx);
+       my ($part, $depth, $idx) = @$p;
+
+       # Eml iteration clobbers multipart ->{bdy}, so do not offer
+       # downloads for 0-byte multipart attachments
+       return unless $part->{bdy};
+
+       my $nl = $idx eq '1' ? '' : "\n"; # like join("\n", ...)
        my $size = bytes::length($part->body);
 
        # hide attributes normally, unless we want to aid users in
@@ -512,20 +532,30 @@ EOF
        $desc = ascii_html($desc);
        $$rv .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]";
        $$rv .= "</a>\n";
+
+       submsg_hdr($ctx, $part) if $part->{is_submsg};
+
        undef;
 }
 
-sub add_text_body { # callback for msg_iter
+sub add_text_body { # callback for each_part
        my ($p, $ctx) = @_;
        my $upfx = $ctx->{mhref};
        my $ibx = $ctx->{-inbox};
-       # $p - from msg_iter: [ Email::MIME, depth, @idx ]
-       my ($part, $depth, @idx) = @$p;
+       my $l = $ctx->{-linkify} //= PublicInbox::Linkify->new;
+       # $p - from each_part: [ Email::MIME-like, depth, $idx ]
+       my ($part, $depth, $idx) = @$p;
        my $ct = $part->content_type || 'text/plain';
        my $fn = $part->filename;
        my ($s, $err) = msg_part_text($part, $ct);
        return attach_link($ctx, $ct, $p, $fn) unless defined $s;
 
+       my $rv = $ctx->{obuf};
+       if ($part->{is_submsg}) {
+               submsg_hdr($ctx, $part);
+               $$rv .= "\n";
+       }
+
        # 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;
@@ -536,11 +566,15 @@ sub add_text_body { # callback for msg_iter
        # always support diff-highlighting, but we can't linkify hunk
        # headers for solver unless some coderepo are configured:
        my $diff;
-       if ($s =~ /^(?:diff|---|\+{3}) /ms) {
-               # diffstat anchors do not link across attachments or messages:
-               $idx[0] = $upfx . $idx[0] if $upfx ne '';
-               $ctx->{-apfx} = join('/', @idx);
-               $ctx->{-anchors} = {}; # attr => filename
+       if ($s =~ /^--- [^\n]+\n\+{3} [^\n]+\n@@ /ms) {
+               # diffstat anchors do not link across attachments or messages,
+               # -apfx is just a stable prefix for making diffstat anchors
+               # linkable to the first diff hunk w/o crossing attachments
+               $idx =~ tr!.!/!; # compatibility with previous versions
+               $ctx->{-apfx} = $upfx . $idx;
+
+               # do attr => filename mappings for diffstats in git diffs:
+               $ctx->{-anchors} = {} if $s =~ /^diff --git /sm;
                $diff = 1;
                delete $ctx->{-long_path};
                my $spfx;
@@ -568,13 +602,11 @@ sub add_text_body { # callback for msg_iter
        # split off quoted and unquoted blocks:
        my @sections = PublicInbox::MsgIter::split_quotes($s);
        undef $s; # free memory
-       my $rv = $ctx->{obuf};
-       if (defined($fn) || $depth > 0 || $err) {
+       if (defined($fn) || ($depth > 0 && !$part->{is_submsg}) || $err) {
                # badly-encoded message with $err? tell the world about it!
                attach_link($ctx, $ct, $p, $fn, $err);
                $$rv .= "\n";
        }
-       my $l = $ctx->{-linkify} //= PublicInbox::Linkify->new;
        foreach my $cur (@sections) {
                if ($cur =~ /\A>/) {
                        # we use a <span> here to allow users to specify
@@ -1096,6 +1128,7 @@ sub dump_topics {
                        my @next_prev = @subj; # full copy
                        my $omit = dedupe_subject($prev_subj, \@subj, ' &#34;');
                        $prev_subj = \@next_prev;
+                       $subj = join(' ', @subj);
                        $subj = ascii_html($subj);
                        obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx;
                        $href = mid_href($mid);