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/References.
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;
25 my ($messages, $ordersub, $srch) = @_;
27 _add_message($id_table, $_) foreach @$messages;
28 my $rootset = [ grep {
29 !delete($_->{parent}) && $_->visible($srch)
30 } values %$id_table ];
32 $rootset = $ordersub->($rootset);
33 $_->order_children($ordersub, $srch) for @$rootset;
37 sub _get_cont_for_id ($$) {
38 my ($id_table, $mid) = @_;
39 $id_table->{$mid} ||= PublicInbox::SearchThread::Msg->new($mid);
42 sub _add_message ($$) {
43 my ($id_table, $smsg) = @_;
46 my $this = _get_cont_for_id($id_table, $smsg->{mid});
47 $this->{smsg} = $smsg;
49 # B. For each element in the message's References field:
50 defined(my $refs = $smsg->{references}) or return;
52 # This loop exists to help fill in gaps left from missing
53 # messages. It is not needed in a perfect world where
54 # everything is perfectly referenced, only the last ref
57 foreach my $ref ($refs =~ m/<([^>]+)>/g) {
58 # Find a Container object for the given Message-ID
59 my $cont = _get_cont_for_id($id_table, $ref);
61 # Link the References field's Containers together in
62 # the order implied by the References header
64 # * If they are already linked don't change the
66 # * Do not add a link if adding that link would
69 !$cont->{parent} && # already linked
70 !$cont->has_descendent($prev) # would loop
72 $prev->add_child($cont);
77 # C. Set the parent of this message to be the last element in
79 $prev->add_child($this) if defined $prev;
82 package PublicInbox::SearchThread::Msg;
90 children => {}, # becomes an array when sorted by ->order(...)
97 while (my $cont = shift @q) {
98 return $cont if $cont->{smsg};
99 push @q, values %{$cont->{children}};
105 my ($self, $child) = @_;
106 croak "Cowardly refusing to become my own parent: $self"
109 my $cid = $child->{id};
112 if (defined(my $parent = $child->{parent})) {
113 delete $parent->{children}->{$cid};
116 $self->{children}->{$cid} = $child;
117 $child->{parent} = $self;
121 my ($self, $child) = @_;
122 my %seen; # loop prevention
124 return 1 if $self == $child || $seen{$child}++;
125 $child = $child->{parent};
130 # Do not show/keep ghosts iff they have no children. Sometimes
131 # a ghost Message-ID is the result of a long header line
132 # being folded/mangled by a MUA, and not a missing message.
134 my ($self, $srch) = @_;
135 ($self->{smsg} ||= eval { $srch->lookup_mail($self->{id}) }) ||
136 (scalar values %{$self->{children}});
140 my ($cur, $ordersub, $srch) = @_;
142 my %seen = ($cur => 1); # self-referential loop prevention
144 while (defined($cur = shift @q)) {
145 my $c = $cur->{children}; # The hashref here...
147 $c = [ grep { !$seen{$_}++ && visible($_, $srch) } values %$c ];
148 $c = $ordersub->($c) if scalar @$c > 1;
149 $cur->{children} = $c; # ...becomes an arrayref