From 79633d3bb8334fc58be49b6d86fcd6a40f569f75 Mon Sep 17 00:00:00 2001 From: aamine Date: Fri, 22 Dec 2000 18:40:55 +0000 Subject: aamine * lib/net/protocol.rb: set @closed false in Socket#reopen. * lib/net/pop.rb: add POP3.foreach, delete_all. * lib/net/pop.rb: add POP3#delete_all. * lib/net/http.rb: add HTTP.version_1_1, version_1_2 * lib/net/http.rb: refactoring. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@1071 b2dd03c8-39d4-4d8f-98ff-823fe69b080e --- lib/net/http.rb | 24 +++++----- lib/net/pop.rb | 128 +++++++++++++++++++++++++++++++++++++++++++--------- lib/net/protocol.rb | 75 ++++++++++++++++++------------ lib/net/smtp.rb | 2 +- 4 files changed, 167 insertions(+), 62 deletions(-) (limited to 'lib') diff --git a/lib/net/http.rb b/lib/net/http.rb index 6b7ee6d875..011d9c8845 100644 --- a/lib/net/http.rb +++ b/lib/net/http.rb @@ -1,6 +1,6 @@ =begin -= net/http.rb version 1.1.31 += net/http.rb version 1.1.32 maintained by Minero Aoki This file is derived from "http-access.rb". @@ -55,8 +55,8 @@ You can get it from RAA : start {|http| .... } creates a new Net::HTTP object and starts HTTP session. - When this method is called with block, gives HTTP object to block - and close HTTP session after block call finished. + When this method is called with block, gives a HTTP object to block + and close HTTP session after returning from the block. : proxy? true if self is a HTTP proxy class @@ -233,16 +233,16 @@ All "key" is case-insensitive. = http.rb version 1.2 features -You can use 1.2 features by calling HTTP.new_implementation. And -calling Net::HTTP.old_implementation allows to use 1.1 features. +You can use 1.2 features by calling HTTP.version_1_2. And +calling Net::HTTP.version_1_1 allows to use 1.1 features. # example HTTP.start {|http1| ...(http1 has 1.1 features)... } - HTTP.new_implementation + HTTP.version_1_2 HTTP.start {|http2| ...(http2 has 1.2 features)... } - HTTP.old_implementation + HTTP.version_1_1 HTTP.start {|http3| ...(http3 has 1.1 features)... } == Method (only diff to 1.1) @@ -293,7 +293,7 @@ module Net def new( address = nil, port = nil, p_addr = nil, p_port = nil ) c = p_addr ? self::Proxy(p_addr, p_port) : self i = c.orig_new( address, port ) - setvar i + setimplv i i end @@ -340,17 +340,17 @@ module Net #class << self - def self.new_implementation + def self.version_1_2 @@newimpl = true end - def self.old_implementation + def self.version_1_1 @@newimpl = false end #private - def self.setvar( obj ) + def self.setimplv( obj ) f = @@newimpl obj.instance_eval { @newimpl = f } end @@ -575,7 +575,7 @@ module Net end def edit_path( path ) - 'http://' + address + (port == HTTP.port ? '' : ":#{port}") + path + 'http://' + address + (port == type.port ? '' : ":#{port}") + path end end diff --git a/lib/net/pop.rb b/lib/net/pop.rb index a1bcaa88c5..4f6eb930a4 100644 --- a/lib/net/pop.rb +++ b/lib/net/pop.rb @@ -1,6 +1,6 @@ =begin -= net/pop.rb version 1.1.31 += net/pop.rb version 1.1.32 written by Minero Aoki @@ -25,26 +25,82 @@ Net::Protocol creates a new Net::POP3 object. This method does not open TCP connection yet. -: start( address = 'localhost', port = 110, *protoargs ) -: start( address = 'localhost', port = 110, *protoargs ) {|pop| .... } - equals to Net::POP3.new( address, port ).start( *protoargs ) +: start( address = 'localhost', port = 110, account, password ) +: start( address = 'localhost', port = 110, account, password ) {|pop| .... } + equals to Net::POP3.new( address, port ).start( account, password ) + # typical usage + Net::POP3.start( addr, port, acnt, pass ) do |pop| + pop.each_mail do |m| + any_file.write m.pop + m.delete + end + end + +: foreach( address = 'localhost', port = 110, account, password ) {|mail| .... } + starts protocol and iterate for each POPMail object. + This method equals to + + Net::POP3.start( address, port, account, password ) do |pop| + pop.each do |m| + yield m + end + end + + . + + # typical usage + Net::POP3.foreach( addr, nil, acnt, pass ) do |m| + m.pop file + m.delete + end + +: delete_all( address = 'localhost', port = 110, account, password ) +: delete_all( address = 'localhost', port = 110, account, password ) {|mail| .... } + starts POP3 session and delete all mails. + If block is given, iterates for each POPMail object before delete. + + # typical usage + Net::POP3.delete_all( addr, nil, acnt, pass ) do |m| + m.pop file + end + === Methods : start( account, password ) : start( account, password ) {|pop| .... } starts POP3 session. - When called with block, give a POP3 object to block and - close session after block call is finished. - -: each {|popmail| .... } - This method is equals to "pop3.mails.each" + When called with block, gives a POP3 object to block and + closes the session after block call finish. : mails an array of ((URL:#POPMail)). This array is renewed when session started. +: each_mail {|popmail| .... } +: each {|popmail| .... } + is equals to "pop3.mails.each" + +: delete_all +: delete_all {|popmail| .... } + deletes all mails. + If called with block, gives mails to the block before deleting. + + # example 1 + # pop and delete all mails + n = 1 + pop.delete_all do |m| + File.open("inbox/#{n}") {|f| f.write m.pop } + n += 1 + end + + # example 2 + # clear all mails on server + Net::POP3.start( addr, port, acc, pass ) do |pop| + pop.delete_all + end + : reset reset the session. All "deleted mark" are removed. @@ -70,31 +126,33 @@ Object === Methods -: all( dest = '' ) -: pop -: mail +: pop( dest = '' ) This method fetches a mail and write to 'dest' using '<<' method. # usage example mailarr = [] POP3.start( 'localhost', 110 ) do |pop| - pop.each do |popm| + pop.each_mail do |popm| mailarr.push popm.pop # all() returns 'dest' (this time, string) # or, you can also # popm.pop( $stdout ) # write mail to stdout + + # maybe you also want to delete mail after popping + popm.delete end end -: all {|str| .... } - You can call all/pop/mail with block. - argument 'str' is a read string (a part of mail). +: pop {|str| .... } + If pop() is called with block, it gives the block part strings of a mail. # usage example - POP3.start( 'localhost', 110 ) do |pop| - pop.mails[0].pop do |str| # pop only first mail... - _do_anything_( str ) + POP3.start( 'localhost', 110 ) do |pop3| + pop3.each_mail do |m| + m.pop do |str| + # do anything + end end end @@ -129,6 +187,25 @@ module Net protocol_param :mail_type, '::Net::POPMail' + class << self + + def foreach( address = nil, port = nil, + account = nil, password = nil, &block ) + start( address, port, account, password ) do |pop| + pop.each_mail( &block ) + end + end + + def delete_all( address = nil, port = nil, + account = nil, password = nil, &block ) + start( address, port, account, password ) do |pop| + pop.delete_all( &block ) + end + end + + end + + def initialize( addr = nil, port = nil ) super @mails = nil @@ -136,9 +213,18 @@ module Net attr :mails - def each( &block ) + def each_mail( &block ) io_check - @mails.each &block + @mails.each( &block ) + end + + alias each each_mail + + def delete_all + @mails.each do |m| + yield m if block_given? + m.delete unless m.deleted? + end end def reset diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb index 34b379361b..161024cfe2 100644 --- a/lib/net/protocol.rb +++ b/lib/net/protocol.rb @@ -1,6 +1,6 @@ =begin -= net/protocol.rb version 1.1.31 += net/protocol.rb version 1.1.32 written by Minero Aoki @@ -65,7 +65,7 @@ module Net class Protocol - Version = '1.1.31' + Version = '1.1.32' class << self @@ -113,15 +113,18 @@ module Net @address = addr || 'localhost' @port = port || type.port - @active = false - @pipe = nil - @command = nil @socket = nil + + @active = false + @pipe = nil end - attr_reader :address, :port, - :command, :socket + attr_reader :address + attr_reader :port + + attr_reader :command + attr_reader :socket def inspect "#<#{type} #{address}:#{port} open=#{active?}>" @@ -131,20 +134,29 @@ module Net def start( *args ) return false if active? - begin - connect - do_start( *args ) - @active = true - yield self if block_given? - ensure - finish if block_given? + if block_given? then + begin + _start args + yield self + ensure + finish + end + else + _start args end end + def _start( args ) + connect + do_start( *args ) + @active = true + end + private :_start + def finish return false unless active? - do_finish + do_finish unless @command.critical? disconnect @active = false true @@ -375,24 +387,35 @@ module Net rep.error! end - def getok( line, ok = SuccessCode ) + def getok( line, expect = SuccessCode ) @socket.writeline line - check_reply ok + check_reply expect end + # + # error handle + # + + public + + def critical? + @critical + end + + def error_ok + @critical = false + end + + private + def critical - return if @critical @critical = true ret = yield @critical = false ret end - def critical? - @critical - end - def begin_critical ret = @critical @critical = true @@ -403,11 +426,6 @@ module Net @critical = false end - def error_ok - @critical = false - end - public :error_ok - end @@ -441,10 +459,11 @@ module Net def reopen unless closed? then - @socket.close + close @buffer = '' end @socket = TCPsocket.new( @addr, @port ) + @closed = false end attr :socket, true diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb index f573409063..9679984e2c 100644 --- a/lib/net/smtp.rb +++ b/lib/net/smtp.rb @@ -1,6 +1,6 @@ =begin -= net/smtp.rb version 1.1.31 += net/smtp.rb version 1.1.32 written by Minero Aoki -- cgit v1.2.3