Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

521 lines
12 KiB

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