]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Smsg.pm
www: improve navigation around contemporary threads
[public-inbox.git] / lib / PublicInbox / Smsg.pm
index 7a47703a25054158c4f1ef12f61cad87ef7a1627..171e0a00908e4b7bdf51ba05beda6dcc498aa08c 100644 (file)
@@ -12,21 +12,11 @@ use strict;
 use warnings;
 use base qw(Exporter);
 our @EXPORT_OK = qw(subject_normalized);
-use PublicInbox::MID qw/mid_clean mid_mime/;
+use PublicInbox::MID qw(mids);
 use PublicInbox::Address;
 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
 use Time::Local qw(timegm);
 
-sub new {
-       my ($class, $mime) = @_;
-       bless { mime => $mime }, $class;
-}
-
-sub wrap {
-       my ($class, $mid) = @_;
-       bless { mid => $mid }, $class;
-}
-
 sub get_val ($$) {
        my ($doc, $col) = @_;
        # sortable_unserialise is defined by PublicInbox::Search::load_xapian()
@@ -34,16 +24,15 @@ sub get_val ($$) {
 }
 
 sub to_doc_data {
-       my ($self, $oid, $mid0) = @_;
-       $oid = '' unless defined $oid;
+       my ($self) = @_;
        join("\n",
-               $self->subject,
-               $self->from,
-               $self->references,
-               $self->to,
-               $self->cc,
-               $oid,
-               $mid0,
+               $self->{subject},
+               $self->{from},
+               $self->{references} // '',
+               $self->{to},
+               $self->{cc},
+               $self->{blob},
+               $self->{mid},
                $self->{bytes} // '',
                $self->{lines} // ''
        );
@@ -51,6 +40,7 @@ sub to_doc_data {
 
 sub load_from_data ($$) {
        my ($self) = $_[0]; # data = $_[1]
+       utf8::decode($_[1]);
        (
                $self->{subject},
                $self->{from},
@@ -78,113 +68,81 @@ sub load_expand {
        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) = @_;
-       from_name($self); # fill in {from_name} so we can delete {from}
+
+       # ghosts don't have ->{from}
+       my $from = delete($self->{from}) // '';
+       my @n = PublicInbox::Address::names($from);
+       $self->{from_name} = join(', ', @n);
 
        # drop NNTP-only fields which aren't relevant to PSGI results:
        # saves ~80K on a 200 item search result:
-       delete @$self{qw(from 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
+sub populate {
+       my ($self, $hdr, $sync) = @_;
+       for my $f (qw(From To Cc Subject)) {
+               my @all = $hdr->header($f);
+               my $val = join(', ', @all);
+               $val =~ tr/\r//d;
+               # MIME decoding can create NULs, replace them with spaces
+               # 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;
+
+               # Capitalized From/Subject for git-fast-import
+               next if $f eq 'To' || $f eq 'Cc';
+               if (scalar(@all) > 1) {
+                       $val = $all[0];
+                       $val =~ tr/\r//d;
+                       $val =~ tr/\0\t\n/   /;
+               }
+               $self->{$f} = $val if $val ne '';
+       }
+       $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];
 
-# :bytes and :lines metadata in RFC 3977
-sub bytes ($) { $_[0]->{bytes} }
-sub lines ($) { $_[0]->{lines} }
-
-sub __hdr ($$) {
-       my ($self, $field) = @_;
-       my $val = $self->{$field};
-       return $val if defined $val;
-
-       my $mime = $self->{mime} or return;
-       my @raw = $mime->header($field);
-       $val = join(', ', @raw);
-       $val =~ tr/\t\n/  /;
-       $val =~ tr/\r//d;
-       $self->{$field} = $val;
+       # for v1 users w/o SQLite
+       $self->{mid} //= eval { mids($hdr)->[0] } // '';
 }
 
-sub subject ($) { __hdr($_[0], 'subject') }
-sub to ($) { __hdr($_[0], 'to') }
-sub cc ($) { __hdr($_[0], 'cc') }
-
 # 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 from ($) {
+sub internaldate { # for IMAP
        my ($self) = @_;
-       my $from = __hdr($self, 'from');
-       if (defined $from && !defined $self->{from_name}) {
-               my @n = PublicInbox::Address::names($from);
-               $self->{from_name} = join(', ', @n);
-       }
-       $from;
-}
-
-sub from_name {
-       my ($self) = @_;
-       my $from_name = $self->{from_name};
-       return $from_name if defined $from_name;
-       $self->from;
-       $self->{from_name};
-}
-
-sub ts {
-       my ($self) = @_;
-       $self->{ts} ||= eval { msg_timestamp($self->{mime}->header_obj) } || 0;
-}
-
-sub ds {
-       my ($self) = @_;
-       $self->{ds} ||= eval { msg_datestamp($self->{mime}->header_obj); } || 0;
-}
-
-sub references {
-       my ($self) = @_;
-       my $x = $self->{references};
-       defined $x ? $x : '';
-}
-
-sub mid ($;$) {
-       my ($self, $mid) = @_;
-
-       if (defined $mid) {
-               $self->{mid} = $mid;
-       } elsif (defined(my $rv = $self->{mid})) {
-               $rv;
-       } else {
-               die "NO {mime} for mid\n" unless $self->{mime};
-               $self->_extract_mid; # v1 w/o Xapian
-       }
+       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);
 }
 
-sub _extract_mid { mid_clean(mid_mime($_[0]->{mime})) }
-
 our $REPLY_RE = qr/^re:\s+/i;
 
 sub subject_normalized ($) {