lib/AnyEvent/MCP/Worker/IMAP.pm

filter: Statementstmt 11.4% 55 / 479 Branchbran 1.4% 6 / 408 Conditioncond 0.9% 4 / 406 MC/DCmcdc 0.0% 0 / 215 Subroutinesub 27.7% 10 / 36 Podpod 0.0% 0 / 22 totaltotal 4.7% 75 / 1566 CC 369 SCAR 75.4
CC
369
Cov
5%
run
5112.0
messages
3906.0
search_filter
1980.0
CRAP 1877.1
« bin/mcp-webbrowser ↑ Summary bin/mcp-fs »
1 package AnyEvent::MCP::Worker::IMAP;
2  
3 1 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use Mail::IMAPTalk ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use IO::Socket::SSL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use MIME::QuotedPrint ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Encode::MIME::Header ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 # Extra charsets register with Encode only when their modules are loaded.
11 # They're optional: missing modules must not prevent IMAP from starting.
12 1 eval { require Encode::HanExtra };
13 1 eval { require Encode::JIS2K };
14 1 eval { require Encode::EUCJPASCII };
15 1 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use AnyEvent::MCP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # mboxo stores raw messages without reliably escaping From_ lines.
19 # check the original bytes, not decoded MIME parts; never rewrite the message.
20 0 ▶sub mbox_from {
Subroutine mbox_from: not called CC=2 SCAR=17.9
Pod undocumented
21 0 my ($bytes) = @_;
22 0 ▶ $bytes =~ /^From[ \t]+\S+[ \t]+[^\r\n]*
Branch: 1 branch
BranchTrueFalseTotal
$bytes =~ /^From[ \t]+\S+[ \t]+[^\r\n]* (?: [0-9]{2} | \b(?i:Mon|Tue|Wed|Thu|Fri|Sat|Sun |Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b )/mux or return 0 0 0
23 (?: [0-9]{2}
24 | \b(?i:Mon|Tue|Wed|Thu|Fri|Sat|Sun
25 |Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b
26 )/mx or return;
27  
28 0 1 + ((substr $bytes, 0, $-[0]) =~ tr/\n//)
29 }
30  
31 1 my ($imap, $config, $sort_snapshot);
32  
33 2 ▶sub imap_connect {
Subroutine imap_connect: called CC=21 SCAR=48.2
Pod undocumented
34 2 my ($settings) = @_;
35  
36 2 $config = $settings;
37 2 undef $imap;
38  
39 2 ▶ $config->{tls} =~ /\A(?:connect|starttls|none)\z/ or die "Invalid IMAP TLS mode\n";
Branch: 1 branch
BranchTrueFalseTotal
$config->{"tls"} =~ /\A(?:connect|starttls|none)\z/u or die "Invalid IMAP TLS mode\n" 2 0 2
40  
41 my %tls = (
42 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER (),
43 SSL_verifycn_name => $config->{server},
44 2 SSL_verifycn_scheme => "http", # DNS names and IP addresses, not a web protocol
45 );
46  
47 2 ▶ if ($config->{tls} eq "connect") {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'tls'} eq 'connect') { } 2 0 2
48 $imap = Mail::IMAPTalk->new (
49 Server => $config->{server}, Port => $config->{port},
50 2 UseSSL => 1, UseBlocking => 1, PreserveINBOX => 1, %tls,
51 );
52 } else {
53 $imap = Mail::IMAPTalk->new (
54 0 Server => $config->{server}, Port => $config->{port}, PreserveINBOX => 1,
55 );
56  
57 0 ▶ $imap or die "IMAP connection failed: $@\n";
Branch: 1 branch
BranchTrueFalseTotal
$imap or die "IMAP connection failed: $@\n" 0 0 0
58 0 ▶ if ($config->{tls} eq "starttls") {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{"tls"} eq "starttls") 0 0 0
59 0 ▶ $imap->_imap_cmd ("starttls", 0, "")
Branch: 1 branch
BranchTrueFalseTotal
$imap->_imap_cmd("starttls", 0, "") or die 'IMAP STARTTLS rejected: ' . $imap->get_last_error . "\n" 0 0 0
60 or die "IMAP STARTTLS rejected: " . $imap->get_last_error . "\n";
61 0 ▶ IO::Socket::SSL->start_SSL ($imap->{Socket}, %tls)
Branch: 1 branch
BranchTrueFalseTotal
"IO::Socket::SSL"->start_SSL($imap->{"Socket"}, %tls) or die 'IMAP TLS handshake failed: ' . IO::Socket::SSL::errstr() . "\n" 0 0 0
62 or die "IMAP TLS handshake failed: " . IO::Socket::SSL::errstr () . "\n";
63 0 delete $imap->{Cache}{capability}; # TLS changes advertised mechanisms
64 }
65 0 $imap->{UseBlocking} = 1;
66 }
67  
68 2 ▶ $imap or die "IMAP connection failed: $@\n";
Branch: 1 branch
BranchTrueFalseTotal
$imap or die "IMAP connection failed: $@\n" 2 0 2
69 2 ▶ if (($config->{method} || "password") eq "oauth2") {
Branch: 1 branch
BranchTrueFalseTotal
if (($config->{'method'} || 'password') eq 'oauth2') { } 2 0 2
Condition50%1/2
$config->{'method'} || 'password'
l!l
20
MC/DC0%0/1
$config->{'method'} || 'password'
$config->{'method'}
Truth table50%1/2
A
A= $config->{'method'}
Aresult
11
00
70 2 require AnyEvent::MCP::IMAP::OAuth2;
71 2 require Authen::SASL;
72 2 ▶ my $mechanism = uc ($config->{oauth}{"oauth-sasl"} || "xoauth2");
Condition50%1/2
$config->{'oauth'}{'oauth-sasl'} || 'xoauth2'
l!l
20
MC/DC0%0/1
$config->{'oauth'}{'oauth-sasl'} || 'xoauth2'
$config->{'oauth'}{'oauth-sasl'}
Truth table50%1/2
A
A= $config->{'oauth'}{'oauth-sasl'}
Aresult
11
00
73 2 ▶ $imap->capability->{"auth=" . lc $mechanism}
Branch: 1 branch
BranchTrueFalseTotal
$imap->capability->{'auth=' . lc($mechanism)} or die "IMAP server does not advertise AUTH=$mechanism\n" 2 0 2
74 or die "IMAP server does not advertise AUTH=$mechanism\n";
75 my $token = $config->{access_token}
76 2 ▶ || AnyEvent::MCP::IMAP::OAuth2::refresh ($config->{account_file}, $config->{oauth});
Condition66%2/3
$config->{'access_token'} || AnyEvent::MCP::IMAP::OAuth2::refresh($config->{'account_file'}, $config->{'oauth'})
l!l&&r!l&&!r
110
MC/DC0%0/2
$config->{'access_token'} || AnyEvent::MCP::IMAP::OAuth2::refresh($config->{'account_file'}, $config->{'oauth'})
$config->{'access_token'} AnyEvent::MCP::IMAP::OAuth2::refresh($config->{'account_file'}, $config->{'oauth'})
Truth table66%2/3
A || B
A= $config->{'access_token'}B= AnyEvent::MCP::IMAP::OAuth2::refresh($config->{'account_file'}, $config->{'oauth'})
ABresult
1X1
011
000
77 my $sasl = Authen::SASL->new (
78 mechanism => $mechanism,
79 2 callback => { user => $config->{user}, pass => $token },
80 );
81 2 ▶ $imap->authenticate ($sasl)
Branch: 1 branch
BranchTrueFalseTotal
$imap->authenticate($sasl) or die 'IMAP OAuth authentication failed: ' . $imap->get_last_error . "\n" 2 0 2
82 or die "IMAP OAuth authentication failed: " . $imap->get_last_error . "\n";
83 } else {
84 0 my $caps = $imap->capability;
85 0 my $sasl = grep /^auth=(?:plain|login|digest-md5)\z/, keys %$caps;
86 0 ▶ if (!$sasl && $caps->{logindisabled}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sasl and $caps->{"logindisabled"}) 0 0 0
Condition0%0/3
not $sasl and $caps->{"logindisabled"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $sasl and $caps->{"logindisabled"}
not $sasl $caps->{"logindisabled"}
Truth table0%0/3
not(A) and B
A= not $saslB= $caps->{"logindisabled"}
ABresult
0X0
100
111
87 0 die "IMAP server advertises LOGINDISABLED and no supported AUTH mechanism. "
88 . "Use tls connect/starttls, or allow cleartext authentication on the IMAP server.\n";
89 }
90 0 ▶ if (!$sasl && $config->{password} =~ /[^\x00-\x7f]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $sasl and $config->{"password"} =~ /[^\x00-\x7f]/u) 0 0 0
Condition0%0/3
not $sasl and $config->{"password"} =~ /[^\x00-\x7f]/u
!ll&&!rl&&r
000
MC/DC0%0/2
not $sasl and $config->{"password"} =~ /[^\x00-\x7f]/u
not $sasl $config->{"password"} =~ /[^\x00-\x7f]/u
Truth table0%0/3
not(A) and B
A= not $saslB= $config->{"password"} =~ /[^\x00-\x7f]/u
ABresult
0X0
100
111
91 0 die "IMAP server offers no supported AUTH mechanism for a non-ASCII password.\n";
92 }
93 $imap->login ($config->{user}, $config->{password})
94 0 ▶ or die "IMAP authentication failed: " .
Branch: 1 branch
BranchTrueFalseTotal
$imap->login($config->{"user"}, $config->{"password"}) or die 'IMAP authentication failed: ' . ($imap->get_last_error || $@ || "server supplied no error details") . "\n" 0 0 0
Condition0%0/5
$imap->get_last_error || $@ || 'server supplied no error details'
l!l
00
$imap->get_last_error or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
$imap->get_last_error || $@ || 'server supplied no error details'
$imap->get_last_error $@
Truth table0%0/3
A or B
A= $imap->get_last_errorB= $@
ABresult
1X1
011
000
95 ($imap->get_last_error || $@ || "server supplied no error details") . "\n";
96 }
97  
98 2 $imap->uid (1);
99 2 $imap->parse_mode (DecodeUTF8 => 1);
100  
101 2 +{ connected => JSON::XS::true, capabilities => [sort keys %{$imap->capability}] }
102 }
103  
104 0 ▶sub checked ($$) {
Subroutine checked: not called CC=5 SCAR=34.0
Pod undocumented
105 0 my ($result, $what) = @_;
106  
107 0 ▶ defined $result && $result
Branch: 1 branch
BranchTrueFalseTotal
defined $result and $result or die "${what}: " . ($imap->get_last_error || $@ || "IMAP command failed") . "\n" 0 0 0
Condition0%0/8
defined $result and $result
!ll&&!rl&&r
000
$imap->get_last_error || $@ || 'IMAP command failed'
l!l
00
$imap->get_last_error or $@
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $result and $result
defined $result $result
MC/DC0%0/2
$imap->get_last_error || $@ || 'IMAP command failed'
$imap->get_last_error $@
Truth table0%0/3
A and B
A= defined $resultB= $result
ABresult
0X0
100
111
Truth table0%0/3
A or B
A= $imap->get_last_errorB= $@
ABresult
1X1
011
000
108 or die "$what: " . ($imap->get_last_error || $@ || "IMAP command failed") . "\n";
109  
110 0 $result
111 }
112  
113 0 ▶sub mailbox {
Subroutine mailbox: not called CC=2 SCAR=17.9
Pod undocumented
114 0 my ($name, $writable) = @_;
115  
116 0 checked ($imap->select ($name, ReadOnly => !$writable), "Select $name");
117 0 my $validity = $imap->get_response_code ("uidvalidity");
118 0 ▶ defined $validity or die "UIDVALIDITY not supplied for $name\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $validity or die "UIDVALIDITY not supplied for $name\n" 0 0 0
119  
120 0 $validity
121 }
122  
123 0 ▶sub same_validity {
Subroutine same_validity: not called CC=3 SCAR=24.8
Pod undocumented
124 0 my ($args, $writable) = @_;
125  
126 0 my $validity = mailbox ($args->{mailbox}, $writable);
127 0 ▶ defined $args->{uidvalidity} && "$args->{uidvalidity}" eq "$validity"
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"uidvalidity"} and qq[$args->{"uidvalidity"}] eq "$validity" or die "UIDVALIDITY mismatch (current $validity); refresh message references\n" 0 0 0
Condition0%0/3
defined $args->{"uidvalidity"} and qq[$args->{"uidvalidity"}] eq "$validity"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args->{"uidvalidity"} and qq[$args->{"uidvalidity"}] eq "$validity"
defined $args->{"uidvalidity"} qq[$args->{"uidvalidity"}] eq "$validity"
Truth table0%0/3
A and B
A= defined $args->{"uidvalidity"}B= qq[$args->{"uidvalidity"}] eq "$validity"
ABresult
0X0
100
111
128 or die "UIDVALIDITY mismatch (current $validity); refresh message references\n";
129  
130 0 $validity
131 }
132  
133 0 ▶sub uids {
Subroutine uids: not called CC=8 SCAR=42.8
Pod undocumented
134 0 my ($args) = @_;
135  
136 0 ▶ ref $args->{uids} eq "ARRAY" && @{$args->{uids}} && @{$args->{uids}} <= 1000
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{"uids"} eq "ARRAY" and @{$args->{"uids"};} and @{$args->{"uids"};} <= 1000 or die "uids must contain 1..1000 message UIDs\n" 0 0 0
Condition0%0/6
ref $args->{"uids"} eq "ARRAY" and @{$args->{"uids"};} and @{$args->{"uids"};} <= 1000
!ll&&!rl&&r
000
ref $args->{"uids"} eq "ARRAY" and @{$args->{"uids"};}
!ll&&!rl&&r
000
MC/DC0%0/3
ref $args->{"uids"} eq "ARRAY" and @{$args->{"uids"};} and @{$args->{"uids"};} <= 1000
ref $args->{"uids"} eq "ARRAY" @{$args->{"uids"};} @{$args->{"uids"};} <= 1000
Truth table0%0/4
A and B and C
A= ref $args->{"uids"} eq "ARRAY"B= @{$args->{"uids"};}C= @{$args->{"uids"};} <= 1000
ABCresult
0XX0
10X0
1100
1111
137 or die "uids must contain 1..1000 message UIDs\n";
138 0 for (@{$args->{uids}}) {
139 0 ▶ defined $_ && !ref $_ && /\A[1-9][0-9]*\z/
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and not ref $_ and /\A[1-9][0-9]*\z/u or die "Invalid UID\n" 0 0 0
Condition0%0/6
defined $_ and not ref $_ and /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
000
defined $_ and not ref $_
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_ and not ref $_ and /\A[1-9][0-9]*\z/u
defined $_ not ref $_ /\A[1-9][0-9]*\z/u
Truth table0%0/4
A and not(B) and C
A= defined $_B= not ref $_C= /\A[1-9][0-9]*\z/u
ABCresult
0XX0
10X0
1100
1111
140 or die "Invalid UID\n";
141 }
142  
143 0 join ",", @{$args->{uids}}
144 }
145  
146 0 ▶sub message {
Subroutine message: not called CC=4 SCAR=30.0
Pod undocumented
147 0 my ($args) = @_;
148  
149 0 my $validity = same_validity ($args, 0);
150 0 my $uid = $args->{uid};
151 0 ▶ my $f = checked ($imap->fetch ($uid, "(envelope flags internaldate rfc822.size bodystructure)"), "Fetch $uid")->{$uid}
Branch: 1 branch
BranchTrueFalseTotal
my $f = &checked(scalar $imap->fetch($uid, "(envelope flags internaldate rfc822.size bodystructure)"), "Fetch $uid")->{$uid} or die "Message $uid vanished\n" 0 0 0
152 or die "Message $uid vanished\n";
153 0 for my $key (qw(Subject From To Cc Bcc Reply-To Sender)) {
154 0 ▶ $f->{envelope}{$key} = mime_header ($f->{envelope}{$key}) if exists $f->{envelope}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $f->{"envelope"}{$key} 0 0 0
155 }
156 0 +{ uid => 0 + $uid, uidvalidity => $validity, %$f }
157 }
158  
159 0 ▶sub mime_header {
Subroutine mime_header: not called CC=4 SCAR=30.0
Pod undocumented
160 0 my ($value) = @_;
161 0 ▶ return $value unless defined $value && $value =~ /=\?[^?]+\?[bq]\?/i;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and $value =~ /=\?[^?]+\?[bq]\?/iu 0 0 0
Condition0%0/3
defined $value and $value =~ /=\?[^?]+\?[bq]\?/iu
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and $value =~ /=\?[^?]+\?[bq]\?/iu
defined $value $value =~ /=\?[^?]+\?[bq]\?/iu
Truth table0%0/3
A and B
A= defined $valueB= $value =~ /=\?[^?]+\?[bq]\?/iu
ABresult
0X0
100
111
162 0 ▶ (eval { Encode::decode ("MIME-Header", $value) }) // $value
Condition0%0/3
eval { do { &Encode::decode("MIME-Header", $value) } } // $value
l!l&&r!l&&!r
000
MC/DC0%0/2
eval { do { &Encode::decode("MIME-Header", $value) } } // $value
eval { do { &Encode::decode("MIME-Header", $value) } } $value
Truth table0%0/3
A // B
A= eval { do { &Encode::decode("MIME-Header", $value) } }B= $value
ABresult
1X1
011
000
163 }
164  
165 0 ▶sub search_filter {
Subroutine search_filter: not called CC=44 SCAR=75.9
Pod undocumented
166 0 my ($filter, $depth, $nodes) = @_;
167  
168 0 ▶ ref $filter eq "HASH" && (!$depth || keys %$filter)
Branch: 1 branch
BranchTrueFalseTotal
ref $filter eq "HASH" and !$depth || keys %$filter or die "Invalid SEARCH filter group\n" 0 0 0
Condition0%0/6
ref $filter eq "HASH" and !$depth || keys %$filter
!ll&&!rl&&r
000
not $depth or keys %$filter
l!l&&r!l&&!r
000
MC/DC0%0/3
ref $filter eq "HASH" and !$depth || keys %$filter
ref $filter eq "HASH" not $depth keys %$filter
Truth table0%0/4
A and not(B) or C
A= ref $filter eq "HASH"B= not $depthC= keys %$filter
ABCresult
0XX0
1000
11X1
1011
169 or die "Invalid SEARCH filter group\n";
170 0 ▶ ++$$nodes <= 100 && $depth <= 12 or die "SEARCH filter too complex\n";
Branch: 1 branch
BranchTrueFalseTotal
++$$nodes <= 100 and $depth <= 12 or die "SEARCH filter too complex\n" 0 0 0
Condition0%0/3
++$$nodes <= 100 and $depth <= 12
!ll&&!rl&&r
000
MC/DC0%0/2
++$$nodes <= 100 and $depth <= 12
++$$nodes <= 100 $depth <= 12
Truth table0%0/3
A and B
A= ++$$nodes <= 100B= $depth <= 12
ABresult
0X0
100
111
171  
172 0 my @criteria;
173 0 for my $key (sort keys %$filter) {
174 0 my $value = $filter->{$key};
175  
176 0 ▶ if ($key eq "all" || $key eq "any") {
Branch: 7 branches
BranchTrueFalseTotal
if ($key eq 'all' or $key eq 'any') { } 0 0 0
elsif ($key eq 'not') { } 0 0 0
elsif ($key eq 'header') { } 0 0 0
elsif ($key =~ /\A(?:from|to|cc|bcc|subject|body|text)\z/u) { } 0 0 0
elsif ($key =~ /\A(?:since|before|on|sent_since|sent_before|sent_on)\z/u) { } 0 0 0
elsif ($key eq 'larger' or $key eq 'smaller') { } 0 0 0
elsif ($key =~ /\A(?:seen|unseen|answered|unanswered|flagged|unflagged|deleted|undeleted|draft|undraft|recent|old|new)\z/u) { } 0 0 0
Condition0%0/3
$key eq "all" or $key eq "any"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "all" or $key eq "any"
$key eq "all" $key eq "any"
Truth table0%0/3
A or B
A= $key eq "all"B= $key eq "any"
ABresult
1X1
011
000
177 0 ▶ ref $value eq "ARRAY" && @$value && @$value <= 100
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq "ARRAY" and @$value and @$value <= 100 or die "SEARCH $key requires a nonempty array of filters\n" 0 0 0
Condition0%0/6
ref $value eq "ARRAY" and @$value and @$value <= 100
!ll&&!rl&&r
000
ref $value eq "ARRAY" and @$value
!ll&&!rl&&r
000
MC/DC0%0/3
ref $value eq "ARRAY" and @$value and @$value <= 100
ref $value eq "ARRAY" @$value @$value <= 100
Truth table0%0/4
A and B and C
A= ref $value eq "ARRAY"B= @$valueC= @$value <= 100
ABCresult
0XX0
10X0
1100
1111
178 or die "SEARCH $key requires a nonempty array of filters\n";
179 0 my @groups = map [search_filter ($_, $depth + 1, $nodes)], @$value;
180 0 ▶ if ($key eq "all") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'all') { } 0 0 0
181 0 push @criteria, @groups;
182 } else {
183 0 my $or = pop @groups;
184 0 ▶ $or = ["OR", pop @groups, $or] while @groups;
Branch: 1 branch
BranchTrueFalseTotal
@groups and do { $or = ["OR", pop @groups, $or] } 0 0 0
185 0 push @criteria, @$or;
186 }
187 } elsif ($key eq "not") {
188 0 push @criteria, "NOT", [search_filter ($value, $depth + 1, $nodes)];
189 } elsif ($key eq "header") {
190 0 ▶ ref $value eq "HASH" && keys %$value && keys %$value <= 20
Branch: 1 branch
BranchTrueFalseTotal
ref $value eq "HASH" and keys %$value and keys %$value <= 20 or die "SEARCH header requires 1..20 header names\n" 0 0 0
Condition0%0/6
ref $value eq "HASH" and keys %$value and keys %$value <= 20
!ll&&!rl&&r
000
ref $value eq "HASH" and keys %$value
!ll&&!rl&&r
000
MC/DC0%0/3
ref $value eq "HASH" and keys %$value and keys %$value <= 20
ref $value eq "HASH" keys %$value keys %$value <= 20
Truth table0%0/4
A and B and C
A= ref $value eq "HASH"B= keys %$valueC= keys %$value <= 20
ABCresult
0XX0
10X0
1100
1111
191 or die "SEARCH header requires 1..20 header names\n";
192 0 for my $name (sort keys %$value) {
193 0 ▶ $name =~ /\A[A-Za-z0-9][A-Za-z0-9-]{0,62}\z/
Branch: 1 branch
BranchTrueFalseTotal
$name =~ /\A[A-Za-z0-9][A-Za-z0-9-]{0,62}\z/u or die "Invalid SEARCH header name\n" 0 0 0
194 or die "Invalid SEARCH header name\n";
195 0 my $text = $value->{$name};
196 0 ▶ defined $text && !ref $text && length $text <= 500
Branch: 1 branch
BranchTrueFalseTotal
defined $text and not ref $text and length $text <= 500 or die "Invalid SEARCH header value\n" 0 0 0
Condition0%0/6
defined $text and not ref $text and length $text <= 500
!ll&&!rl&&r
000
defined $text and not ref $text
!ll&&!rl&&r
000
MC/DC0%0/3
defined $text and not ref $text and length $text <= 500
defined $text not ref $text length $text <= 500
Truth table0%0/4
A and not(B) and C
A= defined $textB= not ref $textC= length $text <= 500
ABCresult
0XX0
10X0
1100
1111
197 or die "Invalid SEARCH header value\n";
198 0 push @criteria, "HEADER", $name, { Quote => $text };
199 }
200 } elsif ($key =~ /\A(?:from|to|cc|bcc|subject|body|text)\z/) {
201 0 ▶ defined $value && !ref $value && length $value <= 500
Branch: 1 branch
BranchTrueFalseTotal
defined $value and not ref $value and length $value <= 500 or die "Invalid SEARCH $key value\n" 0 0 0
Condition0%0/6
defined $value and not ref $value and length $value <= 500
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/3
defined $value and not ref $value and length $value <= 500
defined $value not ref $value length $value <= 500
Truth table0%0/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length $value <= 500
ABCresult
0XX0
10X0
1100
1111
202 or die "Invalid SEARCH $key value\n";
203 0 push @criteria, uc $key, { Quote => $value };
204 } elsif ($key =~ /\A(?:since|before|on|sent_since|sent_before|sent_on)\z/) {
205 0 ▶ defined $value && !ref $value && $value =~ /\A[0-9]{1,2}-[A-Za-z]{3}-[0-9]{4}\z/
Branch: 1 branch
BranchTrueFalseTotal
defined $value and not ref $value and $value =~ /\A[0-9]{1,2}-[A-Za-z]{3}-[0-9]{4}\z/u or die "Invalid SEARCH $key date (D-Mon-YYYY)\n" 0 0 0
Condition0%0/6
defined $value and not ref $value and $value =~ /\A[0-9]{1,2}-[A-Za-z]{3}-[0-9]{4}\z/u
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/3
defined $value and not ref $value and $value =~ /\A[0-9]{1,2}-[A-Za-z]{3}-[0-9]{4}\z/u
defined $value not ref $value $value =~ /\A[0-9]{1,2}-[A-Za-z]{3}-[0-9]{4}\z/u
Truth table0%0/4
A and not(B) and C
A= defined $valueB= not ref $valueC= $value =~ /\A[0-9]{1,2}-[A-Za-z]{3}-[0-9]{4}\z/u
ABCresult
0XX0
10X0
1100
1111
206 or die "Invalid SEARCH $key date (D-Mon-YYYY)\n";
207 0 (my $command = uc $key) =~ tr/_//d;
208 0 push @criteria, $command, $value;
209 ▶ } elsif ($key eq "larger" || $key eq "smaller") {
Condition0%0/3
$key eq "larger" or $key eq "smaller"
l!l&&r!l&&!r
000
MC/DC0%0/2
$key eq "larger" or $key eq "smaller"
$key eq "larger" $key eq "smaller"
Truth table0%0/3
A or B
A= $key eq "larger"B= $key eq "smaller"
ABresult
1X1
011
000
210 0 ▶ defined $value && !ref $value && $value =~ /\A[0-9]{1,10}\z/ && $value <= 4294967295
Branch: 1 branch
BranchTrueFalseTotal
defined $value and not ref $value and $value =~ /\A[0-9]{1,10}\z/u and $value <= 4294967295 or die "Invalid SEARCH $key size\n" 0 0 0
Condition0%0/9
defined $value and not ref $value and $value =~ /\A[0-9]{1,10}\z/u and $value <= 4294967295
!ll&&!rl&&r
000
defined $value and not ref $value and $value =~ /\A[0-9]{1,10}\z/u
!ll&&!rl&&r
000
defined $value and not ref $value
!ll&&!rl&&r
000
MC/DC0%0/4
defined $value and not ref $value and $value =~ /\A[0-9]{1,10}\z/u and $value <= 4294967295
defined $value not ref $value $value =~ /\A[0-9]{1,10}\z/u $value <= 4294967295
Truth table0%0/5
A and not(B) and C and D
A= defined $valueB= not ref $valueC= $value =~ /\A[0-9]{1,10}\z/uD= $value <= 4294967295
ABCDresult
0XXX0
10XX0
110X0
11100
11111
211 or die "Invalid SEARCH $key size\n";
212 0 push @criteria, uc $key, $value;
213 } elsif ($key =~ /\A(?:seen|unseen|answered|unanswered|flagged|unflagged|deleted|undeleted|draft|undraft|recent|old|new)\z/) {
214 0 ▶ defined $value && "$value" =~ /\A[01]\z/
Branch: 1 branch
BranchTrueFalseTotal
defined $value and "$value" =~ /\A[01]\z/u or die "Invalid SEARCH $key boolean\n" 0 0 0
Condition0%0/3
defined $value and "$value" =~ /\A[01]\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $value and "$value" =~ /\A[01]\z/u
defined $value "$value" =~ /\A[01]\z/u
Truth table0%0/3
A and B
A= defined $valueB= "$value" =~ /\A[01]\z/u
ABresult
0X0
100
111
215 or die "Invalid SEARCH $key boolean\n";
216 0 ▶ push @criteria, uc $key if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 0 0 0
217 } else {
218 0 die "Unknown SEARCH filter: $key\n";
219 }
220 }
221  
222 0 ▶ @criteria or !$depth or die "Empty SEARCH filter group\n";
Branch: 1 branch
BranchTrueFalseTotal
@criteria or not $depth or die "Empty SEARCH filter group\n" 0 0 0
Condition0%0/3
@criteria or not $depth
l!l&&r!l&&!r
000
MC/DC0%0/2
@criteria or not $depth
@criteria not $depth
Truth table0%0/3
A or not(B)
A= @criteriaB= not $depth
ABresult
1X1
011
000
223 @criteria
224 0 }
225  
226 0 ▶sub messages {
Subroutine messages: not called CC=62 SCAR=82.7
Pod undocumented
227 0 my ($args) = @_;
228  
229 0 my $validity = mailbox ($args->{mailbox}, 0);
230 0 my $explicit = exists $args->{uids};
231 0 ▶ if ($explicit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($explicit) { } 0 0 0
232 0 ▶ defined $args->{uidvalidity} && "$args->{uidvalidity}" eq "$validity"
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"uidvalidity"} and qq[$args->{"uidvalidity"}] eq "$validity" or die "UIDVALIDITY mismatch (current $validity); refresh message references\n" 0 0 0
Condition0%0/3
defined $args->{"uidvalidity"} and qq[$args->{"uidvalidity"}] eq "$validity"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args->{"uidvalidity"} and qq[$args->{"uidvalidity"}] eq "$validity"
defined $args->{"uidvalidity"} qq[$args->{"uidvalidity"}] eq "$validity"
Truth table0%0/3
A and B
A= defined $args->{"uidvalidity"}B= qq[$args->{"uidvalidity"}] eq "$validity"
ABresult
0X0
100
111
233 or die "UIDVALIDITY mismatch (current $validity); refresh message references\n";
234 !exists $args->{cursor} && !exists $args->{filter} && !exists $args->{limit}
235 0 ▶ or die "Explicit UIDs cannot be combined with cursor, filter or limit\n";
Branch: 1 branch
BranchTrueFalseTotal
exists $args->{"cursor"} || exists $args->{"filter"} || exists $args->{"limit"} and die "Explicit UIDs cannot be combined with cursor, filter or limit\n" 0 0 0
Condition0%0/6
exists $args->{"cursor"} or exists $args->{"filter"} or exists $args->{"limit"}
l!l&&r!l&&!r
000
exists $args->{"cursor"} or exists $args->{"filter"}
l!l&&r!l&&!r
000
MC/DC0%0/3
exists $args->{"cursor"} or exists $args->{"filter"} or exists $args->{"limit"}
exists $args->{"cursor"} exists $args->{"filter"} exists $args->{"limit"}
Truth table0%0/4
A or B or C
A= exists $args->{"cursor"}B= exists $args->{"filter"}C= exists $args->{"limit"}
ABCresult
1XX1
01X1
0011
0000
236 0 uids ($args);
237 } else {
238 0 my $cursor = $args->{cursor};
239 0 ▶ if (defined $cursor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cursor) 0 0 0
240 ref $cursor eq "HASH" && defined $cursor->{uidvalidity}
241 && "$cursor->{uidvalidity}" eq "$validity"
242 0 ▶ && defined $cursor->{before_uid} && $cursor->{before_uid} =~ /\A[1-9][0-9]*\z/
Branch: 1 branch
BranchTrueFalseTotal
ref $cursor eq "HASH" and defined $cursor->{"uidvalidity"} and qq[$cursor->{"uidvalidity"}] eq "$validity" and defined $cursor->{"before_uid"} and $cursor->{"before_uid"} =~ /\A[1-9][0-9]*\z/u or die "Stale or invalid UID cursor\n" 0 0 0
Condition0%0/12
ref $cursor eq "HASH" and defined $cursor->{"uidvalidity"} and qq[$cursor->{"uidvalidity"}] eq "$validity" and defined $cursor->{"before_uid"} and $cursor->{"before_uid"} =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
000
ref $cursor eq "HASH" and defined $cursor->{"uidvalidity"} and qq[$cursor->{"uidvalidity"}] eq "$validity" and defined $cursor->{"before_uid"}
!ll&&!rl&&r
000
ref $cursor eq "HASH" and defined $cursor->{"uidvalidity"} and qq[$cursor->{"uidvalidity"}] eq "$validity"
!ll&&!rl&&r
000
ref $cursor eq "HASH" and defined $cursor->{"uidvalidity"}
!ll&&!rl&&r
000
MC/DC0%0/5
ref $cursor eq "HASH" and defined $cursor->{"uidvalidity"} and qq[$cursor->{"uidvalidity"}] eq "$validity" and defined $cursor->{"before_uid"} and $cursor->{"before_uid"} =~ /\A[1-9][0-9]*\z/u
ref $cursor eq "HASH" defined $cursor->{"uidvalidity"} qq[$cursor->{"uidvalidity"}] eq "$validity" defined $cursor->{"before_uid"} $cursor->{"before_uid"} =~ /\A[1-9][0-9]*\z/u
Truth table0%0/6
A and B and C and D and E
A= ref $cursor eq "HASH"B= defined $cursor->{"uidvalidity"}C= qq[$cursor->{"uidvalidity"}] eq "$validity"D= defined $cursor->{"before_uid"}E= $cursor->{"before_uid"} =~ /\A[1-9][0-9]*\z/u
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
243 or die "Stale or invalid UID cursor\n";
244 }
245 }
246  
247 0 my $nodes = 0;
248 0 ▶ my @criteria = $explicit ? () : search_filter ($args->{filter} // {}, 0, \$nodes);
Branch: 1 branch
BranchTrueFalseTotal
$explicit ? : 0 0 0
Condition0%0/2
$args->{"filter"} // {}
l!l
00
MC/DC0%0/1
$args->{"filter"} // {}
$args->{"filter"}
Truth table0%0/2
A
A= $args->{"filter"}
Aresult
11
00
249  
250 0 ▶ my @fields = @{$args->{fields} || [qw(subject from date flags size)]};
Condition0%0/2
$args->{'fields'} || ['subject', 'from', 'date', 'flags', 'size']
l!l
00
MC/DC0%0/1
$args->{'fields'} || ['subject', 'from', 'date', 'flags', 'size']
$args->{'fields'}
Truth table0%0/2
A
A= $args->{'fields'}
Aresult
11
00
251 0 my %valid = map +($_ => 1), qw(uid from to subject date cc reply-to message-id flags size internaldate);
252 0 ▶ $valid{$_} or die "Invalid summary field: $_\n" for @fields;
Branch: 1 branch
BranchTrueFalseTotal
$valid{$_} or die "Invalid summary field: $_\n" 0 0 0
253 0 ▶ my @headers = @{$args->{headers} || []};
Condition0%0/2
$args->{'headers'} || []
l!l
00
MC/DC0%0/1
$args->{'headers'} || []
$args->{'headers'}
Truth table0%0/2
A
A= $args->{'headers'}
Aresult
11
00
254 0 ▶ @headers <= 20 or die "Too many headers\n";
Branch: 1 branch
BranchTrueFalseTotal
@headers <= 20 or die "Too many headers\n" 0 0 0
255 0 for (@headers) {
256 0 ▶ /\A[A-Za-z0-9][A-Za-z0-9-]{0,62}\z/ or die "Invalid header name: $_\n";
Branch: 1 branch
BranchTrueFalseTotal
/\A[A-Za-z0-9][A-Za-z0-9-]{0,62}\z/u or die "Invalid header name: $_\n" 0 0 0
257 }
258  
259 # search in descending UID windows: ordinary UID SEARCH can otherwise return
260 # millions of matches even when only one summary page was requested.
261 # Mail::IMAPTalk caches SELECT, so UIDNEXT from that SELECT becomes stale.
262 0 my @found;
263 0 my ($more, $incomplete, $hi);
264 0 ▶ if ($explicit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($explicit) { } 0 0 0
265 0 @found = @{$args->{uids}};
266 } else {
267 0 ▶ my $limit = $args->{limit} // 100;
Condition0%0/2
$args->{"limit"} // 100
l!l
00
MC/DC0%0/1
$args->{"limit"} // 100
$args->{"limit"}
Truth table0%0/2
A
A= $args->{"limit"}
Aresult
11
00
268 0 ▶ $limit =~ /\A[1-9][0-9]*\z/ && $limit <= 1000 or die "limit must be 1..1000\n";
Branch: 1 branch
BranchTrueFalseTotal
$limit =~ /\A[1-9][0-9]*\z/u and $limit <= 1000 or die "limit must be 1..1000\n" 0 0 0
Condition0%0/3
$limit =~ /\A[1-9][0-9]*\z/u and $limit <= 1000
!ll&&!rl&&r
000
MC/DC0%0/2
$limit =~ /\A[1-9][0-9]*\z/u and $limit <= 1000
$limit =~ /\A[1-9][0-9]*\z/u $limit <= 1000
Truth table0%0/3
A and B
A= $limit =~ /\A[1-9][0-9]*\z/uB= $limit <= 1000
ABresult
0X0
100
111
269 0 my $status = checked ($imap->status ($args->{mailbox}, "(UIDNEXT UIDVALIDITY)"), "STATUS");
270 0 ▶ "$status->{uidvalidity}" eq "$validity"
Branch: 1 branch
BranchTrueFalseTotal
qq[$status->{"uidvalidity"}] eq "$validity" or die "Mailbox UIDVALIDITY changed; restart mcp-imap\n" 0 0 0
271 or die "Mailbox UIDVALIDITY changed; restart mcp-imap\n";
272 0 my $uidnext = $status->{uidnext};
273 0 ▶ defined $uidnext && $uidnext =~ /\A[1-9][0-9]*\z/
Branch: 1 branch
BranchTrueFalseTotal
defined $uidnext and $uidnext =~ /\A[1-9][0-9]*\z/u or die "Server did not provide UIDNEXT\n" 0 0 0
Condition0%0/3
defined $uidnext and $uidnext =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $uidnext and $uidnext =~ /\A[1-9][0-9]*\z/u
defined $uidnext $uidnext =~ /\A[1-9][0-9]*\z/u
Truth table0%0/3
A and B
A= defined $uidnextB= $uidnext =~ /\A[1-9][0-9]*\z/u
ABresult
0X0
100
111
274 or die "Server did not provide UIDNEXT\n";
275 0 ▶ $hi = $args->{cursor} ? $args->{cursor}{before_uid} - 1 : $uidnext - 1;
Branch: 1 branch
BranchTrueFalseTotal
$args->{'cursor'} ? : 0 0 0
276 0 my $scanned = 0;
277 0 ▶ while ($hi > 0 && $scanned < 20000 && @found <= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hi > 0 and $scanned < 20000 and @found <= $limit) 0 0 0
Condition0%0/6
$hi > 0 and $scanned < 20000 and @found <= $limit
!ll&&!rl&&r
000
$hi > 0 and $scanned < 20000
!ll&&!rl&&r
000
MC/DC0%0/3
$hi > 0 and $scanned < 20000 and @found <= $limit
$hi > 0 $scanned < 20000 @found <= $limit
Truth table0%0/4
A and B and C
A= $hi > 0B= $scanned < 20000C= @found <= $limit
ABCresult
0XX0
10X0
1100
1111
278 0 ▶ my $lo = $hi > 999 ? $hi - 999 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$hi > 999 ? : 0 0 0
279 0 my $matches = checked ($imap->search ("$lo:$hi", @criteria), "UID SEARCH");
280 0 push @found, sort { $b <=> $a } @$matches;
281 0 $scanned += $hi - $lo + 1;
282 0 $hi = $lo - 1;
283 }
284 0 $more = @found > $limit;
285 0 ▶ splice @found, $limit if $more;
Branch: 1 branch
BranchTrueFalseTotal
if $more 0 0 0
286 0 ▶ $incomplete = $hi > 0 && !$more;
Condition0%0/3
$hi > 0 && !$more
!ll&&!rl&&r
000
MC/DC0%0/2
$hi > 0 && !$more
$hi > 0 !$more
Truth table0%0/3
A && not(B)
A= $hi > 0B= !$more
ABresult
0X0
100
111
287 }
288  
289 0 my %selected = map +($_ => 1), @fields;
290 0 my @items = ("uid");
291 0 ▶ push @items, "envelope" if grep $selected{$_}, qw(from to subject date cc reply-to message-id);
Branch: 1 branch
BranchTrueFalseTotal
if grep $selected{$_}, ("from", "to", "subject", "date", "cc", "reply-to", "message-id") 0 0 0
292 0 ▶ push @items, "flags" if $selected{flags};
Branch: 1 branch
BranchTrueFalseTotal
if $selected{"flags"} 0 0 0
293 0 ▶ push @items, "rfc822.size" if $selected{size};
Branch: 1 branch
BranchTrueFalseTotal
if $selected{"size"} 0 0 0
294 0 ▶ push @items, "internaldate" if $selected{internaldate};
Branch: 1 branch
BranchTrueFalseTotal
if $selected{"internaldate"} 0 0 0
295 0 ▶ if (@headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@headers) 0 0 0
296 0 push @items, "body.peek[header.fields (" . (join " ", @headers) . ")]";
297 }
298  
299 0 ▶ my $data = @found
Branch: 1 branch
BranchTrueFalseTotal
@found ? : 0 0 0
300 ? checked ($imap->fetch (\@found, "(" . join (" ", @items) . ")"), "Fetch summaries")
301 : {};
302 0 my %env = (
303 (map +($_ => ucfirst $_), qw(from to subject date cc)),
304 "reply-to" => "Reply-To",
305 "message-id" => "Message-ID",
306 );
307 0 my @messages;
308 0 for my $uid (@found) {
309 0 ▶ my $item = $data->{$uid} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $item = $data->{$uid} or next 0 0 0
310 0 my %row = (uid => 0 + $uid);
311 0 for my $field (@fields) {
312 0 ▶ next if $field eq "uid";
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "uid" 0 0 0
313 0 my $key = $env{$field};
314 $row{$field} = $key ? $item->{envelope}{$key}
315 0 ▶ : $field eq "size" ? $item->{"rfc822.size"} : $item->{$field};
Branch: 2 branches
BranchTrueFalseTotal
$key ? : 0 0 0
$field eq 'size' ? : 0 0 0
316 0 ▶ $row{$field} = mime_header ($row{$field}) if $field =~ /\A(?:subject|from|to|cc|reply-to)\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $field =~ /\A(?:subject|from|to|cc|reply-to)\z/u 0 0 0
317 }
318 0 ▶ if (@headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@headers) 0 0 0
319 0 ▶ $row{headers} = $item->{headers} || {};
Condition0%0/2
$item->{'headers'} || {}
l!l
00
MC/DC0%0/1
$item->{'headers'} || {}
$item->{'headers'}
Truth table0%0/2
A
A= $item->{'headers'}
Aresult
11
00
320 0 ▶ length (JSON::XS->new->utf8->encode ($row{headers})) <= 65536
Branch: 1 branch
BranchTrueFalseTotal
length "JSON::XS"->new->utf8->encode($row{"headers"}) <= 65536 or die "Selected headers exceed 64 KiB\n" 0 0 0
321 or die "Selected headers exceed 64 KiB\n";
322 }
323 0 push @messages, \%row;
324 }
325  
326 0 ▶ my $before = $explicit ? undef : $incomplete ? $hi + 1 : @found ? $found[-1] : undef;
Branch: 3 branches
BranchTrueFalseTotal
$explicit ? : 0 0 0
$incomplete ? : 0 0 0
@found ? : 0 0 0
327  
328 +{
329 uidvalidity => $validity,
330 messages => \@messages,
331 more => $more || $incomplete ? JSON::XS::true : JSON::XS::false,
332 incomplete => $incomplete ? JSON::XS::true : JSON::XS::false,
333 0 ▶ ($explicit ? (missing_uids => [grep !$data->{$_}, @found]) : ()),
Branch: 4 branches
BranchTrueFalseTotal
$more || $incomplete ? : 0 0 0
$incomplete ? : 0 0 0
$explicit ? : 0 0 0
($more || $incomplete) && $before ? : 0 0 0
Condition0%0/9
$more or $incomplete
l!l&&r!l&&!r
000
$more || $incomplete and $before
!ll&&!rl&&r
000
$more or $incomplete
l!l&&r!l&&!r
000
MC/DC0%0/3
$more || $incomplete and $before
$more $incomplete $before
Truth table0%0/5
A or B and C
A= $moreB= $incompleteC= $before
ABCresult
00X0
1X00
0100
1X11
0111
334 next_cursor => ($more || $incomplete) && $before
335 ? { uidvalidity => $validity, before_uid => $before } : undef,
336 }
337 }
338  
339 0 ▶sub threads {
Subroutine threads: not called CC=12 SCAR=50.5
Pod undocumented
340 0 my ($args) = @_;
341 0 my $validity = mailbox ($args->{mailbox}, 0);
342 0 ▶ if (defined $args->{uidvalidity}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args->{"uidvalidity"}) 0 0 0
343 0 ▶ "$args->{uidvalidity}" eq "$validity"
Branch: 1 branch
BranchTrueFalseTotal
qq[$args->{"uidvalidity"}] eq "$validity" or die "UIDVALIDITY mismatch (current $validity); refresh message references\n" 0 0 0
344 or die "UIDVALIDITY mismatch (current $validity); refresh message references\n";
345 }
346 0 ▶ $imap->capability->{"thread=orderedsubject"}
Branch: 1 branch
BranchTrueFalseTotal
$imap->capability->{"thread=orderedsubject"} or die "IMAP server does not advertise THREAD=ORDEREDSUBJECT\n" 0 0 0
347 or die "IMAP server does not advertise THREAD=ORDEREDSUBJECT\n";
348  
349 0 my $nodes = 0;
350 0 ▶ my @criteria = search_filter ($args->{filter} // {}, 0, \$nodes);
Condition0%0/2
$args->{"filter"} // {}
l!l
00
MC/DC0%0/1
$args->{"filter"} // {}
$args->{"filter"}
Truth table0%0/2
A
A= $args->{"filter"}
Aresult
11
00
351 0 my $range = $args->{uid_range};
352 0 ▶ if (defined $range) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $range) 0 0 0
353 0 ▶ !ref $range && $range =~ /\A([1-9][0-9]{0,9}):([1-9][0-9]{0,9})\z/
Branch: 1 branch
BranchTrueFalseTotal
not ref $range and $range =~ /\A([1-9][0-9]{0,9}):([1-9][0-9]{0,9})\z/u or die "uid_range must be START:END (positive UIDs)\n" 0 0 0
Condition0%0/3
not ref $range and $range =~ /\A([1-9][0-9]{0,9}):([1-9][0-9]{0,9})\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
not ref $range and $range =~ /\A([1-9][0-9]{0,9}):([1-9][0-9]{0,9})\z/u
not ref $range $range =~ /\A([1-9][0-9]{0,9}):([1-9][0-9]{0,9})\z/u
Truth table0%0/3
not(A) and B
A= not ref $rangeB= $range =~ /\A([1-9][0-9]{0,9}):([1-9][0-9]{0,9})\z/u
ABresult
0X0
100
111
354 or die "uid_range must be START:END (positive UIDs)\n";
355 0 my ($lo, $hi) = ($1, $2);
356 0 ▶ $lo <= $hi && $hi <= 4294967295
Branch: 1 branch
BranchTrueFalseTotal
$lo <= $hi and $hi <= 4294967295 or die "uid_range must be ascending with 32-bit UIDs\n" 0 0 0
Condition0%0/3
$lo <= $hi and $hi <= 4294967295
!ll&&!rl&&r
000
MC/DC0%0/2
$lo <= $hi and $hi <= 4294967295
$lo <= $hi $hi <= 4294967295
Truth table0%0/3
A and B
A= $lo <= $hiB= $hi <= 4294967295
ABresult
0X0
100
111
357 or die "uid_range must be ascending with 32-bit UIDs\n";
358 0 unshift @criteria, "UID", $range;
359 }
360 0 ▶ @criteria = ("ALL") unless @criteria;
Branch: 1 branch
BranchTrueFalseTotal
unless @criteria 0 0 0
361  
362 0 my $threads = checked ($imap->thread ("ORDEREDSUBJECT", "UTF-8", @criteria), "UID THREAD");
363 +{
364 0 ▶ uidvalidity => $validity,
Branch: 1 branch
BranchTrueFalseTotal
defined $range ? : 0 0 0
365 (defined $range ? (uid_range => $range) : ()),
366 threads => $threads,
367 }
368 }
369  
370 0 ▶sub sort_page {
Subroutine sort_page: not called CC=19 SCAR=59.4
Pod undocumented
371 0 my ($args, $validity) = @_;
372 0 ▶ my $snap = $sort_snapshot
Branch: 1 branch
BranchTrueFalseTotal
my $snap = $sort_snapshot or die "No SORT snapshot (worker restarted or snapshot discarded); create a new one\n" 0 0 0
373 or die "No SORT snapshot (worker restarted or snapshot discarded); create a new one\n";
374 defined $args->{snapshot_id} && $args->{snapshot_id} eq $snap->{id}
375 && $args->{mailbox} eq $snap->{mailbox}
376 0 ▶ or die "Unknown or stale SORT snapshot ID\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"snapshot_id"} and $args->{"snapshot_id"} eq $snap->{"id"} and $args->{"mailbox"} eq $snap->{"mailbox"} or die "Unknown or stale SORT snapshot ID\n" 0 0 0
Condition0%0/6
defined $args->{"snapshot_id"} and $args->{"snapshot_id"} eq $snap->{"id"} and $args->{"mailbox"} eq $snap->{"mailbox"}
!ll&&!rl&&r
000
defined $args->{"snapshot_id"} and $args->{"snapshot_id"} eq $snap->{"id"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $args->{"snapshot_id"} and $args->{"snapshot_id"} eq $snap->{"id"} and $args->{"mailbox"} eq $snap->{"mailbox"}
defined $args->{"snapshot_id"} $args->{"snapshot_id"} eq $snap->{"id"} $args->{"mailbox"} eq $snap->{"mailbox"}
Truth table0%0/4
A and B and C
A= defined $args->{"snapshot_id"}B= $args->{"snapshot_id"} eq $snap->{"id"}C= $args->{"mailbox"} eq $snap->{"mailbox"}
ABCresult
0XX0
10X0
1100
1111
377  
378 0 ▶ $validity = mailbox ($args->{mailbox}, 0) unless defined $validity;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $validity 0 0 0
379 0 ▶ "$validity" eq "$snap->{uidvalidity}"
Branch: 1 branch
BranchTrueFalseTotal
"$validity" eq qq[$snap->{"uidvalidity"}] or die "SORT snapshot UIDVALIDITY changed; discard and recreate it\n" 0 0 0
380 or die "SORT snapshot UIDVALIDITY changed; discard and recreate it\n";
381  
382 0 ▶ my $offset = $args->{offset} // 0;
Condition0%0/2
$args->{"offset"} // 0
l!l
00
MC/DC0%0/1
$args->{"offset"} // 0
$args->{"offset"}
Truth table0%0/2
A
A= $args->{"offset"}
Aresult
11
00
383 0 ▶ defined $offset && !ref $offset && $offset =~ /\A(?:0|[1-9][0-9]*)\z/
Branch: 1 branch
BranchTrueFalseTotal
defined $offset and not ref $offset and $offset =~ /\A(?:0|[1-9][0-9]*)\z/u or die "SORT offset must be nonnegative\n" 0 0 0
Condition0%0/6
defined $offset and not ref $offset and $offset =~ /\A(?:0|[1-9][0-9]*)\z/u
!ll&&!rl&&r
000
defined $offset and not ref $offset
!ll&&!rl&&r
000
MC/DC0%0/3
defined $offset and not ref $offset and $offset =~ /\A(?:0|[1-9][0-9]*)\z/u
defined $offset not ref $offset $offset =~ /\A(?:0|[1-9][0-9]*)\z/u
Truth table0%0/4
A and not(B) and C
A= defined $offsetB= not ref $offsetC= $offset =~ /\A(?:0|[1-9][0-9]*)\z/u
ABCresult
0XX0
10X0
1100
1111
384 or die "SORT offset must be nonnegative\n";
385 0 ▶ my $limit = $args->{limit} // 100;
Condition0%0/2
$args->{"limit"} // 100
l!l
00
MC/DC0%0/1
$args->{"limit"} // 100
$args->{"limit"}
Truth table0%0/2
A
A= $args->{"limit"}
Aresult
11
00
386 0 ▶ defined $limit && !ref $limit && $limit =~ /\A[1-9][0-9]*\z/ && $limit <= 1000
Branch: 1 branch
BranchTrueFalseTotal
defined $limit and not ref $limit and $limit =~ /\A[1-9][0-9]*\z/u and $limit <= 1000 or die "SORT limit must be 1..1000\n" 0 0 0
Condition0%0/9
defined $limit and not ref $limit and $limit =~ /\A[1-9][0-9]*\z/u and $limit <= 1000
!ll&&!rl&&r
000
defined $limit and not ref $limit and $limit =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
000
defined $limit and not ref $limit
!ll&&!rl&&r
000
MC/DC0%0/4
defined $limit and not ref $limit and $limit =~ /\A[1-9][0-9]*\z/u and $limit <= 1000
defined $limit not ref $limit $limit =~ /\A[1-9][0-9]*\z/u $limit <= 1000
Truth table0%0/5
A and not(B) and C and D
A= defined $limitB= not ref $limitC= $limit =~ /\A[1-9][0-9]*\z/uD= $limit <= 1000
ABCDresult
0XXX0
10XX0
110X0
11100
11111
387 or die "SORT limit must be 1..1000\n";
388  
389 0 my $count = @{$snap->{uids}};
390 0 ▶ $offset <= $count or die "SORT offset beyond snapshot\n";
Branch: 1 branch
BranchTrueFalseTotal
$offset <= $count or die "SORT offset beyond snapshot\n" 0 0 0
391 0 ▶ my $end = $offset + $limit < $count ? $offset + $limit : $count;
Branch: 1 branch
BranchTrueFalseTotal
$offset + $limit < $count ? : 0 0 0
392 +{
393 snapshot_id => $snap->{id},
394 uidvalidity => $validity,
395 count => $count,
396 offset => 0 + $offset,
397 0 ▶ uids => [@{$snap->{uids}}[$offset .. $end - 1]],
Branch: 1 branch
BranchTrueFalseTotal
$end < $count ? : 0 0 0
398 next_offset => $end < $count ? $end : undef,
399 }
400 }
401  
402 0 ▶sub sort_create {
Subroutine sort_create: not called CC=10 SCAR=47.0
Pod undocumented
403 0 my ($args) = @_;
404 0 my $validity = mailbox ($args->{mailbox}, 0);
405 $imap->capability->{sort}
406 0 ▶ or die "IMAP server does not advertise SORT\n";
Branch: 1 branch
BranchTrueFalseTotal
$imap->capability->{"sort"} or die "IMAP server does not advertise SORT\n" 0 0 0
407 0 ▶ my $limit = $args->{limit} // 100;
Condition0%0/2
$args->{"limit"} // 100
l!l
00
MC/DC0%0/1
$args->{"limit"} // 100
$args->{"limit"}
Truth table0%0/2
A
A= $args->{"limit"}
Aresult
11
00
408 0 ▶ defined $limit && !ref $limit && $limit =~ /\A[1-9][0-9]*\z/ && $limit <= 1000
Branch: 1 branch
BranchTrueFalseTotal
defined $limit and not ref $limit and $limit =~ /\A[1-9][0-9]*\z/u and $limit <= 1000 or die "SORT limit must be 1..1000\n" 0 0 0
Condition0%0/9
defined $limit and not ref $limit and $limit =~ /\A[1-9][0-9]*\z/u and $limit <= 1000
!ll&&!rl&&r
000
defined $limit and not ref $limit and $limit =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
000
defined $limit and not ref $limit
!ll&&!rl&&r
000
MC/DC0%0/4
defined $limit and not ref $limit and $limit =~ /\A[1-9][0-9]*\z/u and $limit <= 1000
defined $limit not ref $limit $limit =~ /\A[1-9][0-9]*\z/u $limit <= 1000
Truth table0%0/5
A and not(B) and C and D
A= defined $limitB= not ref $limitC= $limit =~ /\A[1-9][0-9]*\z/uD= $limit <= 1000
ABCDresult
0XXX0
10XX0
110X0
11100
11111
409 or die "SORT limit must be 1..1000\n";
410 0 my $nodes = 0;
411 0 ▶ my @criteria = search_filter ($args->{filter} // {}, 0, \$nodes);
Condition0%0/2
$args->{"filter"} // {}
l!l
00
MC/DC0%0/1
$args->{"filter"} // {}
$args->{"filter"}
Truth table0%0/2
A
A= $args->{"filter"}
Aresult
11
00
412 0 ▶ @criteria = ("ALL") unless @criteria;
Branch: 1 branch
BranchTrueFalseTotal
unless @criteria 0 0 0
413  
414 0 my $uids = checked ($imap->sort ("(SUBJECT DATE)", "UTF-8", @criteria), "UID SORT");
415 0 ▶ ref $uids eq "ARRAY" or die "Invalid UID SORT result\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $uids eq "ARRAY" or die "Invalid UID SORT result\n" 0 0 0
416 0 my $id = unpack "H*", AnyEvent::MCP::random_bytes 16;
417 $sort_snapshot = {
418 0 id => $id, mailbox => $args->{mailbox}, uidvalidity => $validity, uids => $uids,
419 };
420 0 sort_page ({ mailbox => $args->{mailbox}, snapshot_id => $id, limit => $limit }, $validity)
421 }
422  
423 0 ▶sub sort_discard {
Subroutine sort_discard: not called CC=5 SCAR=34.0
Pod undocumented
424 0 my ($args) = @_;
425 0 ▶ my $snap = $sort_snapshot
Branch: 1 branch
BranchTrueFalseTotal
my $snap = $sort_snapshot or die "No SORT snapshot (worker restarted or snapshot discarded)\n" 0 0 0
426 or die "No SORT snapshot (worker restarted or snapshot discarded)\n";
427 defined $args->{snapshot_id} && $args->{snapshot_id} eq $snap->{id}
428 && $args->{mailbox} eq $snap->{mailbox}
429 0 ▶ or die "Unknown or stale SORT snapshot ID\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"snapshot_id"} and $args->{"snapshot_id"} eq $snap->{"id"} and $args->{"mailbox"} eq $snap->{"mailbox"} or die "Unknown or stale SORT snapshot ID\n" 0 0 0
Condition0%0/6
defined $args->{"snapshot_id"} and $args->{"snapshot_id"} eq $snap->{"id"} and $args->{"mailbox"} eq $snap->{"mailbox"}
!ll&&!rl&&r
000
defined $args->{"snapshot_id"} and $args->{"snapshot_id"} eq $snap->{"id"}
!ll&&!rl&&r
000
MC/DC0%0/3
defined $args->{"snapshot_id"} and $args->{"snapshot_id"} eq $snap->{"id"} and $args->{"mailbox"} eq $snap->{"mailbox"}
defined $args->{"snapshot_id"} $args->{"snapshot_id"} eq $snap->{"id"} $args->{"mailbox"} eq $snap->{"mailbox"}
Truth table0%0/4
A and B and C
A= defined $args->{"snapshot_id"}B= $args->{"snapshot_id"} eq $snap->{"id"}C= $args->{"mailbox"} eq $snap->{"mailbox"}
ABCresult
0XX0
10X0
1100
1111
430 0 undef $sort_snapshot;
431 0 +{ ok => JSON::XS::true }
432 }
433  
434 0 ▶sub preview_parts {
Subroutine preview_parts: not called CC=11 SCAR=48.8
Pod undocumented
435 0 my ($node) = @_;
436 0 ▶ return unless ref $node eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "HASH" 0 0 0
437 0 ▶ return if lc ($node->{"Disposition-Type"} || "") eq "attachment";
Branch: 1 branch
BranchTrueFalseTotal
if lc($node->{"Disposition-Type"} || "") eq "attachment" 0 0 0
Condition0%0/2
$node->{'Disposition-Type'} || ''
l!l
00
MC/DC0%0/1
$node->{'Disposition-Type'} || ''
$node->{'Disposition-Type'}
Truth table0%0/2
A
A= $node->{'Disposition-Type'}
Aresult
11
00
438 return $node
439 if lc ($node->{"MIME-Type"} || "") eq "text"
440 && lc ($node->{"MIME-Subtype"} || "") =~ /\A(?:plain|html)\z/
441 0 ▶ && ($node->{"IMAP-Partnum"} // "") =~ /\A[1-9][0-9]*(?:\.[1-9][0-9]*)*\z/;
Branch: 1 branch
BranchTrueFalseTotal
if lc($node->{"MIME-Type"} || "") eq "text" and lc($node->{"MIME-Subtype"} or "") =~ /\A(?:plain|html)\z/u and ($node->{"IMAP-Partnum"} // "") =~ /\A[1-9][0-9]*(?:\.[1-9][0-9]*)*\z/u 0 0 0
Condition0%0/12
lc($node->{"MIME-Type"} || "") eq "text" and lc($node->{"MIME-Subtype"} or "") =~ /\A(?:plain|html)\z/u and ($node->{"IMAP-Partnum"} // "") =~ /\A[1-9][0-9]*(?:\.[1-9][0-9]*)*\z/u
!ll&&!rl&&r
000
lc($node->{"MIME-Type"} || "") eq "text" and lc($node->{"MIME-Subtype"} or "") =~ /\A(?:plain|html)\z/u
!ll&&!rl&&r
000
$node->{'MIME-Type'} || ''
l!l
00
$node->{'MIME-Subtype'} || ''
l!l
00
$node->{"IMAP-Partnum"} // ""
l!l
00
MC/DC0%0/3
lc($node->{"MIME-Type"} || "") eq "text" and lc($node->{"MIME-Subtype"} or "") =~ /\A(?:plain|html)\z/u and ($node->{"IMAP-Partnum"} // "") =~ /\A[1-9][0-9]*(?:\.[1-9][0-9]*)*\z/u
$node->{'MIME-Type'} $node->{'MIME-Subtype'} $node->{"IMAP-Partnum"}
Truth table0%0/4
A and B and C
A= $node->{'MIME-Type'}B= $node->{'MIME-Subtype'}C= $node->{"IMAP-Partnum"}
ABCresult
0XX0
10X0
1100
1111
442 0 ▶ map preview_parts ($_), @{$node->{"MIME-Subparts"} || []}
Condition0%0/2
$node->{'MIME-Subparts'} || []
l!l
00
MC/DC0%0/1
$node->{'MIME-Subparts'} || []
$node->{'MIME-Subparts'}
Truth table0%0/2
A
A= $node->{'MIME-Subparts'}
Aresult
11
00
443 }
444  
445 0 ▶sub preview_attachments {
Subroutine preview_attachments: not called CC=15 SCAR=54.8
Pod undocumented
446 0 my ($node) = @_;
447 0 ▶ return unless ref $node eq "HASH";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $node eq "HASH" 0 0 0
448 0 ▶ my $type = lc ($node->{"MIME-TxtType"} || "");
Condition0%0/2
$node->{'MIME-TxtType'} || ''
l!l
00
MC/DC0%0/1
$node->{'MIME-TxtType'} || ''
$node->{'MIME-TxtType'}
Truth table0%0/2
A
A= $node->{'MIME-TxtType'}
Aresult
11
00
449 0 ▶ my $disposition = lc ($node->{"Disposition-Type"} || "");
Condition0%0/2
$node->{'Disposition-Type'} || ''
l!l
00
MC/DC0%0/1
$node->{'Disposition-Type'} || ''
$node->{'Disposition-Type'}
Truth table0%0/2
A
A= $node->{'Disposition-Type'}
Aresult
11
00
450 0 ▶ my $name = $node->{"Content-Disposition"}{filename} // $node->{"Content-Type"}{name};
Condition0%0/3
$node->{"Content-Disposition"}{"filename"} // $node->{"Content-Type"}{"name"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$node->{"Content-Disposition"}{"filename"} // $node->{"Content-Type"}{"name"}
$node->{"Content-Disposition"}{"filename"} $node->{"Content-Type"}{"name"}
Truth table0%0/3
A // B
A= $node->{"Content-Disposition"}{"filename"}B= $node->{"Content-Type"}{"name"}
ABresult
1X1
011
000
451 0 ▶ if ($node->{"IMAP-Partnum"}
Branch: 1 branch
BranchTrueFalseTotal
if ($node->{"IMAP-Partnum"} and $disposition eq "attachment" || defined $name || $type =~ m[\A(?:image|audio|video|application|message)/]u || $type =~ m[\Atext/(?!plain\z|html\z)]u) 0 0 0
Condition0%0/12
$node->{"IMAP-Partnum"} and $disposition eq "attachment" || defined $name || $type =~ m[\A(?:image|audio|video|application|message)/]u || $type =~ m[\Atext/(?!plain\z|html\z)]u
!ll&&!rl&&r
000
$disposition eq "attachment" or defined $name or $type =~ m[\A(?:image|audio|video|application|message)/]u or $type =~ m[\Atext/(?!plain\z|html\z)]u
l!l&&r!l&&!r
000
$disposition eq "attachment" or defined $name or $type =~ m[\A(?:image|audio|video|application|message)/]u
l!l&&r!l&&!r
000
$disposition eq "attachment" or defined $name
l!l&&r!l&&!r
000
MC/DC0%0/5
$node->{"IMAP-Partnum"} and $disposition eq "attachment" || defined $name || $type =~ m[\A(?:image|audio|video|application|message)/]u || $type =~ m[\Atext/(?!plain\z|html\z)]u
$node->{"IMAP-Partnum"} $disposition eq "attachment" defined $name $type =~ m[\A(?:image|audio|video|application|message)/]u $type =~ m[\Atext/(?!plain\z|html\z)]u
Truth table0%0/6
A and B or C or D or E
A= $node->{"IMAP-Partnum"}B= $disposition eq "attachment"C= defined $nameD= $type =~ m[\A(?:image|audio|video|application|message)/]uE= $type =~ m[\Atext/(?!plain\z|html\z)]u
ABCDEresult
0XXXX0
100000
11XXX1
101XX1
1001X1
100011
452 && ($disposition eq "attachment" || defined $name
453 || $type =~ m!\A(?:image|audio|video|application|message)/!
454 || $type =~ m{\Atext/(?!plain\z|html\z)})) {
455 0 my $size = $node->{Size};
456 return +{
457 0 ▶ part => $node->{"IMAP-Partnum"},
Branch: 3 branches
BranchTrueFalseTotal
defined $name ? : 0 0 0
defined $size && $size =~ /\A\d+\z/u ? : 0 0 0
$disposition ? : 0 0 0
Condition0%0/3
defined $size and $size =~ /\A\d+\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $size and $size =~ /\A\d+\z/u
defined $size $size =~ /\A\d+\z/u
Truth table0%0/3
A and B
A= defined $sizeB= $size =~ /\A\d+\z/u
ABresult
0X0
100
111
458 type => $type,
459 (defined $name ? (name => mime_header ($name)) : ()),
460 (defined $size && $size =~ /\A\d+\z/ ? (size => 0 + $size) : ()),
461 ($disposition ? (disposition => $disposition) : ()),
462 };
463 }
464 0 ▶ map preview_attachments ($_), @{$node->{"MIME-Subparts"} || []}
Condition0%0/2
$node->{'MIME-Subparts'} || []
l!l
00
MC/DC0%0/1
$node->{'MIME-Subparts'} || []
$node->{'MIME-Subparts'}
Truth table0%0/2
A
A= $node->{'MIME-Subparts'}
Aresult
11
00
465 }
466  
467 0 ▶sub preview_html {
Subroutine preview_html: not called CC=1 SCAR=6.9
Pod undocumented
468 0 my ($html) = @_;
469 0 require HTML::Parser;
470  
471 0 my ($text, $skip, @links) = ("", 0);
472 my $parser = HTML::Parser->new (
473 api_version => 3,
474 0 ▶ start_h => [sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
475 0 my ($tag, $attr) = @_;
476 0 ▶ if ($tag =~ /\A(?:head|script|style|title)\z/) { ++$skip; return }
Branch: 1 branch
BranchTrueFalseTotal
if ($tag =~ /\A(?:head|script|style|title)\z/u) 0 0 0
477 0 ▶ return if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 0 0 0
478 0 ▶ $text .= "\n" if $tag =~ /\A(?:br|p|div|li|tr|h[1-6])\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /\A(?:br|p|div|li|tr|h[1-6])\z/u 0 0 0
479 0 ▶ push @links, [$attr->{href} // "", length $text] if $tag eq "a";
Branch: 1 branch
BranchTrueFalseTotal
if $tag eq "a" 0 0 0
Condition0%0/2
$attr->{"href"} // ""
l!l
00
MC/DC0%0/1
$attr->{"href"} // ""
$attr->{"href"}
Truth table0%0/2
A
A= $attr->{"href"}
Aresult
11
00
480 }, "tagname,attr"],
481 0 ▶ end_h => [sub {
Subroutine __ANON__: not called CC=9 SCAR=45.0
482 0 my ($tag) = @_;
483 0 ▶ if ($tag =~ /\A(?:head|script|style|title)\z/) { --$skip if $skip; return }
Branch: 2 branches
BranchTrueFalseTotal
if ($tag =~ /\A(?:head|script|style|title)\z/u) 0 0 0
if $skip 0 0 0
484 0 ▶ return if $skip;
Branch: 1 branch
BranchTrueFalseTotal
if $skip 0 0 0
485 0 ▶ if ($tag eq "a" && @links) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tag eq "a" and @links) 0 0 0
Condition0%0/3
$tag eq "a" and @links
!ll&&!rl&&r
000
MC/DC0%0/2
$tag eq "a" and @links
$tag eq "a" @links
Truth table0%0/3
A and B
A= $tag eq "a"B= @links
ABresult
0X0
100
111
486 0 my ($url, $start) = @{pop @links};
487 0 ▶ $text .= " ($url)" if $url =~ /\A(?:https?:\/\/|mailto:)/i
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ m[\A(?:https?://|mailto:)]iu and not substr($text, $start) =~ /\Q$url\E/u 0 0 0
Condition0%0/3
$url =~ m[\A(?:https?://|mailto:)]iu and not substr($text, $start) =~ /\Q$url\E/u
!ll&&!rl&&r
000
MC/DC0%0/2
$url =~ m[\A(?:https?://|mailto:)]iu and not substr($text, $start) =~ /\Q$url\E/u
$url =~ m[\A(?:https?://|mailto:)]iu not substr($text, $start) =~ /\Q$url\E/u
Truth table0%0/3
A and not(B)
A= $url =~ m[\A(?:https?://|mailto:)]iuB= not substr($text, $start) =~ /\Q$url\E/u
ABresult
0X0
100
111
488 && substr ($text, $start) !~ /\Q$url\E/;
489 }
490 0 ▶ $text .= "\n" if $tag =~ /\A(?:p|div|li|tr|h[1-6])\z/;
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ /\A(?:p|div|li|tr|h[1-6])\z/u 0 0 0
491 }, "tagname"],
492 0 ▶ text_h => [sub { $text .= $_[0] unless $skip }, "dtext"],
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $skip 0 0 0
493 );
494 0 $parser->parse ($html);
495 0 $parser->eof;
496 0 $text =~ s/[\t \x{a0}]+/ /g;
497 0 $text =~ s/ *\n */\n/g;
498 0 $text =~ s/\n{3,}/\n\n/g;
499 0 $text =~ s/\A\s+|\s+\z//g;
500 0 $text
501 }
502  
503 0 ▶sub preview_decode {
Subroutine preview_decode: not called CC=16 SCAR=56.1
Pod undocumented
504 0 my ($part, $bytes, $max) = @_;
505 0 ▶ defined $bytes && !ref $bytes or die "Missing MIME part data\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $bytes and not ref $bytes or die "Missing MIME part data\n" 0 0 0
Condition0%0/3
defined $bytes and not ref $bytes
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bytes and not ref $bytes
defined $bytes not ref $bytes
Truth table0%0/3
A and not(B)
A= defined $bytesB= not ref $bytes
ABresult
0X0
100
111
506 0 my $truncated = length $bytes > $max;
507 0 ▶ substr ($bytes, $max) = "" if $truncated;
Branch: 1 branch
BranchTrueFalseTotal
if $truncated 0 0 0
508 0 ▶ my $encoding = uc ($part->{"Content-Transfer-Encoding"} || "7BIT");
Condition0%0/2
$part->{'Content-Transfer-Encoding'} || '7BIT'
l!l
00
MC/DC0%0/1
$part->{'Content-Transfer-Encoding'} || '7BIT'
$part->{'Content-Transfer-Encoding'}
Truth table0%0/2
A
A= $part->{'Content-Transfer-Encoding'}
Aresult
11
00
509 0 ▶ if ($encoding eq "BASE64") {
Branch: 3 branches
BranchTrueFalseTotal
if ($encoding eq 'BASE64') { } 0 0 0
elsif ($encoding eq 'QUOTED-PRINTABLE') { } 0 0 0
elsif (not $encoding =~ /\A(?:7BIT|8BIT|BINARY)\z/u) { } 0 0 0
510 0 $bytes =~ s/\s+//g;
511 0 ▶ substr ($bytes, length ($bytes) - length ($bytes) % 4) = "" if $truncated;
Branch: 1 branch
BranchTrueFalseTotal
if $truncated 0 0 0
512 0 $bytes = MIME::Base64::decode_base64 ($bytes);
513 } elsif ($encoding eq "QUOTED-PRINTABLE") {
514 0 ▶ $bytes =~ s/=(?:[0-9A-Fa-f]|\r)?\z// if $truncated;
Branch: 1 branch
BranchTrueFalseTotal
if $truncated 0 0 0
515 0 $bytes = MIME::QuotedPrint::decode_qp ($bytes);
516 } elsif ($encoding !~ /\A(?:7BIT|8BIT|BINARY)\z/) {
517 0 die "Unsupported transfer encoding $encoding\n";
518 }
519  
520 0 ▶ my $charset = $part->{"Content-Type"}{charset} || "us-ascii";
Condition0%0/2
$part->{'Content-Type'}{'charset'} || 'us-ascii'
l!l
00
MC/DC0%0/1
$part->{'Content-Type'}{'charset'} || 'us-ascii'
$part->{'Content-Type'}{'charset'}
Truth table0%0/2
A
A= $part->{'Content-Type'}{'charset'}
Aresult
11
00
521 0 my $text = Encode::decode ($charset, $bytes, Encode::FB_QUIET ());
522 0 ▶ my $error = length $bytes && !$truncated ? "Invalid $charset text encoding" : undef;
Branch: 1 branch
BranchTrueFalseTotal
length $bytes && !$truncated ? : 0 0 0
Condition0%0/3
length $bytes and not $truncated
!ll&&!rl&&r
000
MC/DC0%0/2
length $bytes and not $truncated
length $bytes not $truncated
Truth table0%0/3
A and not(B)
A= length $bytesB= not $truncated
ABresult
0X0
100
111
523 0 my $type = lc $part->{"MIME-Subtype"};
524 0 ▶ $text = preview_html ($text) if $type eq "html";
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "html" 0 0 0
525 +{
526 text => $text,
527 content_type => "text/$type",
528 0 ▶ part => $part->{"IMAP-Partnum"},
Branch: 2 branches
BranchTrueFalseTotal
$truncated ? : 0 0 0
defined $error ? : 0 0 0
529 truncated => $truncated ? JSON::XS::true : JSON::XS::false,
530 (defined $error ? (error => $error) : ()),
531 }
532 }
533  
534 # One UID FETCH per distinct MIME part number, never per message.
535 0 ▶sub preview_fetch {
Subroutine preview_fetch: not called CC=6 SCAR=37.4
Pod undocumented
536 0 my ($parts, $max) = @_;
537 0 my %group;
538 0 push @{$group{$parts->{$_}{"IMAP-Partnum"}}}, $_ for keys %$parts;
539 0 my %result;
540 0 for my $part (keys %group) {
541 0 my $f = checked ($imap->fetch ($group{$part},
542 "body.peek[$part]<0." . ($max + 1) . ">"), "Fetch preview $part");
543 0 for my $uid (@{$group{$part}}) {
544 0 ▶ $result{$uid} = eval { preview_decode ($parts->{$uid}, $f->{$uid}{body}, $max) }
Condition0%0/4
eval { do { preview_decode($parts->{$uid}, $f->{$uid}{'body'}, $max) } } || {'error', "$@" || 'Unable to decode preview'}
l!l
00
"$@" || 'Unable to decode preview'
l!l
00
MC/DC0%0/1
eval { do { preview_decode($parts->{$uid}, $f->{$uid}{'body'}, $max) } } || {'error', "$@" || 'Unable to decode preview'}
eval { do { preview_decode($parts->{$uid}, $f->{$uid}{'body'}, $max) } }
MC/DC0%0/1
"$@" || 'Unable to decode preview'
"$@"
Truth table0%0/2
A
A= eval { do { preview_decode($parts->{$uid}, $f->{$uid}{'body'}, $max) } }
Aresult
11
00
Truth table0%0/2
A
A= "$@"
Aresult
11
00
545 || { error => "$@" || "Unable to decode preview" };
546 }
547 }
548 0 \%result
549 }
550  
551 0 ▶sub previews {
Subroutine previews: not called CC=29 SCAR=67.7
Pod undocumented
552 0 my ($args) = @_;
553 0 my $validity = same_validity ($args, 0);
554 0 my $uids = $args->{uids};
555 0 uids ($args);
556 0 ▶ @$uids <= 25 or die "previews limited to 25 UIDs\n";
Branch: 1 branch
BranchTrueFalseTotal
@$uids <= 25 or die "previews limited to 25 UIDs\n" 0 0 0
557 0 ▶ my $max = $args->{max_bytes} // 2048;
Condition0%0/2
$args->{"max_bytes"} // 2048
l!l
00
MC/DC0%0/1
$args->{"max_bytes"} // 2048
$args->{"max_bytes"}
Truth table0%0/2
A
A= $args->{"max_bytes"}
Aresult
11
00
558 0 ▶ $max =~ /\A[1-9][0-9]*\z/ && $max <= 4096 or die "max_bytes must be 1..4096\n";
Branch: 1 branch
BranchTrueFalseTotal
$max =~ /\A[1-9][0-9]*\z/u and $max <= 4096 or die "max_bytes must be 1..4096\n" 0 0 0
Condition0%0/3
$max =~ /\A[1-9][0-9]*\z/u and $max <= 4096
!ll&&!rl&&r
000
MC/DC0%0/2
$max =~ /\A[1-9][0-9]*\z/u and $max <= 4096
$max =~ /\A[1-9][0-9]*\z/u $max <= 4096
Truth table0%0/3
A and B
A= $max =~ /\A[1-9][0-9]*\z/uB= $max <= 4096
ABresult
0X0
100
111
559 0 my $f = checked ($imap->fetch ($uids, "bodystructure"), "Fetch MIME structures");
560 0 my (%primary, %fallback, %missing, %attachments);
561 0 for my $uid (@$uids) {
562 0 my $item = $f->{$uid};
563 0 ▶ my @parts = preview_parts ($item ? $item->{bodystructure} : undef);
Branch: 1 branch
BranchTrueFalseTotal
$item ? : 0 0 0
564 0 ▶ $attachments{$uid} = [preview_attachments ($item->{bodystructure})] if $item;
Branch: 1 branch
BranchTrueFalseTotal
if $item 0 0 0
565 0 my ($plain) = grep lc ($_ ->{"MIME-Subtype"}) eq "plain", @parts;
566 0 my ($html) = grep lc ($_ ->{"MIME-Subtype"}) eq "html", @parts;
567 0 ▶ $primary{$uid} = $plain || $html if $plain || $html;
Branch: 1 branch
BranchTrueFalseTotal
if $plain or $html 0 0 0
Condition0%0/6
$plain or $html
l!l&&r!l&&!r
000
$plain || $html
l!l&&r!l&&!r
000
MC/DC0%0/2
$plain or $html
$plain $html
MC/DC0%0/2
$plain || $html
$plain $html
Truth table0%0/3
A or B
A= $plainB= $html
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $plainB= $html
ABresult
1X1
011
000
568 0 ▶ $fallback{$uid} = $html if $plain && $html;
Branch: 1 branch
BranchTrueFalseTotal
if $plain and $html 0 0 0
Condition0%0/3
$plain and $html
!ll&&!rl&&r
000
MC/DC0%0/2
$plain and $html
$plain $html
Truth table0%0/3
A and B
A= $plainB= $html
ABresult
0X0
100
111
569 0 ▶ $missing{$uid} = $item ? "No text MIME part" : "Message vanished"
Branch: 2 branches
BranchTrueFalseTotal
unless $plain or $html 0 0 0
$item ? : 0 0 0
Condition0%0/3
$plain or $html
l!l&&r!l&&!r
000
MC/DC0%0/2
$plain or $html
$plain $html
Truth table0%0/3
A or B
A= $plainB= $html
ABresult
1X1
011
000
570 unless $plain || $html;
571 }
572 0 my $result = preview_fetch (\%primary, $max);
573 0 my %retry;
574 0 for my $uid (keys %fallback) {
575 0 ▶ my $text = $result->{$uid}{text} // "";
Condition0%0/2
$result->{$uid}{"text"} // ""
l!l
00
MC/DC0%0/1
$result->{$uid}{"text"} // ""
$result->{$uid}{"text"}
Truth table0%0/2
A
A= $result->{$uid}{"text"}
Aresult
11
00
576 0 ▶ $retry{$uid} = $fallback{$uid}
Branch: 1 branch
BranchTrueFalseTotal
if length $text < 120 or $text =~ /(?:view (?:this |the )?(?:mail|message|email) (?:on|in) (?:the )?(?:web|browser)|click here)/iu 0 0 0
Condition0%0/3
length $text < 120 or $text =~ /(?:view (?:this |the )?(?:mail|message|email) (?:on|in) (?:the )?(?:web|browser)|click here)/iu
l!l&&r!l&&!r
000
MC/DC0%0/2
length $text < 120 or $text =~ /(?:view (?:this |the )?(?:mail|message|email) (?:on|in) (?:the )?(?:web|browser)|click here)/iu
length $text < 120 $text =~ /(?:view (?:this |the )?(?:mail|message|email) (?:on|in) (?:the )?(?:web|browser)|click here)/iu
Truth table0%0/3
A or B
A= length $text < 120B= $text =~ /(?:view (?:this |the )?(?:mail|message|email) (?:on|in) (?:the )?(?:web|browser)|click here)/iu
ABresult
1X1
011
000
577 if length ($text) < 120
578 || $text =~ /(?:view (?:this |the )?(?:mail|message|email) (?:on|in) (?:the )?(?:web|browser)|click here)/i;
579 }
580 0 ▶ if (%retry) {
Branch: 1 branch
BranchTrueFalseTotal
if (%retry) 0 0 0
581 0 my $html = preview_fetch (\%retry, $max);
582 0 for my $uid (keys %retry) {
583 0 my $new = $html->{$uid};
584 0 my $old = $result->{$uid};
585 $result->{$uid} = $new if defined $new->{text}
586 0 ▶ && (!defined $old->{text} || length ($new->{text}) > length ($old->{text}));
Branch: 1 branch
BranchTrueFalseTotal
if defined $new->{"text"} and !defined($old->{"text"}) || length $new->{"text"} > length $old->{"text"} 0 0 0
Condition0%0/6
defined $new->{"text"} and !defined($old->{"text"}) || length $new->{"text"} > length $old->{"text"}
!ll&&!rl&&r
000
not defined $old->{"text"} or length $new->{"text"} > length $old->{"text"}
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $new->{"text"} and !defined($old->{"text"}) || length $new->{"text"} > length $old->{"text"}
defined $new->{"text"} not defined $old->{"text"} length $new->{"text"} > length $old->{"text"}
Truth table0%0/4
A and not(B) or C
A= defined $new->{"text"}B= not defined $old->{"text"}C= length $new->{"text"} > length $old->{"text"}
ABCresult
0XX0
1000
11X1
1011
587 }
588 }
589 +{
590 uidvalidity => $validity,
591 previews => [map +{
592 uid => 0 + $_,
593 0 ▶ %{$result->{$_} || { error => $missing{$_} || "No preview" }},
Branch: 1 branch
BranchTrueFalseTotal
@{$attachments{$_} or [];} ? : 0 0 0
Condition0%0/4
$result->{$_} || {'error', $missing{$_} || 'No preview'}
l!l
00
$missing{$_} || 'No preview'
l!l
00
MC/DC0%0/1
$result->{$_} || {'error', $missing{$_} || 'No preview'}
$result->{$_}
MC/DC0%0/1
$missing{$_} || 'No preview'
$missing{$_}
Truth table0%0/2
A
A= $result->{$_}
Aresult
11
00
Truth table0%0/2
A
A= $missing{$_}
Aresult
11
00
594 0 ▶ (@{$attachments{$_} || []} ? (attachments => $attachments{$_}) : ()),
Condition0%0/2
$attachments{$_} || []
l!l
00
MC/DC0%0/1
$attachments{$_} || []
$attachments{$_}
Truth table0%0/2
A
A= $attachments{$_}
Aresult
11
00
595 }, @$uids],
596 }
597 }
598  
599 0 ▶sub part {
Subroutine part: not called CC=25 SCAR=64.8
Pod undocumented
600 0 my ($args, $raw_message) = @_;
601  
602 0 my $validity = same_validity ($args, 0);
603 0 my $uid = $args->{uid};
604 0 ▶ my $part = $raw_message ? "" : $args->{part};
Branch: 1 branch
BranchTrueFalseTotal
$raw_message ? : 0 0 0
605 0 ▶ defined $part && $part =~ /\A(?:[1-9][0-9]*(?:\.[1-9][0-9]*)*)?\z/
Branch: 1 branch
BranchTrueFalseTotal
defined $part and $part =~ /\A(?:[1-9][0-9]*(?:\.[1-9][0-9]*)*)?\z/u or die "Invalid MIME part number\n" 0 0 0
Condition0%0/3
defined $part and $part =~ /\A(?:[1-9][0-9]*(?:\.[1-9][0-9]*)*)?\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $part and $part =~ /\A(?:[1-9][0-9]*(?:\.[1-9][0-9]*)*)?\z/u
defined $part $part =~ /\A(?:[1-9][0-9]*(?:\.[1-9][0-9]*)*)?\z/u
Truth table0%0/3
A and B
A= defined $partB= $part =~ /\A(?:[1-9][0-9]*(?:\.[1-9][0-9]*)*)?\z/u
ABresult
0X0
100
111
606 or die "Invalid MIME part number\n";
607 0 ▶ my $max = $args->{max_bytes} || 65536;
Condition0%0/2
$args->{'max_bytes'} || 65536
l!l
00
MC/DC0%0/1
$args->{'max_bytes'} || 65536
$args->{'max_bytes'}
Truth table0%0/2
A
A= $args->{'max_bytes'}
Aresult
11
00
608 0 ▶ $max =~ /\A[1-9][0-9]*\z/ && $max <= 262144
Branch: 1 branch
BranchTrueFalseTotal
$max =~ /\A[1-9][0-9]*\z/u and $max <= 262144 or die "max_bytes must be 1..262144\n" 0 0 0
Condition0%0/3
$max =~ /\A[1-9][0-9]*\z/u and $max <= 262144
!ll&&!rl&&r
000
MC/DC0%0/2
$max =~ /\A[1-9][0-9]*\z/u and $max <= 262144
$max =~ /\A[1-9][0-9]*\z/u $max <= 262144
Truth table0%0/3
A and B
A= $max =~ /\A[1-9][0-9]*\z/uB= $max <= 262144
ABresult
0X0
100
111
609 or die "max_bytes must be 1..262144\n";
610  
611 0 my $f = checked ($imap->fetch ($uid, "body.peek[$part]<0." . ($max + 1) . ">"), "Fetch part $uid");
612 0 ▶ my $item = $f->{$uid} or die "Message $uid vanished\n";
Branch: 1 branch
BranchTrueFalseTotal
my $item = $f->{$uid} or die "Message $uid vanished\n" 0 0 0
613 0 my $bytes = $item->{body};
614 0 ▶ defined $bytes && !ref $bytes or die "Invalid part data\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $bytes and not ref $bytes or die "Invalid part data\n" 0 0 0
Condition0%0/3
defined $bytes and not ref $bytes
!ll&&!rl&&r
000
MC/DC0%0/2
defined $bytes and not ref $bytes
defined $bytes not ref $bytes
Truth table0%0/3
A and not(B)
A= defined $bytesB= not ref $bytes
ABresult
0X0
100
111
615 0 my $truncated = length $bytes > $max;
616 0 ▶ substr ($bytes, $max) = "" if $truncated;
Branch: 1 branch
BranchTrueFalseTotal
if $truncated 0 0 0
617 0 ▶ my $result = {
Branch: 1 branch
BranchTrueFalseTotal
$truncated ? : 0 0 0
618 uid => 0 + $uid, uidvalidity => $validity, part => $part,
619 data_base64 => MIME::Base64::encode_base64 ($bytes, ""),
620 truncated => $truncated ? JSON::XS::true : JSON::XS::false,
621 };
622  
623 0 ▶ if ($args->{decode_text} && !$truncated && !$raw_message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"decode_text"} and not $truncated and not $raw_message) 0 0 0
Condition0%0/6
$args->{"decode_text"} and not $truncated and not $raw_message
!ll&&!rl&&r
000
$args->{"decode_text"} and not $truncated
!ll&&!rl&&r
000
MC/DC0%0/3
$args->{"decode_text"} and not $truncated and not $raw_message
$args->{"decode_text"} not $truncated not $raw_message
Truth table0%0/4
A and not(B) and not(C)
A= $args->{"decode_text"}B= not $truncatedC= not $raw_message
ABCresult
0XX0
10X0
1100
1111
624 0 my $bs = checked ($imap->fetch ($uid, "bodystructure"), "Fetch MIME structure")->{$uid}{bodystructure};
625 0 my @pending = ($bs);
626 0 my $node;
627 0 ▶ while (@pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending) 0 0 0
628 0 my $candidate = shift @pending;
629 0 ▶ if (($candidate->{"IMAP-Partnum"} // "") eq $part && $candidate->{"MIME-Type"} eq "text") {
Branch: 1 branch
BranchTrueFalseTotal
if (($candidate->{"IMAP-Partnum"} // "") eq $part and $candidate->{"MIME-Type"} eq "text") 0 0 0
Condition0%0/5
($candidate->{"IMAP-Partnum"} // "") eq $part and $candidate->{"MIME-Type"} eq "text"
!ll&&!rl&&r
000
$candidate->{"IMAP-Partnum"} // ""
l!l
00
MC/DC0%0/2
($candidate->{"IMAP-Partnum"} // "") eq $part and $candidate->{"MIME-Type"} eq "text"
$candidate->{"IMAP-Partnum"} $candidate->{"MIME-Type"} eq "text"
Truth table0%0/3
A and B
A= $candidate->{"IMAP-Partnum"}B= $candidate->{"MIME-Type"} eq "text"
ABresult
0X0
100
111
630 0 $node = $candidate;
631 0 last;
632 }
633 0 ▶ push @pending, @{$candidate->{"MIME-Subparts"} || []};
Condition0%0/2
$candidate->{'MIME-Subparts'} || []
l!l
00
MC/DC0%0/1
$candidate->{'MIME-Subparts'} || []
$candidate->{'MIME-Subparts'}
Truth table0%0/2
A
A= $candidate->{'MIME-Subparts'}
Aresult
11
00
634 }
635 0 ▶ $node or die "Part is not a text MIME leaf\n";
Branch: 1 branch
BranchTrueFalseTotal
$node or die "Part is not a text MIME leaf\n" 0 0 0
636 0 ▶ my $encoding = uc ($node->{"Content-Transfer-Encoding"} || "");
Condition0%0/2
$node->{'Content-Transfer-Encoding'} || ''
l!l
00
MC/DC0%0/1
$node->{'Content-Transfer-Encoding'} || ''
$node->{'Content-Transfer-Encoding'}
Truth table0%0/2
A
A= $node->{'Content-Transfer-Encoding'}
Aresult
11
00
637 0 ▶ $bytes = MIME::Base64::decode_base64 ($bytes) if $encoding eq "BASE64";
Branch: 1 branch
BranchTrueFalseTotal
if $encoding eq "BASE64" 0 0 0
638 0 ▶ $bytes = MIME::QuotedPrint::decode_qp ($bytes) if $encoding eq "QUOTED-PRINTABLE";
Branch: 1 branch
BranchTrueFalseTotal
if $encoding eq "QUOTED-PRINTABLE" 0 0 0
639 0 ▶ my $charset = $node->{"Content-Type"}{charset} || "us-ascii";
Condition0%0/2
$node->{'Content-Type'}{'charset'} || 'us-ascii'
l!l
00
MC/DC0%0/1
$node->{'Content-Type'}{'charset'} || 'us-ascii'
$node->{'Content-Type'}{'charset'}
Truth table0%0/2
A
A= $node->{'Content-Type'}{'charset'}
Aresult
11
00
640 0 $result->{text} = Encode::decode ($charset, $bytes, Encode::FB_CROAK ());
641 0 $result->{content_type} = $node->{"MIME-TxtType"};
642 }
643  
644 $result
645 0 }
646  
647 0 ▶sub run {
Subroutine run: not called CC=71 SCAR=85.4
Pod undocumented
648 0 my ($cmd, $args) = @_;
649  
650 0 my $result = eval {
651 0 ▶ if ($cmd eq "connect") {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "connect") 0 0 0
652 0 undef $sort_snapshot;
653 0 return (1, imap_connect ($args));
654 }
655 0 ▶ return (1, sort_discard ($args)) if $cmd eq "sort_discard";
Branch: 1 branch
BranchTrueFalseTotal
if $cmd eq "sort_discard" 0 0 0
656  
657 0 ▶ if ($cmd eq "ping") {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "ping") 0 0 0
658 0 ▶ $imap && $imap->is_open or die "IMAP connection closed during keepalive\n";
Branch: 1 branch
BranchTrueFalseTotal
$imap and $imap->is_open or die "IMAP connection closed during keepalive\n" 0 0 0
Condition0%0/3
$imap and $imap->is_open
!ll&&!rl&&r
000
MC/DC0%0/2
$imap and $imap->is_open
$imap $imap->is_open
Truth table0%0/3
A and B
A= $imapB= $imap->is_open
ABresult
0X0
100
111
659 0 eval {
660 0 my $timeout = "IMAP keepalive timed out\n";
661 0 ▶ local $SIG{__WARN__} = sub { CORE::warn @_ unless $_[0] eq $timeout };
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
unless $_[0] eq $timeout 0 0 0
662 0 ▶ local $SIG{ALRM} = sub { die $timeout };
Subroutine __ANON__: not called CC=1 SCAR=6.9
663 0 alarm 30;
664 0 checked ($imap->noop, "NOOP");
665 };
666 0 my $error = $@;
667 0 alarm 0;
668 0 ▶ die $error if $error;
Branch: 1 branch
BranchTrueFalseTotal
if $error 0 0 0
669 0 return (1, { ok => JSON::XS::true });
670 }
671 0 ▶ $imap && $imap->is_open or imap_connect $config;
Branch: 1 branch
BranchTrueFalseTotal
$imap and $imap->is_open or imap_connect($config) 0 0 0
Condition0%0/3
$imap and $imap->is_open
!ll&&!rl&&r
000
MC/DC0%0/2
$imap and $imap->is_open
$imap $imap->is_open
Truth table0%0/3
A and B
A= $imapB= $imap->is_open
ABresult
0X0
100
111
672  
673 0 ▶ if ($cmd eq "mailboxes" || $cmd eq "lsub") {
Branch: 13 branches
BranchTrueFalseTotal
if ($cmd eq 'mailboxes' or $cmd eq 'lsub') { } 0 0 0
elsif ($cmd eq 'messages') { } 0 0 0
elsif ($cmd eq 'threads') { } 0 0 0
elsif ($cmd eq 'sort_create') { } 0 0 0
elsif ($cmd eq 'sort_page') { } 0 0 0
elsif ($cmd eq 'previews') { } 0 0 0
elsif ($cmd eq 'message') { } 0 0 0
elsif ($cmd eq 'part' or $cmd eq 'raw') { } 0 0 0
elsif ($cmd eq 'flags') { } 0 0 0
elsif ($cmd eq 'copy' or $cmd eq 'move') { } 0 0 0
elsif ($cmd eq 'append') { } 0 0 0
elsif ($cmd eq 'expunge') { } 0 0 0
elsif ($cmd eq 'mailbox') { } 0 0 0
Condition0%0/3
$cmd eq "mailboxes" or $cmd eq "lsub"
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd eq "mailboxes" or $cmd eq "lsub"
$cmd eq "mailboxes" $cmd eq "lsub"
Truth table0%0/3
A or B
A= $cmd eq "mailboxes"B= $cmd eq "lsub"
ABresult
1X1
011
000
674 0 ▶ my $method = $cmd eq "lsub" ? "lsub" : "list";
Branch: 1 branch
BranchTrueFalseTotal
$cmd eq 'lsub' ? : 0 0 0
675 0 return (1, { mailboxes => checked ($imap->$method ("", "*"), uc $method) });
676 } elsif ($cmd eq "messages") {
677 0 return (1, messages ($args));
678 } elsif ($cmd eq "threads") {
679 0 return (1, threads ($args));
680 } elsif ($cmd eq "sort_create") {
681 0 return (1, sort_create ($args));
682 } elsif ($cmd eq "sort_page") {
683 0 return (1, sort_page ($args));
684 } elsif ($cmd eq "previews") {
685 0 return (1, previews ($args));
686 } elsif ($cmd eq "message") {
687 0 return (1, message ($args));
688 ▶ } elsif ($cmd eq "part" || $cmd eq "raw") {
Condition0%0/3
$cmd eq "part" or $cmd eq "raw"
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd eq "part" or $cmd eq "raw"
$cmd eq "part" $cmd eq "raw"
Truth table0%0/3
A or B
A= $cmd eq "part"B= $cmd eq "raw"
ABresult
1X1
011
000
689 0 return (1, part ($args, $cmd eq "raw"));
690 } elsif ($cmd eq "flags") {
691 0 my $validity = same_validity ($args, 1);
692 0 my $uidset = uids ($args);
693 0 ▶ $args->{operation} =~ /\A(?:add|remove)\z/
Branch: 1 branch
BranchTrueFalseTotal
$args->{"operation"} =~ /\A(?:add|remove)\z/u or die "Invalid flag operation\n" 0 0 0
694 or die "Invalid flag operation\n";
695 0 my $flags = $args->{flags};
696 0 ▶ ref $flags eq "ARRAY" && @$flags && @$flags <= 20
Branch: 1 branch
BranchTrueFalseTotal
ref $flags eq "ARRAY" and @$flags and @$flags <= 20 or die "flags must be an array of 1..20 flags\n" 0 0 0
Condition0%0/6
ref $flags eq "ARRAY" and @$flags and @$flags <= 20
!ll&&!rl&&r
000
ref $flags eq "ARRAY" and @$flags
!ll&&!rl&&r
000
MC/DC0%0/3
ref $flags eq "ARRAY" and @$flags and @$flags <= 20
ref $flags eq "ARRAY" @$flags @$flags <= 20
Truth table0%0/4
A and B and C
A= ref $flags eq "ARRAY"B= @$flagsC= @$flags <= 20
ABCresult
0XX0
10X0
1100
1111
697 or die "flags must be an array of 1..20 flags\n";
698 0 for (@$flags) {
699 0 ▶ /\A(?:\\(?:Seen|Flagged|Answered|Deleted|Draft)|[A-Za-z][A-Za-z0-9_-]{0,63})\z/i
Branch: 1 branch
BranchTrueFalseTotal
/\A(?:\\(?:Seen|Flagged|Answered|Deleted|Draft)|[A-Za-z][A-Za-z0-9_-]{0,63})\z/iu or die "Invalid IMAP flag\n" 0 0 0
700 or die "Invalid IMAP flag\n";
701 }
702 0 ▶ checked ($imap->store ($uidset, ($args->{operation} eq "add" ? "+" : "-") . "flags.silent",
Branch: 1 branch
BranchTrueFalseTotal
$args->{'operation'} eq 'add' ? : 0 0 0
703 "(" . join (" ", @$flags) . ")"), "STORE");
704 0 return (1, { ok => JSON::XS::true, uidvalidity => $validity, uids => $args->{uids} });
705 ▶ } elsif ($cmd eq "copy" || $cmd eq "move") {
Condition0%0/3
$cmd eq "copy" or $cmd eq "move"
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd eq "copy" or $cmd eq "move"
$cmd eq "copy" $cmd eq "move"
Truth table0%0/3
A or B
A= $cmd eq "copy"B= $cmd eq "move"
ABresult
1X1
011
000
706 0 my $validity = same_validity ($args, 1);
707 0 my $uidset = uids ($args);
708 $cmd ne "move" || $imap->capability->{move}
709 0 ▶ or die "Server does not advertise MOVE; refusing unsafe fallback\n";
Branch: 1 branch
BranchTrueFalseTotal
$cmd ne "move" or $imap->capability->{"move"} or die "Server does not advertise MOVE; refusing unsafe fallback\n" 0 0 0
Condition0%0/3
$cmd ne "move" or $imap->capability->{"move"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmd ne "move" or $imap->capability->{"move"}
$cmd ne "move" $imap->capability->{"move"}
Truth table0%0/3
A or B
A= $cmd ne "move"B= $imap->capability->{"move"}
ABresult
1X1
011
000
710 0 checked ($imap->$cmd ($uidset, $args->{destination}), uc $cmd);
711 0 return (1, { ok => JSON::XS::true, uidvalidity => $validity, uids => $args->{uids} });
712 } elsif ($cmd eq "append") {
713 defined $args->{data_base64} && !ref $args->{data_base64}
714 && length $args->{data_base64} <= 1398112
715 0 ▶ && $args->{data_base64} =~ /\A(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?\z/
Branch: 1 branch
BranchTrueFalseTotal
defined $args->{"data_base64"} and not ref $args->{"data_base64"} and length $args->{"data_base64"} <= 1398112 and $args->{"data_base64"} =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]u or die "Invalid or oversized Base64 message\n" 0 0 0
Condition0%0/9
defined $args->{"data_base64"} and not ref $args->{"data_base64"} and length $args->{"data_base64"} <= 1398112 and $args->{"data_base64"} =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]u
!ll&&!rl&&r
000
defined $args->{"data_base64"} and not ref $args->{"data_base64"} and length $args->{"data_base64"} <= 1398112
!ll&&!rl&&r
000
defined $args->{"data_base64"} and not ref $args->{"data_base64"}
!ll&&!rl&&r
000
MC/DC0%0/4
defined $args->{"data_base64"} and not ref $args->{"data_base64"} and length $args->{"data_base64"} <= 1398112 and $args->{"data_base64"} =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]u
defined $args->{"data_base64"} not ref $args->{"data_base64"} length $args->{"data_base64"} <= 1398112 $args->{"data_base64"} =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]u
Truth table0%0/5
A and not(B) and C and D
A= defined $args->{"data_base64"}B= not ref $args->{"data_base64"}C= length $args->{"data_base64"} <= 1398112D= $args->{"data_base64"} =~ m[\A(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?\z]u
ABCDresult
0XXX0
10XX0
110X0
11100
11111
716 or die "Invalid or oversized Base64 message\n";
717 0 my $bytes = MIME::Base64::decode_base64 ($args->{data_base64});
718 0 ▶ length $bytes <= 1048576 or die "APPEND limited to 1 MiB\n";
Branch: 1 branch
BranchTrueFalseTotal
length $bytes <= 1048576 or die "APPEND limited to 1 MiB\n" 0 0 0
719 0 ▶ $bytes =~ /\r\n/ or die "APPEND expects an RFC 5322 message with CRLF line endings\n";
Branch: 1 branch
BranchTrueFalseTotal
$bytes =~ /\r\n/u or die "APPEND expects an RFC 5322 message with CRLF line endings\n" 0 0 0
720  
721 0 ▶ unless ($config->{allow_mbox_from}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"allow_mbox_from"}) 0 0 0
722 0 my $line = mbox_from ($bytes);
723 0 ▶ die "APPEND refused: raw message line $line resembles an unescaped mboxo From_ separator "
Branch: 1 branch
BranchTrueFalseTotal
if $line 0 0 0
724 . "(starts with 'From ' and has date-like text). Some mbox servers may mistake it "
725 . "for a new message boundary. No APPEND attempted. Escape the line, or ask the "
726 . "server operator to use --allow-mbox-from.\n"
727 if $line;
728 }
729  
730 ref $args->{flags} eq "ARRAY" || !defined $args->{flags}
731 0 ▶ or die "Invalid flags\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{"flags"} eq "ARRAY" or not defined $args->{"flags"} or die "Invalid flags\n" 0 0 0
Condition0%0/3
ref $args->{"flags"} eq "ARRAY" or not defined $args->{"flags"}
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $args->{"flags"} eq "ARRAY" or not defined $args->{"flags"}
ref $args->{"flags"} eq "ARRAY" not defined $args->{"flags"}
Truth table0%0/3
A or not(B)
A= ref $args->{"flags"} eq "ARRAY"B= not defined $args->{"flags"}
ABresult
1X1
011
000
732 0 ▶ for (@{$args->{flags} || []}) {
Condition0%0/2
$args->{'flags'} || []
l!l
00
MC/DC0%0/1
$args->{'flags'} || []
$args->{'flags'}
Truth table0%0/2
A
A= $args->{'flags'}
Aresult
11
00
733 0 ▶ defined $_ && !ref $_ && /\A(?:\\(?:Seen|Flagged|Answered|Deleted|Draft)|[A-Za-z][A-Za-z0-9_-]{0,63})\z/i
Branch: 1 branch
BranchTrueFalseTotal
defined $_ and not ref $_ and /\A(?:\\(?:Seen|Flagged|Answered|Deleted|Draft)|[A-Za-z][A-Za-z0-9_-]{0,63})\z/iu or die "Invalid IMAP flag\n" 0 0 0
Condition0%0/6
defined $_ and not ref $_ and /\A(?:\\(?:Seen|Flagged|Answered|Deleted|Draft)|[A-Za-z][A-Za-z0-9_-]{0,63})\z/iu
!ll&&!rl&&r
000
defined $_ and not ref $_
!ll&&!rl&&r
000
MC/DC0%0/3
defined $_ and not ref $_ and /\A(?:\\(?:Seen|Flagged|Answered|Deleted|Draft)|[A-Za-z][A-Za-z0-9_-]{0,63})\z/iu
defined $_ not ref $_ /\A(?:\\(?:Seen|Flagged|Answered|Deleted|Draft)|[A-Za-z][A-Za-z0-9_-]{0,63})\z/iu
Truth table0%0/4
A and not(B) and C
A= defined $_B= not ref $_C= /\A(?:\\(?:Seen|Flagged|Answered|Deleted|Draft)|[A-Za-z][A-Za-z0-9_-]{0,63})\z/iu
ABCresult
0XX0
10X0
1100
1111
734 or die "Invalid IMAP flag\n";
735 }
736 0 my @extra = $args->{flags} && @{$args->{flags}}
737 0 ▶ ? ("(" . (join " ", @{$args->{flags}}) . ")") : ();
Branch: 1 branch
BranchTrueFalseTotal
$args->{'flags'} && @{$args->{'flags'};} ? : 0 0 0
Condition0%0/3
$args->{"flags"} and @{$args->{"flags"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$args->{"flags"} and @{$args->{"flags"};}
$args->{"flags"} @{$args->{"flags"};}
Truth table0%0/3
A and B
A= $args->{"flags"}B= @{$args->{"flags"};}
ABresult
0X0
100
111
738 0 checked ($imap->append ($args->{mailbox}, @extra, { Literal => $bytes }), "APPEND");
739 0 return (1, { ok => JSON::XS::true });
740 } elsif ($cmd eq "expunge") {
741 0 my $validity = same_validity ($args, 1);
742 0 ▶ if ($args->{uids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'uids'}) { } 0 0 0
743 $imap->capability->{uidplus}
744 0 ▶ or die "Server lacks UIDPLUS; refusing whole-mailbox fallback\n";
Branch: 1 branch
BranchTrueFalseTotal
$imap->capability->{"uidplus"} or die "Server lacks UIDPLUS; refusing whole-mailbox fallback\n" 0 0 0
745 0 checked ($imap->uidexpunge (uids ($args)), "UID EXPUNGE");
746 } else {
747 0 checked ($imap->expunge, "EXPUNGE");
748 }
749 0 return (1, { ok => JSON::XS::true, uidvalidity => $validity });
750 } elsif ($cmd eq "mailbox") {
751 0 my $op = $args->{operation};
752 0 ▶ defined $op && $op =~ /\A(?:create|rename|delete|subscribe|unsubscribe)\z/
Branch: 1 branch
BranchTrueFalseTotal
defined $op and $op =~ /\A(?:create|rename|delete|subscribe|unsubscribe)\z/u or die "Invalid mailbox operation\n" 0 0 0
Condition0%0/3
defined $op and $op =~ /\A(?:create|rename|delete|subscribe|unsubscribe)\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $op and $op =~ /\A(?:create|rename|delete|subscribe|unsubscribe)\z/u
defined $op $op =~ /\A(?:create|rename|delete|subscribe|unsubscribe)\z/u
Truth table0%0/3
A and B
A= defined $opB= $op =~ /\A(?:create|rename|delete|subscribe|unsubscribe)\z/u
ABresult
0X0
100
111
753 or die "Invalid mailbox operation\n";
754  
755 0 ▶ if ($op eq "delete" && !$args->{force}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "delete" and not $args->{"force"}) 0 0 0
Condition0%0/3
$op eq "delete" and not $args->{"force"}
!ll&&!rl&&r
000
MC/DC0%0/2
$op eq "delete" and not $args->{"force"}
$op eq "delete" not $args->{"force"}
Truth table0%0/3
A and not(B)
A= $op eq "delete"B= not $args->{"force"}
ABresult
0X0
100
111
756 0 my $count = checked ($imap->status ($args->{mailbox}, "(MESSAGES)"), "STATUS")->{messages};
757 0 ▶ defined $count && $count =~ /\A[0-9]+\z/
Branch: 1 branch
BranchTrueFalseTotal
defined $count and $count =~ /\A[0-9]+\z/u or die "Server did not report mailbox message count\n" 0 0 0
Condition0%0/3
defined $count and $count =~ /\A[0-9]+\z/u
!ll&&!rl&&r
000
MC/DC0%0/2
defined $count and $count =~ /\A[0-9]+\z/u
defined $count $count =~ /\A[0-9]+\z/u
Truth table0%0/3
A and B
A= defined $countB= $count =~ /\A[0-9]+\z/u
ABresult
0X0
100
111
758 or die "Server did not report mailbox message count\n";
759 0 ▶ $count == 0
Branch: 1 branch
BranchTrueFalseTotal
$count == 0 or die "Mailbox contains $count messages; use force to delete it\n" 0 0 0
760 or die "Mailbox contains $count messages; use force to delete it\n";
761 }
762  
763 0 ▶ my @params = $op eq "rename" ? @{$args}{qw(mailbox destination)} : ($args->{mailbox});
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'rename' ? : 0 0 0
764 0 checked ($imap->$op (@params), uc $op);
765 0 return (1, { ok => JSON::XS::true });
766 }
767  
768 0 die "Unknown IMAP operation\n";
769 };
770 0 my $error = $@;
771 0 ▶ if (!$result && ($cmd eq "connect" || $cmd eq "ping" || !eval { $imap && $imap->is_open })) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $result and $cmd eq "connect" || $cmd eq "ping" || !eval { do { $imap and $imap->is_open } }) 0 0 0
Condition0%0/12
not $result and $cmd eq "connect" || $cmd eq "ping" || !eval { do { $imap and $imap->is_open } }
!ll&&!rl&&r
000
$cmd eq "connect" or $cmd eq "ping" or not eval { do { $imap and $imap->is_open } }
l!l&&r!l&&!r
000
$cmd eq "connect" or $cmd eq "ping"
l!l&&r!l&&!r
000
$imap && $imap->is_open
!ll&&!rl&&r
000
MC/DC0%0/4
not $result and $cmd eq "connect" || $cmd eq "ping" || !eval { do { $imap and $imap->is_open } }
not $result $cmd eq "connect" $cmd eq "ping" not eval { do { $imap and $imap->is_open } }
MC/DC0%0/2
$imap && $imap->is_open
$imap $imap->is_open
Truth table0%0/5
not(A) and B or C or not(D)
A= not $resultB= $cmd eq "connect"C= $cmd eq "ping"D= not eval { do { $imap and $imap->is_open } }
ABCDresult
0XXX0
10000
11XX1
101X1
10011
Truth table0%0/3
A && B
A= $imapB= $imap->is_open
ABresult
0X0
100
111
772 # do not let IMAPTalk's destructor issue a blocking LOGOUT on a dead socket
773 0 ▶ $imap->state (Mail::IMAPTalk::Unconnected) if $imap;
Branch: 1 branch
BranchTrueFalseTotal
if $imap 0 0 0
774 0 undef $imap;
775 }
776 0 ▶ $result ? (1, $result) : (0, $error || "IMAP operation failed\n", $imap ? 1 : 0)
Branch: 3 branches
BranchTrueFalseTotal
$result ? : 0 0 0
$error or "IMAP operation failed\n" 0 0 0
$imap ? : 0 0 0
777 }
778  
779 1 1;
« bin/mcp-webbrowser ↑ Summary bin/mcp-fs »