1 # Copyright (C) 2015-2018 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 # based on notmuch, but with no concept of folders, files or flags
5 # Wraps a document inside our Xapian search index.
6 package PublicInbox::SearchMsg;
9 use PublicInbox::MID qw/mid_clean mid_mime/;
10 use PublicInbox::Address;
11 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
12 use Time::Local qw(timegm);
15 my ($class, $mime) = @_;
16 my $doc = Search::Xapian::Document->new;
17 bless { doc => $doc, mime => $mime }, $class;
21 my ($class, $doc, $mid) = @_;
22 bless { doc => $doc, mime => undef, mid => $mid }, $class;
26 my ($class, $head, $db, $mid) = @_;
27 my $doc_id = $head->get_docid;
28 my $doc = $db->get_document($doc_id);
29 load_expand(wrap($class, $doc, $mid))
34 Search::Xapian::sortable_unserialise($doc->get_value($col));
38 my ($self, $oid, $mid0) = @_;
39 $oid = '' unless defined $oid;
53 sub load_from_data ($$) {
54 my ($self) = $_[0]; # data = $_[1]
60 # To: and Cc: are stored to optimize HDR/XHDR in NNTP since
61 # some NNTP clients will use that for message displays.
69 ) = split(/\n/, $_[1]);
74 my $doc = $self->{doc};
75 my $data = $doc->get_data or return;
76 $self->{ts} = get_val($doc, PublicInbox::Search::TS());
77 my $dt = get_val($doc, PublicInbox::Search::DT());
78 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $dt);
79 $self->{ds} = timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
81 load_from_data($self, $data);
86 my ($class, $doc) = @_;
87 my $self = bless { doc => $doc }, $class;
91 # :bytes and :lines metadata in RFC 3977
92 sub bytes ($) { $_[0]->{bytes} }
93 sub lines ($) { $_[0]->{lines} }
96 my ($self, $field) = @_;
97 my $val = $self->{$field};
98 return $val if defined $val;
100 my $mime = $self->{mime} or return;
101 $val = $mime->header($field);
102 $val = '' unless defined $val;
105 $self->{$field} = $val;
108 sub subject ($) { __hdr($_[0], 'subject') }
109 sub to ($) { __hdr($_[0], 'to') }
110 sub cc ($) { __hdr($_[0], 'cc') }
112 # no strftime, that is locale-dependent and not for RFC822
113 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
114 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
118 my $ds = $self->{ds};
119 return unless defined $ds;
120 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
121 "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
122 $mday, $year+1900, $hour, $min, $sec);
128 my $from = __hdr($self, 'from');
129 if (defined $from && !defined $self->{from_name}) {
130 my @n = PublicInbox::Address::names($from);
131 $self->{from_name} = join(', ', @n);
138 my $from_name = $self->{from_name};
139 return $from_name if defined $from_name;
146 $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0;
151 $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0;
156 my $x = $self->{references};
157 defined $x ? $x : '';
160 sub _get_term_val ($$$) {
161 my ($self, $pfx, $re) = @_;
162 my $doc = $self->{doc};
163 my $end = $doc->termlist_end;
164 my $i = $doc->termlist_begin;
167 my $val = $i->get_termname;
168 $val =~ s/$re// and return $val;
174 my ($self, $mid) = @_;
178 } elsif (my $rv = $self->{mid}) {
180 } elsif ($self->{doc}) {
181 $self->{mid} = _get_term_val($self, 'Q', qr/\AQ/);
183 $self->_extract_mid; # v1 w/o Xapian
187 sub _extract_mid { mid_clean(mid_mime($_[0]->{mime})) }