# This license differs from the rest of public-inbox
#
# Our own jwz-style threading class based on Mail::Thread from CPAN.
-# Mail::Thread is unmaintained and available on some distros.
+# Mail::Thread is unmaintained and unavailable on some distros.
# We also do not want pruning or subject grouping, since we want
# to encourage strict threading and hopefully encourage people
# to use proper In-Reply-To.
my $self = shift;
_add_message($self, $_) foreach @{$self->{messages}};
my $id_table = delete $self->{id_table};
- $self->{rootset} = [ grep { !delete $_->{parent} } values %$id_table ];
+ $self->{rootset} = [ grep {
+ !delete($_->{parent}) && $_->visible } values %$id_table ];
}
sub _get_cont_for_id ($$) {
# B. For each element in the message's References field:
defined(my $refs = $smsg->{references}) or return;
+ # This loop exists to help fill in gaps left from missing
+ # messages. It is not needed in a perfect world where
+ # everything is perfectly referenced, only the last ref
+ # matters.
my $prev;
foreach my $ref ($refs =~ m/<([^>]+)>/g) {
# Find a Container object for the given Message-ID
}
# C. Set the parent of this message to be the last element in
- # References...
- if ($prev && !$this->has_descendent($prev)) { # would loop
- $prev->add_child($this)
- }
+ # References.
+ $prev->add_child($this) if defined $prev;
}
sub order {
sub has_descendent {
my ($self, $child) = @_;
+ my %seen; # loop prevention
while ($child) {
- return 1 if $self == $child;
+ return 1 if $self == $child || $seen{$child}++;
$child = $child->{parent};
}
0;
}
+# Do not show/keep ghosts iff they have no children. Sometimes
+# a ghost Message-ID is the result of a long header line
+# being folded/mangled by a MUA, and not a missing message.
+sub visible ($) {
+ my ($self) = @_;
+ $self->{smsg} || scalar values %{$self->{children}};
+}
+
sub order_children {
my ($cur, $ordersub) = @_;
- my %seen = ($cur => 1);
+ my %seen = ($cur => 1); # self-referential loop prevention
my @q = ($cur);
while (defined($cur = shift @q)) {
my $c = $cur->{children}; # The hashref here...
- $c = [ grep { !$seen{$_}++ } values %$c ]; # spot/break loops
+ $c = [ grep { !$seen{$_}++ && visible($_) } values %$c ];
$c = $ordersub->($c) if scalar @$c > 1;
$cur->{children} = $c; # ...becomes an arrayref
push @q, @$c;