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)
88 my $root = _get_cont_for_id($self, 'fakeroot');
89 $root->add_child( $_ ) for @{ $self->{rootset} };
92 $root->order_children( $ordersub );
95 my $kids = $root->children;
96 $self->{rootset} = $kids;
97 $root->remove_child($_) for @$kids;
100 package PublicInbox::SearchThread::Msg;
102 use Scalar::Util qw(weaken);
104 sub new { my $self = shift; bless { id => shift }, $self; }
107 my ($self, $child) = @_;
108 croak "Cowardly refusing to become my own parent: $self"
111 if (grep { $_ == $child } @{$self->children}) {
112 # All is potentially correct with the world
113 weaken($child->{parent} = $self);
117 my $parent = $child->{parent};
118 remove_child($parent, $child) if $parent;
120 $child->{next} = $self->{child};
121 $self->{child} = $child;
122 weaken($child->{parent} = $self);
126 my ($self, $child) = @_;
128 my $x = $self->{child} or return;
129 if ($x == $child) { # First one's easy.
130 $self->{child} = $child->{next};
131 $child->{parent} = $child->{next} = undef;
136 while ($x = $x->{next}) {
138 $prev->{next} = $x->{next}; # Unlink x
139 $x->{next} = $x->{parent} = undef; # Deparent it
144 # oddly, we can get here
145 $child->{next} = $child->{parent} = undef;
151 die "Assertion failed: $child" unless eval {$child};
153 $self->recurse_down(sub { $there = 1 if $_[0] == $child });
161 my $visitor = $self->{child};
163 push @children, $visitor;
164 $visitor = $visitor->{next};
170 my ($self, $children) = @_;
171 my $walk = $self->{child} = shift @$children;
173 $walk = $walk->{next} = shift @$children;
179 my $ordersub = shift;
181 return unless $ordersub;
185 my $children = $cont->children;
186 return if @$children < 2;
187 $cont->set_children( $ordersub->( $children ) );
189 $self->iterate_down( undef, $sub );
193 # non-recursive version of recurse_down to avoid stack depth warnings
195 my ($self, $callback) = @_;
198 while (my $cont = shift @q) {
202 if (my $next = $cont->{next}) {
204 $cont->{next} = undef;
209 if (my $child = $cont->{child}) {
211 $cont->{child} = undef;
221 my ($before, $after) = @_;
228 push @visited, [ $walk, $depth ];
229 $before->($walk, $depth) if $before;
234 my $child = $walk->{child};
235 if ($child && $seen{$child}) {
236 $walk->{child} = $child = undef;
239 my $next = $walk->{next};
240 if ($next && $seen{$next}) {
241 $walk->{next} = $next = undef;
253 while ($up && !$next) {
256 $next = $up->{next} if $up;
261 return unless $after;
262 while (@visited) { $after->(@{ pop @visited }) }