blib/lib/Net/ManageSieve.pm

filter: Statementstmt 4.7% 22 / 464 Branchbran 0.0% 0 / 290 Conditioncond 0.0% 0 / 95 MC/DCmcdc 0.0% 0 / 56 Subroutinesub 12.5% 6 / 48 Podpod 74.0% 20 / 27 totaltotal 4.9% 48 / 980 CC 182 SCAR 52.4
CC
182
Cov
3%
new
702.0
auth
380.0
_token
380.0
CRAP 188.2
↑ Summary
1 package Net::ManageSieve;
2  
3 =head1 NAME
4  
5 Net::ManageSieve - ManageSieve Protocol Client
6  
7 =head1 SYNOPSIS
8  
9 use Net::ManageSieve;
10  
11 # Constructors
12 $sieve = Net::ManageSieve->new('localhost');
13 $sieve = Net::ManageSieve->new('localhost', Timeout => 60);
14  
15 =head1 DESCRIPTION
16  
17 This module implements a client interface to the ManageSieve protocol
18 (L<http://tools.ietf.org/html/draft-martin-managesieve-09>). This
19 documentation assumes that you are familiar with the concepts of the
20 protocol.
21  
22 A new Net::ManageSieve object must be created with the I<new> method. Once
23 this has been done, all ManageSieve commands are accessed through
24 this object.
25  
26 I<Note>: ManageSieve allows one to manipulate scripts on a host running a
27 ManageSieve service, this module does not perform, validate or something
28 like that Sieve scipts themselves.
29  
30 This module works in taint mode.
31  
32 =head1 EXAMPLES
33  
34 This example prints the capabilities of the server known as mailhost:
35  
36 #!/usr/local/bin/perl -w
37  
38 use Net::ManageSieve;
39  
40 $sieve = Net::ManageSieve->new('mailhost');
41 print "$k=$v\n" while ($k, $v) = each %{ $sieve->capabilities };
42 $sieve->logout;
43  
44 This example lists all storred scripts on the server and requires TLS:
45  
46 #!/usr/local/bin/perl -w
47  
48 use Net::ManageSieve;
49  
50 my $sieve = Net::ManageSieve->new('mailhost', tls => 'require')
51 or die "$@\n";
52 print "Cipher: ", $sieve->get_cipher(), "\n";
53 $sieve->login('user', 'password')
54 or die "Login: ".$sieve->error()."\n";
55 my $scripts = $sieve->listscripts
56 or die "List: ".$sieve->error()."\n";
57 my $activeScript = pop(@$scripts);
58 print "$_\n" for sort @$scripts;
59 print $activeScript
60 ? 'active script: ' . $activeScript
61 : 'no script active'
62 , "\n";
63 $sieve->logout;
64  
65 =head1 ERROR HANDLING
66  
67 By default all functions return C<undef> on failure and set an
68 error description into C<$@>, which can be retrieved with the
69 method C<error()> as well.
70  
71 The constructor accepts the setting C<on_fail>, which alters this
72 behaviour by changing the step to assign C<$@>:
73 If its value is:
74  
75 =over 4
76  
77 =item C<warn>
78  
79 the program carps the error description.
80  
81 If C<debug> is enabled, too, the description is printed twice.
82  
83 =item C<die>
84  
85 the program croaks.
86  
87 =item is a CODE ref
88  
89 this subroutine is called with the arguments:
90  
91 &code_ref ( $object, $error_message )
92  
93 The return value controls, whether or not the error message will be
94 assigned to C<$@>. Private functions may just signal that an error
95 occurred, but keep C<$@> unchanged. In this case C<$@> remains unchanged,
96 if code_ref returns true.
97  
98 I<Note>: Even if the code ref returns false, C<$@> might bi clobberred
99 by called modules. This is especially true in the C<new()> constructor.
100  
101 =item otherwise
102  
103 the default behaviour is retained by setting C<$@>.
104  
105 =back
106  
107 =cut
108  
109 2 require 5.001;
110  
111 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
112 2 ▶use vars qw($VERSION @ISA);
Subroutine BEGIN: called CC=1 SCAR=0.0
113 2 ▶use Socket 1.3;
Subroutine BEGIN: called CC=1 SCAR=0.0
114 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
115 2 ▶use IO::Socket;
Subroutine BEGIN: called CC=1 SCAR=0.0
116 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
117  
118 2 $VERSION = "0.13";
119  
120 2 @ISA = qw();
121  
122 =head1 CONSTRUCTOR
123  
124 =over 4
125  
126 =item new ( [ HOST ] [, OPTIONS ] )
127  
128 This is the constructor for a new Net::ManageSieve object. C<HOST> is the
129 name of the remote host to which an ManageSieve connection is required.
130  
131 C<HOST> is optional. If C<HOST> is not given then it may instead be
132 passed as the C<Host> option described below. If neither is given then
133 C<localhost> will be used.
134  
135 C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
136 Possible options are:
137  
138 B<Host> - ManageSieve host to connect to. It may be a single scalar,
139 as defined for the C<PeerAddr> option in L<IO::Socket::INET>, or a
140 reference to an array with hosts to try in turn. The L</host> method
141 will return the value which was used to connect to the host.
142  
143 B<LocalAddr> and B<LocalPort> - These parameters are passed directly
144 to IO::Socket to allow binding the socket to a local port.
145  
146 B<Timeout> - Maximum time, in seconds, to wait for a response from the
147 ManageSieve server (default: 120)
148  
149 B<Port> - Select a port on the remote host to connect to (default is 2000)
150  
151 B<Debug> or B<debug> - enable debugging if true (default OFF)
152  
153 I<Note>: All of the above options are passed through to L<IO::Socket::INET>.
154  
155 B<tls> - issue STARTTLS right after connect. If B<tls> is a HASH ref,
156 the mode is in member C<mode>, otherwise C<tls> itself is the mode and
157 an empty SSL option HASH is passed to L<starttls()>. The C<mode> may be
158 one of C<require> to fail, if TLS negotiation fails, or C<auto>,
159 C<on> or C<yes>, if TLS is to attempt, but a failure is ignored.
160 (Aliases: B<TLS>, B<Tls>)
161  
162 B<on_fail> - Changes the error handling of all functions that would
163 otherwise return undef and set C<$@>. See section ERROR HANDLING
164 (Aliases: B<On_fail>)
165  
166 Example:
167  
168 $sieve = Net::ManageSieve->new('mailhost',
169 Timeout => 30,
170 );
171  
172 use the first host one can connect to successfully C<mailhost> on port
173 C<2000>, the default port, then C<localhost> on port C<2008>.
174  
175 $sieve = Net::ManageSieve->new(Host => [ 'mailhost', 'localhost:2008' ],
176 Timeout => 30,
177 tls => {
178 mode => require,
179 SSL_ca_path => '/usr/ssl/cert',
180 }
181 );
182  
183 =back
184  
185 =cut
186  
187 0 ▶sub _decodeCap ($$) {
Subroutine _decodeCap: not called CC=7 SCAR=40.3
188 0 my $self = shift;
189 0 my $cap = shift;
190  
191 0 ▶ if(ref($cap) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cap eq "ARRAY") 0 0 0
192 0 $self->{capabilities} = { };
193 0 ▶ while(my $c = shift(@$cap)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $c = shift @$cap) 0 0 0
194 0 ▶ next if ref($c);
Branch: 1 branch
BranchTrueFalseTotal
if ref $c 0 0 0
195 0 $c = lc($c); # capability-name
196 0 my @v;
197 0 ▶ while(my $v = shift(@$cap)) { # quaff even multiple tokens
Branch: 1 branch
BranchTrueFalseTotal
if (my $v = shift @$cap) 0 0 0
198 0 ▶ last if ref($v);#CRLF # standard allows one
Branch: 1 branch
BranchTrueFalseTotal
if ref $v 0 0 0
199 0 push(@v, $v); # optional value
200 } # lasr CRLF had been quaffed by ok() already
201 0 ▶ $self->{capabilities}->{$c}
Branch: 1 branch
BranchTrueFalseTotal
scalar @v ? : 0 0 0
202 = scalar(@v)? join(',', @v)
203 : '0 but true';
204 }
205 }
206  
207 0 return $self;
208 }
209  
210 0 ▶sub new {
Subroutine new: not called CC=26 SCAR=65.5
Pod documented
211 0 my $self = shift;
212 0 ▶ my $type = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
213 0 $self = bless {}, $type;
214  
215 0 my ($host,%arg);
216 0 ▶ if(@_ % 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ % 2) { } 0 0 0
217 0 $host = shift ;
218 0 %arg = @_;
219 } else {
220 0 %arg = @_;
221 0 $host = delete $arg{Host};
222 }
223 0 ▶ $host ||= 'localhost';
Condition0%0/2
$host ||= "localhost"
l!l
00
MC/DC0%0/1
$host ||= "localhost"
$host
Truth table0%0/2
A
A= $host
Aresult
11
00
224 0 ▶ $arg{Proto} ||= 'tcp';
Condition0%0/2
$arg{"Proto"} ||= "tcp"
l!l
00
MC/DC0%0/1
$arg{"Proto"} ||= "tcp"
$arg{"Proto"}
Truth table0%0/2
A
A= $arg{"Proto"}
Aresult
11
00
225 0 ▶ $arg{Port} ||= 'managesieve(2000)';
Condition0%0/2
$arg{"Port"} ||= "managesieve(2000)"
l!l
00
MC/DC0%0/1
$arg{"Port"} ||= "managesieve(2000)"
$arg{"Port"}
Truth table0%0/2
A
A= $arg{"Port"}
Aresult
11
00
226 0 $arg{PeerPort} = $arg{Port};
227 0 ▶ $arg{Timeout} = 120 unless defined $arg{Timeout};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $arg{"Timeout"} 0 0 0
228 0 $self->{timeout} = $arg{Timeout};
229 0 $self->{_last_response} = 'OK no response, yet';
230 0 $self->{_last_error} = '';
231 0 $self->{_last_command} = '';
232 0 ▶ $self->{_debug} = 1 if $arg{Debug} || $arg{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $arg{"Debug"} or $arg{"debug"} 0 0 0
Condition0%0/3
$arg{"Debug"} or $arg{"debug"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$arg{"Debug"} or $arg{"debug"}
$arg{"Debug"} $arg{"debug"}
Truth table0%0/3
A or B
A= $arg{"Debug"}B= $arg{"debug"}
ABresult
1X1
011
000
233 0 ▶ $self->{_on_fail} = delete $arg{on_fail} || delete $arg{On_fail};
Condition0%0/3
delete $arg{'on_fail'} || delete $arg{'On_fail'}
l!l&&r!l&&!r
000
MC/DC0%0/2
delete $arg{'on_fail'} || delete $arg{'On_fail'}
delete $arg{'on_fail'} delete $arg{'On_fail'}
Truth table0%0/3
A || B
A= delete $arg{'on_fail'}B= delete $arg{'On_fail'}
ABresult
1X1
011
000
234 0 ▶ $self->{_tls} = delete $arg{tls} || delete $arg{Tls} || delete $arg{TLS};
Condition0%0/6
delete $arg{'tls'} || delete $arg{'Tls'} || delete $arg{'TLS'}
l!l&&r!l&&!r
000
delete $arg{"tls"} or delete $arg{"Tls"}
l!l&&r!l&&!r
000
MC/DC0%0/3
delete $arg{'tls'} || delete $arg{'Tls'} || delete $arg{'TLS'}
delete $arg{"tls"} delete $arg{"Tls"} delete $arg{'TLS'}
Truth table0%0/4
A or B || C
A= delete $arg{"tls"}B= delete $arg{"Tls"}C= delete $arg{'TLS'}
ABCresult
1XX1
01X1
0011
0000
235  
236 0 ▶ foreach my $h (@{ref($host) ? $host : [ $host ]}) {
Branch: 1 branch
BranchTrueFalseTotal
ref $host ? : 0 0 0
237 0 $arg{PeerAddr} = $h;
238 0 ▶ if($self->{fh} = IO::Socket::INET->new(%arg)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"fh"} = "IO::Socket::INET"->new(%arg)) 0 0 0
239 0 $self->{host} = $h;
240 0 last;
241 }
242 }
243  
244 0 ▶ unless(defined $self->{host}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"host"}) 0 0 0
245 0 my $err = $@;
246 0 ▶ $err = 'failed to connect to host(s): '.$! unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 0 0 0
247 0 $self->_set_error($err);
248 0 return undef;
249 }
250  
251 0 $self->{fh}->autoflush(1);
252  
253 # Read the capabilities
254 0 my $cap = $self->_response();
255 0 ▶ return undef unless $self->ok($cap);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ok($cap) 0 0 0
256 0 $self->_decodeCap($cap);
257  
258 0 ▶ if(my $mode = $self->{_tls}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mode = $self->{"_tls"}) 0 0 0
259 0 my $tls;
260 0 ▶ if(ref($mode) eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $mode eq 'HASH') { } 0 0 0
261 0 $tls = $mode;
262 0 ▶ $mode = delete $tls->{mode} || 'auto';
Condition0%0/2
delete $tls->{'mode'} || 'auto'
l!l
00
MC/DC0%0/1
delete $tls->{'mode'} || 'auto'
delete $tls->{'mode'}
Truth table0%0/2
A
A= delete $tls->{'mode'}
Aresult
11
00
263 } else {
264 0 $tls = { }; # no arguments
265 }
266  
267 0 ▶ if($mode && $mode =~ /\A(?:require|auto|yes|on|y)\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mode and $mode =~ /\A(?:require|auto|yes|on|y)\Z/) 0 0 0
Condition0%0/3
$mode and $mode =~ /\A(?:require|auto|yes|on|y)\Z/
!ll&&!rl&&r
000
MC/DC0%0/2
$mode and $mode =~ /\A(?:require|auto|yes|on|y)\Z/
$mode $mode =~ /\A(?:require|auto|yes|on|y)\Z/
Truth table0%0/3
A and B
A= $modeB= $mode =~ /\A(?:require|auto|yes|on|y)\Z/
ABresult
0X0
100
111
268 0 my $rc = $self->starttls(%$tls);
269 0 ▶ if(!$rc && $mode eq 'require') {
Branch: 1 branch
BranchTrueFalseTotal
if (not $rc and $mode eq "require") 0 0 0
Condition0%0/3
not $rc and $mode eq "require"
!ll&&!rl&&r
000
MC/DC0%0/2
not $rc and $mode eq "require"
not $rc $mode eq "require"
Truth table0%0/3
not(A) and B
A= not $rcB= $mode eq "require"
ABresult
0X0
100
111
270 0 my $err = $@;
271 0 ▶ $err = 'unknown error' unless defined $err;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $err 0 0 0
272 0 $self->_set_error('failed to enable TLS: '.$err);
273 0 return undef;
274 }
275 }
276 }
277  
278 0 return $self;
279 }
280  
281 =head1 METHODS
282  
283 Unless otherwise stated all methods return either a I<true> or I<false>
284 value, with I<true> meaning that the operation was a success. When
285 a method states that it returns a value, failure will be returned as
286 I<undef> or an empty list. The error is specified in C<$@> and can be
287 returned with the L</error> method. Please see section ERROR HANDLING
288 for an alternative error handling scheme.
289  
290 =over 4
291  
292 =item close ()
293  
294 Closes the connection to the server. Any already cached data is kept
295 active, though, there should be no pending data, if an user calls this
296 function.
297  
298 =cut
299  
300 0 ▶sub close {
Subroutine close: not called CC=2 SCAR=17.9
Pod documented
301 0 my $self = shift;
302 0 ▶ return undef unless $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fh"} 0 0 0
303 0 my $rc = $self->{fh}->close();
304 0 delete $self->{fh};
305 0 return $rc; # we keep locally cached data intentionally
306 }
307  
308 =item starttls ( %SSL_opts )
309  
310 Initiates a TLS session, may be used only before any
311 authentication.
312  
313 The C<SSL_opts> is a HASH containing any options you can
314 pass to L<< IO::Socket::SSL->new() >>. No one is passed by default.
315  
316 In order to detect in the later run, if the connection is encrypted,
317 use the C<encrypted()> function.
318  
319 Return: $self or C<undef> on failure - the socket is still
320 functioning, but is not encrypted.
321  
322 =cut
323  
324 0 ▶sub starttls {
Subroutine starttls: not called CC=7 SCAR=40.3
Pod documented
325 0 my $self = shift;
326 0 ▶ unless(scalar(@_) % 2 == 0) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar(@_) % 2 == 0) 0 0 0
327 0 $@ = 'The argument list must be a HASH';
328 0 return undef;
329 }
330 0 my %opts = @_;
331  
332 0 ▶ return undef unless $self->ok($self->_command("STARTTLS"));
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ok($self->_command("STARTTLS")) 0 0 0
333  
334 # Initiate TLS
335 0 ▶ unless(defined &IO::Socket::SSL::new) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &IO::Socket::SSL::new) 0 0 0
336 0 eval { require IO::Socket::SSL };
337 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
338 0 $self->_set_error('cannot find module IO::Socket::SSL', 'skipAd');
339 0 return undef;
340 }
341 }
342  
343 0 IO::Socket::SSL->start_SSL($self->{fh} , %opts);
344 # In-place upgrade of socket
345 0 ▶ return undef unless ref($self->{fh}) eq 'IO::Socket::SSL';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self->{"fh"} eq "IO::Socket::SSL" 0 0 0
346  
347 # success, state now is the same as right after connect
348 0 my $cap = $self->_response();
349 0 ▶ return undef unless $self->ok($cap);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ok($cap) 0 0 0
350 0 $self->_decodeCap($cap);
351  
352 0 return $self;
353 }
354  
355 =item encrypted ()
356  
357 Returns C<undef>, if the connection is not encrypted, otherwise
358 C<true>.
359  
360 =cut
361  
362 0 ▶sub encrypted {
Subroutine encrypted: not called CC=3 SCAR=24.8
Pod documented
363 0 my $fh = $_[0]->{fh};
364 0 ▶ return $fh && ref($fh) && $fh->isa('IO::Socket::SSL');
Condition0%0/6
$fh && ref $fh && $fh->isa('IO::Socket::SSL')
!ll&&!rl&&r
000
$fh and ref $fh
!ll&&!rl&&r
000
MC/DC0%0/3
$fh && ref $fh && $fh->isa('IO::Socket::SSL')
$fh ref $fh $fh->isa('IO::Socket::SSL')
Truth table0%0/4
A and B && C
A= $fhB= ref $fhC= $fh->isa('IO::Socket::SSL')
ABCresult
0XX0
10X0
1100
1111
365 }
366  
367  
368 =item get_cipher (), dump_peer_certificate (), peer_certificate ($field)
369  
370 Returns C<undef>, if the connection is not encrypted, otherwise
371 the functions directly calls the equally named function
372 of L<IO::Socket::SSL>.
373  
374 =cut
375  
376 0 ▶sub _encrypted {
Subroutine _encrypted: not called CC=3 SCAR=24.8
377 0 my $fh = $_[0]->{fh};
378 0 ▶ unless($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
379 0 $_[0]->_set_error('no connection opened');
380 0 return undef;
381 }
382 0 ▶ unless(encrypted($_[0])) {
Branch: 1 branch
BranchTrueFalseTotal
unless (encrypted($_[0])) 0 0 0
383 0 $_[0]->_set_error('connection not encrypted');
384 0 return undef;
385 }
386 0 return $fh;
387 }
388  
389 0 ▶sub get_cipher {
Subroutine get_cipher: not called CC=2 SCAR=17.9
Pod documented
390 0 ▶ return undef unless &_encrypted;
Branch: 1 branch
BranchTrueFalseTotal
unless &_encrypted 0 0 0
391 0 return $_[0]->{fh}->get_cipher();
392 }
393 0 ▶sub dump_peer_certificate {
Subroutine dump_peer_certificate: not called CC=2 SCAR=17.9
Pod documented
394 0 ▶ return undef unless &_encrypted;
Branch: 1 branch
BranchTrueFalseTotal
unless &_encrypted 0 0 0
395 0 return $_[0]->{fh}->dump_peer_certificate();
396 }
397 0 ▶sub peer_certificate {
Subroutine peer_certificate: not called CC=2 SCAR=17.9
Pod documented
398 0 ▶ return undef unless &_encrypted;
Branch: 1 branch
BranchTrueFalseTotal
unless &_encrypted 0 0 0
399 0 shift;
400 0 return $_[0]->{fh}->peer_certificate(@_);
401 }
402  
403 =item auth (USER [, PASSWORD [, AUTHNAME ] ])
404  
405 Authentificates as C<USER>.
406  
407 If the module L<Authen::SASL> is available, this module is tried first. In
408 this case, the C<USER> parameter may be a C<Authen::SASL> object, that
409 is not furtherly modified. If C<USER> is no C<Authen::SASL> object,
410 C<USER> is passed as C<user>, C<PASSWORD> as C<pass> and C<AUTHNAME>
411 as C<authname> to C<< Authen::SASL->new() >>. If C<AUTHNAME> is
412 undefined, C<USER> is passed as C<authname>. This way you can
413 authentificate against Cyrus: C<auth('cyrus', $password, $username)>.
414  
415 If L<Authen::SASL> is I<not> available or the initialization of it fails,
416 this function attempts to authentificate via the C<PLAIN> method.
417  
418 Aliases: C<login>, C<authentificate>.
419  
420 =cut
421  
422 0 ▶sub _encode_base64 {
Subroutine _encode_base64: not called CC=4 SCAR=30.0
423 0 my $self = shift;
424  
425 0 ▶ unless(defined &MIME::Base64::encode_base64) { # Automatically load it
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &MIME::Base64::encode_base64) 0 0 0
426 0 eval { require MIME::Base64; };
427 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
428 0 $self->_set_error('failed to load MIME::Base64: ' . $@);
429 0 return undef;
430 }
431 }
432  
433 0 my $r = &MIME::Base64::encode_base64;
434 0 ▶ $r and $r =~ s/[\s\r\n]+$//s;
Branch: 1 branch
BranchTrueFalseTotal
$r and $r =~ s/[\s\r\n]+$//s 0 0 0
435 0 return $r;
436 }
437 0 ▶sub auth {
Subroutine auth: not called CC=19 SCAR=59.4
Pod documented
438 0 my ($self, $username, $password, $authname) = @_;
439
440 0 ▶ if(my $mech = $self->{capabilities}{sasl}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $mech = $self->{"capabilities"}{"sasl"}) 0 0 0
441 # If the server does not announce SASL, we try PLAIN anyway
442 0 my $doSASL = 1;
443 0 ▶ unless(defined &Authen::SASL::new) { # Automatically load it
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Authen::SASL::new) 0 0 0
444 0 eval { require Authen::SASL; };
445 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
446 0 $self->_set_error("failed to load Authen::SASL: $@\nFallback to PLAIN\n");
447 0 $doSASL = undef;
448 }
449 }
450 0 ▶ if($doSASL) {
Branch: 1 branch
BranchTrueFalseTotal
if ($doSASL) 0 0 0
451 0 my $sasl;
452 0 ▶ if(ref($username) && UNIVERSAL::isa($username, 'Authen::SASL')) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $username and UNIVERSAL::isa($username, 'Authen::SASL')) { } 0 0 0
Condition0%0/3
ref $username and UNIVERSAL::isa($username, "Authen::SASL")
!ll&&!rl&&r
000
MC/DC0%0/2
ref $username and UNIVERSAL::isa($username, "Authen::SASL")
ref $username UNIVERSAL::isa($username, "Authen::SASL")
Truth table0%0/3
A and B
A= ref $usernameB= UNIVERSAL::isa($username, "Authen::SASL")
ABresult
0X0
100
111
453 0 $sasl = $username;
454 # $sasl->mechanism($mech);
455 } else {
456 0 ▶ unless(length $username) {
Branch: 1 branch
BranchTrueFalseTotal
unless (length $username) 0 0 0
457 0 $self->_set_error("need username or Authen::SASL object");
458 0 return undef;
459 }
460 0 ▶ unless(defined $authname) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $authname) 0 0 0
461 0 $authname = $username;
462 }
463 # for unknown reason to pass in a space
464 # separated string leads to the problem
465 # that $client->mechnism returns the same
466 # string, but ought to return the _used_
467 # mechnism only therefore, we use the
468 # first one of the list
469 # 2008-04-25 ska
470 0 $mech =~ s/\s.*//;
471 # $mech = "LOGIN";
472 0 $sasl = Authen::SASL->new(mechanism=> "".$mech, # without "". the behaviour is funny
473 callback => { user => $username,
474 pass => $password,
475 password => $password, # needed it to work properly
476 authname => $authname,
477 }
478 );
479 }
480  
481 # draft-martin-managesieve-08: service := 'sieve'
482 0 my $client = $sasl->client_new('sieve', $self->{host}, 0);
483 # I did understood the documentation that way that
484 # 'undef' means error, this is wrong. client_start() returns
485 # undef for no initial client response.
486 0 my $msg = $client->client_start;
487 0 ▶ if($client->mechanism) {
Branch: 1 branch
BranchTrueFalseTotal
if ($client->mechanism) 0 0 0
488 0 ▶ if($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) { } 0 0 0
489 return undef
490 0 ▶ unless defined($msg = $self->_encode_base64($msg,''));
Branch: 1 branch
BranchTrueFalseTotal
unless defined($msg = $self->_encode_base64($msg, "")) 0 0 0
491 0 $msg = ' "' . $msg . '"';
492 } else {
493 0 $msg = ''; # Empty initial request
494 # Force to load MIME::Encode
495 0 ▶ return undef unless defined $self->_encode_base64('z');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->_encode_base64("z") 0 0 0
496 }
497 # Initial response
498 0 ▶ $self->_send_command(
Branch: 1 branch
BranchTrueFalseTotal
$self->_send_command('Authenticate "' . $client->mechanism . '"' . $msg) or return undef 0 0 0
499 'Authenticate "'. $client->mechanism . '"' . $msg)
500 or return undef;
501 0 ▶ while($msg = $self->_token()) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg = $self->_token) 0 0 0
502 0 ▶ if(ref($msg)) { # end of command received OK|NO
Branch: 1 branch
BranchTrueFalseTotal
if (ref $msg) 0 0 0
503 0 ▶ next if $msg->[0] eq "\n"; #CRLF is a token
Branch: 1 branch
BranchTrueFalseTotal
if $msg->[0] eq "\n" 0 0 0
504 0 $msg = $self->ok([ $msg ]);
505 0 last;
506 }
507 # MIME::Base64 is definitely loaded here
508 $self->_write(
509 0 '"' . $self->_encode_base64(
510 $client->client_step(
511 MIME::Base64::decode_base64($msg)
512 ), ''
513 ) . "\"\r\n"
514 );
515 }
516  
517 0 ▶ return $msg if $msg;
Branch: 1 branch
BranchTrueFalseTotal
if $msg 0 0 0
518 0 $self->_set_error('SASL authentification failed');
519 0 return undef;
520 }
521 0 $self->_set_error("start of SASL failed");
522 # Circumvent SASL problems by falling back to plain PLAIN
523 }
524 }
525  
526 0 my $r = $self->_encode_base64(
527 join("\0", ($username, $username, $password))
528 , '');
529 0 ▶ return undef unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 0 0 0
530 0 return $self->ok($self->_command('Authenticate "PLAIN" "'.$r.'"'));
531 }
532 0 ▶sub login { goto &auth; }
Subroutine login: not called CC=1 SCAR=6.9
Pod undocumented
533 0 ▶sub authentificate { goto &auth; }
Subroutine authentificate: not called CC=1 SCAR=6.9
Pod undocumented
534  
535 =item logout ()
536  
537 Sends the C<LOGOUT> command to the server and closes the
538 connection to the server.
539  
540 Aliases: C<quit>, C<bye>.
541  
542 =cut
543  
544 0 ▶sub logout {
Subroutine logout: not called CC=2 SCAR=17.9
Pod documented
545 0 my ($self) = @_;
546  
547 0 ▶ return 1 unless $self->{fh};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"fh"} 0 0 0
548 0 my $rc = $self->_command("LOGOUT");
549 0 $self->close();
550 0 return $self->ok($rc, 'bye');
551 }
552 0 ▶sub quit { goto &logout; }
Subroutine quit: not called CC=1 SCAR=6.9
Pod undocumented
553 0 ▶sub bye { goto &logout; }
Subroutine bye: not called CC=1 SCAR=6.9
Pod undocumented
554  
555 =item host ()
556  
557 Returns the remote host of the connection.
558  
559 =cut
560  
561 0 ▶sub host {
Subroutine host: not called CC=1 SCAR=6.9
Pod documented
562 0 my ($self) = @_;
563  
564 0 return $self->{host};
565 }
566  
567 =item capabilities ([reget])
568  
569 Returns the capabilities as HASH ref, e.g.:
570  
571 {
572 'starttls' => 1,
573 'sasl' => 'PLAIN LOGIN',
574 'implementation' => 'dovecot',
575 'sieve' => 'fileinto reject envelope vacation imapflags notify subaddress relational comparator-i;ascii-numeric regex'
576 };
577  
578 If the argument C<bool> is specified and is boolean C<TRUE>,
579 the capabilities are reaquired from the server using
580 the I<CAPABILITY> command.
581 Note: The initial capabilities may be different from the set
582 acquired later.
583  
584 =cut
585  
586 0 ▶sub capabilities {
Subroutine capabilities: not called CC=4 SCAR=30.0
Pod documented
587 0 my ($self, $reget) = @_;
588  
589 0 ▶ if($reget) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reget) 0 0 0
590 0 ▶ my $cap = $self->_command("CAPABILITY") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $cap = $self->_command("CAPABILITY") or return undef 0 0 0
591 0 ▶ return undef unless $self->ok($cap);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->ok($cap) 0 0 0
592 0 $self->_decodeCap($cap);
593 }
594 0 return $self->{capabilities};
595 }
596  
597 =item havespace (NAME, SIZE)
598  
599 Return whether or not a script with the specified size (and name)
600 might fit into the space of the user on the server.
601  
602 Due to various reasons, the result of this function is not very
603 reliable, because in the meantime lots of changes may take place
604 on the server.
605  
606 =cut
607  
608 0 ▶sub havespace {
Subroutine havespace: not called CC=3 SCAR=24.8
Pod documented
609 0 my ($self, $name, $size) = @_;
610  
611 0 ▶ unless($size =~ /\A\d+\Z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($size =~ /\A\d+\Z/) 0 0 0
612 0 $self->_set_error("size is not numeric: $size");
613 0 return undef;
614 }
615 0 ▶ return undef unless $name = $self->_chkName($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name = $self->_chkName($name) 0 0 0
616 0 return $self->ok($self->_command("HAVESPACE $name $size"));
617 }
618  
619 =item putscript (NAME, SCRIPT)
620  
621 Stores the C<SCRIPT> as name C<NAME> on the server, the script
622 is I<not> activated by default. C<SCRIPT> is a scalar in UTF-8.
623  
624 The script must not be empty.
625  
626 =cut
627  
628 0 ▶sub putscript {
Subroutine putscript: not called CC=3 SCAR=24.8
Pod documented
629 0 my ($self, $name, $script) = @_;
630  
631 0 $script = Encode::encode_utf8($script); # need octets
632 0 ▶ $script .= "\n" unless $script =~ /\n\Z/;
Branch: 1 branch
BranchTrueFalseTotal
unless $script =~ /\n\Z/ 0 0 0
633 0 ▶ return undef unless $name = $self->_chkName($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name = $self->_chkName($name) 0 0 0
634 0 return $self->ok($self->_command("PUTSCRIPT $name "
635 . _literal($script)));
636 }
637  
638 =item listscripts ()
639  
640 returns an ARRAY ref of the names of the scripts.
641  
642 The last entry in the list, specifies the active script, it is
643 an empty string C<"">, if there is none.
644  
645 e.g.:
646  
647 [ "script1",
648 "script2",
649 "script1"
650 ]
651  
652 means that C<script1> is active currently.
653  
654 =cut
655  
656 0 ▶sub listscripts {
Subroutine listscripts: not called CC=6 SCAR=37.4
Pod documented
657 0 my ($self) = @_;
658  
659 0 ▶ my $r = $self->_command("LISTSCRIPTS") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $r = $self->_command("LISTSCRIPTS") or return undef 0 0 0
660  
661 0 ▶ unless(my $rc = $self->ok($r)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (my $rc = $self->ok($r)) 0 0 0
662 0 return $rc;
663 }
664  
665 0 my $c = [ ];
666 0 my $act = ''; # Default: no active script
667 0 my $last;
668 0 for(@$r) {
669 0 ▶ if(ref($_)) { # active flag or CRLF
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_) { } 0 0 0
670 0 ▶ $act = $last if $_->[0] eq 'ACTIVE';
Branch: 1 branch
BranchTrueFalseTotal
if $_->[0] eq "ACTIVE" 0 0 0
671 } else {
672 0 push(@$c, $last = $_);
673 }
674 }
675  
676 0 push(@$c, $act);
677  
678 0 return $c;
679 }
680  
681 =item setactive (NAME)
682  
683 Activates the script named C<NAME>.
684  
685 =cut
686  
687 0 ▶sub setactive {
Subroutine setactive: not called CC=2 SCAR=17.9
Pod documented
688 0 my ($self, $name) = @_;
689  
690 0 ▶ return undef unless $name = $self->_chkName($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name = $self->_chkName($name) 0 0 0
691 0 return $self->ok($self->_command("SETACTIVE $name"));
692 }
693  
694 =item getscript (NAME)
695  
696 Returns the named script. The contents is in perl-internal UTF8.
697  
698 =cut
699  
700 0 ▶sub getscript {
Subroutine getscript: not called CC=4 SCAR=30.0
Pod documented
701 0 my ($self, $name) = @_;
702  
703 0 ▶ return undef unless $name = $self->_chkName($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name = $self->_chkName($name) 0 0 0
704 0 ▶ my $r = $self->_command("GETSCRIPT $name") or return undef;
Branch: 1 branch
BranchTrueFalseTotal
my $r = $self->_command("GETSCRIPT $name") or return undef 0 0 0
705  
706 0 ▶ if($self->ok($r)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->ok($r)) 0 0 0
707 0 my $l = join("\n", @$r);
708 0 $l =~ s/[\s\r\n]+\Z//s;
709 0 $l .= "\n";
710 0 return $l;
711 }
712 0 return undef;
713 }
714  
715 =item deletescript (NAME)
716  
717 Deletes the script named C<NAME>.
718  
719 =cut
720  
721 0 ▶sub deletescript {
Subroutine deletescript: not called CC=2 SCAR=17.9
Pod documented
722 0 my ($self, $name) = @_;
723  
724 0 ▶ return undef unless $name = $self->_chkName($name);
Branch: 1 branch
BranchTrueFalseTotal
unless $name = $self->_chkName($name) 0 0 0
725 0 return $self->ok($self->_command("DELETESCRIPT $name"));
726 }
727  
728 =item error ()
729  
730 Returns the locally cached error information in the form:
731  
732 error description respn=last server response
733  
734 =cut
735  
736 0 ▶sub error {
Subroutine error: not called CC=1 SCAR=6.9
Pod documented
737 0 my ($self) = @_;
738  
739 return $self->{_last_error} . '; cmd=' . $self->{_last_command}
740 0 . '; rspn=' . join(' ', @{ $self->{_last_response} });
741 }
742  
743 =begin COMMENT
744  
745 arg1 :- error string, always != undef
746 arg2 :- if passed, but not true: DO NOT assign $@
747  
748 See ERROR HANDLING about C<_on_fail>
749  
750 =end COMMENT
751  
752 =cut
753  
754 0 ▶sub _set_error {
Subroutine _set_error: not called CC=12 SCAR=50.5
755 0 my ($self, $err, $Ad) = @_;
756  
757 0 ▶ dbgPrint('ERROR:', $err) if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
758 0 $self->{_last_error} = $err;
759 0 ▶ my $assignAd = !defined $Ad || $Ad;
Condition0%0/3
!defined($Ad) || $Ad
l!l&&r!l&&!r
000
MC/DC0%0/2
!defined($Ad) || $Ad
!defined($Ad) $Ad
Truth table0%0/3
not(A) || B
A= !defined($Ad)B= $Ad
ABresult
1X1
011
000
760 0 ▶ my $op = $self->{_on_fail} if exists $self->{_on_fail};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"_on_fail"} 0 0 0
761 0 ▶ if(defined($op) && ref($op) eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if (defined $op and ref $op eq 'CODE') { } 0 0 0
elsif (defined $op and $op eq 'warn') { } 0 0 0
elsif (defined $op and $op eq 'die') { } 0 0 0
Condition0%0/3
defined $op and ref $op eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $op and ref $op eq "CODE"
defined $op ref $op eq "CODE"
Truth table0%0/3
A and B
A= defined $opB= ref $op eq "CODE"
ABresult
0X0
100
111
762 0 ▶ $assignAd &&= $op->($self, $err);
Condition0%0/3
$assignAd &&= &$op($self, $err)
!ll&&!rl&&r
000
MC/DC0%0/2
$assignAd &&= &$op($self, $err)
$assignAd &$op($self, $err)
Truth table0%0/3
A &&= B
A= $assignAdB= &$op($self, $err)
ABresult
0X0
100
111
763 ▶ } elsif(defined($op) && $op eq 'warn') {
Condition0%0/3
defined $op and $op eq "warn"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $op and $op eq "warn"
defined $op $op eq "warn"
Truth table0%0/3
A and B
A= defined $opB= $op eq "warn"
ABresult
0X0
100
111
764 0 Carp::carp $err;
765 ▶ } elsif(defined($op) && $op eq 'die') {
Condition0%0/3
defined $op and $op eq "die"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $op and $op eq "die"
defined $op $op eq "die"
Truth table0%0/3
A and B
A= defined $opB= $op eq "die"
ABresult
0X0
100
111
766 0 Carp::croak $err."\n";
767 # } else {
768 }
769 0 ▶ $@ = $err if $assignAd;
Branch: 1 branch
BranchTrueFalseTotal
if $assignAd 0 0 0
770  
771 0 return $self;
772 }
773  
774 =item debug ( [state] )
775  
776 Returns the current state of debugging.
777  
778 If C<state> is given, the boolean value enables or
779 disables debugging.
780  
781 =cut
782  
783 0 ▶sub debug {
Subroutine debug: not called CC=3 SCAR=24.8
Pod documented
784 0 my ($self, $state) = @_;
785  
786 0 my $rc = $self->{_debug};
787 0 ▶ if(defined $state) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $state) 0 0 0
788 0 ▶ if($state) {
Branch: 1 branch
BranchTrueFalseTotal
if ($state) { } 0 0 0
789 0 $self->{_debug} = 1;
790 } else {
791 0 delete $self->{_debug};
792 }
793 }
794  
795 0 return $rc;
796 }
797  
798 =begin COMMENT
799  
800 arg1 :- Prefix
801 arg2 :- passed through Data::Dumper
802  
803 =end COMMENT
804  
805 =cut
806  
807 0 ▶sub dbgPrint {
Subroutine dbgPrint: not called CC=7 SCAR=40.3
Pod undocumented
808 0 my ($prefix, $msg) = @_;
809  
810 0 my $txt = '';
811 0 ▶ $txt .= $prefix . ' ' if $prefix;
Branch: 1 branch
BranchTrueFalseTotal
if $prefix 0 0 0
812 0 ▶ MANGLE: { if($msg) {
Branch: 1 branch
BranchTrueFalseTotal
if ($msg) 0 0 0
813 0 ▶ unless(defined &Data::Dumper::Dump) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &Data::Dumper::Dump) 0 0 0
814 0 eval "require Data::Dumper";
815 0 ▶ if($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
816 0 $txt .= 'Failed to require Data::Dumper';
817 0 last MANGLE;
818 }
819 }
820 0 my $d = Data::Dumper->new([$msg], []);
821 0 $txt .= $d->Indent(1)->Terse(1)->Sortkeys(1)->Dump;
822 } }
823  
824 0 ▶ if($txt) {
Branch: 1 branch
BranchTrueFalseTotal
if ($txt) 0 0 0
825 0 print STDERR $txt;
826 0 ▶ print STDERR "\n" unless $txt =~ /\n$/s;
Branch: 1 branch
BranchTrueFalseTotal
unless $txt =~ /\n$/s 0 0 0
827 }
828 }
829  
830 #############################
831  
832  
833 0 ▶sub _literal {
Subroutine _literal: not called CC=2 SCAR=17.9
834 0 my $str = shift;
835 0 ▶ $str = shift if ref($str);
Branch: 1 branch
BranchTrueFalseTotal
if ref $str 0 0 0
836  
837 # [1] We send non-synchronizing literals: "+}CRLF"
838 0 return '{' . length($str) . '+}' . "\r\n" . $str;
839 }
840  
841 0 ▶sub _chkName {
Subroutine _chkName: not called CC=4 SCAR=30.0
842 0 my $self = shift;
843 0 my $name = shift;
844  
845 0 ▶ if(ref($name)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $name) 0 0 0
846 0 $self->_set_error("need a scalar");
847 0 return undef;
848 }
849  
850 0 ▶ if($name =~ /[\0\r\n]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /[\0\r\n]/) 0 0 0
851 0 $self->_set_error("invalid character in name");
852 0 return undef;
853 }
854  
855 0 ▶ return _literal($name) if $name =~ /[\\"[:cntrl:]]/;
Branch: 1 branch
BranchTrueFalseTotal
if $name =~ /[\\"[:cntrl:]]/ 0 0 0
856 0 return '"' . $name . '"';
857 }
858  
859 0 ▶sub _send_command {
Subroutine _send_command: not called CC=2 SCAR=17.9
860 0 my ($self, $cmd) = @_;
861  
862 0 ▶ if($cmd =~ /\A(\S+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($cmd =~ /\A(\S+)/) 0 0 0
863 0 $self->{_last_command} = $1;
864 }
865 0 $self->_write($cmd."\r\n");
866 }
867 0 ▶sub _command {
Subroutine _command: not called CC=2 SCAR=17.9
868 0 ▶ return &_send_command && &_response;
Condition0%0/3
&_send_command && &_response
!ll&&!rl&&r
000
MC/DC0%0/2
&_send_command && &_response
&_send_command &_response
Truth table0%0/3
A && B
A= &_send_commandB= &_response
ABresult
0X0
100
111
869 }
870  
871 0 ▶sub ok {
Subroutine ok: not called CC=13 SCAR=52.0
Pod undocumented
872 0 my $self = shift;
873 0 my $c = shift;
874 0 my $okRsp = shift;
875 0 my $cmt;
876  
877 0 ▶ return undef unless defined $c;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $c 0 0 0
878  
879 0 ▶ if(ref($c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $c) 0 0 0
880 0 ▶ unless(ref($c) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $c eq "ARRAY") 0 0 0
881 0 $self->_set_error("response code must be ARRAY ref or SCALAR");
882 0 return undef;
883 }
884 0 my $cx = pop(@$c);
885 0 ▶ if(defined(my $cy = pop(@$c))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $cy = pop @$c)) 0 0 0
886 0 ▶ push(@$c, $cy) # Remove the CRLF preceeding the OK/NO
Branch: 1 branch
BranchTrueFalseTotal
if ref $cy ne "ARRAY" or $cy->[0] ne "\n" 0 0 0
Condition0%0/3
ref $cy ne "ARRAY" or $cy->[0] ne "\n"
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $cy ne "ARRAY" or $cy->[0] ne "\n"
ref $cy ne "ARRAY" $cy->[0] ne "\n"
Truth table0%0/3
A or B
A= ref $cy ne "ARRAY"B= $cy->[0] ne "\n"
ABresult
1X1
011
000
887 if ref($cy) ne 'ARRAY' || $cy->[0] ne "\n";
888 }
889 0 ▶ if(ref($cx) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $cx eq 'ARRAY') { } 0 0 0
890 0 $cmt = join("; ", @$cx);
891 0 $c = $cx->[0];
892 } else {
893 0 $c = $cx;
894 }
895 }
896 0 ▶ $cmt ||= $c;
Condition0%0/3
$cmt ||= $c
l!l&&r!l&&!r
000
MC/DC0%0/2
$cmt ||= $c
$cmt $c
Truth table0%0/3
A ||= B
A= $cmtB= $c
ABresult
1X1
011
000
897  
898 0 ▶ unless($c =~ /\A(OK|NO|BYE)\b/i) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($c =~ /\A(OK|NO|BYE)\b/i) 0 0 0
899 0 $self->_set_error("invalid response: $cmt");
900 0 return undef;
901 }
902 0 ▶ return $c if uc($1) eq 'OK'
Branch: 1 branch
BranchTrueFalseTotal
if uc $1 eq "OK" or defined $okRsp and uc $okRsp eq uc $1 0 0 0
Condition0%0/6
uc $1 eq "OK" or defined $okRsp and uc $okRsp eq uc $1
l!l&&r!l&&!r
000
defined $okRsp and uc $okRsp eq uc $1
!ll&&!rl&&r
000
MC/DC0%0/3
uc $1 eq "OK" or defined $okRsp and uc $okRsp eq uc $1
uc $1 eq "OK" defined $okRsp uc $okRsp eq uc $1
Truth table0%0/4
A or B and C
A= uc $1 eq "OK"B= defined $okRspC= uc $okRsp eq uc $1
ABCresult
1XX1
0111
00X0
0100
903 || defined($okRsp) && uc($okRsp) eq uc($1); # e.g. LOGOUT gets BYE
904  
905 0 $self->_set_error("command failed with '$cmt'");
906 0 return 0; # Failed
907 }
908  
909 0 ▶sub _write {
Subroutine _write: not called CC=8 SCAR=42.8
910 0 my ($self, $l) = @_;
911  
912 0 my $fh = $self->{fh};
913 0 ▶ unless($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
914 0 $self->_set_error("no connection open");
915 0 return undef;
916 }
917  
918 0 my $len = length($l);
919 0 ▶ dbgPrint('WRITE:', $l) if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
920 0 ▶ local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MacOS" 0 0 0
921 0 my $in = ''; # For select
922 0 vec($in, fileno($fh), 1) = 1;
923 0 ▶ my $timeout = $self->{timeout} || undef;
Condition0%0/2
$self->{'timeout'} || undef
l!l
00
MC/DC0%0/1
$self->{'timeout'} || undef
$self->{'timeout'}
Truth table0%0/2
A
A= $self->{'timeout'}
Aresult
11
00
924 0 my $offset = 0;
925  
926 0 ▶ while($len) {
Branch: 1 branch
BranchTrueFalseTotal
if ($len) 0 0 0
927 0 ▶ if(select(undef, $in, undef, $timeout) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (select(undef, $in, undef, $timeout) > 0) { } 0 0 0
928 0 my $w = syswrite($fh, $l, $len, $offset);
929 0 ▶ unless(defined($w)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $w) 0 0 0
930 0 $self->_set_error("write failed: $!");
931 0 return undef;
932 }
933 0 $len -= $w;
934 0 $offset += $w;
935 } else {
936 0 $self->_set_error("write timeout");
937 0 return undef;
938 }
939 }
940  
941 0 return 1;
942 }
943  
944 0 ▶sub _response {
Subroutine _response: not called CC=4 SCAR=30.0
945 0 my ($self) = @_;
946  
947 0 my $l = [];
948 GET_LOOP: {
949 0 ▶ defined(my $r = $self->_token()) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
defined(my $r = $self->_token) or return undef 0 0 0
950 0 push(@$l, $r);
951 redo GET_LOOP
952 0 ▶ if !ref($r) # string
Branch: 1 branch
BranchTrueFalseTotal
if not ref $r or $r->[0] =~ /^(?:\n|active)$/i 0 0 0
Condition0%0/3
not ref $r or $r->[0] =~ /^(?:\n|active)$/i
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $r or $r->[0] =~ /^(?:\n|active)$/i
not ref $r $r->[0] =~ /^(?:\n|active)$/i
Truth table0%0/3
not(A) or B
A= not ref $rB= $r->[0] =~ /^(?:\n|active)$/i
ABresult
1X1
011
000
953 || $r->[0] =~ /^(?:\n|active)$/i # CRLF; ACTIVE: LISTSCRIPTS
954 ;
955 # OK|NO|BYE -> end loop
956 }
957  
958 0 return $l;
959 }
960  
961 0 ▶sub _unget {
Subroutine _unget: not called CC=3 SCAR=24.8
962 0 my $self = shift;
963  
964 0 ▶ return $self unless scalar @_;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 0 0 0
965  
966 0 ▶ unless(ref($self->{line_buffer}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'line_buffer'} eq 'ARRAY') { } 0 0 0
967 0 $self->{line_buffer} = [ @_ ];
968 } else {
969 0 unshift @{$self->{line_buffer}}, @_;
970 }
971 0 return $self;
972 }
973  
974 0 ▶sub _getline {
Subroutine _getline: not called CC=12 SCAR=50.5
975 0 my ($self) = @_;
976  
977 0 ▶ unless(ref($self->{line_buffer}) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self->{"line_buffer"} eq "ARRAY") 0 0 0
978 0 $self->{line_buffer} = [ ];
979 }
980  
981 0 return shift @{$self->{line_buffer}}
982 0 ▶ if scalar(@{$self->{line_buffer}});
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$self->{"line_buffer"};} 0 0 0
983  
984 0 my $fh = $self->{fh};
985 0 ▶ unless($fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 0 0
986 0 $self->_set_error("no connection open");
987 0 return undef;
988 }
989  
990 0 ▶ local $SIG{PIPE} = 'IGNORE' unless $^O eq 'MacOS';
Branch: 1 branch
BranchTrueFalseTotal
unless $^O eq "MacOS" 0 0 0
991 0 my $out = ''; # For select
992 0 vec($out, fileno($fh), 1) = 1;
993 0 ▶ my $timeout = $self->{timeout} || undef;
Condition0%0/2
$self->{'timeout'} || undef
l!l
00
MC/DC0%0/1
$self->{'timeout'} || undef
$self->{'timeout'}
Truth table0%0/2
A
A= $self->{'timeout'}
Aresult
11
00
994 0 ▶ my $buf = $self->{pending_line} || '';
Condition0%0/2
$self->{'pending_line'} || ''
l!l
00
MC/DC0%0/1
$self->{'pending_line'} || ''
$self->{'pending_line'}
Truth table0%0/2
A
A= $self->{'pending_line'}
Aresult
11
00
995  
996 0 ▶ until(scalar(@{$self->{line_buffer}})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @{$self->{"line_buffer"};}) 0 0 0
997 0 ▶ if(select($out, undef, undef, $timeout)) {
Branch: 1 branch
BranchTrueFalseTotal
if (select $out, undef, undef, $timeout) { } 0 0 0
998 0 ▶ unless(sysread($fh, $buf, 64 * 1024, length($buf))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (sysread $fh, $buf, 65536, length $buf) 0 0 0
999 0 $self->_set_error("socket empty although there is data pending: $!");
1000 0 $self->close();
1001 0 return undef;
1002 }
1003 0 ▶ while($buf =~ s/\A(.*?\r*\n)//s) {
Branch: 1 branch
BranchTrueFalseTotal
if ($buf =~ s/\A(.*?\r*\n)//s) 0 0 0
1004 #my $l = $1; # one full line within the CRLF
1005 0 push(@{$self->{line_buffer}}, $1);
1006 }
1007 } else {
1008 0 $self->_set_error("read socket timeout");
1009 0 return undef;
1010 }
1011 }
1012 0 $self->{pending_line} = $buf;
1013  
1014 #print STDERR "Read line: $_" for @{$self->{line_buffer}};
1015 #print STDERR "Pending line: $buf\n";
1016  
1017 0 ▶ dbgPrint('READ:', join('', @{$self->{line_buffer}})) if $self->{_debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"_debug"} 0 0 0
1018 0 return shift @{$self->{line_buffer}};
1019 }
1020  
1021 =for comment Returns a plain quoted string as SCALAR or a keyword or CRLF
1022 as HASH ref [ keyword, string / arguments ]
1023  
1024 =cut
1025  
1026 0 ▶sub _token {
Subroutine _token: not called CC=19 SCAR=59.4
1027 0 my ($self, $mode) = @_;
1028  
1029 0 my $l = '';
1030 0 ▶ until($l) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($l) 0 0 0
1031 0 $l = $self->_getline();
1032 0 ▶ return undef unless defined $l;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $l 0 0 0
1033  
1034 0 $l =~ s/\A[[:blank:]]+//;
1035 }
1036  
1037 0 ▶ if($l =~ /\A\{(\d+)\+?\}\r*\Z/) { # The next $1 octets are the token
Branch: 4 branches
BranchTrueFalseTotal
if ($l =~ /\A\{(\d+)\+?\}\r*\Z/) { } 0 0 0
elsif ($l =~ s/\A"//) { } 0 0 0
elsif (!defined($mode) || $mode ne 'end' and $l =~ s/\A(active|ok|no|bye)\b//i) { } 0 0 0
elsif ($l =~ s/\A(\r*\n)//s) { } 0 0 0
1038 0 my $cnt = $1;
1039 0 $l = '';
1040 0 ▶ while($cnt > 0) { # Need some characters still
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt > 0) 0 0 0
1041 # ManageSieve is line oriented, hence, we can use _getline()
1042 0 my $r = $self->_getline();
1043 0 ▶ return undef unless defined $r;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r 0 0 0
1044 0 $l .= $r;
1045 0 $cnt -= length($r);
1046 }
1047 0 ▶ if($cnt < 0) { # Read too much
Branch: 1 branch
BranchTrueFalseTotal
if ($cnt < 0) 0 0 0
1048 0 $self->_unget(substr($l, $cnt));
1049 # $l = substr($l, 0, length($l) + $cnt);
1050 0 substr($l, $cnt) = '';
1051 }
1052 } elsif($l =~ s/\A"//) { # quoted string
1053 # I interprete http://tools.ietf.org/html/draft-martin-managesieve-08#section-4
1054 # so that a quoted string must not cross line boundaries
1055 # that makes parsing easier
1056 0 ▶ unless($l =~ s/\A((?:[^"\\]|\\.)*)"//) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($l =~ s/\A((?:[^"\\]|\\.)*)"//) 0 0 0
1057 0 $self->_set_error("missing final quote on line: $l");
1058 0 return undef;
1059 }
1060 0 $self->_unget($l);
1061 0 $l = $1;
1062 0 $l =~ s/\\(.)/$1/gs;
1063 ▶ } elsif((!defined($mode) || $mode ne 'end')
Condition0%0/6
!defined($mode) || $mode ne "end" and $l =~ s/\A(active|ok|no|bye)\b//i
!ll&&!rl&&r
000
not defined $mode or $mode ne "end"
l!l&&r!l&&!r
000
MC/DC0%0/3
!defined($mode) || $mode ne "end" and $l =~ s/\A(active|ok|no|bye)\b//i
not defined $mode $mode ne "end" $l =~ s/\A(active|ok|no|bye)\b//i
Truth table0%0/5
not(A) or B and C
A= not defined $modeB= $mode ne "end"C= $l =~ s/\A(active|ok|no|bye)\b//i
ABCresult
00X0
1X00
0100
1X11
0111
1064 && $l =~ s/\A(active|ok|no|bye)\b//i) { # response codes
1065 0 my $r = $l;
1066 0 $l = [ $1 ];
1067 0 ▶ if(uc($1) eq 'BYE') {
Branch: 1 branch
BranchTrueFalseTotal
if (uc $1 eq "BYE") 0 0 0
1068 0 $self->close();
1069 }
1070 0 ▶ if($r =~ s/\A\s+(\(.+?\))//) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r =~ s/\A\s+(\(.+?\))//) 0 0 0
1071 0 push(@$l, $1);
1072 }
1073 0 $self->_unget($r);
1074 0 ▶ while($r = $self->_token('end')) { # prevent recursion of bad server response
Branch: 1 branch
BranchTrueFalseTotal
if ($r = $self->_token("end")) 0 0 0
1075 0 ▶ if(ref($r)) { # should be NEWLINE
Branch: 1 branch
BranchTrueFalseTotal
if (ref $r) { } 0 0 0
1076 0 last;
1077 } else {
1078 0 push(@$l, $r);
1079 }
1080 }
1081 0 $self->{_last_response} = deep_copy($l);
1082 } elsif($l =~ s/\A(\r*\n)//s) {
1083 0 ▶ $self->_unget($l) if $l;
Branch: 1 branch
BranchTrueFalseTotal
if $l 0 0 0
1084 0 $l = [ "\n", $1 ];
1085 } else {
1086 0 $self->_set_error("invalid token: $l");
1087 0 return undef;
1088 }
1089  
1090 # Communication is in UTF8
1091 0 ▶ return ref($l)? $l: str2utf8($l);
Branch: 1 branch
BranchTrueFalseTotal
ref $l ? : 0 0 0
1092 }
1093  
1094 #############################################
1095 #### Helpers ##########################
1096 #############################################
1097  
1098 0 ▶sub deep_copy {
Subroutine deep_copy: not called CC=5 SCAR=34.0
Pod undocumented
1099 0 my $this = shift;
1100 0 ▶ if(!ref($this) || ref($this) eq 'CODE') {
Branch: 3 branches
BranchTrueFalseTotal
if (not ref $this or ref $this eq 'CODE') { } 0 0 0
elsif (ref $this eq 'ARRAY') { } 0 0 0
elsif (ref $this eq 'HASH') { } 0 0 0
Condition0%0/3
not ref $this or ref $this eq "CODE"
l!l&&r!l&&!r
000
MC/DC0%0/2
not ref $this or ref $this eq "CODE"
not ref $this ref $this eq "CODE"
Truth table0%0/3
not(A) or B
A= not ref $thisB= ref $this eq "CODE"
ABresult
1X1
011
000
1101 0 $this;
1102 } elsif(ref $this eq "ARRAY") {
1103 0 [map deep_copy($_), @$this];
1104 } elsif(ref $this eq "HASH") {
1105 0 +{map { $_ => deep_copy($this->{$_}) } keys %$this};
1106 } else {
1107 0 die "what type is $_?"
1108 }
1109 }
1110  
1111 =item C<< str2utf8([encoding,] string) >>
1112  
1113 Encodes the string into internal UTF8.
1114  
1115 If encoding is specified, it is tried first; then C<utf-8-strict>, and,
1116 if all fails, C<Latin1>, which is not fail.
1117  
1118 =cut
1119  
1120 0 ▶sub __decode {
Subroutine __decode: not called CC=9 SCAR=45.0
1121 0 my $enc = shift;
1122 0 my $str = shift;
1123  
1124 0 undef $@;
1125 0 ▶ return undef unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 0 0 0
1126 0 ▶ return $str unless $enc;
Branch: 1 branch
BranchTrueFalseTotal
unless $enc 0 0 0
1127  
1128 0 my $string = $str; # enforce a local copy of the string
1129 0 my $h;
1130 #my $error;
1131 0 eval { $h = decode($enc, $string, Encode::FB_QUIET ); };
1132 # next won't work
1133 # eval { $h = decode($enc, $string, sub { $error = 1; die "Invalid char " . (shift()+0) . "\n" } ); };
1134  
1135 0 my $e = $@;
1136 # print "__decode($enc): \$\@:", (defined $e? $e||'': '<<undef>>'), " len(string)=", length($string), "\n";
1137 #if($error && !$e) {
1138 # print "add \$\@\n";
1139 # $e = "some error";
1140 #}
1141 0 $@ = $e;
1142 0 ▶ return $h if $e || length($string) == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $e or length $string == 0 0 0 0
Condition0%0/3
$e or length $string == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$e or length $string == 0
$e length $string == 0
Truth table0%0/3
A or B
A= $eB= length $string == 0
ABresult
1X1
011
000
1143 0 ▶ if($string eq $str) {# the string had been decoded wholly
Branch: 1 branch
BranchTrueFalseTotal
if ($string eq $str) 0 0 0
1144 0 ▶ return $h if $h || !$h && !$str;
Branch: 1 branch
BranchTrueFalseTotal
if $h or not $h || $str 0 0 0
Condition0%0/6
$h or not $h || $str
l!l&&r!l&&!r
000
$h || $str
l!l&&r!l&&!r
000
MC/DC0%0/3
$h or not $h || $str
$h $h $str
Truth table0%0/4
A or not(B || C)
A= $hB= $hC= $str
ABCresult
1XX1
0001
01X0
0010
1145 }
1146 0 $@ = length($string) . ' characters not decoded';
1147 0 return undef;
1148 }
1149 0 ▶sub str2utf8 ($;$) {
Subroutine str2utf8: not called CC=8 SCAR=42.8
Pod documented
1150 0 my $enc;
1151 0 ▶ if(scalar(@_) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ > 1) 0 0 0
1152 0 $enc = shift;
1153 }
1154 0 my $string = shift;
1155  
1156 0 ▶ return undef unless defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 0 0
1157  
1158 0 my $h;
1159 0 ▶ if($enc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc) 0 0 0
1160 0 $h = __decode($enc, $string);
1161 0 ▶ unless($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
1162 # print "Decoded as $enc\n";
1163 0 return $h;
1164 }
1165 0 ▶ if($enc eq 'iso-2022-jp') {
Branch: 1 branch
BranchTrueFalseTotal
if ($enc eq "iso-2022-jp") 0 0 0
1166 # Don't know why but most of the above enceded string decode with next
1167 0 $h = __decode('shiftjis', $string);
1168 0 ▶ unless($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
1169 # print "Decoded as shiftjis\n";
1170 0 return $h;
1171 }
1172 }
1173 }
1174  
1175 0 $h = __decode('utf-8-strict', $string);
1176 0 ▶ unless($@) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($@) 0 0 0
1177 # print "Decoded as utf8\n";
1178 0 return $h;
1179 }
1180  
1181 # print "Decode from latin1\n";
1182 0 $h = $string; # enforce copy
1183 # For some reason, decode('latin1', ...) is doing nothing
1184 # Encode::from_to($h, "iso-8859-1", "utf8");
1185 0 utf8::upgrade($h); # set the UTF8 flag
1186 0 return $h;
1187 }
1188  
1189 sub DESTROY
1190 0 ▶{
Subroutine DESTROY: not called CC=1 SCAR=6.9
1191 0 my $self = shift;
1192 0 $self->close();
1193 }
1194  
1195 2 1;
1196  
1197 __END__
↑ Summary