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);
24 $self->{references} // '',
34 sub load_from_data ($$) {
35 my ($self) = $_[0]; # data = $_[1]
42 # To: and Cc: are stored to optimize HDR/XHDR in NNTP since
43 # some NNTP clients will use that for message displays.
44 # NNTP only, and only stored in Over(view), not Xapian
54 ) = split(/\n/, $_[1]);
60 # ghosts don't have ->{from}
61 my $from = delete($self->{from}) // '';
62 my @n = PublicInbox::Address::names($from);
63 $self->{from_name} = join(', ', @n);
65 # drop NNTP-only fields which aren't relevant to PSGI results:
66 # saves ~80K on a 200 item search result:
67 # TODO: we may need to keep some of these for JMAP...
68 delete @$self{qw(tid to cc bytes lines)};
72 # used for v2, Import and v1 non-SQLite WWW code paths
74 my ($self, $hdr, $sync) = @_;
75 for my $f (qw(From To Cc Subject)) {
76 my @all = $hdr->header($f);
77 my $val = join(', ', @all);
79 # MIME decoding can create NULs, replace them with spaces
80 # to protect git and NNTP clients
83 # rare: in case headers have wide chars (not RFC2047-encoded)
86 # lower-case fields for read-only stuff
87 $self->{lc($f)} = $val;
89 # Capitalized From/Subject for git-fast-import
90 next if $f eq 'To' || $f eq 'Cc';
91 if (scalar(@all) > 1) {
96 $self->{$f} = $val if $val ne '';
99 $self->{-ds} = [ my @ds = msg_datestamp($hdr, $sync->{autime}) ];
100 $self->{-ts} = [ my @ts = msg_timestamp($hdr, $sync->{cotime}) ];
101 $self->{ds} //= $ds[0]; # no zone
102 $self->{ts} //= $ts[0];
103 $self->{mid} //= mids($hdr)->[0];
106 # no strftime, that is locale-dependent and not for RFC822
107 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
108 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
110 sub date ($) { # for NNTP
112 my $ds = $self->{ds};
113 return unless defined $ds;
114 my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
115 "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
116 $mday, $year+1900, $hour, $min, $sec);
119 sub internaldate { # for IMAP
121 my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($self->{ts} // 0);
122 sprintf("%02d-$MoY[$mon]-%04d %02d:%02d:%02d +0000",
123 $mday, $year+1900, $hour, $min, $sec);
126 our $REPLY_RE = qr/^re:\s+/i;
128 sub subject_normalized ($) {
130 $subj =~ s/\A\s+//s; # no leading space
131 $subj =~ s/\s+\z//s; # no trailing space
132 $subj =~ s/\s+/ /gs; # no redundant spaces
133 $subj =~ s/\.+\z//; # no trailing '.'
134 $subj =~ s/$REPLY_RE//igo; # remove reply prefix