Skip to content

Commit 113dc5c

Browse files
committed
don't force encode emails to binary
We should assume that incoming data is tagged and scrubbed correctly. We are not working with binary data.
1 parent 8724505 commit 113dc5c

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

lib/email_reply_parser.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,15 +76,14 @@ def visible_text
7676
#
7777
# Returns this same Email instance.
7878
def read(text)
79-
# in 1.9 we want to operate on the raw bytes
80-
text = text.dup.force_encoding('binary') if text.respond_to?(:force_encoding)
79+
text = text.dup
8180

8281
# Normalize line endings.
8382
text.gsub!("\r\n", "\n")
8483

8584
# Check for multi-line reply headers. Some clients break up
8685
# the "On DATE, NAME <EMAIL> wrote:" line into multiple lines.
87-
if text =~ /^(?!On.*On\s.+?wrote:)(On\s(.+?)wrote:)$/nm
86+
if text =~ /^(?!On.*On\s.+?wrote:)(On\s(.+?)wrote:)$/m
8887
# Remove all new lines from the reply header.
8988
text.gsub! $1, $1.gsub("\n", " ")
9089
end
@@ -110,7 +109,7 @@ def read(text)
110109

111110
# Use the StringScanner to pull out each line of the email content.
112111
@scanner = StringScanner.new(text)
113-
while line = @scanner.scan_until(/\n/n)
112+
while line = @scanner.scan_until(/\n/)
114113
scan_line(line)
115114
end
116115

@@ -156,7 +155,7 @@ def scan_line(line)
156155

157156
# We're looking for leading `>`'s to see if this line is part of a
158157
# quoted Fragment.
159-
is_quoted = !!(line =~ /(>+)$/n)
158+
is_quoted = !!(line =~ /(>+)$/)
160159

161160
# Mark the current Fragment as a signature if the current line is empty
162161
# and the Fragment starts with a common signature indicator.
@@ -189,7 +188,7 @@ def scan_line(line)
189188
#
190189
# Returns true if the line is a valid header, or false.
191190
def quote_header?(line)
192-
line =~ /^:etorw.*nO$/n
191+
line =~ /^:etorw.*nO$/
193192
end
194193

195194
# Builds the fragment string and reverses it, after all lines have been

test/email_reply_parser_test.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,17 @@
99
EMAIL_FIXTURE_PATH = dir + 'emails'
1010

1111
class EmailReplyParserTest < Test::Unit::TestCase
12+
def test_encoding_should_be_maintained
13+
body = IO.read EMAIL_FIXTURE_PATH.join("email_1_1.txt").to_s
14+
EmailReplyParser.read body
15+
reply = email(:email_1_1)
16+
fragments = reply.fragments
17+
refute_predicate fragments, :empty?
18+
fragments.each do |fragment|
19+
assert_equal body.encoding, fragment.to_s.encoding
20+
end
21+
end
22+
1223
def test_reads_simple_body
1324
reply = email(:email_1_1)
1425
assert_equal 3, reply.fragments.size

0 commit comments

Comments
 (0)