class Mail::Ruby18

Public Class Methods

b_value_decode(str) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 90
def Ruby18.b_value_decode(str)
  match = str.match(/\=\?(.+)?\?[Bb]\?(.*)\?\=/m)
  if match
    encoding = match[1]
    str = Ruby18.decode_base64(match[2])
    str = transcode_charset(str, encoding)
  end
  str
end
b_value_encode(str, encoding) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 83
def Ruby18.b_value_encode(str, encoding)
  # Ruby 1.8 requires an encoding to work
  raise ArgumentError, "Must supply an encoding" if encoding.nil?
  encoding = encoding.to_s.upcase.gsub('_', '-')
  [Encodings::Base64.encode(str), normalize_iconv_charset_encoding(encoding)]
end
bracket( str ) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 37
def Ruby18.bracket( str )
  str = $1 if str =~ /^\<(.*)?\>$/
  str = escape_bracket( str )
  '<' + str + '>'
end
decode_base64(str) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 43
def Ruby18.decode_base64(str)
  Base64.decode64(str) if str
end
decode_utf7(str) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 79
def Ruby18.decode_utf7(str)
  Net::IMAP.decode_utf7(str)
end
encode_base64(str) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 47
def Ruby18.encode_base64(str)
  Base64.encode64(str)
end
escape_bracket( str ) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 28
def Ruby18.escape_bracket( str )
  re = /\\\>/
  str = str.gsub(re) { |s| '>'}
  re = /\\\</
  str = str.gsub(re) { |s| '<'}
  re = /([\<\>])/          # Only match unescaped parens
  str.gsub(re) { |s| '\\' + s }
end
escape_paren( str ) click to toggle source

Escapes any parenthesis in a string that are unescaped. This can’t use the Ruby 1.9.1 regexp feature of negative look behind so we have to do two replacement, first unescape everything, then re-escape it

# File lib/mail/version_specific/ruby_1_8.rb, line 13
def Ruby18.escape_paren( str )
  re = /\\\)/
  str = str.gsub(re) { |s| ')'}
  re = /\\\(/
  str = str.gsub(re) { |s| '('}
  re = /([\(\)])/          # Only match unescaped parens
  str.gsub(re) { |s| '\\' + s }
end
get_constant(klass, string) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 55
def Ruby18.get_constant(klass, string)
  klass.const_get( string )
end
has_constant?(klass, string) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 51
def Ruby18.has_constant?(klass, string)
  klass.constants.include?( string )
end
param_decode(str, encoding) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 120
def Ruby18.param_decode(str, encoding)
  str = URI.unescape(str)
  if encoding
    transcode_charset(str, encoding)
  else
    str
  end
end
param_encode(str) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 129
def Ruby18.param_encode(str)
  encoding = $KCODE.to_s.downcase
  language = Configuration.instance.param_encode_language
  "#{encoding}'#{language}'#{URI.escape(str)}"
end
paren( str ) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 22
def Ruby18.paren( str )
  str = $1 if str =~ /^\((.*)?\)$/
  str = escape_paren( str )
  '(' + str + ')'
end
q_value_decode(str) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 107
def Ruby18.q_value_decode(str)
  match = str.match(/\=\?(.+)?\?[Qq]\?(.*)\?\=/m)
  if match
    encoding = match[1]
    string = match[2].gsub(/_/, '=20')
    # Remove trailing = if it exists in a Q encoding
    string = string.sub(/\=$/, '')
    str = Encodings::QuotedPrintable.decode(string)
    str = transcode_charset(str, encoding)
  end
  str
end
q_value_encode(str, encoding) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 100
def Ruby18.q_value_encode(str, encoding)
  # Ruby 1.8 requires an encoding to work
  raise ArgumentError, "Must supply an encoding" if encoding.nil?
  encoding = encoding.to_s.upcase.gsub('_', '-')
  [Encodings::QuotedPrintable.encode(str), encoding]
end
string_byteslice(str, *args) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 135
def Ruby18.string_byteslice(str, *args)
  str.slice(*args)
end
transcode_charset(str, from_encoding, to_encoding = 'UTF-8') click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 59
def Ruby18.transcode_charset(str, from_encoding, to_encoding = 'UTF-8')
  case from_encoding
  when /utf-?7/i
    decode_utf7(str)
  else
    retried = false
    begin
      Iconv.conv("#{normalize_iconv_charset_encoding(to_encoding)}//IGNORE", normalize_iconv_charset_encoding(from_encoding), str)
    rescue Iconv::InvalidEncoding
      if retried
        raise
      else
        from_encoding = 'ASCII'
        retried = true
        retry
      end
    end
  end
end

Private Class Methods

normalize_iconv_charset_encoding(encoding) click to toggle source
# File lib/mail/version_specific/ruby_1_8.rb, line 141
def Ruby18.normalize_iconv_charset_encoding(encoding)
  case encoding.upcase
  when 'UTF8', 'UTF_8'
    'UTF-8'
  when 'UTF16', 'UTF-16'
    'UTF-16BE'
  when 'UTF32', 'UTF-32'
    'UTF-32BE'
  when 'KS_C_5601-1987'
    'CP949'
  else
    # Fall back to ASCII for charsets that Iconv doesn't recognize
    begin
      Iconv.new('UTF-8', encoding)
    rescue Iconv::InvalidEncoding => e
      'ASCII'
    else
      encoding
    end
  end
end