From 82d1465d18c7bfa539f76d8ba0e8c6a710d9f726 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Wed, 19 Jun 2013 12:14:01 -0400 Subject: [PATCH] Ugly Hack: Remove improperly parsed headers from Mail::Message --- lib/email/receiver.rb | 30 ++++++++++-- spec/components/email/receiver_spec.rb | 11 +++++ spec/fixtures/emails/boundary_email.txt | 61 +++++++++++++++++++++++++ 3 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 spec/fixtures/emails/boundary_email.txt diff --git a/lib/email/receiver.rb b/lib/email/receiver.rb index 8efad423575..ee68ce13450 100644 --- a/lib/email/receiver.rb +++ b/lib/email/receiver.rb @@ -18,13 +18,12 @@ module Email def process return Email::Receiver.results[:unprocessable] if @raw.blank? - message = Mail::Message.new(@raw) - return Email::Receiver.results[:unprocessable] if message.body.blank? + @message = Mail::Message.new(@raw) + parse_body - @body = EmailReplyParser.read(message.body.to_s).visible_text return Email::Receiver.results[:unprocessable] if @body.blank? - @reply_key = message.to.first + @reply_key = @message.to.first # Extract the `reply_key` from the format the site has specified tokens = SiteSetting.reply_by_email_address.split("%{reply_key}") @@ -43,6 +42,29 @@ module Email private + def parse_body + @body = @message.body.to_s.strip + return if @body.blank? + + # I really hate to have to do this, but there seems to be a bug in Mail::Message + # with content boundaries in emails. Until it is fixed, this hack removes stuff + # we don't want from emails bodies + content_type = @message.header['Content-Type'].to_s + if content_type.present? + boundary_match = content_type.match(/boundary\=(.*)$/) + boundary = boundary_match[1] if boundary_match && boundary_match[1].present? + if boundary.present? and @body.present? + + lines = @body.lines + lines = lines[1..-1] if lines.present? and lines[0] =~ /^--#{boundary}/ + lines = lines[1..-1] if lines.present? and lines[0] =~ /^Content-Type/ + + @body = lines.join.strip! + end + end + + @body = EmailReplyParser.read(@body).visible_text + end def create_reply diff --git a/spec/components/email/receiver_spec.rb b/spec/components/email/receiver_spec.rb index 05bac6dd68e..a9ff44bd6d9 100644 --- a/spec/components/email/receiver_spec.rb +++ b/spec/components/email/receiver_spec.rb @@ -17,6 +17,17 @@ describe Email::Receiver do end end + describe "with a content boundary" do + let(:bounded_email) { File.read("#{Rails.root}/spec/fixtures/emails/boundary_email.txt") } + let(:receiver) { Email::Receiver.new(bounded_email) } + + it "does something" do + receiver.process + expect(receiver.body).to eq("I'll look into it, thanks!") + end + + end + describe "with a valid email" do let(:reply_key) { "59d8df8370b7e95c5a49fbf86aeb2c93" } let(:valid_reply) { File.read("#{Rails.root}/spec/fixtures/emails/valid_reply.txt") } diff --git a/spec/fixtures/emails/boundary_email.txt b/spec/fixtures/emails/boundary_email.txt new file mode 100644 index 00000000000..78dba44f5af --- /dev/null +++ b/spec/fixtures/emails/boundary_email.txt @@ -0,0 +1,61 @@ + +MIME-Version: 1.0 +Received: by 10.64.14.41 with HTTP; Wed, 19 Jun 2013 06:29:41 -0700 (PDT) +In-Reply-To: <51c19490e928a_13442dd8ae892548@tree.mail> +References: <51c19490e928a_13442dd8ae892548@tree.mail> +Date: Wed, 19 Jun 2013 09:29:41 -0400 +Delivered-To: finn@adventuretime.ooo +Message-ID: +Subject: Re: [Adventure Time] jake mentioned you in 'peppermint butler is + missing' +From: Finn the Human +To: jake via Adventure Time +Content-Type: multipart/alternative; boundary=001a11c206a073876a04df81d2a9 + +--001a11c206a073876a04df81d2a9 +Content-Type: text/plain; charset=ISO-8859-1 + +I'll look into it, thanks! + + +On Wednesday, June 19, 2013, jake via Adventure Time wrote: + +> jake mentioned you in 'peppermint butler is missing' on Adventure +> Time: +> ------------------------------ +> +> yeah, just noticed this cc @jake +> ------------------------------ +> +> Please visit this link to respond: +> http://adventuretime.ooo/t/peppermint-butler-is-missing/7628/2 +> +> To unsubscribe from these emails, visit your user preferences +> . +> + +--001a11c206a073876a04df81d2a9 +Content-Type: text/html; charset=ISO-8859-1 +Content-Transfer-Encoding: quoted-printable + +I'll look into it, thanks!

On Wednesday, June 19, 2= +013, jake via Adventure Time wrote:

sa= +m mentioned you in 'Duplicate message are shown in profile' on Adve= +nture Time

+ + +

yeah, just noticed this cc @eviltrout

+ +

Please visit this link to respond: http= +://adventuretime.ooo/t/peppermint-butler-is-missing/7628/2 + + +

To unsubscribe from these emails, visit your user preferences.

+
+ +--001a11c206a073876a04df81d2a9-- \ No newline at end of file