]> 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 1ce6ba8514ef10d4485b7d3269554e9b080697fa..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';
@@ -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 {
@@ -952,9 +965,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 +1034,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);