Skip to content

Commit

Permalink
detect another common multiline quote header - From, Sent, To, Subject
Browse files Browse the repository at this point in the history
  • Loading branch information
Drew Batshaw committed Nov 28, 2012
1 parent 9ff0012 commit bee8272
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
11 changes: 7 additions & 4 deletions lib/email_reply_parser.rb
Expand Up @@ -266,18 +266,21 @@ def quote_header?(line)

def multiline_quote_header_in_fragment?(fragment)
fragment_text = @fragment.lines.join("\n")
quoted_regex = "Date:.*\nFrom:.*\nTo:.*\nSubject:.*"
quoted_header_regexV1 = "Date:.*\nFrom:.*\nTo:.*\nSubject:.*"
quoted_header_regexV2 = "From:.*\nSent:.*\nTo:.*\nSubject:.*"
quoted_regex = "(#{quoted_header_regexV1}|#{quoted_header_regexV2})"
fragment_text =~ reverse_regexp(quoted_regex)
end

def reverse_regexp(regexp, ignore_case = true)
regexp_text = regexp.to_s
regexp_text.gsub!(".*", "*.")
regexp_text = regexp.to_s.reverse
regexp_text.gsub!("*.", ".*")
regexp_text.gsub!("$", "^")
regexp_text.gsub!(/\)(.*)\(/m, "(\\1)") #reverses parathesis

regexp_options = []
regexp_options << Regexp::IGNORECASE if ignore_case
Regexp.new(regexp_text.reverse, *regexp_options)
Regexp.new(regexp_text, *regexp_options)
end

# Detects if a given line is the beginning of a signature
Expand Down
11 changes: 9 additions & 2 deletions test/email_reply_parser_test.rb
Expand Up @@ -226,6 +226,13 @@ def test_from_name_in_quote_header
assert_equal expected_body, EmailReplyParser.parse_reply(body, "Smith, Shelly <shelly@example.com>")
end

def test_multiline_quote_header_from_first
body = IO.read EMAIL_FIXTURE_PATH.join("email_multiline_quote_header_from_first.txt").to_s
expected_body = "I have gained valuable experience from working with students from other cultures. They bring a significantly different perspective to the work we do. I have also had the opportunity to practice making myself very clear in discussion, so that everyone understands. I've also seen how different our culture is to them, in their reactions to what I think is a normal approach to assignments, and to life in general."
assert_equal expected_body, EmailReplyParser.parse_reply(body, "Smith, Shelly <shelly@example.com>")
end


def test_parsing_name_from_address
address = "Bob Jones <bob@gmail.com>"
email = EmailReplyParser::Email.new
Expand Down Expand Up @@ -264,8 +271,8 @@ def test_parsing_email_from_address_without_name

def test_reverse_regexp_string
email = EmailReplyParser::Email.new
regexp = "abc.*def$"
assert_equal Regexp.new("^fed.*cba", Regexp::IGNORECASE), email.send(:reverse_regexp, regexp)
regexp = "(abc.*\ndef)$"
assert_equal Regexp.new("^(fed\n.*cba)", Regexp::IGNORECASE), email.send(:reverse_regexp, regexp)
end

def test_one_is_not_on
Expand Down
11 changes: 11 additions & 0 deletions test/emails/email_multiline_quote_header_from_first.txt
@@ -0,0 +1,11 @@
I have gained valuable experience from working with students from other cultures. They bring a significantly different perspective to the work we do. I have also had the opportunity to practice making myself very clear in discussion, so that everyone understands. I've also seen how different our culture is to them, in their reactions to what I think is a normal approach to assignments, and to life in general.

From: john@example.com
Sent: Tue, 2 Oct 2012 02:35:10 +0000
To: shelly@example.com
Subject: Tell us what you learned.




Thank you for recently participating in our class. We're interested in feedback. Please reply to this email.

0 comments on commit bee8272

Please sign in to comment.