Simple.pm

filter: Statementstmt 7.2% 49 / 675 Branchbran 1.7% 6 / 336 Conditioncond 3.6% 3 / 83 MC/DCmcdc 4.2% 2 / 47 Subroutinesub 6.4% 11 / 171 Podpod 100% 77 / 77 totaltotal 10.6% 148 / 1389 CC 210 SCAR 55.4
CC
210
Cov
5%
new
1980.0
_process_cmd
210.0
_read_multiline
132.0
CRAP 255.2
↑ Summary SimpleX.pm »
1 package Net::IMAP::Simple;
2  
3 20 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 20 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 20 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 20 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 20 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 20 ▶use IO::Select;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 20 ▶use Net::IMAP::Simple::PipeSocket;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 20 our $VERSION = "1.2212";
13  
14 20 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=1.2
15 # I'd really rather the pause/cpan indexers miss this "package"
16 eval ## no critic
17 0 ▶ q( package Net::IMAP::Simple::_message;
Subroutine BEGIN: called CC=1 SCAR=1.2
Subroutine __ANON__: not called CC=1 SCAR=1.2
Subroutine new: not called CC=1 SCAR=1.2
18 use overload fallback=>1, '""' => sub { local $"=""; "@{$_[0]}" };
19 sub new { bless $_[1] })
20 }
21  
22 20 our $uidm;
23  
24 0 ▶sub new {
Subroutine new: not called CC=44 SCAR=75.9
Pod documented
25 0 my ( $class, $server, %opts ) = @_;
26  
27 ## warn "use of Net::IMAP::Simple::SSL is depricated, pass use_ssl to new() instead\n"
28 ## if $class =~ m/::SSL/;
29  
30 0 my $self = bless { count => -1 } => $class;
31  
32 0 ▶ $self->{use_v6} = ( $opts{use_v6} ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$opts{'use_v6'} ? : 0 0 0
33 0 ▶ $self->{use_ssl} = ( $opts{use_ssl} ? 1 : 0 );
Branch: 1 branch
BranchTrueFalseTotal
$opts{'use_ssl'} ? : 0 0 0
34  
35 0 ▶ unless( $opts{shutup_about_v6ssl} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($opts{"shutup_about_v6ssl"}) 0 0 0
36 carp "use_ssl with IPv6 is not yet supported"
37 0 ▶ if $opts{use_v6} and $opts{use_ssl};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"use_v6"} and $opts{"use_ssl"} 0 0 0
Condition0%0/3
$opts{"use_v6"} and $opts{"use_ssl"}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"use_v6"} and $opts{"use_ssl"}
$opts{"use_v6"} $opts{"use_ssl"}
Truth table0%0/3
A and B
A= $opts{"use_v6"}B= $opts{"use_ssl"}
ABresult
0X0
100
111
38 }
39  
40 0 ▶ if( $opts{ssl_version} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"ssl_version"}) 0 0 0
41 0 $self->{ssl_version} = $opts{ssl_version};
42 0 $opts{use_ssl} = 1;
43 }
44  
45 0 ▶ $opts{use_ssl} = 1 if $opts{find_ssl_defaults};
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"find_ssl_defaults"} 0 0 0
46  
47 0 ▶ if( $opts{use_ssl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_ssl"}) 0 0 0
48 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require IO::Socket::SSL; "IO::Socket::SSL"->import; "true" } } or croak("IO::Socket::SSL must be installed in order to use_ssl") 0 0 0
49 0 require IO::Socket::SSL;
50 0 import IO::Socket::SSL;
51 0 "true";
52  
53 } or croak "IO::Socket::SSL must be installed in order to use_ssl";
54  
55 0 $self->{ssl_options} = [ eval {@{ $opts{ssl_options} }} ];
56 0 ▶ carp "ignoring ssl_options: $@" if $opts{ssl_options} and not @{ $self->{ssl_options} };
Branch: 1 branch
BranchTrueFalseTotal
if $opts{"ssl_options"} and not @{$self->{"ssl_options"};} 0 0 0
Condition0%0/3
$opts{"ssl_options"} and not @{$self->{"ssl_options"};}
!ll&&!rl&&r
000
MC/DC0%0/2
$opts{"ssl_options"} and not @{$self->{"ssl_options"};}
$opts{"ssl_options"} not @{$self->{"ssl_options"};}
Truth table0%0/3
A and not(B)
A= $opts{"ssl_options"}B= not @{$self->{"ssl_options"};}
ABresult
0X0
100
111
57  
58 0 ▶ unless( @{ $self->{ssl_options} } ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$self->{"ssl_options"};}) 0 0 0
59 0 ▶ if( $opts{find_ssl_defaults} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{'find_ssl_defaults'}) { } 0 0 0
60 0 my $nothing = 1;
61  
62 0 for(qw(
63 /etc/ssl/certs/ca-certificates.crt
64 /etc/pki/tls/certs/ca-bundle.crt
65 /etc/ssl/ca-bundle.pem
66 /etc/ssl/certs/
67 )) {
68  
69 0 ▶ if( -f $_ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $_) { } 0 0 0
elsif (-d $_) { } 0 0 0
70 0 @{ $self->{ssl_options} } = (SSL_ca_file=>$_, SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER());
71 0 $nothing = 0;
72 0 last;
73  
74 } elsif( -d $_ ) {
75 0 @{ $self->{ssl_options} } = (SSL_ca_path=>$_, SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_PEER());
76 0 $nothing = 0;
77 0 last;
78 }
79 }
80  
81 0 ▶ if( $nothing ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($nothing) 0 0 0
82 0 carp "couldn't find rational defaults for ssl verify. Choosing to not verify.";
83 0 @{ $self->{ssl_options} } = (SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE());
84 }
85  
86 } else {
87 0 @{ $self->{ssl_options} } = (SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE());
88 }
89 }
90 }
91  
92 0 ▶ if ( $opts{use_v6} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opts{"use_v6"}) 0 0 0
93 0 ▶ eval {
Branch: 1 branch
BranchTrueFalseTotal
eval { do { require IO::Socket::INET6; "IO::Socket::INET6"->import; "true" } } or croak("IO::Socket::INET6 must be installed in order to use_v6") 0 0 0
94 0 require IO::Socket::INET6;
95 0 import IO::Socket::INET6;
96 0 "true";
97  
98 } or croak "IO::Socket::INET6 must be installed in order to use_v6";
99 }
100  
101 0 ▶ if( $server =~ m/cmd:(.+)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($server =~ /cmd:(.+)/) { } 0 0 0
102 0 $self->{cmd} = $1;
103  
104 } else {
105 0 ▶ if( ($self->{server}, $self->{port}) = $server =~ m/^(\d{1,3}(?:\.\d{1,3}){3})(?::(\d+))?\z/ ) {
Branch: 4 branches
BranchTrueFalseTotal
if (($self->{'server'}, $self->{'port'}) = $server =~ /^(\d{1,3}(?:\.\d{1,3}){3})(?::(\d+))?\z/) { } 0 0 0
elsif (($self->{'server'}, $self->{'port'}) = $server =~ /^\[([a-fA-F0-9:]+)\]:(\d+)\z/) { } 0 0 0
elsif (($self->{'server'}, $self->{'port'}) = $server =~ /^([a-fA-F0-9:]+)\z/) { } 0 0 0
elsif (($self->{'server'}, $self->{'port'}) = $server =~ /^([^:]+):(\d+)\z/) { } 0 0 0
106  
107 } elsif( ($self->{server}, $self->{port}) = $server =~ m/^\[([a-fA-F0-9:]+)\]:(\d+)\z/ ) {
108  
109 } elsif( ($self->{server}, $self->{port}) = $server =~ m/^([a-fA-F0-9:]+)\z/ ) {
110  
111 } elsif( ($self->{server}, $self->{port}) = $server =~ m/^([^:]+):(\d+)\z/ ) {
112  
113 } else {
114 0 $self->{server} = $server;
115 0 $self->{port} = $opts{port};
116 }
117  
118 0 ▶ $self->{port} = $self->_port unless defined $self->{port};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"port"} 0 0 0
119 }
120  
121 0 ▶ $self->{timeout} = ( $opts{timeout} ? $opts{timeout} : $self->_timeout );
Branch: 1 branch
BranchTrueFalseTotal
$opts{'timeout'} ? : 0 0 0
122 0 ▶ $self->{retry} = ( defined($opts{retry}) ? $opts{retry} : $self->_retry );
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'retry'} ? : 0 0 0
123 0 ▶ $self->{retry_delay} = ( defined($opts{retry_delay}) ? $opts{retry_delay} : $self->_retry_delay );
Branch: 1 branch
BranchTrueFalseTotal
defined $opts{'retry_delay'} ? : 0 0 0
124 0 $self->{bindaddr} = $opts{bindaddr};
125 0 $self->{use_select_cache} = $opts{use_select_cache};
126 0 $self->{select_cache_ttl} = $opts{select_cache_ttl};
127 0 $self->{debug} = $opts{debug};
128 0 $self->{readline_callback} = $opts{readline_callback};
129  
130 0 my $sock;
131 my $c;
132 0 ▶ for ( my $i = 0 ; $i <= $self->{retry} ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $self->{"retry"}) 0 0 0
133 0 ▶ if ( $sock = $self->{sock} = $self->_connect ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($sock = $self->{'sock'} = $self->_connect) { } 0 0 0
elsif ($i < $self->{'retry'}) { } 0 0 0
134 0 $c = 1;
135 0 last;
136  
137 } elsif ( $i < $self->{retry} ) {
138 0 sleep $self->{retry_delay};
139  
140 # Critic NOTE: I'm not sure why this was done, but it was removed
141 # beucase the critic said it was bad and sleep makes more sense.
142 # select( undef, undef, undef, $self->{retry_delay} );
143 }
144 }
145  
146 0 ▶ if ( !$c ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c) 0 0 0
147 0 $@ =~ s/IO::Socket::INET6?: //g;
148 0 $Net::IMAP::Simple::errstr = "connection failed $@";
149 0 return;
150 }
151  
152 0 ▶ return unless $sock;
Branch: 1 branch
BranchTrueFalseTotal
unless $sock 0 0 0
153  
154 0 my $select = $self->{sel} = IO::Select->new($sock);
155  
156 0 ▶ $self->_debug( caller, __LINE__, 'new', "waiting for socket ready" ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
157  
158 0 my $greeting_ok = 0;
159 0 ▶ if( $select->can_read($self->{timeout}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($select->can_read($self->{'timeout'})) { } 0 0 0
160 0 ▶ $self->_debug( caller, __LINE__, 'new', "looking for greeting" ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
161 0 ▶ if( my $line = $sock->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $sock->getline) { } 0 0 0
162 # Cool, we got a line, check to see if it's a
163 # greeting.
164  
165 0 ▶ $self->_debug( caller, __LINE__, 'new', "got a greeting: $line" ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
166 0 ▶ $greeting_ok = 1 if $line =~ m/^\*\s+(?:OK|PREAUTH)/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\*\s+(?:OK|PREAUTH)/i 0 0 0
167  
168 # Also, check to see if we failed before we sent any
169 # commands.
170 0 ▶ return if $line =~ /^\*\s+(?:NO|BAD)(?:\s+(.+))?/i;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /^\*\s+(?:NO|BAD)(?:\s+(.+))?/i 0 0 0
171  
172 } else {
173 0 ▶ $self->_debug( caller, __LINE__, 'new', "server hung up during connect" ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
174  
175 # The server hung up on us, otherwise we'd get a line
176 # after can_read.
177 0 return;
178 }
179  
180 } else {
181 0 ▶ $self->_debug( caller, __LINE__, 'new', "no greeting found before timeout" ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
182 }
183  
184 0 ▶ return unless $greeting_ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $greeting_ok 0 0 0
185 0 return $self;
186 }
187  
188 0 ▶sub _connect {
Subroutine _connect: not called CC=8 SCAR=42.8
189 0 my ($self) = @_;
190 0 my $sock;
191  
192 0 ▶ if( $self->{cmd} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'cmd'}) { } 0 0 0
193 0 ▶ $self->_debug( caller, __LINE__, '_connect', "popping open a pipesocket for command: $self->{cmd}" ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
194 0 $sock = Net::IMAP::Simple::PipeSocket->new(cmd=>$self->{cmd});
195  
196 } else {
197 0 ▶ $self->_debug( caller, __LINE__, '_connect', "connecting to $self->{server}:$self->{port}" ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
198 $sock = $self->_sock_from->new(
199 PeerAddr => $self->{server},
200 PeerPort => $self->{port},
201 Timeout => $self->{timeout},
202 Proto => 'tcp',
203 ( $self->{bindaddr} ? ( LocalAddr => $self->{bindaddr} ) : () ),
204 ( $_[0]->{ssl_version} ? ( SSL_version => $self->{ssl_version} ) : () ),
205 0 ▶ ( $_[0]->{use_ssl} ? (@{ $self->{ssl_options} }) : () ),
Branch: 3 branches
BranchTrueFalseTotal
$self->{'bindaddr'} ? : 0 0 0
$_[0]{'ssl_version'} ? : 0 0 0
$_[0]{'use_ssl'} ? : 0 0 0
206 );
207 }
208  
209 0 ▶ $self->_debug( caller, __LINE__, '_connect', "connected, returning socket" ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
210 0 return $sock;
211 }
212  
213 0 ▶sub _port { return $_[0]->{use_ssl} ? 993 : 143 }
Subroutine _port: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_[0]{'use_ssl'} ? : 0 0 0
214 0 ▶sub _sock { return $_[0]->{sock} }
Subroutine _sock: not called CC=1 SCAR=6.9
215 0 ▶sub _count { return $_[0]->{count} }
Subroutine _count: not called CC=1 SCAR=6.9
216 0 ▶sub _last { $_[0]->select unless exists $_[0]->{last}; return $_[0]->{last}||0 }
Subroutine _last: not called CC=3 SCAR=24.8
Branch: 1 branch
BranchTrueFalseTotal
unless exists $_[0]{"last"} 0 0 0
Condition0%0/2
$_[0]{'last'} || 0
l!l
00
MC/DC0%0/1
$_[0]{'last'} || 0
$_[0]{'last'}
Truth table0%0/2
A
A= $_[0]{'last'}
Aresult
11
00
217 0 ▶sub _timeout { return 90 }
Subroutine _timeout: not called CC=1 SCAR=6.9
218 0 ▶sub _retry { return 1 }
Subroutine _retry: not called CC=1 SCAR=6.9
219 0 ▶sub _retry_delay { return 5 }
Subroutine _retry_delay: not called CC=1 SCAR=6.9
220 0 ▶sub _sock_from { return $_[0]->{use_v6} ? 'IO::Socket::INET6' : $_[0]->{use_ssl} ? 'IO::Socket::SSL' : 'IO::Socket::INET' }
Subroutine _sock_from: not called CC=3 SCAR=24.8
Branch: 2 branches
BranchTrueFalseTotal
$_[0]{'use_v6'} ? : 0 0 0
$_[0]{'use_ssl'} ? : 0 0 0
221  
222 0 ▶sub starttls {
Subroutine starttls: not called CC=1 SCAR=6.9
Pod documented
223 0 my ($self) = @_;
224  
225 0 require IO::Socket::SSL; import IO::Socket::SSL;
226 0 require Net::SSLeay; import Net::SSLeay;
227  
228 # $self->{debug} = 1;
229 # warn "Processing STARTTLS command";
230  
231 return $self->_process_cmd(
232 cmd => ['STARTTLS'],
233 0 ▶ final => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
234 0 Net::SSLeay::load_error_strings();
235 0 Net::SSLeay::SSLeay_add_ssl_algorithms();
236 0 Net::SSLeay::randomize();
237  
238 my $startres = IO::Socket::SSL->start_SSL(
239 $self->{sock},
240 0 ▶ SSL_version => $self->{ssl_version} || "SSLv3 TLSv1",
Condition0%0/2
$self->{'ssl_version'} || 'SSLv3 TLSv1'
l!l
00
MC/DC0%0/1
$self->{'ssl_version'} || 'SSLv3 TLSv1'
$self->{'ssl_version'}
Truth table0%0/2
A
A= $self->{'ssl_version'}
Aresult
11
00
241 SSL_startHandshake => 0,
242 );
243  
244 0 ▶ unless ( $startres ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($startres) 0 0 0
245 0 croak "Couldn't start TLS: " . IO::Socket::SSL::errstr() . "\n";
246 }
247  
248 0 ▶ $self->_debug( caller, __LINE__, 'starttls', "TLS initialization done" ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
249 0 1;
250 },
251  
252 # process => sub { push @lines, $_[0] if $_[0] =~ /^(?: \s+\S+ | [^:]+: )/x },
253 0 );
254 }
255  
256 0 ▶sub login {
Subroutine login: not called CC=1 SCAR=6.9
Pod documented
257 0 my ( $self, $user, $pass ) = @_;
258  
259 0 $pass = _escape($pass);
260  
261 return $self->_process_cmd(
262 cmd => [ LOGIN => qq[$user $pass] ],
263 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
264 0 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
265 0 );
266 }
267  
268 0 ▶sub separator {
Subroutine separator: not called CC=1 SCAR=6.9
Pod documented
269 0 my ( $self, ) = @_;
270 0 my $sep;
271  
272 return $self->_process_cmd (
273 cmd => [ LIST => qq["" ""] ],
274 0 ▶ final => sub { $sep },
Subroutine __ANON__: not called CC=1 SCAR=6.9
275 0 ▶ process => sub { (undef,undef,undef,$sep,undef) = split /\s/smx , $_[0];
Subroutine __ANON__: not called CC=1 SCAR=6.9
276 0 $sep =~ s/["]//g; },
277 0 );
278 }
279  
280 0 ▶sub _clear_cache {
Subroutine _clear_cache: not called CC=5 SCAR=34.0
281 0 my $self = shift;
282 0 my $cb = $self->current_box;
283  
284 0 ▶ push @_, $cb if $cb and not @_;
Branch: 1 branch
BranchTrueFalseTotal
if $cb and not @_ 0 0 0
Condition0%0/3
$cb and not @_
!ll&&!rl&&r
000
MC/DC0%0/2
$cb and not @_
$cb not @_
Truth table0%0/3
A and not(B)
A= $cbB= not @_
ABresult
0X0
100
111
285 0 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
286  
287 0 for my $box (@_) {
288 0 delete $self->{BOXES}{$box};
289 }
290  
291 0 delete $self->{last};
292  
293 0 return 1;
294 }
295  
296 0 ▶sub uidnext {
Subroutine uidnext: not called CC=3 SCAR=24.8
Pod documented
297 0 my $self = shift;
298 0 ▶ my $mbox = shift || $self->current_box || "INBOX";
Condition0%0/5
shift() || $self->current_box || 'INBOX'
l!l
00
shift() or $self->current_box
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->current_box || 'INBOX'
shift() $self->current_box
Truth table0%0/3
A or B
A= shift()B= $self->current_box
ABresult
1X1
011
000
299  
300 0 return $self->status($mbox => 'uidnext');
301 }
302  
303 0 ▶sub uidvalidity {
Subroutine uidvalidity: not called CC=3 SCAR=24.8
Pod documented
304 0 my $self = shift;
305 0 ▶ my $mbox = shift || $self->current_box || "INBOX";
Condition0%0/5
shift() || $self->current_box || 'INBOX'
l!l
00
shift() or $self->current_box
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->current_box || 'INBOX'
shift() $self->current_box
Truth table0%0/3
A or B
A= shift()B= $self->current_box
ABresult
1X1
011
000
306  
307 0 return $self->status($mbox => 'uidvalidity');
308 }
309  
310 0 ▶sub uidsearch {
Subroutine uidsearch: not called CC=1 SCAR=6.9
Pod documented
311 0 my $self = shift;
312  
313 0 local $uidm = 1;
314  
315 0 return $self->search(@_);
316 }
317  
318 0 ▶sub uid {
Subroutine uid: not called CC=2 SCAR=17.9
Pod documented
319 0 my $self = shift;
320 0 $self->_be_on_a_box; # does a select if we're not on a mailbox
321  
322 0 ▶ return $self->uidsearch( shift || "1:*" );
Condition0%0/2
shift() || '1:*'
l!l
00
MC/DC0%0/1
shift() || '1:*'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
323 }
324  
325 0 ▶sub seq {
Subroutine seq: not called CC=2 SCAR=17.9
Pod documented
326 0 my $self = shift;
327 0 ▶ my $msgno = shift || "1:*";
Condition0%0/2
shift() || '1:*'
l!l
00
MC/DC0%0/1
shift() || '1:*'
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
328  
329 0 $self->_be_on_a_box; # does a select if we're not on a mailbox
330  
331 0 return $self->search("uid $msgno");
332 }
333  
334 0 ▶sub status {
Subroutine status: not called CC=4 SCAR=30.0
Pod documented
335 0 my $self = shift;
336 0 ▶ my $mbox = shift || $self->current_box || "INBOX";
Condition0%0/5
shift() || $self->current_box || 'INBOX'
l!l
00
shift() or $self->current_box
l!l&&r!l&&!r
000
MC/DC0%0/2
shift() || $self->current_box || 'INBOX'
shift() $self->current_box
Truth table0%0/3
A or B
A= shift()B= $self->current_box
ABresult
1X1
011
000
337 0 ▶ my @fields = @_ ? @_ : qw(unseen recent messages);
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
338  
339 # Example: C: A042 STATUS blurdybloop (UIDNEXT MESSAGES)
340 # S: * STATUS blurdybloop (MESSAGES 231 UIDNEXT 44292)
341 # S: A042 OK STATUS completed
342  
343 0 @fields = map{uc$_} @fields;
344 0 my %fields;
345  
346 return $self->_process_cmd(
347 cmd => [ STATUS => _escape($mbox) . " (@fields)" ],
348 0 ▶ final => sub { (@fields{@fields}) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
349 0 ▶ process => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
350 0 ▶ if( my ($status) = $_[0] =~ m/\* STATUS.+?$mbox.+?\((.+?)\)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($status) = $_[0] =~ /\* STATUS.+?$mbox.+?\((.+?)\)/i) 0 0 0
351  
352 0 for( @fields ) {
353 0 ▶ $fields{$_} = _unescape($1)
Branch: 1 branch
BranchTrueFalseTotal
if $status =~ /$_\s+(\S+|"[^"]+"|'[^']+')/i 0 0 0
354 if $status =~ m/$_\s+(\S+|"[^"]+"|'[^']+')/i
355 # NOTE: this regex isn't perfect, but should almost always work
356 # for status values returned by a well meaning IMAP server
357 }
358  
359 }
360 },
361 0 );
362 }
363  
364 0 ▶sub select { ## no critic -- too late to choose a different name now...
Subroutine select: not called CC=8 SCAR=42.8
Pod documented
365 0 my ( $self, $mbox, $examine_mode ) = @_;
366 0 ▶ $examine_mode = $examine_mode ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$examine_mode ? : 0 0 0
367 0 ▶ $self->{examine_mode} = 0 unless exists $self->{examine_mode};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"examine_mode"} 0 0 0
368  
369 0 ▶ $mbox = $self->current_box unless $mbox;
Branch: 1 branch
BranchTrueFalseTotal
unless $mbox 0 0 0
370  
371 0 ▶ if( $examine_mode == $self->{examine_mode} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($examine_mode == $self->{"examine_mode"}) 0 0 0
372 0 ▶ if ( $self->{use_select_cache} && ( time - $self->{BOXES}{$mbox}{proc_time} ) <= $self->{select_cache_ttl} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"use_select_cache"} and time - $self->{"BOXES"}{$mbox}{"proc_time"} <= $self->{"select_cache_ttl"}) 0 0 0
Condition0%0/3
$self->{"use_select_cache"} and time - $self->{"BOXES"}{$mbox}{"proc_time"} <= $self->{"select_cache_ttl"}
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{"use_select_cache"} and time - $self->{"BOXES"}{$mbox}{"proc_time"} <= $self->{"select_cache_ttl"}
$self->{"use_select_cache"} time - $self->{"BOXES"}{$mbox}{"proc_time"} <= $self->{"select_cache_ttl"}
Truth table0%0/3
A and B
A= $self->{"use_select_cache"}B= time - $self->{"BOXES"}{$mbox}{"proc_time"} <= $self->{"select_cache_ttl"}
ABresult
0X0
100
111
373 0 return $self->{BOXES}{$mbox}{messages};
374 }
375 }
376  
377 0 $self->{BOXES}{$mbox}{proc_time} = time;
378  
379 0 ▶ my $cmd = $examine_mode ? 'EXAMINE' : 'SELECT';
Branch: 1 branch
BranchTrueFalseTotal
$examine_mode ? : 0 0 0
380  
381 return $self->_process_cmd(
382 cmd => [ $cmd => _escape($mbox) ],
383 0 ▶ final => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
384 0 my $nm = $self->{last} = $self->{BOXES}{$mbox}{messages};
385  
386 0 $self->{working_box} = $mbox;
387 0 $self->{examine_mode} = $examine_mode;
388  
389 0 ▶ $nm ? $nm : "0E0";
Branch: 1 branch
BranchTrueFalseTotal
$nm ? : 0 0 0
390 },
391 0 ▶ process => sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
392 0 ▶ if ( $_[0] =~ /^\*\s+(\d+)\s+EXISTS/i ) {
Branch: 4 branches
BranchTrueFalseTotal
if ($_[0] =~ /^\*\s+(\d+)\s+EXISTS/i) { } 0 0 0
elsif ($_[0] =~ /^\*\s+FLAGS\s+\((.*?)\)/i) { } 0 0 0
elsif ($_[0] =~ /^\*\s+(\d+)\s+RECENT/i) { } 0 0 0
elsif ($_[0] =~ /^\*\s+OK\s+\[(.*?)\s+(.*?)\]/i) { } 0 0 0
393 0 $self->{BOXES}{$mbox}{messages} = $1;
394  
395 } elsif ( $_[0] =~ /^\*\s+FLAGS\s+\((.*?)\)/i ) {
396 0 $self->{BOXES}{$mbox}{flags} = [ split( /\s+/, $1 ) ];
397  
398 } elsif ( $_[0] =~ /^\*\s+(\d+)\s+RECENT/i ) {
399 0 $self->{BOXES}{$mbox}{recent} = $1;
400  
401 } elsif ( $_[0] =~ /^\*\s+OK\s+\[(.*?)\s+(.*?)\]/i ) {
402 0 my ( $flag, $value ) = ( $1, $2 );
403  
404 0 ▶ if ( $value =~ /\((.*?)\)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\((.*?)\)/) { } 0 0 0
405 # NOTE: the sflags really aren't used anywhere, should they be?
406 0 $self->{BOXES}{$mbox}{sflags}{$flag} = [ split( /\s+/, $1 ) ];
407  
408 } else {
409 0 $self->{BOXES}{$mbox}{oflags}{$flag} = $value;
410 }
411 }
412 },
413 0 );
414 }
415  
416 0 ▶sub examine {
Subroutine examine: not called CC=1 SCAR=6.9
Pod documented
417 0 my $self = shift;
418  
419 0 return $self->select($_[0], 1);
420 }
421  
422 0 ▶sub messages {
Subroutine messages: not called CC=1 SCAR=6.9
Pod documented
423 0 my ( $self, $folder ) = @_;
424  
425 0 return $self->select($folder);
426 }
427  
428 0 ▶sub flags {
Subroutine flags: not called CC=2 SCAR=17.9
Pod documented
429 0 my ( $self, $folder ) = @_;
430  
431 0 $self->select($folder);
432  
433 0 ▶ return @{ $self->{BOXES}{ $self->current_box }{flags} || [] };
Condition0%0/2
$self->{'BOXES'}{$self->current_box}{'flags'} || []
l!l
00
MC/DC0%0/1
$self->{'BOXES'}{$self->current_box}{'flags'} || []
$self->{'BOXES'}{$self->current_box}{'flags'}
Truth table0%0/2
A
A= $self->{'BOXES'}{$self->current_box}{'flags'}
Aresult
11
00
434 }
435  
436 0 ▶sub recent {
Subroutine recent: not called CC=1 SCAR=6.9
Pod documented
437 0 my ( $self, $folder ) = @_;
438  
439 0 $self->select($folder);
440  
441 0 return $self->{BOXES}{ $self->current_box }{recent};
442 }
443  
444 0 ▶sub unseen {
Subroutine unseen: not called CC=2 SCAR=17.9
Pod documented
445 0 my ( $self, $folder ) = @_;
446  
447 0 my $oflags = $self->{BOXES}{ $self->current_box }{oflags};
448  
449 0 ▶ if( exists $oflags->{UNSEEN} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $oflags->{"UNSEEN"}) 0 0 0
450 0 $self->select($folder);
451  
452 0 return $self->{BOXES}{ $self->current_box }{oflags}{UNSEEN};
453 }
454  
455 0 my ($unseen) = $self->status;
456  
457 0 return $unseen;
458 }
459  
460 0 ▶sub current_box {
Subroutine current_box: not called CC=2 SCAR=17.9
Pod documented
461 0 my ($self) = @_;
462  
463 0 ▶ return ( $self->{working_box} ? $self->{working_box} : 'INBOX' );
Branch: 1 branch
BranchTrueFalseTotal
$self->{'working_box'} ? : 0 0 0
464 }
465  
466 0 ▶sub close { ## no critic -- we already have tons of methods with built in names
Subroutine close: not called CC=1 SCAR=6.9
Pod documented
467  
468 0 my $self = shift;
469 0 $self->{working_box} = undef;
470 0 return $self->_process_cmd(
471 cmd => [ "CLOSE" ],
472 );
473 }
474  
475 0 ▶sub noop {
Subroutine noop: not called CC=1 SCAR=6.9
Pod documented
476 0 my $self = shift;
477 0 return $self->_process_cmd(
478 cmd => [ "NOOP" ],
479 );
480 }
481  
482 0 ▶sub top {
Subroutine top: not called CC=2 SCAR=17.9
Pod documented
483 0 my ( $self, $number ) = @_;
484 0 ▶ my $messages = $number || '1:' . $self->_last;
Condition0%0/2
$number || '1:' . $self->_last
l!l
00
MC/DC0%0/1
$number || '1:' . $self->_last
$number
Truth table0%0/2
A
A= $number
Aresult
11
00
485  
486 0 my @lines;
487  
488 ## rfc2822 ## 2.2. Header Fields
489  
490 ## rfc2822 ## Header fields are lines composed of a field name, followed by a colon
491 ## rfc2822 ## (":"), followed by a field body, and terminated by CRLF. A field
492 ## rfc2822 ## name MUST be composed of printable US-ASCII characters (i.e.,
493 ## rfc2822 ## characters that have values between 33 and 126, inclusive), except
494 ## rfc2822 ## colon. A field body may be composed of any US-ASCII characters,
495 ## rfc2822 ## except for CR and LF. However, a field body may contain CRLF when
496 ## rfc2822 ## used in header "folding" and "unfolding" as described in section
497 ## rfc2822 ## 2.2.3. All field bodies MUST conform to the syntax described in
498 ## rfc2822 ## sections 3 and 4 of this standard.
499  
500 return $self->_process_cmd(
501 cmd => [ FETCH => qq[$messages RFC822.HEADER] ],
502 0 ▶ final => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
503 0 ▶ $lines[-1] =~ s/\)\x0d\x0a\z// # sometimes we get this and I don't think we should
Branch: 1 branch
BranchTrueFalseTotal
if @lines 0 0 0
504 # I really hoping I'm not breaking someting by doing this.
505 if @lines;
506  
507 0 ▶ return wantarray ? @lines : \@lines
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
508 },
509 0 ▶ process => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
510 0 ▶ return if $_[0] =~ m/\*\s+\d+\s+FETCH/i; # should this really be case insensitive?
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] =~ /\*\s+\d+\s+FETCH/i 0 0 0
511  
512 0 ▶ if( not @lines or $_[0] =~ m/^[!-9;-~]+:/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @lines or $_[0] =~ /^[!-9;-~]+:/) { } 0 0 0
Condition0%0/3
not @lines or $_[0] =~ /^[!-9;-~]+:/
l!l&&r!l&&!r
000
MC/DC0%0/2
not @lines or $_[0] =~ /^[!-9;-~]+:/
not @lines $_[0] =~ /^[!-9;-~]+:/
Truth table0%0/3
not(A) or B
A= not @linesB= $_[0] =~ /^[!-9;-~]+:/
ABresult
1X1
011
000
513 0 push @lines, $_[0];
514  
515 } else {
516 0 $lines[-1] .= $_[0];
517 }
518 },
519 0 );
520 }
521  
522 0 ▶sub seen {
Subroutine seen: not called CC=3 SCAR=24.8
Pod documented
523 0 my ( $self, $number ) = @_;
524  
525 0 my @flags = $self->msg_flags($number);
526 0 ▶ return if $self->waserr;
Branch: 1 branch
BranchTrueFalseTotal
if $self->waserr 0 0 0
527 0 ▶ return 1 if grep {$_ eq '\Seen'} @flags;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "\\Seen";} @flags 0 0 0
528 0 return 0;
529 }
530  
531 0 ▶sub deleted {
Subroutine deleted: not called CC=3 SCAR=24.8
Pod documented
532 0 my ( $self, $number ) = @_;
533  
534 0 my @flags = $self->msg_flags($number);
535 0 ▶ return if $self->waserr;
Branch: 1 branch
BranchTrueFalseTotal
if $self->waserr 0 0 0
536 0 ▶ return 1 if grep {$_ eq '\Deleted'} @flags;
Branch: 1 branch
BranchTrueFalseTotal
if grep {$_ eq "\\Deleted";} @flags 0 0 0
537 0 return 0;
538 }
539  
540 1 ▶sub range2list {
Subroutine range2list: called CC=2 SCAR=7.0
Pod documented
541 1 my $self_or_class = shift;
542 1 my %h;
543 1 ▶ my @items = grep {!$h{$_}++} map { m/(\d+):(\d+)/ ? ($1 .. $2) : ($_) } split(m/[,\s]+/, shift);
Branch: 1 branch
BranchTrueFalseTotal
/(\d+):(\d+)/ ? : 3 0 3
544  
545 1 return @items;
546 }
547  
548 1 ▶sub list2range {
Subroutine list2range: called CC=5 SCAR=16.1
Pod documented
549 1 my $self_or_class = shift;
550 1 my %h;
551 1 my @a = sort { $a<=>$b } grep {!$h{$_}++} grep {m/^\d+/} grep {defined $_} @_;
552 1 my @b;
553  
554 1 ▶ while(@a) {
Branch: 1 branch
BranchTrueFalseTotal
if (@a) 3 1 4
555 3 my $e = 0;
556  
557 3 ▶ $e++ while $e+1 < @a and $a[$e]+1 == $a[$e+1];
Branch: 1 branch
BranchTrueFalseTotal
$e + 1 < @a and $a[$e] + 1 == $a[$e + 1] and do { ++$e } 68 3 71
Condition100%3/3
$e + 1 < @a and $a[$e] + 1 == $a[$e + 1]
!ll&&!rl&&r
1268
MC/DC100%2/2
$e + 1 < @a and $a[$e] + 1 == $a[$e + 1]
$e + 1 < @a $a[$e] + 1 == $a[$e + 1]
Truth table100%3/3
A and B
A= $e + 1 < @aB= $a[$e] + 1 == $a[$e + 1]
ABresult
0X0
100
111
558  
559 3 ▶ push @b, ($e>0 ? [$a[0], $a[$e]] : [$a[0]]);
Branch: 1 branch
BranchTrueFalseTotal
$e > 0 ? : 3 0 3
560 3 splice @a, 0, $e+1;
561 }
562  
563 1 return join(",", map {join(":", @$_)} @b);
564 }
565  
566 0 ▶sub list {
Subroutine list: not called CC=3 SCAR=24.8
Pod documented
567 0 my ( $self, $number ) = @_;
568  
569 # NOTE: this entire function is horrible:
570 # 1. it should be called message_size() or something similar
571 # 2. what if $number is a range? none of this works right
572  
573 0 ▶ my $messages = $number || '1:' . $self->_last;
Condition0%0/2
$number || '1:' . $self->_last
l!l
00
MC/DC0%0/1
$number || '1:' . $self->_last
$number
Truth table0%0/2
A
A= $number
Aresult
11
00
574 0 my %list;
575  
576 0 ▶ return {} if $messages eq "1:0";
Branch: 1 branch
BranchTrueFalseTotal
if $messages eq "1:0" 0 0 0
577  
578 return $self->_process_cmd(
579 cmd => [ FETCH => qq[$messages RFC822.SIZE] ],
580 0 ▶ final => sub { $number ? $list{$number} : \%list },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$number ? : 0 0 0
581 0 ▶ process => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
582 0 ▶ if ( $_[0] =~ /^\*\s+(\d+).*RFC822.SIZE\s+(\d+)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\*\s+(\d+).*RFC822.SIZE\s+(\d+)/i) 0 0 0
583 0 $list{$1} = $2;
584 }
585 },
586 0 );
587 }
588  
589 0 ▶sub search {
Subroutine search: not called CC=6 SCAR=37.4
Pod documented
590 0 my ($self, $search, $sort, $charset) = @_;
591  
592 0 ▶ $search ||= "ALL";
Condition0%0/2
$search ||= "ALL"
l!l
00
MC/DC0%0/1
$search ||= "ALL"
$search
Truth table0%0/2
A
A= $search
Aresult
11
00
593 0 ▶ $charset ||= 'UTF-8';
Condition0%0/2
$charset ||= "UTF-8"
l!l
00
MC/DC0%0/1
$charset ||= "UTF-8"
$charset
Truth table0%0/2
A
A= $charset
Aresult
11
00
594  
595 0 ▶ my $cmd = $uidm ? 'UID SEARCH' : 'SEARCH';
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMAP::Simple::uidm ? : 0 0 0
596  
597 0 $self->_be_on_a_box; # does a select if we're not on a mailbox
598  
599 # add rfc5256 sort, requires charset :(
600 0 ▶ if ($sort) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sort) 0 0 0
601 0 $sort = uc $sort;
602 0 ▶ $cmd = ($uidm ? "UID ": "") . "SORT ($sort) \"$charset\"";
Branch: 1 branch
BranchTrueFalseTotal
$Net::IMAP::Simple::uidm ? : 0 0 0
603 }
604  
605 0 my @seq;
606  
607 return $self->_process_cmd(
608 cmd => [ $cmd => $search ],
609 0 ▶ final => sub { wantarray ? @seq : int @seq },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
610 0 ▶ process => sub { if ( my ($msgs) = $_[0] =~ /^\*\s+(?:SEARCH|SORT)\s+(.*)/i ) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (my($msgs) = $_[0] =~ /^\*\s+(?:SEARCH|SORT)\s+(.*)/i) 0 0 0
611  
612 0 @seq = $self->range2list($msgs);
613  
614 }},
615 0 );
616 }
617  
618 0 ▶sub search_seen { my $self = shift; return $self->search("SEEN"); }
Subroutine search_seen: not called CC=1 SCAR=6.9
Pod documented
619 0 ▶sub search_recent { my $self = shift; return $self->search("RECENT"); }
Subroutine search_recent: not called CC=1 SCAR=6.9
Pod documented
620 0 ▶sub search_answered { my $self = shift; return $self->search("ANSWERED"); }
Subroutine search_answered: not called CC=1 SCAR=6.9
Pod documented
621 0 ▶sub search_deleted { my $self = shift; return $self->search("DELETED"); }
Subroutine search_deleted: not called CC=1 SCAR=6.9
Pod documented
622 0 ▶sub search_flagged { my $self = shift; return $self->search("FLAGGED"); }
Subroutine search_flagged: not called CC=1 SCAR=6.9
Pod documented
623 0 ▶sub search_draft { my $self = shift; return $self->search("FLAGGED"); }
Subroutine search_draft: not called CC=1 SCAR=6.9
Pod documented
624  
625 0 ▶sub search_unseen { my $self = shift; return $self->search("UNSEEN"); }
Subroutine search_unseen: not called CC=1 SCAR=6.9
Pod documented
626 0 ▶sub search_old { my $self = shift; return $self->search("OLD"); }
Subroutine search_old: not called CC=1 SCAR=6.9
Pod documented
627 0 ▶sub search_unanswered { my $self = shift; return $self->search("UNANSWERED"); }
Subroutine search_unanswered: not called CC=1 SCAR=6.9
Pod documented
628 0 ▶sub search_undeleted { my $self = shift; return $self->search("UNDELETED"); }
Subroutine search_undeleted: not called CC=1 SCAR=6.9
Pod documented
629 0 ▶sub search_unflagged { my $self = shift; return $self->search("UNFLAGGED"); }
Subroutine search_unflagged: not called CC=1 SCAR=6.9
Pod documented
630  
631 0 ▶sub search_smaller { my $self = shift; my $octets = int shift; return $self->search("SMALLER $octets"); }
Subroutine search_smaller: not called CC=1 SCAR=6.9
Pod documented
632 0 ▶sub search_larger { my $self = shift; my $octets = int shift; return $self->search("LARGER $octets"); }
Subroutine search_larger: not called CC=1 SCAR=6.9
Pod documented
633  
634 0 ▶sub _process_date {
Subroutine _process_date: not called CC=3 SCAR=24.8
635 0 my $d = shift;
636  
637 0 ▶ if( eval 'use Date::Manip (); 1' ) { ## no critic
Branch: 1 branch
BranchTrueFalseTotal
if (eval 'use Date::Manip (); 1') { } 0 0 0
638 0 ▶ if( my $pd = Date::Manip::ParseDate($d) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $pd = Date::Manip::ParseDate($d)) 0 0 0
639  
640 # NOTE: RFC 3501 wants this poorly-internationalized date format
641 # for SEARCH. Not my fault.
642  
643 0 return Date::Manip::UnixDate($pd, '%d-%b-%Y');
644 }
645  
646 } else {
647 # TODO: complain if the date isn't %d-%m-%Y
648  
649 # I'm not sure there's anything to be gained by doing so ... They'll
650 # just get an imap error they can choose to handle.
651 }
652  
653 0 return $d;
654 }
655  
656 0 ▶sub _process_qstring {
Subroutine _process_qstring: not called CC=1 SCAR=6.9
657 0 my $t = shift;
658 0 $t =~ s/\\/\\\\/g;
659 0 $t =~ s/"/\\"/g;
660  
661 0 return "\"$t\"";
662 }
663  
664 0 ▶sub search_before { my $self = shift; my $d = _process_date(shift); return $self->search("BEFORE $d"); }
Subroutine search_before: not called CC=1 SCAR=6.9
Pod documented
665 0 ▶sub search_since { my $self = shift; my $d = _process_date(shift); return $self->search("SINCE $d"); }
Subroutine search_since: not called CC=1 SCAR=6.9
Pod documented
666 0 ▶sub search_sent_before { my $self = shift; my $d = _process_date(shift); return $self->search("SENTBEFORE $d"); }
Subroutine search_sent_before: not called CC=1 SCAR=6.9
Pod documented
667 0 ▶sub search_sent_since { my $self = shift; my $d = _process_date(shift); return $self->search("SENTSINCE $d"); }
Subroutine search_sent_since: not called CC=1 SCAR=6.9
Pod documented
668  
669 0 ▶sub search_from { my $self = shift; my $t = _process_qstring(shift); return $self->search("FROM $t"); }
Subroutine search_from: not called CC=1 SCAR=6.9
Pod documented
670 0 ▶sub search_to { my $self = shift; my $t = _process_qstring(shift); return $self->search("TO $t"); }
Subroutine search_to: not called CC=1 SCAR=6.9
Pod documented
671 0 ▶sub search_cc { my $self = shift; my $t = _process_qstring(shift); return $self->search("CC $t"); }
Subroutine search_cc: not called CC=1 SCAR=6.9
Pod documented
672 0 ▶sub search_bcc { my $self = shift; my $t = _process_qstring(shift); return $self->search("BCC $t"); }
Subroutine search_bcc: not called CC=1 SCAR=6.9
Pod documented
673 0 ▶sub search_subject { my $self = shift; my $t = _process_qstring(shift); return $self->search("SUBJECT $t"); }
Subroutine search_subject: not called CC=1 SCAR=6.9
Pod documented
674 0 ▶sub search_body { my $self = shift; my $t = _process_qstring(shift); return $self->search("BODY $t"); }
Subroutine search_body: not called CC=1 SCAR=6.9
Pod documented
675  
676 0 ▶sub get {
Subroutine get: not called CC=2 SCAR=17.9
Pod documented
677 0 my ( $self, $number, $part ) = @_;
678 0 ▶ my $arg = $part ? "BODY[$part]" : 'RFC822';
Branch: 1 branch
BranchTrueFalseTotal
$part ? : 0 0 0
679  
680 0 return $self->fetch( $number, $arg );
681 }
682  
683 0 ▶sub fetch {
Subroutine fetch: not called CC=2 SCAR=17.9
Pod documented
684 0 my ( $self, $number, $part ) = @_;
685 0 ▶ my $arg = $part || 'RFC822';
Condition0%0/2
$part || 'RFC822'
l!l
00
MC/DC0%0/1
$part || 'RFC822'
$part
Truth table0%0/2
A
A= $part
Aresult
11
00
686  
687 0 my @lines;
688 my $fetching;
689  
690 return $self->_process_cmd(
691 cmd => [ FETCH => qq[$number $arg] ],
692 0 ▶ final => sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
693 0 ▶ if( $fetching ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fetching) 0 0 0
694 0 ▶ if( $fetching > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fetching > 0) 0 0 0
695 # XXX: this is just about the least efficient way in the
696 # world to do this; I should appologize, but really,
697 # nothing in this module is done particularly well. I
698 # doubt anyone will notice this.
699  
700 0 local $"="";
701 0 my $message = "@lines";
702 0 ▶ @lines = split m/(?<=\x0d\x0a)/, substr($message, 0, $fetching)
Branch: 1 branch
BranchTrueFalseTotal
if length $message > $fetching 0 0 0
703 if( length $message > $fetching );
704 }
705 0 ▶ return wantarray ? @lines : Net::IMAP::Simple::_message->new(\@lines)
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
706 }
707  
708 0 ▶ if( defined $fetching and $fetching == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $fetching and $fetching == 0) 0 0 0
Condition0%0/3
defined $fetching and $fetching == 0
!ll&&!rl&&r
000
MC/DC0%0/2
defined $fetching and $fetching == 0
defined $fetching $fetching == 0
Truth table0%0/3
A and B
A= defined $fetchingB= $fetching == 0
ABresult
0X0
100
111
709 0 return "\n"; # XXX: Your 0 byte message is incorrectly returned as a newline. Meh.
710 }
711  
712 # NOTE: There is not supposed to be an error if you ask for a
713 # message that's not there, but this is a rather confusing
714 # notion … so we generate an error here.
715  
716 0 $self->{_errstr} = "message not found";
717 0 return;
718 },
719 0 ▶ process => sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
720 0 ▶ if ( $_[0] =~ /^\*\s+\d+\s+FETCH\s+\(.+?\{(\d+)\}/ ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($_[0] =~ /^\*\s+\d+\s+FETCH\s+\(.+?\{(\d+)\}/) { } 0 0 0
elsif ($_[0] =~ /^\*\s+\d+\s+FETCH\s+\(.+?\"(.*)\"\s*\)/) { } 0 0 0
elsif ($fetching) { } 0 0 0
721 0 $fetching = $1;
722  
723 } elsif( $_[0] =~ /^\*\s+\d+\s+FETCH\s+\(.+?\"(.*)\"\s*\)/ ) {
724 # XXX: this is not tested because Net::IMAP::Server doesn't do
725 # this type of string result (that I know of) for this it might
726 # work, ... frog knows. Not likely to come up very often, if
727 # ever; although you do sometimes see the occasional 0byte
728 # message. Valid really.
729  
730 0 $fetching = -1;
731 0 @lines = ($1);
732  
733 } elsif( $fetching ) {
734 0 push @lines, join( ' ', @_ );
735 }
736 },
737 0 );
738  
739 }
740  
741 0 ▶sub _process_flags {
Subroutine _process_flags: not called CC=1 SCAR=6.9
742 0 my $self = shift;
743 0 my @ret = map { split m/\s+/, $_ } grep { $_ } @_;
744  
745 0 return @ret;
746 }
747  
748 0 ▶sub put {
Subroutine put: not called CC=5 SCAR=34.0
Pod documented
749 0 my ( $self, $mailbox_name, $msg, @flags ) = @_;
750  
751 0 ▶ croak "usage: \$imap->put(mailbox, message, \@flags)" unless defined $msg and defined $mailbox_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg and defined $mailbox_name 0 0 0
Condition0%0/3
defined $msg and defined $mailbox_name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $msg and defined $mailbox_name
defined $msg defined $mailbox_name
Truth table0%0/3
A and B
A= defined $msgB= defined $mailbox_name
ABresult
0X0
100
111
752  
753 0 my $size = length $msg;
754 0 ▶ if ( ref $msg eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg eq "ARRAY") 0 0 0
755 0 $size = 0;
756 0 $size += length $_ for @$msg;
757 }
758  
759 0 @flags = $self->_process_flags(@flags);
760  
761 return $self->_process_cmd(
762 cmd => [ APPEND => _escape($mailbox_name) ." (@flags) {$size}" ],
763 0 ▶ final => sub { $self->_clear_cache; 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
764 0 ▶ process => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
765 0 ▶ if( $_[0] =~ m/^\+\s+/ ) { # + continue (or go ahead, or whatever)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\+\s+/) 0 0 0
766 0 ▶ if ($size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size) 0 0 0
767 0 my $sock = $self->_sock;
768 0 ▶ if ( ref $msg eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg eq 'ARRAY') { } 0 0 0
769 0 print $sock $_ for @$msg;
770  
771 } else {
772 0 print $sock $msg;
773 }
774 0 $size = undef;
775 0 print $sock "\r\n";
776 }
777 }
778 },
779  
780 0 );
781 }
782  
783 # This supports supplying a date per IMAP RFC 3501
784 # APPEND Command - Section 6.3.11
785 # Implemented here as a new method so when calling the put above
786 # older code will not break
787 0 ▶sub put_with_date {
Subroutine put_with_date: not called CC=6 SCAR=37.4
Pod documented
788 0 my ( $self, $mailbox_name, $msg, $date, @flags ) = @_;
789  
790 0 ▶ croak "usage: \$imap->put_with_date(mailbox, message, date, \@flags)" unless defined $msg and defined $mailbox_name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $msg and defined $mailbox_name 0 0 0
Condition0%0/3
defined $msg and defined $mailbox_name
!ll&&!rl&&r
000
MC/DC0%0/2
defined $msg and defined $mailbox_name
defined $msg defined $mailbox_name
Truth table0%0/3
A and B
A= defined $msgB= defined $mailbox_name
ABresult
0X0
100
111
791  
792 0 my $size = length $msg;
793 0 ▶ if ( ref $msg eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg eq "ARRAY") 0 0 0
794 0 $size = 0;
795 0 $size += length $_ for @$msg;
796 }
797  
798 0 @flags = $self->_process_flags(@flags);
799  
800 0 my $cmd_str = _escape($mailbox_name) . " (@flags)";
801 0 ▶ $cmd_str .= " " . _escape($date) if $date ne "";
Branch: 1 branch
BranchTrueFalseTotal
if $date ne "" 0 0 0
802 0 $cmd_str .= " {$size}";
803  
804 return $self->_process_cmd(
805 cmd => [ APPEND => $cmd_str ],
806 0 ▶ final => sub { $self->_clear_cache; 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
807 0 ▶ process => sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
808 0 ▶ if( $_[0] =~ m/^\+\s+/ ) { # + continue (or go ahead, or whatever)
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\+\s+/) 0 0 0
809 0 ▶ if ($size) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size) 0 0 0
810 0 my $sock = $self->_sock;
811 0 ▶ if ( ref $msg eq "ARRAY" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg eq 'ARRAY') { } 0 0 0
812 0 print $sock $_ for @$msg;
813  
814 } else {
815 0 print $sock $msg;
816 }
817 0 $size = undef;
818 0 print $sock "\r\n";
819 }
820 }
821 },
822  
823 0 );
824 }
825  
826 0 ▶sub msg_flags {
Subroutine msg_flags: not called CC=1 SCAR=6.9
Pod documented
827 0 my ( $self, $number ) = @_;
828  
829 0 my @flags;
830 0 $self->{_waserr} = 1; # assume something went wrong.
831 0 $self->{_errstr} = "flags not found during fetch";
832  
833 # _send_cmd] 15 FETCH 12 (FLAGS)\r\n
834 # _process_cmd] * 12 FETCH (FLAGS (\Seen))\r\n
835 # _cmd_ok] * 12 FETCH (FLAGS (\Seen))\r\n
836 # _seterrstr] warning unknown return string (id=15): * 12 FETCH (FLAGS (\Seen))\r\n
837 # _process_cmd] 15 OK Success\r\n
838  
839 return $self->_process_cmd(
840 cmd => [ FETCH => qq[$number (FLAGS)] ],
841 0 ▶ final => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
842 0 ▶ return if $self->{_waserr};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_waserr"} 0 0 0
843 0 ▶ wantarray ? @flags : "@flags";
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
844 },
845 0 ▶ process => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
846 0 ▶ if( $_[0] =~ m/\* $number FETCH \(FLAGS \(([^()]*?)\)\)/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /\* $number FETCH \(FLAGS \(([^()]*?)\)\)/i) 0 0 0
847 0 @flags = $self->_process_flags($1);
848 0 delete $self->{_waserr};
849 }
850 },
851 0 );
852 }
853  
854 0 ▶sub getfh {
Subroutine getfh: not called CC=1 SCAR=6.9
Pod documented
855 0 my ( $self, $number ) = @_;
856  
857 0 my $file = IO::File->new_tmpfile;
858 0 my $buffer;
859  
860 return $self->_process_cmd(
861 cmd => [ FETCH => qq[$number RFC822] ],
862 0 ▶ final => sub { seek $file, 0, 0; $file },
Subroutine __ANON__: not called CC=1 SCAR=6.9
863 0 ▶ process => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
864 0 ▶ if ( $_[0] !~ /^\* \d+ FETCH/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $_[0] =~ /^\* \d+ FETCH/) 0 0 0
865 0 ▶ defined($buffer) and print $file $buffer;
Branch: 1 branch
BranchTrueFalseTotal
defined $buffer and print $file $buffer 0 0 0
866 0 $buffer = $_[0];
867 }
868 },
869 0 );
870 }
871  
872 0 ▶sub logout {
Subroutine logout: not called CC=1 SCAR=6.9
Pod documented
873 0 my $self = shift;
874  
875 0 ▶ return $self->_process_cmd( cmd => ['LOGOUT'], final => sub { $self->_sock->close; 1 }, process => sub { } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
876 }
877  
878 0 ▶sub quit {
Subroutine quit: not called CC=2 SCAR=17.9
Pod documented
879 0 my ( $self, $hq ) = @_;
880 0 $self->_send_cmd('EXPUNGE'); # XXX: $self->expunge_mailbox?
881  
882 0 ▶ if ( !$hq ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $hq) { } 0 0 0
883 # XXX: $self->logout?
884 0 ▶ $self->_process_cmd( cmd => ['LOGOUT'], final => sub { 1 }, process => sub { } );
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine __ANON__: not called CC=1 SCAR=6.9
885  
886 } else {
887 # XXX: do people use the $hq?
888 0 $self->_send_cmd('LOGOUT');
889 }
890  
891 0 $self->_sock->close;
892  
893 0 return 1;
894 }
895  
896 0 ▶sub _be_on_a_box {
Subroutine _be_on_a_box: not called CC=2 SCAR=17.9
897 0 my $self = shift;
898 0 ▶ return if $self->{working_box};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"working_box"} 0 0 0
899 0 $self->select; # sit on something
900 0 return;
901 }
902  
903 0 ▶sub last { ## no critic -- too late to choose a different name now...
Subroutine last: not called CC=3 SCAR=24.8
Pod documented
904 0 my $self = shift;
905 0 my $last = $self->_last;
906  
907 0 ▶ if( not defined $last ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $last) 0 0 0
908 0 ▶ $self->select or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->select or return 0 0 0
909 0 $last = $self->_last;
910 }
911  
912 0 return $last;
913 }
914  
915 0 ▶sub add_flags {
Subroutine add_flags: not called CC=2 SCAR=17.9
Pod documented
916 0 my ( $self, $number, @flags ) = @_;
917  
918 0 @flags = $self->_process_flags(@flags);
919 0 ▶ return unless @flags;
Branch: 1 branch
BranchTrueFalseTotal
unless @flags 0 0 0
920  
921 return $self->_process_cmd(
922 cmd => [ STORE => qq[$number +FLAGS (@flags)] ],
923 0 ▶ final => sub { $self->_clear_cache },
Subroutine __ANON__: not called CC=1 SCAR=6.9
924 0 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
925 0 );
926 }
927  
928 0 ▶sub sub_flags {
Subroutine sub_flags: not called CC=2 SCAR=17.9
Pod documented
929 0 my ( $self, $number, @flags ) = @_;
930  
931 0 @flags = $self->_process_flags(@flags);
932 0 ▶ return unless @flags;
Branch: 1 branch
BranchTrueFalseTotal
unless @flags 0 0 0
933  
934 return $self->_process_cmd(
935 cmd => [ STORE => qq[$number -FLAGS (@flags)] ],
936 0 ▶ final => sub { $self->_clear_cache },
Subroutine __ANON__: not called CC=1 SCAR=6.9
937 0 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
938 0 );
939 }
940  
941 0 ▶sub delete { ## no critic -- too late to choose a different name now...
Subroutine delete: not called CC=1 SCAR=6.9
Pod documented
942 0 my ( $self, $number ) = @_;
943  
944 0 return $self->add_flags( $number, '\Deleted' );
945 }
946  
947 0 ▶sub undelete {
Subroutine undelete: not called CC=1 SCAR=6.9
Pod documented
948 0 my ( $self, $number ) = @_;
949  
950 0 return $self->sub_flags( $number, '\Deleted' );
951 }
952  
953 0 ▶sub see {
Subroutine see: not called CC=1 SCAR=6.9
Pod documented
954 0 my ( $self, $number ) = @_;
955  
956 0 return $self->add_flags( $number, '\Seen' );
957 }
958  
959 0 ▶sub unsee {
Subroutine unsee: not called CC=1 SCAR=6.9
Pod documented
960 0 my ( $self, $number ) = @_;
961  
962 0 return $self->sub_flags( $number, '\Seen' );
963 }
964  
965 0 ▶sub _process_list {
Subroutine _process_list: not called CC=6 SCAR=37.4
966 0 my ( $self, $line ) = @_;
967 0 ▶ $self->_debug( caller, __LINE__, '_process_list', $line ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
968  
969 0 my @list;
970 0 ▶ if ( $line =~ /^\*\s+(LIST|LSUB).*\s+\{\d+\}\s*$/i ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^\*\s+(LIST|LSUB).*\s+\{\d+\}\s*$/i) { } 0 0 0
elsif ($line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/i or $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i) { } 0 0 0
971 0 chomp( my $res = $self->_sock->getline );
972  
973 0 $res =~ s/\r//;
974  
975 0 push @list, _escape($res);
976  
977 0 ▶ $self->_debug( caller, __LINE__, '_process_list', $res ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
978  
979 ▶ } elsif ( $line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/i || $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i ) {
Condition0%0/3
$line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/i or $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
$line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/i or $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i
$line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/i $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i
Truth table0%0/3
A or B
A= $line =~ /^\*\s+(LIST|LSUB).*\s+(\".*?\")\s*$/iB= $line =~ /^\*\s+(LIST|LSUB).*\s+(\S+)\s*$/i
ABresult
1X1
011
000
980 0 push @list, $2;
981 }
982  
983 0 return @list;
984 }
985  
986 0 ▶sub mailboxes {
Subroutine mailboxes: not called CC=3 SCAR=24.8
Pod documented
987 0 my ( $self, $box, $ref ) = @_;
988  
989 0 ▶ $ref ||= '""';
Condition0%0/2
$ref ||= "\"\""
l!l
00
MC/DC0%0/1
$ref ||= "\"\""
$ref
Truth table0%0/2
A
A= $ref
Aresult
11
00
990 0 my @list;
991 0 ▶ if ( !defined $box ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $box) 0 0 0
992  
993 # recurse, should probably follow
994 # RFC 2683: 3.2.1.1. Listing Mailboxes
995 return $self->_process_cmd(
996 cmd => [ LIST => qq[$ref *] ],
997 0 ▶ final => sub { map { _unescape($_) } @list },
Subroutine __ANON__: not called CC=1 SCAR=6.9
998 0 ▶ process => sub { push @list, $self->_process_list( $_[0] ); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
999 0 );
1000  
1001 }
1002  
1003 return $self->_process_cmd(
1004 cmd => [ LIST => qq[$ref $box] ],
1005 0 ▶ final => sub { map { _unescape($_) } @list },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1006 0 ▶ process => sub { push @list, $self->_process_list( $_[0] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1007 0 );
1008 }
1009  
1010 0 ▶sub mailboxes_subscribed {
Subroutine mailboxes_subscribed: not called CC=3 SCAR=24.8
Pod documented
1011 0 my ( $self, $box, $ref ) = @_;
1012  
1013 0 ▶ $ref ||= '""';
Condition0%0/2
$ref ||= "\"\""
l!l
00
MC/DC0%0/1
$ref ||= "\"\""
$ref
Truth table0%0/2
A
A= $ref
Aresult
11
00
1014  
1015 0 my @list;
1016 0 ▶ if ( !defined $box ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $box) 0 0 0
1017  
1018 # recurse, should probably follow
1019 # RFC 2683: 3.2.2. Subscriptions
1020 return $self->_process_cmd(
1021 cmd => [ LSUB => qq[$ref *] ],
1022 0 ▶ final => sub { map { _unescape($_) } @list },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1023 0 ▶ process => sub { push @list, $self->_process_list( $_[0] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1024 0 );
1025  
1026 }
1027  
1028 return $self->_process_cmd(
1029 cmd => [ LSUB => qq[$ref $box] ],
1030 0 ▶ final => sub { map { _unescape($_) } @list },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1031 0 ▶ process => sub { push @list, $self->_process_list( $_[0] ) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1032 0 );
1033 }
1034  
1035 0 ▶sub create_mailbox {
Subroutine create_mailbox: not called CC=1 SCAR=6.9
Pod documented
1036 0 my ( $self, $box ) = @_;
1037  
1038 return $self->_process_cmd(
1039 cmd => [ CREATE => _escape($box) ],
1040 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1041 0 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
1042 0 );
1043 }
1044  
1045 0 ▶sub expunge_mailbox {
Subroutine expunge_mailbox: not called CC=2 SCAR=17.9
Pod documented
1046 0 my ( $self, $box ) = @_;
1047  
1048 0 ▶ return if !$self->select($box);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->select($box) 0 0 0
1049  
1050 # C: A202 EXPUNGE
1051 # S: * 3 EXPUNGE
1052 # S: * 3 EXPUNGE
1053 # S: * 5 EXPUNGE
1054 # S: * 8 EXPUNGE
1055 # S: A202 OK EXPUNGE completed
1056  
1057 0 my @expunged;
1058 return $self->_process_cmd(
1059 cmd => ['EXPUNGE'],
1060 0 ▶ final => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
1061 0 $self->_clear_cache;
1062 0 ▶ return @expunged if wantarray; # don't return 0E0 if want array and we're empty
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
1063 0 ▶ return "0E0" unless @expunged;
Branch: 1 branch
BranchTrueFalseTotal
unless @expunged 0 0 0
1064 0 return @expunged;
1065 },
1066 0 ▶ process => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
1067 0 ▶ if( $_[0] =~ m/^\s*\*\s+(\d+)\s+EXPUNGE[\r\n]*$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0] =~ /^\s*\*\s+(\d+)\s+EXPUNGE[\r\n]*$/i) 0 0 0
1068 0 push @expunged, $1;
1069 }
1070 },
1071 0 );
1072 }
1073  
1074 0 ▶sub delete_mailbox {
Subroutine delete_mailbox: not called CC=1 SCAR=6.9
Pod documented
1075 0 my ( $self, $box ) = @_;
1076  
1077 return $self->_process_cmd(
1078 cmd => [ DELETE => _escape($box) ],
1079 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1080 0 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
1081 0 );
1082 }
1083  
1084 0 ▶sub rename_mailbox {
Subroutine rename_mailbox: not called CC=1 SCAR=6.9
Pod documented
1085 0 my ( $self, $old_box, $new_box ) = @_;
1086 0 my $o = _escape($old_box);
1087 0 my $n = _escape($new_box);
1088  
1089 return $self->_process_cmd(
1090 cmd => [ RENAME => qq[$o $n] ],
1091 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1092 0 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
1093 0 );
1094 }
1095  
1096 0 ▶sub folder_subscribe {
Subroutine folder_subscribe: not called CC=1 SCAR=6.9
Pod documented
1097 0 my ( $self, $box ) = @_;
1098 0 $self->select($box);
1099  
1100 return $self->_process_cmd(
1101 cmd => [ SUBSCRIBE => _escape($box) ],
1102 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1103 0 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
1104 0 );
1105 }
1106  
1107 0 ▶sub folder_unsubscribe {
Subroutine folder_unsubscribe: not called CC=1 SCAR=6.9
Pod documented
1108 0 my ( $self, $box ) = @_;
1109 0 $self->select($box);
1110  
1111 return $self->_process_cmd(
1112 cmd => [ UNSUBSCRIBE => _escape($box) ],
1113 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1114 0 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
1115 0 );
1116 }
1117  
1118 0 ▶sub copy {
Subroutine copy: not called CC=1 SCAR=6.9
Pod documented
1119 0 my ( $self, $number, $box ) = @_;
1120 0 my $b = _escape($box);
1121  
1122 return $self->_process_cmd(
1123 cmd => [ COPY => qq[$number $b] ],
1124 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1125 0 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
1126 0 );
1127 }
1128  
1129 0 ▶sub uidcopy {
Subroutine uidcopy: not called CC=1 SCAR=6.9
Pod documented
1130 0 my ( $self, $number, $box ) = @_;
1131 0 my $b = _escape($box);
1132  
1133 return $self->_process_cmd(
1134 cmd => [ 'UID COPY' => qq[$number $b] ],
1135 0 ▶ final => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
1136 0 ▶ process => sub { },
Subroutine __ANON__: not called CC=1 SCAR=0.0
1137 0 );
1138 }
1139  
1140 0 ▶sub waserr {
Subroutine waserr: not called CC=1 SCAR=6.9
Pod documented
1141 0 return $_[0]->{_waserr};
1142 }
1143  
1144 0 ▶sub errstr {
Subroutine errstr: not called CC=1 SCAR=6.9
Pod documented
1145 0 return $_[0]->{_errstr};
1146 }
1147  
1148 0 ▶sub _nextid { return ++$_[0]->{count} }
Subroutine _nextid: not called CC=1 SCAR=6.9
1149  
1150 0 ▶sub _escape {
Subroutine _escape: not called CC=1 SCAR=6.9
1151 0 my $val = shift;
1152 0 $val =~ s/\\/\\\\/g;
1153 0 $val =~ s/\"/\\\"/g;
1154 0 $val = "\"$val\"";
1155  
1156 0 return $val;
1157 }
1158  
1159 0 ▶sub _unescape {
Subroutine _unescape: not called CC=1 SCAR=6.9
1160 0 my $val = shift;
1161 0 $val =~ s/^"//g;
1162 0 $val =~ s/"$//g;
1163 0 $val =~ s/\\\"/\"/g;
1164 0 $val =~ s/\\\\/\\/g;
1165  
1166 0 return $val;
1167 }
1168  
1169 0 ▶sub _send_cmd {
Subroutine _send_cmd: not called CC=3 SCAR=24.8
1170 0 my ( $self, $name, $value ) = @_;
1171 0 my $sock = $self->_sock;
1172 0 my $id = $self->_nextid;
1173 0 ▶ my $cmd = "$id $name" . ( $value ? " $value" : "" ) . "\r\n";
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 0 0 0
1174  
1175 0 ▶ $self->_debug( caller, __LINE__, '_send_cmd', $cmd ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1176  
1177 0 { local $\; print $sock $cmd; }
1178  
1179 0 return ( $sock => $id );
1180 }
1181  
1182 0 ▶sub _cmd_ok {
Subroutine _cmd_ok: not called CC=6 SCAR=37.4
1183 0 my ( $self, $res ) = @_;
1184 0 my $id = $self->_count;
1185  
1186 0 ▶ $self->_debug( caller, __LINE__, '_cmd_ok', $res ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1187  
1188 0 ▶ if ( $res =~ /^$id\s+OK/i ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($res =~ /^$id\s+OK/i) { } 0 0 0
elsif ($res =~ /^$id\s+(?:NO|BAD)(?:\s+(.+))?/i) { } 0 0 0
elsif ($res =~ /^\*\s+/) { } 0 0 0
1189 0 return 1;
1190  
1191 } elsif ( $res =~ /^$id\s+(?:NO|BAD)(?:\s+(.+))?/i ) {
1192 0 ▶ $self->_seterrstr( $1 || 'unknown error' );
Condition0%0/2
$1 || 'unknown error'
l!l
00
MC/DC0%0/1
$1 || 'unknown error'
$1
Truth table0%0/2
A
A= $1
Aresult
11
00
1193 0 return 0;
1194  
1195 } elsif ( $res =~ m/^\*\s+/ ) {
1196  
1197 } else {
1198 0 $self->_seterrstr("warning unknown return string (id=$id): $res");
1199 }
1200  
1201 0 return;
1202 }
1203  
1204 0 ▶sub _read_multiline {
Subroutine _read_multiline: not called CC=11 SCAR=48.8
1205 0 my ( $self, $sock, $list, $count ) = @_;
1206  
1207 0 my @lines;
1208 0 my $read_so_far = 0;
1209  
1210 0 ▶ while ( $read_so_far < $count ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($read_so_far < $count) 0 0 0
1211 0 ▶ if( defined( my $line = $sock->getline ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $sock->getline)) { } 0 0 0
1212 0 $read_so_far += length( $line );
1213 0 push @lines, $line;
1214  
1215 } else {
1216 0 $self->_seterrstr( "error reading $count bytes from socket" );
1217 0 last;
1218 }
1219 }
1220  
1221 0 ▶ if( $list and $lines[-1] !~ m/\)[\x0d\x0a\s]*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($list and not $lines[-1] =~ /\)[\x0d\x0a\s]*$/) 0 0 0
Condition0%0/3
$list and not $lines[-1] =~ /\)[\x0d\x0a\s]*$/
!ll&&!rl&&r
000
MC/DC0%0/2
$list and not $lines[-1] =~ /\)[\x0d\x0a\s]*$/
$list not $lines[-1] =~ /\)[\x0d\x0a\s]*$/
Truth table0%0/3
A and not(B)
A= $listB= not $lines[-1] =~ /\)[\x0d\x0a\s]*$/
ABresult
0X0
100
111
1222 0 ▶ $self->_debug( caller, __LINE__, '_read_multiline', "Looking for ending parenthesis match..." ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1223  
1224 0 my $unmatched = 1;
1225 0 ▶ while( $unmatched ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($unmatched) 0 0 0
1226  
1227 0 ▶ if( defined( my $line = $sock->getline ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = $sock->getline)) { } 0 0 0
1228 0 push @lines, $line;
1229 0 ▶ $unmatched = 0 if $line =~ m/\)[\x0d\x0a\s]*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $line =~ /\)[\x0d\x0a\s]*$/ 0 0 0
1230  
1231 } else {
1232 0 $self->_seterrstr( "error reading $count bytes from socket" );
1233 0 last;
1234 }
1235  
1236 }
1237 }
1238  
1239 0 ▶ if ( $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"debug"}) 0 0 0
1240 0 my $count=0;
1241 0 ▶ for ( my $i = 0 ; $i < @lines ; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @lines) 0 0 0
1242 0 $count += length($lines[$i]);
1243 0 $self->_debug( caller, __LINE__, '_read_multiline', "[$i] ($count) $lines[$i]" );
1244 }
1245 }
1246  
1247 0 return @lines;
1248 }
1249  
1250 0 ▶sub _process_cmd {
Subroutine _process_cmd: not called CC=14 SCAR=53.5
1251 0 my ( $self, %args ) = @_;
1252 0 my ( $sock, $id ) = $self->_send_cmd( @{ $args{cmd} } );
1253  
1254 0 ▶ $args{process} = sub {} unless ref($args{process}) eq "CODE";
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"process"} eq "CODE" 0 0 0
1255 0 ▶ $args{final} = sub {} unless ref($args{final}) eq "CODE";
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
unless ref $args{"final"} eq "CODE" 0 0 0
1256  
1257 0 my $cb = $self->{readline_callback};
1258  
1259 0 my $res;
1260 0 ▶ while ( $res = $sock->getline ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res = $sock->getline) 0 0 0
1261 0 ▶ $cb->($res) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
1262 0 ▶ $self->_debug( caller, __LINE__, '_process_cmd', $res ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1263  
1264 0 ▶ if ( $res =~ /^\*.*\{(\d+)\}[\r\n]*$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($res =~ /^\*.*\{(\d+)\}[\r\n]*$/) { } 0 0 0
1265 0 my $count = $1;
1266 0 my $list;
1267  
1268 0 ▶ $list = 1 if($res =~ /\(/);
Branch: 1 branch
BranchTrueFalseTotal
if $res =~ /\(/ 0 0 0
1269  
1270 0 $args{process}->($res);
1271 0 foreach( $self->_read_multiline( $sock, $list, $count ) ) {
1272 0 ▶ $cb->($_) if $cb;
Branch: 1 branch
BranchTrueFalseTotal
if $cb 0 0 0
1273 0 $args{process}->($_)
1274 }
1275  
1276 } else {
1277 0 my $ok = $self->_cmd_ok($res);
1278 0 ▶ if ( defined($ok) && $ok == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $ok and $ok == 1) { } 0 0 0
elsif (defined $ok and not $ok) { } 0 0 0
Condition0%0/3
defined $ok and $ok == 1
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ok and $ok == 1
defined $ok $ok == 1
Truth table0%0/3
A and B
A= defined $okB= $ok == 1
ABresult
0X0
100
111
1279 0 return $args{final}->($res);
1280  
1281 ▶ } elsif ( defined($ok) && !$ok ) {
Condition0%0/3
defined $ok and not $ok
!ll&&!rl&&r
000
MC/DC0%0/2
defined $ok and not $ok
defined $ok not $ok
Truth table0%0/3
A and not(B)
A= defined $okB= not $ok
ABresult
0X0
100
111
1282 0 return;
1283  
1284 } else {
1285 0 $args{process}->($res);
1286 }
1287 }
1288 }
1289  
1290 0 return;
1291 }
1292  
1293 0 ▶sub _seterrstr {
Subroutine _seterrstr: not called CC=2 SCAR=17.9
1294 0 my ( $self, $err ) = @_;
1295  
1296 0 $self->{_errstr} = $err;
1297 0 ▶ $self->_debug( caller, __LINE__, '_seterrstr', $err ) if $self->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"debug"} 0 0 0
1298  
1299 0 return;
1300 }
1301  
1302 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
1303 0 my $this = shift;
1304 0 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
1305 0 $this->{debug} = shift;
1306 }
1307  
1308 0 return $this->{debug};
1309 }
1310  
1311 0 ▶sub _debug {
Subroutine _debug: not called CC=8 SCAR=42.8
1312 0 my ( $self, $package, $filename, $line, $dline, $routine, $str ) = @_;
1313  
1314 0 $str =~ s/\n/\\n/g;
1315 0 $str =~ s/\r/\\r/g;
1316 0 $str =~ s/\cM/^M/g;
1317  
1318 0 my $shortness = 30;
1319 0 my $elipsissn = $shortness-3;
1320 0 my $flen = length $filename;
1321  
1322 0 ▶ my $short_fname = ($flen > $shortness ? "..." . substr($filename, $flen - $elipsissn) : $filename);
Branch: 1 branch
BranchTrueFalseTotal
$flen > $shortness ? : 0 0 0
1323  
1324 0 $line = "[$short_fname line $line in sub $routine] $str\n";
1325  
1326 0 ▶ if( exists $self->{debug} and defined $self->{debug} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"debug"} and defined $self->{"debug"}) 0 0 0
Condition0%0/3
exists $self->{"debug"} and defined $self->{"debug"}
!ll&&!rl&&r
000
MC/DC0%0/2
exists $self->{"debug"} and defined $self->{"debug"}
exists $self->{"debug"} defined $self->{"debug"}
Truth table0%0/3
A and B
A= exists $self->{"debug"}B= defined $self->{"debug"}
ABresult
0X0
100
111
1327  
1328 0 ▶ if ( ref( $self->{debug} ) eq 'GLOB' ) {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $self->{'debug'} eq 'GLOB') { } 0 0 0
elsif ($self->{'debug'} eq 'warn') { } 0 0 0
elsif ($self->{'debug'} =~ /^file:(.+)/) { } 0 0 0
1329 0 print { $self->{debug} } $line;
1330  
1331 } elsif( $self->{debug} eq "warn" ) {
1332 0 warn $line;
1333  
1334 } elsif( $self->{debug} =~ m/^file:(.+)/ ) {
1335 0 ▶ open my $out, ">>", $1 or warn "[log io fail: $@] $line";
Branch: 1 branch
BranchTrueFalseTotal
open my $out, ">>", $1 or warn "[log io fail: $@] $line" 0 0 0
1336 0 print $out $line;
1337 0 CORE::close($out);
1338  
1339 } else {
1340 0 print STDOUT $line;
1341 }
1342  
1343 }
1344  
1345 0 return;
1346 }
1347  
1348 20 1;
↑ Summary SimpleX.pm »