require POSIX;
use Time::Local qw(timegm);
+use constant COLS => 72;
use constant INDENT => ' ';
use constant TCHILD => '` ';
sub th_pfx ($) { $_[0] == 0 ? '' : TCHILD };
my ($ctx) = @_;
my $mid = $ctx->{mid};
my $ibx = $ctx->{-inbox};
- my ($first, $more, $head, $tail, $db);
+ my ($first, $more);
my $smsg;
if (my $srch = $ibx->search) {
- $srch->retry_reopen(sub {
- ($head, $tail, $db) = $srch->each_smsg_by_mid($mid);
- for (; !defined($first) && $head != $tail; $head++) {
- my @args = ($head, $db, $mid);
- $smsg = PublicInbox::SearchMsg->get(@args);
- $first = $ibx->msg_by_smsg($smsg);
- }
- if ($head != $tail) {
- $more = [ $head, $tail, $db ];
- }
- });
+ my ($id, $prev);
+ $smsg = $srch->next_by_mid($mid, \$id, \$prev);
+ $first = $ibx->msg_by_smsg($smsg) if $smsg;
+ if ($first) {
+ my $next = $srch->next_by_mid($mid, \$id, \$prev);
+ $more = [ $id, $prev, $next ] if $next;
+ }
return unless $first;
} else {
$first = $ibx->msg_by_mid($mid) or return;
sub msg_html_more {
my ($ctx, $more, $nr) = @_;
my $str = eval {
- my $smsg;
- my ($head, $tail, $db) = @$more;
+ my ($id, $prev, $smsg) = @$more;
my $mid = $ctx->{mid};
- for (; !defined($smsg) && $head != $tail; $head++) {
- my $m = PublicInbox::SearchMsg->get($head, $db, $mid);
- $smsg = $ctx->{-inbox}->smsg_mime($m);
- }
- if ($head == $tail) { # done
- @$more = ();
- } else {
- $more->[0] = $head;
- }
+ $smsg = $ctx->{-inbox}->smsg_mime($smsg);
+ my $next = $ctx->{srch}->next_by_mid($mid, \$id, \$prev);
+ @$more = $next ? ($id, $prev, $next) : ();
if ($smsg) {
my $mime = $smsg->{mime};
my $upfx = '../' . mid_escape($smsg->mid) . '/';
$refs->[-1];
}
+sub fold_addresses ($) {
+ return $_[0] if length($_[0]) <= COLS;
+ # try to fold on commas after non-word chars before $lim chars,
+ # Try to get the "," preceeded by ">" or ")", but avoid folding
+ # on the comma where somebody uses "Lastname, Firstname".
+ # We also try to keep the last and penultimate addresses in
+ # the list on the same line if possible, hence the extra \z
+ # Fall back to folding on spaces at $lim + 1 chars
+ my $lim = COLS - 8; # 8 = "\t" display width
+ my $too_long = $lim + 1;
+ $_[0] =~ s/\s*\z//s; # Email::Simple doesn't strip trailing spaces
+ $_[0] = join("\n\t",
+ ($_[0] =~ /(.{0,$lim}\W(?:,|\z)|
+ .{1,$lim}(?:,|\z)|
+ .{1,$lim}|
+ .{$too_long,}?)(?:\s|\z)/xgo));
+}
+
sub _hdr_names_html ($$) {
my ($hdr, $field) = @_;
my $val = $hdr->header($field) or return '';
my @tocc;
my $mime = $smsg->{mime};
my $hdr = $mime->header_obj;
- 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 $from = _hdr_names_html($hdr, 'From');
obfuscate_addrs($obfs_ibx, $from) if $obfs_ibx;
$rv .= "From: $from @ ".fmt_ts($smsg->ds)." UTC";
my $mhref = $upfx . mid_escape($mid_raw) . '/';
$rv .= qq{ (<a\nhref="$mhref">permalink</a> / };
$rv .= qq{<a\nhref="${mhref}raw">raw</a>)\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 .= '; <b>+Cc:</b> '.$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)))) {
$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: <a\nhref="#r"\nid=t>);
- $rv .= $v->as_html . "</a>\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: <a\nhref="#r"\nid=t>$v</a>\n);
+ } else {
+ $rv .= "Subject: $v\n";
+ }
+ $title[0] = $v;
+ } else { # dummy anchor for thread skeleton at bottom of page
+ $rv .= qq(<a\nhref="#r"\nid=t></a>) 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($_) ;
pagination_footer($ctx, '.')
}
-sub paginate_recent ($) {
- my ($ctx) = @_;
+sub paginate_recent ($$) {
+ my ($ctx, $lim) = @_;
my $t = $ctx->{qp}->{t} || '';
- my $lim = 200; # this is our window
my $opts = { limit => $lim };
my ($after, $before);
sub index_topics {
my ($ctx) = @_;
- my $msgs = paginate_recent($ctx);
+ my $msgs = paginate_recent($ctx, 200); # 200 is our window
if (@$msgs) {
walk_thread(thread_results($ctx, $msgs), $ctx, *acc_topic);
}