lib/AnyEvent/MCP/Worker/IMAP.pm

filter: Statementstmt 92.2% 239 / 259 Branchbran 70.0% 154 / 220 Conditioncond 51.4% 107 / 208 MC/DCmcdc 24.1% 27 / 112 Subroutinesub 100% 18 / 18 Podpod 0.0% 0 / 11 totaltotal 65.8% 545 / 828 CC 194 SCAR 42.3
CC
194
Cov
73%
run
115.5
messages
89.1
search_filter
59.5
CRAP 68.8
« bin/mcp-webbrowser ↑ Summary blib/lib/AnyEvent/MCP/Server.pm »
1 package AnyEvent::MCP::Worker::IMAP;
2  
3 3 ▶use common::sense;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Mail::IMAPTalk ();
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use IO::Socket::SSL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use MIME::Base64 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use MIME::QuotedPrint ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use JSON::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # mboxo stores raw messages without reliably escaping From_ lines.
12 # check the original bytes, not decoded MIME parts; never rewrite the message.
13 28 ▶sub mbox_from {
Subroutine mbox_from: called CC=2 SCAR=6.9
Pod undocumented
14 28 my ($bytes) = @_;
15 28 ▶ $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 18 10 28
16 (?: [0-9]{2}
17 | \b(?i:Mon|Tue|Wed|Thu|Fri|Sat|Sun
18 |Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\b
19 )/mx or return;
20  
21 18 1 + ((substr $bytes, 0, $-[0]) =~ tr/\n//)
22 }
23  
24 3 my ($imap, $config);
25  
26 8 ▶sub imap_connect {
Subroutine imap_connect: called CC=13 SCAR=30.8
Pod undocumented
27 8 my ($settings) = @_;
28  
29 8 $config = $settings;
30 8 undef $imap;
31  
32 my %tls = (
33 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER (),
34 SSL_verifycn_name => $config->{server},
35 8 SSL_verifycn_scheme => "http", # DNS names and IP addresses, not a web protocol
36 );
37  
38 8 ▶ if ($config->{tls} eq "ssl") {
Branch: 1 branch
BranchTrueFalseTotal
if ($config->{'tls'} eq 'ssl') { } 8 0 8
39 $imap = Mail::IMAPTalk->new (
40 Server => $config->{server}, Port => $config->{port},
41 8 UseSSL => 1, UseBlocking => 1, PreserveINBOX => 1, %tls,
42 );
43 } else {
44 $imap = Mail::IMAPTalk->new (
45 0 Server => $config->{server}, Port => $config->{port}, PreserveINBOX => 1,
46 );
47  
48 0 ▶ $imap or die "IMAP connection failed: $@\n";
Branch: 1 branch
BranchTrueFalseTotal
$imap or die "IMAP connection failed: $@\n" 0 0 0
49 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
50 or die "IMAP STARTTLS rejected: " . $imap->get_last_error . "\n";
51 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
52 or die "IMAP TLS handshake failed: " . IO::Socket::SSL::errstr () . "\n";
53 0 $imap->{UseBlocking} = 1;
54 0 delete $imap->{Cache}{capability}; # TLS changes advertised mechanisms
55 }
56  
57 8 ▶ $imap or die "IMAP connection failed: $@\n";
Branch: 1 branch
BranchTrueFalseTotal
$imap or die "IMAP connection failed: $@\n" 8 0 8
58 8 ▶ if (($config->{method} || "password") eq "oauth2") {
Branch: 1 branch
BranchTrueFalseTotal
if (($config->{'method'} || 'password') eq 'oauth2') { } 2 6 8
Condition100%2/2
$config->{'method'} || 'password'
l!l
26
MC/DC100%1/1
$config->{'method'} || 'password'
$config->{'method'}
Truth table100%2/2
A
A= $config->{'method'}
Aresult
11
00
59 2 require AnyEvent::MCP::IMAP::OAuth2;
60 2 require Authen::SASL;
61 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
62 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
63 or die "IMAP server does not advertise AUTH=$mechanism\n";
64 my $token = $config->{access_token}
65 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
66 my $sasl = Authen::SASL->new (
67 mechanism => $mechanism,
68 2 callback => { user => $config->{user}, pass => $token },
69 );
70 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
71 or die "IMAP OAuth authentication failed: " . $imap->get_last_error . "\n";
72 } else {
73 $imap->login ($config->{user}, $config->{password})
74 6 ▶ or die "IMAP authentication failed: " . $imap->get_last_error . "\n";
Branch: 1 branch
BranchTrueFalseTotal
$imap->login($config->{"user"}, $config->{"password"}) or die 'IMAP authentication failed: ' . $imap->get_last_error . "\n" 6 0 6
75 }
76  
77 8 $imap->uid (1);
78 8 $imap->parse_mode (DecodeUTF8 => 1);
79  
80 8 +{ connected => JSON::XS::true, capabilities => [sort keys %{$imap->capability}] }
81 }
82  
83 94 ▶sub checked ($$) {
Subroutine checked: called CC=5 SCAR=23.8
Pod undocumented
84 94 my ($result, $what) = @_;
85  
86 94 ▶ defined $result && $result
Branch: 1 branch
BranchTrueFalseTotal
defined $result and $result or die "${what}: " . ($imap->get_last_error || $@ || "IMAP command failed") . "\n" 94 0 94
Condition12%1/8
defined $result and $result
!ll&&!rl&&r
0094
$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 table33%1/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
87 or die "$what: " . ($imap->get_last_error || $@ || "IMAP command failed") . "\n";
88  
89 94 $result
90 }
91  
92 30 ▶sub mailbox {
Subroutine mailbox: called CC=2 SCAR=7.0
Pod undocumented
93 30 my ($name, $writable) = @_;
94  
95 30 checked ($imap->select ($name, ReadOnly => !$writable), "Select $name");
96 30 my $validity = $imap->get_response_code ("uidvalidity");
97 30 ▶ defined $validity or die "UIDVALIDITY not supplied for $name\n";
Branch: 1 branch
BranchTrueFalseTotal
defined $validity or die "UIDVALIDITY not supplied for $name\n" 30 0 30
98  
99 30 $validity
100 }
101  
102 20 ▶sub same_validity {
Subroutine same_validity: called CC=3 SCAR=12.0
Pod undocumented
103 20 my ($args, $writable) = @_;
104  
105 20 my $validity = mailbox ($args->{mailbox}, $writable);
106 20 ▶ 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" 20 0 20
Condition33%1/3
defined $args->{"uidvalidity"} and qq[$args->{"uidvalidity"}] eq "$validity"
!ll&&!rl&&r
0020
MC/DC0%0/2
defined $args->{"uidvalidity"} and qq[$args->{"uidvalidity"}] eq "$validity"
defined $args->{"uidvalidity"} qq[$args->{"uidvalidity"}] eq "$validity"
Truth table33%1/3
A and B
A= defined $args->{"uidvalidity"}B= qq[$args->{"uidvalidity"}] eq "$validity"
ABresult
0X0
100
111
107 or die "UIDVALIDITY mismatch (current $validity); refresh message references\n";
108  
109 20 $validity
110 }
111  
112 10 ▶sub uids {
Subroutine uids: called CC=8 SCAR=24.9
Pod undocumented
113 10 my ($args) = @_;
114  
115 10 ▶ ref $args->{uids} eq "ARRAY" && @{$args->{uids}} && @{$args->{uids}} <= 100
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{"uids"} eq "ARRAY" and @{$args->{"uids"};} and @{$args->{"uids"};} <= 100 or die "uids must contain 1..100 message UIDs\n" 10 0 10
Condition33%2/6
ref $args->{"uids"} eq "ARRAY" and @{$args->{"uids"};} and @{$args->{"uids"};} <= 100
!ll&&!rl&&r
0010
ref $args->{"uids"} eq "ARRAY" and @{$args->{"uids"};}
!ll&&!rl&&r
0010
MC/DC0%0/3
ref $args->{"uids"} eq "ARRAY" and @{$args->{"uids"};} and @{$args->{"uids"};} <= 100
ref $args->{"uids"} eq "ARRAY" @{$args->{"uids"};} @{$args->{"uids"};} <= 100
Truth table25%1/4
A and B and C
A= ref $args->{"uids"} eq "ARRAY"B= @{$args->{"uids"};}C= @{$args->{"uids"};} <= 100
ABCresult
0XX0
10X0
1100
1111
116 or die "uids must contain 1..100 message UIDs\n";
117 10 for (@{$args->{uids}}) {
118 10 ▶ 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" 10 0 10
Condition33%2/6
defined $_ and not ref $_ and /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
0010
defined $_ and not ref $_
!ll&&!rl&&r
0010
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 table25%1/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
119 or die "Invalid UID\n";
120 }
121  
122 10 join ",", @{$args->{uids}}
123 }
124  
125 2 ▶sub message {
Subroutine message: called CC=2 SCAR=7.0
Pod undocumented
126 2 my ($args) = @_;
127  
128 2 my $validity = same_validity ($args, 0);
129 2 my $uid = $args->{uid};
130 2 ▶ 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" 2 0 2
131 or die "Message $uid vanished\n";
132  
133 2 +{ uid => 0 + $uid, uidvalidity => $validity, %$f }
134 }
135  
136 84 ▶sub search_filter {
Subroutine search_filter: called CC=44 SCAR=40.9
Pod undocumented
137 84 my ($filter, $depth, $nodes) = @_;
138  
139 84 ▶ 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" 80 4 84
Condition83%5/6
ref $filter eq "HASH" and !$depth || keys %$filter
!ll&&!rl&&r
0480
not $depth or keys %$filter
l!l&&r!l&&!r
32484
MC/DC66%2/3
ref $filter eq "HASH" and !$depth || keys %$filter
ref $filter eq "HASH" not $depth keys %$filter
Truth table75%3/4
A and not(B) or C
A= ref $filter eq "HASH"B= not $depthC= keys %$filter
ABCresult
0XX0
1000
11X1
1011
140 or die "Invalid SEARCH filter group\n";
141 80 ▶ ++$$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" 78 2 80
Condition66%2/3
++$$nodes <= 100 and $depth <= 12
!ll&&!rl&&r
0278
MC/DC50%1/2
++$$nodes <= 100 and $depth <= 12
++$$nodes <= 100 $depth <= 12
Truth table66%2/3
A and B
A= ++$$nodes <= 100B= $depth <= 12
ABresult
0X0
100
111
142  
143 78 my @criteria;
144 78 for my $key (sort keys %$filter) {
145 88 my $value = $filter->{$key};
146  
147 88 ▶ if ($key eq "all" || $key eq "any") {
Branch: 7 branches
BranchTrueFalseTotal
if ($key eq 'all' or $key eq 'any') { } 12 76 88
elsif ($key eq 'not') { } 32 44 76
elsif ($key eq 'header') { } 6 38 44
elsif ($key =~ /\A(?:from|to|cc|bcc|subject|body|text)\z/u) { } 18 20 38
elsif ($key =~ /\A(?:since|before|on|sent_since|sent_before|sent_on)\z/u) { } 6 14 20
elsif ($key eq 'larger' or $key eq 'smaller') { } 4 10 14
elsif ($key =~ /\A(?:seen|unseen|answered|unanswered|flagged|unflagged|deleted|undeleted|draft|undraft|recent|old|new)\z/u) { } 8 2 10
Condition100%3/3
$key eq "all" or $key eq "any"
l!l&&r!l&&!r
4876
MC/DC100%2/2
$key eq "all" or $key eq "any"
$key eq "all" $key eq "any"
Truth table100%3/3
A or B
A= $key eq "all"B= $key eq "any"
ABresult
1X1
011
000
148 12 ▶ 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" 10 2 12
Condition66%4/6
ref $value eq "ARRAY" and @$value and @$value <= 100
!ll&&!rl&&r
2010
ref $value eq "ARRAY" and @$value
!ll&&!rl&&r
0210
MC/DC33%1/3
ref $value eq "ARRAY" and @$value and @$value <= 100
ref $value eq "ARRAY" @$value @$value <= 100
Truth table50%2/4
A and B and C
A= ref $value eq "ARRAY"B= @$valueC= @$value <= 100
ABCresult
0XX0
10X0
1100
1111
149 or die "SEARCH $key requires a nonempty array of filters\n";
150 10 my @groups = map [search_filter ($_, $depth + 1, $nodes)], @$value;
151 6 ▶ if ($key eq "all") {
Branch: 1 branch
BranchTrueFalseTotal
if ($key eq 'all') { } 2 4 6
152 2 push @criteria, @groups;
153 } else {
154 4 my $or = pop @groups;
155 4 ▶ $or = ["OR", pop @groups, $or] while @groups;
Branch: 1 branch
BranchTrueFalseTotal
@groups and do { $or = ["OR", pop @groups, $or] } 6 4 10
156 4 push @criteria, @$or;
157 }
158 } elsif ($key eq "not") {
159 32 push @criteria, "NOT", [search_filter ($value, $depth + 1, $nodes)];
160 } elsif ($key eq "header") {
161 6 ▶ 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" 6 0 6
Condition33%2/6
ref $value eq "HASH" and keys %$value and keys %$value <= 20
!ll&&!rl&&r
006
ref $value eq "HASH" and keys %$value
!ll&&!rl&&r
006
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 table25%1/4
A and B and C
A= ref $value eq "HASH"B= keys %$valueC= keys %$value <= 20
ABCresult
0XX0
10X0
1100
1111
162 or die "SEARCH header requires 1..20 header names\n";
163 6 for my $name (sort keys %$value) {
164 6 ▶ $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" 4 2 6
165 or die "Invalid SEARCH header name\n";
166 4 my $text = $value->{$name};
167 4 ▶ 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" 2 2 4
Condition66%4/6
defined $text and not ref $text and length $text <= 500
!ll&&!rl&&r
202
defined $text and not ref $text
!ll&&!rl&&r
022
MC/DC33%1/3
defined $text and not ref $text and length $text <= 500
defined $text not ref $text length $text <= 500
Truth table50%2/4
A and not(B) and C
A= defined $textB= not ref $textC= length $text <= 500
ABCresult
0XX0
10X0
1100
1111
168 or die "Invalid SEARCH header value\n";
169 2 push @criteria, "HEADER", $name, { Quote => $text };
170 }
171 } elsif ($key =~ /\A(?:from|to|cc|bcc|subject|body|text)\z/) {
172 18 ▶ 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" 18 0 18
Condition33%2/6
defined $value and not ref $value and length $value <= 500
!ll&&!rl&&r
0018
defined $value and not ref $value
!ll&&!rl&&r
0018
MC/DC0%0/3
defined $value and not ref $value and length $value <= 500
defined $value not ref $value length $value <= 500
Truth table25%1/4
A and not(B) and C
A= defined $valueB= not ref $valueC= length $value <= 500
ABCresult
0XX0
10X0
1100
1111
173 or die "Invalid SEARCH $key value\n";
174 18 push @criteria, uc $key, { Quote => $value };
175 } elsif ($key =~ /\A(?:since|before|on|sent_since|sent_before|sent_on)\z/) {
176 6 ▶ 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" 4 2 6
Condition50%3/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
024
defined $value and not ref $value
!ll&&!rl&&r
006
MC/DC33%1/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 table50%2/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
177 or die "Invalid SEARCH $key date (D-Mon-YYYY)\n";
178 4 (my $command = uc $key) =~ tr/_//d;
179 4 push @criteria, $command, $value;
180 ▶ } elsif ($key eq "larger" || $key eq "smaller") {
Condition66%2/3
$key eq "larger" or $key eq "smaller"
l!l&&r!l&&!r
4010
MC/DC50%1/2
$key eq "larger" or $key eq "smaller"
$key eq "larger" $key eq "smaller"
Truth table66%2/3
A or B
A= $key eq "larger"B= $key eq "smaller"
ABresult
1X1
011
000
181 4 ▶ 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" 2 2 4
Condition55%5/9
defined $value and not ref $value and $value =~ /\A[0-9]{1,10}\z/u and $value <= 4294967295
!ll&&!rl&&r
202
defined $value and not ref $value and $value =~ /\A[0-9]{1,10}\z/u
!ll&&!rl&&r
022
defined $value and not ref $value
!ll&&!rl&&r
004
MC/DC25%1/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 table40%2/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
182 or die "Invalid SEARCH $key size\n";
183 2 push @criteria, uc $key, $value;
184 } elsif ($key =~ /\A(?:seen|unseen|answered|unanswered|flagged|unflagged|deleted|undeleted|draft|undraft|recent|old|new)\z/) {
185 8 ▶ 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" 6 2 8
Condition66%2/3
defined $value and "$value" =~ /\A[01]\z/u
!ll&&!rl&&r
026
MC/DC50%1/2
defined $value and "$value" =~ /\A[01]\z/u
defined $value "$value" =~ /\A[01]\z/u
Truth table66%2/3
A and B
A= defined $valueB= "$value" =~ /\A[01]\z/u
ABresult
0X0
100
111
186 or die "Invalid SEARCH $key boolean\n";
187 6 ▶ push @criteria, uc $key if $value;
Branch: 1 branch
BranchTrueFalseTotal
if $value 4 2 6
188 } else {
189 2 die "Unknown SEARCH filter: $key\n";
190 }
191 }
192  
193 32 ▶ @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" 32 0 32
Condition66%2/3
@criteria or not $depth
l!l&&r!l&&!r
2660
MC/DC0%0/2
@criteria or not $depth
@criteria not $depth
Truth table66%2/3
A or not(B)
A= @criteriaB= not $depth
ABresult
1X1
011
000
194 @criteria
195 32 }
196  
197 10 ▶sub messages {
Subroutine messages: called CC=50 SCAR=44.9
Pod undocumented
198 10 my ($args) = @_;
199  
200 10 my $validity = mailbox ($args->{mailbox}, 0);
201 10 ▶ my $limit = $args->{limit} || 25;
Condition100%2/2
$args->{'limit'} || 25
l!l
82
MC/DC100%1/1
$args->{'limit'} || 25
$args->{'limit'}
Truth table100%2/2
A
A= $args->{'limit'}
Aresult
11
00
202 10 ▶ $limit =~ /\A[1-9][0-9]*\z/ && $limit <= 100 or die "limit must be 1..100\n";
Branch: 1 branch
BranchTrueFalseTotal
$limit =~ /\A[1-9][0-9]*\z/u and $limit <= 100 or die "limit must be 1..100\n" 10 0 10
Condition33%1/3
$limit =~ /\A[1-9][0-9]*\z/u and $limit <= 100
!ll&&!rl&&r
0010
MC/DC0%0/2
$limit =~ /\A[1-9][0-9]*\z/u and $limit <= 100
$limit =~ /\A[1-9][0-9]*\z/u $limit <= 100
Truth table33%1/3
A and B
A= $limit =~ /\A[1-9][0-9]*\z/uB= $limit <= 100
ABresult
0X0
100
111
203  
204 10 my $cursor = $args->{cursor};
205 10 ▶ if (defined $cursor) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $cursor) 4 6 10
206 ref $cursor eq "HASH" && defined $cursor->{uidvalidity}
207 && "$cursor->{uidvalidity}" eq "$validity"
208 4 ▶ && 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" 2 2 4
Condition58%7/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
202
ref $cursor eq "HASH" and defined $cursor->{"uidvalidity"} and qq[$cursor->{"uidvalidity"}] eq "$validity" and defined $cursor->{"before_uid"}
!ll&&!rl&&r
202
ref $cursor eq "HASH" and defined $cursor->{"uidvalidity"} and qq[$cursor->{"uidvalidity"}] eq "$validity"
!ll&&!rl&&r
022
ref $cursor eq "HASH" and defined $cursor->{"uidvalidity"}
!ll&&!rl&&r
004
MC/DC20%1/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 table33%2/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
209 or die "Stale or invalid UID cursor\n";
210 }
211  
212 8 my $nodes = 0;
213 8 ▶ my @criteria = search_filter ($args->{filter} // {}, 0, \$nodes);
Condition100%2/2
$args->{"filter"} // {}
l!l
44
MC/DC100%1/1
$args->{"filter"} // {}
$args->{"filter"}
Truth table100%2/2
A
A= $args->{"filter"}
Aresult
11
00
214  
215 8 ▶ my @fields = @{$args->{fields} || [qw(subject from date flags size)]};
Condition100%2/2
$args->{'fields'} || ['subject', 'from', 'date', 'flags', 'size']
l!l
26
MC/DC100%1/1
$args->{'fields'} || ['subject', 'from', 'date', 'flags', 'size']
$args->{'fields'}
Truth table100%2/2
A
A= $args->{'fields'}
Aresult
11
00
216 8 my %valid = map +($_ => 1), qw(uid from to subject date cc reply-to message-id flags size internaldate);
217 8 ▶ $valid{$_} or die "Invalid summary field: $_\n" for @fields;
Branch: 1 branch
BranchTrueFalseTotal
$valid{$_} or die "Invalid summary field: $_\n" 34 0 34
218 8 ▶ my @headers = @{$args->{headers} || []};
Condition50%1/2
$args->{'headers'} || []
l!l
08
MC/DC0%0/1
$args->{'headers'} || []
$args->{'headers'}
Truth table50%1/2
A
A= $args->{'headers'}
Aresult
11
00
219 8 ▶ @headers <= 20 or die "Too many headers\n";
Branch: 1 branch
BranchTrueFalseTotal
@headers <= 20 or die "Too many headers\n" 8 0 8
220 8 for (@headers) {
221 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
222 }
223  
224 # search in descending UID windows: ordinary UID SEARCH can otherwise return
225 # millions of matches even when only one summary page was requested.
226 # Mail::IMAPTalk caches SELECT, so UIDNEXT from that SELECT becomes stale.
227 8 my $status = checked ($imap->status ($args->{mailbox}, "(UIDNEXT UIDVALIDITY)"), "STATUS");
228 8 ▶ "$status->{uidvalidity}" eq "$validity"
Branch: 1 branch
BranchTrueFalseTotal
qq[$status->{"uidvalidity"}] eq "$validity" or die "Mailbox UIDVALIDITY changed; restart mcp-imap\n" 8 0 8
229 or die "Mailbox UIDVALIDITY changed; restart mcp-imap\n";
230 8 my $uidnext = $status->{uidnext};
231 8 ▶ 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" 8 0 8
Condition33%1/3
defined $uidnext and $uidnext =~ /\A[1-9][0-9]*\z/u
!ll&&!rl&&r
008
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 table33%1/3
A and B
A= defined $uidnextB= $uidnext =~ /\A[1-9][0-9]*\z/u
ABresult
0X0
100
111
232 or die "Server did not provide UIDNEXT\n";
233 8 ▶ my $hi = $cursor ? $cursor->{before_uid} - 1 : $uidnext - 1;
Branch: 1 branch
BranchTrueFalseTotal
$cursor ? : 2 6 8
234 8 my @found;
235 8 my $scanned = 0;
236 8 ▶ while ($hi > 0 && $scanned < 20000 && @found <= $limit) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hi > 0 and $scanned < 20000 and @found <= $limit) 16 8 24
Condition83%5/6
$hi > 0 and $scanned < 20000 and @found <= $limit
!ll&&!rl&&r
2616
$hi > 0 and $scanned < 20000
!ll&&!rl&&r
2022
MC/DC66%2/3
$hi > 0 and $scanned < 20000 and @found <= $limit
$hi > 0 $scanned < 20000 @found <= $limit
Truth table75%3/4
A and B and C
A= $hi > 0B= $scanned < 20000C= @found <= $limit
ABCresult
0XX0
10X0
1100
1111
237 16 ▶ my $lo = $hi > 999 ? $hi - 999 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$hi > 999 ? : 14 2 16
238 16 my $matches = checked ($imap->search ("$lo:$hi", @criteria), "UID SEARCH");
239 16 push @found, sort { $b <=> $a } @$matches;
240 16 $scanned += $hi - $lo + 1;
241 16 $hi = $lo - 1;
242 }
243  
244 8 my $more = @found > $limit;
245 8 ▶ splice @found, $limit if $more;
Branch: 1 branch
BranchTrueFalseTotal
if $more 6 2 8
246 8 ▶ my $incomplete = $hi > 0 && !$more;
Condition66%2/3
$hi > 0 && !$more
!ll&&!rl&&r
260
MC/DC0%0/2
$hi > 0 && !$more
$hi > 0 !$more
Truth table66%2/3
A && not(B)
A= $hi > 0B= !$more
ABresult
0X0
100
111
247 8 my %selected = map +($_ => 1), @fields;
248 8 my @items = ("uid");
249 8 ▶ 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") 8 0 8
250 8 ▶ push @items, "flags" if $selected{flags};
Branch: 1 branch
BranchTrueFalseTotal
if $selected{"flags"} 8 0 8
251 8 ▶ push @items, "rfc822.size" if $selected{size};
Branch: 1 branch
BranchTrueFalseTotal
if $selected{"size"} 6 2 8
252 8 ▶ push @items, "internaldate" if $selected{internaldate};
Branch: 1 branch
BranchTrueFalseTotal
if $selected{"internaldate"} 0 8 8
253 8 ▶ if (@headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@headers) 0 8 8
254 0 push @items, "body.peek[header.fields (" . (join " ", @headers) . ")]";
255 }
256  
257 8 ▶ my $data = @found
Branch: 1 branch
BranchTrueFalseTotal
@found ? : 8 0 8
258 ? checked ($imap->fetch (\@found, "(" . join (" ", @items) . ")"), "Fetch summaries")
259 : {};
260 8 my %env = (
261 (map +($_ => ucfirst $_), qw(from to subject date cc)),
262 "reply-to" => "Reply-To",
263 "message-id" => "Message-ID",
264 );
265 8 my @messages;
266 8 for my $uid (@found) {
267 14 ▶ my $item = $data->{$uid} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $item = $data->{$uid} or next 14 0 14
268 14 my %row = (uid => 0 + $uid);
269 14 for my $field (@fields) {
270 58 ▶ next if $field eq "uid";
Branch: 1 branch
BranchTrueFalseTotal
if $field eq "uid" 0 58 58
271 58 my $key = $env{$field};
272 $row{$field} = $key ? $item->{envelope}{$key}
273 58 ▶ : $field eq "size" ? $item->{"rfc822.size"} : $item->{$field};
Branch: 2 branches
BranchTrueFalseTotal
$key ? : 34 24 58
$field eq 'size' ? : 10 14 24
274 }
275 14 ▶ if (@headers) {
Branch: 1 branch
BranchTrueFalseTotal
if (@headers) 0 14 14
276 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
277 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
278 or die "Selected headers exceed 64 KiB\n";
279 }
280 14 push @messages, \%row;
281 }
282  
283 8 ▶ my $before = $incomplete ? $hi + 1 : @found ? $found[-1] : undef;
Branch: 2 branches
BranchTrueFalseTotal
$incomplete ? : 0 8 8
@found ? : 8 0 8
284  
285 +{
286 8 ▶ uidvalidity => $validity,
Branch: 3 branches
BranchTrueFalseTotal
$more ? : 6 2 8
$incomplete ? : 0 8 8
($more || $incomplete) && $before ? : 6 2 8
Condition66%4/6
$more || $incomplete and $before
!ll&&!rl&&r
206
$more or $incomplete
l!l&&r!l&&!r
602
MC/DC33%1/3
$more || $incomplete and $before
$more $incomplete $before
Truth table40%2/5
A or B and C
A= $moreB= $incompleteC= $before
ABCresult
00X0
1X00
0100
1X11
0111
287 messages => \@messages,
288 more => $more ? JSON::XS::true : JSON::XS::false,
289 incomplete => $incomplete ? JSON::XS::true : JSON::XS::false,
290 next_cursor => ($more || $incomplete) && $before
291 ? { uidvalidity => $validity, before_uid => $before } : undef,
292 }
293 }
294  
295 4 ▶sub part {
Subroutine part: called CC=25 SCAR=38.7
Pod undocumented
296 4 my ($args, $raw_message) = @_;
297  
298 4 my $validity = same_validity ($args, 0);
299 4 my $uid = $args->{uid};
300 4 ▶ my $part = $raw_message ? "" : $args->{part};
Branch: 1 branch
BranchTrueFalseTotal
$raw_message ? : 2 2 4
301 4 ▶ 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" 4 0 4
Condition33%1/3
defined $part and $part =~ /\A(?:[1-9][0-9]*(?:\.[1-9][0-9]*)*)?\z/u
!ll&&!rl&&r
004
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 table33%1/3
A and B
A= defined $partB= $part =~ /\A(?:[1-9][0-9]*(?:\.[1-9][0-9]*)*)?\z/u
ABresult
0X0
100
111
302 or die "Invalid MIME part number\n";
303 4 ▶ my $max = $args->{max_bytes} || 65536;
Condition50%1/2
$args->{'max_bytes'} || 65536
l!l
04
MC/DC0%0/1
$args->{'max_bytes'} || 65536
$args->{'max_bytes'}
Truth table50%1/2
A
A= $args->{'max_bytes'}
Aresult
11
00
304 4 ▶ $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" 4 0 4
Condition33%1/3
$max =~ /\A[1-9][0-9]*\z/u and $max <= 262144
!ll&&!rl&&r
004
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 table33%1/3
A and B
A= $max =~ /\A[1-9][0-9]*\z/uB= $max <= 262144
ABresult
0X0
100
111
305 or die "max_bytes must be 1..262144\n";
306  
307 4 my $f = checked ($imap->fetch ($uid, "body.peek[$part]<0." . ($max + 1) . ">"), "Fetch part $uid");
308 4 ▶ my $item = $f->{$uid} or die "Message $uid vanished\n";
Branch: 1 branch
BranchTrueFalseTotal
my $item = $f->{$uid} or die "Message $uid vanished\n" 4 0 4
309 4 my $bytes = $item->{body};
310 4 ▶ 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" 4 0 4
Condition33%1/3
defined $bytes and not ref $bytes
!ll&&!rl&&r
004
MC/DC0%0/2
defined $bytes and not ref $bytes
defined $bytes not ref $bytes
Truth table33%1/3
A and not(B)
A= defined $bytesB= not ref $bytes
ABresult
0X0
100
111
311 4 my $truncated = length $bytes > $max;
312 4 ▶ substr $bytes, $max, "" if $truncated;
Branch: 1 branch
BranchTrueFalseTotal
if $truncated 0 4 4
313 4 ▶ my $result = {
Branch: 1 branch
BranchTrueFalseTotal
$truncated ? : 0 4 4
314 uid => 0 + $uid, uidvalidity => $validity, part => $part,
315 data_base64 => MIME::Base64::encode_base64 ($bytes, ""),
316 truncated => $truncated ? JSON::XS::true : JSON::XS::false,
317 };
318  
319 4 ▶ if ($args->{decode_text} && !$truncated && !$raw_message) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"decode_text"} and not $truncated and not $raw_message) 2 2 4
Condition66%4/6
$args->{"decode_text"} and not $truncated and not $raw_message
!ll&&!rl&&r
202
$args->{"decode_text"} and not $truncated
!ll&&!rl&&r
202
MC/DC33%1/3
$args->{"decode_text"} and not $truncated and not $raw_message
$args->{"decode_text"} not $truncated not $raw_message
Truth table50%2/4
A and not(B) and not(C)
A= $args->{"decode_text"}B= not $truncatedC= not $raw_message
ABCresult
0XX0
10X0
1100
1111
320 2 my $bs = checked ($imap->fetch ($uid, "bodystructure"), "Fetch MIME structure")->{$uid}{bodystructure};
321 2 my @pending = ($bs);
322 2 my $node;
323 2 ▶ while (@pending) {
Branch: 1 branch
BranchTrueFalseTotal
if (@pending) 2 0 2
324 2 my $candidate = shift @pending;
325 2 ▶ 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") 2 0 2
Condition40%2/5
($candidate->{"IMAP-Partnum"} // "") eq $part and $candidate->{"MIME-Type"} eq "text"
!ll&&!rl&&r
002
$candidate->{"IMAP-Partnum"} // ""
l!l
20
MC/DC0%0/2
($candidate->{"IMAP-Partnum"} // "") eq $part and $candidate->{"MIME-Type"} eq "text"
$candidate->{"IMAP-Partnum"} $candidate->{"MIME-Type"} eq "text"
Truth table33%1/3
A and B
A= $candidate->{"IMAP-Partnum"}B= $candidate->{"MIME-Type"} eq "text"
ABresult
0X0
100
111
326 2 $node = $candidate;
327 2 last;
328 }
329 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
330 }
331 2 ▶ $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" 2 0 2
332 2 ▶ my $encoding = uc ($node->{"Content-Transfer-Encoding"} || "");
Condition50%1/2
$node->{'Content-Transfer-Encoding'} || ''
l!l
20
MC/DC0%0/1
$node->{'Content-Transfer-Encoding'} || ''
$node->{'Content-Transfer-Encoding'}
Truth table50%1/2
A
A= $node->{'Content-Transfer-Encoding'}
Aresult
11
00
333 2 ▶ $bytes = MIME::Base64::decode_base64 ($bytes) if $encoding eq "BASE64";
Branch: 1 branch
BranchTrueFalseTotal
if $encoding eq "BASE64" 0 2 2
334 2 ▶ $bytes = MIME::QuotedPrint::decode_qp ($bytes) if $encoding eq "QUOTED-PRINTABLE";
Branch: 1 branch
BranchTrueFalseTotal
if $encoding eq "QUOTED-PRINTABLE" 2 0 2
335 2 ▶ my $charset = $node->{"Content-Type"}{charset} || "us-ascii";
Condition50%1/2
$node->{'Content-Type'}{'charset'} || 'us-ascii'
l!l
20
MC/DC0%0/1
$node->{'Content-Type'}{'charset'} || 'us-ascii'
$node->{'Content-Type'}{'charset'}
Truth table50%1/2
A
A= $node->{'Content-Type'}{'charset'}
Aresult
11
00
336 2 $result->{text} = Encode::decode ($charset, $bytes, Encode::FB_CROAK ());
337 2 $result->{content_type} = $node->{"MIME-TxtType"};
338 }
339  
340 $result
341 4 }
342  
343 74 ▶sub run {
Subroutine run: called CC=50 SCAR=47.5
Pod undocumented
344 74 my ($cmd, $args) = @_;
345  
346 74 my $result = eval {
347 74 ▶ if ($cmd eq "connect") {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd eq "connect") 6 68 74
348 6 return (1, imap_connect ($args));
349 }
350  
351 68 ▶ $imap && $imap->is_open or die "IMAP connection closed; restart mcp-imap\n";
Branch: 1 branch
BranchTrueFalseTotal
$imap and $imap->is_open or die "IMAP connection closed; restart mcp-imap\n" 68 0 68
Condition33%1/3
$imap and $imap->is_open
!ll&&!rl&&r
0068
MC/DC0%0/2
$imap and $imap->is_open
$imap $imap->is_open
Truth table33%1/3
A and B
A= $imapB= $imap->is_open
ABresult
0X0
100
111
352  
353 68 ▶ if ($cmd eq "mailboxes" || $cmd eq "lsub") {
Branch: 9 branches
BranchTrueFalseTotal
if ($cmd eq 'mailboxes' or $cmd eq 'lsub') { } 4 64 68
elsif ($cmd eq 'messages') { } 10 54 64
elsif ($cmd eq 'message') { } 2 52 54
elsif ($cmd eq 'part' or $cmd eq 'raw') { } 4 48 52
elsif ($cmd eq 'flags') { } 4 44 48
elsif ($cmd eq 'copy' or $cmd eq 'move') { } 4 40 44
elsif ($cmd eq 'append') { } 32 8 40
elsif ($cmd eq 'expunge') { } 6 2 8
elsif ($cmd eq 'mailbox') { } 2 0 2
Condition100%3/3
$cmd eq "mailboxes" or $cmd eq "lsub"
l!l&&r!l&&!r
2264
MC/DC100%2/2
$cmd eq "mailboxes" or $cmd eq "lsub"
$cmd eq "mailboxes" $cmd eq "lsub"
Truth table100%3/3
A or B
A= $cmd eq "mailboxes"B= $cmd eq "lsub"
ABresult
1X1
011
000
354 4 ▶ my $method = $cmd eq "lsub" ? "lsub" : "list";
Branch: 1 branch
BranchTrueFalseTotal
$cmd eq 'lsub' ? : 2 2 4
355 4 return (1, { mailboxes => checked ($imap->$method ("", "*"), uc $method) });
356 } elsif ($cmd eq "messages") {
357 10 return (1, messages ($args));
358 } elsif ($cmd eq "message") {
359 2 return (1, message ($args));
360 ▶ } elsif ($cmd eq "part" || $cmd eq "raw") {
Condition100%3/3
$cmd eq "part" or $cmd eq "raw"
l!l&&r!l&&!r
2248
MC/DC100%2/2
$cmd eq "part" or $cmd eq "raw"
$cmd eq "part" $cmd eq "raw"
Truth table100%3/3
A or B
A= $cmd eq "part"B= $cmd eq "raw"
ABresult
1X1
011
000
361 4 return (1, part ($args, $cmd eq "raw"));
362 } elsif ($cmd eq "flags") {
363 4 my $validity = same_validity ($args, 1);
364 4 my $uidset = uids ($args);
365 4 ▶ $args->{operation} =~ /\A(?:add|remove)\z/
Branch: 1 branch
BranchTrueFalseTotal
$args->{"operation"} =~ /\A(?:add|remove)\z/u or die "Invalid flag operation\n" 2 2 4
366 or die "Invalid flag operation\n";
367 2 my $flags = $args->{flags};
368 2 ▶ 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" 2 0 2
Condition33%2/6
ref $flags eq "ARRAY" and @$flags and @$flags <= 20
!ll&&!rl&&r
002
ref $flags eq "ARRAY" and @$flags
!ll&&!rl&&r
002
MC/DC0%0/3
ref $flags eq "ARRAY" and @$flags and @$flags <= 20
ref $flags eq "ARRAY" @$flags @$flags <= 20
Truth table25%1/4
A and B and C
A= ref $flags eq "ARRAY"B= @$flagsC= @$flags <= 20
ABCresult
0XX0
10X0
1100
1111
369 or die "flags must be an array of 1..20 flags\n";
370 2 for (@$flags) {
371 2 ▶ /\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" 2 0 2
372 or die "Invalid IMAP flag\n";
373 }
374 2 ▶ checked ($imap->store ($uidset, ($args->{operation} eq "add" ? "+" : "-") . "flags.silent",
Branch: 1 branch
BranchTrueFalseTotal
$args->{'operation'} eq 'add' ? : 2 0 2
375 "(" . join (" ", @$flags) . ")"), "STORE");
376 2 return (1, { ok => JSON::XS::true, uidvalidity => $validity, uids => $args->{uids} });
377 ▶ } elsif ($cmd eq "copy" || $cmd eq "move") {
Condition66%2/3
$cmd eq "copy" or $cmd eq "move"
l!l&&r!l&&!r
0440
MC/DC50%1/2
$cmd eq "copy" or $cmd eq "move"
$cmd eq "copy" $cmd eq "move"
Truth table66%2/3
A or B
A= $cmd eq "copy"B= $cmd eq "move"
ABresult
1X1
011
000
378 4 my $validity = same_validity ($args, 1);
379 4 my $uidset = uids ($args);
380 $cmd ne "move" || $imap->capability->{move}
381 4 ▶ 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" 2 2 4
Condition66%2/3
$cmd ne "move" or $imap->capability->{"move"}
l!l&&r!l&&!r
022
MC/DC50%1/2
$cmd ne "move" or $imap->capability->{"move"}
$cmd ne "move" $imap->capability->{"move"}
Truth table66%2/3
A or B
A= $cmd ne "move"B= $imap->capability->{"move"}
ABresult
1X1
011
000
382 2 checked ($imap->$cmd ($uidset, $args->{destination}), uc $cmd);
383 2 return (1, { ok => JSON::XS::true, uidvalidity => $validity, uids => $args->{uids} });
384 } elsif ($cmd eq "append") {
385 defined $args->{data_base64} && !ref $args->{data_base64}
386 && length $args->{data_base64} <= 1398112
387 32 ▶ && $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" 30 2 32
Condition44%4/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
0230
defined $args->{"data_base64"} and not ref $args->{"data_base64"} and length $args->{"data_base64"} <= 1398112
!ll&&!rl&&r
0032
defined $args->{"data_base64"} and not ref $args->{"data_base64"}
!ll&&!rl&&r
0032
MC/DC25%1/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 table40%2/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
388 or die "Invalid or oversized Base64 message\n";
389 30 my $bytes = MIME::Base64::decode_base64 ($args->{data_base64});
390 30 ▶ 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" 30 0 30
391 30 ▶ $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" 30 0 30
392  
393 30 ▶ unless ($config->{allow_mbox_from}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($config->{"allow_mbox_from"}) 28 2 30
394 28 my $line = mbox_from ($bytes);
395 28 ▶ die "APPEND refused: raw message line $line resembles an unescaped mboxo From_ separator "
Branch: 1 branch
BranchTrueFalseTotal
if $line 18 10 28
396 . "(starts with 'From ' and has date-like text). Some mbox servers may mistake it "
397 . "for a new message boundary. No APPEND attempted. Escape the line, or ask the "
398 . "server operator to use --allow-mbox-from.\n"
399 if $line;
400 }
401  
402 ref $args->{flags} eq "ARRAY" || !defined $args->{flags}
403 12 ▶ or die "Invalid flags\n";
Branch: 1 branch
BranchTrueFalseTotal
ref $args->{"flags"} eq "ARRAY" or not defined $args->{"flags"} or die "Invalid flags\n" 12 0 12
Condition33%1/3
ref $args->{"flags"} eq "ARRAY" or not defined $args->{"flags"}
l!l&&r!l&&!r
0120
MC/DC0%0/2
ref $args->{"flags"} eq "ARRAY" or not defined $args->{"flags"}
ref $args->{"flags"} eq "ARRAY" not defined $args->{"flags"}
Truth table33%1/3
A or not(B)
A= ref $args->{"flags"} eq "ARRAY"B= not defined $args->{"flags"}
ABresult
1X1
011
000
404 12 ▶ for (@{$args->{flags} || []}) {
Condition50%1/2
$args->{'flags'} || []
l!l
012
MC/DC0%0/1
$args->{'flags'} || []
$args->{'flags'}
Truth table50%1/2
A
A= $args->{'flags'}
Aresult
11
00
405 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
406 or die "Invalid IMAP flag\n";
407 }
408 0 my @extra = $args->{flags} && @{$args->{flags}}
409 0 ▶ ? ("(" . (join " ", @{$args->{flags}}) . ")") : ();
Branch: 1 branch
BranchTrueFalseTotal
$args->{'flags'} && @{$args->{'flags'};} ? : 0 12 12
Condition33%1/3
$args->{"flags"} and @{$args->{"flags"};}
!ll&&!rl&&r
1200
MC/DC0%0/2
$args->{"flags"} and @{$args->{"flags"};}
$args->{"flags"} @{$args->{"flags"};}
Truth table33%1/3
A and B
A= $args->{"flags"}B= @{$args->{"flags"};}
ABresult
0X0
100
111
410 12 checked ($imap->append ($args->{mailbox}, @extra, { Literal => $bytes }), "APPEND");
411 12 return (1, { ok => JSON::XS::true });
412 } elsif ($cmd eq "expunge") {
413 6 my $validity = same_validity ($args, 1);
414 6 ▶ if ($args->{uids}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{'uids'}) { } 4 2 6
415 $imap->capability->{uidplus}
416 4 ▶ 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" 2 2 4
417 2 checked ($imap->uidexpunge (uids ($args)), "UID EXPUNGE");
418 } else {
419 2 checked ($imap->expunge, "EXPUNGE");
420 }
421 4 return (1, { ok => JSON::XS::true, uidvalidity => $validity });
422 } elsif ($cmd eq "mailbox") {
423 2 my $op = $args->{operation};
424 2 ▶ 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 2 2
Condition33%1/3
defined $op and $op =~ /\A(?:create|rename|delete|subscribe|unsubscribe)\z/u
!ll&&!rl&&r
020
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 table33%1/3
A and B
A= defined $opB= $op =~ /\A(?:create|rename|delete|subscribe|unsubscribe)\z/u
ABresult
0X0
100
111
425 or die "Invalid mailbox operation\n";
426 0 ▶ my @params = $op eq "rename" ? @{$args}{qw(mailbox destination)} : ($args->{mailbox});
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'rename' ? : 0 0 0
427 0 checked ($imap->$op (@params), uc $op);
428 0 return (1, { ok => JSON::XS::true });
429 }
430  
431 0 die "Unknown IMAP operation\n";
432 };
433 74 my $error = $@;
434 74 ▶ $result ? (1, $result) : (0, $error || "IMAP operation failed\n")
Branch: 2 branches
BranchTrueFalseTotal
$result ? : 44 30 74
$error or "IMAP operation failed\n" 30 0 30
435 }
436  
437 3 1;
« bin/mcp-webbrowser ↑ Summary blib/lib/AnyEvent/MCP/Server.pm »