Ugly Hack: Remove improperly parsed headers from Mail::Message

This commit is contained in:
Robin Ward 2013-06-19 12:14:01 -04:00
parent 6491bda4ab
commit 82d1465d18
3 changed files with 98 additions and 4 deletions

View File

@ -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

View File

@ -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") }

61
spec/fixtures/emails/boundary_email.txt vendored Normal file
View File

@ -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: <CADkmRcL=2aPV7jOcs2i00QGZNwmjYj3qMaJTsKnB7DTP5sgyFQ@mail.gmail.com>
Subject: Re: [Adventure Time] jake mentioned you in 'peppermint butler is
missing'
From: Finn the Human <finn@adventuretime.ooo>
To: jake via Adventure Time <math+96dcd9072ba9072d06226009d4223a6e@tree.mail>
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
> <http://adventuretime.ooo/user_preferences>.
>
--001a11c206a073876a04df81d2a9
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
I&#39;ll look into it, thanks!<span></span><br><br>On Wednesday, June 19, 2=
013, jake via Adventure Time wrote:<br><blockquote class=3D"gmail_quote" st=
yle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p>sa=
m mentioned you in &#39;Duplicate message are shown in profile&#39; on Adve=
nture Time</p>
<hr><p>yeah, just noticed this cc <a href=3D"http://users/eviltrout" target=
=3D"_blank">@eviltrout</a> </p>
<hr><p>Please visit this link to respond: <a href=3D"http://adventuretime.oo=
o/t/duplicate-message-are-shown-in-profile/7628/2" target=3D"_blank">http=
://adventuretime.ooo/t/peppermint-butler-is-missing/7628/2</a></=
p>
<p>To unsubscribe from these emails, visit your <a href=3D"http://adventuret=
ime.ooo/user_preferences" target=3D"_blank">user preferences</a>.</p>
</blockquote>
--001a11c206a073876a04df81d2a9--