1 # This library is free software; you can redistribute it and/or modify
2 # it under the same terms as Perl itself.
4 # This license differs from the rest of public-inbox
6 # Our own jwz-style threading class based on Mail::Thread from CPAN.
7 # Mail::Thread is unmaintained and unavailable on some distros.
8 # We also do not want pruning or subject grouping, since we want
9 # to encourage strict threading and hopefully encourage people
10 # to use proper In-Reply-To.
12 # This includes fixes from several open bugs for Mail::Thread
14 # Avoid circular references
15 # - https://rt.cpan.org/Public/Bug/Display.html?id=22817
17 # And avoid recursion in recurse_down:
18 # - https://rt.cpan.org/Ticket/Display.html?id=116727
19 # - http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=833479
20 package PublicInbox::SearchThread;
34 _add_message($self, $_) foreach @{$self->{messages}};
35 my $id_table = delete $self->{id_table};
36 $self->{rootset} = [ grep {
37 !delete($_->{parent}) && $_->visible } values %$id_table ];
40 sub _get_cont_for_id ($$) {
41 my ($self, $mid) = @_;
42 $self->{id_table}{$mid} ||= PublicInbox::SearchThread::Msg->new($mid);
45 sub _add_message ($$) {
46 my ($self, $smsg) = @_;
49 my $this = _get_cont_for_id($self, $smsg->{mid});
50 $this->{smsg} = $smsg;
52 # B. For each element in the message's References field:
53 defined(my $refs = $smsg->{references}) or return;
56 foreach my $ref ($refs =~ m/<([^>]+)>/g) {
57 # Find a Container object for the given Message-ID
58 my $cont = _get_cont_for_id($self, $ref);
60 # Link the References field's Containers together in
61 # the order implied by the References header
63 # * If they are already linked don't change the
65 # * Do not add a link if adding that link would
68 !$cont->{parent} && # already linked
69 !$cont->has_descendent($prev) # would loop
71 $prev->add_child($cont);
76 # C. Set the parent of this message to be the last element in
78 if ($prev && !$this->has_descendent($prev)) { # would loop
79 $prev->add_child($this)
84 my ($self, $ordersub) = @_;
85 my $rootset = $ordersub->($self->{rootset});
86 $self->{rootset} = $rootset;
87 $_->order_children($ordersub) for @$rootset;
90 package PublicInbox::SearchThread::Msg;
98 children => {}, # becomes an array when sorted by ->order(...)
105 while (my $cont = shift @q) {
106 return $cont if $cont->{smsg};
107 push @q, values %{$cont->{children}};
113 my ($self, $child) = @_;
114 croak "Cowardly refusing to become my own parent: $self"
117 my $cid = $child->{id};
120 if (defined(my $parent = $child->{parent})) {
121 delete $parent->{children}->{$cid};
124 $self->{children}->{$cid} = $child;
125 $child->{parent} = $self;
129 my ($self, $child) = @_;
131 return 1 if $self == $child;
132 $child = $child->{parent};
137 # Do not show/keep ghosts iff they have no children. Sometimes
138 # a ghost Message-ID is the result of a long header line
139 # being folded/mangled by a MUA, and not a missing message.
142 $self->{smsg} || scalar values %{$self->{children}};
146 my ($cur, $ordersub) = @_;
148 my %seen = ($cur => 1); # self-referential loop prevention
150 while (defined($cur = shift @q)) {
151 my $c = $cur->{children}; # The hashref here...
153 $c = [ grep { !$seen{$_}++ && visible($_) } values %$c ];
154 $c = $ordersub->($c) if scalar @$c > 1;
155 $cur->{children} = $c; # ...becomes an arrayref