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