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));
31 $self->{references} // '',
41 sub load_from_data ($$) {
42 my ($self) = $_[0]; # data = $_[1]
49 # To: and Cc: are stored to optimize HDR/XHDR in NNTP since
50 # some NNTP clients will use that for message displays.
51 # NNTP only, and only stored in Over(view), not Xapian
61 ) = split(/\n/, $_[1]);
65 my ($self, $doc) = @_;
66 my $data = $doc->get_data or return;
67 $self->{ts} = get_val($doc, PublicInbox::Search::TS());
68 my $dt = get_val($doc, PublicInbox::Search::DT());
69 my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $dt);
70 $self->{ds} = timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
71 load_from_data($self, $data);
78 # ghosts don't have ->{from}
79 my $from = delete($self->{from}) // '';
80 my @n = PublicInbox::Address::names($from);
81 $self->{from_name} = join(', ', @n);
83 # drop NNTP-only fields which aren't relevant to PSGI results:
84 # saves ~80K on a 200 item search result:
85 delete @$self{qw(ts to cc bytes lines)};
89 # for Import and v1 non-SQLite WWW code paths
91 my ($self, $hdr, $sync) = @_;
92 for my $f (qw(From To Cc Subject)) {
93 my @all = $hdr->header($f);
94 my $val = join(', ', @all);
96 # MIME decoding can create NULs, replace them with spaces
97 # to protect git and NNTP clients
100 # rare: in case headers have wide chars (not RFC2047-encoded)
103 # lower-case fields for read-only stuff
104 $self->{lc($f)} = $val;
106 # Capitalized From/Subject for git-fast-import
107 next if $f eq 'To' || $f eq 'Cc';
108 if (scalar(@all) > 1) {
111 $val =~ tr/\0\t\n/ /;
113 $self->{$f} = $val if $val ne '';
116 $self->{-ds} = [ my @ds = msg_datestamp($hdr, $sync->{autime}) ];
117 $self->{-ts} = [ my @ts = msg_timestamp($hdr, $sync->{cotime}) ];
118 $self->{ds} //= $ds[0]; # no zone
119 $self->{ts} //= $ts[0];
121 # for v1 users w/o SQLite
122 $self->{mid} //= eval { mids($hdr)->[0] } // '';
125 # no strftime, that is locale-dependent and not for RFC822
126 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
127 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
129 sub date ($) { # for NNTP
131 my $ds = $self->{ds};
132 return unless defined $ds;
133 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
134 "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
135 $mday, $year+1900, $hour, $min, $sec);
138 sub internaldate { # for IMAP
140 my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($self->{ts} // 0);
141 sprintf("%02d-$MoY[$mon]-%04d %02d:%02d:%02d +0000",
142 $mday, $year+1900, $hour, $min, $sec);
145 our $REPLY_RE = qr/^re:\s+/i;
147 sub subject_normalized ($) {
149 $subj =~ s/\A\s+//s; # no leading space
150 $subj =~ s/\s+\z//s; # no trailing space
151 $subj =~ s/\s+/ /gs; # no redundant spaces
152 $subj =~ s/\.+\z//; # no trailing '.'
153 $subj =~ s/$REPLY_RE//igo; # remove reply prefix