Box/Mbox.pm

filter: Statementstmt 79.8% 87 / 109 Branchbran 64.0% 41 / 64 Conditioncond 42.8% 21 / 49 MC/DCmcdc 18.7% 6 / 32 Subroutinesub 71.4% 10 / 14 Podpod 87.5% 7 / 8 totaltotal 62.3% 172 / 276 CC 51 SCAR 29.3
CC
51
Cov
67%
foundIn
25.7
listSubFolders
25.4
create
20.0
CRAP 18.8
« Message/Head/Subset.pm ↑ Summary Box/Net/Message.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::Mbox;
10 25 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
11 25 $VERSION = '3.009';
12  
13 25 ▶use base 'Mail::Box::File';
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 25 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 25 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 25 ▶use filetest 'access';
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 25 ▶use Mail::Box::Mbox::Message;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21  
22 25 ▶our $default_folder_dir = exists $ENV{HOME} ? $ENV{HOME} . '/Mail' : '.';
Branch: 1 branch
BranchTrueFalseTotal
exists $ENV{'HOME'} ? : 25 0 25
23 25 our $default_sub_extension = '.d';
24  
25 sub init($)
26 15 ▶{ my ($self, $args) = @_;
Subroutine init: called CC=2 SCAR=7.6
Pod undocumented
27  
28 $self->{MBM_sub_ext} # required during init
29 15 ▶ = $args->{subfolder_extension} || $default_sub_extension;
Condition33%1/3
$args->{'subfolder_extension'} || $Mail::Box::Mbox::default_sub_extension
l!l&&r!l&&!r
0150
MC/DC0%0/2
$args->{'subfolder_extension'} || $Mail::Box::Mbox::default_sub_extension
$args->{'subfolder_extension'} $Mail::Box::Mbox::default_sub_extension
Truth table33%1/3
A || B
A= $args->{'subfolder_extension'}B= $Mail::Box::Mbox::default_sub_extension
ABresult
1X1
011
000
30  
31 15 $self->SUPER::init($args);
32 }
33  
34  
35 sub create($@)
36 0 ▶{ my ($thingy, $name, %args) = @_;
Subroutine create: not called CC=4 SCAR=30.0
Pod documented
37 0 ▶ my $class = ref $thingy || $thingy;
Condition0%0/3
ref $thingy || $thingy
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $thingy || $thingy
ref $thingy $thingy
Truth table0%0/3
A || B
A= ref $thingyB= $thingy
ABresult
1X1
011
000
38 0 ▶ $args{folderdir} ||= $default_folder_dir;
Condition0%0/3
$args{"folderdir"} ||= $Mail::Box::Mbox::default_folder_dir
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"folderdir"} ||= $Mail::Box::Mbox::default_folder_dir
$args{"folderdir"} $Mail::Box::Mbox::default_folder_dir
Truth table0%0/3
A ||= B
A= $args{"folderdir"}B= $Mail::Box::Mbox::default_folder_dir
ABresult
1X1
011
000
39 0 ▶ $args{subfolder_extension} ||= $default_sub_extension;
Condition0%0/3
$args{"subfolder_extension"} ||= $Mail::Box::Mbox::default_sub_extension
l!l&&r!l&&!r
000
MC/DC0%0/2
$args{"subfolder_extension"} ||= $Mail::Box::Mbox::default_sub_extension
$args{"subfolder_extension"} $Mail::Box::Mbox::default_sub_extension
Truth table0%0/3
A ||= B
A= $args{"subfolder_extension"}B= $Mail::Box::Mbox::default_sub_extension
ABresult
1X1
011
000
40  
41 0 $class->SUPER::create($name, %args);
42 }
43  
44  
45 sub foundIn($@)
46 15 ▶{ my $class = shift;
Subroutine foundIn: called CC=17 SCAR=32.5
Pod documented
47 15 ▶ my $name = @_ % 2 ? shift : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ % 2 ? : 14 1 15
48 15 my %args = @_;
49 15 ▶ $name ||= $args{folder} or return;
Branch: 1 branch
BranchTrueFalseTotal
$name ||= $args{"folder"} or return 0 15 15
Condition66%2/3
$name ||= $args{"folder"}
l!l&&r!l&&!r
1410
MC/DC0%0/2
$name ||= $args{"folder"}
$name $args{"folder"}
Truth table66%2/3
A ||= B
A= $nameB= $args{"folder"}
ABresult
1X1
011
000
50  
51 15 ▶ my $folderdir = $args{folderdir} || $default_folder_dir;
Condition66%2/3
$args{'folderdir'} || $Mail::Box::Mbox::default_folder_dir
l!l&&r!l&&!r
1140
MC/DC0%0/2
$args{'folderdir'} || $Mail::Box::Mbox::default_folder_dir
$args{'folderdir'} $Mail::Box::Mbox::default_folder_dir
Truth table66%2/3
A || B
A= $args{'folderdir'}B= $Mail::Box::Mbox::default_folder_dir
ABresult
1X1
011
000
52 15 ▶ my $extension = $args{subfolder_extension} || $default_sub_extension;
Condition33%1/3
$args{'subfolder_extension'} || $Mail::Box::Mbox::default_sub_extension
l!l&&r!l&&!r
0150
MC/DC0%0/2
$args{'subfolder_extension'} || $Mail::Box::Mbox::default_sub_extension
$args{'subfolder_extension'} $Mail::Box::Mbox::default_sub_extension
Truth table33%1/3
A || B
A= $args{'subfolder_extension'}B= $Mail::Box::Mbox::default_sub_extension
ABresult
1X1
011
000
53 15 my $filename = $class->folderToFilename($name, $folderdir, $extension);
54  
55 15 ▶ if(-d $filename)
Branch: 1 branch
BranchTrueFalseTotal
if (-d $filename) 4 11 15
56 { # Maildir and MH Sylpheed have a 'new' sub-directory
57 4 ▶ return 0 if -d File::Spec->catdir($filename, 'new');
Branch: 1 branch
BranchTrueFalseTotal
if -d "File::Spec"->catdir($filename, "new") 1 3 4
58 3 local *DIR;
59 3 ▶ if(opendir DIR, $filename)
Branch: 1 branch
BranchTrueFalseTotal
if (opendir DIR, $filename) 3 0 3
60 3 { my @f = grep !/^\./, readdir DIR; # skip . .. and hidden
61 3 ▶ return 0 if @f && ! grep /\D/, @f; # MH
Branch: 1 branch
BranchTrueFalseTotal
if @f and not grep(/\D/, @f) 3 0 3
Condition33%1/3
@f and not grep(/\D/, @f)
!ll&&!rl&&r
003
MC/DC0%0/2
@f and not grep(/\D/, @f)
@f not grep(/\D/, @f)
Truth table33%1/3
A and not(B)
A= @fB= not grep(/\D/, @f)
ABresult
0X0
100
111
62 0 closedir DIR;
63 }
64  
65 0 ▶ return 0 # Other MH
Branch: 1 branch
BranchTrueFalseTotal
if -f "$filename/.mh_sequences" 0 0 0
66 if -f "$filename/.mh_sequences";
67  
68 0 return 1; # faked empty Mbox sub-folder (with subsub-folders?)
69 }
70  
71 11 ▶ return 0 unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 11 11
72 11 ▶ return 1 if -z $filename; # empty folder is ok
Branch: 1 branch
BranchTrueFalseTotal
if -z $filename 1 10 11
73  
74 10 ▶ open my $file, '<:raw', $filename or return 0;
Branch: 1 branch
BranchTrueFalseTotal
open my $file, "<:raw", $filename or return 0 0 10 10
75 10 local $_;
76 10 ▶ while(<$file>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $file)) 10 0 10
77 10 ▶ { next if /^\s*$/; # skip empty lines
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 0 10 10
78 10 $file->close;
79 10 return substr($_, 0, 5) eq 'From '; # found Mbox separator?
80 }
81  
82 0 return 1;
83 }
84  
85 sub delete(@)
86 0 ▶{ my $self = shift;
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
87 0 $self->SUPER::delete(@_);
88  
89 0 my $subfdir = $self->filename . $default_sub_extension;
90 0 rmdir $subfdir; # may fail, when there are still subfolders (no recurse)
91 }
92  
93 sub writeMessages($)
94 0 ▶{ my ($self, $args) = @_;
Subroutine writeMessages: not called CC=3 SCAR=24.8
Pod documented
95  
96 0 ▶ $self->SUPER::writeMessages($args) or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::writeMessages($args) or return 0 0 0
97  
98 0 ▶ if($self->{MB_remove_empty})
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"MB_remove_empty"}) 0 0 0
99 { # Can the sub-folder directory be removed? Don't mind if this
100 # doesn't work: probably no subdir or still something in it. This
101 # is a rather blunt approach...
102 0 rmdir $self->filename . $self->{MBM_sub_ext};
103 }
104  
105 0 $self;
106 }
107  
108 sub type() {'mbox'}
109  
110  
111 sub listSubFolders(@)
112 7 ▶{ my ($thingy, %args) = @_;
Subroutine listSubFolders: called CC=22 SCAR=32.4
Pod documented
113 7 ▶ my $class = ref $thingy || $thingy;
Condition33%1/3
ref $thingy || $thingy
l!l&&r!l&&!r
070
MC/DC0%0/2
ref $thingy || $thingy
ref $thingy $thingy
Truth table33%1/3
A || B
A= ref $thingyB= $thingy
ABresult
1X1
011
000
114  
115 7 ▶ my $skip_empty = $args{skip_empty} || 0;
Condition100%2/2
$args{'skip_empty'} || 0
l!l
16
MC/DC100%1/1
$args{'skip_empty'} || 0
$args{'skip_empty'}
Truth table100%2/2
A
A= $args{'skip_empty'}
Aresult
11
00
116 7 ▶ my $check = $args{check} || 0;
Condition100%2/2
$args{'check'} || 0
l!l
16
MC/DC100%1/1
$args{'check'} || 0
$args{'check'}
Truth table100%2/2
A
A= $args{'check'}
Aresult
11
00
117  
118 7 ▶ my $folder = exists $args{folder} ? $args{folder} : '=';
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'folder'} ? : 2 5 7
119 my $folderdir = exists $args{folderdir}
120 ? $args{folderdir}
121 7 ▶ : $default_folder_dir;
Branch: 1 branch
BranchTrueFalseTotal
exists $args{'folderdir'} ? : 6 1 7
122  
123 7 my $extension = $args{subfolder_extension};
124  
125 7 my $dir;
126 7 ▶ if(ref $thingy) # Mail::Box::Mbox
Branch: 1 branch
BranchTrueFalseTotal
if (ref $thingy) { } 0 7 7
127 0 ▶ { $extension ||= $thingy->{MBM_sub_ext};
Condition0%0/3
$extension ||= $thingy->{"MBM_sub_ext"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$extension ||= $thingy->{"MBM_sub_ext"}
$extension $thingy->{"MBM_sub_ext"}
Truth table0%0/3
A ||= B
A= $extensionB= $thingy->{"MBM_sub_ext"}
ABresult
1X1
011
000
128 0 $dir = $thingy->filename;
129 }
130 else
131 7 ▶ { $extension ||= $default_sub_extension;
Condition33%1/3
$extension ||= $Mail::Box::Mbox::default_sub_extension
l!l&&r!l&&!r
070
MC/DC0%0/2
$extension ||= $Mail::Box::Mbox::default_sub_extension
$extension $Mail::Box::Mbox::default_sub_extension
Truth table33%1/3
A ||= B
A= $extensionB= $Mail::Box::Mbox::default_sub_extension
ABresult
1X1
011
000
132 7 $dir = $class->folderToFilename($folder, $folderdir, $extension);
133 }
134  
135 7 ▶ my $real = -d $dir ? $dir : "$dir$extension";
Branch: 1 branch
BranchTrueFalseTotal
-d $dir ? : 6 1 7
136  
137 7 ▶ opendir DIR, $real
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $real or return () 0 7 7
138 or return ();
139  
140 # Some files have to be removed because they are created by all
141 # kinds of programs, but are no folders.
142  
143 7 my @entries = grep !m/\.lo?ck$|^\./, readdir DIR;
144 7 closedir DIR;
145  
146 # Look for files in the folderdir. They should be readable to
147 # avoid warnings for usage later. Furthermore, if we check on
148 # the size too, we avoid a syscall especially to get the size
149 # of the file by performing that check immediately.
150  
151 7 my %folders; # hash to immediately un-double names.
152  
153 7 foreach (@entries)
154 37 { my $entry = File::Spec->catfile($real, $_);
155 37 ▶ if( -f $entry )
Branch: 2 branches
BranchTrueFalseTotal
if (-f $entry) { } 25 12 37
elsif (-d _) { } 12 0 12
156 25 ▶ { next if $args{skip_empty} && ! -s _;
Branch: 1 branch
BranchTrueFalseTotal
if $args{"skip_empty"} and not -s _ 1 24 25
Condition100%3/3
$args{"skip_empty"} and not -s _
!ll&&!rl&&r
2131
MC/DC100%2/2
$args{"skip_empty"} and not -s _
$args{"skip_empty"} not -s _
Truth table100%3/3
A and not(B)
A= $args{"skip_empty"}B= not -s _
ABresult
0X0
100
111
157 24 ▶ next if $args{check} && !$class->foundIn($entry);
Branch: 1 branch
BranchTrueFalseTotal
if $args{"check"} and not $class->foundIn($entry) 1 23 24
Condition100%3/3
$args{"check"} and not $class->foundIn($entry)
!ll&&!rl&&r
2031
MC/DC100%2/2
$args{"check"} and not $class->foundIn($entry)
$args{"check"} not $class->foundIn($entry)
Truth table100%3/3
A and not(B)
A= $args{"check"}B= not $class->foundIn($entry)
ABresult
0X0
100
111
158 23 $folders{$_}++;
159 }
160 elsif( -d _ )
161 { # Directories may create fake folders.
162 12 ▶ if($args{skip_empty})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"skip_empty"}) 3 9 12
163 3 ▶ { opendir DIR, $entry or next;
Branch: 1 branch
BranchTrueFalseTotal
opendir DIR, $entry or next 0 3 3
164 3 my @sub = grep !/^\./, readdir DIR;
165 3 closedir DIR;
166 3 ▶ next unless @sub;
Branch: 1 branch
BranchTrueFalseTotal
unless @sub 1 2 3
167 }
168  
169 11 (my $folder = $_) =~ s/$extension$//;
170 11 $folders{$folder}++;
171 }
172 }
173  
174 7 ▶ map +(m/(.*)/ && $1), keys %folders; # untained names
Condition33%1/3
/(.*)/ && $1
!ll&&!rl&&r
0030
MC/DC0%0/2
/(.*)/ && $1
/(.*)/ $1
Truth table33%1/3
A && B
A= /(.*)/B= $1
ABresult
0X0
100
111
175 }
176  
177 sub openRelatedFolder(@)
178 0 ▶{ my $self = shift;
Subroutine openRelatedFolder: not called CC=1 SCAR=6.9
Pod documented
179 $self->SUPER::openRelatedFolder(subfolder_extension => $self->{MBM_sub_ext}
180 0 , @_);
181 }
182  
183 #-------------------------------------------
184  
185  
186 sub folderToFilename($$;$)
187 22 ▶{ my ($thingy, $name, $folderdir, $extension) = @_;
Subroutine folderToFilename: called CC=8 SCAR=22.6
Pod documented
188  
189 $extension ||=
190 22 ▶ ref $thingy ? $thingy->{MBM_sub_ext} : $default_sub_extension;
Branch: 1 branch
BranchTrueFalseTotal
ref $thingy ? : 0 0 0
Condition33%1/3
$extension ||= ref $thingy ? $thingy->{"MBM_sub_ext"} : $Mail::Box::Mbox::default_sub_extension
l!l&&r!l&&!r
2200
MC/DC0%0/2
$extension ||= ref $thingy ? $thingy->{"MBM_sub_ext"} : $Mail::Box::Mbox::default_sub_extension
$extension ref $thingy ? $thingy->{"MBM_sub_ext"} : $Mail::Box::Mbox::default_sub_extension
Truth table33%1/3
A ||= B
A= $extensionB= ref $thingy ? $thingy->{"MBM_sub_ext"} : $Mail::Box::Mbox::default_sub_extension
ABresult
1X1
011
000
191  
192 22 $name =~ s#^=#$folderdir/#;
193 22 my @parts = split m!/!, $name;
194  
195 22 my $real = shift @parts;
196 22 ▶ $real = '/' if $real eq '';
Branch: 1 branch
BranchTrueFalseTotal
if $real eq "" 19 3 22
197  
198 22 ▶ if(@parts)
Branch: 1 branch
BranchTrueFalseTotal
if (@parts) 22 0 22
199 22 { my $file = pop @parts;
200  
201 $real = File::Spec->catdir($real.(-d $real ? '' : $extension), $_)
202 22 ▶ foreach @parts;
Branch: 1 branch
BranchTrueFalseTotal
-d $real ? : 48 0 48
203  
204 22 ▶ $real = File::Spec->catfile($real.(-d $real ? '' : $extension), $file);
Branch: 1 branch
BranchTrueFalseTotal
-d $real ? : 22 0 22
205 }
206  
207 22 $real;
208 }
209  
210 #-------------------------------------------
211  
212  
213 25 1;
« Message/Head/Subset.pm ↑ Summary Box/Net/Message.pm »