]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Smsg.pm
view: save memory by dropping smsg->{from_name} on use
[public-inbox.git] / lib / PublicInbox / Smsg.pm
1 # Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
2 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
3 #
4 # A small/skeleton/slim representation of a message.
5
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;
11 use strict;
12 use warnings;
13 use base qw(Exporter);
14 our @EXPORT_OK = qw(subject_normalized);
15 use PublicInbox::MID qw(mids references);
16 use PublicInbox::Address;
17 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
18
19 sub to_doc_data {
20         my ($self) = @_;
21         join("\n",
22                 $self->{subject},
23                 $self->{from},
24                 $self->{references} // '',
25                 $self->{to},
26                 $self->{cc},
27                 $self->{blob},
28                 $self->{mid},
29                 $self->{bytes} // '',
30                 $self->{lines} // ''
31         );
32 }
33
34 sub load_from_data ($$) {
35         my ($self) = $_[0]; # data = $_[1]
36         utf8::decode($_[1]);
37         (
38                 $self->{subject},
39                 $self->{from},
40                 $self->{references},
41
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
45                 $self->{to},
46                 $self->{cc},
47
48                 $self->{blob},
49                 $self->{mid},
50
51                 # NNTP only
52                 $self->{bytes},
53                 $self->{lines}
54         ) = split(/\n/, $_[1]);
55 }
56
57 sub psgi_cull ($) {
58         my ($self) = @_;
59
60         # drop NNTP-only fields which aren't relevant to PSGI results:
61         # saves ~80K on a 200 item search result:
62         # TODO: we may need to keep some of these for JMAP...
63         my ($f) = delete @$self{qw(from tid to cc bytes lines)};
64         # ghosts don't have ->{from}
65         $self->{from_name} = join(', ', PublicInbox::Address::names($f // ''));
66         $self;
67 }
68
69 sub parse_references ($$$) {
70         my ($smsg, $hdr, $mids) = @_;
71         my $refs = references($hdr);
72         push(@$refs, @$mids) if scalar(@$mids) > 1;
73         return $refs if scalar(@$refs) == 0;
74
75         # prevent circular references here:
76         my %seen = ( ($smsg->{mid} // '') => 1 );
77         my @keep;
78         foreach my $ref (@$refs) {
79                 if (length($ref) > PublicInbox::MID::MAX_MID_SIZE) {
80                         warn "References: <$ref> too long, ignoring\n";
81                         next;
82                 }
83                 $seen{$ref} //= push(@keep, $ref);
84         }
85         $smsg->{references} = '<'.join('> <', @keep).'>' if @keep;
86         \@keep;
87 }
88
89 # used for v2, Import and v1 non-SQLite WWW code paths
90 sub populate {
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);
95                 $val =~ tr/\r//d;
96                 # MIME decoding can create NULs, replace them with spaces
97                 # to protect git and NNTP clients
98                 $val =~ tr/\0\t\n/   /;
99
100                 # rare: in case headers have wide chars (not RFC2047-encoded)
101                 utf8::decode($val);
102
103                 # lower-case fields for read-only stuff
104                 $self->{lc($f)} = $val;
105
106                 # Capitalized From/Subject for git-fast-import
107                 next if $f eq 'To' || $f eq 'Cc';
108                 if (scalar(@all) > 1) {
109                         $val = $all[0];
110                         $val =~ tr/\r//d;
111                         $val =~ tr/\0\t\n/   /;
112                 }
113                 $self->{$f} = $val if $val ne '';
114         }
115         $sync //= {};
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];
120         $self->{mid} //= mids($hdr)->[0];
121 }
122
123 # no strftime, that is locale-dependent and not for RFC822
124 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
125 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
126
127 sub date ($) { # for NNTP
128         my ($self) = @_;
129         my $ds = $self->{ds};
130         return unless defined $ds;
131         my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
132         "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
133                                 $mday, $year+1900, $hour, $min, $sec);
134 }
135
136 sub internaldate { # for IMAP
137         my ($self) = @_;
138         my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($self->{ts} // 0);
139         sprintf("%02d-$MoY[$mon]-%04d %02d:%02d:%02d +0000",
140                                 $mday, $year+1900, $hour, $min, $sec);
141 }
142
143 our $REPLY_RE = qr/^re:\s+/i;
144
145 # TODO: see RFC 5256 sec 2.1 "Base Subject" and evaluate compatibility
146 # w/ existing indices...
147 sub subject_normalized ($) {
148         my ($subj) = @_;
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
154         $subj;
155 }
156
157 # returns the number of bytes to add if given a non-CRLF arg
158 sub crlf_adjust ($) {
159         if (index($_[0], "\r\n") < 0) {
160                 # common case is LF-only, every \n needs an \r;
161                 # so favor a cheap tr// over an expensive m//g
162                 $_[0] =~ tr/\n/\n/;
163         } else { # count number of '\n' w/o '\r', expensive:
164                 scalar(my @n = ($_[0] =~ m/(?<!\r)\n/g));
165         }
166 }
167
168 sub set_bytes { $_[0]->{bytes} = $_[2] + crlf_adjust($_[1]) }
169
170 1;