]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchThread.pm
searchthread: update comment about loop prevention
[public-inbox.git] / lib / PublicInbox / SearchThread.pm
1 # This library is free software; you can redistribute it and/or modify
2 # it under the same terms as Perl itself.
3 #
4 # This license differs from the rest of public-inbox
5 #
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.
11 #
12 # This includes fixes from several open bugs for Mail::Thread
13 #
14 # Avoid circular references
15 # - https://rt.cpan.org/Public/Bug/Display.html?id=22817
16 #
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;
21 use strict;
22 use warnings;
23
24 sub new {
25         return bless {
26                 messages => $_[1],
27                 id_table => {},
28                 rootset  => []
29         }, $_[0];
30 }
31
32 sub thread {
33         my $self = shift;
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 ];
38 }
39
40 sub _get_cont_for_id ($$) {
41         my ($self, $mid) = @_;
42         $self->{id_table}{$mid} ||= PublicInbox::SearchThread::Msg->new($mid);
43 }
44
45 sub _add_message ($$) {
46         my ($self, $smsg) = @_;
47
48         # A. if id_table...
49         my $this = _get_cont_for_id($self, $smsg->{mid});
50         $this->{smsg} = $smsg;
51
52         # B. For each element in the message's References field:
53         defined(my $refs = $smsg->{references}) or return;
54
55         # This loop exists to help fill in gaps left from missing
56         # messages.  It is not needed in a perfect world where
57         # everything is perfectly referenced, only the last ref
58         # matters.
59         my $prev;
60         foreach my $ref ($refs =~ m/<([^>]+)>/g) {
61                 # Find a Container object for the given Message-ID
62                 my $cont = _get_cont_for_id($self, $ref);
63
64                 # Link the References field's Containers together in
65                 # the order implied by the References header
66                 #
67                 # * If they are already linked don't change the
68                 #   existing links
69                 # * Do not add a link if adding that link would
70                 #   introduce a loop...
71                 if ($prev &&
72                         !$cont->{parent} &&  # already linked
73                         !$cont->has_descendent($prev) # would loop
74                    ) {
75                         $prev->add_child($cont);
76                 }
77                 $prev = $cont;
78         }
79
80         # C. Set the parent of this message to be the last element in
81         # References.
82         $prev->add_child($this) if defined $prev;
83 }
84
85 sub order {
86         my ($self, $ordersub) = @_;
87         my $rootset = $ordersub->($self->{rootset});
88         $self->{rootset} = $rootset;
89         $_->order_children($ordersub) for @$rootset;
90 }
91
92 package PublicInbox::SearchThread::Msg;
93 use strict;
94 use warnings;
95 use Carp qw(croak);
96
97 sub new {
98         bless {
99                 id => $_[1],
100                 children => {}, # becomes an array when sorted by ->order(...)
101         }, $_[0];
102 }
103
104 sub topmost {
105         my ($self) = @_;
106         my @q = ($self);
107         while (my $cont = shift @q) {
108                 return $cont if $cont->{smsg};
109                 push @q, values %{$cont->{children}};
110         }
111         undef;
112 }
113
114 sub add_child {
115         my ($self, $child) = @_;
116         croak "Cowardly refusing to become my own parent: $self"
117           if $self == $child;
118
119         my $cid = $child->{id};
120
121         # reparenting:
122         if (defined(my $parent = $child->{parent})) {
123                 delete $parent->{children}->{$cid};
124         }
125
126         $self->{children}->{$cid} = $child;
127         $child->{parent} = $self;
128 }
129
130 sub has_descendent {
131         my ($self, $child) = @_;
132         my %seen; # loop prevention
133         while ($child) {
134                 return 1 if $self == $child || $seen{$child}++;
135                 $child = $child->{parent};
136         }
137         0;
138 }
139
140 # Do not show/keep ghosts iff they have no children.  Sometimes
141 # a ghost Message-ID is the result of a long header line
142 # being folded/mangled by a MUA, and not a missing message.
143 sub visible ($) {
144         my ($self) = @_;
145         $self->{smsg} || scalar values %{$self->{children}};
146 }
147
148 sub order_children {
149         my ($cur, $ordersub) = @_;
150
151         my %seen = ($cur => 1); # self-referential loop prevention
152         my @q = ($cur);
153         while (defined($cur = shift @q)) {
154                 my $c = $cur->{children}; # The hashref here...
155
156                 $c = [ grep { !$seen{$_}++ && visible($_) } values %$c ];
157                 $c = $ordersub->($c) if scalar @$c > 1;
158                 $cur->{children} = $c; # ...becomes an arrayref
159                 push @q, @$c;
160         }
161 }
162
163 1;