]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/Smsg.pm
smsg: handle wide characters in raw mail headers
[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(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
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);
82
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)};
86         $self;
87 }
88
89 # Only called by PSGI interface, not NNTP
90 sub from_mitem {
91         my ($mitem, $srch) = @_;
92         return $srch->retry_reopen(\&from_mitem, $mitem) if $srch;
93         my $self = bless {}, __PACKAGE__;
94         psgi_cull(load_expand($self, $mitem->get_document));
95 }
96
97 # for Import and v1 non-SQLite WWW code paths
98 sub populate {
99         my ($self, $hdr, $sync) = @_;
100         for my $f (qw(From To Cc Subject)) {
101                 my @all = $hdr->header($f);
102                 my $val = join(', ', @all);
103                 $val =~ tr/\r//d;
104                 # MIME decoding can create NULs, replace them with spaces
105                 # to protect git and NNTP clients
106                 $val =~ tr/\0\t\n/   /;
107
108                 # rare: in case headers have wide chars (not RFC2047-encoded)
109                 utf8::decode($val);
110
111                 # lower-case fields for read-only stuff
112                 $self->{lc($f)} = $val;
113
114                 # Capitalized From/Subject for git-fast-import
115                 next if $f eq 'To' || $f eq 'Cc';
116                 if (scalar(@all) > 1) {
117                         $val = $all[0];
118                         $val =~ tr/\r//d;
119                         $val =~ tr/\0\t\n/   /;
120                 }
121                 $self->{$f} = $val if $val ne '';
122         }
123         $sync //= {};
124         $self->{-ds} = [ my @ds = msg_datestamp($hdr, $sync->{autime}) ];
125         $self->{-ts} = [ my @ts = msg_timestamp($hdr, $sync->{cotime}) ];
126         $self->{ds} //= $ds[0]; # no zone
127         $self->{ts} //= $ts[0];
128
129         # for v1 users w/o SQLite
130         $self->{mid} //= eval { mids($hdr)->[0] } // '';
131 }
132
133 # no strftime, that is locale-dependent and not for RFC822
134 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
135 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
136
137 sub date ($) { # for NNTP
138         my ($self) = @_;
139         my $ds = $self->{ds};
140         return unless defined $ds;
141         my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
142         "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
143                                 $mday, $year+1900, $hour, $min, $sec);
144 }
145
146 sub internaldate { # for IMAP
147         my ($self) = @_;
148         my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($self->{ts} // 0);
149         sprintf("%02d-$MoY[$mon]-%04d %02d:%02d:%02d +0000",
150                                 $mday, $year+1900, $hour, $min, $sec);
151 }
152
153 our $REPLY_RE = qr/^re:\s+/i;
154
155 sub subject_normalized ($) {
156         my ($subj) = @_;
157         $subj =~ s/\A\s+//s; # no leading space
158         $subj =~ s/\s+\z//s; # no trailing space
159         $subj =~ s/\s+/ /gs; # no redundant spaces
160         $subj =~ s/\.+\z//; # no trailing '.'
161         $subj =~ s/$REPLY_RE//igo; # remove reply prefix
162         $subj;
163 }
164
165 1;