home *** CD-ROM | disk | FTP | other *** search
/ Acorn User 10 / AU_CD10.iso / Updates / Perl / Perl_Libs / site_perl / Net / POP3.pm < prev    next >
Encoding:
Perl POD Document  |  1997-11-30  |  10.1 KB  |  494 lines

  1. # Net::POP3.pm
  2. #
  3. # Copyright (c) 1995-1997 Graham Barr <gbarr@pobox.com>. All rights reserved.
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the same terms as Perl itself.
  6.  
  7. package Net::POP3;
  8.  
  9. use strict;
  10. use IO::Socket;
  11. use vars qw(@ISA $VERSION $debug);
  12. use Net::Cmd;
  13. use Carp;
  14. use Net::Config;
  15.  
  16. $VERSION = "2.12"; # $Id: //depot/libnet/Net/POP3.pm#6$
  17.  
  18. @ISA = qw(Net::Cmd IO::Socket::INET);
  19.  
  20. sub new
  21. {
  22.  my $self = shift;
  23.  my $type = ref($self) || $self;
  24.  my $host = shift if @_ % 2;
  25.  my %arg  = @_; 
  26.  my $hosts = defined $host ? [ $host ] : $NetConfig{pop3_hosts};
  27.  my $obj;
  28.  
  29.  my $h;
  30.  foreach $h (@{$hosts})
  31.   {
  32.    $obj = $type->SUPER::new(PeerAddr => ($host = $h), 
  33.                 PeerPort => $arg{Port} || 'pop3(110)',
  34.                 Proto    => 'tcp',
  35.                 Timeout  => defined $arg{Timeout}
  36.                         ? $arg{Timeout}
  37.                         : 120
  38.                ) and last;
  39.   }
  40.  
  41.  return undef
  42.     unless defined $obj;
  43.  
  44.  ${*$obj}{'net_pop3_host'} = $host;
  45.  
  46.  $obj->autoflush(1);
  47.  $obj->debug(exists $arg{Debug} ? $arg{Debug} : undef);
  48.  
  49.  unless ($obj->response() == CMD_OK)
  50.   {
  51.    $obj->close();
  52.    return undef;
  53.   }
  54.  
  55.  ${*$obj}{'net_pop3_banner'} = $obj->message;
  56.  
  57.  $obj;
  58. }
  59.  
  60. ##
  61. ## We don't want people sending me their passwords when they report problems
  62. ## now do we :-)
  63. ##
  64.  
  65. sub debug_text { $_[2] =~ /^(pass|rpop)/i ? "$1 ....\n" : $_[2]; }
  66.  
  67. sub login
  68. {
  69.  @_ >= 1 && @_ <= 3 or croak 'usage: $pop3->login( USER, PASS )';
  70.  my($me,$user,$pass) = @_;
  71.  
  72.  if(@_ <= 2)
  73.   {
  74.    require Net::Netrc;
  75.  
  76.    $user ||= (getpwuid($>))[0];
  77.  
  78.    my $m = Net::Netrc->lookup(${*$me}{'net_pop3_host'},$user);
  79.  
  80.    $m ||= Net::Netrc->lookup(${*$me}{'net_pop3_host'});
  81.  
  82.    $pass = $m ? $m->password || ""
  83.               : "";
  84.   }
  85.  
  86.  $me->user($user) and
  87.     $me->pass($pass);
  88. }
  89.  
  90. sub apop
  91. {
  92.  @_ >= 1 && @_ <= 3 or croak 'usage: $pop3->apop( USER, PASS )';
  93.  my($me,$user,$pass) = @_;
  94.  my $banner;
  95.  
  96.  unless(eval { require MD5 })
  97.   {
  98.    carp "You need to install MD5 to use the APOP command";
  99.    return undef;
  100.   }
  101.  
  102.  return undef
  103.    unless ( $banner = (${*$me}{'net_pop3_banner'} =~ /(<.*>)/)[0] );
  104.  
  105.  if(@_ <= 2)
  106.   {
  107.    require Net::Netrc;
  108.  
  109.    $user ||= (getpwuid($>))[0];
  110.  
  111.    my $m = Net::Netrc->lookup(${*$me}{'net_pop3_host'},$user);
  112.  
  113.    $m ||= Net::Netrc->lookup(${*$me}{'net_pop3_host'});
  114.  
  115.    $pass = $m ? $m->password || ""
  116.               : "";
  117.   }
  118.  
  119.  my $md = new MD5;
  120.  $md->add($banner,$pass);
  121.  
  122.  return undef
  123.     unless($me->_APOP($user,$md->hexdigest));
  124.  
  125.  $me->message =~ /(\d+)\s+message/io;
  126.  
  127.  ${*$me}{'net_pop3_count'} = $1 || 0;
  128. }
  129.  
  130. sub user
  131. {
  132.  @_ == 2 or croak 'usage: $pop3->user( USER )';
  133.  $_[0]->_USER($_[1]) ? 1 : undef;
  134. }
  135.  
  136. sub pass
  137. {
  138.  @_ == 2 or croak 'usage: $pop3->pass( PASS )';
  139.  
  140.  my($me,$pass) = @_;
  141.  
  142.  return undef
  143.    unless($me->_PASS($pass));
  144.  
  145.  $me->message =~ /(\d+)\s+message/io;
  146.  
  147.  ${*$me}{'net_pop3_count'} = $1 || 0;
  148. }
  149.  
  150. sub reset
  151. {
  152.  @_ == 1 or croak 'usage: $obj->reset()';
  153.  
  154.  my $me = shift;
  155.  
  156.  return 0 
  157.    unless($me->_RSET);
  158.   
  159.  if(defined ${*$me}{'net_pop3_mail'})
  160.   {
  161.    local $_;
  162.    foreach (@{${*$me}{'net_pop3_mail'}})
  163.     {
  164.      delete $_->{'net_pop3_deleted'};
  165.     }
  166.   }
  167. }
  168.  
  169. sub last
  170. {
  171.  @_ == 1 or croak 'usage: $obj->last()';
  172.  
  173.  return undef
  174.     unless $_[0]->_LAST && $_[0]->message =~ /(\d+)/;
  175.  
  176.  return $1;
  177. }
  178.  
  179. sub top
  180. {
  181.  @_ == 2 || @_ == 3 or croak 'usage: $pop3->top( MSGNUM [, NUMLINES ])';
  182.  my $me = shift;
  183.  
  184.  return undef
  185.     unless $me->_TOP($_[0], $_[1] || 0);
  186.  
  187.  $me->read_until_dot;
  188. }
  189.  
  190. sub popstat
  191. {
  192.  @_ == 1 or croak 'usage: $pop3->popstat()';
  193.  my $me = shift;
  194.  
  195.  return ()
  196.     unless $me->_STAT && $me->message =~ /(\d+)\D+(\d+)/;
  197.  
  198.  ($1 || 0, $2 || 0);
  199. }
  200.  
  201. sub list
  202. {
  203.  @_ == 1 || @_ == 2 or croak 'usage: $pop3->list( [ MSGNUM ] )';
  204.  my $me = shift;
  205.  
  206.  return undef
  207.     unless $me->_LIST(@_);
  208.  
  209.  if(@_)
  210.   {
  211.    $me->message =~ /\d+\D+(\d+)/;
  212.    return $1 || undef;
  213.   }
  214.  
  215.  my $info = $me->read_until_dot
  216.     or return undef;
  217.  
  218.  my %hash = ();
  219.  map { /(\d+)\D+(\d+)/; $hash{$1} = $2; } @$info;
  220.  
  221.  return \%hash;
  222. }
  223.  
  224. sub get
  225. {
  226.  @_ == 2 or croak 'usage: $pop3->get( MSGNUM )';
  227.  my $me = shift;
  228.  
  229.  return undef
  230.     unless $me->_RETR(@_);
  231.  
  232.  $me->read_until_dot;
  233. }
  234.  
  235. sub delete
  236. {
  237.  @_ == 2 or croak 'usage: $pop3->delete( MSGNUM )';
  238.  $_[0]->_DELE($_[1]);
  239. }
  240.  
  241. sub uidl
  242. {
  243.  @_ == 1 || @_ == 2 or croak 'usage: $pop3->uidl( [ MSGNUM ] )';
  244.  my $me = shift;
  245.  my $uidl;
  246.  
  247.  $me->_UIDL(@_) or
  248.     return undef;
  249.  if(@_)
  250.   {
  251.    $uidl = ($me->message =~ /\d+\s+([\041-\176]+)/)[0];
  252.   }
  253.  else
  254.   {
  255.    my $ref = $me->read_until_dot
  256.     or return undef;
  257.    my $ln;
  258.    $uidl = {};
  259.    foreach $ln (@$ref) {
  260.      my($msg,$uid) = $ln =~ /^\s*(\d+)\s+([\041-\176]+)/;
  261.      $uidl->{$msg} = $uid;
  262.    }
  263.   }
  264.  return $uidl;
  265. }
  266.  
  267. sub _STAT { shift->command('STAT')->response() == CMD_OK }
  268. sub _LIST { shift->command('LIST',@_)->response() == CMD_OK }
  269. sub _RETR { shift->command('RETR',$_[0])->response() == CMD_OK }
  270. sub _DELE { shift->command('DELE',$_[0])->response() == CMD_OK }
  271. sub _NOOP { shift->command('NOOP')->response() == CMD_OK }
  272. sub _RSET { shift->command('RSET')->response() == CMD_OK }
  273. sub _QUIT { shift->command('QUIT')->response() == CMD_OK }
  274. sub _TOP  { shift->command('TOP', @_)->response() == CMD_OK }
  275. sub _UIDL { shift->command('UIDL')->response() == CMD_OK }
  276. sub _USER { shift->command('USER',$_[0])->response() == CMD_OK }
  277. sub _PASS { shift->command('PASS',$_[0])->response() == CMD_OK }
  278. sub _APOP { shift->command('APOP',@_)->response() == CMD_OK }
  279.  
  280. sub _RPOP { shift->command('RPOP',$_[0])->response() == CMD_OK }
  281. sub _LAST { shift->command('LAST')->response() == CMD_OK }
  282.  
  283. sub quit
  284. {
  285.  my $me = shift;
  286.  
  287.  $me->_QUIT;
  288.  $me->close;
  289. }
  290.  
  291. sub DESTROY
  292. {
  293.  my $me = shift;
  294.  
  295.  if(defined fileno($me))
  296.   {
  297.    $me->reset;
  298.    $me->quit;
  299.   }
  300. }
  301.  
  302. ##
  303. ## POP3 has weird responses, so we emulate them to look the same :-)
  304. ##
  305.  
  306. sub response
  307. {
  308.  my $cmd = shift;
  309.  my $str = $cmd->getline() || return undef;
  310.  my $code = "500";
  311.  
  312.  $cmd->debug_print(0,$str)
  313.    if ($cmd->debug);
  314.  
  315.  if($str =~ s/^\+OK\s+//io)
  316.   {
  317.    $code = "200"
  318.   }
  319.  else
  320.   {
  321.    $str =~ s/^\+ERR\s+//io;
  322.   }
  323.  
  324.  ${*$cmd}{'net_cmd_resp'} = [ $str ];
  325.  ${*$cmd}{'net_cmd_code'} = $code;
  326.  
  327.  substr($code,0,1);
  328. }
  329.  
  330. 1;
  331.  
  332. __END__
  333.  
  334. =head1 NAME
  335.  
  336. Net::POP3 - Post Office Protocol 3 Client class (RFC1081)
  337.  
  338. =head1 SYNOPSIS
  339.  
  340.     use Net::POP3;
  341.     
  342.     # Constructors
  343.     $pop = Net::POP3->new('pop3host');
  344.     $pop = Net::POP3->new('pop3host', Timeout => 60);
  345.  
  346. =head1 DESCRIPTION
  347.  
  348. This module implements a client interface to the POP3 protocol, enabling
  349. a perl5 application to talk to POP3 servers. This documentation assumes
  350. that you are familiar with the POP3 protocol described in RFC1081.
  351.  
  352. A new Net::POP3 object must be created with the I<new> method. Once
  353. this has been done, all POP3 commands are accessed via method calls
  354. on the object.
  355.  
  356. =head1 EXAMPLES
  357.  
  358.     Need some small examples in here :-)
  359.  
  360. =head1 CONSTRUCTOR
  361.  
  362. =over 4
  363.  
  364. =item new ( [ HOST, ] [ OPTIONS ] )
  365.  
  366. This is the constructor for a new Net::POP3 object. C<HOST> is the
  367. name of the remote host to which a POP3 connection is required.
  368.  
  369. If C<HOST> is not given, then the C<POP3_Host> specified in C<Net::Config>
  370. will be used.
  371.  
  372. C<OPTIONS> are passed in a hash like fashion, using key and value pairs.
  373. Possible options are:
  374.  
  375. B<Timeout> - Maximum time, in seconds, to wait for a response from the
  376. POP3 server (default: 120)
  377.  
  378. B<Debug> - Enable debugging information
  379.  
  380. =back
  381.  
  382. =head1 METHODS
  383.  
  384. Unless otherwise stated all methods return either a I<true> or I<false>
  385. value, with I<true> meaning that the operation was a success. When a method
  386. states that it returns a value, failure will be returned as I<undef> or an
  387. empty list.
  388.  
  389. =over 4
  390.  
  391. =item user ( USER )
  392.  
  393. Send the USER command.
  394.  
  395. =item pass ( PASS )
  396.  
  397. Send the PASS command. Returns the number of messages in the mailbox.
  398.  
  399. =item login ( [ USER [, PASS ]] )
  400.  
  401. Send both the the USER and PASS commands. If C<PASS> is not given the
  402. C<Net::POP3> uses C<Net::Netrc> to lookup the password using the host
  403. and username. If the username is not specified then the current user name
  404. will be used.
  405.  
  406. Returns the number of messages in the mailbox.
  407.  
  408. If the server cannot authenticate C<USER> the I<undef> will be returned.
  409.  
  410. =item apop ( USER, PASS )
  411.  
  412. Authenticate with the server identifying as C<USER> with password C<PASS>.
  413. Similar ti L<login>, but the password is not sent in clear text. 
  414.  
  415. To use this method you must have the MD5 package installed, if you do not
  416. this method will return I<undef>
  417.  
  418.  
  419. =item top ( MSGNUM [, NUMLINES ] )
  420.  
  421. Get the header and the first C<NUMLINES> of the body for the message
  422. C<MSGNUM>. Returns a reference to an array which contains the lines of text
  423. read from the server.
  424.  
  425. =item list ( [ MSGNUM ] )
  426.  
  427. If called with an argument the C<list> returns the size of the message
  428. in octets.
  429.  
  430. If called without arguments a reference to a hash is returned. The
  431. keys will be the C<MSGNUM>'s of all undeleted messages and the values will
  432. be their size in octets.
  433.  
  434. =item get ( MSGNUM )
  435.  
  436. Get the message C<MSGNUM> from the remote mailbox. Returns a reference to an
  437. array which contains the lines of text read from the server.
  438.  
  439. =item last ()
  440.  
  441. Returns the highest C<MSGNUM> of all the messages accessed.
  442.  
  443. =item popstat ()
  444.  
  445. Returns an array of two elements. These are the number of undeleted
  446. elements and the size of the mbox in octets.
  447.  
  448. =item uidl ( [ MSGNUM ] )
  449.  
  450. Returns a unique identifier for C<MSGNUM> if given. If C<MSGNUM> is not
  451. given C<uidl> returns a reference to a hash where the keys are the
  452. message numbers and the values are the unique identifiers.
  453.  
  454. =item delete ( MSGNUM )
  455.  
  456. Mark message C<MSGNUM> to be deleted from the remote mailbox. All messages
  457. that are marked to be deleted will be removed from the remote mailbox
  458. when the server connection closed.
  459.  
  460. =item reset ()
  461.  
  462. Reset the status of the remote POP3 server. This includes reseting the
  463. status of all messages to not be deleted.
  464.  
  465. =item quit ()
  466.  
  467. Quit and close the connection to the remote POP3 server. Any messages marked
  468. as deleted will be deleted from the remote mailbox.
  469.  
  470. =back
  471.  
  472. =head1 NOTES
  473.  
  474. If a C<Net::POP3> object goes out of scope before C<quit> method is called
  475. then the C<reset> method will called before the connection is closed. This
  476. means that any messages marked to be deleted will not be.
  477.  
  478. =head1 SEE ALSO
  479.  
  480. L<Net::Netrc>
  481. L<Net::Cmd>
  482.  
  483. =head1 AUTHOR
  484.  
  485. Graham Barr <gbarr@pobox.com>
  486.  
  487. =head1 COPYRIGHT
  488.  
  489. Copyright (c) 1995-1997 Graham Barr. All rights reserved.
  490. This program is free software; you can redistribute it and/or modify
  491. it under the same terms as Perl itself.
  492.  
  493. =cut
  494.