]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/SearchMsg.pm
use both Date: and Received: times
[public-inbox.git] / lib / PublicInbox / SearchMsg.pm
index b8eee6651b47fe748275b37e237f794302311c38..de1fd1313aaf28e035f4bb93d2ae9e7e8135a1a6 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2016 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 # based on notmuch, but with no concept of folders, files or flags
 #
@@ -7,14 +7,14 @@ package PublicInbox::SearchMsg;
 use strict;
 use warnings;
 use Search::Xapian;
-use Date::Parse qw/str2time/;
-use PublicInbox::MID qw/mid_clean/;
+use PublicInbox::MID qw/mid_clean mid_mime/;
 use PublicInbox::Address;
+use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
 
 sub new {
        my ($class, $mime) = @_;
        my $doc = Search::Xapian::Document->new;
-       $doc->add_term(PublicInbox::Search::xpfx('type') . 'mail');
+       $doc->add_boolean_term('T' . 'mail');
 
        bless { type => 'mail', doc => $doc, mime => $mime }, $class;
 }
@@ -29,28 +29,41 @@ sub get_val ($$) {
        Search::Xapian::sortable_unserialise($doc->get_value($col));
 }
 
-sub load_doc {
-       my ($class, $doc) = @_;
+sub load_from_data ($$) {
+       my ($self) = $_[0]; # data = $_[1]
+       my ($subj, $from, $refs, $to, $cc, $blob, $mid0) = split(/\n/, $_[1]);
+       $self->{subject} = $subj;
+       $self->{from} = $from;
+       $self->{references} = $refs;
+       $self->{to} = $to;
+       $self->{cc} = $cc;
+       $self->{blob} = $blob;
+       $self->{mid} = $mid0;
+}
+
+sub load_expand {
+       my ($self) = @_;
+       my $doc = $self->{doc};
        my $data = $doc->get_data or return;
-       my $ts = get_val($doc, &PublicInbox::Search::TS);
+       $self->{ts} = get_val($doc, &PublicInbox::Search::TS);
+       $self->{ds} = get_val($doc, &PublicInbox::Search::DS);
        utf8::decode($data);
-       my ($subj, $from, $refs, $to, $cc, $blob) = split(/\n/, $data);
-       bless {
-               doc => $doc,
-               subject => $subj,
-               ts => $ts,
-               from => $from,
-               references => $refs,
-               to => $to,
-               cc => $cc,
-               blob => $blob,
-       }, $class;
+       load_from_data($self, $data);
+       $self;
+}
+
+sub load_doc {
+       my ($class, $doc) = @_;
+       my $self = bless { doc => $doc }, $class;
+       $self->load_expand;
 }
 
 # :bytes and :lines metadata in RFC 3977
 sub bytes ($) { get_val($_[0]->{doc}, &PublicInbox::Search::BYTES) }
 sub lines ($) { get_val($_[0]->{doc}, &PublicInbox::Search::LINES) }
-sub num ($) { get_val($_[0]->{doc}, &PublicInbox::Search::NUM) }
+sub num ($) {
+       $_[0]->{num} ||= get_val($_[0]->{doc}, PublicInbox::Search::NUM)
+}
 
 sub __hdr ($$) {
        my ($self, $field) = @_;
@@ -75,9 +88,9 @@ my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
 
 sub date ($) {
        my ($self) = @_;
-       my $ts = $self->{ts};
-       return unless defined $ts;
-       my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ts);
+       my $ds = $self->{ds};
+       return unless defined $ds;
+       my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
        "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
                                $mday, $year+1900, $hour, $min, $sec);
 
@@ -103,15 +116,20 @@ sub from_name {
 
 sub ts {
        my ($self) = @_;
-       $self->{ts} ||= eval { str2time($self->{mime}->header('Date')) } || 0;
+       $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0;
+}
+
+sub ds {
+       my ($self) = @_;
+       $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0;
 }
 
 sub to_doc_data {
-       my ($self, $blob) = @_;
+       my ($self, $oid, $mid0) = @_;
        my @rows = ($self->subject, $self->from, $self->references,
                        $self->to, $self->cc);
-       push @rows, $blob if defined $blob;
-       join("\n", @rows);
+       $oid = '' unless defined $oid;
+       join("\n", @rows, $oid, $mid0);
 }
 
 sub references {
@@ -163,4 +181,11 @@ sub path {
        $self->{path} = _get_term_val($self, 'XPATH', qr/\AXPATH/); # path
 }
 
+sub type {
+       my ($self) = @_;
+       my $type = $self->{type};
+       return $type if defined $type;
+       $self->{type} = _get_term_val($self, 'T', qr/\AT/);
+}
+
 1;