X-Git-Url: http://www.git.stargrave.org/?a=blobdiff_plain;f=lib%2FPublicInbox%2FView.pm;h=7339467120205679175462e2a716f84588ef37df;hb=0f28e69ed76f6d0c14b0458019224c10590474df;hp=34ab3e58384e3640d2809bfd5c3fe52429037b7e;hpb=007cb8774e7013e2aaca32003bbd1653e3d8582f;p=public-inbox.git diff --git a/lib/PublicInbox/View.pm b/lib/PublicInbox/View.pm index 34ab3e58..73394671 100644 --- a/lib/PublicInbox/View.pm +++ b/lib/PublicInbox/View.pm @@ -9,13 +9,15 @@ use warnings; use PublicInbox::MsgTime qw(msg_datestamp); use PublicInbox::Hval qw/ascii_html obfuscate_addrs/; use PublicInbox::Linkify; -use PublicInbox::MID qw/mid_clean id_compress mid_mime mid_escape mids/; +use PublicInbox::MID qw/id_compress mid_escape mids references/; use PublicInbox::MsgIter; use PublicInbox::Address; use PublicInbox::WwwStream; use PublicInbox::Reply; require POSIX; +use Time::Local qw(timegm); +use constant COLS => 72; use constant INDENT => ' '; use constant TCHILD => '` '; sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD }; @@ -23,7 +25,7 @@ sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD }; # public functions: (unstable) sub msg_html { - my ($ctx, $mime, $more) = @_; + my ($ctx, $mime, $more, $smsg) = @_; my $hdr = $mime->header_obj; my $ibx = $ctx->{-inbox}; my $obfs_ibx = $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef; @@ -32,7 +34,9 @@ sub msg_html { PublicInbox::WwwStream->response($ctx, 200, sub { my ($nr, undef) = @_; if ($nr == 1) { - $tip . multipart_text_as_html($mime, '', $obfs_ibx) . + # $more cannot be true w/o $smsg being defined: + my $upfx = $more ? '../'.mid_escape($smsg->mid).'/' : ''; + $tip . multipart_text_as_html($mime, $upfx, $obfs_ibx) . '
sub index_entry { - my ($mime, $ctx, $more) = @_; + my ($smsg, $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 $subj = $smsg->subject; + my $mid_raw = $smsg->mid; my $id = id_compress($mid_raw, 1); my $id_m = 'm'.$id; @@ -211,21 +215,33 @@ sub index_entry { $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"; - } - } + my $mime = $smsg->{mime}; + my $hdr = $mime->header_obj; my $from = _hdr_names_html($hdr, 'From'); obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx; - $rv .= "From: $from @ "._msg_date($hdr)." UTC"; + $rv .= "From: $from @ ".fmt_ts($smsg->ds)." 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 $to = fold_addresses(_hdr_names_html($hdr, 'To')); + my $cc = fold_addresses(_hdr_names_html($hdr, 'Cc')); + my ($tlen, $clen) = (length($to), length($cc)); + my $to_cc = ''; + if (($tlen + $clen) > COLS) { + $to_cc .= ' To: '.$to."\n" if $tlen; + $to_cc .= ' Cc: '.$cc."\n" if $clen; + } else { + if ($tlen) { + $to_cc .= ' To: '.$to; + $to_cc .= '; +Cc: '.$cc if $clen; + } else { + $to_cc .= ' Cc: '.$cc if $clen; + } + $to_cc .= "\n"; + } + obfuscate_addrs($obfs_ibx, $to_cc) if $obfs_ibx; + $rv .= $to_cc; my $mapping = $ctx->{mapping}; if (!$mapping && (defined($irt) || defined($irt = in_reply_to($hdr)))) { @@ -363,30 +379,30 @@ sub pre_thread { } sub thread_index_entry { - my ($ctx, $level, $mime) = @_; + my ($ctx, $level, $smsg) = @_; my ($beg, $end) = thread_adj_level($ctx, $level); - $beg . '' . index_entry($mime, $ctx, 0) . '' . $end; + $beg . '' . index_entry($smsg, $ctx, 0) . '' . $end; } sub stream_thread ($$) { my ($rootset, $ctx) = @_; my $inbox = $ctx->{-inbox}; - my $mime; my @q = map { (0, $_) } @$rootset; my $level; + my $smsg; while (@q) { $level = shift @q; my $node = shift @q or next; my $cl = $level + 1; unshift @q, map { ($cl, $_) } @{$node->{children}}; - $mime = $inbox->msg_by_smsg($node->{smsg}) and last; + $smsg = $inbox->smsg_mime($node->{smsg}) and last; } - return missing_thread($ctx) unless $mime; + return missing_thread($ctx) unless $smsg; $ctx->{-obfs_ibx} = $inbox->{obfuscate} ? $inbox : undef; - $mime = PublicInbox::MIME->new($mime); - $ctx->{-title_html} = ascii_html($mime->header('Subject')); - $ctx->{-html_tip} = thread_index_entry($ctx, $level, $mime); + $ctx->{-title_html} = ascii_html($smsg->subject); + $ctx->{-html_tip} = thread_index_entry($ctx, $level, $smsg); + $smsg = undef; PublicInbox::WwwStream->response($ctx, 200, sub { return unless $ctx; while (@q) { @@ -394,10 +410,8 @@ sub stream_thread ($$) { my $node = shift @q or next; my $cl = $level + 1; unshift @q, map { ($cl, $_) } @{$node->{children}}; - my $mid = $node->{id}; - if ($mime = $inbox->msg_by_smsg($node->{smsg})) { - $mime = PublicInbox::MIME->new($mime); - return thread_index_entry($ctx, $level, $mime); + if ($smsg = $inbox->smsg_mime($node->{smsg})) { + return thread_index_entry($ctx, $level, $smsg); } else { return ghost_index_entry($ctx, $level, $node); } @@ -413,9 +427,7 @@ sub thread_html { my ($ctx) = @_; my $mid = $ctx->{mid}; my $srch = $ctx->{srch}; - my $sres = $srch->get_thread($mid); - my $msgs = load_results($srch, $sres); - my $nr = $sres->{total}; + my ($nr, $msgs) = $srch->get_thread($mid); return missing_thread($ctx) if $nr == 0; my $skel = ''; $skel .= $nr == 1 ? 'only message in thread' : 'end of thread'; @@ -434,7 +446,7 @@ sub thread_html { $ctx->{mapping} = {}; $ctx->{s_nr} = "$nr+ messages in thread"; - my $rootset = thread_results($msgs, $srch); + my $rootset = thread_results($ctx, $msgs); # reduce hash lookups in pre_thread->skel_dump my $inbox = $ctx->{-inbox}; @@ -445,24 +457,21 @@ sub thread_html { return stream_thread($rootset, $ctx) unless $ctx->{flat}; # flat display: lazy load the full message from smsg - my $mime; - while ($mime = shift @$msgs) { - $mime = $inbox->msg_by_smsg($mime) and last; + my $smsg; + while (my $m = shift @$msgs) { + $smsg = $inbox->smsg_mime($m) and last; } - return missing_thread($ctx) unless $mime; - $mime = PublicInbox::MIME->new($mime); - $ctx->{-title_html} = ascii_html($mime->header('Subject')); - $ctx->{-html_tip} = ''.index_entry($mime, $ctx, scalar @$msgs); - $mime = undef; + 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; - while ($mime = shift @$msgs) { - $mime = $inbox->msg_by_smsg($mime) and last; - } - if ($mime) { - $mime = PublicInbox::MIME->new($mime); - return index_entry($mime, $ctx, scalar @$msgs); + $smsg = undef; + while (my $m = shift @$msgs) { + $smsg = $inbox->smsg_mime($m) and last; } + return index_entry($smsg, $ctx, scalar @$msgs) if $smsg; $msgs = undef; $skel; }); @@ -611,29 +620,43 @@ sub _msg_html_prepare { $ctx->{-upfx} = '../'; } my @title; - foreach my $h (qw(From To Cc Subject Date)) { - my $v = $hdr->header($h); - defined($v) && ($v ne '') or next; + my $v; + if (defined($v = $hdr->header('From'))) { $v = PublicInbox::Hval->new($v); - - if ($h eq 'From') { - my @n = PublicInbox::Address::names($v->raw); - $title[1] = ascii_html(join(', ', @n)); - obfuscate_addrs($obfs_ibx, $title[1]) if $obfs_ibx; - } elsif ($h eq 'Subject') { - $title[0] = $v->as_html; - if ($srch) { - $rv .= qq($h: ); - $rv .= $v->as_html . "\n"; - next; - } - } + my @n = PublicInbox::Address::names($v->raw); + $title[1] = ascii_html(join(', ', @n)); $v = $v->as_html; + if ($obfs_ibx) { + obfuscate_addrs($obfs_ibx, $v); + obfuscate_addrs($obfs_ibx, $title[1]); + } + $rv .= "From: $v\n" if $v ne ''; + } + foreach my $h (qw(To Cc)) { + defined($v = $hdr->header($h)) or next; + fold_addresses($v); + $v = ascii_html($v); obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; - $rv .= "$h: $v\n"; - + $rv .= "$h: $v\n" if $v ne ''; + } + if (defined($v = $hdr->header('Subject')) && ($v ne '')) { + $v = ascii_html($v); + obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; + if ($srch) { + $rv .= qq(Subject: $v\n); + } else { + $rv .= "Subject: $v\n"; + } + $title[0] = $v; + } else { # dummy anchor for thread skeleton at bottom of page + $rv .= qq() if $srch; + $title[0] = '(no subject)'; + } + if (defined($v = $hdr->header('Date'))) { + $v = ascii_html($v); + obfuscate_addrs($obfs_ibx, $v) if $obfs_ibx; # possible :P + $rv .= "Date: $v\n"; } - $title[0] ||= '(no subject)'; $ctx->{-title_html} = join(' - ', @title); foreach (@$mids) { my $mid = PublicInbox::Hval->new_msgid($_) ; @@ -656,9 +679,8 @@ sub _msg_html_prepare { sub thread_skel { my ($dst, $ctx, $hdr, $tpfx) = @_; my $srch = $ctx->{srch}; - my $mid = mid_clean($hdr->header_raw('Message-ID')); - my $sres = $srch->get_thread($mid); - my $nr = $sres->{total}; + my $mid = mids($hdr)->[0]; + my ($nr, $msgs) = $srch->get_thread($mid); my $expand = qq(expand[flat) . qq(|nested] ) . qq(mbox.gz ) . @@ -688,12 +710,11 @@ sub thread_skel { $ctx->{prev_attr} = ''; $ctx->{prev_level} = 0; $ctx->{dst} = $dst; - $sres = load_results($srch, $sres); # reduce hash lookups in skel_dump my $ibx = $ctx->{-inbox}; $ctx->{-obfs_ibx} = $ibx->{obfuscate} ? $ibx : undef; - walk_thread(thread_results($sres, $srch), $ctx, *skel_dump); + walk_thread(thread_results($ctx, $msgs), $ctx, *skel_dump); $ctx->{parent_msg} = $parent; } @@ -809,16 +830,10 @@ sub indent_for { $level ? INDENT x ($level - 1) : ''; } -sub load_results { - my ($srch, $sres) = @_; - my $msgs = delete $sres->{msgs}; - $srch->retry_reopen(sub { [ map { $_->mid; $_ } @$msgs ] }); -} - sub thread_results { - my ($msgs, $srch) = @_; + my ($ctx, $msgs) = @_; require PublicInbox::SearchThread; - PublicInbox::SearchThread::thread($msgs, *sort_ds, $srch); + PublicInbox::SearchThread::thread($msgs, *sort_ds, $ctx->{-inbox}); } sub missing_thread { @@ -959,7 +974,7 @@ sub acc_topic { my ($ctx, $level, $node) = @_; my $srch = $ctx->{srch}; my $mid = $node->{id}; - my $x = $node->{smsg} || $srch->lookup_mail($mid); + my $x = $node->{smsg} || $ctx->{-inbox}->smsg_by_mid($mid); my ($subj, $ds); my $topic; if ($x) { @@ -1050,45 +1065,81 @@ sub dump_topics { 200; } +sub ts2str ($) { + my ($ts) = @_; + POSIX::strftime('%Y%m%d%H%M%S', gmtime($ts)); +} + +sub str2ts ($) { + my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $_[0]); + timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy); +} + +sub pagination_footer ($$) { + my ($ctx, $latest) = @_; + delete $ctx->{qp} or return; + my $next = $ctx->{next_page} || ''; + my $prev = $ctx->{prev_page} || ''; + if ($prev) { + $next = $next ? "$next " : ' '; + $prev .= qq! latest!; + } + "page: $next$prev"; +} + sub index_nav { # callback for WwwStream my (undef, $ctx) = @_; - delete $ctx->{qp} or return; - my ($next, $prev); - $next = $prev = ' '; - my $latest = ''; + pagination_footer($ctx, '.') +} - my $next_o = $ctx->{-next_o}; - if ($next_o) { - $next = qq!next!; +sub paginate_recent ($$) { + my ($ctx, $lim) = @_; + my $t = $ctx->{qp}->{t} || ''; + my $opts = { limit => $lim }; + my ($after, $before); + + # Xapian uses '..' but '-' is perhaps friendier to URL linkifiers + # if only $after exists "YYYYMMDD.." because "." could be skipped + # if interpreted as an end-of-sentence + $t =~ s/\A(\d{8,14})-// and $after = str2ts($1); + $t =~ /\A(\d{8,14})\z/ and $before = str2ts($1); + + my $ibx = $ctx->{-inbox}; + my $msgs = $ibx->recent($opts, $after, $before); + my $nr = scalar @$msgs; + if ($nr < $lim && defined($after)) { + $after = $before = undef; + $msgs = $ibx->recent($opts); + $nr = scalar @$msgs; } - if (my $cur_o = $ctx->{-cur_o}) { - $latest = qq! latest!; - - my $o = $cur_o - ($next_o - $cur_o); - if ($o > 0) { - $prev = qq!prev!; - } elsif ($o == 0) { - $prev = qq!prev!; + my $more = $nr == $lim; + my ($newest, $oldest); + if ($nr) { + $newest = $msgs->[0]->{ts}; + $oldest = $msgs->[-1]->{ts}; + # if we only had $after, our SQL query in ->recent ordered + if ($newest < $oldest) { + ($oldest, $newest) = ($newest, $oldest); + $more = 0 if defined($after) && $after < $oldest; } } - "page: $next $prev$latest"; + if (defined($oldest) && $more) { + my $s = ts2str($oldest); + $ctx->{next_page} = qq!next!; + } + if (defined($newest) && (defined($before) || defined($after))) { + my $s = ts2str($newest); + $ctx->{prev_page} = qq!prev!; + } + $msgs; } sub index_topics { my ($ctx) = @_; - my ($off) = (($ctx->{qp}->{o} || '0') =~ /(\d+)/); - my $opts = { offset => $off, limit => 200 }; - - $ctx->{order} = []; - my $srch = $ctx->{srch}; - my $sres = $srch->query('', $opts); - my $nr = scalar @{$sres->{msgs}}; - if ($nr) { - $sres = load_results($srch, $sres); - walk_thread(thread_results($sres, $srch), $ctx, *acc_topic); + my $msgs = paginate_recent($ctx, 200); # 200 is our window + if (@$msgs) { + walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic); } - $ctx->{-next_o} = $off+ $nr; - $ctx->{-cur_o} = $off; PublicInbox::WwwStream->response($ctx, dump_topics($ctx), *index_nav); }