use Encode::MIME::Header;
use Email::MIME::ContentType qw/parse_content_type/;
use PublicInbox::Hval;
-use PublicInbox::MID qw/mid_clean mid_compressed mid2path/;
+use PublicInbox::MID qw/mid_clean mid_compress mid2path/;
use Digest::SHA;
require POSIX;
# public functions:
sub msg_html {
- my ($class, $mime, $full_pfx, $footer, $srch) = @_;
+ my ($ctx, $mime, $full_pfx, $footer) = @_;
if (defined $footer) {
$footer = "\n" . $footer;
} else {
$footer = '';
}
- headers_to_html_header($mime, $full_pfx, $srch) .
+ headers_to_html_header($mime, $full_pfx, $ctx) .
multipart_text_as_html($mime, $full_pfx) .
'</pre><hr />' . PRE_WRAP .
- html_footer($mime, 1, $full_pfx, $srch) . $footer .
+ html_footer($mime, 1, $full_pfx, $ctx) .
+ $footer .
'</pre></body></html>';
}
}
# this is already inside a <pre>
-# state = [ time, seen = {}, first_commit, page_nr = 0 ]
sub index_entry {
- my (undef, $mime, $level, $state) = @_;
- my ($srch, $seen, $first_commit) = @$state;
- my $midx = $state->[3]++;
+ my ($fh, $mime, $level, $state) = @_;
+ my $midx = $state->{anchor_idx}++;
+ my $ctx = $state->{ctx};
+ my $srch = $ctx->{srch};
my ($prev, $next) = ($midx - 1, $midx + 1);
- my $rv = '';
my $part_nr = 0;
- my $enc_msg = enc_for($mime->header("Content-Type"));
+ my $enc = enc_for($mime->header("Content-Type"));
my $subj = $mime->header('Subject');
my $header_obj = $mime->header_obj;
- my $mid_raw = $header_obj->header_raw('Message-ID');
+ my $mid_raw = $header_obj->header('Message-ID');
my $id = anchor_for($mid_raw);
+ my $seen = $state->{seen};
$seen->{$id} = "#$id"; # save the anchor for later
my $mid = PublicInbox::Hval->new_msgid($mid_raw);
my $from = PublicInbox::Hval->new_oneline($mime->header('From'))->raw;
my @from = Email::Address->parse($from);
$from = $from[0]->name;
- (defined($from) && length($from)) or $from = $from[0]->address;
$from = PublicInbox::Hval->new_oneline($from)->as_html;
$subj = PublicInbox::Hval->new_oneline($subj)->as_html;
- my $pfx = (' ' x $level);
- my $root_anchor = $seen->{root_anchor};
- my $path;
my $more = 'permalink';
- if ($root_anchor) {
- $path = '../';
- } else {
- $path = '';
- }
+ my $root_anchor = $state->{root_anchor};
+ my $path = $root_anchor ? '../../' : '';
my $href = $mid->as_href;
- my $irt = $header_obj->header_raw('In-Reply-To');
- my ($anchor_idx, $anchor, $t_anchor);
+ my $irt = $header_obj->header('In-Reply-To');
+ my ($anchor_idx, $anchor);
if (defined $irt) {
$anchor_idx = anchor_for($irt);
$anchor = $seen->{$anchor_idx};
- $t_anchor = T_ANCHOR;
- } else {
- $t_anchor = '';
}
-
- if (defined $srch) {
- $subj = "<a\nhref=\"${path}t/$href.html#u\">$subj</a>";
+ if ($srch) {
+ $subj = "<a\nhref=\"${path}t/$href/#u\">$subj</a>";
}
if ($root_anchor && $root_anchor eq $id) {
$subj = "<u\nid=\"u\">$subj</u>";
}
my $ts = $mime->header('X-PI-TS');
- my $fmt = '%Y-%m-%d %H:%M UTC';
- $ts = POSIX::strftime($fmt, gmtime($ts));
+ unless (defined $ts) {
+ $ts = msg_timestamp($mime);
+ }
+ $ts = POSIX::strftime('%Y-%m-%d %H:%M', gmtime($ts));
- $rv .= "$pfx<b\nid=\"$id\">$subj</b>\n$pfx";
- $rv .= "- by $from @ $ts - ";
- $rv .= "<a\nid=\"s$midx\"\nhref=\"#s$next\">next</a>";
+ my $rv = "<table\nsummary=l$level><tr>";
+ if ($level) {
+ $rv .= '<td><pre>' . (' ' x $level) . '</pre></td>';
+ }
+ $rv .= "<td\nid=s$midx>" . PRE_WRAP;
+ $rv .= "<b\nid=\"$id\">$subj</b>\n";
+ $rv .= "- by $from @ $ts UTC - ";
+ $rv .= "<a\nhref=\"#s$next\">next</a>";
if ($prev >= 0) {
$rv .= "/<a\nhref=\"#s$prev\">prev</a>";
}
- $rv .= "\n\n";
+ $fh->write($rv .= "\n\n");
- my $mhref = "${path}m/$href.html";
- my $fhref = "${path}f/$href.html";
+ my ($fhref, $more_ref);
+ my $mhref = "${path}m/$href/";
+ if ($level > 0) {
+ $fhref = "${path}f/$href/";
+ $more_ref = \$more;
+ }
# scan through all parts, looking for displayable text
$mime->walk_parts(sub {
- $rv .= index_walk($_[0], $pfx, $enc_msg, $part_nr, $fhref,
- \$more);
- $part_nr++;
+ index_walk($fh, $_[0], $enc, \$part_nr, $fhref, $more_ref);
});
+ $mime->body_set('');
- $rv .= "\n$pfx<a\nhref=\"$mhref\">$more</a> ";
- my $txt = "${path}m/$href.txt";
- $rv .= "<a\nhref=\"$txt\">raw</a> ";
- $rv .= html_footer($mime, 0);
+ my $txt = "${path}m/$href/raw";
+ $rv = "\n<a\nhref=\"$mhref\">$more</a> <a\nhref=\"$txt\">raw</a> ";
+ $rv .= html_footer($mime, 0, undef, $ctx);
if (defined $irt) {
unless (defined $anchor) {
my $v = PublicInbox::Hval->new_msgid($irt);
$v = $v->as_href;
- $anchor = "${path}m/$v.html";
+ $anchor = "${path}m/$v/";
$seen->{$anchor_idx} = $anchor;
}
$rv .= " <a\nhref=\"$anchor\">parent</a>";
}
- if ($srch) {
- $rv .= " <a\nhref=\"${path}t/$href.html$t_anchor\">" .
- "threadlink</a>";
- }
-
- $rv . "\n\n";
+ $fh->write($rv .= '</pre></td></tr></table>');
}
sub thread_html {
- my (undef, $ctx, $foot, $srch) = @_;
- my $mid = mid_compressed($ctx->{mid});
- my $res = $srch->get_thread($mid);
- my $rv = '';
- my $msgs = load_results($ctx, $res);
- my $nr = scalar @$msgs;
- return $rv if $nr == 0;
- require PublicInbox::Thread;
- my $th = PublicInbox::Thread->new(@$msgs);
- $th->thread;
- $th->order(*PublicInbox::Thread::sort_ts);
- my $state = [ $srch, { root_anchor => anchor_for($mid) }, undef, 0 ];
- thread_entry(\$rv, $state, $_, 0) for $th->rootset;
- my $final_anchor = $state->[3];
- my $next = "<a\nid=\"s$final_anchor\">";
-
- if ($final_anchor == 1) {
- $next .= 'only message in thread';
- } else {
- $next .= 'end of thread';
- }
- $next .= ", back to <a\nhref=\"../\">index</a>\n";
-
- $rv .= "</pre><hr />" . PRE_WRAP . $next . $foot . "</pre>";
+ my ($ctx, $foot, $srch) = @_;
+ sub { emit_thread_html($_[0], $ctx, $foot, $srch) }
}
-sub subject_path_html {
- my (undef, $ctx, $foot, $srch) = @_;
- my $path = $ctx->{subject_path};
- my $res = $srch->get_subject_path($path);
- my $rv = '';
- my $msgs = load_results($ctx, $res);
- my $nr = scalar @$msgs;
- return $rv if $nr == 0;
- require PublicInbox::Thread;
- my $th = PublicInbox::Thread->new(@$msgs);
- $th->thread;
- $th->order(*PublicInbox::Thread::sort_ts);
- my $state = [ $srch, { root_anchor => 'dummy' }, undef, 0 ];
- thread_entry(\$rv, $state, $_, 0) for $th->rootset;
- my $final_anchor = $state->[3];
- my $next = "<a\nid=\"s$final_anchor\">end of thread</a>\n";
+# only private functions below.
- $rv .= "</pre><hr />" . PRE_WRAP . $next . $foot . "</pre>";
+sub emit_thread_html {
+ my ($cb, $ctx, $foot, $srch) = @_;
+ my $mid = mid_compress($ctx->{mid});
+ my $res = $srch->get_thread($mid);
+ my $msgs = load_results($res);
+ my $nr = scalar @$msgs;
+ return missing_thread($cb) if $nr == 0;
+ my $fh = $cb->([200,['Content-Type'=>'text/html; charset=UTF-8']]);
+ my $th = thread_results($msgs);
+ my $state = {
+ ctx => $ctx,
+ seen => {},
+ root_anchor => anchor_for($mid),
+ anchor_idx => 0,
+ };
+ {
+ require PublicInbox::GitCatFile;
+ my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
+ thread_entry($fh, $git, $state, $_, 0) for $th->rootset;
+ }
+ my $final_anchor = $state->{anchor_idx};
+ my $next = "<a\nid=\"s$final_anchor\">";
+ $next .= $final_anchor == 1 ? 'only message in' : 'end of';
+ $next .= " thread</a>, back to <a\nhref=\"../../\">index</a>\n";
+ $next .= "download: <a\nhref=\"mbox.gz\">mbox.gz</a>\n\n";
+ $fh->write("<hr />" . PRE_WRAP . $next . $foot .
+ "</pre></body></html>");
+ $fh->close;
}
-# only private functions below.
-
sub index_walk {
- my ($part, $pfx, $enc_msg, $part_nr, $fhref, $more) = @_;
- my $rv = '';
- return $rv if $part->subparts; # walk_parts already recurses
- my $ct = $part->content_type;
+ my ($fh, $part, $enc, $part_nr, $fhref, $more) = @_;
+ my $s = add_text_body($enc, $part, $part_nr, $fhref);
- # account for filter bugs...
- return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
+ if ($more) {
+ # drop the remainder of git patches, they're usually better
+ # to review when the full message is viewed
+ $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
- my $enc = enc_for($ct, $enc_msg);
-
- if ($part_nr > 0) {
- my $fn = $part->filename;
- defined($fn) or $fn = "part #" . ($part_nr + 1);
- $rv .= $pfx . add_filename_line($enc->decode($fn));
+ # Drop signatures
+ $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
}
- my $s = add_text_body_short($enc, $part, $part_nr, $fhref);
-
- # drop the remainder of git patches, they're usually better
- # to review when the full message is viewed
- $s =~ s!^---+\n.*\z!!ms and $$more = 'more...';
-
- # Drop signatures
- $s =~ s/^-- \n.*\z//ms and $$more = 'more...';
-
# kill any leading or trailing whitespace lines
$s =~ s/^\s*$//sgm;
$s =~ s/\s+\z//s;
- if (length $s) {
- # add prefix:
- $s =~ s/^/$pfx/sgm;
-
+ if ($s ne '') {
# kill per-line trailing whitespace
$s =~ s/[ \t]+$//sgm;
-
- $rv .= $s . "\n";
+ $s .= "\n" unless $s =~ /\n\z/s;
}
- $rv;
+ $fh->write($s);
}
sub enc_for {
my ($mime, $full_pfx, $srch) = @_;
my $rv = "";
my $part_nr = 0;
- my $enc_msg = enc_for($mime->header("Content-Type"));
+ my $enc = enc_for($mime->header("Content-Type"));
# scan through all parts, looking for displayable text
$mime->walk_parts(sub {
my ($part) = @_;
- return if $part->subparts; # walk_parts already recurses
- my $ct = $part->content_type;
-
- # account for filter bugs...
- return if defined $ct && $ct =~ m!\btext/[xh]+tml\b!i;
-
- my $enc = enc_for($ct, $enc_msg);
-
- if ($part_nr > 0) {
- my $fn = $part->filename;
- defined($fn) or $fn = "part #" . ($part_nr + 1);
- $rv .= add_filename_line($enc->decode($fn));
- }
-
- if (defined $full_pfx) {
- $rv .= add_text_body_short($enc, $part, $part_nr,
- $full_pfx);
- } else {
- $rv .= add_text_body_full($enc, $part, $part_nr);
- }
- $rv .= "\n" unless $rv =~ /\n\z/s;
- ++$part_nr;
+ $rv .= add_text_body($enc, $part, \$part_nr, $full_pfx);
});
+ $mime->body_set('');
$rv;
}
sub add_filename_line {
- my ($fn) = @_;
+ my ($enc, $fn) = @_;
my $len = 72;
my $pad = "-";
-
+ $fn = $enc->decode($fn);
$len -= length($fn);
$pad x= ($len/2) if ($len > 0);
"$pad " . ascii_html($fn) . " $pad\n";
}
-my $LINK_RE = qr!\b((?:ftp|https?|nntp)://[@\w\+\&\?\.\%\;/#=-]+)!;
+my $LINK_RE = qr!\b((?:ftp|https?|nntp)://
+ [\@:\w\.-]+/
+ ?[\@\w\+\&\?\.\%\;/#=-]*)!x;
sub linkify {
# no newlines added here since it'd break the splitting we do
# to fold quotes
- $_[0] =~ s!$LINK_RE!<a href="$1">$1</a>!g;
+ $_[0] =~ s!$LINK_RE!<a\nhref="$1">$1</a>!g;
}
-sub add_text_body_short {
- my ($enc, $part, $part_nr, $full_pfx) = @_;
- my $n = 0;
- my $s = ascii_html($enc->decode($part->body));
- linkify($s);
- $s =~ s!^((?:(?:>[^\n]*)\n)+)!
- my $cur = $1;
- my @lines = split(/\n/, $cur);
- if (@lines > MAX_INLINE_QUOTED) {
- # show a short snippet of quoted text
- $cur = join(' ', @lines);
- $cur =~ s/^>\s*//;
-
- my @sum = split(/\s+/, $cur);
- $cur = '';
- do {
- my $tmp = shift(@sum);
- my $len = length($tmp) + length($cur);
- if ($len > MAX_TRUNC_LEN) {
- @sum = ();
- } else {
- $cur .= $tmp . ' ';
- }
- } while (@sum && length($cur) < MAX_TRUNC_LEN);
- $cur =~ s/ \z/ .../;
- "> <<a\nhref=\"${full_pfx}#q${part_nr}_" . $n++ .
- "\">$cur<\/a>>\n";
- } else {
- $cur;
+sub flush_quote {
+ my ($quot, $n, $part_nr, $full_pfx, $final) = @_;
+
+ if ($full_pfx) {
+ if (!$final && scalar(@$quot) <= MAX_INLINE_QUOTED) {
+ # show quote inline
+ my $rv = join('', map { linkify($_); $_ } @$quot);
+ @$quot = ();
+ return $rv;
}
- !emg;
- $s;
+
+ # show a short snippet of quoted text and link to full version:
+ @$quot = map { s/^(?:>\s*)+//gm; $_ } @$quot;
+ my $cur = join(' ', @$quot);
+ @$quot = split(/\s+/, $cur);
+ $cur = '';
+ do {
+ my $tmp = shift(@$quot);
+ my $len = length($tmp) + length($cur);
+ if ($len > MAX_TRUNC_LEN) {
+ @$quot = ();
+ } else {
+ $cur .= $tmp . ' ';
+ }
+ } while (@$quot && length($cur) < MAX_TRUNC_LEN);
+ @$quot = ();
+ $cur =~ s/ \z/ .../s;
+ my $nr = ++$$n;
+ "> [<a\nhref=\"$full_pfx#q${part_nr}_$nr\">$cur</a>]\n";
+ } else {
+ # show everything in the full version with anchor from
+ # short version (see above)
+ my $nr = ++$$n;
+ my $rv = "<a\nid=q${part_nr}_$nr></a>";
+ $rv .= join('', map { linkify($_); $_ } @$quot);
+ @$quot = ();
+ $rv;
+ }
}
-sub add_text_body_full {
- my ($enc, $part, $part_nr) = @_;
+sub add_text_body {
+ my ($enc_msg, $part, $part_nr, $full_pfx) = @_;
+ return '' if $part->subparts;
+
+ my $ct = $part->content_type;
+ # account for filter bugs...
+ if (defined $ct && $ct =~ m!\btext/[xh]+tml\b!i) {
+ $part->body_set('');
+ return '';
+ }
+ my $enc = enc_for($ct, $enc_msg);
my $n = 0;
- my $s = ascii_html($enc->decode($part->body));
- linkify($s);
- $s =~ s!^((?:(?:>[^\n]*)\n)+)!
- my $cur = $1;
- my @lines = split(/\n/, $cur);
- if (@lines > MAX_INLINE_QUOTED) {
- "<a\nid=q${part_nr}_" . $n++ . ">$cur</a>";
+ my $nr = 0;
+ my $s = $part->body;
+ $part->body_set('');
+ $s = $enc->decode($s);
+ $s = ascii_html($s);
+ my @lines = split(/^/m, $s);
+ $s = '';
+
+ if ($$part_nr > 0) {
+ my $fn = $part->filename;
+ defined($fn) or $fn = "part #" . ($$part_nr + 1);
+ $s .= add_filename_line($enc, $fn);
+ }
+
+ my @quot;
+ while (defined(my $cur = shift @lines)) {
+ if ($cur !~ /^>/) {
+ # show the previously buffered quote inline
+ if (scalar @quot) {
+ $s .= flush_quote(\@quot, \$n, $$part_nr,
+ $full_pfx, 0);
+ }
+
+ # regular line, OK
+ linkify($cur);
+ $s .= $cur;
} else {
- $cur;
+ push @quot, $cur;
}
- !emg;
+ }
+ $s .= flush_quote(\@quot, \$n, $$part_nr, $full_pfx, 1) if scalar @quot;
+ $s .= "\n" unless $s =~ /\n\z/s;
+ ++$$part_nr;
$s;
}
sub headers_to_html_header {
- my ($mime, $full_pfx, $srch) = @_;
-
+ my ($mime, $full_pfx, $ctx) = @_;
+ my $srch = $ctx->{srch} if $ctx;
my $rv = "";
my @title;
my $header_obj = $mime->header_obj;
- my $mid = $header_obj->header_raw('Message-ID');
+ my $mid = $header_obj->header('Message-ID');
$mid = PublicInbox::Hval->new_msgid($mid);
my $mid_href = $mid->as_href;
foreach my $h (qw(From To Cc Subject Date)) {
my $v = $mime->header($h);
- defined($v) && length($v) or next;
+ defined($v) && ($v ne '') or next;
$v = PublicInbox::Hval->new_oneline($v);
if ($h eq 'From') {
} elsif ($h eq 'Subject') {
$title[0] = $v->as_html;
if ($srch) {
- $rv .= "$h: <a\nhref=\"../t/$mid_href.html\">";
+ $rv .= "$h: <a\nid=\"t\"\n" .
+ "href=\"../../t/$mid_href/\">";
$rv .= $v->as_html . "</a>\n";
next;
}
$rv .= "$h: " . $v->as_html . "\n";
}
-
$rv .= 'Message-ID: <' . $mid->as_html . '> ';
- $mid_href = "../m/$mid_href" unless $full_pfx;
- $rv .= "(<a\nhref=\"$mid_href.txt\">raw</a>)\n";
+ my $raw_ref = $full_pfx ? 'raw' : "../../m/$mid_href/raw";
+ $rv .= "(<a\nhref=\"$raw_ref\">raw</a>)\n";
+ if ($srch) {
+ $rv .= "<a\nhref=\"#r\">References: [see below]</a>\n";
+ } else {
+ $rv .= _parent_headers_nosrch($header_obj);
+ }
+ $rv .= "\n";
+
+ ("<html><head><title>". join(' - ', @title) .
+ '</title></head><body>' . PRE_WRAP . $rv);
+}
+
+sub thread_inline {
+ my ($dst, $ctx, $cur) = @_;
+ my $srch = $ctx->{srch};
+ my $mid = mid_compress(mid_clean($cur->header('Message-ID')));
+ my $res = $srch->get_thread($mid);
+ my $nr = $res->{total};
+
+ if ($nr <= 1) {
+ $$dst .= "\n[no followups, yet]</a>\n";
+ return;
+ }
+
+ $$dst .= "\n\n~$nr messages in thread: ".
+ "(<a\nhref=\"../../t/$mid/#u\">expand</a>)\n";
+ my $subj = $srch->subject_path($cur->header('Subject'));
+ my $state = {
+ seen => { $subj => 1 },
+ srch => $srch,
+ cur => $mid,
+ };
+ for (thread_results(load_results($res))->rootset) {
+ inline_dump($dst, $state, $_, 0);
+ }
+ $state->{next_msg};
+}
- my $irt = $header_obj->header_raw('In-Reply-To');
+sub _parent_headers_nosrch {
+ my ($header_obj) = @_;
+ my $rv = '';
+
+ my $irt = $header_obj->header('In-Reply-To');
if (defined $irt) {
my $v = PublicInbox::Hval->new_msgid($irt);
my $html = $v->as_html;
my $href = $v->as_href;
$rv .= "In-Reply-To: <";
- $rv .= "<a\nhref=\"$href.html\">$html</a>>\n";
+ $rv .= "<a\nhref=\"../$href/\">$html</a>>\n";
}
- my $refs = $header_obj->header_raw('References');
+ my $refs = $header_obj->header('References');
if ($refs) {
- $refs =~ s/\s*\Q$irt\E\s*// if (defined $irt);
- my @refs = ($refs =~ /<([^>]+)>/g);
+ # avoid redundant URLs wasting bandwidth
+ my %seen;
+ $seen{mid_clean($irt)} = 1 if defined $irt;
+ my @refs;
+ my @raw_refs = ($refs =~ /<([^>]+)>/g);
+ foreach my $ref (@raw_refs) {
+ next if $seen{$ref};
+ $seen{$ref} = 1;
+ push @refs, linkify_ref($ref);
+ }
+
if (@refs) {
- $rv .= 'References: '. linkify_refs(@refs) . "\n";
+ $rv .= 'References: '. join(' ', @refs) . "\n";
}
}
-
- $rv .= "\n";
-
- ("<html><head><title>". join(' - ', @title) .
- '</title></head><body>' . PRE_WRAP . $rv);
+ $rv;
}
sub html_footer {
- my ($mime, $standalone, $full_pfx, $srch) = @_;
+ my ($mime, $standalone, $full_pfx, $ctx) = @_;
my %cc; # everyone else
my $to; # this is the From address
foreach my $h (qw(From To Cc)) {
my $v = $mime->header($h);
- defined($v) && length($v) or next;
+ defined($v) && ($v ne '') or next;
my @addrs = Email::Address->parse($v);
foreach my $recip (@addrs) {
my $address = $recip->address;
Email::Address->purge_cache if $standalone;
my $subj = $mime->header('Subject') || '';
- $subj = "Re: $subj" unless $subj =~ /\bRe:/;
- my $mid = $mime->header_obj->header_raw('Message-ID');
+ $subj = "Re: $subj" unless $subj =~ /\bRe:/i;
+ my $mid = $mime->header('Message-ID');
my $irt = uri_escape_utf8($mid);
delete $cc{$to};
$to = uri_escape_utf8($to);
my $cc = uri_escape_utf8(join(',', sort values %cc));
my $href = "mailto:$to?In-Reply-To=$irt&Cc=${cc}&Subject=$subj";
- my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
+ my $srch = $ctx->{srch} if $ctx;
+ my $idx = $standalone ? " <a\nhref=\"../../\">index</a>" : '';
if ($idx && $srch) {
- $irt = $mime->header_obj->header_raw('In-Reply-To') || '';
- $mid = mid_compressed(mid_clean($mid));
- my $t_anchor = length $irt ? T_ANCHOR : '';
- $idx = " <a\nhref=\"../t/$mid.html$t_anchor\">".
- "threadlink</a>$idx";
- my $res = $srch->get_followups($mid);
- if (my $c = $res->{count}) {
- $c = $c == 1 ? '1 followup' : "$c followups";
- $idx .= "\n$c:\n";
- thread_followups(\$idx, $mime, $res);
- } else {
- $idx .= "\n(no followups, yet)\n";
- }
- if ($irt) {
+ my $next = thread_inline(\$idx, $ctx, $mime);
+ if (defined $irt) {
$irt = PublicInbox::Hval->new_msgid($irt);
$irt = $irt->as_href;
- $irt = "<a\nhref=\"$irt\">parent</a> ";
+ $irt = "<a\nhref=\"../$irt/\">parent</a> ";
} else {
$irt = ' ' x length('parent ');
}
+ if ($next) {
+ $irt .= "<a\nhref=\"../$next/\">next</a> ";
+ } else {
+ $irt .= ' ';
+ }
} else {
$irt = '';
}
"$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
}
-sub linkify_refs {
- join(' ', map {
- my $v = PublicInbox::Hval->new_msgid($_);
- my $html = $v->as_html;
- my $href = $v->as_href;
- "<<a\nhref=\"$href.html\">$html</a>>";
- } @_);
+sub linkify_ref {
+ my $v = PublicInbox::Hval->new_msgid($_[0]);
+ my $html = $v->as_html;
+ my $href = $v->as_href;
+ "<<a\nhref=\"../$href/\">$html</a>>";
}
sub anchor_for {
my ($msgid) = @_;
- 'm' . mid_compressed(mid_clean($msgid));
+ my $id = $msgid;
+ if ($id !~ /\A[a-f0-9]{40}\z/) {
+ $id = mid_compress(mid_clean($id), 1);
+ }
+ 'm' . $id;
}
-sub simple_dump {
- my ($dst, $root, $node, $level) = @_;
- my $pfx = ' ' x $level;
- $$dst .= $pfx;
- if (my $x = $node->message) {
- my $mid = $x->header('Message-ID');
- if ($root->[0] ne $mid) {
- my $s = $x->header('Subject');
- my $h = hash_subj($s);
- if ($root->[1]->{$h}) {
- $s = '';
- } else {
- $root->[1]->{$h} = 1;
- $s = PublicInbox::Hval->new($s);
- $s = $s->as_html;
- }
- my $m = PublicInbox::Hval->new_msgid($mid);
- my $f = PublicInbox::Hval->new($x->header('X-PI-From'));
- my $d = PublicInbox::Hval->new($x->header('X-PI-Date'));
- $m = $m->as_href . '.html';
- $f = $f->as_html;
- $d = $d->as_html . ' UTC';
- if (length($s) == 0) {
- $$dst .= "` <a\nhref=\"$m\">$f @ $d</a>\n";
- } else {
- $$dst .= "` <a\nhref=\"$m\">$s</a>\n" .
- "$pfx by $f @ $d\n";
+sub thread_html_head {
+ my ($mime) = @_;
+ my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
+ $s = $s->as_html;
+ "<html><head><title>$s</title></head><body>";
+}
+
+sub thread_entry {
+ my ($fh, $git, $state, $node, $level) = @_;
+ return unless $node;
+ if (my $mime = $node->message) {
+
+ # lazy load the full message from mini_mime:
+ my $path = mid2path(mid_clean($mime->header('Message-ID')));
+ $mime = eval { Email::MIME->new($git->cat_file("HEAD:$path")) };
+ if ($mime) {
+ if ($state->{anchor_idx} == 0) {
+ $fh->write(thread_html_head($mime));
}
+ index_entry($fh, $mime, $level, $state);
}
}
- simple_dump($dst, $root, $node->child, $level + 1) if $node->child;
- simple_dump($dst, $root, $node->next, $level) if $node->next;
+ thread_entry($fh, $git, $state, $node->child, $level + 1);
+ thread_entry($fh, $git, $state, $node->next, $level);
}
-sub hash_subj {
- my ($subj) = @_;
- $subj =~ s/\A\s+//;
- $subj =~ s/\s+\z//;
- $subj =~ s/^(?:re|aw):\s*//i; # remove reply prefix (aw: German)
- $subj =~ s/\s+/ /;
- Digest::SHA::sha1($subj);
+sub load_results {
+ my ($res) = @_;
+
+ [ map { $_->mini_mime } @{delete $res->{msgs}} ];
}
-sub thread_followups {
- my ($dst, $root, $res) = @_;
- my @msgs = map { $_->mini_mime } @{$res->{msgs}};
+sub msg_timestamp {
+ my ($mime) = @_;
+ my $ts = eval { str2time($mime->header('Date')) };
+ defined($ts) ? $ts : 0;
+}
+
+sub thread_results {
+ my ($msgs) = @_;
require PublicInbox::Thread;
- $root->header_set('X-PI-TS', '0');
- my $th = PublicInbox::Thread->new($root, @msgs);
+ my $th = PublicInbox::Thread->new(@$msgs);
$th->thread;
+ no warnings 'once';
$th->order(*PublicInbox::Thread::sort_ts);
- $root = [ $root->header('Message-ID'),
- { hash_subj($root->header('Subject')) => 1 } ];
- simple_dump($dst, $root, $_, 0) for $th->rootset;
+ $th
}
-sub thread_html_head {
- my ($mime) = @_;
- my $s = PublicInbox::Hval->new_oneline($mime->header('Subject'));
- $s = $s->as_html;
- "<html><head><title>$s</title></head><body>" . PRE_WRAP
-
+sub missing_thread {
+ my ($cb) = @_;
+ my $title = 'Thread does not exist';
+ $cb->([404, ['Content-Type' => 'text/html']])->write(<<EOF);
+<html><head><title>$title</title></head><body><pre>$title
+<a href="../../">Return to index</a></pre></body></html>
+EOF
}
-sub thread_entry {
- my ($dst, $state, $node, $level) = @_;
- # $state = [ $search_res, $seen, undef, 0 (msg_nr) ];
- # $seen is overloaded with 3 types of fields:
- # 1) "root_anchor" => anchor_for(Message-ID),
- # 2) seen subject hashes: sha1(subject) => 1
- # 3) anchors hashes: "#$sha1_hex" (same as $seen in index_entry)
- if (my $mime = $node->message) {
- if (length($$dst) == 0) {
- $$dst .= thread_html_head($mime);
+sub _inline_header {
+ my ($dst, $state, $mime, $level) = @_;
+ my $pfx = ' ' x $level;
+
+ my $cur = $state->{cur};
+ my $mid = $mime->header('Message-ID');
+ my $f = $mime->header('X-PI-From');
+ my $d = $mime->header('X-PI-Date');
+ $f = PublicInbox::Hval->new($f);
+ $d = PublicInbox::Hval->new($d);
+ $f = $f->as_html;
+ $d = $d->as_html . ' UTC';
+ my $midc = mid_compress(mid_clean($mid));
+ if ($cur) {
+ if ($cur eq $midc) {
+ delete $state->{cur};
+ $$dst .= "$pfx` <b><a\nid=\"r\"\nhref=\"#t\">".
+ "[this message]</a></b> by $f @ $d\n";
+
+ return;
}
- $$dst .= index_entry(undef, $mime, $level, $state);
+ } else {
+ $state->{next_msg} ||= $midc;
+ }
+
+ # Subject is never undef, this mail was loaded from
+ # our Xapian which would've resulted in '' if it were
+ # really missing (and Filter rejects empty subjects)
+ my $s = $mime->header('Subject');
+ my $h = $state->{srch}->subject_path($s);
+ if ($state->{seen}->{$h}) {
+ $s = undef;
+ } else {
+ $state->{seen}->{$h} = 1;
+ $s = PublicInbox::Hval->new($s);
+ $s = $s->as_html;
+ }
+ my $m = PublicInbox::Hval->new_msgid($mid);
+ $m = '../' . $m->as_href . '/';
+ if (defined $s) {
+ $$dst .= "$pfx` <a\nhref=\"$m\">$s</a>\n" .
+ "$pfx $f @ $d\n";
+ } else {
+ $$dst .= "$pfx` <a\nhref=\"$m\">$f @ $d</a>\n";
}
- thread_entry($dst, $state, $node->child, $level + 1) if $node->child;
- thread_entry($dst, $state, $node->next, $level) if $node->next;
}
-sub load_results {
- my ($ctx, $res) = @_;
-
- require PublicInbox::GitCatFile;
- my $git = PublicInbox::GitCatFile->new($ctx->{git_dir});
- my @msgs;
- while (my $smsg = shift @{$res->{msgs}}) {
- my $m = $smsg->mid;
- my $path = mid2path($m);
-
- # FIXME: duplicated code from Feed.pm
- my $mime = eval {
- my $str = $git->cat_file("HEAD:$path");
- Email::MIME->new($str);
- };
- unless ($@) {
- my $t = eval { str2time($mime->header('Date')) };
- defined($t) or $t = 0;
- $mime->header_set('X-PI-TS', $t);
- push @msgs, $mime;
- }
+sub inline_dump {
+ my ($dst, $state, $node, $level) = @_;
+ return unless $node;
+ return if $state->{stopped};
+ if (my $mime = $node->message) {
+ _inline_header($dst, $state, $mime, $level);
}
- \@msgs;
+ inline_dump($dst, $state, $node->child, $level+1);
+ inline_dump($dst, $state, $node->next, $level);
}
1;