X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FView.pm;h=9f1bf46005ad66c3332ae4e875b5e71cd96254da;hb=172416d1cd465da4242cc744a3f309d307f1311d;hp=d906276a63e8f27e49c45d64a7eff3fcbe8c73d5;hpb=dde6d1169d8f9d6aaede9730144d1f60be2134c8;p=public-inbox.git diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index d906276a..9f1bf460 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -8,11 +8,9 @@ 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::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; @@ -24,14 +22,13 @@ 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 { + PublicInbox::WwwStream->response($ctx, 200, sub { my ($nr, undef) = @_; if ($nr == 1) { - $tip . multipart_text_as_html($mime, '') . - '
". - "You may reply publically to this message via\n". - "plain-text email using any one of the following methods:\n\n" . - "* Save the following mbox file, import it into your mail client,\n" . - " and reply-to-all from there: mbox\n\n" . - "* Reply to all the recipients using the --to, --cc,\n" . - " and --in-reply-to switches of git-send-email(1):\n\n" . - " git send-email \\\n " . - join(" \\\n ", @$arg ). "\n\n" . - qq( $se_url\n\n) . - "* If your mail client supports setting the In-Reply-To" . - " header\n via mailto: links, try the " . - qq(mailto: link\n) . - ''; + $arg = ascii_html(join(" \\\n ", '', @$arg)); + <
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 + +* If your mail client supports setting the In-Reply-To header + via mailto: links, try the mailto: link+EOF } sub in_reply_to { my ($hdr) = @_; my $irt = $hdr->header_raw('In-Reply-To'); - return mid_clean($irt) if (defined $irt); + return mid_clean($irt) if defined $irt && $irt ne ''; my $refs = $hdr->header_raw('References'); if ($refs && $refs =~ /<([^>]+)>\s*\z/s) { @@ -89,151 +109,276 @@ sub _hdr_names ($$) { 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 a
sub 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 $id = id_compress($mid_raw, 1); + my $id_m = 'm'.$id; - my $root_anchor = $state->{root_anchor} || ''; - my $path = $root_anchor ? '../../' : ''; - my $href = $mid->as_href; + my $root_anchor = $ctx->{root_anchor} || ''; my $irt = in_reply_to($hdr); - my $parent_anchor = $seen->{anchor_for($irt)} if defined $irt; - - $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}; - my $from = _hdr_names($hdr, 'From'); - $rv .= "- $from @ $ts UTC (raw)\n"; + + my $rv = "* "; + $subj = ''.ascii_html($subj).''; + $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($hdr, $f); push @tocc, "$f: $dst" if $dst ne ''; } + $rv .= "From: "._hdr_names($hdr, '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; - $fh->write($rv .= "\n"); - my $mhref = "${path}$href/"; + my $mapping = $ctx->{mapping}; + if (!$mapping && $irt) { + 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 { 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/"; - } - $rv .= " parent"; - } - if (my $pct = $state->{pct}) { # used by SearchView.pm - $rv .= " [relevance $pct->{$mid_raw}%]"; - } elsif ($srch) { + msg_iter($mime, sub { $rv .= add_text_body($mhref, $_[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 $threaded = 'threaded'; my $flat = 'flat'; my $end = ''; if ($ctx->{flat}) { + $hr = 1; $flat = "$flat"; - $end = "\n"; # for lynx } else { $threaded = "$threaded"; } - $rv .= " [$threaded"; - $rv .= "|$flat]$end"; + $rv .= "\t[$flat"; + $rv .= "|$threaded]"; + $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"; +} + +sub _th_index_lite { + my ($mid_raw, $irt, $id, $ctx) = @_; + my $rv = ''; + my $mapping = $ctx->{mapping} or return $rv; + my $pad = ' '; + # map = [children, attr, node, idx, level] + my $map = $mapping->{$mid_raw}; + my $nr_c = scalar @{$map->[0]}; + my $nr_s = 0; + my $level = $map->[4]; + my $idx = $map->[3]; + my $irt_map = $mapping->{$irt} if defined $irt; + if (defined $irt_map) { + my $siblings = $irt_map->[0]; + $nr_s = scalar(@$siblings) - 1; + $rv .= $pad . $irt_map->[1]; + if ($idx > 0) { + my $prev = $siblings->[$idx - 1]; + my $pmid = $prev->messageid; + if ($idx > 2) { + my $s = ($idx - 1). ' preceding siblings ...'; + $rv .= pad_link($pmid, $level, $s); + } elsif ($idx == 2) { + my $ppmid = $siblings->[0]->messageid; + $rv .= $pad . $mapping->{$ppmid}->[1]; + } + $rv .= $pad . $mapping->{$pmid}->[1]; + } + } + my $s_s = nr_to_s($nr_s, 'sibling', 'siblings'); + my $s_c = nr_to_s($nr_c, 'reply', 'replies'); + my $this = $map->[1]; + $this =~ s!\n\z!\n!s; + $this =~ s! !!s; # no point in duplicating subject + $this =~ s!]+>([^<]+)!$1!s; # no point linking to self + $rv .= "@ $this"; + my $node = $map->[2]; + if (my $child = $node->child) { + my $cmid = $child->messageid; + $rv .= $pad . $mapping->{$cmid}->[1]; + if ($nr_c > 2) { + my $s = ($nr_c - 1). ' more replies'; + $rv .= pad_link($cmid, $level + 1, $s); + } elsif (my $cn = $child->next) { + $rv .= $pad . $mapping->{$cn->messageid}->[1]; + } + } + if (my $next = $node->next) { + my $nmid = $next->messageid; + $rv .= $pad . $mapping->{$nmid}->[1]; + my $nnext = $nr_s - $idx; + if ($nnext > 2) { + my $s = ($nnext - 1).' subsequent siblings'; + $rv .= pad_link($nmid, $level, $s); + } elsif (my $nn = $next->next) { + $rv .= $pad . $mapping->{$nn->messageid}->[1]; + } + } + $rv .= $pad ."$s_s, $s_c; $ctx->{s_nr}\n"; } sub walk_thread { - my ($th, $state, $cb) = @_; + my ($th, $ctx, $cb) = @_; my @q = map { (0, $_) } $th->rootset; while (@q) { my $level = shift @q; my $node = shift @q or next; - $cb->($state, $level, $node); + $cb->($ctx, $level, $node); unshift @q, $level+1, $node->child, $level, $node->next; } } -# only private functions below. - -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 { - walk_thread(thread_results($msgs), $state, *thread_entry); - if (my $max = $state->{cur_level}) { - $state->{fh}->write( - ('' x ($max - 1)) . ''); - } +sub pre_thread { + my ($ctx, $level, $node) = @_; + my $mapping = $ctx->{mapping}; + my $idx = -1; + if (my $parent = $node->parent) { + my $m = $mapping->{$parent->messageid}->[0]; + $idx = scalar @$m; + push @$m, $node; } + $mapping->{$node->messageid} = [ [], '', $node, $idx, $level ]; + skel_dump($ctx, $level, $node); +} - # 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); - - 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 . '