+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;
+}
+
+# normalize subjects so they are suitable as pathnames for URLs
+# XXX: consider for removal
+sub subject_path ($) {
+ my ($subj) = @_;
+ $subj = subject_normalized($subj);
+ $subj =~ s![^a-zA-Z0-9_\.~/\-]+!_!g;
+ lc($subj);
+}
+
+sub add_overview {
+ my ($self, $eml, $smsg) = @_;
+ $smsg->{lines} = $eml->body_raw =~ tr!\n!\n!;
+ my $mids = mids_for_index($eml);
+ my $refs = parse_references($smsg, $eml, $mids);
+ my $subj = $smsg->{subject};
+ my $xpath;
+ if ($subj ne '') {
+ $xpath = subject_path($subj);
+ $xpath = id_compress($xpath);
+ }
+ my $dd = $smsg->to_doc_data;
+ utf8::encode($dd);
+ $dd = compress($dd);
+ add_over($self, $smsg, $mids, $refs, $xpath, $dd);
+}
+
+sub _add_over {
+ my ($self, $smsg, $mid, $refs, $old_tid, $v) = @_;
+ my $cur_tid = $smsg->{tid};
+ my $n = $smsg->{num};
+ die "num must not be zero for $mid" if !$n;
+ my $cur_valid = $cur_tid > $self->{min_tid};
+
+ if ($n > 0) { # regular mail
+ if ($cur_valid) {
+ $$old_tid //= $cur_tid;
+ merge_threads($self, $$old_tid, $cur_tid);
+ } else {
+ $$old_tid //= next_tid($self);
+ }
+ } elsif ($n < 0) { # ghost
+ $$old_tid //= $cur_valid ? $cur_tid : next_tid($self);
+ link_refs($self, $refs, $$old_tid);
+ delete_by_num($self, $n);
+ $$v++;
+ }
+ 1;
+}
+