diff options
Diffstat (limited to 'lib/PublicInbox/View.pm')
-rw-r--r-- | lib/PublicInbox/View.pm | 187 |
1 files changed, 99 insertions, 88 deletions
diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index 0b102638..c38a1289 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -24,37 +24,44 @@ use constant INDENT => ' '; use constant TCHILD => '` '; sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD }; +sub msg_html_i { + my ($nr, $ctx) = @_; + my $more = $ctx->{more}; + if ($nr == 1) { + # $more cannot be true w/o $smsg being defined: + my $upfx = $more ? '../'.mid_escape($ctx->{smsg}->mid).'/' : ''; + $ctx->{tip} . + multipart_text_as_html($ctx->{mime}, $upfx, $ctx) . + '</pre><hr>' + } elsif ($more && @$more) { + ++$ctx->{end_nr}; + msg_html_more($ctx, $more, $nr); + } elsif ($nr == $ctx->{end_nr}) { + # fake an EOF if generating the footer fails; + # we want to at least show the message if something + # here crashes: + eval { + my $hdr = delete($ctx->{mime})->header_obj; + '<pre>' . html_footer($hdr, 1, $ctx) . + '</pre>' . msg_reply($ctx, $hdr) + }; + } else { + undef + } +} + # public functions: (unstable) sub msg_html { my ($ctx, $mime, $more, $smsg) = @_; - my $hdr = $mime->header_obj; my $ibx = $ctx->{-inbox}; $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef; - my $tip = _msg_html_prepare($hdr, $ctx, $more, 0); - my $end = 2; - PublicInbox::WwwStream->response($ctx, 200, sub { - my ($nr, undef) = @_; - if ($nr == 1) { - # $more cannot be true w/o $smsg being defined: - my $upfx = $more ? '../'.mid_escape($smsg->mid).'/' : ''; - $tip . multipart_text_as_html($mime, $upfx, $ctx) . - '</pre><hr>' - } elsif ($more && @$more) { - ++$end; - msg_html_more($ctx, $more, $nr); - } elsif ($nr == $end) { - # fake an EOF if generating the footer fails; - # we want to at least show the message if something - # here crashes: - eval { - '<pre>' . html_footer($hdr, 1, $ctx) . - '</pre>' . msg_reply($ctx, $hdr) - }; - } else { - undef - } - }); + $ctx->{tip} = _msg_html_prepare($mime->header_obj, $ctx, $more, 0); + $ctx->{more} = $more; + $ctx->{end_nr} = 2; + $ctx->{smsg} = $smsg; + $ctx->{mime} = $mime; + PublicInbox::WwwStream->response($ctx, 200, \&msg_html_i); } sub msg_page { @@ -262,8 +269,10 @@ sub index_entry { $rv .= "\n"; # scan through all parts, looking for displayable text - my $ibx = $ctx->{-inbox}; - msg_iter($mime, sub { $rv .= add_text_body($mhref, $ctx, $_[0]) }); + $ctx->{mhref} = $mhref; + $ctx->{rv} = \$rv; + msg_iter($mime, \&add_text_body, $ctx); + delete $ctx->{rv}; # add the footer $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ". @@ -272,8 +281,8 @@ sub index_entry { " <a\nhref=\"${mhref}#R\">reply</a>"; my $hr; - if (my $pct = $ctx->{pct}) { # used by SearchView.pm - $rv .= "\t[relevance $pct->{$mid_raw}%]"; + if (defined(my $pct = $smsg->{pct})) { # used by SearchView.pm + $rv .= "\t[relevance $pct%]"; $hr = 1; } elsif ($mapping) { my $nested = 'nested'; @@ -397,12 +406,29 @@ sub thread_index_entry { $beg . '<pre>' . index_entry($smsg, $ctx, 0) . '</pre>' . $end; } +sub stream_thread_i { # PublicInbox::WwwStream::getline callback + my ($nr, $ctx) = @_; + return unless exists($ctx->{dst}); + my $q = $ctx->{-queue}; + while (@$q) { + my $level = shift @$q; + my $node = shift @$q or next; + my $cl = $level + 1; + unshift @$q, map { ($cl, $_) } @{$node->{children}}; + if (my $smsg = $ctx->{-inbox}->smsg_mime($node->{smsg})) { + return thread_index_entry($ctx, $level, $smsg); + } else { + return ghost_index_entry($ctx, $level, $node); + } + } + join('', thread_adj_level($ctx, 0)) . ${delete $ctx->{dst}}; # skel +} + sub stream_thread ($$) { my ($rootset, $ctx) = @_; my $ibx = $ctx->{-inbox}; my @q = map { (0, $_) } @$rootset; - my $level; - my $smsg; + my ($smsg, $level); while (@q) { $level = shift @q; my $node = shift @q or next; @@ -415,25 +441,8 @@ sub stream_thread ($$) { $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef; $ctx->{-title_html} = ascii_html($smsg->subject); $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg); - $smsg = undef; - PublicInbox::WwwStream->response($ctx, 200, sub { - return unless $ctx; - while (@q) { - $level = shift @q; - my $node = shift @q or next; - my $cl = $level + 1; - unshift @q, map { ($cl, $_) } @{$node->{children}}; - if ($smsg = $ibx->smsg_mime($node->{smsg})) { - return thread_index_entry($ctx, $level, $smsg); - } else { - return ghost_index_entry($ctx, $level, $node); - } - } - my $ret = join('', thread_adj_level($ctx, 0)); - $ret .= ${$ctx->{dst}}; # skel - $ctx = undef; - $ret; - }); + $ctx->{-queue} = \@q; + PublicInbox::WwwStream->response($ctx, 200, \&stream_thread_i); } sub thread_html { @@ -477,26 +486,29 @@ sub thread_html { return missing_thread($ctx) unless $smsg; $ctx->{-title_html} = ascii_html($smsg->subject); $ctx->{-html_tip} = '<pre>'.index_entry($smsg, $ctx, scalar @$msgs); - $smsg = undef; - PublicInbox::WwwStream->response($ctx, 200, sub { - return unless $msgs; - $smsg = undef; - while (my $m = shift @$msgs) { - $smsg = $ibx->smsg_mime($m) and last; - } - return index_entry($smsg, $ctx, scalar @$msgs) if $smsg; - $msgs = undef; - $skel; - }); + $ctx->{msgs} = $msgs; + PublicInbox::WwwStream->response($ctx, 200, \&thread_html_i); +} + +sub thread_html_i { # PublicInbox::WwwStream::getline callback + my ($nr, $ctx) = @_; + my $msgs = $ctx->{msgs} or return; + while (my $smsg = shift @$msgs) { + $ctx->{-inbox}->smsg_mime($smsg) or next; + return index_entry($smsg, $ctx, scalar @$msgs); + } + my ($skel) = delete @$ctx{qw(dst msgs)}; + $$skel; } sub multipart_text_as_html { - my ($mime, $upfx, $ctx) = @_; - my $rv = ""; + my ($mime, $mhref, $ctx) = @_; + $ctx->{mhref} = $mhref; + $ctx->{rv} = \(my $rv = ''); # scan through all parts, looking for displayable text - msg_iter($mime, sub { $rv .= add_text_body($upfx, $ctx, $_[0]) }); - $rv; + msg_iter($mime, \&add_text_body, $ctx); + ${delete $ctx->{rv}}; } sub flush_quote { @@ -514,7 +526,7 @@ sub flush_quote { } sub attach_link ($$$$;$) { - my ($upfx, $ct, $p, $fn, $err) = @_; + my ($ctx, $ct, $p, $fn, $err) = @_; my ($part, $depth, @idx) = @$p; my $nl = $idx[-1] > 1 ? "\n" : ''; my $idx = join('.', @idx); @@ -535,29 +547,29 @@ sub attach_link ($$$$;$) { } else { $sfn = 'a.bin'; } - my $ret = qq($nl<a\nhref="$upfx$idx-$sfn">); + my $rv = $ctx->{rv}; + $$rv .= qq($nl<a\nhref="$ctx->{mhref}$idx-$sfn">); if ($err) { - $ret .= -"[-- Warning: decoded text below may be mangled --]\n"; + $$rv .= "[-- Warning: decoded text below may be mangled --]\n"; } - $ret .= "[-- Attachment #$idx: "; + $$rv .= "[-- Attachment #$idx: "; my $ts = "Type: $ct, Size: $size bytes"; $desc = ascii_html($desc); - $ret .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]"; - $ret .= "</a>\n"; + $$rv .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]"; + $$rv .= "</a>\n"; + undef; } -sub add_text_body { - my ($upfx, $ctx, $p) = @_; +sub add_text_body { # callback for msg_iter + my ($p, $ctx) = @_; + my $upfx = $ctx->{mhref}; my $ibx = $ctx->{-inbox}; - my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef; # $p - from msg_iter: [ Email::MIME, 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($upfx, $ct, $p, $fn) unless defined $s; + return attach_link($ctx, $ct, $p, $fn) unless defined $s; # makes no difference to browsers, and don't screw up filename # link generation in diffs with the extra '%0D' @@ -598,29 +610,29 @@ sub add_text_body { # split off quoted and unquoted blocks: my @sections = split(/((?:^>[^\n]*\n)+)/sm, $s); $s = ''; + my $rv = $ctx->{rv}; if (defined($fn) || $depth > 0 || $err) { # badly-encoded message with $err? tell the world about it! - $s .= attach_link($upfx, $ct, $p, $fn, $err); - $s .= "\n"; + attach_link($ctx, $ct, $p, $fn, $err); + $$rv .= "\n"; } my $l = PublicInbox::Linkify->new; foreach my $cur (@sections) { if ($cur =~ /\A>/) { - flush_quote(\$s, $l, \$cur); + flush_quote($rv, $l, \$cur); } elsif ($diff) { @$diff = split(/^/m, $cur); $cur = undef; - flush_diff(\$s, $ctx, $l); + flush_diff($rv, $ctx, $l); } else { # regular lines, OK $l->linkify_1($cur); - $s .= $l->linkify_2(ascii_html($cur)); + $$rv .= $l->linkify_2(ascii_html($cur)); $cur = undef; } } - obfuscate_addrs($obfs_ibx, $s) if $obfs_ibx; - $s; + obfuscate_addrs($ibx, $$rv) if $ibx->{obfuscate}; } sub _msg_html_prepare { @@ -952,9 +964,8 @@ sub skel_dump { my $d = fmt_ts($smsg->{ds}); my $unmatched; # if lazy-loaded by SearchThread::Msg::visible() - if (my $pct = $ctx->{pct}) { - $pct = $pct->{$smsg->{mid}}; - if (defined $pct) { + if (exists $ctx->{searchview}) { + if (defined(my $pct = $smsg->{pct})) { $d .= (sprintf(' % 2u', $pct) . '%'); } else { $unmatched = 1; @@ -1022,7 +1033,7 @@ sub _skel_ghost { my $mid = $node->{id}; my $d = ' [not found] '; - $d .= ' ' if exists $ctx->{pct}; + $d .= ' ' if exists $ctx->{searchview}; $d .= indent_for($level) . th_pfx($level); my $upfx = $ctx->{-upfx}; my $m = PublicInbox::Hval->new_msgid($mid); |