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 # There may be many of these objects loaded in memory at once
7 # for large threads in our WWW UI.
8 package PublicInbox::SearchMsg;
11 use PublicInbox::MID qw/mid_clean mid_mime/;
12 use PublicInbox::Address;
13 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
14 use Time::Local qw(timegm);
17 my ($class, $mime) = @_;
18 bless { mime => $mime }, $class;
22 my ($class, $mid) = @_;
23 bless { mid => $mid }, $class;
27 my ($class, $head, $db, $mid) = @_;
28 my $doc_id = $head->get_docid;
29 load_expand(wrap($class, $mid), $db->get_document($doc_id));
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.
62 # NNTP only, and only stored in Over(view), not Xapian
72 ) = split(/\n/, $_[1]);
76 my ($self, $doc) = @_;
77 my $data = $doc->get_data or return;
78 $self->{ts} = get_val($doc, PublicInbox::Search::TS());
79 my $dt = get_val($doc, PublicInbox::Search::DT());
80 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $dt);
81 $self->{ds} = timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
83 load_from_data($self, $data);
89 from_name($self); # fill in {from_name} so we can delete {from}
91 # drop NNTP-only fields which aren't relevant to PSGI results:
92 # saves ~80K on a 200 item search result:
93 delete @$self{qw(from ts to cc bytes lines)};
97 # Only called by PSGI interface, not NNTP
99 my ($class, $doc) = @_;
100 my $self = bless {}, $class;
101 psgi_cull(load_expand($self, $doc));
104 # :bytes and :lines metadata in RFC 3977
105 sub bytes ($) { $_[0]->{bytes} }
106 sub lines ($) { $_[0]->{lines} }
109 my ($self, $field) = @_;
110 my $val = $self->{$field};
111 return $val if defined $val;
113 my $mime = $self->{mime} or return;
114 $val = $mime->header($field);
115 $val = '' unless defined $val;
118 $self->{$field} = $val;
121 sub subject ($) { __hdr($_[0], 'subject') }
122 sub to ($) { __hdr($_[0], 'to') }
123 sub cc ($) { __hdr($_[0], 'cc') }
125 # no strftime, that is locale-dependent and not for RFC822
126 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
127 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
131 my $ds = $self->{ds};
132 return unless defined $ds;
133 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
134 "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
135 $mday, $year+1900, $hour, $min, $sec);
141 my $from = __hdr($self, 'from');
142 if (defined $from && !defined $self->{from_name}) {
143 my @n = PublicInbox::Address::names($from);
144 $self->{from_name} = join(', ', @n);
151 my $from_name = $self->{from_name};
152 return $from_name if defined $from_name;
159 $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0;
164 $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0;
169 my $x = $self->{references};
170 defined $x ? $x : '';
174 my ($self, $mid) = @_;
178 } elsif (defined(my $rv = $self->{mid})) {
181 die "NO {mime} for mid\n" unless $self->{mime};
182 $self->_extract_mid; # v1 w/o Xapian
186 sub _extract_mid { mid_clean(mid_mime($_[0]->{mime})) }