Skip to content

Commit

Permalink
support uu encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Oct 29, 2014
1 parent 8ab866e commit bc4c9bb
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/mail.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ def self.eager_autoload!
require 'mail/encodings'
require 'mail/encodings/base64'
require 'mail/encodings/quoted_printable'
require 'mail/encodings/unix_to_unix'

require 'mail/matchers/has_sent_mail'

Expand Down
17 changes: 17 additions & 0 deletions lib/mail/encodings/unix_to_unix.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module Mail
module Encodings
module UnixToUnix
NAME = "x-uuencode"

def self.decode(str)
str.sub(/\Abegin \d+ [^\n]*\n/, '').unpack('u').first
end

def self.encode(str)
[str].pack("u")
end

Encodings.register(NAME, self)
end
end
end
40 changes: 40 additions & 0 deletions spec/mail/encodings/unix_to_unix_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# encoding: utf-8
require 'spec_helper'

describe Mail::Encodings::UnixToUnix do
def decode(str)
Mail::Encodings::UnixToUnix.decode(str)
end

def encode(str)
Mail::Encodings::UnixToUnix.encode(str)
end

it "decodes" do
text = strip_heredoc(<<-TEXT)
begin 644 Happy.txt
M2&%P<'D@=&]D87D@9F]R('1O(&)E(&]N92!O9B!P96%C92!A;F0@<V5R96YE
'('1I;64N"@``
`
end
TEXT
expect(decode(text)).to eq "Happy today for to be one of peace and serene time.\n"
end

it "encodes" do
expect(encode("Happy today")).to eq "+2&%P<'D@=&]D87D`\n"
end

if RUBY_VERSION > "1.9"
it "encodes / decodes non-ascii" do
expect(encode("Happy ああr")).to eq "-2&%P<'D@XX&\"XX&\"<@``\n"
expected = (RUBY_ENGINE == "jruby" ? "Happy ああr" : "Happy ああr".force_encoding("binary"))
expect(decode("-2&%P<'D@XX&\"XX&\"<@``\n")).to eq expected
end
end

it "can read itself" do
expect(decode(encode("Happy today"))).to eq "Happy today"
expect(encode(decode("+2&%P<'D@=&]D87D`\n"))).to eq "+2&%P<'D@=&]D87D`\n"
end
end
8 changes: 6 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@
end
end


def fixture(*name)
File.join(SPEC_ROOT, 'fixtures', name)
end


# Produces an array or printable ascii by default.
#
# We can assume if a, m and z and 1, 5, 0 work, then the rest
Expand All @@ -56,6 +54,12 @@ def ascii(from = 33, to = 126)
chars - boring
end

# https://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/string/strip.rb#L22
def strip_heredoc(string)
indent = string.scan(/^[ \t]*(?=\S)/).min.size
string.gsub(/^[ \t]{#{indent}}/, '')
end

# Original mockup from ActionMailer
class MockSMTP

Expand Down

0 comments on commit bc4c9bb

Please sign in to comment.