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 available 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}};
36 grep { !$_->{parent} } values %{$self->{id_table}} ];
37 delete $self->{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:
54 if (defined(my $refs = $smsg->{references})) {
55 foreach my $ref ($refs =~ m/<([^>]+)>/g) {
56 # Find a Container object for the given Message-ID
57 my $cont = _get_cont_for_id($self, $ref);
59 # Link the References field's Containers together in
60 # the order implied by the References header
62 # * If they are already linked don't change the
64 # * Do not add a link if adding that link would
67 !$cont->{parent} && # already linked
68 !$cont->has_descendent($prev) # would loop
70 $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) = @_;
87 my $root = _get_cont_for_id($self, 'fakeroot');
88 $root->add_child( $_ ) for @{ $self->{rootset} };
91 $root->order_children( $ordersub );
94 my $kids = $root->children;
95 $self->{rootset} = $kids;
96 $root->remove_child($_) for @$kids;
99 package PublicInbox::SearchThread::Msg;
101 use Scalar::Util qw(weaken);
103 sub new { my $self = shift; bless { id => shift }, $self; }
106 my ($self, $child) = @_;
107 croak "Cowardly refusing to become my own parent: $self"
110 if (grep { $_ == $child } @{$self->children}) {
111 # All is potentially correct with the world
112 weaken($child->{parent} = $self);
116 my $parent = $child->{parent};
117 remove_child($parent, $child) if $parent;
119 $child->{next} = $self->{child};
120 $self->{child} = $child;
121 weaken($child->{parent} = $self);
125 my ($self, $child) = @_;
127 my $x = $self->{child} or return;
128 if ($x == $child) { # First one's easy.
129 $self->{child} = $child->{next};
130 $child->{parent} = $child->{next} = undef;
135 while ($x = $x->{next}) {
137 $prev->{next} = $x->{next}; # Unlink x
138 $x->{next} = $x->{parent} = undef; # Deparent it
143 # oddly, we can get here
144 $child->{next} = $child->{parent} = undef;
148 my ($self, $child) = @_;
151 while (my $cont = shift @q) {
154 return 1 if $cont == $child;
156 if (my $next = $cont->{next}) {
158 $cont->{next} = undef;
163 if (my $child = $cont->{child}) {
165 $cont->{child} = undef;
177 my $visitor = $self->{child};
179 push @children, $visitor;
180 $visitor = $visitor->{next};
186 my ($self, $children) = @_;
187 my $walk = $self->{child} = shift @$children;
189 $walk = $walk->{next} = shift @$children;
194 my ($walk, $ordersub) = @_;
199 push @visited, $walk;
204 my $child = $walk->{child};
205 if ($child && $seen{$child}) {
206 $walk->{child} = $child = undef;
209 my $next = $walk->{next};
210 if ($next && $seen{$next}) {
211 $walk->{next} = $next = undef;
215 $next = $child if $child;
220 while ($up && !$next) {
222 $next = $up->{next} if $up;
227 foreach my $cont (@visited) {
228 my $children = $cont->children;
229 next if @$children < 2;
230 $children = $ordersub->($children);
231 $cont = $cont->{child} = shift @$children;
233 $cont = $cont->{next} = shift @$children;