1 # Copyright (C) 2015-2019 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;
30 Search::Xapian::sortable_unserialise($doc->get_value($col));
34 my ($self, $oid, $mid0) = @_;
35 $oid = '' unless defined $oid;
49 sub load_from_data ($$) {
50 my ($self) = $_[0]; # data = $_[1]
56 # To: and Cc: are stored to optimize HDR/XHDR in NNTP since
57 # some NNTP clients will use that for message displays.
58 # NNTP only, and only stored in Over(view), not Xapian
68 ) = split(/\n/, $_[1]);
72 my ($self, $doc) = @_;
73 my $data = $doc->get_data or return;
74 $self->{ts} = get_val($doc, PublicInbox::Search::TS());
75 my $dt = get_val($doc, PublicInbox::Search::DT());
76 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $dt);
77 $self->{ds} = timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
79 load_from_data($self, $data);
85 from_name($self); # fill in {from_name} so we can delete {from}
87 # drop NNTP-only fields which aren't relevant to PSGI results:
88 # saves ~80K on a 200 item search result:
89 delete @$self{qw(from ts to cc bytes lines)};
93 # Only called by PSGI interface, not NNTP
95 my ($class, $doc) = @_;
96 my $self = bless {}, $class;
97 psgi_cull(load_expand($self, $doc));
100 # :bytes and :lines metadata in RFC 3977
101 sub bytes ($) { $_[0]->{bytes} }
102 sub lines ($) { $_[0]->{lines} }
105 my ($self, $field) = @_;
106 my $val = $self->{$field};
107 return $val if defined $val;
109 my $mime = $self->{mime} or return;
110 $val = $mime->header($field);
111 $val = '' unless defined $val;
114 $self->{$field} = $val;
117 sub subject ($) { __hdr($_[0], 'subject') }
118 sub to ($) { __hdr($_[0], 'to') }
119 sub cc ($) { __hdr($_[0], 'cc') }
121 # no strftime, that is locale-dependent and not for RFC822
122 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
123 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
127 my $ds = $self->{ds};
128 return unless defined $ds;
129 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
130 "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
131 $mday, $year+1900, $hour, $min, $sec);
137 my $from = __hdr($self, 'from');
138 if (defined $from && !defined $self->{from_name}) {
139 my @n = PublicInbox::Address::names($from);
140 $self->{from_name} = join(', ', @n);
147 my $from_name = $self->{from_name};
148 return $from_name if defined $from_name;
155 $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0;
160 $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0;
165 my $x = $self->{references};
166 defined $x ? $x : '';
170 my ($self, $mid) = @_;
174 } elsif (defined(my $rv = $self->{mid})) {
177 die "NO {mime} for mid\n" unless $self->{mime};
178 $self->_extract_mid; # v1 w/o Xapian
182 sub _extract_mid { mid_clean(mid_mime($_[0]->{mime})) }
184 our $REPLY_RE = qr/^re:\s+/i;
186 sub subject_normalized ($) {
188 $subj =~ s/\A\s+//s; # no leading space
189 $subj =~ s/\s+\z//s; # no trailing space
190 $subj =~ s/\s+/ /gs; # no redundant spaces
191 $subj =~ s/\.+\z//; # no trailing '.'
192 $subj =~ s/$REPLY_RE//igo; # remove reply prefix