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 base qw(Exporter);
12 our @EXPORT_OK = qw(subject_normalized);
13 use PublicInbox::MID qw/mid_clean mid_mime/;
14 use PublicInbox::Address;
15 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
16 use Time::Local qw(timegm);
19 my ($class, $mime) = @_;
20 bless { mime => $mime }, $class;
24 my ($class, $mid) = @_;
25 bless { mid => $mid }, $class;
29 my ($class, $head, $db, $mid) = @_;
30 my $doc_id = $head->get_docid;
31 load_expand(wrap($class, $mid), $db->get_document($doc_id));
36 Search::Xapian::sortable_unserialise($doc->get_value($col));
40 my ($self, $oid, $mid0) = @_;
41 $oid = '' unless defined $oid;
55 sub load_from_data ($$) {
56 my ($self) = $_[0]; # data = $_[1]
62 # To: and Cc: are stored to optimize HDR/XHDR in NNTP since
63 # some NNTP clients will use that for message displays.
64 # NNTP only, and only stored in Over(view), not Xapian
74 ) = split(/\n/, $_[1]);
78 my ($self, $doc) = @_;
79 my $data = $doc->get_data or return;
80 $self->{ts} = get_val($doc, PublicInbox::Search::TS());
81 my $dt = get_val($doc, PublicInbox::Search::DT());
82 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $dt);
83 $self->{ds} = timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
85 load_from_data($self, $data);
91 from_name($self); # fill in {from_name} so we can delete {from}
93 # drop NNTP-only fields which aren't relevant to PSGI results:
94 # saves ~80K on a 200 item search result:
95 delete @$self{qw(from ts to cc bytes lines)};
99 # Only called by PSGI interface, not NNTP
101 my ($class, $doc) = @_;
102 my $self = bless {}, $class;
103 psgi_cull(load_expand($self, $doc));
106 # :bytes and :lines metadata in RFC 3977
107 sub bytes ($) { $_[0]->{bytes} }
108 sub lines ($) { $_[0]->{lines} }
111 my ($self, $field) = @_;
112 my $val = $self->{$field};
113 return $val if defined $val;
115 my $mime = $self->{mime} or return;
116 $val = $mime->header($field);
117 $val = '' unless defined $val;
120 $self->{$field} = $val;
123 sub subject ($) { __hdr($_[0], 'subject') }
124 sub to ($) { __hdr($_[0], 'to') }
125 sub cc ($) { __hdr($_[0], 'cc') }
127 # no strftime, that is locale-dependent and not for RFC822
128 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
129 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
133 my $ds = $self->{ds};
134 return unless defined $ds;
135 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
136 "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
137 $mday, $year+1900, $hour, $min, $sec);
143 my $from = __hdr($self, 'from');
144 if (defined $from && !defined $self->{from_name}) {
145 my @n = PublicInbox::Address::names($from);
146 $self->{from_name} = join(', ', @n);
153 my $from_name = $self->{from_name};
154 return $from_name if defined $from_name;
161 $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0;
166 $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0;
171 my $x = $self->{references};
172 defined $x ? $x : '';
176 my ($self, $mid) = @_;
180 } elsif (defined(my $rv = $self->{mid})) {
183 die "NO {mime} for mid\n" unless $self->{mime};
184 $self->_extract_mid; # v1 w/o Xapian
188 sub _extract_mid { mid_clean(mid_mime($_[0]->{mime})) }
190 our $REPLY_RE = qr/^re:\s+/i;
192 sub subject_normalized ($) {
194 $subj =~ s/\A\s+//s; # no leading space
195 $subj =~ s/\s+\z//s; # no trailing space
196 $subj =~ s/\s+/ /gs; # no redundant spaces
197 $subj =~ s/\.+\z//; # no trailing '.'
198 $subj =~ s/$REPLY_RE//igo; # remove reply prefix