]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Smsg.pm
imap+nntp: share COMPRESS implementation
[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 v5.10.1;
13 use parent 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 oidbin { pack('H*', $_[0]->{blob}) }
20
21 sub to_doc_data {
22         my ($self) = @_;
23         join("\n",
24                 $self->{subject},
25                 $self->{from},
26                 $self->{references} // '',
27                 $self->{to},
28                 $self->{cc},
29                 $self->{blob},
30                 $self->{mid},
31                 $self->{bytes} // '',
32                 $self->{lines} // ''
33         );
34 }
35
36 sub load_from_data ($$) {
37         my ($self) = $_[0]; # data = $_[1]
38         utf8::decode($_[1]);
39         (
40                 $self->{subject},
41                 $self->{from},
42                 $self->{references},
43
44                 # To: and Cc: are stored to optimize HDR/XHDR in NNTP since
45                 # some NNTP clients will use that for message displays.
46                 # NNTP only, and only stored in Over(view), not Xapian
47                 $self->{to},
48                 $self->{cc},
49
50                 $self->{blob},
51                 $self->{mid},
52
53                 # NNTP only
54                 $self->{bytes},
55                 $self->{lines}
56         ) = split(/\n/, $_[1]);
57 }
58
59 sub psgi_cull ($) {
60         my ($self) = @_;
61
62         # drop NNTP-only fields which aren't relevant to PSGI results:
63         # saves ~80K on a 200 item search result:
64         # TODO: we may need to keep some of these for JMAP...
65         my ($f) = delete @$self{qw(from tid to cc bytes lines)};
66         # ghosts don't have ->{from}
67         $self->{from_name} = join(', ', PublicInbox::Address::names($f // ''));
68         $self;
69 }
70
71 sub parse_references ($$$) {
72         my ($smsg, $hdr, $mids) = @_;
73         my $refs = references($hdr);
74         push(@$refs, @$mids) if scalar(@$mids) > 1;
75         return $refs if scalar(@$refs) == 0;
76
77         # prevent circular references here:
78         my %seen = ( ($smsg->{mid} // '') => 1 );
79         my @keep;
80         foreach my $ref (@$refs) {
81                 if (length($ref) > PublicInbox::MID::MAX_MID_SIZE) {
82                         warn "References: <$ref> too long, ignoring\n";
83                         next;
84                 }
85                 $seen{$ref} //= push(@keep, $ref);
86         }
87         $smsg->{references} = '<'.join('> <', @keep).'>' if @keep;
88         \@keep;
89 }
90
91 # used for v2, Import and v1 non-SQLite WWW code paths
92 sub populate {
93         my ($self, $hdr, $sync) = @_;
94         for my $f (qw(From To Cc Subject)) {
95                 my @all = $hdr->header($f);
96                 my $val = join(', ', @all);
97                 $val =~ tr/\r//d;
98                 # MIME decoding can create NULs, replace them with spaces
99                 # to protect git and NNTP clients
100                 $val =~ tr/\0\t\n/   /;
101
102                 # rare: in case headers have wide chars (not RFC2047-encoded)
103                 utf8::decode($val);
104
105                 # lower-case fields for read-only stuff
106                 $self->{lc($f)} = $val;
107
108                 # Capitalized From/Subject for git-fast-import
109                 next if $f eq 'To' || $f eq 'Cc';
110                 if (scalar(@all) > 1) {
111                         $val = $all[0];
112                         $val =~ tr/\r//d;
113                         $val =~ tr/\0\t\n/   /;
114                 }
115                 $self->{$f} = $val if $val ne '';
116         }
117         $sync //= {};
118         $self->{-ds} = [ my @ds = msg_datestamp($hdr, $sync->{autime}) ];
119         $self->{-ts} = [ my @ts = msg_timestamp($hdr, $sync->{cotime}) ];
120         $self->{ds} //= $ds[0]; # no zone
121         $self->{ts} //= $ts[0];
122         $self->{mid} //= mids($hdr)->[0];
123 }
124
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);
128
129 sub date ($) { # for NNTP
130         my ($self) = @_;
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);
136 }
137
138 sub internaldate { # for IMAP
139         my ($self) = @_;
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);
143 }
144
145 our $REPLY_RE = qr/^re:\s+/i;
146
147 # TODO: see RFC 5256 sec 2.1 "Base Subject" and evaluate compatibility
148 # w/ existing indices...
149 sub subject_normalized ($) {
150         my ($subj) = @_;
151         $subj =~ s/\A\s+//s; # no leading space
152         $subj =~ s/\s+\z//s; # no trailing space
153         $subj =~ s/\s+/ /gs; # no redundant spaces
154         $subj =~ s/\.+\z//; # no trailing '.'
155         $subj =~ s/$REPLY_RE//igo; # remove reply prefix
156         $subj;
157 }
158
159 # returns the number of bytes to add if given a non-CRLF arg
160 sub crlf_adjust ($) {
161         if (index($_[0], "\r\n") < 0) {
162                 # common case is LF-only, every \n needs an \r;
163                 # so favor a cheap tr// over an expensive m//g
164                 $_[0] =~ tr/\n/\n/;
165         } else { # count number of '\n' w/o '\r', expensive:
166                 scalar(my @n = ($_[0] =~ m/(?<!\r)\n/g));
167         }
168 }
169
170 sub set_bytes { $_[0]->{bytes} = $_[2] + crlf_adjust($_[1]) }
171
172 1;