File Coverage

blib/lib/Authen/Simple/IMAP.pm
Criterion Covered Total %
statement 57 85 67.0
branch 18 54 33.3
condition 0 4 0.0
subroutine 9 12 75.0
pod 2 3 66.6
total 86 158 54.4


line stmt bran cond sub pod time code
1             package Authen::Simple::IMAP;
2              
3 2     2   24676 use 5.8.6;
  2         8  
  2         92  
4 2     2   12 use warnings;
  2         4  
  2         58  
5 2     2   10 use strict;
  2         9  
  2         63  
6 2     2   10 use Carp;
  2         8  
  2         180  
7 2     2   8 use base 'Authen::Simple::Adapter';
  2         3  
  2         1907  
8             #use Data::Dumper;
9 2     2   138408 use Params::Validate qw(validate_pos :types);
  2         4  
  2         3912  
10              
11             our $VERSION = '0.1.2';
12              
13             __PACKAGE__->options({
14             host => {
15             type => Params::Validate::SCALAR,
16             optional => 1,
17             depends => [ 'protocol' ],
18             },
19             protocol => {
20             type => Params::Validate::SCALAR,
21             default => 'IMAP',
22             optional => 1,
23             depends => [ 'host' ],
24             },
25             imap => {
26             type => Params::Validate::OBJECT,
27             can => ['login','errstr'],
28             optional => 1,
29             },
30             timeout => {
31             type => Params::Validate::SCALAR,
32             optional => 1,
33             },
34             escape_slash => {
35             type => Params::Validate::SCALAR,
36             optional => 1,
37             default => 1,
38             },
39             });
40              
41             sub init {
42 1     1 1 504 my ($self, $args) = @_;
43 1 50       5 if ( $args->{log} ) {
44 1         7 $self->log($args->{log});
45             }
46 1         15 $self->log->info("Starting init routine for Authen::Simple::IMAP");
47 1 50       28 $self->log->debug("Starting init routine\n") if $self->log;
48 1         20 my $is_user_provided_object;
49 1         3 my @imap_args = $args->{host};
50 1 50       6 if ( defined($args->{timeout}) ) {
51 0         0 push(@imap_args, timeout => $args->{timeout});
52             }
53 1 50       4 if ( defined($args->{imap}) ) {
    0          
    0          
    0          
54 1 50       3 $self->log->info("setting up with user provided IMAP object ".
55             ref($args->{imap})."\n") if $self->log;
56 1         20 $is_user_provided_object = 1;
57             }
58             elsif ( $args->{protocol} eq 'IMAPS' ) {
59 0         0 require Net::IMAP::Simple::SSL;
60             }
61             elsif ( $args->{protocol} eq 'IMAP' ) {
62 0         0 require Net::IMAP::Simple;
63             }
64             elsif ( defined($args->{protocol}) ) {
65 0         0 croak "Valid protocols are 'IMAP' and 'IMAPS', not '".$args->{protocol}."'";
66             }
67             else {
68 0         0 croak "A protocol or an imap object is required";
69             }
70 1         9 my $obj = $self->SUPER::init($args);
71 1         33 $obj->{imap_args} = \@imap_args;
72 1 50       4 if ( $is_user_provided_object ) {
73 1         2 $obj->{user_provided_object} = $args->{imap};
74             }
75 1         5 return $obj;
76             }
77              
78             sub connect {
79 2     2 0 4 my $self = shift;
80 2 50       5 die 'Should never happen' if !defined($self->{imap_args});
81 2 50       5 if ( $self->{user_provided_object} ) {
82 2         3 $self->{imap} = $self->{user_provided_object};
83 2         3 return;
84             }
85 0         0 my @imap_args = @{$self->{imap_args}};
  0         0  
86             #warn 'imap args '.join(", ",@imap_args)."\n";
87 0         0 my $host = shift(@imap_args);
88 0         0 my $args = { @imap_args };
89 0         0 unshift(@imap_args,$host);
90              
91 0     0   0 local( $SIG{ALRM} ) = sub { croak "timeout while connecting to server" };
  0         0  
92 0 0       0 if ( defined($args->{timeout}) ) {
93 0         0 alarm $args->{timeout};
94             }
95             else {
96 0         0 alarm 90;
97             }
98 0 0       0 if ( defined($self->{imap}) ) {
    0          
    0          
99 0 0       0 $self->log->info("already have a user provided IMAP object ".
100             ref($self->{imap})."\n") if $self->log;
101             }
102             elsif ( $self->{protocol} eq 'IMAPS' ) {
103             local( $SIG{ALRM} ) = sub {
104 0     0   0 croak "timeout while connecting to IMAPS server at $host"
105 0         0 };
106 0 0       0 $self->log->info("connecting to ".$host." with IMAPS\n")
107             if $self->log;
108 0   0     0 $self->{imap} = Net::IMAP::Simple::SSL->new(@imap_args) ||
109             die "Unable to connect to IMAPS: $Net::IMAP::Simple::SSL::errstr\n";
110             }
111             elsif ( $self->{protocol} eq 'IMAP' ) {
112             local( $SIG{ALRM} ) = sub {
113 0     0   0 croak "timeout while connecting to IMAP server at $host"
114 0         0 };
115 0 0       0 $self->log->info("connecting to ".$host." with IMAP (no SSL)\n")
116             if $self->log;
117 0   0     0 $self->{imap} = Net::IMAP::Simple->new(@imap_args) ||
118             die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n";
119             }
120             else {
121 0         0 croak 'This should never happen!';
122             }
123 0         0 alarm 0;
124 0         0 return $self->{imap};
125             }
126              
127              
128             sub check {
129 2     2 1 534 my @params = validate_pos(@_,
130             {
131             type => OBJECT,
132             isa => 'Authen::Simple::IMAP',
133             },
134             {
135             type => SCALAR,
136             },
137             {
138             type => SCALAR,
139             },
140             );
141 2         7 my ($self,$username,$password) = @params;
142 2 50       7 $self->log->debug("Starting check routine\n") if $self->log;
143             #$self->log->debug("Username = '$username'");
144             #$self->log->debug("Password = '$password'");
145            
146 2 50       67 if ( $self->escape_slash ) {
147 2         10 $password =~ s[\\][\\\\]g;
148             }
149             #$self->log->debug("Password post escape_slash = '$password'");
150              
151             #delete($self->{imap}) if exists($self->{imap});
152              
153 2         6 $self->connect;
154              
155 2 50       7 $self->log->info('Attempting to authenticate user \''.$username.'\''."\n")
156             if $self->log;
157 2 100       35 if ( $self->imap->login($username,$password) ) {
158 1 50       13 $self->log->info("Successfully logged in '".$username."'\n")
159             if $self->log;
160 1 50       27 $self->imap->quit() if $self->imap->can('quit');
161 1         10 $self->imap(undef);
162 1         7 return 1;
163             }
164 1         10 my $fail = 'Failed to authenticate user \''.$username.'\'';
165 1 50       4 $fail .= ': '.$self->imap->errstr if $self->imap->errstr;
166 1 50       15 $self->log->info($fail) if $self->log;
167 1 50       18 $self->imap->quit() if $self->imap->can('quit');
168 1         30 $self->imap(undef);
169 1         6 return 0;
170             }
171              
172             1; # Magic true value required at end of module
173             __END__