diff options
author | aamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-12-30 19:18:45 +0000 |
---|---|---|
committer | aamine <aamine@b2dd03c8-39d4-4d8f-98ff-823fe69b080e> | 2001-12-30 19:18:45 +0000 |
commit | f3d9a0cc213f8b09155f063e057afb576178557e (patch) | |
tree | 92cf1665627f99c3fccdb3cf806f809348b3bd5c /lib/net/pop.rb | |
parent | 653f326bb108108b82890e37400b4ca6e851e7ed (diff) | |
download | ruby-f3d9a0cc213f8b09155f063e057afb576178557e.tar.gz |
aamine
* lib/net/protocol.rb: Protocol#start returns the return value of block.
* lib/net/protocol.rb: set timeout limit by default.
* lib/net/protocol.rb: new methods WriteAdapter#write, puts, print, printf.
* lib/net/http.rb: rename HTTP#get2 to request_get, post2 to request_post ...
* lib/net/smtp.rb: should not resolve HELO domain automatically.
git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1951 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
Diffstat (limited to 'lib/net/pop.rb')
-rw-r--r-- | lib/net/pop.rb | 178 |
1 files changed, 96 insertions, 82 deletions
diff --git a/lib/net/pop.rb b/lib/net/pop.rb index 02729ca259..1dbbc9aeb0 100644 --- a/lib/net/pop.rb +++ b/lib/net/pop.rb @@ -367,86 +367,96 @@ module Net end + def auth_only( account, password ) + active? and raise IOError, 'opening already opened POP session' + start( account, password ) { + ; + } + end + + + # + # connection + # + def initialize( addr, port = nil, apop = false ) super addr, port @mails = nil @apop = false end - def auth_only( account, password ) - begin - connect - @active = true - @command.auth address(), port() - @command.quit - ensure - @active = false - disconnect - end + private + + def do_start( account, password ) + conn_socket + @command = (@apop ? type.apop_command_type : type.command_type).new(socket()) + @command.auth account, password + end + + def do_finish + @mails = nil + disconn_command + disconn_socket end - attr :mails + + # + # POP operations + # + + public + + def mails + return @mails if @mails + + mails = [] + mtype = type.mail_type + command().list.each_with_index do |size,idx| + mails.push mtype.new(idx, size, command()) if size + end + @mails = mails.freeze + end def each_mail( &block ) - io_check - @mails.each( &block ) + mails().each( &block ) end alias each each_mail def delete_all - io_check - @mails.each do |m| + mails().each do |m| yield m if block_given? m.delete unless m.deleted? end end def reset - io_check - @command.rset - @mails.each do |m| + command().rset + mails().each do |m| m.instance_eval { @deleted = false } end end - private - - def conn_command( sock ) - @command = - (@apop ? type.apop_command_type : type.command_type).new(sock) - end - - def do_start( account, password ) - @command.auth account, password - - mails = [] - mtype = type.mail_type - @command.list.each_with_index do |size,idx| - mails.push mtype.new(idx, size, @command) if size - end - @mails = mails.freeze + def command + io_check + super end def io_check - (not @socket or @socket.closed?) and - raise IOError, 'pop session is not opened yet' + (not socket() or socket().closed?) and + raise IOError, 'POP session is not opened yet' end end - POP = POP3 - POPSession = POP3 - POP3Session = POP3 + POP = POP3 class APOP < POP3 protocol_param :command_type, '::Net::APOPCommand' end - APOPSession = APOP - class POPMail @@ -500,86 +510,84 @@ module Net end - class POP3Command < Command def initialize( sock ) super - critical { - check_reply SuccessCode + atomic { + check_reply SuccessCode } end def auth( account, pass ) - critical { - @socket.writeline 'USER ' + account - check_reply_auth + atomic { + @socket.writeline 'USER ' + account + check_reply_auth - @socket.writeline 'PASS ' + pass - check_reply_auth + @socket.writeline 'PASS ' + pass + check_reply_auth } end def list arr = [] - critical { - getok 'LIST' - @socket.read_pendlist do |line| - m = /\A(\d+)[ \t]+(\d+)/.match(line) or - raise BadResponse, "illegal response: #{line}" - arr[ m[1].to_i ] = m[2].to_i - end + atomic { + getok 'LIST' + @socket.read_pendlist do |line| + m = /\A(\d+)[ \t]+(\d+)/.match(line) or + raise BadResponse, "illegal response: #{line}" + arr[ m[1].to_i ] = m[2].to_i + end } arr end def rset - critical { - getok 'RSET' + atomic { + getok 'RSET' } end def top( num, lines = 0, dest = '' ) - critical { - getok sprintf( 'TOP %d %d', num, lines ) - @socket.read_pendstr dest + atomic { + getok sprintf( 'TOP %d %d', num, lines ) + @socket.read_pendstr dest } end def retr( num, dest = '', &block ) - critical { - getok sprintf('RETR %d', num) - @socket.read_pendstr dest, &block + atomic { + getok sprintf('RETR %d', num) + @socket.read_pendstr dest, &block } end def dele( num ) - critical { - getok sprintf('DELE %d', num) + atomic { + getok sprintf('DELE %d', num) } end def uidl( num ) - critical { - getok( sprintf('UIDL %d', num) ).msg.split(' ')[1] + atomic { + getok( sprintf('UIDL %d', num) ).msg.split(' ')[1] } end def quit - critical { - getok 'QUIT' + atomic { + getok 'QUIT' } end - private def check_reply_auth begin - return check_reply( SuccessCode ) + return check_reply(SuccessCode) rescue ProtocolError => err - raise ProtoAuthError.new( 'Fail to POP authentication', err.response ) + raise ProtoAuthError.new('Fail to POP authentication', err.response) end end @@ -599,22 +607,28 @@ module Net class APOPCommand < POP3Command def initialize( sock ) - rep = super( sock ) - - m = /<.+>/.match( rep.msg ) or - raise ProtoAuthError.new( "not APOP server: cannot login", nil ) + response = super(sock) + m = /<.+>/.match(response.msg) or + raise ProtoAuthError.new("not APOP server: cannot login", nil) @stamp = m[0] end def auth( account, pass ) - critical { - @socket.writeline sprintf( 'APOP %s %s', - account, - Digest::MD5.hexdigest(@stamp + pass) ) - check_reply_auth + atomic { + @socket.writeline sprintf('APOP %s %s', + account, + Digest::MD5.hexdigest(@stamp + pass)) + check_reply_auth } end end + + # for backward compatibility + + POPSession = POP3 + POP3Session = POP3 + APOPSession = APOP + end # module Net |