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(mids);
16 use PublicInbox::Address;
17 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
18 use Time::Local qw(timegm);
22 # sortable_unserialise is defined by PublicInbox::Search::load_xapian()
23 sortable_unserialise($doc->get_value($col));
41 sub load_from_data ($$) {
42 my ($self) = $_[0]; # data = $_[1]
48 # To: and Cc: are stored to optimize HDR/XHDR in NNTP since
49 # some NNTP clients will use that for message displays.
50 # NNTP only, and only stored in Over(view), not Xapian
60 ) = split(/\n/, $_[1]);
64 my ($self, $doc) = @_;
65 my $data = $doc->get_data or return;
66 $self->{ts} = get_val($doc, PublicInbox::Search::TS());
67 my $dt = get_val($doc, PublicInbox::Search::DT());
68 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $dt);
69 $self->{ds} = timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
71 load_from_data($self, $data);
77 from_name($self); # fill in {from_name} so we can delete {from}
79 # drop NNTP-only fields which aren't relevant to PSGI results:
80 # saves ~80K on a 200 item search result:
81 delete @$self{qw(from ts to cc bytes lines)};
85 # Only called by PSGI interface, not NNTP
87 my ($mitem, $srch) = @_;
88 return $srch->retry_reopen(\&from_mitem, $mitem) if $srch;
89 my $self = bless {}, __PACKAGE__;
90 psgi_cull(load_expand($self, $mitem->get_document));
93 # :bytes and :lines metadata in RFC 3977
94 sub bytes ($) { $_[0]->{bytes} }
95 sub lines ($) { $_[0]->{lines} }
98 my ($self, $field) = @_;
102 # for Import and v1 non-SQLite WWW code paths
104 my ($self, $hdr, $v2w) = @_;
105 for my $f (qw(From To Cc Subject)) {
106 my @all = $hdr->header($f);
107 my $val = join(', ', @all);
109 # MIME decoding can create NULs, replace them with spaces
110 # to protect git and NNTP clients
111 $val =~ tr/\0\t\n/ /;
113 # lower-case fields for read-only stuff
114 $self->{lc($f)} = $val;
116 # Capitalized From/Subject for git-fast-import
117 next if $f eq 'To' || $f eq 'Cc';
118 if (scalar(@all) > 1) {
121 $val =~ tr/\0\t\n/ /;
123 $self->{$f} = $val if $val ne '';
126 $self->{-ds} = [ my @ds = msg_datestamp($hdr, $v2w->{autime}) ];
127 $self->{-ts} = [ my @ts = msg_timestamp($hdr, $v2w->{cotime}) ];
128 $self->{ds} //= $ds[0]; # no zone
129 $self->{ts} //= $ts[0];
131 # for v1 users w/o SQLite
132 $self->{mid} //= eval { mids($hdr)->[0] } // '';
135 sub subject ($) { __hdr($_[0], 'Subject') }
136 sub to ($) { __hdr($_[0], 'To') }
137 sub cc ($) { __hdr($_[0], 'Cc') }
139 # no strftime, that is locale-dependent and not for RFC822
140 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
141 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
145 my $ds = $self->{ds};
146 return unless defined $ds;
147 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
148 "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
149 $mday, $year+1900, $hour, $min, $sec);
155 my $from = __hdr($self, 'From');
156 if (defined $from && !defined $self->{from_name}) {
157 my @n = PublicInbox::Address::names($from);
158 $self->{from_name} = join(', ', @n);
165 my $from_name = $self->{from_name};
166 return $from_name if defined $from_name;
173 my $x = $self->{references};
174 defined $x ? $x : '';
177 sub mid { $_[0]->{mid} }
179 our $REPLY_RE = qr/^re:\s+/i;
181 sub subject_normalized ($) {
183 $subj =~ s/\A\s+//s; # no leading space
184 $subj =~ s/\s+\z//s; # no trailing space
185 $subj =~ s/\s+/ /gs; # no redundant spaces
186 $subj =~ s/\.+\z//; # no trailing '.'
187 $subj =~ s/$REPLY_RE//igo; # remove reply prefix