use warnings;
use Search::Xapian;
use Email::Address qw//;
+use Email::Simple qw//;
use POSIX qw//;
use Date::Parse qw/str2time/;
use PublicInbox::MID qw/mid_clean mid_compressed/;
+use Encode qw/find_encoding/;
+my $enc_utf8 = find_encoding('UTF-8');
our $PFX2TERM_RE = undef;
sub new {
sub load_doc {
my ($class, $doc) = @_;
- my ($mid, $subj, $from, $date) = split(/\n/, $doc->get_data);
+ my $data = $doc->get_data;
+ $data = $enc_utf8->decode($data);
+ my ($mid, $subj, $from, $date) = split(/\n/, $data);
bless {
doc => $doc,
mid => $mid,
my @from;
if ($from) {
+ $from =~ tr/\n/ /;
@from = Email::Address->parse($from);
$self->{from} = $from[0];
$from = $from[0]->name;
}
- $from =~ tr/\n/ /;
$self->{from_name} = $from;
$self->{from};
}
}
}
+# for threading only
+sub mini_mime {
+ my ($self) = @_;
+ $self->ensure_metadata;
+ my @h = (
+ Subject => $self->subject,
+ 'X-PI-From' => $self->from_name,
+ 'X-PI-Date' => $self->date,
+ 'X-PI-TS' => $self->ts,
+ 'Message-ID' => "<$self->{mid}>",
+ );
+ if (my $refs = $self->{references}) {
+ push @h, References => '<' . join('> <', @$refs) . '>';
+ }
+ if (my $irt = $self->{inreplyto}) {
+ push @h, 'In-Reply-To' => "<$irt>";
+ }
+
+ Email::MIME->create(header_str => \@h);
+}
+
sub mid {
my ($self, $mid) = @_;
# public functions:
sub msg_html {
- my ($class, $mime, $full_pfx, $footer) = @_;
+ my ($class, $mime, $full_pfx, $footer, $srch) = @_;
if (defined $footer) {
$footer = "\n" . $footer;
} else {
headers_to_html_header($mime, $full_pfx) .
multipart_text_as_html($mime, $full_pfx) .
'</pre><hr />' . PRE_WRAP .
- html_footer($mime, 1) . $footer .
+ html_footer($mime, 1, $full_pfx, $srch) . $footer .
'</pre></body></html>';
}
}
sub html_footer {
- my ($mime, $standalone) = @_;
+ my ($mime, $standalone, $full_pfx, $srch) = @_;
my %cc; # everyone else
my $to; # this is the From address
my $subj = $mime->header('Subject') || '';
$subj = "Re: $subj" unless $subj =~ /\bRe:/;
- my $irp = uri_escape_utf8(
- $mime->header_obj->header_raw('Message-ID') || '');
+ my $mid = $mime->header_obj->header_raw('Message-ID');
+ my $irp = uri_escape_utf8($mid);
delete $cc{$to};
$to = uri_escape_utf8($to);
$subj = uri_escape_utf8($subj);
my $cc = uri_escape_utf8(join(',', sort values %cc));
my $href = "mailto:$to?In-Reply-To=$irp&Cc=${cc}&Subject=$subj";
+ my $irt = '';
my $idx = $standalone ? " <a\nhref=\"../\">index</a>" : '';
+ if ($idx && $srch) {
+ my $res = $srch->get_replies($mid);
+ if (my $c = $res->{count}) {
+ $c = $c == 1 ? '1 reply' : "$c replies";
+ $idx .= "\n$c:\n";
+ thread_replies(\$idx, $mime, $res);
+ } else {
+ $idx .= "\n(no replies yet)\n";
+ }
+ $irt = $mime->header_obj->header_raw('In-Reply-To');
+ if ($irt) {
+ $irt = PublicInbox::Hval->new_msgid($irt);
+ $irt = $irt->as_href;
+ $irt = "<a\nhref=\"$irt\">parent</a> ";
+ } else {
+ $irt = ' ' x length('parent ');
+ }
+ }
- "<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
+ "$irt<a\nhref=\"" . ascii_html($href) . '">reply</a>' . $idx;
}
sub linkify_refs {
'm' . mid_compressed(mid_clean($msgid));
}
+# children are chronological
+sub simple_sort_children {
+ sort {
+ (eval { $a->topmost->message->header('X-PI-TS') } || 0) <=>
+ (eval { $b->topmost->message->header('X-PI-TS') } || 0)
+ } @_;
+}
+
+sub simple_dump {
+ my ($dst, $root, $node, $level) = @_;
+ $$dst .= ' ' x $level;
+ if (my $x = $node->message) {
+ my $mid = $x->header('Message-ID');
+ if ($root->[0] ne $mid) {
+ my $s = clean_subj($x->header('Subject'));
+ if ($root->[1] eq $s) {
+ $s = ' ';
+ } else {
+ $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;
+ $$dst .= "` <a\nhref=\"$m\">$s$f @ $d UTC</a>\n";
+ }
+ }
+ simple_dump($dst, $root, $node->child, $level + 1) if $node->child;
+ simple_dump($dst, $root, $node->next, $level) if $node->next;
+}
+
+sub clean_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+/ /;
+ $subj;
+}
+
+sub thread_replies {
+ my ($dst, $root, $res) = @_;
+ my @msgs = map { $_->mini_mime } @{$res->{msgs}};
+ require PublicInbox::Thread;
+ $root->header_set('X-PI-TS', '0');
+ my $th = PublicInbox::Thread->new($root, @msgs);
+ $th->thread;
+ $th->order(sub { simple_sort_children(@_) });
+ $root = [ $root->header('Message-ID'),
+ clean_subj($root->header('Subject')) ];
+ simple_dump($dst, $root, $_, 0) for $th->rootset;
+}
+
1;
my $pfx = "../f/$mid_href.html";
my $foot = footer($ctx);
require Email::MIME;
+ my $mime = Email::MIME->new($x);
+ my $srch = searcher($ctx);
[ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
- [ PublicInbox::View->msg_html(Email::MIME->new($x), $pfx, $foot) ] ];
+ [ PublicInbox::View->msg_html($mime, $pfx, $foot, $srch) ] ];
}
# /$LISTNAME/f/$MESSAGE_ID.html -> HTML content (fullquotes)
my $x = mid2blob($ctx);
return r404() unless $x;
require PublicInbox::View;
- require Email::MIME;
my $foot = footer($ctx);
+ require Email::MIME;
+ my $mime = Email::MIME->new($x);
+ my $srch = searcher($ctx);
[ 200, [ 'Content-Type' => 'text/html; charset=UTF-8' ],
- [ PublicInbox::View->msg_html(Email::MIME->new($x), undef, $foot)] ];
+ [ PublicInbox::View->msg_html($mime, undef, $foot, $srch)] ];
}
sub self_url {
);
}
+# search support is optional, returns undef if Xapian is not installed
+# or not configured for the given GIT_DIR
+sub searcher {
+ my ($ctx) = @_;
+ eval {
+ require PublicInbox::Search;
+ PublicInbox::Search->new($ctx->{git_dir});
+ };
+}
+
1;