| 1 | # Copyrights 2001-2023 by [Mark Overmeer]. | ||||||||||||||||||||||||||||
| 2 | # For other contributors see ChangeLog. | ||||||||||||||||||||||||||||
| 3 | # See the manual pages for details on the licensing terms. | ||||||||||||||||||||||||||||
| 4 | # Pod stripped from pm file by OODoc 2.03. | ||||||||||||||||||||||||||||
| 5 | # This code is part of distribution Mail-Box-POP3. Meta-POD processed with | ||||||||||||||||||||||||||||
| 6 | # OODoc into POD and HTML manual-pages. See README.md | ||||||||||||||||||||||||||||
| 7 | # Copyright Mark Overmeer. Licensed under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | package Mail::Transport::POP3; | ||||||||||||||||||||||||||||
| 10 | 4 | ▶ | use vars '$VERSION'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 4 | $VERSION = '3.006'; | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 4 | ▶ | use base 'Mail::Transport::Receive'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||
| 15 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 4 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 4 | ▶ | use IO::Socket (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 4 | ▶ | use IO::Socket::INET (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | use IO::Socket::SSL qw(SSL_VERIFY_NONE); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 4 | ▶ | use Socket qw/$CRLF/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 4 | ▶ | use Digest::MD5 qw/md5_hex/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | use MIME::Base64 qw/encode_base64/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | 0 | ▶ | sub _OK($) { substr(shift // '', 0, 3) eq '+OK' } | ||||||||||||||||||||||||||
|
Subroutine
_OK: not called CC=2 SCAR=17.9
Condition0%0/2
shift() // ""
MC/DC0%0/1
shift() // ""
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | sub init($) | ||||||||||||||||||||||||||||
| 29 | 0 | ▶ | { my ($self, $args) = @_; | ||||||||||||||||||||||||||
|
Subroutine
init: not called CC=8 SCAR=42.8
Pod
undocumented
| |||||||||||||||||||||||||||||
| 30 | 0 | $args->{via} = 'pop3'; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | $args->{port} ||= 110; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{"port"} ||= 110
MC/DC0%0/1
$args->{"port"} ||= 110
$args->{"port"}
Truth table0%0/2
A
A= $args->{"port"}
| |||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | 0 | ▶ | $self->SUPER::init($args) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 0 | ▶ | $self->{MTP_auth} = $args->{authenticate} || 'AUTO'; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'authenticate'} || 'AUTO'
MC/DC0%0/1
$args->{'authenticate'} || 'AUTO'
$args->{'authenticate'}
Truth table0%0/2
A
A= $args->{'authenticate'}
| |||||||||||||||||||||||||||||
| 36 | 0 | $self->{MTP_ssl} = $args->{use_ssl}; | |||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 0 | ▶ | my $opts = $self->{MTP_ssl_opts} = $args->{ssl_options} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$args->{'ssl_options'} || {}
MC/DC0%0/1
$args->{'ssl_options'} || {}
$args->{'ssl_options'}
Truth table0%0/2
A
A= $args->{'ssl_options'}
| |||||||||||||||||||||||||||||
| 39 | 0 | ▶ | $opts->{verify_hostname} ||= 0; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{"verify_hostname"} ||= 0
MC/DC0%0/1
$opts->{"verify_hostname"} ||= 0
$opts->{"verify_hostname"}
Truth table0%0/2
A
A= $opts->{"verify_hostname"}
| |||||||||||||||||||||||||||||
| 40 | 0 | ▶ | $opts->{SSL_verify_mode} ||= SSL_VERIFY_NONE; | ||||||||||||||||||||||||||
|
Condition0%0/2
$opts->{"SSL_verify_mode"} ||= 0
MC/DC0%0/1
$opts->{"SSL_verify_mode"} ||= 0
$opts->{"SSL_verify_mode"}
Truth table0%0/2
A
A= $opts->{"SSL_verify_mode"}
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 0 | ▶ | $self->socket or return; # establish connection | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 0 | $self; | |||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | #------------------------------------------ | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | 0 | ▶ | sub useSSL() { shift->{MTP_ssl} } | ||||||||||||||||||||||||||
|
Subroutine
useSSL: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | ▶ | sub SSLOptions() { shift->{MTP_ssl_opts} } | ||||||||||||||||||||||||||
|
Subroutine
SSLOptions: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | #------------------------------------------ | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub ids(;@) | ||||||||||||||||||||||||||||
| 57 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
ids: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 58 | 0 | ▶ | $self->socket or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 0 | ▶ | wantarray ? @{$self->{MTP_n2uidl}} : $self->{MTP_n2uidl}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | } | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | sub messages() | ||||||||||||||||||||||||||||
| 64 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
messages: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 0 | ▶ | $self->log(ERROR =>"Cannot get the messages of pop3 via messages()."), return () | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 67 | if wantarray; | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 0 | $self->{MTP_messages}; | |||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | sub folderSize() { shift->{MTP_folder_size} } | ||||||||||||||||||||||||||
|
Subroutine
folderSize: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | sub header($;$) | ||||||||||||||||||||||||||||
| 77 | 0 | ▶ | { my ($self, $uidl) = (shift, shift); | ||||||||||||||||||||||||||
|
Subroutine
header: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | 0 | ▶ | return unless $uidl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 79 | 0 | ▶ | my $bodylines = shift || 0;; | ||||||||||||||||||||||||||
|
Condition0%0/2
shift() || 0
MC/DC0%0/1
shift() || 0
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 0 | ▶ | my $socket = $self->socket or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 0 | ▶ | my $n = $self->id2n($uidl) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | $self->sendList($socket, "TOP $n $bodylines$CRLF"); | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | sub message($;$) | ||||||||||||||||||||||||||||
| 89 | 0 | ▶ | { my ($self, $uidl) = @_; | ||||||||||||||||||||||||||
|
Subroutine
message: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | 0 | ▶ | return unless $uidl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 0 | ▶ | my $socket = $self->socket or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 0 | ▶ | my $n = $self->id2n($uidl) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 94 | 0 | my $message = $self->sendList($socket, "RETR $n$CRLF"); | |||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||
| 96 | 0 | ▶ | return unless $message; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||
| 98 | # Some POP3 servers add a trailing empty line | ||||||||||||||||||||||||||||
| 99 | 0 | ▶ | pop @$message if @$message && $message->[-1] =~ m/^[\012\015]*$/; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
@$message and $message->[-1] =~ /^[\012\015]*$/
MC/DC0%0/2
@$message and $message->[-1] =~ /^[\012\015]*$/
@$message
$message->[-1] =~ /^[\012\015]*$/
Truth table0%0/3
A and B
A= @$messageB= $message->[-1] =~ /^[\012\015]*$/
| |||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | $self->{MTP_fetched}{$uidl} = undef # mark this ID as fetched | ||||||||||||||||||||||||||||
| 102 | 0 | ▶ | unless exists $self->{MTP_nouidl}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||
| 104 | 0 | $message; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | sub messageSize($) | ||||||||||||||||||||||||||||
| 109 | 0 | ▶ | { my ($self, $uidl) = @_; | ||||||||||||||||||||||||||
|
Subroutine
messageSize: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return unless $uidl; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | 0 | my $list; | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | unless($list = $self->{MTP_n2length}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | ▶ | { my $socket = $self->socket or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | my $raw = $self->sendList($socket, "LIST$CRLF") or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 116 | 0 | my @n2length; | |||||||||||||||||||||||||||
| 117 | 0 | foreach (@$raw) | |||||||||||||||||||||||||||
| 118 | 0 | { m#^(\d+) (\d+)#; | |||||||||||||||||||||||||||
| 119 | 0 | $n2length[$1] = $2; | |||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 0 | $self->{MTP_n2length} = $list = \@n2length; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||
| 124 | 0 | ▶ | my $n = $self->id2n($uidl) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 125 | 0 | $list->[$n]; | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | sub deleted($@) | ||||||||||||||||||||||||||||
| 130 | 0 | ▶ | { my $dele = shift->{MTP_dele} ||= {}; | ||||||||||||||||||||||||||
|
Subroutine
deleted: not called CC=3 SCAR=24.8
Pod
documented
Condition0%0/2
+(shift())->{"MTP_dele"} ||= {}
MC/DC0%0/1
+(shift())->{"MTP_dele"} ||= {}
+(shift())->{"MTP_dele"}
Truth table0%0/2
A
A= +(shift())->{"MTP_dele"}
| |||||||||||||||||||||||||||||
| 131 | 0 | ▶ | (shift) ? @$dele{ @_ } = () : delete @$dele{ @_ }; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | sub deleteFetched() | ||||||||||||||||||||||||||||
| 136 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
deleteFetched: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 0 | $self->deleted(1, keys %{$self->{MTP_fetched}}); | |||||||||||||||||||||||||||
| 138 | } | ||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | sub disconnect() | ||||||||||||||||||||||||||||
| 142 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
disconnect: not called CC=7 SCAR=40.3
Pod
documented
| |||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | my $quit; | |||||||||||||||||||||||||||
| 145 | 0 | ▶ | if($self->{MTP_socket}) # can only disconnect once | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 0 | ▶ | { if(my $socket = $self->socket) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 147 | 0 | ▶ | { my $dele = $self->{MTP_dele} || {}; | ||||||||||||||||||||||||||
|
Condition0%0/2
$self->{'MTP_dele'} || {}
MC/DC0%0/1
$self->{'MTP_dele'} || {}
$self->{'MTP_dele'}
Truth table0%0/2
A
A= $self->{'MTP_dele'}
| |||||||||||||||||||||||||||||
| 148 | 0 | ▶ | while(my $uidl = each %$dele) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 149 | 0 | ▶ | { my $n = $self->id2n($uidl) or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 150 | 0 | ▶ | $self->send($socket, "DELE $n$CRLF") or last; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | } | ||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||
| 153 | 0 | $quit = $self->send($socket, "QUIT$CRLF"); | |||||||||||||||||||||||||||
| 154 | 0 | close $socket; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | } | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 0 | delete @$self{ qw( | |||||||||||||||||||||||||||
| 159 | MTP_socket | ||||||||||||||||||||||||||||
| 160 | MTP_dele | ||||||||||||||||||||||||||||
| 161 | MTP_uidl2n | ||||||||||||||||||||||||||||
| 162 | MTP_n2uidl | ||||||||||||||||||||||||||||
| 163 | MTP_n2length | ||||||||||||||||||||||||||||
| 164 | MTP_fetched | ||||||||||||||||||||||||||||
| 165 | ) }; | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | 0 | _OK $quit; | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | sub fetched(;$) | ||||||||||||||||||||||||||||
| 172 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
fetched: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 173 | 0 | ▶ | return if exists $self->{MTP_nouidl}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 174 | 0 | $self->{MTP_fetched}; | |||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | 0 | ▶ | sub id2n($;$) { shift->{MTP_uidl2n}{shift()} } | ||||||||||||||||||||||||||
|
Subroutine
id2n: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | #------------------------------------------ | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||
| 183 | sub socket() | ||||||||||||||||||||||||||||
| 184 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
socket: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | # Do we (still) have a working connection which accepts commands? | ||||||||||||||||||||||||||||
| 187 | 0 | my $socket = $self->_connection; | |||||||||||||||||||||||||||
| 188 | 0 | ▶ | return $socket if defined $socket; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | 0 | ▶ | if(exists $self->{MTP_nouidl}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 191 | 0 | { $self->log(ERROR => | |||||||||||||||||||||||||||
| 192 | "Can not re-connect reliably to server which doesn't support UIDL"); | ||||||||||||||||||||||||||||
| 193 | 0 | return; | |||||||||||||||||||||||||||
| 194 | } | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | # (Re-)establish the connection | ||||||||||||||||||||||||||||
| 197 | 0 | ▶ | $socket = $self->login or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 198 | 0 | ▶ | $self->status($socket) or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 199 | 0 | $self->{MTP_socket} = $socket; | |||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | sub send($$) | ||||||||||||||||||||||||||||
| 205 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
send: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||||||||||||||||||
| 206 | 0 | my $socket = shift; | |||||||||||||||||||||||||||
| 207 | 0 | my $response; | |||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||
| 209 | 0 | ▶ | if(eval {print $socket @_}) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 210 | 0 | { $response = <$socket>; | |||||||||||||||||||||||||||
| 211 | 0 | ▶ | $self->log(ERROR => "Cannot read POP3 from socket: $!") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 212 | unless defined $response; | ||||||||||||||||||||||||||||
| 213 | } | ||||||||||||||||||||||||||||
| 214 | else | ||||||||||||||||||||||||||||
| 215 | 0 | { $self->log(ERROR => "Cannot write POP3 to socket: $@"); | |||||||||||||||||||||||||||
| 216 | } | ||||||||||||||||||||||||||||
| 217 | 0 | $response; | |||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||
| 221 | sub sendList($$) | ||||||||||||||||||||||||||||
| 222 | 0 | ▶ | { my ($self, $socket) = (shift, shift); | ||||||||||||||||||||||||||
|
Subroutine
sendList: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||||||||||
| 223 | 0 | my $response = $self->send($socket, @_); | |||||||||||||||||||||||||||
| 224 | 0 | ▶ | $response && _OK $response or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$response and &_OK($response)
MC/DC0%0/2
$response and &_OK($response)
$response
&_OK($response)
Truth table0%0/3
A and B
A= $responseB= &_OK($response)
| |||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | 0 | my @list; | |||||||||||||||||||||||||||
| 227 | 0 | ▶ | while(my $line = <$socket>) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 228 | 0 | ▶ | { last if $line =~ m#^\.\r?\n#s; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 229 | 0 | $line =~ s#^\.##; | |||||||||||||||||||||||||||
| 230 | 0 | push @list, $line; | |||||||||||||||||||||||||||
| 231 | } | ||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||
| 233 | 0 | \@list; | |||||||||||||||||||||||||||
| 234 | } | ||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||
| 236 | sub DESTROY() | ||||||||||||||||||||||||||||
| 237 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 238 | 0 | $self->SUPER::DESTROY; | |||||||||||||||||||||||||||
| 239 | 0 | ▶ | $self->disconnect if $self->{MTP_socket}; # only when open | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 240 | } | ||||||||||||||||||||||||||||
| 241 | |||||||||||||||||||||||||||||
| 242 | sub _connection() | ||||||||||||||||||||||||||||
| 243 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
_connection: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||
| 245 | 0 | my $socket = $self->{MTP_socket}; | |||||||||||||||||||||||||||
| 246 | 0 | ▶ | defined $socket or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 247 | |||||||||||||||||||||||||||||
| 248 | # Check if we (still) got a connection | ||||||||||||||||||||||||||||
| 249 | 0 | eval { print $socket "NOOP$CRLF" }; | |||||||||||||||||||||||||||
| 250 | 0 | ▶ | if($@ || ! <$socket> ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$@ or not readline $socket
MC/DC0%0/2
$@ or not readline $socket
$@
not readline $socket
Truth table0%0/3
A or not(B)
A= $@B= not readline $socket
| |||||||||||||||||||||||||||||
| 251 | 0 | { delete $self->{MTP_socket}; | |||||||||||||||||||||||||||
| 252 | 0 | return undef; | |||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||
| 255 | 0 | $socket; | |||||||||||||||||||||||||||
| 256 | } | ||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||
| 260 | sub login(;$) | ||||||||||||||||||||||||||||
| 261 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
login: not called CC=24 SCAR=64.0
Pod
documented
| |||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | # Check if we can make a connection | ||||||||||||||||||||||||||||
| 264 | |||||||||||||||||||||||||||||
| 265 | 0 | my ($host, $port, $username, $password) = $self->remoteHost; | |||||||||||||||||||||||||||
| 266 | 0 | ▶ | unless($username && $password) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$username and $password
MC/DC0%0/2
$username and $password
$username
$password
Truth table0%0/3
A and B
A= $usernameB= $password
| |||||||||||||||||||||||||||||
| 267 | 0 | { $self->log(ERROR => "POP3 requires a username and password."); | |||||||||||||||||||||||||||
| 268 | 0 | return; | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | 0 | my $socket; | |||||||||||||||||||||||||||
| 272 | 0 | ▶ | if($self->useSSL) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 273 | 0 | { my $opts = $self->SSLOptions; | |||||||||||||||||||||||||||
| 274 | 0 | $socket = eval { IO::Socket::SSL->new(PeerAddr => "$host:$port", %$opts) }; | |||||||||||||||||||||||||||
| 275 | } | ||||||||||||||||||||||||||||
| 276 | else | ||||||||||||||||||||||||||||
| 277 | 0 | { $socket = eval { IO::Socket::INET->new("$host:$port") }; | |||||||||||||||||||||||||||
| 278 | } | ||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | 0 | ▶ | unless($socket) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 281 | 0 | { $self->log(ERROR => "Cannot connect to $host:$port for POP3: $!"); | |||||||||||||||||||||||||||
| 282 | 0 | return; | |||||||||||||||||||||||||||
| 283 | } | ||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | # Check if it looks like a POP server | ||||||||||||||||||||||||||||
| 286 | |||||||||||||||||||||||||||||
| 287 | 0 | my $connected; | |||||||||||||||||||||||||||
| 288 | 0 | my $authenticate = $self->{MTP_auth}; | |||||||||||||||||||||||||||
| 289 | 0 | my $welcome = <$socket>; | |||||||||||||||||||||||||||
| 290 | 0 | ▶ | unless(_OK $welcome) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 291 | 0 | { $self->log(ERROR => | |||||||||||||||||||||||||||
| 292 | "Server at $host:$port does not seem to be talking POP3."); | ||||||||||||||||||||||||||||
| 293 | 0 | return; | |||||||||||||||||||||||||||
| 294 | } | ||||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | # Check APOP login if automatic or APOP specifically requested | ||||||||||||||||||||||||||||
| 297 | 0 | ▶ | if($authenticate eq 'AUTO' || $authenticate eq 'APOP') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$authenticate eq "AUTO" or $authenticate eq "APOP"
MC/DC0%0/2
$authenticate eq "AUTO" or $authenticate eq "APOP"
$authenticate eq "AUTO"
$authenticate eq "APOP"
Truth table0%0/3
A or B
A= $authenticate eq "AUTO"B= $authenticate eq "APOP"
| |||||||||||||||||||||||||||||
| 298 | 0 | ▶ | { if($welcome =~ m#^\+OK .*(<\d+\.\d+\@[^>]+>)#) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 299 | 0 | { my $md5 = md5_hex $1.$password; | |||||||||||||||||||||||||||
| 300 | 0 | my $response = $self->send($socket, "APOP $username $md5$CRLF"); | |||||||||||||||||||||||||||
| 301 | 0 | $connected = _OK $response; | |||||||||||||||||||||||||||
| 302 | } | ||||||||||||||||||||||||||||
| 303 | } | ||||||||||||||||||||||||||||
| 304 | |||||||||||||||||||||||||||||
| 305 | # Check USER/PASS login if automatic and failed or LOGIN specifically | ||||||||||||||||||||||||||||
| 306 | # requested. | ||||||||||||||||||||||||||||
| 307 | 0 | ▶ | unless($connected) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 0 | ▶ | { if($authenticate eq 'AUTO' || $authenticate eq 'LOGIN') | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$authenticate eq "AUTO" or $authenticate eq "LOGIN"
MC/DC0%0/2
$authenticate eq "AUTO" or $authenticate eq "LOGIN"
$authenticate eq "AUTO"
$authenticate eq "LOGIN"
Truth table0%0/3
A or B
A= $authenticate eq "AUTO"B= $authenticate eq "LOGIN"
| |||||||||||||||||||||||||||||
| 309 | 0 | ▶ | { my $response = $self->send($socket, "USER $username$CRLF") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 310 | or return; | ||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||
| 312 | 0 | ▶ | if(_OK $response) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 313 | 0 | ▶ | { my $response2 = $self->send($socket, "PASS $password$CRLF") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 314 | or return; | ||||||||||||||||||||||||||||
| 315 | 0 | $connected = _OK $response2; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | } | ||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||
| 320 | # Try OAUTH2 login | ||||||||||||||||||||||||||||
| 321 | 0 | ▶ | if(! $connected && $authenticate =~ /^OAUTH2/) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not $connected and $authenticate =~ /^OAUTH2/
MC/DC0%0/2
not $connected and $authenticate =~ /^OAUTH2/
not $connected
$authenticate =~ /^OAUTH2/
Truth table0%0/3
not(A) and B
A= not $connectedB= $authenticate =~ /^OAUTH2/
| |||||||||||||||||||||||||||||
| 322 | { # Borrowed from Net::POP3::XOAuth2 0.0.2 by Kizashi Nagata (also Perl license) | ||||||||||||||||||||||||||||
| 323 | 0 | my $token = encode_base64 "user=$username\001auth=Bearer $password\001\001"; | |||||||||||||||||||||||||||
| 324 | 0 | $token =~ s/[\r\n]//g; # no base64 newlines, anywhere | |||||||||||||||||||||||||||
| 325 | |||||||||||||||||||||||||||||
| 326 | 0 | ▶ | if($authenticate eq 'OAUTH2_SEP') | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | { # Microsofts way | ||||||||||||||||||||||||||||
| 328 | # https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth | ||||||||||||||||||||||||||||
| 329 | 0 | ▶ | my $response = $self->send($socket, "AUTH XOAUTH2$CRLF") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 330 | or return; | ||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | 0 | ▶ | if($response =~ /^\+/) # Office365 sends + here, not +OK | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 333 | 0 | ▶ | { my $response2 = $self->send($socket, "$token$CRLF") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 334 | or return; | ||||||||||||||||||||||||||||
| 335 | 0 | $connected = _OK $response2; | |||||||||||||||||||||||||||
| 336 | } | ||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||
| 338 | else | ||||||||||||||||||||||||||||
| 339 | 0 | ▶ | { my $response = $self->send($socket, "AUTH XOAUTH2 $token$CRLF") | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 340 | or return; | ||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||
| 342 | 0 | $connected = _OK $response; | |||||||||||||||||||||||||||
| 343 | } | ||||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | |||||||||||||||||||||||||||||
| 346 | # If we're still not connected now, we have an error | ||||||||||||||||||||||||||||
| 347 | 0 | ▶ | unless($connected) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 348 | 0 | ▶ | { $self->log(ERROR => $authenticate eq 'AUTO' ? | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 349 | "Could not authenticate using any login method" : | ||||||||||||||||||||||||||||
| 350 | "Could not authenticate using '$authenticate' method"); | ||||||||||||||||||||||||||||
| 351 | 0 | return; | |||||||||||||||||||||||||||
| 352 | } | ||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||
| 354 | 0 | $socket; | |||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||
| 359 | sub status($;$) | ||||||||||||||||||||||||||||
| 360 | 0 | ▶ | { my ($self, $socket) = @_; | ||||||||||||||||||||||||||
|
Subroutine
status: not called CC=13 SCAR=52.0
Pod
documented
| |||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | # Check if we can do a STAT | ||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||
| 364 | 0 | ▶ | my $stat = $self->send($socket, "STAT$CRLF") or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 0 | ▶ | if($stat !~ m#^\+OK (\d+) (\d+)#) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 366 | 0 | { delete $self->{MTP_messages}; | |||||||||||||||||||||||||||
| 367 | 0 | delete $self->{MTP_size}; | |||||||||||||||||||||||||||
| 368 | 0 | $self->log(ERROR => "POP3 Could not do a STAT"); | |||||||||||||||||||||||||||
| 369 | 0 | return; | |||||||||||||||||||||||||||
| 370 | } | ||||||||||||||||||||||||||||
| 371 | 0 | $self->{MTP_messages} = my $nr_msgs = $1; | |||||||||||||||||||||||||||
| 372 | 0 | $self->{MTP_folder_size} = $2; | |||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||
| 374 | # Check if we can do a UIDL | ||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||
| 376 | 0 | ▶ | my $uidl = $self->send($socket, "UIDL$CRLF") or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | 0 | $self->{MTP_nouidl} = undef; | |||||||||||||||||||||||||||
| 378 | 0 | delete $self->{MTP_uidl2n}; # drop the reverse lookup: UIDL -> number | |||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||
| 380 | 0 | ▶ | if(_OK $uidl) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 381 | 0 | { my @n2uidl; | |||||||||||||||||||||||||||
| 382 | 0 | $n2uidl[$nr_msgs] = undef; # pre-alloc | |||||||||||||||||||||||||||
| 383 | |||||||||||||||||||||||||||||
| 384 | 0 | ▶ | while(my $line = <$socket>) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 385 | 0 | ▶ | { last if substr($line, 0, 1) eq '.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 386 | 0 | ▶ | $line =~ m#^(\d+) (.+?)\r?\n# or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 387 | 0 | $n2uidl[$1] = $2; | |||||||||||||||||||||||||||
| 388 | } | ||||||||||||||||||||||||||||
| 389 | |||||||||||||||||||||||||||||
| 390 | 0 | shift @n2uidl; # make message 1 into index 0 | |||||||||||||||||||||||||||
| 391 | 0 | $self->{MTP_n2uidl} = \@n2uidl; | |||||||||||||||||||||||||||
| 392 | 0 | delete $self->{MTP_n2length}; | |||||||||||||||||||||||||||
| 393 | 0 | delete $self->{MTP_nouidl}; | |||||||||||||||||||||||||||
| 394 | } | ||||||||||||||||||||||||||||
| 395 | else | ||||||||||||||||||||||||||||
| 396 | { # We can't do UIDL, we need to fake it | ||||||||||||||||||||||||||||
| 397 | 0 | ▶ | my $list = $self->send($socket, "LIST$CRLF") or return; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 398 | 0 | my (@n2length, @n2uidl); | |||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 0 | ▶ | if(_OK $list) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 401 | 0 | { $n2length[$nr_msgs] = $n2uidl[$nr_msgs] = undef; # alloc all | |||||||||||||||||||||||||||
| 402 | |||||||||||||||||||||||||||||
| 403 | 0 | my ($host, $port) = $self->remoteHost; | |||||||||||||||||||||||||||
| 404 | 0 | ▶ | while(my $line = <$socket>) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 405 | 0 | ▶ | { last if substr($line, 0, 1) eq '.'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 406 | 0 | ▶ | $line =~ m#^(\d+) (\d+)# or next; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 407 | 0 | $n2length[$1] = $2; | |||||||||||||||||||||||||||
| 408 | 0 | $n2uidl[$1] = "$host:$port:$1"; # fake UIDL, for id only | |||||||||||||||||||||||||||
| 409 | } | ||||||||||||||||||||||||||||
| 410 | 0 | shift @n2length; shift @n2uidl; # make 1st message in index 0 | |||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||
| 412 | 0 | $self->{MTP_n2length} = \@n2length; | |||||||||||||||||||||||||||
| 413 | 0 | $self->{MTP_n2uidl} = \@n2uidl; | |||||||||||||||||||||||||||
| 414 | } | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | 0 | my $i = 1; | |||||||||||||||||||||||||||
| 417 | 0 | my %uidl2n = map +($_ => $i++), @{$self->{MTP_n2uidl}}; | |||||||||||||||||||||||||||
| 418 | 0 | $self->{MTP_uidl2n} = \%uidl2n; | |||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | 0 | 1; | |||||||||||||||||||||||||||
| 421 | } | ||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | #------------------------------------------ | ||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | sub url(;$) | ||||||||||||||||||||||||||||
| 427 | 0 | ▶ | { my $self = shift; | ||||||||||||||||||||||||||
|
Subroutine
url: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 428 | 0 | my ($host, $port, $user, $pwd) = $self->remoteHost; | |||||||||||||||||||||||||||
| 429 | 0 | ▶ | my $proto = $self->useSSL ? 'pop3s' : 'pop3'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 430 | 0 | "$proto://$user:$pwd\@$host:$port"; | |||||||||||||||||||||||||||
| 431 | } | ||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | #------------------------------------------ | ||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | 4 | 1; | |||||||||||||||||||||||||||