Box/Thread/Manager.pm

filter: Statementstmt 82.9% 151 / 182 Branchbran 58.1% 43 / 74 Conditioncond 46.4% 13 / 28 MC/DCmcdc 0.0% 0 / 17 Subroutinesub 88.2% 30 / 34 Podpod 94.7% 18 / 19 totaltotal 72.0% 255 / 354 CC 65 SCAR 19.8
CC
65
Cov
73%
_cleanup
14.7
_processDelayedMessage
14.4
__ANON__
12.0
CRAP 7.3
« Box/MH/Labels.pm ↑ Summary Box/Thread/Node.pm »
1 # This code is part of Perl distribution Mail-Box version 4.02.
2 # The POD got stripped from this file by OODoc version 3.06.
3 # For contributors see file ChangeLog.
4  
5 # This software is copyright (c) 2001-2026 by Mark Overmeer.
6  
7 # This is free software; you can redistribute it and/or modify it under
8 # the same terms as the Perl 5 programming language system itself.
9 # SPDX-License-Identifier: Artistic-1.0-Perl OR GPL-1.0-or-later
10  
11  
12 package Mail::Box::Thread::Manager;{
13 4 our $VERSION = '4.02';
14 }
15  
16 4 ▶use parent 'Mail::Reporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 4 ▶use Log::Report 'mail-box', import => [ qw/__x error/ ];
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 4 ▶use Mail::Box::Thread::Node ();
Subroutine BEGIN: called CC=1 SCAR=0.0
24 4 ▶use Mail::Message::Dummy ();
Subroutine BEGIN: called CC=1 SCAR=0.0
25 4 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 #--------------------
28  
29 sub init($)
30 3 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=8 SCAR=22.4
Pod undocumented
31 $self->{MBTM_manager} = $args->{manager}
32 3 ▶ or error __x"thread manager needs a folder manager to work with.";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MBTM_manager"} = $args->{"manager"} or &error(&__x("thread manager needs a folder manager to work with.")) 3 0 3
33  
34 3 ▶ $self->{MBTM_thread_body}= $args->{thread_body} // 0;
Condition50%1/2
$args->{"thread_body"} // 0
l!l
03
MC/DC0%0/1
$args->{"thread_body"} // 0
$args->{"thread_body"}
Truth table50%1/2
A
A= $args->{"thread_body"}
Aresult
11
00
35 3 ▶ $self->{MBTM_thread_type}= $args->{thread_type} // 'Mail::Box::Thread::Node';
Condition50%1/2
$args->{"thread_type"} // "Mail::Box::Thread::Node"
l!l
03
MC/DC0%0/1
$args->{"thread_type"} // "Mail::Box::Thread::Node"
$args->{"thread_type"}
Truth table50%1/2
A
A= $args->{"thread_type"}
Aresult
11
00
36 3 ▶ $self->{MBTM_dummy_type} = $args->{dummy_type} // 'Mail::Message::Dummy';
Condition50%1/2
$args->{"dummy_type"} // "Mail::Message::Dummy"
l!l
03
MC/DC0%0/1
$args->{"dummy_type"} // "Mail::Message::Dummy"
$args->{"dummy_type"}
Truth table50%1/2
A
A= $args->{"dummy_type"}
Aresult
11
00
37 3 ▶ $self->{MBTM_window} = $args->{window} // 10;
Condition50%1/2
$args->{"window"} // 10
l!l
03
MC/DC0%0/1
$args->{"window"} // 10
$args->{"window"}
Truth table50%1/2
A
A= $args->{"window"}
Aresult
11
00
38 3 $self->{MBTM_ids} = +{ };
39 3 $_[0]->{MBTM_folders} = +{ };
40  
41 3 ▶ my $ts = $args->{timespan} || '3 days';
Condition50%1/2
$args->{'timespan'} || '3 days'
l!l
03
MC/DC0%0/1
$args->{'timespan'} || '3 days'
$args->{'timespan'}
Truth table50%1/2
A
A= $args->{'timespan'}
Aresult
11
00
42 3 ▶ $self->{MBTM_timespan} = $ts eq 'EVER' ? 'EVER' : Mail::Box->timespan2seconds($ts);
Branch: 1 branch
BranchTrueFalseTotal
$ts eq 'EVER' ? : 0 3 3
43 3 $self;
44 }
45  
46 #--------------------
47  
48 53 ▶sub folderIndex() { $_[0]->{MBTM_folders} }
Subroutine folderIndex: called CC=1 SCAR=0.0
Pod documented
49 2 ▶sub folders() { values %{$_[0]->folderIndex} }
Subroutine folders: called CC=1 SCAR=0.0
Pod documented
50 45 ▶sub folder($) { $_[0]->folderIndex->{$_[1]} }
Subroutine folder: called CC=1 SCAR=0.0
Pod documented
51  
52  
53 565 ▶sub byId { $_[0]->{MBTM_ids} }
Subroutine byId: called CC=1 SCAR=0.0
Pod documented
54  
55  
56 547 ▶sub msgById($) { my $ids = $_[0]->byId; @_ > 2 ? $ids->{$_[1]} = $_[2] : $ids->{$_[1]} }
Subroutine msgById: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ > 2 ? : 135 412 547
57  
58 #--------------------
59  
60 sub includeFolder(@)
61 3 ▶{ my $self = shift;
Subroutine includeFolder: called CC=6 SCAR=18.8
Pod documented
62 3 my $index = $self->folderIndex;
63  
64 3 foreach my $folder (@_)
65 3 ▶ { blessed $folder && $folder->isa('Mail::Box')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($folder) and $folder->isa("Mail::Box") or &error(&__x("attempt to include a none folder: {what UNKNOWN}.", "what", $folder)) 3 0 3
Condition33%1/3
builtin::blessed($folder) and $folder->isa("Mail::Box")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($folder) and $folder->isa("Mail::Box")
builtin::blessed($folder) $folder->isa("Mail::Box")
Truth table33%1/3
A and B
A= builtin::blessed($folder)B= $folder->isa("Mail::Box")
ABresult
0X0
100
111
66 or error __x"attempt to include a none folder: {what UNKNOWN}.", what => $folder;
67  
68 3 my $name = $folder->name;
69 3 ▶ next if exists $index->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $index->{$name} 0 3 3
70  
71 3 $index->{$name} = $folder;
72 3 $self->inThread($_) for grep ! $_->head->isDelayed, $folder->messages;
73 }
74  
75 3 $self;
76 }
77  
78  
79 sub removeFolder(@)
80 3 ▶{ my $self = shift;
Subroutine removeFolder: called CC=7 SCAR=20.9
Pod documented
81 3 my $index = $self->folderIndex;
82  
83 3 foreach my $folder (@_)
84 3 ▶ { blessed $folder && $folder->isa('Mail::Box')
Branch: 1 branch
BranchTrueFalseTotal
builtin::blessed($folder) and $folder->isa("Mail::Box") or &error(&__x("attempt to remove a none folder: {what UNKNOWN}.", "what", $folder)) 3 0 3
Condition33%1/3
builtin::blessed($folder) and $folder->isa("Mail::Box")
!ll&&!rl&&r
003
MC/DC0%0/2
builtin::blessed($folder) and $folder->isa("Mail::Box")
builtin::blessed($folder) $folder->isa("Mail::Box")
Truth table33%1/3
A and B
A= builtin::blessed($folder)B= $folder->isa("Mail::Box")
ABresult
0X0
100
111
85 or error __x"attempt to remove a none folder: {what UNKNOWN}.", what => $folder;
86  
87 3 my $name = $folder->name;
88 3 ▶ delete $index->{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
delete $index->{$name} or next 3 0 3
89  
90 $_->headIsRead && $self->outThread($_)
91 3 ▶ for $folder->messages;
Branch: 1 branch
BranchTrueFalseTotal
$_->headIsRead and $self->outThread($_) 135 0 135
92  
93 3 $self->{MBTM_cleanup_needed} = 1;
94 }
95  
96 3 $self;
97 }
98  
99 #--------------------
100  
101 sub thread($)
102 4 ▶{ my ($self, $message) = @_;
Subroutine thread: called CC=6 SCAR=23.5
Pod documented
103 4 my $msgid = $message->messageId;
104 4 my $timestamp = $message->timestamp;
105  
106 4 $self->_processDelayedNodes;
107 4 ▶ my $thread = $self->msgById($msgid) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $thread = $self->msgById($msgid) or return 4 0 4
108  
109 4 my @missing;
110 10 ▶ $thread->recurse( sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
111 10 my $node = shift;
112 10 ▶ push @missing, $node->messageId if $node->isDummy;
Branch: 1 branch
BranchTrueFalseTotal
if $node->isDummy 0 10 10
113 10 1;
114 4 });
115  
116 4 ▶ @missing or return $thread;
Branch: 1 branch
BranchTrueFalseTotal
@missing or return $thread 0 4 4
117  
118 0 foreach my $folder ($self->folders)
119 {
120 # Pull-in all messages received after this-one, from any folder.
121 # Clocks may drift a bit, so use margin.
122 0 my @now_missing = $folder->scanForMessages($msgid, \@missing, $timestamp - 3600, 0);
123  
124 0 ▶ if(@now_missing != @missing)
Branch: 1 branch
BranchTrueFalseTotal
if (@now_missing != @missing) 0 0 0
125 0 { $self->_processDelayedNodes;
126 0 ▶ @now_missing or last;
Branch: 1 branch
BranchTrueFalseTotal
@now_missing or last 0 0 0
127 0 @missing = @now_missing;
128 }
129 }
130  
131 0 $thread;
132 }
133  
134  
135 sub threadStart($)
136 3 ▶{ my ($self, $message) = @_;
Subroutine threadStart: called CC=7 SCAR=19.6
Pod documented
137 3 ▶ my $thread = $self->thread($message) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $thread = $self->thread($message) or return 3 0 3
138  
139 3 ▶ while(my $parent = $thread->repliedTo)
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $thread->repliedTo) 2 3 5
140 2 ▶ { unless($parent->isDummy)
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent->isDummy) 1 1 2
141 { # Message already found, no special action to be taken.
142 1 $thread = $parent;
143 1 next;
144 }
145  
146 1 foreach my $folder ($self->folders)
147 1 { my $message = $thread->message;
148 1 ▶ my $timespan = $message->isDummy ? 'ALL' : $message->timestamp - $self->{MBTM_timespan};
Branch: 1 branch
BranchTrueFalseTotal
$message->isDummy ? : 0 1 1
149  
150 $folder->scanForMessages($thread->messageId, $parent->messageId, $timespan, $self->{MBTM_window})
151 1 ▶ or last;
Branch: 1 branch
BranchTrueFalseTotal
$folder->scanForMessages($thread->messageId, $parent->messageId, $timespan, $self->{"MBTM_window"}) or last 1 0 1
152 }
153  
154 1 $self->_processDelayedNodes;
155 1 $thread = $parent;
156 }
157  
158 3 $thread;
159 }
160  
161  
162 sub all()
163 0 ▶{ my $self = shift;
Subroutine all: not called CC=2 SCAR=17.9
Pod documented
164 0 $_->find('not-existing') for $self->folders;
165 0 $self->known;
166 }
167  
168  
169 sub sortedAll(@)
170 1 ▶{ my $self = shift;
Subroutine sortedAll: called CC=2 SCAR=6.9
Pod documented
171 1 $_->find('not-existing') for $self->folders;
172 1 $self->sortedKnown(@_);
173 }
174  
175  
176 sub known()
177 3 ▶{ my $self = shift->_processDelayedNodes->_cleanup;
Subroutine known: called CC=1 SCAR=0.0
Pod documented
178 3 grep !defined $_->repliedTo, values %{$self->byId};
179 }
180  
181  
182 sub sortedKnown(;$$)
183 2 ▶{ my $self = shift;
Subroutine sortedKnown: called CC=4 SCAR=14.6
Pod documented
184 2 ▶ my $prepare = shift || sub { $_[0]->startTimeEstimate || 0 };
Subroutine __ANON__: called CC=2 SCAR=8.4
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->startTimeEstimate or 0 56 0 56
Condition33%1/3
shift() || sub { $_[0]->startTimeEstimate or 0; }
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() || sub { $_[0]->startTimeEstimate or 0; }
shift() sub { $_[0]->startTimeEstimate or 0; }
Truth table33%1/3
A || B
A= shift()B= sub { $_[0]->startTimeEstimate or 0; }
ABresult
1X1
011
000
185 2 ▶ my $compare = shift || sub { $_[0] <=> $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.6
Condition33%1/3
shift() || sub { $_[0] <=> $_[1]; }
l!l&&r!l&&!r
020
MC/DC0%0/2
shift() || sub { $_[0] <=> $_[1]; }
shift() sub { $_[0] <=> $_[1]; }
Truth table33%1/3
A || B
A= shift()B= sub { $_[0] <=> $_[1]; }
ABresult
1X1
011
000
186  
187 # Special care for double keys.
188 2 my %value;
189 2 push @{$value{$prepare->($_)}}, $_ for $self->known;
190 2 map @{$value{$_}}, sort {$compare->($a, $b)} keys %value;
191 }
192  
193 # When a whole folder is removed, many threads can become existing
194 # only of dummies. They must be removed.
195  
196 sub _cleanup()
197 3 ▶{ my $self = shift;
Subroutine _cleanup: called CC=4 SCAR=26.9
198 3 ▶ $self->{MBTM_cleanup_needed} or return $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MBTM_cleanup_needed"} or return $self 0 3 3
199  
200 0 foreach my $thread ($self->known)
201 0 { my $real = 0;
202 0 ▶ $thread->recurse( sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
203 0 my $node = shift;
204 0 foreach my $msg ($node->messages)
205 0 ▶ { next if $msg->isDummy;
Branch: 1 branch
BranchTrueFalseTotal
if $msg->isDummy 0 0 0
206 0 $real = 1;
207 0 return 0;
208 }
209 0 1;
210 0 });
211  
212 0 ▶ next if $real;
Branch: 1 branch
BranchTrueFalseTotal
if $real 0 0 0
213  
214 0 ▶ $thread->recurse( sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
215 0 my $node = shift;
216 0 delete $self->byId->{$node->messageId};
217 0 1;
218 0 });
219 }
220  
221 0 delete $self->{MBTM_cleanup_needed};
222 0 $self;
223 }
224  
225 #--------------------
226  
227 sub toBeThreaded($@)
228 45 ▶{ my ($self, $folder) = (shift, shift);
Subroutine toBeThreaded: called CC=3 SCAR=11.1
Pod documented
229 45 ▶ $self->folder($folder->name) or return $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->folder($folder->name) or return $self 45 0 45
230 45 $self->inThread($_) for @_;
231 45 $self;
232 }
233  
234  
235 sub toBeUnthreaded($@)
236 0 ▶{ my ($self, $folder) = (shift, shift);
Subroutine toBeUnthreaded: not called CC=3 SCAR=24.8
Pod documented
237 0 ▶ $self->folder($folder->name) or return $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->folder($folder->name) or return $self 0 0 0
238 0 $self->outThread($_) for @_;
239 0 $self;
240 }
241  
242  
243 sub inThread($)
244 135 ▶{ my ($self, $message) = @_;
Subroutine inThread: called CC=2 SCAR=7.1
Pod documented
245 135 my $msgid = $message->messageId;
246 135 my $node = $self->msgById($msgid);
247  
248 # Already known, but might reside in many folders.
249 0 ▶ if($node) { $node->addMessage($message) }
Branch: 1 branch
BranchTrueFalseTotal
if ($node) { } 0 135 135
250 else
251 135 { $node = Mail::Box::Thread::Node->new(message => $message, msgid => $msgid, dummy_type => $self->{MBTM_dummy_type});
252 135 $self->msgById($msgid, $node);
253 }
254  
255 135 $self->{MBTM_delayed}{$msgid} = $node; # removes doubles.
256 }
257  
258 # The relation between nodes is delayed, to avoid that first
259 # dummy nodes have to be made, and then immediately upgrades
260 # to real nodes. So: at first we inventory what we have, and
261 # then build thread-lists.
262  
263 sub _processDelayedNodes()
264 8 ▶{ my $self = shift;
Subroutine _processDelayedNodes: called CC=4 SCAR=13.9
265 8 ▶ $self->{MBTM_delayed} or return $self;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MBTM_delayed"} or return $self 3 5 8
266  
267 3 foreach my $node (values %{$self->{MBTM_delayed}})
268 135 { $self->_processDelayedMessage($node, $_) for $node->message;
269 }
270  
271 3 delete $self->{MBTM_delayed};
272 3 $self;
273 }
274  
275 sub _processDelayedMessage($$)
276 135 ▶{ my ($self, $node, $message) = @_;
Subroutine _processDelayedMessage: called CC=14 SCAR=26.7
277 135 my $msgid = $message->messageId;
278  
279 # will force parsing of head when not done yet.
280 135 ▶ my $head = $message->head or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $head = $message->head or return $self 135 0 135
281  
282 135 my $replies;
283 135 ▶ if(my $irt = $head->get('in-reply-to'))
Branch: 1 branch
BranchTrueFalseTotal
if (my $irt = $head->get("in-reply-to")) 54 81 135
284 54 { for($irt =~ m/\<(\S+\@\S+)\>/)
285 54 { my $msgid = $1;
286 54 ▶ $replies = $self->msgById($msgid) || $self->createDummy($msgid);
Condition66%2/3
$self->msgById($msgid) || $self->createDummy($msgid)
l!l&&r!l&&!r
4770
MC/DC0%0/2
$self->msgById($msgid) || $self->createDummy($msgid)
$self->msgById($msgid) $self->createDummy($msgid)
Truth table66%2/3
A || B
A= $self->msgById($msgid)B= $self->createDummy($msgid)
ABresult
1X1
011
000
287 }
288 }
289  
290 135 my @refs;
291 135 ▶ if(my $refs = $head->get('references'))
Branch: 1 branch
BranchTrueFalseTotal
if (my $refs = $head->get("references")) 54 81 135
292 54 ▶ { while($refs =~ s/\<(\S+\@\S+)\>//s)
Branch: 1 branch
BranchTrueFalseTotal
if ($refs =~ s/\<(\S+\@\S+)\>//s) 84 54 138
293 84 { my $msgid = $1;
294 84 ▶ push @refs, $self->msgById($msgid) || $self->createDummy($msgid);
Condition66%2/3
$self->msgById($msgid) || $self->createDummy($msgid)
l!l&&r!l&&!r
7680
MC/DC0%0/2
$self->msgById($msgid) || $self->createDummy($msgid)
$self->msgById($msgid) $self->createDummy($msgid)
Truth table66%2/3
A || B
A= $self->msgById($msgid)B= $self->createDummy($msgid)
ABresult
1X1
011
000
295 }
296 }
297  
298 # Handle the `In-Reply-To' message header.
299 # This is the most secure relationship.
300  
301 135 ▶ if($replies)
Branch: 1 branch
BranchTrueFalseTotal
if ($replies) 54 81 135
302 54 ▶ { $node->follows($replies, 'REPLY')
Branch: 1 branch
BranchTrueFalseTotal
$node->follows($replies, "REPLY") and $replies->followedBy($node) 54 0 54
303 and $replies->followedBy($node);
304 }
305  
306 # Handle the `References' message header.
307 # The (ordered) list of message-IDs give an impression where this
308 # message resides in the thread. There is a little less certainty
309 # that the list is correctly ordered and correctly maintained.
310  
311 135 ▶ if(@refs)
Branch: 1 branch
BranchTrueFalseTotal
if (@refs) 54 81 135
312 54 ▶ { push @refs, $node unless $refs[-1] eq $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $refs[-1] eq $node 54 0 54
313 54 my $from = shift @refs;
314  
315 54 ▶ while(my $to = shift @refs)
Branch: 1 branch
BranchTrueFalseTotal
if (my $to = shift @refs) 84 54 138
316 84 ▶ { $to->follows($from, 'REFERENCE')
Branch: 1 branch
BranchTrueFalseTotal
$to->follows($from, "REFERENCE") and $from->followedBy($to) 84 0 84
317 and $from->followedBy($to);
318 84 $from = $to;
319 }
320 }
321  
322 135 $self;
323 }
324  
325  
326 sub outThread($)
327 135 ▶{ my ($self, $message) = @_;
Subroutine outThread: called CC=2 SCAR=6.9
Pod documented
328 135 my $msgid = $message->messageId;
329 135 ▶ my $node = $self->msgById($msgid) or return $message;
Branch: 1 branch
BranchTrueFalseTotal
my $node = $self->msgById($msgid) or return $message 134 1 135
330  
331 134 $node->{MBTM_messages} = [ grep $_ ne $message, @{$node->{MBTM_messages}} ];
332 134 $self;
333 }
334  
335  
336 sub createDummy($)
337 15 ▶{ my ($self, $msgid) = @_;
Subroutine createDummy: called CC=1 SCAR=0.0
Pod documented
338 15 $self->byId->{$msgid} = $self->{MBTM_thread_type}->new(msgid => $msgid, dummy_type => $self->{MBTM_dummy_type});
339 }
340  
341 #--------------------
342  
343 4 1;
« Box/MH/Labels.pm ↑ Summary Box/Thread/Node.pm »