1 # Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
4 # A small/skeleton/slim representation of a message.
6 # This used to be "SearchMsg", but we split out overview
7 # indexing into over.sqlite3 so it's not just "search". There
8 # may be many of these objects loaded in memory at once for
9 # large threads in our WWW UI and the NNTP range responses.
10 package PublicInbox::Smsg;
13 use base qw(Exporter);
14 our @EXPORT_OK = qw(subject_normalized);
15 use PublicInbox::MID qw/mid_clean mid_mime/;
16 use PublicInbox::Address;
17 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
18 use Time::Local qw(timegm);
21 my ($class, $mime) = @_;
22 bless { mime => $mime }, $class;
26 my ($class, $mid) = @_;
27 bless { mid => $mid }, $class;
32 # sortable_unserialise is defined by PublicInbox::Search::load_xapian()
33 sortable_unserialise($doc->get_value($col));
51 sub load_from_data ($$) {
52 my ($self) = $_[0]; # data = $_[1]
58 # To: and Cc: are stored to optimize HDR/XHDR in NNTP since
59 # some NNTP clients will use that for message displays.
60 # NNTP only, and only stored in Over(view), not Xapian
70 ) = split(/\n/, $_[1]);
74 my ($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);
87 from_name($self); # fill in {from_name} so we can delete {from}
89 # drop NNTP-only fields which aren't relevant to PSGI results:
90 # saves ~80K on a 200 item search result:
91 delete @$self{qw(from ts to cc bytes lines)};
95 # Only called by PSGI interface, not NNTP
97 my ($mitem, $srch) = @_;
98 return $srch->retry_reopen(\&from_mitem, $mitem) if $srch;
99 my $self = bless {}, __PACKAGE__;
100 psgi_cull(load_expand($self, $mitem->get_document));
103 # :bytes and :lines metadata in RFC 3977
104 sub bytes ($) { $_[0]->{bytes} }
105 sub lines ($) { $_[0]->{lines} }
108 my ($self, $field) = @_;
109 my $val = $self->{$field};
110 return $val if defined $val;
112 my $mime = $self->{mime} or return;
113 my @raw = $mime->header($field);
114 $val = join(', ', @raw);
117 $self->{$field} = $val;
120 sub subject ($) { __hdr($_[0], 'subject') }
121 sub to ($) { __hdr($_[0], 'to') }
122 sub cc ($) { __hdr($_[0], 'cc') }
124 # no strftime, that is locale-dependent and not for RFC822
125 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
126 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
130 my $ds = $self->{ds};
131 return unless defined $ds;
132 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
133 "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
134 $mday, $year+1900, $hour, $min, $sec);
140 my $from = __hdr($self, 'from');
141 if (defined $from && !defined $self->{from_name}) {
142 my @n = PublicInbox::Address::names($from);
143 $self->{from_name} = join(', ', @n);
150 my $from_name = $self->{from_name};
151 return $from_name if defined $from_name;
158 $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0;
163 $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0;
168 my $x = $self->{references};
169 defined $x ? $x : '';
173 my ($self, $mid) = @_;
177 } elsif (defined(my $rv = $self->{mid})) {
180 die "NO {mime} for mid\n" unless $self->{mime};
181 $self->_extract_mid; # v1 w/o Xapian
185 sub _extract_mid { mid_clean(mid_mime($_[0]->{mime})) }
187 our $REPLY_RE = qr/^re:\s+/i;
189 sub subject_normalized ($) {
191 $subj =~ s/\A\s+//s; # no leading space
192 $subj =~ s/\s+\z//s; # no trailing space
193 $subj =~ s/\s+/ /gs; # no redundant spaces
194 $subj =~ s/\.+\z//; # no trailing '.'
195 $subj =~ s/$REPLY_RE//igo; # remove reply prefix