2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-11-15 23:39:29 +08:00
|
|
|
# A very simple formatter for imported emails
|
2015-06-05 23:46:21 +08:00
|
|
|
class EmailCook
|
|
|
|
|
2017-11-15 23:39:29 +08:00
|
|
|
def self.raw_regexp
|
|
|
|
@raw_regexp ||= /^\[plaintext\]$\n(.*)\n^\[\/plaintext\]$(?:\s^\[attachments\]$\n(.*)\n^\[\/attachments\]$)?(?:\s^\[elided\]$\n(.*)\n^\[\/elided\]$)?/m
|
2015-10-30 04:01:50 +08:00
|
|
|
end
|
|
|
|
|
2015-06-05 23:46:21 +08:00
|
|
|
def initialize(raw)
|
|
|
|
@raw = raw
|
2017-11-15 23:39:29 +08:00
|
|
|
@body, @attachment_html, @elided = @raw.scan(EmailCook.raw_regexp).first
|
2015-06-05 23:46:21 +08:00
|
|
|
end
|
|
|
|
|
2016-07-16 03:36:56 +08:00
|
|
|
def add_quote(result, buffer)
|
|
|
|
if buffer.present?
|
|
|
|
return if buffer =~ /\A(<br>)+\z$/
|
|
|
|
result << "<blockquote>#{buffer}</blockquote>"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-15 23:39:29 +08:00
|
|
|
def link_string!(line, unescaped_line)
|
|
|
|
unescaped_line = unescaped_line.strip
|
2021-02-03 00:34:00 +08:00
|
|
|
line.gsub!(/\S+/) do |str|
|
|
|
|
if str.match?(/^(https?:\/\/)[\S]+$/i)
|
|
|
|
begin
|
|
|
|
url = URI.parse(str).to_s
|
|
|
|
if unescaped_line == url
|
|
|
|
# this could be oneboxed
|
|
|
|
str = %|<a href="#{url}" class="onebox" target="_blank">#{url}</a>|
|
|
|
|
else
|
|
|
|
str = %|<a href="#{url}">#{url}</a>|
|
|
|
|
end
|
|
|
|
rescue URI::Error
|
|
|
|
# don't fail if uri does not parse
|
|
|
|
end
|
2016-07-19 03:00:12 +08:00
|
|
|
end
|
2021-02-03 00:34:00 +08:00
|
|
|
str
|
2016-07-19 02:38:40 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-11-15 23:39:29 +08:00
|
|
|
def htmlify(text)
|
2019-05-03 06:17:27 +08:00
|
|
|
result = +""
|
|
|
|
quote_buffer = +""
|
2015-06-05 23:46:21 +08:00
|
|
|
|
2016-07-13 03:37:19 +08:00
|
|
|
in_text = false
|
2015-06-05 23:46:21 +08:00
|
|
|
in_quote = false
|
2016-07-13 03:37:19 +08:00
|
|
|
|
2017-11-15 23:39:29 +08:00
|
|
|
text.each_line do |line|
|
2017-11-18 23:50:27 +08:00
|
|
|
# replace indentation with non-breaking spaces
|
|
|
|
line.sub!(/^\s{2,}/) { |s| "\u00A0" * s.length }
|
2015-06-05 23:46:21 +08:00
|
|
|
|
2017-11-15 23:39:29 +08:00
|
|
|
if line =~ /^\s*>/
|
2015-06-05 23:46:21 +08:00
|
|
|
in_quote = true
|
2017-11-15 23:39:29 +08:00
|
|
|
line.sub!(/^[\s>]*/, '')
|
|
|
|
|
|
|
|
unescaped_line = line
|
|
|
|
line = CGI.escapeHTML(line)
|
|
|
|
link_string!(line, unescaped_line)
|
|
|
|
|
|
|
|
quote_buffer << line << "<br>"
|
2015-06-05 23:46:21 +08:00
|
|
|
elsif in_quote
|
2016-07-16 03:36:56 +08:00
|
|
|
add_quote(result, quote_buffer)
|
2015-06-05 23:46:21 +08:00
|
|
|
quote_buffer = ""
|
|
|
|
in_quote = false
|
|
|
|
else
|
2015-10-30 04:01:50 +08:00
|
|
|
|
2017-11-15 23:39:29 +08:00
|
|
|
sz = line.size
|
2016-07-13 01:49:03 +08:00
|
|
|
|
2017-11-15 23:39:29 +08:00
|
|
|
unescaped_line = line
|
|
|
|
line = CGI.escapeHTML(line)
|
|
|
|
link_string!(line, unescaped_line)
|
2016-07-13 03:37:19 +08:00
|
|
|
|
|
|
|
if sz < 60
|
2017-11-15 23:39:29 +08:00
|
|
|
if in_text && line == "\n"
|
2016-07-13 03:37:19 +08:00
|
|
|
result << "<br>"
|
|
|
|
end
|
2017-11-15 23:39:29 +08:00
|
|
|
result << line
|
|
|
|
result << "<br>"
|
|
|
|
|
2016-07-13 03:37:19 +08:00
|
|
|
in_text = false
|
|
|
|
else
|
2017-11-15 23:39:29 +08:00
|
|
|
result << line
|
2016-07-13 03:37:19 +08:00
|
|
|
in_text = true
|
|
|
|
end
|
2015-06-05 23:46:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-07-16 03:36:56 +08:00
|
|
|
if in_quote && quote_buffer.present?
|
|
|
|
add_quote(result, quote_buffer)
|
2015-06-05 23:46:21 +08:00
|
|
|
end
|
|
|
|
|
2016-07-13 03:37:19 +08:00
|
|
|
result.gsub!(/(<br>\n*){3,10}/, '<br><br>')
|
2015-06-05 23:46:21 +08:00
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2017-11-15 23:39:29 +08:00
|
|
|
def cook(opts = {})
|
|
|
|
# fallback to PrettyText if we failed to detect a body
|
|
|
|
return PrettyText.cook(@raw, opts) if @body.nil?
|
|
|
|
|
|
|
|
result = htmlify(@body)
|
|
|
|
result << "\n<br>" << @attachment_html if @attachment_html.present?
|
|
|
|
result << "\n<br><br>" << Email::Receiver.elided_html(htmlify(@elided)) if @elided.present?
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2015-06-05 23:46:21 +08:00
|
|
|
end
|