]> Sergey Matveev's repositories - public-inbox.git/blobdiff - lib/PublicInbox/Smsg.pm
smsg: make parse_references an object method
[public-inbox.git] / lib / PublicInbox / Smsg.pm
index c6ff7f52b0ac90a2aad2326f80c4b862b4cc982b..2b72e8b5ae804e283cbd4711f2a001261da9355c 100644 (file)
@@ -12,7 +12,7 @@ use strict;
 use warnings;
 use base qw(Exporter);
 our @EXPORT_OK = qw(subject_normalized);
-use PublicInbox::MID qw(mids);
+use PublicInbox::MID qw(mids references);
 use PublicInbox::Address;
 use PublicInbox::MsgTime qw(msg_timestamp msg_datestamp);
 
@@ -69,6 +69,26 @@ sub psgi_cull ($) {
        $self;
 }
 
+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;
+               }
+               push(@keep, $ref) unless $seen{$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) = @_;