1999-10-13 03:29:15 -04:00
|
|
|
=begin
|
|
|
|
|
2000-11-10 18:31:32 -05:00
|
|
|
= net/pop.rb version 1.1.31
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
written by Minero Aoki <aamine@dp.u-netsurf.ne.jp>
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2000-08-16 15:26:07 -04:00
|
|
|
This program is free software.
|
|
|
|
You can distribute/modify this program under
|
|
|
|
the terms of the Ruby Distribute License.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2000-09-21 02:58:01 -04:00
|
|
|
Japanese version of this document is in "net" full package.
|
|
|
|
You can get it from RAA
|
|
|
|
(Ruby Application Archive: http://www.ruby-lang.org/en/raa.html).
|
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
== Net::POP3
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
Net::Protocol
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Class Methods
|
|
|
|
|
|
|
|
: new( address = 'localhost', port = 110 )
|
2000-05-18 04:57:37 -04:00
|
|
|
creates a new Net::POP3 object.
|
|
|
|
This method does not open TCP connection yet.
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2000-05-18 04:57:37 -04:00
|
|
|
: start( address = 'localhost', port = 110, *protoargs )
|
|
|
|
: start( address = 'localhost', port = 110, *protoargs ) {|pop| .... }
|
|
|
|
equals to Net::POP3.new( address, port ).start( *protoargs )
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
=== Methods
|
|
|
|
|
|
|
|
: start( account, password )
|
2000-05-18 04:57:37 -04:00
|
|
|
: start( account, password ) {|pop| .... }
|
|
|
|
starts POP3 session.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2000-09-21 02:58:01 -04:00
|
|
|
When called with block, give a POP3 object to block and
|
2000-05-18 04:57:37 -04:00
|
|
|
close session after block call is finished.
|
|
|
|
|
|
|
|
: each {|popmail| .... }
|
1999-12-29 06:14:04 -05:00
|
|
|
This method is equals to "pop3.mails.each"
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
: mails
|
2000-05-18 04:57:37 -04:00
|
|
|
an array of ((URL:#POPMail)).
|
|
|
|
This array is renewed when session started.
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2000-10-12 05:54:32 -04:00
|
|
|
: reset
|
|
|
|
reset the session. All "deleted mark" are removed.
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
== Net::APOP
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
This class defines no new methods.
|
|
|
|
Only difference from POP3 is using APOP authentification.
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
=== Super Class
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
Net::POP3
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
== Net::POPMail
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
A class of mail which exists on POP server.
|
|
|
|
|
|
|
|
=== Super Class
|
|
|
|
|
|
|
|
Object
|
|
|
|
|
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
=== Methods
|
1999-10-13 03:29:15 -04:00
|
|
|
|
2000-02-21 10:25:37 -05:00
|
|
|
: all( dest = '' )
|
1999-10-13 03:29:15 -04:00
|
|
|
: pop
|
|
|
|
: mail
|
2000-02-21 10:25:37 -05:00
|
|
|
This method fetches a mail and write to 'dest' using '<<' method.
|
|
|
|
|
|
|
|
# usage example
|
|
|
|
|
|
|
|
mailarr = []
|
|
|
|
POP3.start( 'localhost', 110 ) do |pop|
|
|
|
|
pop.each do |popm|
|
|
|
|
mailarr.push popm.pop # all() returns 'dest' (this time, string)
|
|
|
|
# or, you can also
|
|
|
|
# popm.pop( $stdout ) # write mail to stdout
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
: all {|str| .... }
|
2000-09-21 02:58:01 -04:00
|
|
|
You can call all/pop/mail with block.
|
2000-02-21 10:25:37 -05:00
|
|
|
argument 'str' is a read string (a part of mail).
|
|
|
|
|
|
|
|
# usage example
|
|
|
|
|
|
|
|
POP3.start( 'localhost', 110 ) do |pop|
|
|
|
|
pop.mails[0].pop do |str| # pop only first mail...
|
|
|
|
_do_anything_( str )
|
|
|
|
end
|
|
|
|
end
|
1999-10-13 03:29:15 -04:00
|
|
|
|
|
|
|
: header
|
|
|
|
This method fetches only mail header.
|
|
|
|
|
|
|
|
: top( lines )
|
|
|
|
This method fetches mail header and 'lines' lines body.
|
|
|
|
|
|
|
|
: delete
|
|
|
|
: delete!
|
|
|
|
This method deletes mail.
|
|
|
|
|
|
|
|
: size
|
|
|
|
size of mail(bytes)
|
|
|
|
|
|
|
|
: deleted?
|
|
|
|
true if mail was deleted
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
require 'net/protocol'
|
|
|
|
require 'md5'
|
|
|
|
|
|
|
|
|
|
|
|
module Net
|
|
|
|
|
|
|
|
class POP3 < Protocol
|
|
|
|
|
|
|
|
protocol_param :port, '110'
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 02:04:40 -04:00
|
|
|
protocol_param :command_type, '::Net::NetPrivate::POP3Command'
|
2000-06-12 12:42:46 -04:00
|
|
|
|
|
|
|
protocol_param :mail_type, '::Net::POPMail'
|
|
|
|
|
|
|
|
def initialize( addr = nil, port = nil )
|
|
|
|
super
|
2000-10-12 05:54:32 -04:00
|
|
|
@mails = nil
|
2000-06-12 12:42:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
attr :mails
|
|
|
|
|
2000-10-12 05:54:32 -04:00
|
|
|
def each( &block )
|
|
|
|
io_check
|
|
|
|
@mails.each &block
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset
|
|
|
|
io_check
|
|
|
|
@command.rset
|
|
|
|
@mails.each do |m|
|
|
|
|
m.instance_eval { @deleted = false }
|
|
|
|
end
|
2000-06-12 12:42:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def do_start( acnt, pwd )
|
|
|
|
@command.auth( acnt, pwd )
|
|
|
|
|
|
|
|
@mails = []
|
2000-10-12 05:54:32 -04:00
|
|
|
mtype = type.mail_type
|
2000-06-12 12:42:46 -04:00
|
|
|
@command.list.each_with_index do |size,idx|
|
|
|
|
if size then
|
2000-10-12 05:54:32 -04:00
|
|
|
@mails.push mtype.new( idx, size, @command )
|
2000-06-12 12:42:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
@mails.freeze
|
|
|
|
end
|
|
|
|
|
2000-10-12 05:54:32 -04:00
|
|
|
def io_check
|
|
|
|
if not @socket or @socket.closed? then
|
|
|
|
raise IOError, 'pop session is not opened yet'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
POP = POP3
|
|
|
|
POPSession = POP3
|
|
|
|
POP3Session = POP3
|
|
|
|
|
|
|
|
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 02:04:40 -04:00
|
|
|
class APOP < POP3
|
|
|
|
protocol_param :command_type, 'Net::NetPrivate::APOPCommand'
|
|
|
|
end
|
|
|
|
|
|
|
|
APOPSession = APOP
|
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
class POPMail
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-29 06:14:04 -05:00
|
|
|
def initialize( n, s, cmd )
|
|
|
|
@num = n
|
|
|
|
@size = s
|
|
|
|
@command = cmd
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
@deleted = false
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
attr :size
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2000-06-12 12:42:46 -04:00
|
|
|
def inspect
|
|
|
|
"#<#{type} #{@num}#{@deleted ? ' deleted' : ''}>"
|
|
|
|
end
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def all( dest = '' )
|
2000-09-12 01:37:38 -04:00
|
|
|
if block_given? then
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 02:04:40 -04:00
|
|
|
dest = NetPrivate::ReadAdapter.new( Proc.new )
|
2000-02-21 10:25:37 -05:00
|
|
|
end
|
1999-12-29 06:14:04 -05:00
|
|
|
@command.retr( @num, dest )
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
alias pop all
|
|
|
|
alias mail all
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def top( lines, dest = '' )
|
1999-12-29 06:14:04 -05:00
|
|
|
@command.top( @num, lines, dest )
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
1999-10-18 05:03:16 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def header( dest = '' )
|
2000-10-12 05:54:32 -04:00
|
|
|
top 0, dest
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def delete
|
1999-12-29 06:14:04 -05:00
|
|
|
@command.dele( @num )
|
1999-12-17 10:00:13 -05:00
|
|
|
@deleted = true
|
|
|
|
end
|
2000-10-12 05:54:32 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
alias delete! delete
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
def deleted?
|
|
|
|
@deleted
|
|
|
|
end
|
|
|
|
|
|
|
|
def uidl
|
1999-12-29 06:14:04 -05:00
|
|
|
@command.uidl @num
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
1999-10-13 03:29:15 -04:00
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 02:04:40 -04:00
|
|
|
module NetPrivate
|
1999-09-22 03:32:33 -04:00
|
|
|
|
1999-12-17 10:00:13 -05:00
|
|
|
|
|
|
|
class POP3Command < Command
|
|
|
|
|
|
|
|
def initialize( sock )
|
|
|
|
super
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
check_reply SuccessCode
|
|
|
|
}
|
1999-12-17 10:00:13 -05:00
|
|
|
end
|
|
|
|
|
1999-09-22 03:32:33 -04:00
|
|
|
def auth( acnt, pass )
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
@socket.writeline 'USER ' + acnt
|
|
|
|
check_reply_auth
|
1999-09-22 03:32:33 -04:00
|
|
|
|
2000-03-27 10:52:27 -05:00
|
|
|
@socket.writeline 'PASS ' + pass
|
|
|
|
check_reply_auth
|
|
|
|
}
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def list
|
|
|
|
arr = []
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
getok 'LIST'
|
|
|
|
@socket.read_pendlist do |line|
|
|
|
|
num, siz = line.split( / +/o )
|
|
|
|
arr[ num.to_i ] = siz.to_i
|
|
|
|
end
|
|
|
|
}
|
|
|
|
arr
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def rset
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
getok 'RSET'
|
|
|
|
}
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
def top( num, lines = 0, dest = '' )
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
getok sprintf( 'TOP %d %d', num, lines )
|
|
|
|
@socket.read_pendstr( dest )
|
|
|
|
}
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def retr( num, dest = '', &block )
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
getok sprintf( 'RETR %d', num )
|
|
|
|
@socket.read_pendstr( dest, &block )
|
|
|
|
}
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def dele( num )
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
getok sprintf( 'DELE %d', num )
|
|
|
|
}
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
1999-10-18 05:03:16 -04:00
|
|
|
def uidl( num )
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
|
|
|
getok( sprintf 'UIDL %d', num ).msg.split(' ')[1]
|
|
|
|
}
|
1999-10-18 05:03:16 -04:00
|
|
|
end
|
|
|
|
|
2000-03-27 10:52:27 -05:00
|
|
|
def quit
|
|
|
|
critical {
|
|
|
|
getok 'QUIT'
|
|
|
|
}
|
|
|
|
end
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
|
2000-03-27 10:52:27 -05:00
|
|
|
private
|
1999-09-22 03:32:33 -04:00
|
|
|
|
|
|
|
def check_reply_auth
|
|
|
|
begin
|
|
|
|
cod = check_reply( SuccessCode )
|
|
|
|
rescue ProtocolError
|
|
|
|
raise ProtoAuthError, 'Fail to POP authentication'
|
|
|
|
end
|
|
|
|
|
|
|
|
return cod
|
|
|
|
end
|
|
|
|
|
|
|
|
def get_reply
|
|
|
|
str = @socket.readline
|
|
|
|
|
|
|
|
if /\A\+/ === str then
|
2000-03-31 08:02:40 -05:00
|
|
|
return Response.new( SuccessCode, str[0,3], str[3, str.size - 3].strip )
|
1999-09-22 03:32:33 -04:00
|
|
|
else
|
2000-03-31 08:02:40 -05:00
|
|
|
return Response.new( ErrorCode, str[0,4], str[4, str.size - 4].strip )
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
class APOPCommand < POP3Command
|
|
|
|
|
|
|
|
def initialize( sock )
|
|
|
|
rep = super( sock )
|
|
|
|
|
2000-04-14 06:41:35 -04:00
|
|
|
m = /<.+>/.match( rep.msg )
|
|
|
|
unless m then
|
1999-09-22 03:32:33 -04:00
|
|
|
raise ProtoAuthError, "This is not APOP server: can't login"
|
|
|
|
end
|
2000-04-14 06:41:35 -04:00
|
|
|
@stamp = m[0]
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
2000-05-17 14:39:43 -04:00
|
|
|
def auth( account, pass )
|
2000-03-27 10:52:27 -05:00
|
|
|
critical {
|
2000-05-17 14:39:43 -04:00
|
|
|
@socket.writeline sprintf( 'APOP %s %s',
|
|
|
|
account, MD5.new(@stamp + pass).hexdigest )
|
2000-03-27 10:52:27 -05:00
|
|
|
check_reply_auth
|
|
|
|
}
|
1999-09-22 03:32:33 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
aamine
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb: 1.1.26.
* lib/net/protocol.rb, smtp.rb, pop.rb, http.rb:
add module Net::NetPrivate and its inner classes
{Read,Write}Adapter, Command, Socket,
SMTPCommand, POP3Command, APOPCommand, HTTPCommand
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@826 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
2000-07-12 02:04:40 -04:00
|
|
|
|
|
|
|
end # module Net::NetPrivate
|
|
|
|
|
|
|
|
end # module Net
|