-# 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));
-}
-
-# :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;
+sub parse_references ($$$) {
+ my ($smsg, $hdr, $mids) = @_;
+ my $refs = references($hdr);
+ push(@$refs, @$mids) if scalar(@$mids) > 1;
+ return $refs if scalar(@$refs) == 0;
+
+ # prevent circular references here:
+ my %seen = ( ($smsg->{mid} // '') => 1 );
+ my @keep;
+ foreach my $ref (@$refs) {
+ if (length($ref) > PublicInbox::MID::MAX_MID_SIZE) {
+ warn "References: <$ref> too long, ignoring\n";
+ next;
+ }
+ $seen{$ref} //= push(@keep, $ref);
+ }
+ $smsg->{references} = '<'.join('> <', @keep).'>' if @keep;
+ \@keep;
+}
+
+# used for v2, 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/ /;
+
+ # 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 //= {};
+ my @ds = msg_datestamp($hdr, $sync->{autime} // $self->{ds});
+ my @ts = msg_timestamp($hdr, $sync->{cotime} // $self->{ts});
+ $self->{-ds} = \@ds;
+ $self->{-ts} = \@ts;
+ $self->{ds} //= $ds[0]; # no zone
+ $self->{ts} //= $ts[0];
+ $self->{mid} //= mids($hdr)->[0];