]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/View.pm
view: improve readability around walk_thread
[public-inbox.git] / lib / PublicInbox / View.pm
index 1ce6ba8514ef10d4485b7d3269554e9b080697fa..040604b08b926552aa797725e3960bebeeacb0ee 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;
@@ -16,7 +16,7 @@ use PublicInbox::Address;
 use PublicInbox::WwwStream;
 use PublicInbox::Reply;
 use PublicInbox::ViewDiff qw(flush_diff);
-require POSIX;
+use POSIX qw(strftime);
 use Time::Local qw(timegm);
 use PublicInbox::SearchMsg qw(subject_normalized);
 use constant COLS => 72;
@@ -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);
        }
 
@@ -141,7 +149,7 @@ EOF
 <hr><pre
 id=R><b>Reply instructions:</b>
 
-You may reply publically to <a
+You may reply publicly to <a
 href=#t>this message</a> via plain-text email
 using any one of the following methods:
 
@@ -200,6 +208,9 @@ sub nr_to_s ($$$) {
        $nr == 1 ? "$nr $singular" : "$nr $plural";
 }
 
+# human-friendly format
+sub fmt_ts ($) { strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
+
 # this is already inside a <pre>
 sub index_entry {
        my ($smsg, $ctx, $more) = @_;
@@ -262,8 +273,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, 1);
+       delete $ctx->{rv};
 
        # add the footer
        $rv .= "\n<a\nhref=#$id_m\nid=e$id>^</a> ".
@@ -272,8 +285,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';
@@ -372,7 +385,8 @@ sub _th_index_lite {
        $rv .= $pad ."<a\nhref=#r$id>$s_s, $s_c; $ctx->{s_nr}</a>\n";
 }
 
-sub walk_thread {
+# non-recursive thread walker
+sub walk_thread ($$$) {
        my ($rootset, $ctx, $cb) = @_;
        my @q = map { (0, $_, -1) } @$rootset;
        while (@q) {
@@ -385,7 +399,7 @@ sub walk_thread {
        }
 }
 
-sub pre_thread  {
+sub pre_thread  { # walk_thread callback
        my ($ctx, $level, $node, $idx) = @_;
        $ctx->{mapping}->{$node->{id}} = [ '', $node, $idx, $level ];
        skel_dump($ctx, $level, $node);
@@ -399,7 +413,7 @@ sub thread_index_entry {
 
 sub stream_thread_i { # PublicInbox::WwwStream::getline callback
        my ($nr, $ctx) = @_;
-       return unless exists($ctx->{dst});
+       return unless exists($ctx->{skel});
        my $q = $ctx->{-queue};
        while (@$q) {
                my $level = shift @$q;
@@ -412,7 +426,7 @@ sub stream_thread_i { # PublicInbox::WwwStream::getline callback
                        return ghost_index_entry($ctx, $level, $node);
                }
        }
-       join('', thread_adj_level($ctx, 0)) . ${delete $ctx->{dst}}; # skel
+       join('', thread_adj_level($ctx, 0)) . ${delete $ctx->{skel}};
 }
 
 sub stream_thread ($$) {
@@ -436,6 +450,7 @@ sub stream_thread ($$) {
        PublicInbox::WwwStream->response($ctx, 200, \&stream_thread_i);
 }
 
+# /$INBOX/$MESSAGE_ID/t/
 sub thread_html {
        my ($ctx) = @_;
        my $mid = $ctx->{mid};
@@ -452,7 +467,7 @@ sub thread_html {
        $skel .= "-- links below jump to the message on this page --\n";
        $ctx->{-upfx} = '../../';
        $ctx->{cur_level} = 0;
-       $ctx->{dst} = \$skel;
+       $ctx->{skel} = \$skel;
        $ctx->{prev_attr} = '';
        $ctx->{prev_level} = 0;
        $ctx->{root_anchor} = anchor_for($mid);
@@ -464,7 +479,7 @@ sub thread_html {
 
        # reduce hash lookups in pre_thread->skel_dump
        $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
-       walk_thread($rootset, $ctx, *pre_thread);
+       walk_thread($rootset, $ctx, \&pre_thread);
 
        $skel .= '</pre>';
        return stream_thread($rootset, $ctx) unless $ctx->{flat};
@@ -477,26 +492,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(skel msgs)};
+       $$skel;
 }
 
 sub multipart_text_as_html {
-       my ($mime, $upfx, $ctx) = @_;
-       my $rv = "";
+       my (undef, $mhref, $ctx) = @_; # $mime = $_[0]
+       $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($_[0], \&add_text_body, $ctx, 1);
+       ${delete $ctx->{rv}};
 }
 
 sub flush_quote {
@@ -514,7 +532,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 +553,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 +616,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 {
@@ -706,7 +724,7 @@ sub _msg_html_prepare {
 }
 
 sub thread_skel {
-       my ($dst, $ctx, $hdr, $tpfx) = @_;
+       my ($skel, $ctx, $hdr, $tpfx) = @_;
        my $mid = mids($hdr)->[0];
        my $ibx = $ctx->{-inbox};
        my ($nr, $msgs) = $ibx->over->get_thread($mid);
@@ -716,21 +734,21 @@ sub thread_skel {
                        qq(<a\nhref="${tpfx}t.atom">Atom feed</a>);
 
        my $parent = in_reply_to($hdr);
-       $$dst .= "\n<b>Thread overview: </b>";
+       $$skel .= "\n<b>Thread overview: </b>";
        if ($nr <= 1) {
                if (defined $parent) {
-                       $$dst .= "$expand\n ";
-                       $$dst .= ghost_parent("$tpfx../", $parent) . "\n";
+                       $$skel .= "$expand\n ";
+                       $$skel .= ghost_parent("$tpfx../", $parent) . "\n";
                } else {
-                       $$dst .= "[no followups] $expand\n";
+                       $$skel .= "[no followups] $expand\n";
                }
                $ctx->{next_msg} = undef;
                $ctx->{parent_msg} = $parent;
                return;
        }
 
-       $$dst .= "$nr+ messages / $expand";
-       $$dst .= qq!  <a\nhref="#b">top</a>\n!;
+       $$skel .= "$nr+ messages / $expand";
+       $$skel .= qq!  <a\nhref="#b">top</a>\n!;
 
        # nb: mutt only shows the first Subject in the index pane
        # when multiple Subject: headers are present, so we follow suit:
@@ -740,11 +758,11 @@ sub thread_skel {
        $ctx->{cur} = $mid;
        $ctx->{prev_attr} = '';
        $ctx->{prev_level} = 0;
-       $ctx->{dst} = $dst;
+       $ctx->{skel} = $skel;
 
        # reduce hash lookups in skel_dump
        $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef;
-       walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump);
+       walk_thread(thread_results($ctx, $msgs), $ctx, \&skel_dump);
 
        $ctx->{parent_msg} = $parent;
 }
@@ -788,11 +806,11 @@ sub html_footer {
        my $ibx = $ctx->{-inbox} if $ctx;
        my $upfx = '../';
        my $tpfx = '';
-       my $idx = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
+       my $skel = $standalone ? " <a\nhref=\"$upfx\">index</a>" : '';
        my $irt = '';
-       if ($idx && $ibx->over) {
-               $idx .= "\n";
-               thread_skel(\$idx, $ctx, $hdr, $tpfx);
+       if ($skel && $ibx->over) {
+               $skel .= "\n";
+               thread_skel(\$skel, $ctx, $hdr, $tpfx);
                my ($next, $prev);
                my $parent = '       ';
                $next = $prev = '    ';
@@ -827,7 +845,7 @@ sub html_footer {
        }
        $rhref ||= '#R';
        $irt .= qq(<a\nhref="$rhref">reply</a>);
-       $irt .= $idx;
+       $irt .= $skel;
 }
 
 sub linkify_ref_no_over {
@@ -895,7 +913,7 @@ sub thread_results {
        if (defined($mid) && scalar(@$rootset) > 1) {
                $ctx->{root_idx} = -1;
                my $nr = scalar @$msgs;
-               walk_thread($rootset, $ctx, *find_mid_root);
+               walk_thread($rootset, $ctx, \&find_mid_root);
                my $idx = $ctx->{found_mid_at};
                if (defined($idx) && $idx != 0) {
                        my $tip = splice(@$rootset, $idx, 1);
@@ -913,8 +931,6 @@ sub missing_thread {
        PublicInbox::ExtMsg::ext_msg($ctx);
 }
 
-sub fmt_ts { POSIX::strftime('%Y-%m-%d %k:%M', gmtime($_[0])) }
-
 sub dedupe_subject {
        my ($prev_subj, $subj, $val) = @_;
 
@@ -934,16 +950,16 @@ sub dedupe_subject {
        $omit;
 }
 
-sub skel_dump {
+sub skel_dump { # walk_thread callback
        my ($ctx, $level, $node) = @_;
        my $smsg = $node->{smsg} or return _skel_ghost($ctx, $level, $node);
 
-       my $dst = $ctx->{dst};
+       my $skel = $ctx->{skel};
        my $cur = $ctx->{cur};
        my $mid = $smsg->{mid};
 
        if ($level == 0 && $ctx->{skel_dump_roots}++) {
-               $$dst .= delete $ctx->{sl_note} || '';
+               $$skel .= delete($ctx->{sl_note}) || '';
        }
 
        my $f = ascii_html($smsg->from_name);
@@ -952,9 +968,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;
@@ -973,7 +988,7 @@ sub skel_dump {
        if ($cur) {
                if ($cur eq $mid) {
                        delete $ctx->{cur};
-                       $$dst .= "<b>$d<a\nid=r\nhref=\"#t\">".
+                       $$skel .= "<b>$d<a\nid=r\nhref=\"#t\">".
                                 "$attr [this message]</a></b>\n";
                        return 1;
                } else {
@@ -1013,7 +1028,7 @@ sub skel_dump {
        } else {
                $m = $ctx->{-upfx}.mid_escape($mid).'/';
        }
-       $$dst .=  $d . "<a\nhref=\"$m\"$id>" . $end;
+       $$skel .=  $d . "<a\nhref=\"$m\"$id>" . $end;
        1;
 }
 
@@ -1022,7 +1037,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);
@@ -1038,8 +1053,7 @@ sub _skel_ghost {
        } else {
                $d .= qq{&lt;<a\nhref="$href">$html</a>&gt;\n};
        }
-       my $dst = $ctx->{dst};
-       $$dst .= $d;
+       ${$ctx->{skel}} .= $d;
        1;
 }
 
@@ -1052,7 +1066,7 @@ sub sort_ds {
 
 # accumulate recent topics if search is supported
 # returns 200 if done, 404 if not
-sub acc_topic {
+sub acc_topic { # walk_thread callback
        my ($ctx, $level, $node) = @_;
        my $mid = $node->{id};
        my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid);
@@ -1147,10 +1161,8 @@ sub dump_topics {
        200;
 }
 
-sub ts2str ($) {
-       my ($ts) = @_;
-       POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts));
-}
+# only for the t= query parameter passed to overview DB
+sub ts2str ($) { strftime('%Y%m%d%H%M%S', gmtime($_[0])) };
 
 sub str2ts ($) {
        my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]);
@@ -1220,9 +1232,9 @@ sub index_topics {
        my ($ctx) = @_;
        my $msgs = paginate_recent($ctx, 200); # 200 is our window
        if (@$msgs) {
-               walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
+               walk_thread(thread_results($ctx, $msgs), $ctx, \&acc_topic);
        }
-       PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav);
+       PublicInbox::WwwStream->response($ctx, dump_topics($ctx), \&index_nav);
 }
 
 sub thread_adj_level {