]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/View.pm
hval: export prurl and add prototype
[public-inbox.git] / lib / PublicInbox / View.pm
index 39b04174fb24d46c26d1eadb6b3995f35576ff57..5acd565a743543ef9db41e4716f1338a4dec71c8 100644 (file)
@@ -8,7 +8,7 @@ use strict;
 use warnings;
 use bytes (); # only for bytes::length
 use PublicInbox::MsgTime qw(msg_datestamp);
-use PublicInbox::Hval qw/ascii_html obfuscate_addrs/;
+use PublicInbox::Hval qw(ascii_html obfuscate_addrs prurl);
 use PublicInbox::Linkify;
 use PublicInbox::MID qw/id_compress mid_escape mids mids_for_index references/;
 use PublicInbox::MsgIter;
@@ -24,37 +24,45 @@ 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(delete $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->{hdr});
+                       '<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
-               }
-       });
+       my $hdr = $ctx->{hdr} = $mime->header_obj;
+       $ctx->{tip} = _msg_html_prepare($hdr, $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 {
@@ -88,8 +96,8 @@ sub msg_html_more {
                my $next = $ibx->over->next_by_mid($mid, \$id, \$prev);
                @$more = $next ? ($id, $prev, $next) : ();
                if ($smsg) {
-                       my $mime = $smsg->{mime};
                        my $upfx = '../' . mid_escape($smsg->mid) . '/';
+                       my $mime = delete $smsg->{mime};
                        _msg_html_prepare($mime->header_obj, $ctx, $more, $nr) .
                                multipart_text_as_html($mime, $upfx, $ctx) .
                                '</pre><hr>'
@@ -115,7 +123,7 @@ sub msg_reply {
        my $info = '';
        my $ibx = $ctx->{-inbox};
        if (my $url = $ibx->{infourl}) {
-               $url = PublicInbox::Hval::prurl($ctx->{env}, $url);
+               $url = prurl($ctx->{env}, $url);
                $info = qq(\n  List information: <a\nhref="$url">$url</a>\n);
        }
 
@@ -262,8 +270,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 +282,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 +407,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 +442,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 +487,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 +527,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 +548,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 +611,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 {
@@ -881,8 +894,7 @@ sub strict_loose_note ($) {
 sub thread_results {
        my ($ctx, $msgs) = @_;
        require PublicInbox::SearchThread;
-       my $ibx = $ctx->{-inbox};
-       my $rootset = PublicInbox::SearchThread::thread($msgs, *sort_ds, $ibx);
+       my $rootset = PublicInbox::SearchThread::thread($msgs, \&sort_ds, $ctx);
 
        # FIXME: `tid' is broken on --reindex, so that needs to be fixed
        # and preserved in the future.  This bug is hidden by `sid' matches
@@ -951,7 +963,17 @@ sub skel_dump {
        my $obfs_ibx = $ctx->{-obfs_ibx};
        obfuscate_addrs($obfs_ibx, $f) if $obfs_ibx;
 
-       my $d = fmt_ts($smsg->{ds}) . ' ' . indent_for($level) . th_pfx($level);
+       my $d = fmt_ts($smsg->{ds});
+       my $unmatched; # if lazy-loaded by SearchThread::Msg::visible()
+       if (exists $ctx->{searchview}) {
+               if (defined(my $pct = $smsg->{pct})) {
+                       $d .= (sprintf(' % 2u', $pct) . '%');
+               } else {
+                       $unmatched = 1;
+                       $d .= '    ';
+               }
+       }
+       $d .= ' ' . indent_for($level) . th_pfx($level);
        my $attr = $f;
        $ctx->{first_level} ||= $level;
 
@@ -977,7 +999,6 @@ sub skel_dump {
        # our Xapian which would've resulted in '' if it were
        # really missing (and Filter rejects empty subjects)
        my @subj = split(/ /, subject_normalized($smsg->subject));
-
        # remove common suffixes from the subject if it matches the previous,
        # so we do not show redundant text at the end.
        my $prev_subj = $ctx->{prev_subj} || [];
@@ -994,7 +1015,7 @@ sub skel_dump {
        }
        my $m;
        my $id = '';
-       my $mapping = $ctx->{mapping};
+       my $mapping = $unmatched ? undef : $ctx->{mapping};
        if ($mapping) {
                my $map = $mapping->{$mid};
                $id = id_compress($mid, 1);
@@ -1012,8 +1033,8 @@ sub _skel_ghost {
        my ($ctx, $level, $node) = @_;
 
        my $mid = $node->{id};
-       my $d = $ctx->{pct} ? '    [irrelevant] ' # search result
-                           : '     [not found] ';
+       my $d = '     [not found] ';
+       $d .= '    '  if exists $ctx->{searchview};
        $d .= indent_for($level) . th_pfx($level);
        my $upfx = $ctx->{-upfx};
        my $m = PublicInbox::Hval->new_msgid($mid);