home *** CD-ROM | disk | FTP | other *** search
/ isnet Internet / Isnet Internet CD.iso / prog / hiz / 09 / 09.exe / adynware.exe / perl / lib / site / Net / POP3.pm < prev    next >
Encoding:
Perl POD Document  |  1999-12-28  |  8.3 KB  |  395 lines

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