Class: Mail::Ruby19

Inherits:
Object show all
Defined in:
lib/mail/version_specific/ruby_1_9.rb

Class Method Summary collapse

Class Method Details

.b_value_decode(str) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/mail/version_specific/ruby_1_9.rb', line 51

def Ruby19.b_value_decode(str)
  match = str.match(/\=\?(.+)?\?[Bb]\?(.+)?\?\=/m)
  if match
    encoding = match[1]
    str = Ruby19.decode_base64(match[2])
    str.force_encoding(fix_encoding(encoding))
  end
  decoded = str.encode("utf-8", :invalid => :replace, :replace => "")
  decoded.valid_encoding? ? decoded : decoded.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8")
end

.b_value_encode(str, encoding = nil) ⇒ Object



46
47
48
49
# File 'lib/mail/version_specific/ruby_1_9.rb', line 46

def Ruby19.b_value_encode(str, encoding = nil)
  encoding = str.encoding.to_s
  [Ruby19.encode_base64(str), encoding]
end

.bracket(str) ⇒ Object



24
25
26
27
28
# File 'lib/mail/version_specific/ruby_1_9.rb', line 24

def Ruby19.bracket( str )
  str = $1 if str =~ /^\<(.*)?\>$/
  str = escape_bracket( str )
  '<' + str + '>'
end

.decode_base64(str) ⇒ Object



30
31
32
# File 'lib/mail/version_specific/ruby_1_9.rb', line 30

def Ruby19.decode_base64(str)
  str.unpack( 'm' ).first
end

.encode_base64(str) ⇒ Object



34
35
36
# File 'lib/mail/version_specific/ruby_1_9.rb', line 34

def Ruby19.encode_base64(str)
  [str].pack( 'm' )
end

.escape_bracket(str) ⇒ Object



19
20
21
22
# File 'lib/mail/version_specific/ruby_1_9.rb', line 19

def Ruby19.escape_bracket( str )
  re = /(?<!\\)([\<\>])/          # Only match unescaped brackets
  str.gsub(re) { |s| '\\' + s }
end

.escape_paren(str) ⇒ Object

Escapes any parenthesis in a string that are unescaped this uses a Ruby 1.9.1 regexp feature of negative look behind



8
9
10
11
# File 'lib/mail/version_specific/ruby_1_9.rb', line 8

def Ruby19.escape_paren( str )
  re = /(?<!\\)([\(\)])/          # Only match unescaped parens
  str.gsub(re) { |s| '\\' + s }
end

.fix_encoding(encoding) ⇒ Object

mails somtimes includes invalid encodings like iso885915 or utf8 so we transform them to iso885915 or utf8 TODO: add this as a test somewhere Encoding.list.map{|e| [e.to_s.upcase==fix_encoding(e.to_s.downcase.gsub(“-”, “”)), e.to_s] }.select {|a,b| !b}

Encoding.list.map{|e| [e.to_s==fix_encoding(e.to_s), e.to_s] }.select {|a,b| !b}


103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/mail/version_specific/ruby_1_9.rb', line 103

def Ruby19.fix_encoding(encoding)
  case encoding
    # ISO-8859-15, ISO-2022-JP and alike
    when /iso-?(\d{4})-?(\w{1,2})/i then return "ISO-#{$1}-#{$2}"
    # "ISO-2022-JP-KDDI"  and alike
    when /iso-?(\d{4})-?(\w{1,2})-?(\w*)/i then return "ISO-#{$1}-#{$2}-#{$3}"
    # UTF-8, UTF-32BE and alike
    when /utf-?(\d{1,2})?(\w{1,2})/i then return "UTF-#{$1}#{$2}".gsub(/\A(UTF-(?:16|32))\z/, '\\1BE')
    # Windows-1252 and alike
    when /Windows-?(.*)/i then return "Windows-#{$1}"
    when /^8bit$/ then return "ASCII-8BIT"
    #more aliases to be added if needed
    else return encoding
  end
end

.get_constant(klass, string) ⇒ Object



42
43
44
# File 'lib/mail/version_specific/ruby_1_9.rb', line 42

def Ruby19.get_constant(klass, string)
  klass.const_get( string )
end

.has_constant?(klass, string) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/mail/version_specific/ruby_1_9.rb', line 38

def Ruby19.has_constant?(klass, string)
  klass.const_defined?( string, false )
end

.param_decode(str, encoding) ⇒ Object



83
84
85
86
87
# File 'lib/mail/version_specific/ruby_1_9.rb', line 83

def Ruby19.param_decode(str, encoding)
  string = uri_parser.unescape(str)
  string.force_encoding(encoding) if encoding
  string
end

.param_encode(str) ⇒ Object



89
90
91
92
93
# File 'lib/mail/version_specific/ruby_1_9.rb', line 89

def Ruby19.param_encode(str)
  encoding = str.encoding.to_s.downcase
  language = Configuration.instance.param_encode_language
  "#{encoding}'#{language}'#{uri_parser.escape(str)}"
end

.paren(str) ⇒ Object



13
14
15
16
17
# File 'lib/mail/version_specific/ruby_1_9.rb', line 13

def Ruby19.paren( str )
  str = $1 if str =~ /^\((.*)?\)$/
  str = escape_paren( str )
  '(' + str + ')'
end

.q_value_decode(str) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/mail/version_specific/ruby_1_9.rb', line 67

def Ruby19.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.force_encoding(fix_encoding(encoding))
  end
  decoded = str.encode("utf-8", :invalid => :replace, :replace => "")
  decoded.valid_encoding? ? decoded : decoded.encode("utf-16le", :invalid => :replace, :replace => "").encode("utf-8")
rescue Encoding::UndefinedConversionError
  str.dup.force_encoding("utf-8")
end

.q_value_encode(str, encoding = nil) ⇒ Object



62
63
64
65
# File 'lib/mail/version_specific/ruby_1_9.rb', line 62

def Ruby19.q_value_encode(str, encoding = nil)
  encoding = str.encoding.to_s
  [Encodings::QuotedPrintable.encode(str), encoding]
end

.uri_parserObject



95
96
97
# File 'lib/mail/version_specific/ruby_1_9.rb', line 95

def Ruby19.uri_parser
  @uri_parser ||= URI::Parser.new
end