File Coverage

blib/lib/Mail/Box/IMAP4/SSL.pm
Criterion Covered Total %
statement 47 53 88.6
branch 8 12 66.6
condition 6 10 60.0
subroutine 10 10 100.0
pod 1 2 50.0
total 72 87 82.7


line stmt bran cond sub pod time code
1 3     3   4015512 use 5.006;
  3         15  
  3         165  
2 3     3   21 use strict;
  3         12  
  3         362  
3 3     3   65 use warnings;
  3         14  
  3         715  
4              
5             package Mail::Box::IMAP4::SSL;
6             # ABSTRACT: handle IMAP4 folders with SSL
7             our $VERSION = '0.03'; # VERSION
8              
9 3     3   3662 use superclass 'Mail::Box::IMAP4' => 2.079;
  3         24387  
  3         42  
10 3     3   960306 use IO::Socket::SSL 1.12;
  3         386228  
  3         29  
11 3     3   867 use Mail::Reporter 2.079 qw();
  3         68  
  3         75  
12 3     3   100 use Mail::Transport::IMAP4 2.079 qw();
  3         76  
  3         71  
13 3     3   19 use Mail::IMAPClient 3.02;
  3         53  
  3         1517  
14              
15             my $imaps_port = 993; # standard port for IMAP over SSL
16              
17             #--------------------------------------------------------------------------#
18             # init
19             #--------------------------------------------------------------------------#
20              
21             sub init {
22 6     6 0 24413 my ( $self, $args ) = @_;
23              
24             # until we're connected, mark as closed in case we exit early
25             # (otherwise, Mail::Box::DESTROY will try to close/unlock, which dies)
26 6         668 $self->{MB_is_closed}++;
27              
28             # if no port is provided, use the default
29 6   66     50 $args->{server_port} ||= $imaps_port;
30              
31             # Mail::Box::IMAP4 wants a folder or it throws warnings
32 6   100     37 $args->{folder} ||= '/';
33              
34             # Use messages classes from our superclass type
35 6   50     51 $args->{message_type} ||= 'Mail::Box::IMAP4::Message';
36              
37             # giving us a transport argument is an error since our only purpose
38             # is to create the right kind of transport object
39 6 100       24 if ( $args->{transporter} ) {
40 1         9 Mail::Reporter->log(
41             ERROR => "The 'transporter' option is not valid for " . __PACKAGE__ );
42 1         71 return;
43             }
44              
45             # some arguments are required to connect to a server
46 5         15 for my $req (qw/ server_name username password/) {
47 12 100       40 if ( not defined $args->{$req} ) {
48 3         29 Mail::Reporter->log( ERROR => "The '$req' option is required for " . __PACKAGE__ );
49 3         168 return;
50             }
51             }
52              
53             # trying to create the transport object
54              
55 2 50       14 my $verify_mode =
56             $ENV{MAIL_BOX_IMAP4_SSL_NOVERIFY} ? SSL_VERIFY_NONE() : SSL_VERIFY_PEER();
57              
58 2         35 my $ssl_socket = IO::Socket::SSL->new(
59             Proto => 'tcp',
60             PeerAddr => $args->{server_name},
61             PeerPort => $args->{server_port},
62             SSL_verify_mode => $verify_mode,
63             );
64              
65 2 50       22113 unless ($ssl_socket) {
66 0         0 Mail::Reporter->log( ERROR => "Couldn't connect to '$args->{server_name}': "
67             . IO::Socket::SSL::errstr() );
68 0         0 return;
69             }
70              
71 2         42 my $imap = Mail::IMAPClient->new(
72             User => $args->{username},
73             Password => $args->{password},
74             Socket => $ssl_socket,
75             Uid => 1, # Mail::Transport::IMAP4 does this
76             Peek => 1, # Mail::Transport::IMAP4 does this
77             );
78 2         83434 my $imap_err = $@;
79              
80 2 50 33     30 unless ( $imap && $imap->IsAuthenticated ) {
81 0         0 Mail::Reporter->log( ERROR => "Login rejected for user '$args->{username}'"
82             . " on server '$args->{server_name}': $imap_err" );
83 0         0 return;
84             }
85              
86 2         79 $args->{transporter} = Mail::Transport::IMAP4->new( imap_client => $imap, );
87              
88 2 50       1188 unless ( $args->{transporter} ) {
89 0         0 Mail::Reporter->log(
90             ERROR => "Error creating Mail::Transport::IMAP4 from the SSL connection." );
91 0         0 return;
92             }
93              
94             # now that we have a valid transporter, mark ourselves open
95             # and let the superclass initialize
96 2         13 delete $self->{MB_is_closed};
97 2         36 $self->SUPER::init($args);
98              
99             }
100              
101 1     1 1 1679 sub type { 'imaps' }
102              
103             1;
104              
105             __END__