~ubuntu-branches/ubuntu/quantal/ruby1.9.1/quantal

« back to all changes in this revision

Viewing changes to lib/net/smtp.rb

  • Committer: Bazaar Package Importer
  • Author(s): Lucas Nussbaum
  • Date: 2011-09-24 19:16:17 UTC
  • mfrom: (1.1.8 upstream) (13.1.7 experimental)
  • Revision ID: james.westby@ubuntu.com-20110924191617-o1qz4rcmqjot8zuy
Tags: 1.9.3~rc1-1
* New upstream release: 1.9.3 RC1.
  + Includes load.c fixes. Closes: #639959.
* Upload to unstable.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
# NOTE: You can find Japanese version of this document at:
15
15
# http://www.ruby-lang.org/ja/man/html/net_smtp.html
16
16
#
17
 
# $Id: smtp.rb 30324 2010-12-23 12:46:02Z yugui $
 
17
# $Id: smtp.rb 31710 2011-05-23 00:21:10Z drbrain $
18
18
#
19
19
# See Net::SMTP for documentation.
20
20
#
172
172
  #
173
173
  class SMTP
174
174
 
175
 
    Revision = %q$Revision: 30324 $.split[1]
 
175
    Revision = %q$Revision: 31710 $.split[1]
176
176
 
177
177
    # The default SMTP port number, 25.
178
178
    def SMTP.default_port
229
229
      "#<#{self.class} #{@address}:#{@port} started=#{@started}>"
230
230
    end
231
231
 
232
 
    # +true+ if the SMTP object uses ESMTP (which it does by default).
233
 
    def esmtp?
234
 
      @esmtp
235
 
    end
236
 
 
237
232
    #
238
233
    # Set whether to use ESMTP or not.  This should be done before
239
234
    # calling #start.  Note that if #start is called in ESMTP mode,
241
236
    # object will automatically switch to plain SMTP mode and
242
237
    # retry (but not vice versa).
243
238
    #
244
 
    def esmtp=(bool)
245
 
      @esmtp = bool
246
 
    end
 
239
    attr_accessor :esmtp
247
240
 
248
 
    alias esmtp esmtp?
 
241
    # +true+ if the SMTP object uses ESMTP (which it does by default).
 
242
    alias :esmtp? :esmtp
249
243
 
250
244
    # true if server advertises STARTTLS.
251
245
    # You cannot get valid value before opening SMTP session.
973
967
      end
974
968
    end
975
969
 
 
970
    # This class represents a response received by the SMTP server. Instances
 
971
    # of this class are created by the SMTP class; they should not be directly
 
972
    # created by the user. For more information on SMTP responses, view
 
973
    # {Section 4.2 of RFC 5321}[http://tools.ietf.org/html/rfc5321#section-4.2]
976
974
    class Response
 
975
      # Parses the received response and separates the reply code and the human
 
976
      # readable reply text
977
977
      def self.parse(str)
978
978
        new(str[0,3], str)
979
979
      end
980
980
 
 
981
      # Creates a new instance of the Response class and sets the status and
 
982
      # string attributes
981
983
      def initialize(status, string)
982
984
        @status = status
983
985
        @string = string
984
986
      end
985
987
 
 
988
      # The three digit reply code of the SMTP response
986
989
      attr_reader :status
 
990
 
 
991
      # The human readable reply text of the SMTP response
987
992
      attr_reader :string
988
993
 
 
994
      # Takes the first digit of the reply code to determine the status type
989
995
      def status_type_char
990
996
        @status[0, 1]
991
997
      end
992
998
 
 
999
      # Determines whether the response received was a Positive Completion
 
1000
      # reply (2xx reply code)
993
1001
      def success?
994
1002
        status_type_char() == '2'
995
1003
      end
996
1004
 
 
1005
      # Determines whether the response received was a Positive Intermediate
 
1006
      # reply (3xx reply code)
997
1007
      def continue?
998
1008
        status_type_char() == '3'
999
1009
      end
1000
1010
 
 
1011
      # The first line of the human readable reply text
1001
1012
      def message
1002
1013
        @string.lines.first
1003
1014
      end
1004
1015
 
 
1016
      # Creates a CRAM-MD5 challenge. You can view more information on CRAM-MD5
 
1017
      # on Wikipedia: http://en.wikipedia.org/wiki/CRAM-MD5
1005
1018
      def cram_md5_challenge
1006
1019
        @string.split(/ /)[1].unpack('m')[0]
1007
1020
      end
1008
1021
 
 
1022
      # Returns a hash of the human readable reply text in the response if it
 
1023
      # is multiple lines. It does not return the first line. The key of the
 
1024
      # hash is the first word the value of the hash is an array with each word
 
1025
      # thereafter being a value in the array
1009
1026
      def capabilities
1010
1027
        return {} unless @string[3, 1] == '-'
1011
1028
        h = {}
1012
1029
        @string.lines.drop(1).each do |line|
1013
 
          k, *v = line[4..-1].chomp.split(nil)
 
1030
          k, *v = line[4..-1].chomp.split
1014
1031
          h[k] = v
1015
1032
        end
1016
1033
        h
1017
1034
      end
1018
1035
 
 
1036
      # Determines whether there was an error and raies the appropriate error
 
1037
      # based on the reply code of the response
1019
1038
      def exception_class
1020
1039
        case @status
1021
1040
        when /\A4/  then SMTPServerBusy