| 1 | package Authen::Simple::IMAP; | ||||||||||||||||||||||
| 2 | |||||||||||||||||||||||
| 3 | 2 | ▶ | use 5.8.6; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | 2 | ▶ | use base 'Authen::Simple::Adapter'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 8 | #use Data::Dumper; | ||||||||||||||||||||||
| 9 | 2 | ▶ | use Params::Validate qw(validate_pos :types); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 10 | |||||||||||||||||||||||
| 11 | 2 | our $VERSION = '0.1.2'; | |||||||||||||||||||||
| 12 | |||||||||||||||||||||||
| 13 | 2 | __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 | 1 | ▶ | sub init { | ||||||||||||||||||||
|
Subroutine
init: called CC=10 SCAR=29.1
Pod
documented
| |||||||||||||||||||||||
| 42 | 1 | my ($self, $args) = @_; | |||||||||||||||||||||
| 43 | 1 | ▶ | if ( $args->{log} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 44 | 1 | $self->log($args->{log}); | |||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||
| 46 | 1 | $self->log->info("Starting init routine for Authen::Simple::IMAP"); | |||||||||||||||||||||
| 47 | 1 | ▶ | $self->log->debug("Starting init routine\n") if $self->log; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 48 | 1 | my $is_user_provided_object; | |||||||||||||||||||||
| 49 | 1 | my @imap_args = $args->{host}; | |||||||||||||||||||||
| 50 | 1 | ▶ | if ( defined($args->{timeout}) ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 51 | 0 | push(@imap_args, timeout => $args->{timeout}); | |||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||
| 53 | 1 | ▶ | if ( defined($args->{imap}) ) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 54 | $self->log->info("setting up with user provided IMAP object ". | ||||||||||||||||||||||
| 55 | 1 | ▶ | ref($args->{imap})."\n") if $self->log; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 56 | 1 | $is_user_provided_object = 1; | |||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||
| 58 | elsif ( $args->{protocol} eq 'IMAPS' ) { | ||||||||||||||||||||||
| 59 | 0 | require Net::IMAP::Simple::SSL; | |||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||
| 61 | elsif ( $args->{protocol} eq 'IMAP' ) { | ||||||||||||||||||||||
| 62 | 0 | require Net::IMAP::Simple; | |||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||
| 64 | elsif ( defined($args->{protocol}) ) { | ||||||||||||||||||||||
| 65 | 0 | croak "Valid protocols are 'IMAP' and 'IMAPS', not '".$args->{protocol}."'"; | |||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||
| 67 | else { | ||||||||||||||||||||||
| 68 | 0 | croak "A protocol or an imap object is required"; | |||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||
| 70 | 1 | my $obj = $self->SUPER::init($args); | |||||||||||||||||||||
| 71 | 1 | $obj->{imap_args} = \@imap_args; | |||||||||||||||||||||
| 72 | 1 | ▶ | if ( $is_user_provided_object ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 73 | 1 | $obj->{user_provided_object} = $args->{imap}; | |||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||
| 75 | 1 | return $obj; | |||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||
| 77 | |||||||||||||||||||||||
| 78 | 2 | ▶ | sub connect { | ||||||||||||||||||||
|
Subroutine
connect: called CC=12 SCAR=46.4
Pod
undocumented
| |||||||||||||||||||||||
| 79 | 2 | my $self = shift; | |||||||||||||||||||||
| 80 | 2 | ▶ | die 'Should never happen' if !defined($self->{imap_args}); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 81 | 2 | ▶ | if ( $self->{user_provided_object} ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 82 | 2 | $self->{imap} = $self->{user_provided_object}; | |||||||||||||||||||||
| 83 | 2 | return; | |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||
| 85 | 0 | my @imap_args = @{$self->{imap_args}}; | |||||||||||||||||||||
| 86 | #warn 'imap args '.join(", ",@imap_args)."\n"; | ||||||||||||||||||||||
| 87 | 0 | my $host = shift(@imap_args); | |||||||||||||||||||||
| 88 | 0 | my $args = { @imap_args }; | |||||||||||||||||||||
| 89 | 0 | unshift(@imap_args,$host); | |||||||||||||||||||||
| 90 | |||||||||||||||||||||||
| 91 | 0 | ▶ | local( $SIG{ALRM} ) = sub { croak "timeout while connecting to server" }; | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 92 | 0 | ▶ | if ( defined($args->{timeout}) ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 93 | 0 | alarm $args->{timeout}; | |||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||
| 95 | else { | ||||||||||||||||||||||
| 96 | 0 | alarm 90; | |||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||
| 98 | 0 | ▶ | if ( defined($self->{imap}) ) { | ||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||
| 99 | $self->log->info("already have a user provided IMAP object ". | ||||||||||||||||||||||
| 100 | 0 | ▶ | ref($self->{imap})."\n") if $self->log; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||
| 102 | elsif ( $self->{protocol} eq 'IMAPS' ) { | ||||||||||||||||||||||
| 103 | 0 | ▶ | local( $SIG{ALRM} ) = sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 104 | 0 | croak "timeout while connecting to IMAPS server at $host" | |||||||||||||||||||||
| 105 | 0 | }; | |||||||||||||||||||||
| 106 | 0 | ▶ | $self->log->info("connecting to ".$host." with IMAPS\n") | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 107 | if $self->log; | ||||||||||||||||||||||
| 108 | 0 | ▶ | $self->{imap} = Net::IMAP::Simple::SSL->new(@imap_args) || | ||||||||||||||||||||
|
Condition0%0/2
'Net::IMAP::Simple::SSL'->new(@imap_args) || die("Unable to connect to IMAPS: $Net::IMAP::Simple::SSL::errstr\n")
MC/DC0%0/1
'Net::IMAP::Simple::SSL'->new(@imap_args) || die("Unable to connect to IMAPS: $Net::IMAP::Simple::SSL::errstr\n")
'Net::IMAP::Simple::SSL'->new(@imap_args)
Truth table0%0/2
A
A= '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 | 0 | ▶ | local( $SIG{ALRM} ) = sub { | ||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||
| 113 | 0 | croak "timeout while connecting to IMAP server at $host" | |||||||||||||||||||||
| 114 | 0 | }; | |||||||||||||||||||||
| 115 | 0 | ▶ | $self->log->info("connecting to ".$host." with IMAP (no SSL)\n") | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 116 | if $self->log; | ||||||||||||||||||||||
| 117 | 0 | ▶ | $self->{imap} = Net::IMAP::Simple->new(@imap_args) || | ||||||||||||||||||||
|
Condition0%0/2
'Net::IMAP::Simple'->new(@imap_args) || die("Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n")
MC/DC0%0/1
'Net::IMAP::Simple'->new(@imap_args) || die("Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n")
'Net::IMAP::Simple'->new(@imap_args)
Truth table0%0/2
A
A= 'Net::IMAP::Simple'->new(@imap_args)
| |||||||||||||||||||||||
| 118 | die "Unable to connect to IMAP: $Net::IMAP::Simple::errstr\n"; | ||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||
| 120 | else { | ||||||||||||||||||||||
| 121 | 0 | croak 'This should never happen!'; | |||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||
| 123 | 0 | alarm 0; | |||||||||||||||||||||
| 124 | 0 | return $self->{imap}; | |||||||||||||||||||||
| 125 | } | ||||||||||||||||||||||
| 126 | |||||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | 2 | ▶ | sub check { | ||||||||||||||||||||
|
Subroutine
check: called CC=10 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||
| 129 | 2 | 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 | my ($self,$username,$password) = @params; | |||||||||||||||||||||
| 142 | 2 | ▶ | $self->log->debug("Starting check routine\n") if $self->log; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 143 | #$self->log->debug("Username = '$username'"); | ||||||||||||||||||||||
| 144 | #$self->log->debug("Password = '$password'"); | ||||||||||||||||||||||
| 145 | |||||||||||||||||||||||
| 146 | 2 | ▶ | if ( $self->escape_slash ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 147 | 2 | $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 | $self->connect; | |||||||||||||||||||||
| 154 | |||||||||||||||||||||||
| 155 | 2 | ▶ | $self->log->info('Attempting to authenticate user \''.$username.'\''."\n") | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 156 | if $self->log; | ||||||||||||||||||||||
| 157 | 2 | ▶ | if ( $self->imap->login($username,$password) ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 158 | 1 | ▶ | $self->log->info("Successfully logged in '".$username."'\n") | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 159 | if $self->log; | ||||||||||||||||||||||
| 160 | 1 | ▶ | $self->imap->quit() if $self->imap->can('quit'); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 161 | 1 | $self->imap(undef); | |||||||||||||||||||||
| 162 | 1 | return 1; | |||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||
| 164 | 1 | my $fail = 'Failed to authenticate user \''.$username.'\''; | |||||||||||||||||||||
| 165 | 1 | ▶ | $fail .= ': '.$self->imap->errstr if $self->imap->errstr; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 166 | 1 | ▶ | $self->log->info($fail) if $self->log; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 167 | 1 | ▶ | $self->imap->quit() if $self->imap->can('quit'); | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 168 | 1 | $self->imap(undef); | |||||||||||||||||||||
| 169 | 1 | return 0; | |||||||||||||||||||||
| 170 | } | ||||||||||||||||||||||
| 171 | |||||||||||||||||||||||
| 172 | 2 | 1; # Magic true value required at end of module | |||||||||||||||||||||
| 173 | __END__ | ||||||||||||||||||||||