From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS208294 185.220.101.0/24 X-Spam-Status: No, score=-1.2 required=3.0 tests=AWL,BAYES_00,RCVD_IN_PBL, RCVD_IN_XBL,RDNS_NONE,SPF_FAIL,SPF_HELO_FAIL shortcircuit=no autolearn=no autolearn_force=no version=3.4.2 Received: from 80x24.org (unknown [185.220.101.137]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id C34871F45A for ; Mon, 20 Apr 2020 07:14:41 +0000 (UTC) From: Eric Wong To: spew@80x24.org Subject: [PATCH] wip Date: Mon, 20 Apr 2020 07:14:35 +0000 Message-Id: <20200420071435.7434-1-e@yhbt.net> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: --- lib/PublicInbox/MsgIter.pm | 6 ++-- lib/PublicInbox/View.pm | 64 +++++++++++++++++++++++++++++++++++--- t/message_subparts.t | 46 +++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 8 deletions(-) create mode 100644 t/message_subparts.t diff --git a/lib/PublicInbox/MsgIter.pm b/lib/PublicInbox/MsgIter.pm index fa25564a..e33e2b2b 100644 --- a/lib/PublicInbox/MsgIter.pm +++ b/lib/PublicInbox/MsgIter.pm @@ -12,8 +12,8 @@ use PublicInbox::MIME; # Like Email::MIME::walk_parts, but this is: # * non-recursive # * passes depth and indices to the iterator callback -sub msg_iter ($$;$$) { - my ($mime, $cb, $cb_arg, $do_undef) = @_; +sub msg_iter ($$;$$$) { + my ($mime, $cb, $cb_arg, $do_undef, $sub_hdr_cb) = @_; my @parts = $mime->subparts; if (@parts) { $mime = $_[0] = undef if $do_undef; # saves some memory @@ -28,7 +28,7 @@ sub msg_iter ($$;$$) { @sub = map { [ $_, $depth, @idx, ++$i ] } @sub; @parts = (@sub, @parts); } else { - $cb->($p, $cb_arg); + $cb->($p, $cb_arg, \$depth, \@parts); } } } else { diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index 9b62ed3c..527b8316 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -515,14 +515,67 @@ EOF undef; } +# show headers on message/rfc822 attachments +sub submsg_hdr { + my ($hdr, $ctx) = @_; + my $obfs_ibx = $ctx->{-obfs_ibx}; + my $rv = $ctx->{obuf}; + $$rv .= "\n"; + for my $h (qw(From To Cc)) { + for my $v ($hdr->header($h)) { + fold_addresses($v); + $v = ascii_html($v); + obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; + $$rv .= "$h: $v\n" if $v ne ''; + } + } + for my $h (qw(Subject Date)) { + for my $v ($hdr->header($h)) { + $v = ascii_html($v); + obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; + $$rv .= "$h: $v\n" if $v ne ''; + } + } + # my $lnk = PublicInbox::Linkify->new; + for my $h (qw(Message-ID X-Alt-Message-ID In-Reply-To References)) { + my $s = ''; + $s .= "$h: $_\n" for ($hdr->header_raw($h)); + # $lnk->linkify_mids('..', \$s, 1); + $$rv .= $s; + } + $$rv .= _parent_headers($hdr); + $$rv .= "\n"; + undef; +} + sub add_text_body { # callback for msg_iter - my ($p, $ctx) = @_; - my $upfx = $ctx->{mhref}; - my $ibx = $ctx->{-inbox}; + my ($p, $ctx, $iter_depth, $iter_parts) = @_; # $p - from msg_iter: [ Email::MIME, depth, @idx ] my ($part, $depth, @idx) = @$p; my $ct = $part->content_type || 'text/plain'; my $fn = $part->filename; + + if ($ct =~ m!\Amessage/(?:rfc822|news)\b!i) { + # required for compatibility with old URLs + attach_link($ctx, $ct, $p, $fn); + + my $submsg = PublicInbox::MIME->new(\($part->body)); + submsg_hdr($submsg->header_obj, $ctx); + my @sub = $submsg->subparts; + @sub = $submsg if !@sub; + my $d = ++$$iter_depth; + my $i = 0; + @sub = map { [ $_, $d, @idx, ++$i ] } @sub; + @$iter_parts = (@sub, @$iter_parts); + return; + # } + # $p->[0] = $part = $submsg; + # $p->[1] = ++$depth; + # $ct = $part->content_type || 'text/plain'; + # $fn = $part->filename; + # fall-through: + } + my ($s, $err) = msg_part_text($part, $ct); return attach_link($ctx, $ct, $p, $fn) unless defined $s; @@ -530,6 +583,7 @@ sub add_text_body { # callback for msg_iter # link generation in diffs with the extra '%0D' $s =~ s/\r\n/\n/sg; + my $ibx = $ctx->{-inbox}; # will be escaped to `•' in HTML obfuscate_addrs($ibx, $s, "\x{2022}") if $ibx->{obfuscate}; @@ -537,6 +591,7 @@ sub add_text_body { # callback for msg_iter # headers for solver unless some coderepo are configured: my $diff; if ($s =~ /^(?:diff|---|\+{3}) /ms) { + my $upfx = $ctx->{mhref}; # diffstat anchors do not link across attachments or messages: $idx[0] = $upfx . $idx[0] if $upfx ne ''; $ctx->{-apfx} = join('/', @idx); @@ -718,12 +773,11 @@ sub thread_skel ($$$) { } sub _parent_headers { - my ($hdr, $over) = @_; + my ($hdr, $over, $lnk) = @_; my $rv = ''; my @irt = $hdr->header_raw('In-Reply-To'); my $refs; if (@irt) { - my $lnk = PublicInbox::Linkify->new; $rv .= "In-Reply-To: $_\n" for @irt; $lnk->linkify_mids('..', \$rv); } else { diff --git a/t/message_subparts.t b/t/message_subparts.t new file mode 100644 index 00000000..55ca8f4e --- /dev/null +++ b/t/message_subparts.t @@ -0,0 +1,46 @@ +# Copyright (C) 2020 all contributors +# License: AGPL-3.0+ +use strict; +use Test::More; +use PublicInbox::MIME; +use PublicInbox::MsgIter; + +my $rfc822 = PublicInbox::MIME->new(<<'EOF'); +From: a@example.com +To: a@example.com +Date: Fri, 02 Oct 1993 00:00:00 +0000 +Subject: message/rfc822 embedded +Content-Type: multipart/mixed; boundary="x" +MIME-Version: 1.0 + +--x +Content-Transfer-Encoding: 7bit +Content-Type: message/rfc822 + +From: a@example.com +To: a@example.com +Date: Fri, 02 Oct 1993 00:00:00 +0000 +Subject: lost +Content-Type: multipart/mixed; boundary="y" +MIME-Version: 1.0 + +--y +Content-Transfer-Encoding: 7bit +Content-Type: text/plain + +my leg + +--y-- + +--x-- +EOF + +use Data::Dumper; +msg_iter($rfc822, sub { + my ($part, $level, @idx) = @{$_[0]}; + my ($s, $err) = msg_part_text($part, $part->content_type); + diag "S[$level,[".join(',', @idx)."]: $s"; +}); + +ok 1; +done_testing();