]> Sergey Matveev's repositories - public-inbox.git/blob - lib/PublicInbox/SearchThread.pm
1d250b4672f0f674a4290136fe519b4cc83ff0a8
[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/References.
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 thread {
25         my ($messages, $ordersub, $ibx) = @_;
26         my $id_table = {};
27         _add_message($id_table, $_) foreach @$messages;
28         my $rootset = [ grep {
29                         !delete($_->{parent}) && $_->visible($ibx)
30                 } values %$id_table ];
31         $id_table = undef;
32         $rootset = $ordersub->($rootset);
33         $_->order_children($ordersub, $ibx) for @$rootset;
34         $rootset;
35 }
36
37 sub _get_cont_for_id ($$) {
38         my ($id_table, $mid) = @_;
39         $id_table->{$mid} ||= PublicInbox::SearchThread::Msg->new($mid);
40 }
41
42 sub _add_message ($$) {
43         my ($id_table, $smsg) = @_;
44
45         # A. if id_table...
46         my $this = _get_cont_for_id($id_table, $smsg->{mid});
47         $this->{smsg} = $smsg;
48
49         # B. For each element in the message's References field:
50         defined(my $refs = $smsg->{references}) or return;
51
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
55         # matters.
56         my $prev;
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);
60
61                 # Link the References field's Containers together in
62                 # the order implied by the References header
63                 #
64                 # * If they are already linked don't change the
65                 #   existing links
66                 # * Do not add a link if adding that link would
67                 #   introduce a loop...
68                 if ($prev &&
69                         !$cont->{parent} &&  # already linked
70                         !$cont->has_descendent($prev) # would loop
71                    ) {
72                         $prev->add_child($cont);
73                 }
74                 $prev = $cont;
75         }
76
77         # C. Set the parent of this message to be the last element in
78         # References.
79         $prev->add_child($this) if defined $prev;
80 }
81
82 package PublicInbox::SearchThread::Msg;
83 use strict;
84 use warnings;
85 use Carp qw(croak);
86
87 sub new {
88         bless {
89                 id => $_[1],
90                 children => {}, # becomes an array when sorted by ->order(...)
91         }, $_[0];
92 }
93
94 sub topmost {
95         my ($self) = @_;
96         my @q = ($self);
97         while (my $cont = shift @q) {
98                 return $cont if $cont->{smsg};
99                 push @q, values %{$cont->{children}};
100         }
101         undef;
102 }
103
104 sub add_child {
105         my ($self, $child) = @_;
106         croak "Cowardly refusing to become my own parent: $self"
107           if $self == $child;
108
109         my $cid = $child->{id};
110
111         # reparenting:
112         if (defined(my $parent = $child->{parent})) {
113                 delete $parent->{children}->{$cid};
114         }
115
116         $self->{children}->{$cid} = $child;
117         $child->{parent} = $self;
118 }
119
120 sub has_descendent {
121         my ($self, $child) = @_;
122         my %seen; # loop prevention
123         while ($child) {
124                 return 1 if $self == $child || $seen{$child}++;
125                 $child = $child->{parent};
126         }
127         0;
128 }
129
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.
133 sub visible ($$) {
134         my ($self, $ibx) = @_;
135         ($self->{smsg} ||= eval { $ibx->smsg_by_mid($self->{id}) }) ||
136          (scalar values %{$self->{children}});
137 }
138
139 sub order_children {
140         my ($cur, $ordersub, $ibx) = @_;
141
142         my %seen = ($cur => 1); # self-referential loop prevention
143         my @q = ($cur);
144         while (defined($cur = shift @q)) {
145                 my $c = $cur->{children}; # The hashref here...
146
147                 $c = [ grep { !$seen{$_}++ && visible($_, $ibx) } values %$c ];
148                 $c = $ordersub->($c) if scalar @$c > 1;
149                 $cur->{children} = $c; # ...becomes an arrayref
150                 push @q, @$c;
151         }
152 }
153
154 1;