about summary refs log tree commit
diff options
context:
space:
mode:
authorGraham Barr <gbarr@pobox.com>2002-02-25 14:20:28 +0000
committerGraham Barr <gbarr@pobox.com>2002-02-25 14:20:28 +0000
commit03156a4b45d27ccf027ce7e165ece60f5b9c80c4 (patch)
treef2e561553ec0404e5106e4064eda9459b413d424
parentfd3669244b4db92463bff16a1dd71ea7dbfc3b67 (diff)
downloadperl-libnet-03156a4b45d27ccf027ce7e165ece60f5b9c80c4.tar.gz
Net::POP3
- Patches from Ville Skytta for various cleanup and use Digest::MD5

-rw-r--r--Net/POP3.pm103
1 files changed, 53 insertions, 50 deletions
diff --git a/Net/POP3.pm b/Net/POP3.pm
index 5a75010..1460416 100644
--- a/Net/POP3.pm
+++ b/Net/POP3.pm
@@ -13,7 +13,7 @@ use Net::Cmd;
 use Carp;
 use Net::Config;
 
-$VERSION = "2.23"; # $Id: //depot/libnet/Net/POP3.pm#21 $
+$VERSION = "2.23"; # $Id: //depot/libnet/Net/POP3.pm#22 $
 
 @ISA = qw(Net::Cmd IO::Socket::INET);
 
@@ -71,19 +71,9 @@ sub login
  @_ >= 1 && @_ <= 3 or croak 'usage: $pop3->login( USER, PASS )';
  my($me,$user,$pass) = @_;
 
- if(@_ <= 2)
-  {
-   require Net::Netrc;
-
-   $user ||= eval { (getpwuid($>))[0] } || $ENV{NAME};
-
-   my $m = Net::Netrc->lookup(${*$me}{'net_pop3_host'},$user);
-
-   $m ||= Net::Netrc->lookup(${*$me}{'net_pop3_host'});
-
-   $pass = $m ? $m->password || ""
-              : "";
-  }
+ if (@_ <= 2) {
+   ($user, $pass) = $me->_lookup_credentials($user);
+ }
 
  $me->user($user) and
     $me->pass($pass);
@@ -94,40 +84,30 @@ sub apop
  @_ >= 1 && @_ <= 3 or croak 'usage: $pop3->apop( USER, PASS )';
  my($me,$user,$pass) = @_;
  my $banner;
-
- unless(eval { require MD5 })
-  {
-   carp "You need to install MD5 to use the APOP command";
+ my $md;
+
+ if (eval { local $SIG{__DIE__}; require Digest::MD5 }) {
+   $md = Digest::MD5->new();
+ } elsif (eval { local $SIG{__DIE__}; require MD5 }) {
+   $md = MD5->new();
+ } else {
+   carp "You need to install Digest::MD5 or MD5 to use the APOP command";
    return undef;
-  }
+ }
 
  return undef
    unless ( $banner = (${*$me}{'net_pop3_banner'} =~ /(<.*>)/)[0] );
 
- if(@_ <= 2)
-  {
-   require Net::Netrc;
-
-   $user ||= eval { (getpwuid($>))[0] } || $ENV{NAME};
-
-   my $m = Net::Netrc->lookup(${*$me}{'net_pop3_host'},$user);
-
-   $m ||= Net::Netrc->lookup(${*$me}{'net_pop3_host'});
-
-   $pass = $m ? $m->password || ""
-              : "";
-  }
+ if (@_ <= 2) {
+   ($user, $pass) = $me->_lookup_credentials($user);
+ }
 
- my $md = MD5->new;
  $md->add($banner,$pass);
 
  return undef
     unless($me->_APOP($user,$md->hexdigest));
 
- my $ret = ${*$me}{'net_pop3_count'} = ($me->message =~ /(\d+)\s+message/io)
-        ? $1 : ($me->popstat)[0];
-
- $ret ? $ret : "0E0";
+ $me->_get_mailbox_count();
 }
 
 sub user
@@ -145,10 +125,7 @@ sub pass
  return undef
    unless($me->_PASS($pass));
 
- my $ret = ${*$me}{'net_pop3_count'} = ($me->message =~ /(\d+)\s+message/io)
-        ? $1 : ($me->popstat)[0];
-
- $ret ? $ret : "0E0";
+ $me->_get_mailbox_count();
 }
 
 sub reset
@@ -288,6 +265,33 @@ sub ping
  ($1 || 0, $2 || 0);
 }
 
+sub _lookup_credentials
+{
+  my ($me, $user) = @_;
+
+  require Net::Netrc;
+
+  $user ||= eval { local $SIG{__DIE__}; (getpwuid($>))[0] } ||
+    $ENV{NAME} || $ENV{USER} || $ENV{LOGNAME};
+
+  my $m = Net::Netrc->lookup(${*$me}{'net_pop3_host'},$user);
+  $m ||= Net::Netrc->lookup(${*$me}{'net_pop3_host'});
+
+  my $pass = $m ? $m->password || ""
+                : "";
+
+  ($user, $pass);
+}
+
+sub _get_mailbox_count
+{
+  my ($me) = @_;
+  my $ret = ${*$me}{'net_pop3_count'} = ($me->message =~ /(\d+)\s+message/io)
+          ? $1 : ($me->popstat)[0];
+
+  $ret ? $ret : "0E0";
+}
+
 
 sub _STAT { shift->command('STAT')->response() == CMD_OK }
 sub _LIST { shift->command('LIST',@_)->response() == CMD_OK }
@@ -359,7 +363,7 @@ __END__
 
 =head1 NAME
 
-Net::POP3 - Post Office Protocol 3 Client class (RFC1081)
+Net::POP3 - Post Office Protocol 3 Client class (RFC1939)
 
 =head1 SYNOPSIS
 
@@ -373,7 +377,7 @@ Net::POP3 - Post Office Protocol 3 Client class (RFC1081)
 
 This module implements a client interface to the POP3 protocol, enabling
 a perl5 application to talk to POP3 servers. This documentation assumes
-that you are familiar with the POP3 protocol described in RFC1081.
+that you are familiar with the POP3 protocol described in RFC1939.
 
 A new Net::POP3 object must be created with the I<new> method. Once
 this has been done, all POP3 commands are accessed via method calls
@@ -439,14 +443,13 @@ will give a true value in a boolean context, but zero in a numeric context.
 
 If there was an error authenticating the user then I<undef> will be returned.
 
-=item apop ( USER, PASS )
+=item apop ( [ USER [, PASS ]] )
 
 Authenticate with the server identifying as C<USER> with password C<PASS>.
-Similar ti L<login>, but the password is not sent in clear text.
-
-To use this method you must have the MD5 package installed, if you do not
-this method will return I<undef>
+Similar to L</login>, but the password is not sent in clear text.
 
+To use this method you must have the Digest::MD5 or the MD5 module installed,
+otherwise this method will return I<undef>.
 
 =item top ( MSGNUM [, NUMLINES ] )
 
@@ -522,7 +525,7 @@ means that any messages marked to be deleted will not be.
 
 =head1 SEE ALSO
 
-L<Net::Netrc>
+L<Net::Netrc>,
 L<Net::Cmd>
 
 =head1 AUTHOR
@@ -537,6 +540,6 @@ it under the same terms as Perl itself.
 
 =for html <hr>
 
-I<$Id: //depot/libnet/Net/POP3.pm#21 $>
+I<$Id: //depot/libnet/Net/POP3.pm#22 $>
 
 =cut