blob: d39fa139abb9f2b25e3353301c66481b90de8029 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# frozen_string_literal: true
# Inspired in great part by Discourse's Email::Receiver
module Gitlab
module Email
class ReplyParser
attr_accessor :message, :allow_only_quotes
def initialize(message, trim_reply: true, append_reply: false, allow_only_quotes: false)
@message = message
@trim_reply = trim_reply
@append_reply = append_reply
@allow_only_quotes = allow_only_quotes
end
def execute
body = select_body(message)
encoding = body.encoding
body, stripped_text = EmailReplyTrimmer.trim(body, @append_reply) if @trim_reply
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.
#
# 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
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")]
end
private
def select_body(message)
part =
if message.multipart?
message.text_part || message.html_part || message
else
message
end
decoded = fix_charset(part)
return "" unless decoded
# Certain trigger phrases that means we didn't parse correctly
if decoded =~ %r{(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 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 StandardError
nil
end
end
end
end
|