about summary refs log tree commit homepage
path: root/lib/PublicInbox/Smsg.pm
diff options
context:
space:
mode:
authorEric Wong <e@yhbt.net>2020-06-01 10:06:49 +0000
committerEric Wong <e@yhbt.net>2020-06-03 04:04:21 +0000
commit9ab886546cc89f37819e1ef09cb49fd9325b3a41 (patch)
treea5503e8cd73db95f357895997acb39ec57482fb3 /lib/PublicInbox/Smsg.pm
parent7d05666ef3a5750c85e481a0c034f1c34f1b70b9 (diff)
downloadpublic-inbox-9ab886546cc89f37819e1ef09cb49fd9325b3a41.tar.gz
smsg: introduce ->populate method
This will eventually replace the __hdr() calling methods and
eradicate {mime} usage from Smsg.  For now, we can eliminate
PublicInbox::Smsg->new since most callers already rely on an
open `bless' to avoid the old {mime} arg.
Diffstat (limited to 'lib/PublicInbox/Smsg.pm')
-rw-r--r--lib/PublicInbox/Smsg.pm43
1 files changed, 34 insertions, 9 deletions
diff --git a/lib/PublicInbox/Smsg.pm b/lib/PublicInbox/Smsg.pm
index 7a2766d8..8e277127 100644
--- a/lib/PublicInbox/Smsg.pm
+++ b/lib/PublicInbox/Smsg.pm
@@ -17,11 +17,6 @@ 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;
@@ -36,11 +31,11 @@ sub get_val ($$) {
 sub to_doc_data {
         my ($self) = @_;
         join("\n",
-                $self->subject,
-                $self->from,
+                $self->{subject},
+                $self->{from},
                 $self->references,
-                $self->to,
-                $self->cc,
+                $self->{to},
+                $self->{cc},
                 $self->{blob},
                 $self->{mid},
                 $self->{bytes} // '',
@@ -115,6 +110,36 @@ sub __hdr ($$) {
         };
 }
 
+# for Import and v1 WWW code paths
+sub populate {
+        my ($self, $hdr, $v2w) = @_;
+        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/   /;
+
+                # 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 '';
+        }
+        $v2w //= {};
+        $self->{-ds} = [ my @ds = msg_datestamp($hdr, $v2w->{autime}) ];
+        $self->{-ts} = [ my @ts = msg_timestamp($hdr, $v2w->{cotime}) ];
+        $self->{ds} //= $ds[0]; # no zone
+        $self->{ts} //= $ts[0];
+}
+
 sub subject ($) { __hdr($_[0], 'Subject') }
 sub to ($) { __hdr($_[0], 'To') }
 sub cc ($) { __hdr($_[0], 'Cc') }