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;
36 grep { !$_->{parent} } values %{$self->{id_table}} ];
42 delete $self->{id_table};
46 sub _get_cont_for_id ($$) {
47 my ($self, $mid) = @_;
48 $self->{id_table}{$mid} ||= PublicInbox::SearchThread::Msg->new($mid);
54 _add_message($self, $_) foreach @{$self->{messages}};
57 sub _add_message ($$) {
58 my ($self, $smsg) = @_;
61 my $this = _get_cont_for_id($self, $smsg->{mid});
62 $this->{smsg} = $smsg;
64 # B. For each element in the message's References field:
66 if (defined(my $refs = $smsg->{references})) {
67 foreach my $ref ($refs =~ m/<([^>]+)>/g) {
68 # Find a Container object for the given Message-ID
69 my $cont = _get_cont_for_id($self, $ref);
71 # Link the References field's Containers together in
72 # the order implied by the References header
74 # * If they are already linked don't change the
76 # * Do not add a link if adding that link would
79 !$cont->{parent} && # already linked
80 !$cont->has_descendent($prev) # would loop
82 $prev->add_child($cont);
88 # C. Set the parent of this message to be the last element in
90 if ($prev && !$this->has_descendent($prev)) { # would loop
91 $prev->add_child($this)
100 my $root = _get_cont_for_id($self, 'fakeroot');
101 $root->add_child( $_ ) for @{ $self->{rootset} };
104 $root->order_children( $ordersub );
107 my $kids = $root->children;
108 $self->{rootset} = $kids;
109 $root->remove_child($_) for @$kids;
112 package PublicInbox::SearchThread::Msg;
114 use Scalar::Util qw(weaken);
116 sub new { my $self = shift; bless { id => shift }, $self; }
119 my ($self, $child) = @_;
120 croak "Cowardly refusing to become my own parent: $self"
123 if (grep { $_ == $child } @{$self->children}) {
124 # All is potentially correct with the world
125 weaken($child->{parent} = $self);
129 my $parent = $child->{parent};
130 remove_child($parent, $child) if $parent;
132 $child->{next} = $self->{child};
133 $self->{child} = $child;
134 weaken($child->{parent} = $self);
138 my ($self, $child) = @_;
140 my $x = $self->{child} or return;
141 if ($x == $child) { # First one's easy.
142 $self->{child} = $child->{next};
143 $child->{parent} = $child->{next} = undef;
148 while ($x = $x->{next}) {
150 $prev->{next} = $x->{next}; # Unlink x
151 $x->{next} = $x->{parent} = undef; # Deparent it
156 # oddly, we can get here
157 $child->{next} = $child->{parent} = undef;
163 die "Assertion failed: $child" unless eval {$child};
165 $self->recurse_down(sub { $there = 1 if $_[0] == $child });
173 my $visitor = $self->{child};
175 push @children, $visitor;
176 $visitor = $visitor->{next};
182 my ($self, $children) = @_;
183 my $walk = $self->{child} = shift @$children;
185 $walk = $walk->{next} = shift @$children;
191 my $ordersub = shift;
193 return unless $ordersub;
197 my $children = $cont->children;
198 return if @$children < 2;
199 $cont->set_children( $ordersub->( $children ) );
201 $self->iterate_down( undef, $sub );
205 # non-recursive version of recurse_down to avoid stack depth warnings
207 my ($self, $callback) = @_;
210 while (my $cont = shift @q) {
214 if (my $next = $cont->{next}) {
216 $cont->{next} = undef;
221 if (my $child = $cont->{child}) {
223 $cont->{child} = undef;
233 my ($before, $after) = @_;
240 push @visited, [ $walk, $depth ];
241 $before->($walk, $depth) if $before;
246 my $child = $walk->{child};
247 if ($child && $seen{$child}) {
248 $walk->{child} = $child = undef;
251 my $next = $walk->{next};
252 if ($next && $seen{$next}) {
253 $walk->{next} = $next = undef;
265 while ($up && !$next) {
268 $next = $up->{next} if $up;
273 return unless $after;
274 while (@visited) { $after->(@{ pop @visited }) }