3 # Copyright (c) 1995 Graham Barr <Graham.Barr@tiuk.ti.com>. All rights
4 # reserved. This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
11 Net::POP3 - Post Office Protocol 3 Client class (RFC1081)
18 $pop = Net::POP3->new('pop3host');
19 $pop = Net::POP3->new('pop3host', Timeout => 60);
23 This module implements a client interface to the POP3 protocol, enabling
24 a perl5 application to talk to POP3 servers. This documentation assumes
25 that you are familiar with the POP3 protocol described in RFC1081.
27 A new Net::POP3 object must be created with the I<new> method. Once
28 this has been done, all POP3 commands are accessed via method calls
33 Need some small examples in here :-)
39 =item new ( HOST, [ OPTIONS ] )
41 This is the constructor for a new Net::POP3 object. C<HOST> is the
42 name of the remote host to which a POP3 connection is required.
44 C<OPTIONS> are passed in a hash like fasion, using key and value pairs.
47 B<Timeout> - Maximum time, in seconds, to wait for a response from the
48 POP3 server (default: 120)
50 B<Debug> - Enable debugging information
56 Unless otherwise stated all methods return either a I<true> or I<false>
57 value, with I<true> meaning that the operation was a success. When a method
58 states that it returns a value, falure will be returned as I<undef> or an
65 Send the USER command.
69 Send the PASS command. Returns the number of messages in the mailbox.
71 =item login ( [ USER [, PASS ]] )
73 Send both the the USER and PASS commands. If C<PASS> is not given the
74 C<Net::POP3> uses C<Net::Netrc> to lookup the password using the host
75 and username. If the username is not specified then the current user name
78 Returns the number of messages in the mailbox.
80 =item top ( MSGNUM [, NUMLINES ] )
82 Get the header and the first C<NUMLINES> of the body for the message
83 C<MSGNUM>. Returns a reference to an array which contains the lines of text
86 =item list ( [ MSGNUM ] )
88 If called with an argument the C<list> returns the size of the messsage
91 If called without arguments the a refererence to a hash is returned. The
92 keys will be the C<MSGNUM>'s of all undeleted messages and the values will
93 be their size in octets.
97 Get the message C<MSGNUM> from the remote mailbox. Returns a reference to an
98 array which contains the lines of text read from the server.
102 Returns the highest C<MSGNUM> of all the messages accessed.
106 Returns an array of two elements. These are the number of undeleted
107 elements and the size of the mbox in octets.
109 =item delete ( MSGNUM )
111 Mark message C<MSGNUM> to be deleted from the remote mailbox. All messages
112 that are marked to be deleted will be removed from the remote mailbox
113 when the server connection closed.
117 Reset the status of the remote POP3 server. This includes reseting the
118 status of all messages to not be deleted.
122 Quit and close the connection to the remote POP3 server. Any messages marked
123 as deleted will be deleted from the remote mailbox.
129 If a C<Net::POP3> object goes out of scope before C<quit> method is called
130 then the C<reset> method will called before the connection is closed. This
131 means that any messages marked to be deleted will not be.
140 Graham Barr <Graham.Barr@tiuk.ti.com>
145 $Date: 1996/07/26 06:44:44 $
147 The VERSION is derived from the revision by changing each number after the
148 first dot into a 2 digit number so
150 Revision 1.8 => VERSION 1.08
151 Revision 1.2.3 => VERSION 1.0203
155 Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
156 software; you can redistribute it and/or modify it under the same terms
163 use vars qw(@ISA $VERSION $debug);
167 $VERSION = do{my @r=(q$Revision: 2.1 $=~/(\d+)/g);sprintf "%d."."%02d"x$#r,@r};
169 @ISA = qw(Net::Cmd IO::Socket::INET);
174 my $type = ref($self) || $self;
177 my $obj = $type->SUPER::new(PeerAddr => $host,
178 PeerPort => $arg{Port} || 'pop3(110)',
180 Timeout => defined $arg{Timeout}
185 ${*$obj}{'net_pop3_host'} = $host;
188 $obj->debug(exists $arg{Debug} ? $arg{Debug} : undef);
190 unless ($obj->response() == CMD_OK)
200 ## We don't want people sending me their passwords when they report problems
204 sub debug_text { $_[2] =~ /^(pass|rpop)/i ? "$1 ....\n" : $_[2]; }
208 @_ >= 1 && @_ <= 3 or croak 'usage: $pop3->login( USER, PASS )';
209 my($me,$user,$pass) = @_;
215 $user ||= (getpwuid($>))[0];
217 my $m = Net::Netrc->lookup(${*$me}{'net_pop3_host'},$user);
219 $m ||= Net::Netrc->lookup(${*$me}{'net_pop3_host'});
221 $pass = $m ? $m->password || ""
231 @_ == 2 or croak 'usage: $pop3->user( USER )';
237 @_ == 2 or croak 'usage: $pop3->pass( PASS )';
242 unless($me->_PASS($pass));
244 $me->message =~ /(\d+)\s+message/io;
246 ${*$me}{'net_pop3_count'} = $1 || 0;
251 @_ == 1 or croak 'usage: $obj->reset()';
258 if(defined ${*$me}{'net_pop3_mail'})
261 foreach (@{${*$me}{'net_pop3_mail'}})
263 delete $_->{'net_pop3_deleted'};
270 @_ == 1 or croak 'usage: $obj->last()';
273 unless $_[0]->_LAST && $_[0]->message =~ /(\d+)/;
280 @_ == 2 || @_ == 3 or croak 'usage: $pop3->top( MSGNUM [, NUMLINES ])';
284 unless $me->_TOP($_[0], $_[1] || 0);
291 @_ == 1 or croak 'usage: $pop3->popstat()';
295 unless $me->_STAT && $me->message =~ /(\d+)\D+(\d+)/;
302 @_ == 1 || @_ == 2 or croak 'usage: $pop3->list( [ MSGNUM ] )';
306 unless $me->_LIST(@_);
310 $me->message =~ /\d+\D+(\d+)/;
314 my $info = $me->read_until_dot;
316 map { /(\d+)\D+(\d+)/; $hash{$1} = $2; } @$info;
323 @_ == 2 or croak 'usage: $pop3->get( MSGNUM )';
327 unless $me->_RETR(@_);
334 @_ == 2 or croak 'usage: $pop3->delete( MSGNUM )';
338 sub _USER { shift->command('USER',$_[0])->response() == CMD_OK }
339 sub _PASS { shift->command('PASS',$_[0])->response() == CMD_OK }
340 sub _RPOP { shift->command('RPOP',$_[0])->response() == CMD_OK }
341 sub _RETR { shift->command('RETR',$_[0])->response() == CMD_OK }
342 sub _DELE { shift->command('DELE',$_[0])->response() == CMD_OK }
343 sub _TOP { shift->command('TOP', @_)->response() == CMD_OK }
344 sub _LIST { shift->command('LIST',@_)->response() == CMD_OK }
345 sub _NOOP { shift->command('NOOP')->response() == CMD_OK }
346 sub _RSET { shift->command('RSET')->response() == CMD_OK }
347 sub _LAST { shift->command('LAST')->response() == CMD_OK }
348 sub _QUIT { shift->command('QUIT')->response() == CMD_OK }
349 sub _STAT { shift->command('STAT')->response() == CMD_OK }
356 unless (ref($me) && defined fileno($me));
358 $me->_QUIT && $me->SUPER::close;
361 sub quit { shift->close }
375 ## POP3 has weird responses, so we emulate them to look the same :-)
381 my $str = $cmd->getline() || return undef;
384 $cmd->debug_print(0,$str)
387 if($str =~ s/^\+OK\s+//io)
393 $str =~ s/^\+ERR\s+//io;
396 ${*$cmd}{'net_cmd_resp'} = [ $str ];
397 ${*$cmd}{'net_cmd_code'} = $code;