mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
* lib/net/smtp.rb: changes coding style.
* lib/net/pop.rb: ditto. * lib/net/protocol.rb: ditto. git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@3071 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
15911a95bf
commit
f80af5150a
4 changed files with 131 additions and 118 deletions
10
ChangeLog
10
ChangeLog
|
@ -1,6 +1,14 @@
|
|||
Thu Nov 21 20:53:06 2002 Minero Aoki <aamine@loveruby.net>
|
||||
|
||||
* lib/net/smtp.rb: changes coding style.
|
||||
|
||||
* lib/net/pop.rb: ditto.
|
||||
|
||||
* lib/net/protocol.rb: ditto.
|
||||
|
||||
Thu Nov 21 20:17:08 2002 Minero Aoki <aamine@loveruby.net>
|
||||
|
||||
* lib/net/http.rb: unify coding style.
|
||||
* lib/net/http.rb: changes coding style.
|
||||
|
||||
Thu Nov 21 20:04:06 2002 Minero Aoki <aamine@loveruby.net>
|
||||
|
||||
|
|
|
@ -341,8 +341,8 @@ module Net
|
|||
protocol_param :socket_type, '::Net::InternetMessageIO'
|
||||
|
||||
|
||||
def POP3.APOP( bool )
|
||||
bool ? APOP : POP3
|
||||
def POP3.APOP( isapop )
|
||||
isapop ? APOP : POP3
|
||||
end
|
||||
|
||||
def POP3.foreach( address, port = nil,
|
||||
|
@ -387,10 +387,15 @@ module Net
|
|||
|
||||
def do_start( account, password )
|
||||
conn_socket
|
||||
@command = (@apop ? self.class.apop_command_type : self.class.command_type).new(socket())
|
||||
conn_command
|
||||
@command.auth account, password
|
||||
end
|
||||
|
||||
def conn_command
|
||||
@command = (@apop ? self.class.apop_command_type :
|
||||
self.class.command_type).new(socket())
|
||||
end
|
||||
|
||||
def do_finish
|
||||
@mails = nil
|
||||
disconn_command
|
||||
|
@ -408,9 +413,9 @@ module Net
|
|||
return @mails if @mails
|
||||
|
||||
mails = []
|
||||
mtype = self.class.mail_type
|
||||
mailclass = self.class.mail_type
|
||||
command().list.each_with_index do |size,idx|
|
||||
mails.push mtype.new(idx, size, command()) if size
|
||||
mails.push mailclass.new(idx, size, command()) if size
|
||||
end
|
||||
@mails = mails.freeze
|
||||
end
|
||||
|
@ -479,7 +484,7 @@ module Net
|
|||
end
|
||||
|
||||
def pop( dest = '', &block )
|
||||
if block then
|
||||
if block
|
||||
dest = ReadAdapter.new(block)
|
||||
end
|
||||
@command.retr @num, dest
|
||||
|
@ -575,7 +580,7 @@ module Net
|
|||
|
||||
def uidl( num )
|
||||
atomic {
|
||||
getok( sprintf('UIDL %d', num) ).message.split(' ')[1]
|
||||
getok(sprintf('UIDL %d', num)).message.split(/ /)[1]
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -598,7 +603,7 @@ module Net
|
|||
def get_reply
|
||||
str = @socket.readline
|
||||
|
||||
if /\A\+/ === str then
|
||||
if /\A\+/ === str
|
||||
Response.new(SuccessCode, str[0,3], str[3, str.size - 3].strip)
|
||||
else
|
||||
Response.new(ErrorCode, str[0,4], str[4, str.size - 4].strip)
|
||||
|
|
|
@ -26,7 +26,7 @@ module Net
|
|||
class Protocol
|
||||
|
||||
Version = '1.2.3'
|
||||
Revision = %q$Revision$.split(/\s+/)[1]
|
||||
Revision = '$Revision$'.slice(/[\d\.]+/)
|
||||
|
||||
|
||||
class << self
|
||||
|
@ -38,11 +38,11 @@ module Net
|
|||
private
|
||||
|
||||
def protocol_param( name, val )
|
||||
module_eval <<-End, __FILE__, __LINE__ + 1
|
||||
module_eval <<-EOS, __FILE__, __LINE__ + 1
|
||||
def self.#{name.id2name}
|
||||
#{val}
|
||||
end
|
||||
End
|
||||
EOS
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -65,9 +65,10 @@ module Net
|
|||
|
||||
def Protocol.start( address, port = nil, *args )
|
||||
instance = new(address, port)
|
||||
|
||||
if block_given? then
|
||||
instance.start(*args) { return yield(instance) }
|
||||
if block_given?
|
||||
instance.start(*args) {
|
||||
return yield(instance)
|
||||
}
|
||||
else
|
||||
instance.start(*args)
|
||||
instance
|
||||
|
@ -125,7 +126,7 @@ module Net
|
|||
def start( *args )
|
||||
@started and raise IOError, 'protocol has been opened already'
|
||||
|
||||
if block_given? then
|
||||
if block_given?
|
||||
begin
|
||||
do_start(*args)
|
||||
@started = true
|
||||
|
@ -173,7 +174,7 @@ module Net
|
|||
public
|
||||
|
||||
def finish
|
||||
@started or raise IOError, 'closing already closed protocol'
|
||||
raise IOError, 'closing already closed protocol' unless @started
|
||||
do_finish
|
||||
@started = false
|
||||
nil
|
||||
|
@ -189,9 +190,7 @@ module Net
|
|||
end
|
||||
|
||||
def disconn_socket
|
||||
if @socket and not @socket.closed? then
|
||||
@socket.close
|
||||
end
|
||||
@socket.close if @socket and not @socket.closed?
|
||||
@socket = nil
|
||||
end
|
||||
|
||||
|
@ -274,7 +273,9 @@ module Net
|
|||
end
|
||||
|
||||
def ===( response )
|
||||
response.code_type.parents.each {|c| c == self and return true }
|
||||
response.code_type.parents.each do |c|
|
||||
return true if c == self
|
||||
end
|
||||
false
|
||||
end
|
||||
|
||||
|
@ -395,14 +396,14 @@ module Net
|
|||
def connect( otime )
|
||||
D "opening connection to #{@address}..."
|
||||
timeout(otime) {
|
||||
@socket = TCPSocket.new( @address, @port )
|
||||
@socket = TCPsocket.new(@address, @port)
|
||||
}
|
||||
@rbuf = ''
|
||||
end
|
||||
private :connect
|
||||
|
||||
def close
|
||||
if @socket then
|
||||
if @socket
|
||||
@socket.close
|
||||
D 'closed'
|
||||
else
|
||||
|
@ -438,7 +439,7 @@ module Net
|
|||
|
||||
rsize = 0
|
||||
begin
|
||||
while rsize + @rbuf.size < len do
|
||||
while rsize + @rbuf.size < len
|
||||
rsize += rbuf_moveto(dest, @rbuf.size)
|
||||
rbuf_fill
|
||||
end
|
||||
|
@ -456,7 +457,7 @@ module Net
|
|||
|
||||
rsize = 0
|
||||
begin
|
||||
while true do
|
||||
while true
|
||||
rsize += rbuf_moveto(dest, @rbuf.size)
|
||||
rbuf_fill
|
||||
end
|
||||
|
@ -471,9 +472,7 @@ module Net
|
|||
def readuntil( target, ignore = false )
|
||||
dest = ''
|
||||
begin
|
||||
while true do
|
||||
idx = @rbuf.index(target)
|
||||
break if idx
|
||||
until idx = @rbuf.index(target)
|
||||
rbuf_fill
|
||||
end
|
||||
rbuf_moveto dest, idx + target.size
|
||||
|
@ -495,7 +494,7 @@ module Net
|
|||
BLOCK_SIZE = 1024
|
||||
|
||||
def rbuf_fill
|
||||
until IO.select [@socket], nil, nil, @read_timeout do
|
||||
until IO.select [@socket], nil, nil, @read_timeout
|
||||
on_read_timeout
|
||||
end
|
||||
@rbuf << @socket.sysread(BLOCK_SIZE)
|
||||
|
@ -521,7 +520,7 @@ module Net
|
|||
D_off 'reading text...'
|
||||
|
||||
rsize = 0
|
||||
while (str = readuntil("\r\n")) != ".\r\n" do
|
||||
while (str = readuntil("\r\n")) != ".\r\n"
|
||||
rsize += str.size
|
||||
dest << str.sub(/\A\./, '')
|
||||
end
|
||||
|
@ -532,7 +531,7 @@ module Net
|
|||
|
||||
# private use only (cannot handle 'break')
|
||||
def each_list_item
|
||||
while (str = readuntil("\r\n")) != ".\r\n" do
|
||||
while (str = readuntil("\r\n")) != ".\r\n"
|
||||
yield str.chop
|
||||
end
|
||||
end
|
||||
|
@ -624,13 +623,13 @@ module Net
|
|||
|
||||
yield
|
||||
|
||||
if not @wbuf.empty? then # unterminated last line
|
||||
if @wbuf[-1] == ?\r then
|
||||
if not @wbuf.empty? # unterminated last line
|
||||
if @wbuf[-1] == ?\r
|
||||
@wbuf.chop!
|
||||
end
|
||||
@wbuf.concat "\r\n"
|
||||
do_write @wbuf
|
||||
elsif @writtensize == 0 then # empty src
|
||||
elsif @writtensize == 0 # empty src
|
||||
do_write "\r\n"
|
||||
end
|
||||
do_write ".\r\n"
|
||||
|
@ -645,9 +644,9 @@ module Net
|
|||
adding(src) do
|
||||
beg = 0
|
||||
buf = @wbuf
|
||||
while buf.index( /\n|\r\n|\r/, beg ) do
|
||||
while buf.index(/\n|\r\n|\r/, beg)
|
||||
m = Regexp.last_match
|
||||
if m.begin(0) == buf.size - 1 and buf[-1] == ?\r then
|
||||
if m.begin(0) == buf.size - 1 and buf[-1] == ?\r
|
||||
# "...\r" : can follow "\n..."
|
||||
break
|
||||
end
|
||||
|
@ -661,7 +660,7 @@ module Net
|
|||
end
|
||||
|
||||
def adding( src )
|
||||
i = nil
|
||||
i = s = nil
|
||||
|
||||
case src
|
||||
when String
|
||||
|
@ -671,20 +670,16 @@ module Net
|
|||
end
|
||||
|
||||
when File
|
||||
while true do
|
||||
i = src.read(2048)
|
||||
break unless i
|
||||
i[0,0] = @wbuf
|
||||
@wbuf = i
|
||||
while s = src.read(2048)
|
||||
s[0,0] = @wbuf
|
||||
@wbuf = s
|
||||
yield
|
||||
end
|
||||
|
||||
else
|
||||
src.each do |i|
|
||||
@wbuf << i
|
||||
if @wbuf.size > 2048 then
|
||||
yield
|
||||
end
|
||||
src.each do |s|
|
||||
@wbuf << s
|
||||
yield if @wbuf.size > 2048
|
||||
end
|
||||
yield unless @wbuf.empty?
|
||||
end
|
||||
|
@ -759,7 +754,7 @@ module Net
|
|||
end
|
||||
|
||||
def <<( str )
|
||||
call_block str, &@block if @block
|
||||
call_block(str, &@block) if @block
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -21,11 +21,15 @@ This module provides your program the functions to send internet
|
|||
mail via SMTP, Simple Mail Transfer Protocol. For details of
|
||||
SMTP itself, refer [RFC2821] ((<URL:http://www.ietf.org/rfc/rfc2821.txt>)).
|
||||
|
||||
== What This Module is NOT?
|
||||
== What is NOT This Module?
|
||||
|
||||
This module does NOT provide the functions to compose internet
|
||||
mail. You must create it by yourself. For details of internet mail
|
||||
format, see [RFC2822] ((<URL:http://www.ietf.org/rfc/rfc2822.txt>)).
|
||||
This module does NOT provide functions to compose internet mails.
|
||||
You must create it by yourself. If you want better mail support,
|
||||
try RubyMail or TMail. You can get both libraries from RAA.
|
||||
((<URL:http://www.ruby-lang.org/en/raa.html>))
|
||||
|
||||
FYI: official documentation of internet mail is:
|
||||
[RFC2822] ((<URL:http://www.ietf.org/rfc/rfc2822.txt>)).
|
||||
|
||||
== Examples
|
||||
|
||||
|
@ -34,7 +38,7 @@ format, see [RFC2822] ((<URL:http://www.ietf.org/rfc/rfc2822.txt>)).
|
|||
You must open connection to SMTP server before sending mails.
|
||||
First argument is the address of SMTP server, and second argument
|
||||
is port number. Using SMTP.start with block is the most simple way
|
||||
to do it. SMTP Connection is closed automatically after block is
|
||||
to do it. SMTP connection is closed automatically after block is
|
||||
executed.
|
||||
|
||||
require 'net/smtp'
|
||||
|
@ -48,10 +52,7 @@ for you.
|
|||
|
||||
Then you can send mail.
|
||||
|
||||
require 'net/smtp'
|
||||
|
||||
Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp|
|
||||
smtp.send_mail <<EndOfMail, 'your@mail.address', 'to@some.domain'
|
||||
mail_text = <<END_OF_MAIL
|
||||
From: Your Name <your@mail.address>
|
||||
To: Dest Address <to@some.domain>
|
||||
Subject: test mail
|
||||
|
@ -59,7 +60,13 @@ Then you can send mail.
|
|||
Message-Id: <unique.message.id.string@some.domain>
|
||||
|
||||
This is test mail.
|
||||
EndOfMail
|
||||
END_OF_MAIL
|
||||
|
||||
require 'net/smtp'
|
||||
Net::SMTP.start('your.smtp.server', 25) {|smtp|
|
||||
smtp.send_mail mail_text,
|
||||
'your@mail.address',
|
||||
'his_addess@example.com'
|
||||
}
|
||||
|
||||
=== Closing Session
|
||||
|
@ -79,9 +86,9 @@ more beautiful and simple.
|
|||
smtp.send_mail mail_string, 'from@address', 'to@address'
|
||||
}
|
||||
|
||||
=== Sending Mails from Any Sources
|
||||
=== Sending Mails From non-String Sources
|
||||
|
||||
In an example above I sent mail from String (here document literal).
|
||||
In an example above I has sent mail from String (here document literal).
|
||||
SMTP#send_mail accepts any objects which has "each" method
|
||||
like File and Array.
|
||||
|
||||
|
@ -248,13 +255,13 @@ module Net
|
|||
conn_command
|
||||
|
||||
begin
|
||||
if @esmtp then
|
||||
if @esmtp
|
||||
command().ehlo helo
|
||||
else
|
||||
command().helo helo
|
||||
end
|
||||
rescue ProtocolError
|
||||
if @esmtp then
|
||||
if @esmtp
|
||||
@esmtp = false
|
||||
command().error_ok
|
||||
retry
|
||||
|
@ -263,14 +270,12 @@ module Net
|
|||
end
|
||||
end
|
||||
|
||||
if user or secret then
|
||||
(user and secret) or
|
||||
raise ArgumentError, 'both of account and password are required'
|
||||
|
||||
if user or secret
|
||||
raise ArgumentError, 'both of account and password are required'\
|
||||
unless user and secret
|
||||
mid = 'auth_' + (authtype || 'cram_md5').to_s
|
||||
command().respond_to? mid or
|
||||
raise ArgumentError, "wrong auth type #{authtype.to_s}"
|
||||
|
||||
raise ArgumentError, "wrong auth type #{authtype}"\
|
||||
unless command().respond_to?(mid)
|
||||
command().__send__ mid, user, secret
|
||||
end
|
||||
end
|
||||
|
@ -289,7 +294,7 @@ module Net
|
|||
|
||||
def send_mail( mailsrc, from_addr, *to_addrs )
|
||||
do_ready from_addr, to_addrs.flatten
|
||||
command().write_mail(mailsrc)
|
||||
command().write_mail mailsrc
|
||||
end
|
||||
|
||||
alias sendmail send_mail
|
||||
|
@ -304,7 +309,7 @@ module Net
|
|||
def do_ready( from_addr, to_addrs )
|
||||
raise ArgumentError, 'mail destination does not given' if to_addrs.empty?
|
||||
command().mailfrom from_addr
|
||||
command().rcpt(to_addrs)
|
||||
command().rcpt to_addrs
|
||||
end
|
||||
|
||||
end
|
||||
|
@ -345,7 +350,7 @@ module Net
|
|||
def auth_cram_md5( user, secret )
|
||||
atomic {
|
||||
rep = getok('AUTH CRAM-MD5', ContinueCode)
|
||||
challenge = rep.msg.split(' ')[1].unpack('m')[0]
|
||||
challenge = rep.msg.split(/ /)[1].unpack('m')[0]
|
||||
secret = Digest::MD5.digest(secret) if secret.size > 64
|
||||
|
||||
isecret = secret + "\0" * (64 - secret.size)
|
||||
|
@ -421,9 +426,9 @@ module Net
|
|||
|
||||
def read_reply
|
||||
arr = []
|
||||
while true do
|
||||
while true
|
||||
str = @socket.readline
|
||||
break unless str[3] == ?- # ex: "210-..."
|
||||
break unless str[3] == ?- # "210-PIPELINING"
|
||||
arr.push str
|
||||
end
|
||||
arr.push str
|
||||
|
|
Loading…
Add table
Reference in a new issue