2018-11-05 23:45:35 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2015-08-20 14:05:06 -04:00
|
|
|
# Inspired in great part by Discourse's Email::Receiver
|
|
|
|
module Gitlab
|
|
|
|
module Email
|
|
|
|
class ReplyParser
|
2021-11-12 10:12:37 -05:00
|
|
|
attr_accessor :message, :allow_only_quotes
|
2015-08-20 14:05:06 -04:00
|
|
|
|
2021-11-12 10:12:37 -05:00
|
|
|
def initialize(message, trim_reply: true, append_reply: false, allow_only_quotes: false)
|
2015-08-20 14:05:06 -04:00
|
|
|
@message = message
|
2018-05-09 16:19:16 -04:00
|
|
|
@trim_reply = trim_reply
|
2021-08-12 23:10:09 -04:00
|
|
|
@append_reply = append_reply
|
2021-11-12 10:12:37 -05:00
|
|
|
@allow_only_quotes = allow_only_quotes
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
|
|
|
body = select_body(message)
|
|
|
|
|
|
|
|
encoding = body.encoding
|
2021-08-12 23:10:09 -04:00
|
|
|
body, stripped_text = EmailReplyTrimmer.trim(body, @append_reply) if @trim_reply
|
2016-12-22 03:10:35 -05:00
|
|
|
return '' unless body
|
2015-08-20 14:05:06 -04:00
|
|
|
|
2016-11-22 01:37:30 -05:00
|
|
|
# not using /\s+$/ here because that deletes empty lines
|
2016-11-15 02:58:37 -05:00
|
|
|
body = body.gsub(/[ \t]$/, '')
|
|
|
|
|
2016-12-13 15:15:42 -05:00
|
|
|
# NOTE: We currently don't support empty quotes.
|
|
|
|
# EmailReplyTrimmer allows this as a special case,
|
|
|
|
# so we detect it manually here.
|
2021-11-12 10:12:37 -05:00
|
|
|
#
|
|
|
|
# If allow_only_quotes is true a message where all lines starts with ">" is allowed.
|
|
|
|
# This could happen if an email has an empty quote, forwarded without any new content.
|
|
|
|
return "" if body.lines.all? do |l|
|
|
|
|
l.strip.empty? || (!allow_only_quotes && l.start_with?('>'))
|
|
|
|
end
|
2016-12-13 15:14:38 -05:00
|
|
|
|
2022-06-13 23:08:16 -04:00
|
|
|
encoded_body = force_utf8(body.force_encoding(encoding))
|
2021-08-12 23:10:09 -04:00
|
|
|
return encoded_body unless @append_reply
|
|
|
|
|
2022-06-13 23:08:16 -04:00
|
|
|
[encoded_body, force_utf8(stripped_text.force_encoding(encoding))]
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def select_body(message)
|
2017-02-22 10:10:32 -05:00
|
|
|
part =
|
|
|
|
if message.multipart?
|
|
|
|
message.text_part || message.html_part || message
|
|
|
|
else
|
|
|
|
message
|
|
|
|
end
|
2015-08-20 14:05:06 -04:00
|
|
|
|
2016-11-16 00:39:07 -05:00
|
|
|
decoded = fix_charset(part)
|
2016-11-10 01:24:02 -05:00
|
|
|
|
|
|
|
return "" unless decoded
|
2015-08-20 14:05:06 -04:00
|
|
|
|
|
|
|
# Certain trigger phrases that means we didn't parse correctly
|
2018-01-27 00:35:53 -05:00
|
|
|
if decoded =~ %r{(Content\-Type\:|multipart/alternative|text/plain)}
|
2015-08-20 14:05:06 -04:00
|
|
|
return ""
|
|
|
|
end
|
|
|
|
|
2016-11-16 00:39:07 -05:00
|
|
|
if (part.content_type || '').include? 'text/html'
|
2016-11-10 01:24:02 -05:00
|
|
|
HTMLParser.parse_reply(decoded)
|
|
|
|
else
|
|
|
|
decoded
|
|
|
|
end
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Force encoding to UTF-8 on a Mail::Message or Mail::Part
|
|
|
|
def fix_charset(object)
|
2019-02-08 07:19:53 -05:00
|
|
|
return if object.nil?
|
2015-08-20 14:05:06 -04:00
|
|
|
|
|
|
|
if object.charset
|
2022-06-13 23:08:16 -04:00
|
|
|
# A part of a multi-part may have a different encoding. Its encoding
|
|
|
|
# is denoted in its header. For example:
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# ------=_Part_2192_32400445.1115745999735
|
|
|
|
# Content-Type: text/plain; charset=ISO-8859-1
|
|
|
|
# Content-Transfer-Encoding: 7bit
|
|
|
|
#
|
|
|
|
# Plain email.
|
|
|
|
# ```
|
|
|
|
# So, we had to force its part to corresponding encoding before able
|
|
|
|
# to convert it to UTF-8
|
|
|
|
force_utf8(object.body.decoded.force_encoding(object.charset.gsub(/utf8/i, "UTF-8")))
|
2015-08-20 14:05:06 -04:00
|
|
|
else
|
|
|
|
object.body.to_s
|
|
|
|
end
|
2021-04-26 08:09:44 -04:00
|
|
|
rescue StandardError
|
2015-08-20 14:05:06 -04:00
|
|
|
nil
|
|
|
|
end
|
2022-06-13 23:08:16 -04:00
|
|
|
|
|
|
|
def force_utf8(str)
|
|
|
|
Gitlab::EncodingHelper.encode_utf8(str).to_s
|
|
|
|
end
|
2015-08-20 14:05:06 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|