X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;ds=sidebyside;f=lib%2FPublicInbox%2FView.pm;h=81e83d97738256511a530bc4093bfd2793267eb8;hb=956ede734d7c2e8d0a3003c6e2d554114586643e;hp=9095c505b88c814e0ce289092e86e4d3533685f3;hpb=b2b225a64fdad6d24e95833e6d311589407c17ee;p=public-inbox.git diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index 9095c505..81e83d97 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -6,16 +6,14 @@ package PublicInbox::View; use strict; use warnings; -use URI::Escape qw/uri_escape_utf8/; use Date::Parse qw/str2time/; -use Encode::MIME::Header; -use Plack::Util; -use PublicInbox::Hval qw/ascii_html/; +use PublicInbox::Hval qw/ascii_html obfuscate_addrs/; use PublicInbox::Linkify; -use PublicInbox::MID qw/mid_clean id_compress mid_mime/; +use PublicInbox::MID qw/mid_clean id_compress mid_mime mid_escape/; use PublicInbox::MsgIter; use PublicInbox::Address; use PublicInbox::WwwStream; +use PublicInbox::Reply; require POSIX; use constant INDENT => ' '; @@ -24,17 +22,24 @@ sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD }; # public functions: (unstable) sub msg_html { - my ($ctx, $mime, $footer) = @_; + my ($ctx, $mime) = @_; my $hdr = $mime->header_obj; - my $tip = _msg_html_prepare($hdr, $ctx); - PublicInbox::WwwStream->new($ctx, sub { + my $ibx = $ctx->{-inbox}; + my $obfs_ibx = $ibx->{obfuscate} ? $ibx : undef; + my $tip = _msg_html_prepare($hdr, $ctx, $obfs_ibx); + PublicInbox::WwwStream->response($ctx, 200, sub { my ($nr, undef) = @_; if ($nr == 1) { - $tip . multipart_text_as_html($mime, '') . - '
' . html_footer($hdr, 1, $ctx) . - '' . msg_reply($ctx, $hdr); + # fake an EOF if generating the footer fails; + # we want to at least show the message if something + # here crashes: + eval { + '
' . html_footer($hdr, 1, $ctx) . + '' . msg_reply($ctx, $hdr) + }; } else { undef } @@ -46,179 +51,365 @@ sub msg_reply { my ($ctx, $hdr) = @_; my $se_url = 'https://kernel.org/pub/software/scm/git/docs/git-send-email.html'; + my $p_url = + 'https://en.wikipedia.org/wiki/Posting_style#Interleaved_style'; + + my $info = ''; + my $ibx = $ctx->{-inbox}; + if (my $url = $ibx->{infourl}) { + $url = PublicInbox::Hval::prurl($ctx->{env}, $url); + $info = qq(\n List information: $url\n); + } - my ($arg, $link) = mailto_arg_link($hdr); - push @$arg, '/path/to/YOUR_REPLY'; + my ($arg, $link) = PublicInbox::Reply::mailto_arg_link($ibx, $hdr); + + # mailto: link only works if address obfuscation is disabled + if ($link) { + $link = <
Reply instructions: + +You may reply publically to this message via plain-text email +using any one of the following methods: + +* Save the following mbox file, import it into your mail client, + and reply-to-all from there: mbox + + Avoid top-posting and favor interleaved quoting: + $p_url +$info +* Reply to all the recipients using the --to, --cc, + and --in-reply-to switches of git-send-email(1): + + git send-email$arg + + $se_url +$link +EOF } sub in_reply_to { my ($hdr) = @_; - my $irt = $hdr->header_raw('In-Reply-To'); - - return mid_clean($irt) if (defined $irt); - - my $refs = $hdr->header_raw('References'); - if ($refs && $refs =~ /<([^>]+)>\s*\z/s) { - return $1; + my %mid = map { $_ => 1 } $hdr->header_raw('Message-ID'); + my @refs = (($hdr->header_raw('References') || '') =~ /<([^>]+)>/g); + push(@refs, (($hdr->header_raw('In-Reply-To') || '') =~ /<([^>]+)>/g)); + while (defined(my $irt = pop @refs)) { + next if $mid{"<$irt>"}; + return $irt; } undef; } +sub _hdr_names_html ($$) { + my ($hdr, $field) = @_; + my $val = $hdr->header($field) or return ''; + ascii_html(join(', ', PublicInbox::Address::names($val))); +} + +sub nr_to_s ($$$) { + my ($nr, $singular, $plural) = @_; + return "0 $plural" if $nr == 0; + $nr == 1 ? "$nr $singular" : "$nr $plural"; +} + # this is already inside asub index_entry { - my ($mime, $level, $state) = @_; - my $midx = $state->{anchor_idx}++; - my $ctx = $state->{ctx}; + my ($mime, $ctx, $more) = @_; my $srch = $ctx->{srch}; my $hdr = $mime->header_obj; my $subj = $hdr->header('Subject'); my $mid_raw = mid_clean(mid_mime($mime)); - my $id = anchor_for($mid_raw); - my $seen = $state->{seen}; - $seen->{$id} = "#$id"; # save the anchor for children, later - - my $mid = PublicInbox::Hval->new_msgid($mid_raw); - my $from = PublicInbox::Address::from_name($hdr->header('From')); - - my $root_anchor = $state->{root_anchor} || ''; - my $path = $root_anchor ? '../../' : ''; - my $href = $mid->as_href; - my $irt = in_reply_to($hdr); - my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt; - - $from = ascii_html($from); - $subj = ascii_html($subj); - $subj = "$subj"; - $subj = "$subj" if $root_anchor eq $id; - - my $ts = _msg_date($hdr); - my $rv = ""; - $rv .= "$subj\n"; - my $txt = "${path}$href/raw"; - my $fh = $state->{fh}; - $fh->write($rv .= "- $from @ $ts UTC (raw)\n\n"); - - my $mhref = "${path}$href/"; - - # scan through all parts, looking for displayable text - msg_iter($mime, sub { index_walk($fh, $mhref, $_[0]) }); - $rv = "\n" . html_footer($hdr, 0, $ctx, "$path$href/#R"); - - if (defined $irt) { - unless (defined $parent_anchor) { - my $v = PublicInbox::Hval->new_msgid($irt, 1); - $v = $v->as_href; - $parent_anchor = "${path}$v/"; + my $id = id_compress($mid_raw, 1); + my $id_m = 'm'.$id; + + my $root_anchor = $ctx->{root_anchor} || ''; + my $irt; + my $obfs_ibx = $ctx->{-obfs_ibx}; + + my $rv = "* "; + $subj = ''.ascii_html($subj).''; + obfuscate_addrs($obfs_ibx, $subj) if $obfs_ibx; + $subj = "$subj" if $root_anchor eq $id_m; + $rv .= $subj . "\n"; + $rv .= _th_index_lite($mid_raw, \$irt, $id, $ctx); + my @tocc; + foreach my $f (qw(To Cc)) { + my $dst = _hdr_names_html($hdr, $f); + if ($dst ne '') { + obfuscate_addrs($obfs_ibx, $dst) if $obfs_ibx; + push @tocc, "$f: $dst"; } - $rv .= " parent"; } - if (my $pct = $state->{pct}) { # used by SearchView.pm - $rv .= " [relevance $pct->{$mid_raw}%]"; - } elsif ($srch) { - my $threaded = 'threaded'; + my $from = _hdr_names_html($hdr, 'From'); + obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx; + $rv .= "From: $from @ "._msg_date($hdr)." UTC"; + my $upfx = $ctx->{-upfx}; + my $mhref = $upfx . mid_escape($mid_raw) . '/'; + $rv .= qq{ (permalink / }; + $rv .= qq{raw)\n}; + $rv .= ' '.join('; +', @tocc) . "\n" if @tocc; + + my $mapping = $ctx->{mapping}; + if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) { + my $mirt = PublicInbox::Hval->new_msgid($irt); + my $href = $upfx . $mirt->{href}. '/'; + my $html = $mirt->as_html; + $rv .= qq(In-Reply-To: <$html>\n) + } + $rv .= "\n"; + + # scan through all parts, looking for displayable text + msg_iter($mime, sub { $rv .= add_text_body($mhref, $obfs_ibx, $_[0]) }); + + # add the footer + $rv .= "\n^ ". + "permalink" . + " raw" . + " reply"; + + my $hr; + if (my $pct = $ctx->{pct}) { # used by SearchView.pm + $rv .= "\t[relevance $pct->{$mid_raw}%]"; + $hr = 1; + } elsif ($mapping) { + my $nested = 'nested'; my $flat = 'flat'; my $end = ''; if ($ctx->{flat}) { + $hr = 1; $flat = "$flat"; - $end = "\n"; # for lynx } else { - $threaded = "$threaded"; + $nested = "$nested"; } - $rv .= " [$threaded"; - $rv .= "|$flat]$end"; + $rv .= "\t[$flat"; + $rv .= "|$nested]"; + $rv .= " $ctx->{s_nr}"; + } else { + $hr = $ctx->{-hr}; } - $fh->write($rv .= ''); + + $rv .= $more ? '' : '' if $hr; + $rv; } -sub thread_html { - my ($ctx, $foot, $srch) = @_; - # $_[0] in sub is the Plack callback - sub { emit_thread_html($_[0], $ctx, $foot, $srch) } +sub pad_link ($$;$) { + my ($mid, $level, $s) = @_; + $s ||= '...'; + my $id = id_compress($mid, 1); + (' 'x19).indent_for($level).th_pfx($level)."($s)\n"; } -# only private functions below. +sub _th_index_lite { + my ($mid_raw, $irt, $id, $ctx) = @_; + my $rv = ''; + my $mapping = $ctx->{mapping} or return $rv; + my $pad = ' '; + my $mid_map = $mapping->{$mid_raw}; + defined $mid_map or + return 'public-inbox BUG: '.ascii_html($mid_raw).' not mapped'; + my ($attr, $node, $idx, $level) = @$mid_map; + my $children = $node->{children}; + my $nr_c = scalar @$children; + my $nr_s = 0; + my $siblings; + if (my $smsg = $node->{smsg}) { + ($$irt) = (($smsg->{references} || '') =~ m/<([^>]+)>\z/); + } + my $irt_map = $mapping->{$$irt} if defined $$irt; + if (defined $irt_map) { + $siblings = $irt_map->[1]->{children}; + $nr_s = scalar(@$siblings) - 1; + $rv .= $pad . $irt_map->[0]; + if ($idx > 0) { + my $prev = $siblings->[$idx - 1]; + my $pmid = $prev->{id}; + if ($idx > 2) { + my $s = ($idx - 1). ' preceding siblings ...'; + $rv .= pad_link($pmid, $level, $s); + } elsif ($idx == 2) { + my $ppmid = $siblings->[0]->{id}; + $rv .= $pad . $mapping->{$ppmid}->[0]; + } + $rv .= $pad . $mapping->{$pmid}->[0]; + } + } + my $s_s = nr_to_s($nr_s, 'sibling', 'siblings'); + my $s_c = nr_to_s($nr_c, 'reply', 'replies'); + $attr =~ s!\n\z!\n!s; + $attr =~ s! !!s; # no point in duplicating subject + $attr =~ s!]+>([^<]+)!$1!s; # no point linking to self + $rv .= "@ $attr"; + if ($nr_c) { + my $cmid = $children->[0]->{id}; + $rv .= $pad . $mapping->{$cmid}->[0]; + if ($nr_c > 2) { + my $s = ($nr_c - 1). ' more replies'; + $rv .= pad_link($cmid, $level + 1, $s); + } elsif (my $cn = $children->[1]) { + $rv .= $pad . $mapping->{$cn->{id}}->[0]; + } + } -sub emit_thread_html { - my ($res, $ctx, $foot, $srch) = @_; - my $mid = $ctx->{mid}; - my $flat = $ctx->{flat}; - my $msgs = load_results($srch->get_thread($mid, { asc => $flat })); - my $nr = scalar @$msgs; - return missing_thread($res, $ctx) if $nr == 0; - my $seen = {}; - my $state = { - res => $res, - ctx => $ctx, - seen => $seen, - root_anchor => anchor_for($mid), - anchor_idx => 0, - cur_level => 0, - }; - - require PublicInbox::Git; - $ctx->{git} ||= PublicInbox::Git->new($ctx->{git_dir}); - if ($flat) { - pre_anchor_entry($seen, $_) for (@$msgs); - __thread_entry($state, $_, 0) for (@$msgs); - } else { - my $th = thread_results($msgs); - thread_entry($state, $_, 0) for $th->rootset; - if (my $max = $state->{cur_level}) { - $state->{fh}->write( - ('' x ($max - 1)) . ''); + my $next = $siblings->[$idx+1] if $siblings && $idx >= 0; + if ($next) { + my $nmid = $next->{id}; + $rv .= $pad . $mapping->{$nmid}->[0]; + my $nnext = $nr_s - $idx; + if ($nnext > 2) { + my $s = ($nnext - 1).' subsequent siblings'; + $rv .= pad_link($nmid, $level, $s); + } elsif (my $nn = $siblings->[$idx + 2]) { + $rv .= $pad . $mapping->{$nn->{id}}->[0]; } } + $rv .= $pad ."$s_s, $s_c; $ctx->{s_nr}\n"; +} - # there could be a race due to a message being deleted in git - # but still being in the Xapian index: - my $fh = delete $state->{fh} or return missing_thread($res, $ctx); +sub walk_thread { + my ($rootset, $ctx, $cb) = @_; + my @q = map { (0, $_, -1) } @$rootset; + while (@q) { + my ($level, $node, $i) = splice(@q, 0, 3); + defined $node or next; + $cb->($ctx, $level, $node, $i); + ++$level; + $i = 0; + unshift @q, map { ($level, $_, $i++) } @{$node->{children}}; + } +} - my $final_anchor = $state->{anchor_idx}; - my $next = ""; - $next .= $final_anchor == 1 ? 'only message in' : 'end of'; - $next .= " thread, back to index"; - $next .= "\ndownload thread: "; - $next .= "mbox.gz"; - $next .= " / follow: Atom feed"; - $fh->write('' . $next . "\n\n". - $foot . '