X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FView.pm;h=0eb20ae0d7f7f8bc2d1ba103ae70ee151614f72a;hb=0d41983a642b4dccb90d04c4e2c5e2602885444c;hp=1e2d3b55c48f53e0eafae768bf6a0758fd29324c;hpb=1f53f1bf532a390f130c2029168cf90706595570;p=public-inbox.git diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index 1e2d3b55..0eb20ae0 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -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; @@ -31,8 +31,8 @@ sub msg_html_i { # $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) . - '
' . html_footer($hdr, 1, $ctx) . - '' . msg_reply($ctx, $hdr) - }; + eval { html_footer($ctx) }; } else { undef } @@ -56,7 +52,8 @@ sub msg_html { my ($ctx, $mime, $more, $smsg) = @_; my $ibx = $ctx->{-inbox}; $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef; - $ctx->{tip} = _msg_html_prepare($mime->header_obj, $ctx, $more, 0); + 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; @@ -95,8 +92,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) . '
Reply instructions: -You may reply publically to this message via plain-text email using any one of the following methods: @@ -207,6 +204,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 asub index_entry { my ($smsg, $ctx, $more) = @_; @@ -269,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, 1); + delete $ctx->{rv}; # add the footer $rv .= "\n^ ". @@ -279,8 +281,8 @@ sub index_entry { " reply"; 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'; @@ -379,7 +381,8 @@ sub _th_index_lite { $rv .= $pad ."$s_s, $s_c; $ctx->{s_nr}\n"; } -sub walk_thread { +# non-recursive thread walker +sub walk_thread ($$$) { my ($rootset, $ctx, $cb) = @_; my @q = map { (0, $_, -1) } @$rootset; while (@q) { @@ -392,7 +395,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); @@ -406,7 +409,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; @@ -419,7 +422,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 ($$) { @@ -443,6 +446,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}; @@ -459,7 +463,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); @@ -471,7 +475,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 .= ''; return stream_thread($rootset, $ctx) unless $ctx->{flat}; @@ -484,26 +488,29 @@ sub thread_html { return missing_thread($ctx) unless $smsg; $ctx->{-title_html} = ascii_html($smsg->subject); $ctx->{-html_tip} = ''.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 { @@ -521,7 +528,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); @@ -542,29 +549,29 @@ sub attach_link ($$$$;$) { } else { $sfn = 'a.bin'; } - my $ret = qq($nl); + my $rv = $ctx->{rv}; + $$rv .= qq($nl{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 .= "\n"; + $$rv .= ($desc eq '') ? "$ts --]" : "$desc --]\n[-- $ts --]"; + $$rv .= "\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' @@ -605,29 +612,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 { @@ -713,7 +720,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); @@ -723,21 +730,21 @@ sub thread_skel { qq(Atom feed); my $parent = in_reply_to($hdr); - $$dst .= "\nThread overview: "; + $$skel .= "\nThread overview: "; 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! top\n!; + $$skel .= "$nr+ messages / $expand"; + $$skel .= qq! top\n!; # nb: mutt only shows the first Subject in the index pane # when multiple Subject: headers are present, so we follow suit: @@ -747,11 +754,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; } @@ -789,17 +796,17 @@ sub _parent_headers { $rv; } +# returns a string buffer via ->getline sub html_footer { - my ($hdr, $standalone, $ctx, $rhref) = @_; - - my $ibx = $ctx->{-inbox} if $ctx; + my ($ctx) = @_; + my $ibx = $ctx->{-inbox}; + my $hdr = delete $ctx->{hdr}; my $upfx = '../'; - my $tpfx = ''; - my $idx = $standalone ? " index" : ''; - my $irt = ''; - if ($idx && $ibx->over) { - $idx .= "\n"; - thread_skel(\$idx, $ctx, $hdr, $tpfx); + my $skel = " index"; + my $rv = ''; + if ($ibx->over) { + $skel .= "\n"; + thread_skel(\$skel, $ctx, $hdr, ''); my ($next, $prev); my $parent = ' '; $next = $prev = ' '; @@ -828,13 +835,12 @@ sub html_footer { } elsif ($u) { # unlikely $parent = " parent"; } - $irt = "$next $prev$parent "; - } else { - $irt = ''; + $rv .= "$next $prev$parent "; } - $rhref ||= '#R'; - $irt .= qq(reply); - $irt .= $idx; + $rv .= qq(reply); + $rv .= $skel; + $rv .= ''; + $rv .= msg_reply($ctx, $hdr); } sub linkify_ref_no_over { @@ -902,7 +908,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); @@ -920,8 +926,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) = @_; @@ -941,16 +945,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); @@ -959,9 +963,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; @@ -980,7 +983,7 @@ sub skel_dump { if ($cur) { if ($cur eq $mid) { delete $ctx->{cur}; - $$dst .= "$d". + $$skel .= "$d". "$attr [this message]\n"; return 1; } else { @@ -1020,7 +1023,7 @@ sub skel_dump { } else { $m = $ctx->{-upfx}.mid_escape($mid).'/'; } - $$dst .= $d . "" . $end; + $$skel .= $d . "" . $end; 1; } @@ -1029,7 +1032,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); @@ -1045,8 +1048,7 @@ sub _skel_ghost { } else { $d .= qq{<$html>\n}; } - my $dst = $ctx->{dst}; - $$dst .= $d; + ${$ctx->{skel}} .= $d; 1; } @@ -1059,7 +1061,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); @@ -1154,10 +1156,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]); @@ -1227,9 +1227,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 {