gitlab-org--gitlab-foss/lib/gitlab/email/reply_parser.rb

64 lines
1.5 KiB
Ruby
Raw Normal View History

# Inspired in great part by Discourse's Email::Receiver
module Gitlab
module Email
class ReplyParser
attr_accessor :message
def initialize(message)
@message = message
end
def execute
body = select_body(message)
encoding = body.encoding
body = EmailReplyTrimmer.trim(body) or return ""
# [jneen] not using /\s+$/ here because that deletes empty lines
body = body.gsub(/[ \t]$/, '')
body.force_encoding(encoding).encode("UTF-8")
end
private
def select_body(message)
2016-11-16 03:45:44 -05:00
if message.multipart?
part = message.text_part || message.html_part || message
else
2016-11-16 03:45:44 -05:00
part = message
end
decoded = fix_charset(part)
return "" unless decoded
# Certain trigger phrases that means we didn't parse correctly
if decoded =~ /(Content\-Type\:|multipart\/alternative|text\/plain)/
return ""
end
if (part.content_type || '').include? 'text/html'
HTMLParser.parse_reply(decoded)
else
decoded
end
end
# Force encoding to UTF-8 on a Mail::Message or Mail::Part
def fix_charset(object)
return nil if object.nil?
if object.charset
object.body.decoded.force_encoding(object.charset.gsub(/utf8/i, "UTF-8")).encode("UTF-8").to_s
else
object.body.to_s
end
rescue
nil
end
end
end
end