]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Smsg.pm
update copyrights for 2021
[public-inbox.git] / lib / PublicInbox / Smsg.pm
index e8f9c9a3681bd8519997e77729a06949e6ba5ce6..571cbb6faca9417d804b1771b0bb94fdac5e6987 100644 (file)
@@ -1,4 +1,4 @@
-# Copyright (C) 2015-2020 all contributors <meta@public-inbox.org>
+# Copyright (C) 2015-2021 all contributors <meta@public-inbox.org>
 # License: AGPL-3.0+ <https://www.gnu.org/licenses/agpl-3.0.txt>
 #
 # A small/skeleton/slim representation of a message.
@@ -15,13 +15,6 @@ our @EXPORT_OK = qw(subject_normalized);
 use PublicInbox::MID qw(mids);
 use PublicInbox::Address;
 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
-use Time::Local qw(timegm);
-
-sub get_val ($$) {
-       my ($doc, $col) = @_;
-       # sortable_unserialise is defined by PublicInbox::Search::load_xapian()
-       sortable_unserialise($doc->get_value($col));
-}
 
 sub to_doc_data {
        my ($self) = @_;
@@ -40,6 +33,7 @@ sub to_doc_data {
 
 sub load_from_data ($$) {
        my ($self) = $_[0]; # data = $_[1]
+       utf8::decode($_[1]);
        (
                $self->{subject},
                $self->{from},
@@ -60,18 +54,6 @@ sub load_from_data ($$) {
        ) = split(/\n/, $_[1]);
 }
 
-sub load_expand {
-       my ($self, $doc) = @_;
-       my $data = $doc->get_data or return;
-       $self->{ts} = get_val($doc, PublicInbox::Search::TS());
-       my $dt = get_val($doc, PublicInbox::Search::DT());
-       my ($yyyy, $mon, $dd, $hh, $mm, $ss) = unpack('A4A2A2A2A2A2', $dt);
-       $self->{ds} = timegm($ss, $mm, $hh, $dd, $mon - 1, $yyyy);
-       utf8::decode($data);
-       load_from_data($self, $data);
-       $self;
-}
-
 sub psgi_cull ($) {
        my ($self) = @_;
 
@@ -82,21 +64,14 @@ sub psgi_cull ($) {
 
        # drop NNTP-only fields which aren't relevant to PSGI results:
        # saves ~80K on a 200 item search result:
-       delete @$self{qw(ts to cc bytes lines)};
+       # TODO: we may need to keep some of these for JMAP...
+       delete @$self{qw(tid to cc bytes lines)};
        $self;
 }
 
-# Only called by PSGI interface, not NNTP
-sub from_mitem {
-       my ($mitem, $srch) = @_;
-       return $srch->retry_reopen(\&from_mitem, $mitem) if $srch;
-       my $self = bless {}, __PACKAGE__;
-       psgi_cull(load_expand($self, $mitem->get_document));
-}
-
-# for Import and v1 non-SQLite WWW code paths
+# used for v2, Import and v1 non-SQLite WWW code paths
 sub populate {
-       my ($self, $hdr, $v2w) = @_;
+       my ($self, $hdr, $sync) = @_;
        for my $f (qw(From To Cc Subject)) {
                my @all = $hdr->header($f);
                my $val = join(', ', @all);
@@ -105,6 +80,9 @@ sub populate {
                # to protect git and NNTP clients
                $val =~ tr/\0\t\n/   /;
 
+               # rare: in case headers have wide chars (not RFC2047-encoded)
+               utf8::decode($val);
+
                # lower-case fields for read-only stuff
                $self->{lc($f)} = $val;
 
@@ -117,28 +95,32 @@ sub populate {
                }
                $self->{$f} = $val if $val ne '';
        }
-       $v2w //= {};
-       $self->{-ds} = [ my @ds = msg_datestamp($hdr, $v2w->{autime}) ];
-       $self->{-ts} = [ my @ts = msg_timestamp($hdr, $v2w->{cotime}) ];
+       $sync //= {};
+       $self->{-ds} = [ my @ds = msg_datestamp($hdr, $sync->{autime}) ];
+       $self->{-ts} = [ my @ts = msg_timestamp($hdr, $sync->{cotime}) ];
        $self->{ds} //= $ds[0]; # no zone
        $self->{ts} //= $ts[0];
-
-       # for v1 users w/o SQLite
-       $self->{mid} //= eval { mids($hdr)->[0] } // '';
+       $self->{mid} //= mids($hdr)->[0];
 }
 
 # no strftime, that is locale-dependent and not for RFC822
 my @DoW = qw(Sun Mon Tue Wed Thu Fri Sat);
 my @MoY = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec);
 
-sub date ($) {
+sub date ($) { # for NNTP
        my ($self) = @_;
        my $ds = $self->{ds};
        return unless defined $ds;
        my ($sec, $min, $hour, $mday, $mon, $year, $wday) = gmtime($ds);
        "$DoW[$wday], " . sprintf("%02d $MoY[$mon] %04d %02d:%02d:%02d +0000",
                                $mday, $year+1900, $hour, $min, $sec);
+}
 
+sub internaldate { # for IMAP
+       my ($self) = @_;
+       my ($sec, $min, $hour, $mday, $mon, $year) = gmtime($self->{ts} // 0);
+       sprintf("%02d-$MoY[$mon]-%04d %02d:%02d:%02d +0000",
+                               $mday, $year+1900, $hour, $min, $sec);
 }
 
 our $REPLY_RE = qr/^re:\s+/i;