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_mime/;
16 use PublicInbox::Address;
17 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
18 use Time::Local qw(timegm);
21 my ($class, $mid) = @_;
22 bless { mid => $mid }, $class;
27 # sortable_unserialise is defined by PublicInbox::Search::load_xapian()
28 sortable_unserialise($doc->get_value($col));
46 sub load_from_data ($$) {
47 my ($self) = $_[0]; # data = $_[1]
53 # To: and Cc: are stored to optimize HDR/XHDR in NNTP since
54 # some NNTP clients will use that for message displays.
55 # NNTP only, and only stored in Over(view), not Xapian
65 ) = split(/\n/, $_[1]);
69 my ($self, $doc) = @_;
70 my $data = $doc->get_data or return;
71 $self->{ts} = get_val($doc, PublicInbox::Search::TS());
72 my $dt = get_val($doc, PublicInbox::Search::DT());
73 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $dt);
74 $self->{ds} = timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
76 load_from_data($self, $data);
82 from_name($self); # fill in {from_name} so we can delete {from}
84 # drop NNTP-only fields which aren't relevant to PSGI results:
85 # saves ~80K on a 200 item search result:
86 delete @$self{qw(from ts to cc bytes lines)};
90 # Only called by PSGI interface, not NNTP
92 my ($mitem, $srch) = @_;
93 return $srch->retry_reopen(\&from_mitem, $mitem) if $srch;
94 my $self = bless {}, __PACKAGE__;
95 psgi_cull(load_expand($self, $mitem->get_document));
98 # :bytes and :lines metadata in RFC 3977
99 sub bytes ($) { $_[0]->{bytes} }
100 sub lines ($) { $_[0]->{lines} }
103 my ($self, $field) = @_;
104 $self->{lc($field)} //= do {
105 my $mime = $self->{mime} or return;
106 my $val = join(', ', $mime->header($field));
113 # for Import and v1 WWW code paths
115 my ($self, $hdr, $v2w) = @_;
116 for my $f (qw(From To Cc Subject)) {
117 my @all = $hdr->header($f);
118 my $val = join(', ', @all);
120 # MIME decoding can create NULs, replace them with spaces
121 # to protect git and NNTP clients
122 $val =~ tr/\0\t\n/ /;
124 # lower-case fields for read-only stuff
125 $self->{lc($f)} = $val;
127 # Capitalized From/Subject for git-fast-import
128 next if $f eq 'To' || $f eq 'Cc';
129 if (scalar(@all) > 1) {
132 $val =~ tr/\0\t\n/ /;
134 $self->{$f} = $val if $val ne '';
137 $self->{-ds} = [ my @ds = msg_datestamp($hdr, $v2w->{autime}) ];
138 $self->{-ts} = [ my @ts = msg_timestamp($hdr, $v2w->{cotime}) ];
139 $self->{ds} //= $ds[0]; # no zone
140 $self->{ts} //= $ts[0];
143 sub subject ($) { __hdr($_[0], 'Subject') }
144 sub to ($) { __hdr($_[0], 'To') }
145 sub cc ($) { __hdr($_[0], 'Cc') }
147 # no strftime, that is locale-dependent and not for RFC822
148 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
149 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
153 my $ds = $self->{ds};
154 return unless defined $ds;
155 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
156 "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
157 $mday, $year+1900, $hour, $min, $sec);
163 my $from = __hdr($self, 'From');
164 if (defined $from && !defined $self->{from_name}) {
165 my @n = PublicInbox::Address::names($from);
166 $self->{from_name} = join(', ', @n);
173 my $from_name = $self->{from_name};
174 return $from_name if defined $from_name;
181 $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0;
186 $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0;
191 my $x = $self->{references};
192 defined $x ? $x : '';
196 my ($self, $mid) = @_;
200 } elsif (defined(my $rv = $self->{mid})) {
203 die "NO {mime} for mid\n" unless $self->{mime};
204 mid_mime($self->{mime}) # v1 w/o Xapian
208 our $REPLY_RE = qr/^re:\s+/i;
210 sub subject_normalized ($) {
212 $subj =~ s/\A\s+//s; # no leading space
213 $subj =~ s/\s+\z//s; # no trailing space
214 $subj =~ s/\s+/ /gs; # no redundant spaces
215 $subj =~ s/\.+\z//; # no trailing '.'
216 $subj =~ s/$REPLY_RE//igo; # remove reply prefix