diff --git a/ChangeLog b/ChangeLog index 0a624767f8..fa9342450d 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,6 +1,14 @@ +Thu Nov 21 20:53:06 2002 Minero Aoki + + * 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 - * lib/net/http.rb: unify coding style. + * lib/net/http.rb: changes coding style. Thu Nov 21 20:04:06 2002 Minero Aoki diff --git a/lib/net/pop.rb b/lib/net/pop.rb index 5966e6eecb..840aafb46c 100644 --- a/lib/net/pop.rb +++ b/lib/net/pop.rb @@ -341,33 +341,33 @@ 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, - account = nil, password = nil, &block ) - start( address, port, account, password ) {|pop| - pop.each_mail( &block ) + account = nil, password = nil, &block ) + start(address, port, account, password) {|pop| + pop.each_mail(&block) } end def POP3.delete_all( address, port = nil, - account = nil, password = nil, &block ) - start( address, port, account, password ) {|pop| - pop.delete_all( &block ) + account = nil, password = nil, &block ) + start(address, port, account, password) {|pop| + pop.delete_all(&block) } end def POP3.auth_only( address, port = nil, - account = nil, password = nil ) - new( address, port ).auth_only account, password + account = nil, password = nil ) + new(address, port).auth_only account, password end def auth_only( account, password ) raise IOError, 'opening already opened POP session' if active? - start( account, password ) { + start(account, password) { # none } end @@ -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,15 +413,15 @@ 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 def each_mail( &block ) - mails().each( &block ) + mails().each(&block) end alias each each_mail @@ -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 @@ -540,7 +545,7 @@ module Net @socket.each_list_item do |line| m = /\A(\d+)[ \t]+(\d+)/.match(line) or raise BadResponse, "illegal response: #{line}" - arr[ m[1].to_i ] = m[2].to_i + arr[m[1].to_i] = m[2].to_i end } arr @@ -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,10 +603,10 @@ module Net def get_reply str = @socket.readline - if /\A\+/ === str then - Response.new( SuccessCode, str[0,3], str[3, str.size - 3].strip ) + 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 ) + Response.new(ErrorCode, str[0,4], str[4, str.size - 4].strip) end end diff --git a/lib/net/protocol.rb b/lib/net/protocol.rb index bdb60e158d..9da1ad8909 100644 --- a/lib/net/protocol.rb +++ b/lib/net/protocol.rb @@ -25,8 +25,8 @@ module Net class Protocol - Version = '1.2.3' - Revision = %q$Revision$.split(/\s+/)[1] + Version = '1.2.3' + Revision = '$Revision$'.slice(/[\d\.]+/) class << self @@ -38,11 +38,11 @@ module Net private def protocol_param( name, val ) - module_eval <<-End, __FILE__, __LINE__ + 1 - def self.#{name.id2name} - #{val} - end - End + module_eval <<-EOS, __FILE__, __LINE__ + 1 + def self.#{name.id2name} + #{val} + 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,9 +126,9 @@ 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 ) + do_start(*args) @started = true return yield(self) ensure @@ -135,7 +136,7 @@ module Net end end - do_start( *args ) + do_start(*args) @started = true self end @@ -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 @@ -284,17 +285,17 @@ module Net end - ReplyCode = Code.new( [], ProtoUnknownError ) - InformationCode = ReplyCode.mkchild( ProtoUnknownError ) - SuccessCode = ReplyCode.mkchild( ProtoUnknownError ) - ContinueCode = ReplyCode.mkchild( ProtoUnknownError ) - ErrorCode = ReplyCode.mkchild( ProtocolError ) - SyntaxErrorCode = ErrorCode.mkchild( ProtoSyntaxError ) - FatalErrorCode = ErrorCode.mkchild( ProtoFatalError ) - ServerErrorCode = ErrorCode.mkchild( ProtoServerError ) - AuthErrorCode = ErrorCode.mkchild( ProtoAuthError ) - RetriableCode = ReplyCode.mkchild( ProtoRetriableError ) - UnknownCode = ReplyCode.mkchild( ProtoUnknownError ) + ReplyCode = Code.new([], ProtoUnknownError) + InformationCode = ReplyCode.mkchild(ProtoUnknownError) + SuccessCode = ReplyCode.mkchild(ProtoUnknownError) + ContinueCode = ReplyCode.mkchild(ProtoUnknownError) + ErrorCode = ReplyCode.mkchild(ProtocolError) + SyntaxErrorCode = ErrorCode.mkchild(ProtoSyntaxError) + FatalErrorCode = ErrorCode.mkchild(ProtoFatalError) + ServerErrorCode = ErrorCode.mkchild(ProtoServerError) + AuthErrorCode = ErrorCode.mkchild(ProtoAuthError) + RetriableCode = ReplyCode.mkchild(ProtoRetriableError) + UnknownCode = ReplyCode.mkchild(ProtoUnknownError) class Command @@ -394,15 +395,15 @@ module Net def connect( otime ) D "opening connection to #{@address}..." - timeout( otime ) { - @socket = TCPSocket.new( @address, @port ) + timeout(otime) { + @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 @@ -610,7 +609,7 @@ module Net def wpend_in( src ) line = nil pre = @writtensize - each_crlf_line( src ) do |line| + each_crlf_line(src) do |line| do_write '.' if line[0] == ?. do_write line 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" @@ -642,12 +641,12 @@ module Net def each_crlf_line( src ) str = m = beg = nil - adding( src ) do + 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,30 +660,26 @@ module Net end def adding( src ) - i = nil + i = s = nil case src when String - 0.step( src.size - 1, 2048 ) do |i| + 0.step(src.size - 1, 2048) do |i| @wbuf << src[i,2048] yield 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 diff --git a/lib/net/smtp.rb b/lib/net/smtp.rb index a3802f6cb0..4b70f9e11a 100644 --- a/lib/net/smtp.rb +++ b/lib/net/smtp.rb @@ -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] (()). -== 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] (()). +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. +(()) + +FYI: official documentation of internet mail is: +[RFC2822] (()). == Examples @@ -34,11 +38,11 @@ format, see [RFC2822] (()). 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' - Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| + Net::SMTP.start('your.smtp.server', 25) {|smtp| # use smtp object only in this block } @@ -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 < To: Dest Address Subject: test mail @@ -59,7 +60,13 @@ Then you can send mail. Message-Id: 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 @@ -70,24 +77,24 @@ closes session automatically. I strongly recommend later one. It is more beautiful and simple. # using SMTP#finish - smtp = Net::SMTP.start( 'your.smtp.server', 25 ) + smtp = Net::SMTP.start('your.smtp.server', 25) smtp.send_mail mail_string, 'from@address', 'to@address' smtp.finish # using block form of SMTP.start - Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| + Net::SMTP.start('your.smtp.server', 25) {|smtp| 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. require 'net/smtp' - Net::SMTP.start( 'your.smtp.server', 25 ) {|smtp| - File.open( 'Mail/draft/1' ) {|f| + Net::SMTP.start('your.smtp.server', 25) {|smtp| + File.open('Mail/draft/1') {|f| smtp.send_mail f, 'your@mail.address', 'to@some.domain' } } @@ -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 @@ -344,18 +349,18 @@ module Net # "CRAM-MD5" authentication [RFC2195] def auth_cram_md5( user, secret ) atomic { - rep = getok( 'AUTH CRAM-MD5', ContinueCode ) - challenge = rep.msg.split(' ')[1].unpack('m')[0] + rep = getok('AUTH CRAM-MD5', ContinueCode) + challenge = rep.msg.split(/ /)[1].unpack('m')[0] secret = Digest::MD5.digest(secret) if secret.size > 64 isecret = secret + "\0" * (64 - secret.size) osecret = isecret.dup - 0.upto( 63 ) do |i| + 0.upto(63) do |i| isecret[i] ^= 0x36 osecret[i] ^= 0x5c end - tmp = Digest::MD5.digest( isecret + challenge ) - tmp = Digest::MD5.hexdigest( osecret + tmp ) + tmp = Digest::MD5.digest(isecret + challenge) + tmp = Digest::MD5.hexdigest(osecret + tmp) getok [user + ' ' + tmp].pack('m').chomp } @@ -416,14 +421,14 @@ module Net end klass ||= UnknownCode - Response.new( klass, stat, arr.join('') ) + Response.new(klass, stat, arr.join('')) end 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