]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Smsg.pm
www: remove smsg_mime API and adjust callers
[public-inbox.git] / lib / PublicInbox / Smsg.pm
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>
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(mid_mime mids);
16 use PublicInbox::Address;
17 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
18 use Time::Local qw(timegm);
19
20 sub get_val ($$) {
21         my ($doc, $col) = @_;
22         # sortable_unserialise is defined by PublicInbox::Search::load_xapian()
23         sortable_unserialise($doc->get_value($col));
24 }
25
26 sub to_doc_data {
27         my ($self) = @_;
28         join("\n",
29                 $self->{subject},
30                 $self->{from},
31                 $self->references,
32                 $self->{to},
33                 $self->{cc},
34                 $self->{blob},
35                 $self->{mid},
36                 $self->{bytes} // '',
37                 $self->{lines} // ''
38         );
39 }
40
41 sub load_from_data ($$) {
42         my ($self) = $_[0]; # data = $_[1]
43         (
44                 $self->{subject},
45                 $self->{from},
46                 $self->{references},
47
48                 # To: and Cc: are stored to optimize HDR/XHDR in NNTP since
49                 # some NNTP clients will use that for message displays.
50                 # NNTP only, and only stored in Over(view), not Xapian
51                 $self->{to},
52                 $self->{cc},
53
54                 $self->{blob},
55                 $self->{mid},
56
57                 # NNTP only
58                 $self->{bytes},
59                 $self->{lines}
60         ) = split(/\n/, $_[1]);
61 }
62
63 sub load_expand {
64         my ($self, $doc) = @_;
65         my $data = $doc->get_data or return;
66         $self->{ts} = get_val($doc, PublicInbox::Search::TS());
67         my $dt = get_val($doc, PublicInbox::Search::DT());
68         my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $dt);
69         $self->{ds} = timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
70         utf8::decode($data);
71         load_from_data($self, $data);
72         $self;
73 }
74
75 sub psgi_cull ($) {
76         my ($self) = @_;
77         from_name($self); # fill in {from_name} so we can delete {from}
78
79         # drop NNTP-only fields which aren't relevant to PSGI results:
80         # saves ~80K on a 200 item search result:
81         delete @$self{qw(from ts to cc bytes lines)};
82         $self;
83 }
84
85 # Only called by PSGI interface, not NNTP
86 sub from_mitem {
87         my ($mitem, $srch) = @_;
88         return $srch->retry_reopen(\&from_mitem, $mitem) if $srch;
89         my $self = bless {}, __PACKAGE__;
90         psgi_cull(load_expand($self, $mitem->get_document));
91 }
92
93 # :bytes and :lines metadata in RFC 3977
94 sub bytes ($) { $_[0]->{bytes} }
95 sub lines ($) { $_[0]->{lines} }
96
97 sub __hdr ($$) {
98         my ($self, $field) = @_;
99         $self->{lc($field)} //= do {
100                 my $mime = $self->{mime} or return;
101                 my $val = join(', ', $mime->header($field));
102                 $val =~ tr/\r//d;
103                 $val =~ tr/\t\n/  /;
104                 $val;
105         };
106 }
107
108 # for Import and v1 non-SQLite WWW code paths
109 sub populate {
110         my ($self, $hdr, $v2w) = @_;
111         for my $f (qw(From To Cc Subject)) {
112                 my @all = $hdr->header($f);
113                 my $val = join(', ', @all);
114                 $val =~ tr/\r//d;
115                 # MIME decoding can create NULs, replace them with spaces
116                 # to protect git and NNTP clients
117                 $val =~ tr/\0\t\n/   /;
118
119                 # lower-case fields for read-only stuff
120                 $self->{lc($f)} = $val;
121
122                 # Capitalized From/Subject for git-fast-import
123                 next if $f eq 'To' || $f eq 'Cc';
124                 if (scalar(@all) > 1) {
125                         $val = $all[0];
126                         $val =~ tr/\r//d;
127                         $val =~ tr/\0\t\n/   /;
128                 }
129                 $self->{$f} = $val if $val ne '';
130         }
131         $v2w //= {};
132         $self->{-ds} = [ my @ds = msg_datestamp($hdr, $v2w->{autime}) ];
133         $self->{-ts} = [ my @ts = msg_timestamp($hdr, $v2w->{cotime}) ];
134         $self->{ds} //= $ds[0]; # no zone
135         $self->{ts} //= $ts[0];
136
137         # for v1 users w/o SQLite
138         $self->{mid} //= eval { mids($hdr)->[0] } // '';
139 }
140
141 sub subject ($) { __hdr($_[0], 'Subject') }
142 sub to ($) { __hdr($_[0], 'To') }
143 sub cc ($) { __hdr($_[0], 'Cc') }
144
145 # no strftime, that is locale-dependent and not for RFC822
146 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
147 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
148
149 sub date ($) {
150         my ($self) = @_;
151         my $ds = $self->{ds};
152         return unless defined $ds;
153         my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
154         "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
155                                 $mday, $year+1900, $hour, $min, $sec);
156
157 }
158
159 sub from ($) {
160         my ($self) = @_;
161         my $from = __hdr($self, 'From');
162         if (defined $from && !defined $self->{from_name}) {
163                 my @n = PublicInbox::Address::names($from);
164                 $self->{from_name} = join(', ', @n);
165         }
166         $from;
167 }
168
169 sub from_name {
170         my ($self) = @_;
171         my $from_name = $self->{from_name};
172         return $from_name if defined $from_name;
173         $self->from;
174         $self->{from_name};
175 }
176
177 sub ts {
178         my ($self) = @_;
179         $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0;
180 }
181
182 sub ds {
183         my ($self) = @_;
184         $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0;
185 }
186
187 sub references {
188         my ($self) = @_;
189         my $x = $self->{references};
190         defined $x ? $x : '';
191 }
192
193 sub mid ($;$) {
194         my ($self, $mid) = @_;
195
196         if (defined $mid) {
197                 $self->{mid} = $mid;
198         } elsif (defined(my $rv = $self->{mid})) {
199                 $rv;
200         } else {
201                 die "NO {mime} for mid\n" unless $self->{mime};
202                 mid_mime($self->{mime}) # v1 w/o Xapian
203         }
204 }
205
206 our $REPLY_RE = qr/^re:\s+/i;
207
208 sub subject_normalized ($) {
209         my ($subj) = @_;
210         $subj =~ s/\A\s+//s; # no leading space
211         $subj =~ s/\s+\z//s; # no trailing space
212         $subj =~ s/\s+/ /gs; # no redundant spaces
213         $subj =~ s/\.+\z//; # no trailing '.'
214         $subj =~ s/$REPLY_RE//igo; # remove reply prefix
215         $subj;
216 }
217
218 1;