Mail/Cap.pm

filter: Statementstmt 67.1% 96 / 143 Branchbran 54.6% 35 / 64 Conditioncond 42.8% 9 / 21 MC/DCmcdc 15.3% 2 / 13 Subroutinesub 41.6% 10 / 24 Podpod 70.0% 14 / 20 totaltotal 58.2% 166 / 285 CC 45 SCAR 26.8
CC
45
Cov
61%
dump
42.0
new
33.1
_process_file
14.3
CRAP 14.6
« Mail/Mailer.pm ↑ Summary Mail/Address.pm »
1 # Copyrights 1995-2024 by [Mark Overmeer <markov@cpan.org>].
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.03.
5 # This code is part of the bundle MailTools. Meta-POD processed with
6 # OODoc into POD and HTML manual-pages. See README.md for Copyright.
7 # Licensed under the same terms as Perl itself.
8  
9 package Mail::Cap;{
10 2 our $VERSION = '2.22';
11 }
12  
13  
14 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶sub Version { our $VERSION }
Subroutine Version: not called CC=1 SCAR=6.9
Pod undocumented
17  
18  
19 2 our $useCache = 1; # don't evaluate tests every time
20  
21 2 my @path;
22 2 ▶if($^O eq "MacOS")
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq 'MacOS') { } 0 2 2
23 0 ▶{ @path = split /\,/, $ENV{MAILCAPS} || "$ENV{HOME}mailcap";
Condition0%0/2
$ENV{'MAILCAPS'} || "$ENV{'HOME'}mailcap"
l!l
00
MC/DC0%0/1
$ENV{'MAILCAPS'} || "$ENV{'HOME'}mailcap"
$ENV{'MAILCAPS'}
Truth table0%0/2
A
A= $ENV{'MAILCAPS'}
Aresult
11
00
24 }
25 else
26 { @path = split /\:/
27 2 ▶ , ( $ENV{MAILCAPS} || (defined $ENV{HOME} ? "$ENV{HOME}/.mailcap:" : '')
Branch: 1 branch
BranchTrueFalseTotal
defined $ENV{'HOME'} ? : 2 0 2
Condition50%1/2
$ENV{'MAILCAPS'} || (defined $ENV{'HOME'} ? "$ENV{'HOME'}/.mailcap:" : '') . '/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap'
l!l
02
MC/DC0%0/1
$ENV{'MAILCAPS'} || (defined $ENV{'HOME'} ? "$ENV{'HOME'}/.mailcap:" : '') . '/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap'
$ENV{'MAILCAPS'}
Truth table50%1/2
A
A= $ENV{'MAILCAPS'}
Aresult
11
00
28 . '/etc/mailcap:/usr/etc/mailcap:/usr/local/etc/mailcap'
29 ); # this path is specified under RFC1524 appendix A
30 }
31  
32 #--------
33  
34 sub new
35 1 ▶{ my $class = shift;
Subroutine new: called CC=11 SCAR=35.0
Pod documented
36
37 1 ▶ unshift @_, 'filename' if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 0 1
38 1 my %args = @_;
39  
40 1 ▶ my $take_all = $args{take} && uc $args{take} eq 'ALL';
Condition33%1/3
$args{'take'} && uc $args{'take'} eq 'ALL'
!ll&&!rl&&r
100
MC/DC0%0/2
$args{'take'} && uc $args{'take'} eq 'ALL'
$args{'take'} uc $args{'take'} eq 'ALL'
Truth table33%1/3
A && B
A= $args{'take'}B= uc $args{'take'} eq 'ALL'
ABresult
0X0
100
111
41  
42 1 my $self = bless {_count => 0}, $class;
43  
44 $self->_process_file($args{filename})
45 1 ▶ if defined $args{filename} && -r $args{filename};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"filename"} and -r $args{"filename"} 1 0 1
Condition33%1/3
defined $args{"filename"} and -r $args{"filename"}
!ll&&!rl&&r
001
MC/DC0%0/2
defined $args{"filename"} and -r $args{"filename"}
defined $args{"filename"} -r $args{"filename"}
Truth table33%1/3
A and B
A= defined $args{"filename"}B= -r $args{"filename"}
ABresult
0X0
100
111
46  
47 1 ▶ if(!defined $args{filename} || $take_all)
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $args{"filename"} or $take_all) 0 1 1
Condition33%1/3
not defined $args{"filename"} or $take_all
l!l&&r!l&&!r
001
MC/DC0%0/2
not defined $args{"filename"} or $take_all
not defined $args{"filename"} $take_all
Truth table33%1/3
not(A) or B
A= not defined $args{"filename"}B= $take_all
ABresult
1X1
011
000
48 0 { foreach my $fname (@path)
49 0 ▶ { -r $fname or next;
Branch: 1 branch
BranchTrueFalseTotal
-r $fname or next 0 0 0
50  
51 0 $self->_process_file($fname);
52 0 ▶ last unless $take_all;
Branch: 1 branch
BranchTrueFalseTotal
unless $take_all 0 0 0
53 }
54 }
55  
56 1 ▶ unless($self->{_count})
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"_count"}) 0 1 1
57 { # Set up default mailcap
58 0 $self->{'audio/*'} = [{'view' => "showaudio %s"}];
59 0 $self->{'image/*'} = [{'view' => "xv %s"}];
60 0 $self->{'message/rfc822'} = [{'view' => "xterm -e metamail %s"}];
61 }
62  
63 1 $self;
64 }
65  
66 sub _process_file
67 1 ▶{ my $self = shift;
Subroutine _process_file: called CC=14 SCAR=26.6
68 1 ▶ my $file = shift or return;
Branch: 1 branch
BranchTrueFalseTotal
my $file = shift() or return 1 0 1
69  
70 1 local *MAILCAP;
71 1 ▶ open MAILCAP, $file
Branch: 1 branch
BranchTrueFalseTotal
open MAILCAP, $file or return 1 0 1
72 or return;
73  
74 1 $self->{_file} = $file;
75  
76 1 local $_;
77 1 ▶ while(<MAILCAP>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MAILCAP)) 13 1 14
78 13 ▶ { next if /^\s*#/; # comment
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*#/ 1 12 13
79 12 ▶ next if /^\s*$/; # blank line
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*$/ 7 5 12
80 5 ▶ $_ .= <MAILCAP> # continuation line
Branch: 1 branch
BranchTrueFalseTotal
s/(^|[^\\])((?:\\\\)*)\\\s*$/$1$2/ and do { $_ .= <MAILCAP> } 2 5 7
81 while s/(^|[^\\])((?:\\\\)*)\\\s*$/$1$2/;
82 5 chomp;
83 5 s/\0//g; # ensure no NULs in the line
84 5 s/(^|[^\\]);/$1\0/g; # make field separator NUL
85 5 my ($type, $view, @parts) = split /\s*\0\s*/;
86  
87 5 ▶ $type .= "/*" if $type !~ m[/];
Branch: 1 branch
BranchTrueFalseTotal
unless $type =~ m[/] 1 4 5
88 5 $view =~ s/\\;/;/g;
89 5 $view =~ s/\\\\/\\/g;
90 5 my %field = (view => $view);
91  
92 5 foreach (@parts)
93 6 { my($key, $val) = split /\s*\=\s*/, $_, 2;
94 6 ▶ if(defined $val)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 3 3 6
95 3 { $val =~ s/\\;/;/g;
96 3 $val =~ s/\\\\/\\/g;
97 3 $field{$key} = $val;
98 }
99 else
100 3 { $field{$key} = 1;
101 }
102 }
103  
104 5 ▶ if(my $test = $field{test})
Branch: 1 branch
BranchTrueFalseTotal
if (my $test = $field{"test"}) 1 4 5
105 1 ▶ { unless ($test =~ /\%/)
Branch: 1 branch
BranchTrueFalseTotal
unless ($test =~ /\%/) 0 1 1
106 { # No parameters in test, can perform it right away
107 0 system $test;
108 0 ▶ next if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 0 0 0
109 }
110 }
111  
112 # record this entry
113 5 ▶ unless(exists $self->{$type})
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{$type}) 4 1 5
114 4 { $self->{$type} = [];
115 4 $self->{_count}++;
116 }
117 5 push @{$self->{$type}}, \%field;
118 }
119  
120 1 close MAILCAP;
121 }
122  
123 #------------------
124  
125 0 ▶sub view { my $self = shift; $self->_run($self->viewCmd(@_)) }
Subroutine view: not called CC=1 SCAR=6.9
Pod documented
126 0 ▶sub compose { my $self = shift; $self->_run($self->composeCmd(@_)) }
Subroutine compose: not called CC=1 SCAR=6.9
Pod documented
127 0 ▶sub edit { my $self = shift; $self->_run($self->editCmd(@_)) }
Subroutine edit: not called CC=1 SCAR=6.9
Pod documented
128 0 ▶sub print { my $self = shift; $self->_run($self->printCmd(@_)) }
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
129  
130 sub _run($)
131 0 ▶{ my ($self, $cmd) = @_;
Subroutine _run: not called CC=2 SCAR=17.9
132 0 ▶ defined $cmd or return 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $cmd or return 0 0 0 0
133  
134 0 system $cmd;
135 0 1;
136 }
137  
138 #------------------
139  
140 4 ▶sub viewCmd { shift->_createCommand(view => @_) }
Subroutine viewCmd: called CC=1 SCAR=0.0
Pod documented
141 0 ▶sub composeCmd { shift->_createCommand(compose => @_) }
Subroutine composeCmd: not called CC=1 SCAR=6.9
Pod documented
142 0 ▶sub editCmd { shift->_createCommand(edit => @_) }
Subroutine editCmd: not called CC=1 SCAR=6.9
Pod documented
143 1 ▶sub printCmd { shift->_createCommand(print => @_) }
Subroutine printCmd: called CC=1 SCAR=0.0
Pod documented
144  
145 sub _createCommand($$$)
146 5 ▶{ my ($self, $method, $type, $file) = @_;
Subroutine _createCommand: called CC=3 SCAR=12.0
147 5 my $entry = $self->getEntry($type, $file);
148  
149 5 ▶ $entry && exists $entry->{$method}
Branch: 1 branch
BranchTrueFalseTotal
$entry and exists $entry->{$method} or return undef 5 0 5
Condition33%1/3
$entry and exists $entry->{$method}
!ll&&!rl&&r
005
MC/DC0%0/2
$entry and exists $entry->{$method}
$entry exists $entry->{$method}
Truth table33%1/3
A and B
A= $entryB= exists $entry->{$method}
ABresult
0X0
100
111
150 or return undef;
151  
152 5 $self->expandPercentMacros($entry->{$method}, $type, $file);
153 }
154  
155 sub makeName($$)
156 0 ▶{ my ($self, $type, $basename) = @_;
Subroutine makeName: not called CC=2 SCAR=17.9
Pod undocumented
157 0 ▶ my $template = $self->nametemplate($type)
Branch: 1 branch
BranchTrueFalseTotal
my $template = $self->nametemplate($type) or return $basename 0 0 0
158 or return $basename;
159  
160 0 $template =~ s/%s/$basename/g;
161 0 $template;
162 }
163  
164 #------------------
165  
166 sub field($$)
167 1 ▶{ my($self, $type, $field) = @_;
Subroutine field: called CC=1 SCAR=0.0
Pod documented
168 1 my $entry = $self->getEntry($type);
169 1 $entry->{$field};
170 }
171  
172  
173 1 ▶sub description { shift->field(shift, 'description'); }
Subroutine description: called CC=1 SCAR=0.0
Pod documented
174 0 ▶sub textualnewlines { shift->field(shift, 'textualnewlines'); }
Subroutine textualnewlines: not called CC=1 SCAR=6.9
Pod documented
175 0 ▶sub x11_bitmap { shift->field(shift, 'x11-bitmap'); }
Subroutine x11_bitmap: not called CC=1 SCAR=6.9
Pod documented
176 0 ▶sub nametemplate { shift->field(shift, 'nametemplate'); }
Subroutine nametemplate: not called CC=1 SCAR=6.9
Pod documented
177  
178 sub getEntry
179 6 ▶{ my($self, $origtype, $file) = @_;
Subroutine getEntry: called CC=8 SCAR=20.8
Pod undocumented
180  
181 return $self->{_cache}{$origtype}
182 6 ▶ if $useCache && exists $self->{_cache}{$origtype};
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Cap::useCache and exists $self->{"_cache"}{$origtype} 1 5 6
Condition66%2/3
$Mail::Cap::useCache and exists $self->{"_cache"}{$origtype}
!ll&&!rl&&r
051
MC/DC50%1/2
$Mail::Cap::useCache and exists $self->{"_cache"}{$origtype}
$Mail::Cap::useCache exists $self->{"_cache"}{$origtype}
Truth table66%2/3
A and B
A= $Mail::Cap::useCacheB= exists $self->{"_cache"}{$origtype}
ABresult
0X0
100
111
183  
184 5 my ($fulltype, @params) = split /\s*;\s*/, $origtype;
185 5 my ($type, $subtype) = split m[/], $fulltype, 2;
186 5 ▶ $subtype ||= '';
Condition100%2/2
$subtype ||= ""
l!l
41
MC/DC100%1/1
$subtype ||= ""
$subtype
Truth table100%2/2
A
A= $subtype
Aresult
11
00
187  
188 5 my $entry;
189 5 foreach (@{$self->{"$type/$subtype"}}, @{$self->{"$type/*"}})
190 6 ▶ { if(exists $_->{'test'})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $_->{"test"}) 2 4 6
191 { # must run test to see if it applies
192 2 my $test = $self->expandPercentMacros($_->{'test'},
193 $origtype, $file);
194 2 system $test;
195 2 ▶ next if $?;
Branch: 1 branch
BranchTrueFalseTotal
if $? 1 1 2
196 }
197 5 $entry = { %$_ }; # make copy
198 5 last;
199 }
200 5 ▶ $self->{_cache}{$origtype} = $entry if $useCache;
Branch: 1 branch
BranchTrueFalseTotal
if $Mail::Cap::useCache 5 0 5
201 5 $entry;
202 }
203  
204 sub expandPercentMacros
205 7 ▶{ my ($self, $text, $type, $file) = @_;
Subroutine expandPercentMacros: called CC=4 SCAR=13.9
Pod undocumented
206 7 ▶ defined $type or return $text;
Branch: 1 branch
BranchTrueFalseTotal
defined $type or return $text 7 0 7
207 7 ▶ defined $file or $file = "";
Branch: 1 branch
BranchTrueFalseTotal
defined $file or $file = "" 7 0 7
208  
209 7 my ($fulltype, @params) = split /\s*;\s*/, $type;
210 7 ($type, my $subtype) = split m[/], $fulltype, 2;
211  
212 7 my %params;
213 7 foreach (@params)
214 5 { my($key, $val) = split /\s*=\s*/, $_, 2;
215 5 $params{$key} = $val;
216 }
217 7 $text =~ s/\\%/\0/g; # hide all escaped %'s
218 7 $text =~ s/%t/$fulltype/g; # expand %t
219 7 $text =~ s/%s/$file/g; # expand %s
220 { # expand %{field}
221 7 local $^W = 0; # avoid warnings when expanding %params
222 7 $text =~ s/%\{\s*(.*?)\s*\}/$params{$1}/g;
223 }
224 7 $text =~ s/\0/%/g;
225 7 $text;
226 }
227  
228 # This following procedures can be useful for debugging purposes
229  
230 sub dumpEntry
231 0 ▶{ my($hash, $prefix) = @_;
Subroutine dumpEntry: not called CC=3 SCAR=24.8
Pod undocumented
232 0 ▶ defined $prefix or $prefix = "";
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix or $prefix = "" 0 0 0
233 print "$prefix$_ = $hash->{$_}\n"
234 0 for sort keys %$hash;
235 }
236  
237 sub dump
238 0 ▶{ my $self = shift;
Subroutine dump: not called CC=6 SCAR=37.4
Pod undocumented
239 0 foreach (keys %$self)
240 0 ▶ { next if /^_/;
Branch: 1 branch
BranchTrueFalseTotal
if /^_/ 0 0 0
241 0 print "$_\n";
242 0 foreach (@{$self->{$_}})
243 0 { dumpEntry($_, "\t");
244 0 print "\n";
245 }
246 }
247  
248 0 ▶ if(exists $self->{_cache})
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"_cache"}) 0 0 0
249 0 { print "Cached types\n";
250 print "\t$_\n"
251 0 for keys %{$self->{_cache}};
252 }
253 }
254  
255 2 1;
« Mail/Mailer.pm ↑ Summary Mail/Address.pm »