Box/Thread/Manager.pm

filter: Statementstmt 12.9% 23 / 178 Branchbran 0.0% 0 / 72 Conditioncond 0.0% 0 / 32 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 23.3% 7 / 30 Podpod 93.3% 14 / 15 totaltotal 12.7% 44 / 346 CC 68 SCAR 41.7
CC
68
Cov
8%
_process_delayed_message
210.0
init
132.0
includeFolder
56.0
CRAP 64.5
« Box/MH.pm ↑ Summary Box/Net.pm »
1 # Copyrights 2001-2020 by [Mark Overmeer].
2 # For other contributors see ChangeLog.
3 # See the manual pages for details on the licensing terms.
4 # Pod stripped from pm file by OODoc 2.02.
5 # This code is part of distribution Mail-Box. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md
7 # Copyright Mark Overmeer. Licensed under the same terms as Perl itself.
8  
9 package Mail::Box::Thread::Manager;
10 1 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 $VERSION = '3.009';
12  
13 1 ▶use base 'Mail::Reporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 1 ▶use Mail::Box::Thread::Node;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use Mail::Message::Dummy;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22  
23 sub init($)
24 0 ▶{ my ($self, $args) = @_;
Subroutine init: not called CC=11 SCAR=48.8
Pod undocumented
25  
26 $self->{MBTM_manager} = $args->{manager}
27 0 ▶ or croak "Need a manager to work with.";
Branch: 1 branch
BranchTrueFalseTotal
$self->{"MBTM_manager"} = $args->{"manager"} or croak("Need a manager to work with.") 0 0 0
28  
29 0 ▶ $self->{MBTM_thread_body}= $args->{thread_body}|| 0;
Condition0%0/2
$args->{'thread_body'} || 0
l!l
00
MC/DC0%0/1
$args->{'thread_body'} || 0
$args->{'thread_body'}
Truth table0%0/2
A
A= $args->{'thread_body'}
Aresult
11
00
30 0 ▶ $self->{MBTM_thread_type}= $args->{thread_type}||'Mail::Box::Thread::Node';
Condition0%0/2
$args->{'thread_type'} || 'Mail::Box::Thread::Node'
l!l
00
MC/DC0%0/1
$args->{'thread_type'} || 'Mail::Box::Thread::Node'
$args->{'thread_type'}
Truth table0%0/2
A
A= $args->{'thread_type'}
Aresult
11
00
31 0 ▶ $self->{MBTM_dummy_type} = $args->{dummy_type} ||'Mail::Message::Dummy';
Condition0%0/2
$args->{'dummy_type'} || 'Mail::Message::Dummy'
l!l
00
MC/DC0%0/1
$args->{'dummy_type'} || 'Mail::Message::Dummy'
$args->{'dummy_type'}
Truth table0%0/2
A
A= $args->{'dummy_type'}
Aresult
11
00
32  
33 0 ▶ for($args->{timespan} || '3 days')
Condition0%0/2
$args->{'timespan'} || '3 days'
l!l
00
MC/DC0%0/1
$args->{'timespan'} || '3 days'
$args->{'timespan'}
Truth table0%0/2
A
A= $args->{'timespan'}
Aresult
11
00
34 0 ▶ { $self->{MBTM_timespan} = $_ eq 'EVER' ? 'EVER'
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'EVER' ? : 0 0 0
35 : Mail::Box->timespan2seconds($_);
36 }
37  
38 0 ▶ for($args->{window} || 10)
Condition0%0/2
$args->{'window'} || 10
l!l
00
MC/DC0%0/1
$args->{'window'} || 10
$args->{'window'}
Truth table0%0/2
A
A= $args->{'window'}
Aresult
11
00
39 0 ▶ { $self->{MBTM_window} = $_ eq 'ALL' ? 'ALL' : $_;
Branch: 1 branch
BranchTrueFalseTotal
$_ eq 'ALL' ? : 0 0 0
40 }
41 0 $self;
42 }
43  
44 #-------------------------------------------
45  
46 0 ▶sub folders() { values %{shift->{MBTM_folders}} }
Subroutine folders: not called CC=1 SCAR=6.9
Pod documented
47  
48  
49 sub includeFolder(@)
50 0 ▶{ my $self = shift;
Subroutine includeFolder: not called CC=7 SCAR=40.3
Pod documented
51  
52 0 foreach my $folder (@_)
53 0 ▶ { croak "Not a folder: $folder"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $folder and $folder->isa("Mail::Box") 0 0 0
Condition0%0/3
ref $folder and $folder->isa("Mail::Box")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $folder and $folder->isa("Mail::Box")
ref $folder $folder->isa("Mail::Box")
Truth table0%0/3
A and B
A= ref $folderB= $folder->isa("Mail::Box")
ABresult
0X0
100
111
54 unless ref $folder && $folder->isa('Mail::Box');
55  
56 0 my $name = $folder->name;
57 0 ▶ next if exists $self->{MBTM_folders}{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"MBTM_folders"}{$name} 0 0 0
58  
59 0 $self->{MBTM_folders}{$name} = $folder;
60 0 foreach my $msg ($folder->messages)
61 0 ▶ { $self->inThread($msg) unless $msg->head->isDelayed;
Branch: 1 branch
BranchTrueFalseTotal
unless $msg->head->isDelayed 0 0 0
62 }
63 }
64  
65 0 $self;
66 }
67  
68  
69 sub removeFolder(@)
70 0 ▶{ my $self = shift;
Subroutine removeFolder: not called CC=7 SCAR=40.3
Pod documented
71  
72 0 foreach my $folder (@_)
73 0 ▶ { croak "Not a folder: $folder"
Branch: 1 branch
BranchTrueFalseTotal
unless ref $folder and $folder->isa("Mail::Box") 0 0 0
Condition0%0/3
ref $folder and $folder->isa("Mail::Box")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $folder and $folder->isa("Mail::Box")
ref $folder $folder->isa("Mail::Box")
Truth table0%0/3
A and B
A= ref $folderB= $folder->isa("Mail::Box")
ABresult
0X0
100
111
74 unless ref $folder && $folder->isa('Mail::Box');
75  
76 0 my $name = $folder->name;
77 0 ▶ next unless exists $self->{MBTM_folders}{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"MBTM_folders"}{$name} 0 0 0
78  
79 0 delete $self->{MBTM_folders}{$name};
80  
81 $_->headIsRead && $self->outThread($_)
82 0 ▶ foreach $folder->messages;
Branch: 1 branch
BranchTrueFalseTotal
$_->headIsRead and $self->outThread($_) 0 0 0
83  
84 0 $self->{MBTM_cleanup_needed} = 1;
85 }
86  
87 0 $self;
88 }
89  
90 #-------------------------------------------
91  
92 sub thread($)
93 0 ▶{ my ($self, $message) = @_;
Subroutine thread: not called CC=6 SCAR=37.4
Pod documented
94 0 my $msgid = $message->messageId;
95 0 my $timestamp = $message->timestamp;
96  
97 0 $self->_process_delayed_nodes;
98 0 ▶ my $thread = $self->{MBTM_ids}{$msgid} || return;
Condition0%0/2
$self->{'MBTM_ids'}{$msgid} || (return)
l!l
00
MC/DC0%0/1
$self->{'MBTM_ids'}{$msgid} || (return)
$self->{'MBTM_ids'}{$msgid}
Truth table0%0/2
A
A= $self->{'MBTM_ids'}{$msgid}
Aresult
11
00
99  
100 0 my @missing;
101 $thread->recurse
102 0 ▶ ( sub { my $node = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
103 0 ▶ push @missing, $node->messageId if $node->isDummy;
Branch: 1 branch
BranchTrueFalseTotal
if $node->isDummy 0 0 0
104 0 1;
105 }
106 0 );
107  
108 0 ▶ return $thread unless @missing;
Branch: 1 branch
BranchTrueFalseTotal
unless @missing 0 0 0
109  
110 0 foreach my $folder ($self->folders)
111 {
112 # Pull-in all messages received after this-one, from any folder.
113 0 my @now_missing = $folder->scanForMessages
114 ( $msgid
115 , [ @missing ]
116 , $timestamp - 3600 # some clocks are wrong.
117 , 0
118 );
119  
120 0 ▶ if(@now_missing != @missing)
Branch: 1 branch
BranchTrueFalseTotal
if (@now_missing != @missing) 0 0 0
121 0 { $self->_process_delayed_nodes;
122 0 ▶ last unless @now_missing;
Branch: 1 branch
BranchTrueFalseTotal
unless @now_missing 0 0 0
123 0 @missing = @now_missing;
124 }
125 }
126  
127 0 $thread;
128 }
129  
130  
131 sub threadStart($)
132 0 ▶{ my ($self, $message) = @_;
Subroutine threadStart: not called CC=7 SCAR=40.3
Pod documented
133  
134 0 ▶ my $thread = $self->thread($message) || return;
Condition0%0/2
$self->thread($message) || (return)
l!l
00
MC/DC0%0/1
$self->thread($message) || (return)
$self->thread($message)
Truth table0%0/2
A
A= $self->thread($message)
Aresult
11
00
135  
136 0 ▶ while(my $parent = $thread->repliedTo)
Branch: 1 branch
BranchTrueFalseTotal
if (my $parent = $thread->repliedTo) 0 0 0
137 0 ▶ { unless($parent->isDummy)
Branch: 1 branch
BranchTrueFalseTotal
unless ($parent->isDummy) 0 0 0
138 { # Message already found, no special action to be taken.
139 0 $thread = $parent;
140 0 next;
141 }
142  
143 0 foreach ($self->folders)
144 0 { my $message = $thread->message;
145 my $timespan = $message->isDummy ? 'ALL'
146 0 ▶ : $message->timestamp - $self->{MBTM_timespan};
Branch: 1 branch
BranchTrueFalseTotal
$message->isDummy ? : 0 0 0
147  
148 last unless $_->scanForMessages
149 ( $thread->messageId, $parent->messageId
150 , $timespan, $self->{MBTM_window}
151 0 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
unless $_->scanForMessages($thread->messageId, $parent->messageId, $timespan, $self->{"MBTM_window"}) 0 0 0
152 }
153  
154 0 $self->_process_delayed_nodes;
155 0 $thread = $parent;
156 }
157  
158 0 $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 0 ▶{ my $self = shift;
Subroutine sortedAll: not called CC=2 SCAR=17.9
Pod documented
171 0 $_->find('not-existing') for $self->folders;
172 0 $self->sortedKnown(@_);
173 }
174  
175  
176 sub known()
177 0 ▶{ my $self = shift->_process_delayed_nodes->_cleanup;
Subroutine known: not called CC=1 SCAR=6.9
Pod documented
178 0 grep {!defined $_->repliedTo} values %{$self->{MBTM_ids}};
179 }
180  
181  
182 sub sortedKnown(;$$)
183 0 ▶{ my $self = shift;
Subroutine sortedKnown: not called CC=4 SCAR=30.0
Pod documented
184 0 ▶ my $prepare = shift || sub {shift->startTimeEstimate||0};
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
(shift())->startTimeEstimate or 0 0 0 0
Condition0%0/3
shift() || sub { (shift())->startTimeEstimate or 0; }
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || sub { (shift())->startTimeEstimate or 0; }
shift() sub { (shift())->startTimeEstimate or 0; }
Truth table0%0/3
A || B
A= shift()B= sub { (shift())->startTimeEstimate or 0; }
ABresult
1X1
011
000
185 0 ▶ my $compare = shift || sub {(shift) <=> (shift)};
Subroutine __ANON__: not called CC=1 SCAR=6.9
Condition0%0/3
shift() || sub { shift() <=> shift(); }
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || sub { shift() <=> shift(); }
shift() sub { shift() <=> shift(); }
Truth table0%0/3
A || B
A= shift()B= sub { shift() <=> shift(); }
ABresult
1X1
011
000
186
187 # Special care for double keys.
188 0 my %value;
189 0 push @{$value{$prepare->($_)}}, $_ for $self->known;
190 0 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 0 ▶{ my $self = shift;
Subroutine _cleanup: not called CC=4 SCAR=30.0
198 0 ▶ return $self unless $self->{MBTM_cleanup_needed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"MBTM_cleanup_needed"} 0 0 0
199  
200 0 foreach ($self->known)
201 0 { my $real = 0;
202 $_->recurse
203 0 ▶ ( sub { my $node = shift;
Subroutine __ANON__: not called CC=3 SCAR=24.8
204 0 foreach ($node->messages)
205 0 ▶ { next if $_->isDummy;
Branch: 1 branch
BranchTrueFalseTotal
if $_->isDummy 0 0 0
206 0 $real = 1;
207 0 return 0;
208 }
209 0 1;
210 }
211 0 );
212  
213 0 ▶ next if $real;
Branch: 1 branch
BranchTrueFalseTotal
if $real 0 0 0
214  
215 $_->recurse
216 0 ▶ ( sub { my $node = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
217 0 my $msgid = $node->messageId;
218 0 delete $self->{MBTM_ids}{$msgid};
219 0 1;
220 }
221 0 );
222 }
223  
224 0 delete $self->{MBTM_cleanup_needed};
225 0 $self;
226 }
227  
228 #-------------------------------------------
229  
230 sub toBeThreaded($@)
231 0 ▶{ my ($self, $folder) = (shift, shift);
Subroutine toBeThreaded: not called CC=3 SCAR=24.8
Pod documented
232 0 ▶ return $self unless exists $self->{MBTM_folders}{$folder->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"MBTM_folders"}{$folder->name} 0 0 0
233 0 $self->inThread($_) foreach @_;
234 0 $self;
235 }
236  
237  
238 sub toBeUnthreaded($@)
239 0 ▶{ my ($self, $folder) = (shift, shift);
Subroutine toBeUnthreaded: not called CC=3 SCAR=24.8
Pod documented
240 0 ▶ return $self unless exists $self->{MBTM_folders}{$folder->name};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"MBTM_folders"}{$folder->name} 0 0 0
241 0 $self->outThread($_) foreach @_;
242 0 $self;
243 }
244  
245  
246 sub inThread($)
247 0 ▶{ my ($self, $message) = @_;
Subroutine inThread: not called CC=2 SCAR=17.9
Pod documented
248 0 my $msgid = $message->messageId;
249 0 my $node = $self->{MBTM_ids}{$msgid};
250  
251 # Already known, but might reside in many folders.
252 0 ▶ if($node) { $node->addMessage($message) }
Branch: 1 branch
BranchTrueFalseTotal
if ($node) { } 0 0 0
253 else
254 { $node = Mail::Box::Thread::Node->new(message => $message
255 , msgid => $msgid, dummy_type => $self->{MBTM_dummy_type}
256 0 );
257 0 $self->{MBTM_ids}{$msgid} = $node;
258 }
259  
260 0 $self->{MBTM_delayed}{$msgid} = $node; # removes doubles.
261 }
262  
263 # The relation between nodes is delayed, to avoid that first
264 # dummy nodes have to be made, and then immediately upgrades
265 # to real nodes. So: at first we inventory what we have, and
266 # then build thread-lists.
267  
268 sub _process_delayed_nodes()
269 0 ▶{ my $self = shift;
Subroutine _process_delayed_nodes: not called CC=4 SCAR=30.0
270 0 ▶ return $self unless $self->{MBTM_delayed};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"MBTM_delayed"} 0 0 0
271  
272 0 foreach my $node (values %{$self->{MBTM_delayed}})
273 { $self->_process_delayed_message($node, $_)
274 0 foreach $node->message;
275 }
276  
277 0 delete $self->{MBTM_delayed};
278 0 $self;
279 }
280  
281 sub _process_delayed_message($$)
282 0 ▶{ my ($self, $node, $message) = @_;
Subroutine _process_delayed_message: not called CC=14 SCAR=53.5
283 0 my $msgid = $message->messageId;
284  
285 # will force parsing of head when not done yet.
286 0 ▶ my $head = $message->head or return $self;
Branch: 1 branch
BranchTrueFalseTotal
my $head = $message->head or return $self 0 0 0
287  
288 0 my $replies;
289 0 ▶ if(my $irt = $head->get('in-reply-to'))
Branch: 1 branch
BranchTrueFalseTotal
if (my $irt = $head->get("in-reply-to")) 0 0 0
290 0 { for($irt =~ m/\<(\S+\@\S+)\>/)
291 0 { my $msgid = $1;
292 0 ▶ $replies = $self->{MBTM_ids}{$msgid} || $self->createDummy($msgid);
Condition0%0/3
$self->{'MBTM_ids'}{$msgid} || $self->createDummy($msgid)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'MBTM_ids'}{$msgid} || $self->createDummy($msgid)
$self->{'MBTM_ids'}{$msgid} $self->createDummy($msgid)
Truth table0%0/3
A || B
A= $self->{'MBTM_ids'}{$msgid}B= $self->createDummy($msgid)
ABresult
1X1
011
000
293 }
294 }
295  
296 0 my @refs;
297 0 ▶ if(my $refs = $head->get('references'))
Branch: 1 branch
BranchTrueFalseTotal
if (my $refs = $head->get("references")) 0 0 0
298 0 ▶ { while($refs =~ s/\<(\S+\@\S+)\>//s)
Branch: 1 branch
BranchTrueFalseTotal
if ($refs =~ s/\<(\S+\@\S+)\>//s) 0 0 0
299 0 { my $msgid = $1;
300 0 ▶ push @refs, $self->{MBTM_ids}{$msgid} || $self->createDummy($msgid);
Condition0%0/3
$self->{'MBTM_ids'}{$msgid} || $self->createDummy($msgid)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{'MBTM_ids'}{$msgid} || $self->createDummy($msgid)
$self->{'MBTM_ids'}{$msgid} $self->createDummy($msgid)
Truth table0%0/3
A || B
A= $self->{'MBTM_ids'}{$msgid}B= $self->createDummy($msgid)
ABresult
1X1
011
000
301 }
302 }
303  
304 # Handle the `In-Reply-To' message header.
305 # This is the most secure relationship.
306  
307 0 ▶ if($replies)
Branch: 1 branch
BranchTrueFalseTotal
if ($replies) 0 0 0
308 0 ▶ { $node->follows($replies, 'REPLY')
Branch: 1 branch
BranchTrueFalseTotal
$node->follows($replies, "REPLY") and $replies->followedBy($node) 0 0 0
309 and $replies->followedBy($node);
310 }
311  
312 # Handle the `References' message header.
313 # The (ordered) list of message-IDs give an impression where this
314 # message resides in the thread. There is a little less certainty
315 # that the list is correctly ordered and correctly maintained.
316  
317 0 ▶ if(@refs)
Branch: 1 branch
BranchTrueFalseTotal
if (@refs) 0 0 0
318 0 ▶ { push @refs, $node unless $refs[-1] eq $node;
Branch: 1 branch
BranchTrueFalseTotal
unless $refs[-1] eq $node 0 0 0
319 0 my $from = shift @refs;
320  
321 0 ▶ while(my $to = shift @refs)
Branch: 1 branch
BranchTrueFalseTotal
if (my $to = shift @refs) 0 0 0
322 0 ▶ { $to->follows($from, 'REFERENCE')
Branch: 1 branch
BranchTrueFalseTotal
$to->follows($from, "REFERENCE") and $from->followedBy($to) 0 0 0
323 and $from->followedBy($to);
324 0 $from = $to;
325 }
326 }
327  
328 0 $self;
329 }
330  
331 #-------------------------------------------
332  
333  
334 sub outThread($)
335 0 ▶{ my ($self, $message) = @_;
Subroutine outThread: not called CC=2 SCAR=17.9
Pod documented
336 0 my $msgid = $message->messageId;
337 0 ▶ my $node = $self->{MBTM_ids}{$msgid} or return $message;
Branch: 1 branch
BranchTrueFalseTotal
my $node = $self->{"MBTM_ids"}{$msgid} or return $message 0 0 0
338  
339 $node->{MBTM_messages}
340 0 = [ grep {$_ ne $message} @{$node->{MBTM_messages}} ];
341  
342 0 $self;
343 }
344  
345 #-------------------------------------------
346  
347  
348 sub createDummy($)
349 0 ▶{ my ($self, $msgid) = @_;
Subroutine createDummy: not called CC=1 SCAR=6.9
Pod documented
350 $self->{MBTM_ids}{$msgid} = $self->{MBTM_thread_type}->new
351 0 (msgid => $msgid, dummy_type => $self->{MBTM_dummy_type});
352 }
353  
354 #-------------------------------------------
355  
356  
357 1 1;
« Box/MH.pm ↑ Summary Box/Net.pm »