diff --git a/lib/mail.rb b/lib/mail.rb index 738ee60c4..12b1ee652 100644 --- a/lib/mail.rb +++ b/lib/mail.rb @@ -9,8 +9,7 @@ module Mail # :doc: require 'net/smtp' require 'mini_mime' - require 'mail/version_specific/ruby_1_9' - RubyVer = Ruby19 + require 'mail/version_specific' require 'mail/version' diff --git a/lib/mail/version_specific/ruby_1_9.rb b/lib/mail/version_specific.rb similarity index 86% rename from lib/mail/version_specific/ruby_1_9.rb rename to lib/mail/version_specific.rb index b8940ead0..dea57edce 100644 --- a/lib/mail/version_specific/ruby_1_9.rb +++ b/lib/mail/version_specific.rb @@ -2,14 +2,14 @@ # frozen_string_literal: true module Mail - class Ruby19 + class RubyVer class StrictCharsetEncoder def encode(string, charset) case charset when /utf-?7/i - Mail::Ruby19.decode_utf7(string) + Mail::RubyVer.decode_utf7(string) else - string.force_encoding(Mail::Ruby19.pick_encoding(charset)) + string.force_encoding(Mail::RubyVer.pick_encoding(charset)) end end end @@ -18,7 +18,7 @@ class BestEffortCharsetEncoder def encode(string, charset) case charset when /utf-?7/i - Mail::Ruby19.decode_utf7(string) + Mail::RubyVer.decode_utf7(string) else string.force_encoding(pick_encoding(charset)) end @@ -35,7 +35,7 @@ def pick_encoding(charset) else charset end - Mail::Ruby19.pick_encoding(charset) + Mail::RubyVer.pick_encoding(charset) end end @@ -46,55 +46,55 @@ class << self # Escapes any parenthesis in a string that are unescaped this uses # a Ruby 1.9.1 regexp feature of negative look behind - def Ruby19.escape_paren( str ) + def RubyVer.escape_paren( str ) re = /(?])/ # Only match unescaped brackets str.gsub(re) { |s| '\\' + s } end - def Ruby19.bracket( str ) + def RubyVer.bracket( str ) str = ::Mail::Utilities.unbracket( str ) str = escape_bracket( str ) '<' + str + '>' end - def Ruby19.decode_base64(str) + def RubyVer.decode_base64(str) if !str.end_with?("=") && str.length % 4 != 0 str = str.ljust((str.length + 3) & ~3, "=") end str.unpack( 'm' ).first end - def Ruby19.encode_base64(str) + def RubyVer.encode_base64(str) [str].pack( 'm' ) end - def Ruby19.has_constant?(klass, string) + def RubyVer.has_constant?(klass, string) klass.const_defined?( string, false ) end - def Ruby19.get_constant(klass, string) + def RubyVer.get_constant(klass, string) klass.const_get( string ) end - def Ruby19.transcode_charset(str, from_encoding, to_encoding = Encoding::UTF_8) + def RubyVer.transcode_charset(str, from_encoding, to_encoding = Encoding::UTF_8) to_encoding = Encoding.find(to_encoding) replacement_char = to_encoding == Encoding::UTF_8 ? '�' : '?' charset_encoder.encode(str.dup, from_encoding).encode(to_encoding, :undef => :replace, :invalid => :replace, :replace => replacement_char) end # From Ruby stdlib Net::IMAP - def Ruby19.encode_utf7(string) + def RubyVer.encode_utf7(string) string.gsub(/(&)|[^\x20-\x7e]+/) do if $1 "&-" @@ -105,7 +105,7 @@ def Ruby19.encode_utf7(string) end.force_encoding(Encoding::ASCII_8BIT) end - def Ruby19.decode_utf7(utf7) + def RubyVer.decode_utf7(utf7) utf7.gsub(/&([^-]+)?-/n) do if $1 ($1.tr(",", "/") + "===").unpack("m")[0].encode(Encoding::UTF_8, Encoding::UTF_16BE) @@ -115,16 +115,16 @@ def Ruby19.decode_utf7(utf7) end end - def Ruby19.b_value_encode(str, encoding = nil) + def RubyVer.b_value_encode(str, encoding = nil) encoding = str.encoding.to_s - [Ruby19.encode_base64(str), encoding] + [RubyVer.encode_base64(str), encoding] end - def Ruby19.b_value_decode(str) + def RubyVer.b_value_decode(str) match = str.match(/\=\?(.+)?\?[Bb]\?(.*)\?\=/m) if match charset = match[1] - str = Ruby19.decode_base64(match[2]) + str = RubyVer.decode_base64(match[2]) str = charset_encoder.encode(str, charset) end transcode_to_scrubbed_utf8(str) @@ -133,12 +133,12 @@ def Ruby19.b_value_decode(str) str.dup.force_encoding(Encoding::UTF_8) end - def Ruby19.q_value_encode(str, encoding = nil) + def RubyVer.q_value_encode(str, encoding = nil) encoding = str.encoding.to_s [Encodings::QuotedPrintable.encode(str), encoding] end - def Ruby19.q_value_decode(str) + def RubyVer.q_value_decode(str) match = str.match(/\=\?(.+)?\?[Qq]\?(.*)\?\=/m) if match charset = match[1] @@ -157,7 +157,7 @@ def Ruby19.q_value_decode(str) str.dup.force_encoding(Encoding::UTF_8) end - def Ruby19.param_decode(str, encoding) + def RubyVer.param_decode(str, encoding) str = uri_parser.unescape(str) str = charset_encoder.encode(str, encoding) if encoding transcode_to_scrubbed_utf8(str) @@ -166,13 +166,13 @@ def Ruby19.param_decode(str, encoding) str.dup.force_encoding(Encoding::UTF_8) end - def Ruby19.param_encode(str) + def RubyVer.param_encode(str) encoding = str.encoding.to_s.downcase language = Configuration.instance.param_encode_language "#{encoding}'#{language}'#{uri_parser.escape(str)}" end - def Ruby19.uri_parser + def RubyVer.uri_parser URI::DEFAULT_PARSER end @@ -182,7 +182,7 @@ def Ruby19.uri_parser # TODO: add this as a test somewhere: # Encoding.list.map { |e| [e.to_s.upcase == pick_encoding(e.to_s.downcase.gsub("-", "")), e.to_s] }.select {|a,b| !b} # Encoding.list.map { |e| [e.to_s == pick_encoding(e.to_s), e.to_s] }.select {|a,b| !b} - def Ruby19.pick_encoding(charset) + def RubyVer.pick_encoding(charset) charset = charset.to_s encoding = case charset.downcase @@ -242,7 +242,7 @@ def Ruby19.pick_encoding(charset) convert_to_encoding(encoding) end - def Ruby19.string_byteslice(str, *args) + def RubyVer.string_byteslice(str, *args) str.byteslice(*args) end diff --git a/spec/mail/encoding_spec.rb b/spec/mail/encoding_spec.rb index 5fdd93a05..c698f65cd 100644 --- a/spec/mail/encoding_spec.rb +++ b/spec/mail/encoding_spec.rb @@ -199,7 +199,7 @@ describe "#pick_encoding" do it "picks binary for nil" do expect { ::Encoding.find(nil) }.to raise_error(TypeError) - expect(Mail::Ruby19.pick_encoding(nil)).to eq(Encoding::BINARY) + expect(Mail::RubyVer.pick_encoding(nil)).to eq(Encoding::BINARY) end { @@ -210,7 +210,7 @@ }.each do |from, to| it "should support #{from}" do expect { ::Encoding.find(from) }.to raise_error(ArgumentError) - expect(Mail::Ruby19.pick_encoding(from)).to eq(to) + expect(Mail::RubyVer.pick_encoding(from)).to eq(to) end end end diff --git a/spec/mail/encodings_spec.rb b/spec/mail/encodings_spec.rb index 98f438a62..764a81f98 100644 --- a/spec/mail/encodings_spec.rb +++ b/spec/mail/encodings_spec.rb @@ -851,10 +851,10 @@ def encode(str, charset) end def with_encoder(encoder) - old, Mail::Ruby19.charset_encoder = Mail::Ruby19.charset_encoder, encoder + old, Mail::RubyVer.charset_encoder = Mail::RubyVer.charset_encoder, encoder yield ensure - Mail::Ruby19.charset_encoder = old + Mail::RubyVer.charset_encoder = old end it "can use a custom encoder" do @@ -871,7 +871,7 @@ def with_encoder(encoder) end it "can convert ansi with best effort" do - with_encoder Mail::Ruby19::BestEffortCharsetEncoder.new do + with_encoder Mail::RubyVer::BestEffortCharsetEncoder.new do expect(Mail::Encodings.value_decode("=?windows-1258?Q?SV=3A_Spr=F8sm=E5l_om_tilbod?=")).to eq "SV: Sprøsmål om tilbod" end end @@ -925,11 +925,11 @@ def convert(from, to) describe ".pick_encoding" do it "finds encoding" do - expect(Mail::Ruby19.pick_encoding("Windows-1252")).to eq Encoding::Windows_1252 + expect(Mail::RubyVer.pick_encoding("Windows-1252")).to eq Encoding::Windows_1252 end it "uses binary for unfound" do - expect(Mail::Ruby19.pick_encoding("ISO-Foo")).to eq Encoding::BINARY + expect(Mail::RubyVer.pick_encoding("ISO-Foo")).to eq Encoding::BINARY end end end diff --git a/spec/mail/message_spec.rb b/spec/mail/message_spec.rb index 6706cb5cc..5c5e67af2 100644 --- a/spec/mail/message_spec.rb +++ b/spec/mail/message_spec.rb @@ -1656,10 +1656,10 @@ def message_with_iso_8859_1_charset end def with_encoder(encoder) - old, Mail::Ruby19.charset_encoder = Mail::Ruby19.charset_encoder, encoder + old, Mail::RubyVer.charset_encoder = Mail::RubyVer.charset_encoder, encoder yield ensure - Mail::Ruby19.charset_encoder = old + Mail::RubyVer.charset_encoder = old end let(:message){ @@ -1678,8 +1678,8 @@ def with_encoder(encoder) expect(message.inspect_structure).to eq message.inspect end - it "uses the Ruby19 charset encoder" do - with_encoder(Mail::Ruby19::BestEffortCharsetEncoder.new) do + it "uses the RubyVer charset encoder" do + with_encoder(Mail::RubyVer::BestEffortCharsetEncoder.new) do message = Mail.new("Content-Type: text/plain;\r\n charset=windows-1258\r\nContent-Transfer-Encoding: base64\r\n\r\nSGkglg==\r\n") expect(message.decoded).to eq("Hi –") end diff --git a/spec/mail/version_specific/ruby_1_9_spec.rb b/spec/mail/version_specific/ruby_1_9_spec.rb index f7524613b..3ffa90bf4 100644 --- a/spec/mail/version_specific/ruby_1_9_spec.rb +++ b/spec/mail/version_specific/ruby_1_9_spec.rb @@ -4,7 +4,7 @@ describe '.decode_base64' do it "handles unpadded base64 correctly" do - decoded = Mail::Ruby19.decode_base64("YQ") + decoded = Mail::RubyVer.decode_base64("YQ") expect(decoded).to eq "a" end end