Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions lib/email_reply_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,12 +82,7 @@ def read(text)
# Normalize line endings.
text.gsub!("\r\n", "\n")

# Check for multi-line reply headers. Some clients break up
# the "On DATE, NAME <EMAIL> wrote:" line into multiple lines.
if text =~ /^(?!On.*On\s.+?wrote:)(On\s(.+?)wrote:)$/nm
# Remove all new lines from the reply header.
text.gsub! $1, $1.gsub("\n", " ")
end
text = repair_broken_reply_header(text)

# Some users may reply directly above a line of underscores.
# In order to ensure that these fragments are split correctly,
Expand Down Expand Up @@ -229,6 +224,27 @@ def finish_fragment
end
@fragment = nil
end

# Stitch together multi-line reply headers. Some clients break up
# the "On DATE, NAME <EMAIL> wrote:" line into multiple lines.
#
# If the latest reply has a line beginning with 'On', then the match
# will start too early and contain another match.
# So we repair the inner match via recursion
def repair_broken_reply_header(text)
pattern = /^(On\s(.+?)wrote:)$/nm
if text =~ pattern
match = $1
tail = match[1..-1]
if tail =~ pattern
text.gsub tail, repair_broken_reply_header(tail)
else
text.gsub match, match.gsub("\n", " ")
end
else
text
end
end
end

### Fragments
Expand Down
7 changes: 7 additions & 0 deletions test/email_reply_parser_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,13 @@ def test_deals_with_multiline_reply_headers
assert_match /Was this/, reply.fragments[1].to_s
end

def test_multiline_and_quoted_reply_headers
reply = email :broken_reply_header

assert_match /^On the other hand/, reply.fragments[0].to_s
assert_match /^On Wed, Dec 9/, reply.fragments[1].to_s
end

def test_deals_with_windows_line_endings
reply = email :email_1_7

Expand Down
29 changes: 29 additions & 0 deletions test/emails/broken_reply_header.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
This is the latest reply.

The thread contains two previous replies, and the most recent reply header has a line break in it.

If we don't patch the line break, the reply header ends up in THIS fragment, instead of in the quoted fragment.

On the one hand, there could be a line in the reply starting with the word 'On.'

On the other hand, it could happen TWICE!

Stranger

On Wed, Dec 9, Mister Example
<mrmister@example.com>
wrote:
> Hey, Stranger,
> It's mrmister again, and I hope you reply to me. I also hope this fragment has my reply header in it!
>
> -mrmister
>
> On Tue, 2011-03-01 at 18:02 +0530, Stranger Jones wrote:
>> Hey, mrmister,
>>
>> I'm getting tired of writing fake email text.
>>
>> Stranger

--
My name is Stranger