-
- # last References should be IRT, but some mail clients do things
- # out of order, so trust IRT over References iff IRT exists
- my @refs = ($hdr->header_raw('References'),
- $hdr->header_raw('In-Reply-To'));
- @refs = ((join(' ', @refs)) =~ /<([^>]+)>/g);
-
- my $tid;
- if (@refs) {
- my %uniq = ($mid => 1);
- my @orig_refs = @refs;
- @refs = ();
-
- # prevent circular references via References: here:
- foreach my $ref (@orig_refs) {
- if (length($ref) > MAX_MID_SIZE) {
- warn "References: <$ref> too long, ignoring\n";
- }
- next if $uniq{$ref};
- $uniq{$ref} = 1;
- push @refs, $ref;
- }
- }
-
- if (@refs) {
- $smsg->{references} = '<'.join('> <', @refs).'>';
-
- # first ref *should* be the thread root,
- # but we can never trust clients to do the right thing
- my $ref = shift @refs;
- $tid = $self->_resolve_mid_to_tid($ref);
- $self->merge_threads($tid, $old_tid) if defined $old_tid;
-
- # the rest of the refs should point to this tid:
- foreach $ref (@refs) {
- my $ptid = $self->_resolve_mid_to_tid($ref);
- merge_threads($self, $tid, $ptid);
+ my $refs = references($hdr);
+ push(@$refs, @$mids) if scalar(@$mids) > 1;
+ return $refs if scalar(@$refs) == 0;
+
+ # prevent circular references here:
+ my %seen = ( $mid0 => 1 );
+ my @keep;
+ foreach my $ref (@$refs) {
+ if (length($ref) > PublicInbox::MID::MAX_MID_SIZE) {
+ warn "References: <$ref> too long, ignoring\n";
+ next;