debian-mirror-gitlab/lib/gitlab/email/reply_parser.rb

77 lines
2 KiB
Ruby
Raw Normal View History

2019-02-15 15:39:39 +05:30
# frozen_string_literal: true
2015-09-25 12:07:36 +05:30
# Inspired in great part by Discourse's Email::Receiver
module Gitlab
module Email
class ReplyParser
attr_accessor :message
2021-10-27 15:23:28 +05:30
def initialize(message, trim_reply: true, append_reply: false)
2015-09-25 12:07:36 +05:30
@message = message
2018-11-08 19:23:39 +05:30
@trim_reply = trim_reply
2021-10-27 15:23:28 +05:30
@append_reply = append_reply
2015-09-25 12:07:36 +05:30
end
def execute
body = select_body(message)
encoding = body.encoding
2021-10-27 15:23:28 +05:30
body, stripped_text = EmailReplyTrimmer.trim(body, @append_reply) if @trim_reply
2017-08-17 22:00:37 +05:30
return '' unless body
# not using /\s+$/ here because that deletes empty lines
body = body.gsub(/[ \t]$/, '')
# NOTE: We currently don't support empty quotes.
# EmailReplyTrimmer allows this as a special case,
# so we detect it manually here.
return "" if body.lines.all? { |l| l.strip.empty? || l.start_with?('>') }
2015-09-25 12:07:36 +05:30
2021-10-27 15:23:28 +05:30
encoded_body = body.force_encoding(encoding).encode("UTF-8")
return encoded_body unless @append_reply
[encoded_body, stripped_text.force_encoding(encoding).encode("UTF-8")]
2015-09-25 12:07:36 +05:30
end
private
def select_body(message)
2017-08-17 22:00:37 +05:30
part =
if message.multipart?
message.text_part || message.html_part || message
else
message
end
2015-09-25 12:07:36 +05:30
2017-08-17 22:00:37 +05:30
decoded = fix_charset(part)
2015-09-25 12:07:36 +05:30
2017-08-17 22:00:37 +05:30
return "" unless decoded
2015-09-25 12:07:36 +05:30
# Certain trigger phrases that means we didn't parse correctly
2018-03-17 18:26:18 +05:30
if decoded =~ %r{(Content\-Type\:|multipart/alternative|text/plain)}
2015-09-25 12:07:36 +05:30
return ""
end
2017-08-17 22:00:37 +05:30
if (part.content_type || '').include? 'text/html'
HTMLParser.parse_reply(decoded)
else
decoded
end
2015-09-25 12:07:36 +05:30
end
# Force encoding to UTF-8 on a Mail::Message or Mail::Part
def fix_charset(object)
2019-07-07 11:18:12 +05:30
return if object.nil?
2015-09-25 12:07:36 +05:30
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
2021-06-08 01:23:25 +05:30
rescue StandardError
2015-09-25 12:07:36 +05:30
nil
end
end
end
end