2022-01-01 13:38:12 +00:00
|
|
|
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
|
2021-06-01 12:20:20 +00:00
|
|
|
|
2018-06-01 11:32:59 +00:00
|
|
|
require 'rchardet'
|
|
|
|
|
2013-09-20 06:29:09 +00:00
|
|
|
class String
|
2017-10-24 20:34:52 +00:00
|
|
|
alias old_strip strip
|
|
|
|
alias old_strip! strip!
|
|
|
|
|
|
|
|
def strip!
|
|
|
|
begin
|
2021-05-12 11:37:44 +00:00
|
|
|
sub!(%r{\A[[[:space:]]\u{200B}\u{FEFF}]+}, '')
|
|
|
|
sub!(%r{[[[:space:]]\u{200B}\u{FEFF}]+\Z}, '')
|
2017-10-24 20:34:52 +00:00
|
|
|
|
|
|
|
# if incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string) (Encoding::CompatibilityError), use default
|
2018-06-01 11:32:59 +00:00
|
|
|
rescue Encoding::CompatibilityError
|
2017-10-24 20:34:52 +00:00
|
|
|
old_strip!
|
|
|
|
end
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def strip
|
|
|
|
begin
|
2021-05-12 11:37:44 +00:00
|
|
|
new_string = sub(%r{\A[[[:space:]]\u{200B}\u{FEFF}]+}, '')
|
|
|
|
new_string.sub!(%r{[[[:space:]]\u{200B}\u{FEFF}]+\Z}, '')
|
2017-10-24 20:34:52 +00:00
|
|
|
|
|
|
|
# if incompatible encoding regexp match (UTF-8 regexp with ASCII-8BIT string) (Encoding::CompatibilityError), use default
|
2018-06-01 11:32:59 +00:00
|
|
|
rescue Encoding::CompatibilityError
|
2017-10-24 20:34:52 +00:00
|
|
|
new_string = old_strip
|
|
|
|
end
|
|
|
|
new_string
|
|
|
|
end
|
|
|
|
|
2013-09-20 06:29:09 +00:00
|
|
|
def message_quote
|
2015-05-07 12:10:38 +00:00
|
|
|
quote = split("\n")
|
2013-09-20 06:29:09 +00:00
|
|
|
body_quote = ''
|
|
|
|
quote.each do |line|
|
2020-09-30 09:07:01 +00:00
|
|
|
body_quote = "#{body_quote}> #{line}\n"
|
2013-09-20 06:29:09 +00:00
|
|
|
end
|
|
|
|
body_quote
|
|
|
|
end
|
2015-05-07 10:27:12 +00:00
|
|
|
|
2013-09-20 06:29:09 +00:00
|
|
|
def word_wrap(*args)
|
|
|
|
options = args.extract_options!
|
2017-11-23 08:09:44 +00:00
|
|
|
if args.present?
|
2013-09-20 06:29:09 +00:00
|
|
|
options[:line_width] = args[0] || 82
|
|
|
|
end
|
2015-04-27 13:42:53 +00:00
|
|
|
options.reverse_merge!(line_width: 82)
|
2013-09-20 06:29:09 +00:00
|
|
|
|
|
|
|
lines = self
|
|
|
|
lines.split("\n").collect do |line|
|
2021-05-12 11:37:44 +00:00
|
|
|
line.length > options[:line_width] ? line.gsub(%r{(.{1,#{options[:line_width]}})(\s+|$)}, "\\1\n").strip : line
|
2013-09-20 06:29:09 +00:00
|
|
|
end * "\n"
|
|
|
|
end
|
2015-01-09 13:17:34 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
filename = 'Some::Module'.to_filename
|
|
|
|
|
|
|
|
returns
|
|
|
|
'some/module'
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2013-10-07 07:00:26 +00:00
|
|
|
def to_filename
|
2020-02-18 19:51:31 +00:00
|
|
|
camel_cased_word = dup
|
2021-05-12 11:37:44 +00:00
|
|
|
camel_cased_word.gsub(%r{::}, '/')
|
|
|
|
.gsub(%r{([A-Z]+)([A-Z][a-z])}, '\1_\2')
|
|
|
|
.gsub(%r{([a-z\d])([A-Z])}, '\1_\2')
|
2016-01-15 17:22:57 +00:00
|
|
|
.tr('-', '_').downcase
|
2013-10-07 07:00:26 +00:00
|
|
|
end
|
2014-06-01 08:29:58 +00:00
|
|
|
|
2015-06-30 14:45:03 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
filename = 'some/module.rb'.to_classname
|
|
|
|
|
|
|
|
returns
|
|
|
|
'Some::Module'
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def to_classname
|
2020-02-18 19:51:31 +00:00
|
|
|
camel_cased_word = dup
|
2020-05-25 07:05:17 +00:00
|
|
|
camel_cased_word.delete_suffix!('.rb')
|
2015-06-30 14:45:03 +00:00
|
|
|
camel_cased_word.split('/').map(&:camelize).join('::')
|
|
|
|
end
|
|
|
|
|
2014-06-01 08:29:58 +00:00
|
|
|
# because of mysql inno_db limitations, strip 4 bytes utf8 chars (e. g. emojis)
|
|
|
|
# unfortunaly UTF8mb4 will raise other limitaions of max varchar and lower index sizes
|
|
|
|
# More details: http://pjambet.github.io/blog/emojis-and-mysql/
|
|
|
|
def utf8_to_3bytesutf8
|
2016-01-19 22:30:23 +00:00
|
|
|
return self if Rails.application.config.db_4bytes_utf8
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-01 12:25:52 +00:00
|
|
|
each_char.select do |c|
|
2014-06-01 08:29:58 +00:00
|
|
|
if c.bytes.count > 3
|
2015-07-03 15:18:01 +00:00
|
|
|
Rails.logger.warn "strip out 4 bytes utf8 chars '#{c}' of '#{self}'"
|
2014-06-01 08:29:58 +00:00
|
|
|
next
|
|
|
|
end
|
|
|
|
c
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2020-11-24 16:20:57 +00:00
|
|
|
.join
|
2014-06-01 08:29:58 +00:00
|
|
|
end
|
2014-12-27 15:25:58 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
text = html_string.html2text
|
|
|
|
|
2015-01-09 13:17:34 +00:00
|
|
|
returns
|
|
|
|
|
2015-09-02 07:14:48 +00:00
|
|
|
'string with text only'
|
2015-01-09 13:17:34 +00:00
|
|
|
|
2014-12-27 15:25:58 +00:00
|
|
|
=end
|
|
|
|
|
2016-06-21 15:14:15 +00:00
|
|
|
def html2text(string_only = false, strict = false)
|
2020-02-18 19:51:31 +00:00
|
|
|
string = dup
|
2015-01-08 14:27:44 +00:00
|
|
|
|
2017-07-10 15:00:51 +00:00
|
|
|
# in case of invalid encoding, strip invalid chars
|
2018-06-05 03:28:12 +00:00
|
|
|
# see also test/data/mail/mail021.box
|
2015-01-08 14:27:44 +00:00
|
|
|
# note: string.encode!('UTF-8', 'UTF-8', :invalid => :replace, :replace => '?') was not detecting invalid chars
|
|
|
|
if !string.valid_encoding?
|
2015-05-07 10:25:16 +00:00
|
|
|
string = string.chars.select(&:valid_encoding?).join
|
2014-12-27 15:25:58 +00:00
|
|
|
end
|
|
|
|
|
2016-06-22 14:39:20 +00:00
|
|
|
# remove html comments
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{<!--.+?-->}m, '')
|
2016-06-22 14:39:20 +00:00
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
# find <a href=....> and replace it with [x]
|
|
|
|
link_list = ''
|
|
|
|
counter = 0
|
2020-11-05 16:31:00 +00:00
|
|
|
if string_only
|
2017-10-01 12:25:52 +00:00
|
|
|
string.gsub!(%r{<a[[:space:]]+(|\S+[[:space:]]+)href=("|')(.+?)("|')([[:space:]]*|[[:space:]]+[^>]*)>(.+?)<[[:space:]]*/a[[:space:]]*>}mxi) do |_placeholder|
|
2016-06-27 23:32:17 +00:00
|
|
|
link = $3
|
|
|
|
text = $6
|
2021-05-12 11:37:44 +00:00
|
|
|
text.gsub!(%r{<.+?>}, '')
|
2016-06-27 23:32:17 +00:00
|
|
|
|
|
|
|
link_compare = link.dup
|
2017-11-23 08:09:44 +00:00
|
|
|
if link_compare.present?
|
2016-06-21 15:14:15 +00:00
|
|
|
link.strip!
|
2016-06-27 23:32:17 +00:00
|
|
|
link_compare.strip!
|
|
|
|
link_compare.downcase!
|
|
|
|
link_compare.sub!(%r{/$}, '')
|
2016-06-21 15:14:15 +00:00
|
|
|
end
|
2016-06-27 23:32:17 +00:00
|
|
|
text_compare = text.dup
|
2017-11-23 08:09:44 +00:00
|
|
|
if text_compare.present?
|
2016-06-21 15:14:15 +00:00
|
|
|
text.strip!
|
2016-06-27 23:32:17 +00:00
|
|
|
text_compare.strip!
|
|
|
|
text_compare.downcase!
|
|
|
|
text_compare.sub!(%r{/$}, '')
|
2016-06-21 15:14:15 +00:00
|
|
|
end
|
2019-06-28 11:38:49 +00:00
|
|
|
|
|
|
|
if link_compare.present? && text_compare.blank?
|
|
|
|
link
|
2021-05-12 11:37:44 +00:00
|
|
|
elsif (link_compare.blank? && text_compare.present?) || (link_compare && link_compare =~ %r{^mailto}i)
|
2019-06-28 11:38:49 +00:00
|
|
|
text
|
|
|
|
elsif link_compare.present? && text_compare.present? && (link_compare == text_compare || link_compare == "mailto:#{text}".downcase || link_compare == "http://#{text}".downcase)
|
|
|
|
"######LINKEXT:#{link}/TEXT:#{text}######"
|
2021-05-12 11:37:44 +00:00
|
|
|
elsif text !~ %r{^http}
|
2019-06-28 11:38:49 +00:00
|
|
|
"#{text} (######LINKRAW:#{link}######)"
|
|
|
|
else
|
|
|
|
"#{link} (######LINKRAW:#{text}######)"
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2021-05-12 11:37:44 +00:00
|
|
|
elsif string.scan(%r{<a[[:space:]]}i).count < 5_000
|
|
|
|
string.gsub!(%r{<a[[:space:]].*?href=("|')(.+?)("|').*?>}ix) do
|
2020-11-05 16:31:00 +00:00
|
|
|
link = $2
|
2021-07-16 14:10:31 +00:00
|
|
|
counter += 1
|
2020-11-05 16:31:00 +00:00
|
|
|
link_list += "[#{counter}] #{link}\n"
|
|
|
|
"[#{counter}] "
|
|
|
|
end
|
2015-09-02 07:14:48 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# remove style tags with content
|
2016-06-27 23:32:17 +00:00
|
|
|
string.gsub!(%r{<style(|[[:space:]].+?)>(.+?)</style>}im, '')
|
2016-06-03 13:25:06 +00:00
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
# remove empty lines
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{^[[:space:]]*}m, '')
|
2016-06-21 15:14:15 +00:00
|
|
|
if strict
|
2016-06-27 23:32:17 +00:00
|
|
|
string.gsub!(%r{< [[:space:]]* (/*) [[:space:]]* (b|i|ul|ol|li|u|h1|h2|h3|hr) ([[:space:]]*|[[:space:]]+[^>]*) >}mxi, '######\1\2######')
|
2016-06-21 15:14:15 +00:00
|
|
|
end
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# pre/code handling 1/2
|
2017-10-01 12:25:52 +00:00
|
|
|
string.gsub!(%r{<pre>(.+?)</pre>}m) do |placeholder|
|
2021-05-12 11:37:44 +00:00
|
|
|
placeholder.gsub(%r{\n}, '###BR###')
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
string.gsub!(%r{<code>(.+?)</code>}m) do |placeholder|
|
2021-05-12 11:37:44 +00:00
|
|
|
placeholder.gsub(%r{\n}, '###BR###')
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-01-08 14:27:44 +00:00
|
|
|
|
2016-01-13 22:27:34 +00:00
|
|
|
# insert spaces on [A-z]\n[A-z]
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{([A-z])[[:space:]]([A-z])}m, '\1 \2')
|
2016-01-13 22:27:34 +00:00
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
# remove all new lines
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{(\n\r|\r\r\n|\r\n|\n)}, '')
|
2015-09-02 07:14:48 +00:00
|
|
|
|
|
|
|
# blockquote handling
|
2017-10-01 12:25:52 +00:00
|
|
|
string.gsub!(%r{<blockquote(| [^>]*)>(.+?)</blockquote>}m) do
|
2021-05-12 11:37:44 +00:00
|
|
|
"\n#{$2.html2text(true).gsub(%r{^(.*)$}, '> \1')}\n"
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# pre/code handling 2/2
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{###BR###}, "\n")
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# add counting
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{<li(| [^>]*)>}i, "\n* ")
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# add hr
|
2015-09-02 07:14:48 +00:00
|
|
|
string.gsub!(%r{<hr(|/| [^>]*)>}i, "\n___\n")
|
|
|
|
|
|
|
|
# add h\d
|
|
|
|
string.gsub!(%r{</h\d>}i, "\n")
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# add new lines
|
2016-06-27 23:32:17 +00:00
|
|
|
string.gsub!(%r{</div><div(|[[:space:]].+?)>}im, "\n")
|
|
|
|
string.gsub!(%r{</p><p(|[[:space:]].+?)>}im, "\n")
|
2016-06-21 15:14:15 +00:00
|
|
|
string.gsub!(%r{<(div|p|pre|br|table|tr|h)(|/| [^>]*)>}i, "\n")
|
2016-06-27 23:32:17 +00:00
|
|
|
string.gsub!(%r{</(p|br|div)(|[[:space:]].+?)>}i, "\n")
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!(%r{</td>}i, ' ')
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# strip all other tags
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{<.+?>}, '')
|
2015-01-08 14:27:44 +00:00
|
|
|
|
2015-09-14 23:27:14 +00:00
|
|
|
# replace multiple spaces with one
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{ }, ' ')
|
2015-09-02 07:14:48 +00:00
|
|
|
|
2016-06-27 23:32:17 +00:00
|
|
|
# add hyperlinks
|
|
|
|
if strict
|
2017-10-01 12:25:52 +00:00
|
|
|
string.gsub!(%r{([[:space:]])((http|https|ftp|tel)://.+?|(www..+?))([[:space:]]|\.[[:space:]]|,[[:space:]])}mxi) do |_placeholder|
|
2016-06-27 23:32:17 +00:00
|
|
|
pre = $1
|
|
|
|
content = $2
|
|
|
|
post = $5
|
2021-05-12 11:37:44 +00:00
|
|
|
if content.match?(%r{^www}i)
|
2016-06-27 23:32:17 +00:00
|
|
|
content = "http://#{content}"
|
|
|
|
end
|
2019-06-28 11:38:49 +00:00
|
|
|
|
2021-05-12 11:37:44 +00:00
|
|
|
if content =~ %r{^(http|https|ftp|tel)}i
|
2019-06-28 11:38:49 +00:00
|
|
|
"#{pre}######LINKRAW:#{content}#######{post}"
|
|
|
|
else
|
|
|
|
"#{pre}#{content}#{post}"
|
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-27 23:32:17 +00:00
|
|
|
end
|
|
|
|
|
2016-02-03 13:33:03 +00:00
|
|
|
# try HTMLEntities, if it fails on invalid signes, use manual way
|
|
|
|
begin
|
|
|
|
coder = HTMLEntities.new
|
|
|
|
string = coder.decode(string)
|
|
|
|
rescue
|
|
|
|
# strip all & < > "
|
2016-06-03 13:25:06 +00:00
|
|
|
string.gsub!('&', '&')
|
|
|
|
string.gsub!('<', '<')
|
|
|
|
string.gsub!('>', '>')
|
|
|
|
string.gsub!('"', '"')
|
|
|
|
string.gsub!(' ', ' ')
|
2016-02-03 13:33:03 +00:00
|
|
|
|
|
|
|
# encode html entities like "–"
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{(&\#(\d+);?)}x) do
|
2016-02-03 13:33:03 +00:00
|
|
|
$2.chr
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2015-01-08 14:27:44 +00:00
|
|
|
|
2016-02-03 13:33:03 +00:00
|
|
|
# encode html entities like "d;"
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{(&\#[xX]([0-9a-fA-F]+);?)}x) do
|
2016-02-03 13:33:03 +00:00
|
|
|
chr_orig = $1
|
|
|
|
hex = $2.hex
|
|
|
|
if hex
|
|
|
|
chr = hex.chr
|
|
|
|
if chr
|
|
|
|
chr_orig = chr
|
|
|
|
else
|
|
|
|
chr_orig
|
|
|
|
end
|
2015-01-08 14:27:44 +00:00
|
|
|
else
|
|
|
|
chr_orig
|
|
|
|
end
|
|
|
|
|
2016-02-03 13:33:03 +00:00
|
|
|
# check valid encoding
|
|
|
|
begin
|
|
|
|
if !chr_orig.encode('UTF-8').valid_encoding?
|
|
|
|
chr_orig = '?'
|
|
|
|
end
|
|
|
|
rescue
|
2015-01-08 14:27:44 +00:00
|
|
|
chr_orig = '?'
|
|
|
|
end
|
2016-02-03 13:33:03 +00:00
|
|
|
chr_orig
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-02-03 13:33:03 +00:00
|
|
|
end
|
2019-08-29 16:05:17 +00:00
|
|
|
string = string.utf8_encode(fallback: :read_as_sanitized_binary)
|
2015-01-08 14:27:44 +00:00
|
|
|
|
|
|
|
# remove tailing empty spaces
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{[[:blank:]]+$}, '')
|
2015-09-02 07:14:48 +00:00
|
|
|
|
2016-06-22 13:11:22 +00:00
|
|
|
# remove double multiple empty lines
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{\n\n\n+}, "\n\n")
|
2016-06-22 13:11:22 +00:00
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
# add extracted links
|
|
|
|
if link_list != ''
|
2020-09-30 09:07:01 +00:00
|
|
|
string += "\n\n\n#{link_list}"
|
2014-12-27 15:25:58 +00:00
|
|
|
end
|
2015-01-08 14:27:44 +00:00
|
|
|
|
2016-06-22 12:55:23 +00:00
|
|
|
# remove double multiple empty lines
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{\n\n\n+}, "\n\n")
|
2016-06-22 12:55:23 +00:00
|
|
|
|
2015-01-08 14:27:44 +00:00
|
|
|
string.strip
|
2014-12-27 15:25:58 +00:00
|
|
|
end
|
2015-01-03 22:53:07 +00:00
|
|
|
|
|
|
|
=begin
|
|
|
|
|
|
|
|
html = text_string.text2html
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
|
|
|
def text2html
|
2016-01-15 19:40:29 +00:00
|
|
|
text = CGI.escapeHTML(self)
|
2021-05-12 11:37:44 +00:00
|
|
|
text.gsub!(%r{\n}, '<br>')
|
2015-01-03 22:53:07 +00:00
|
|
|
text.chomp
|
|
|
|
end
|
|
|
|
|
2016-06-21 15:14:15 +00:00
|
|
|
=begin
|
|
|
|
|
|
|
|
html = text_string.text2html
|
|
|
|
|
|
|
|
=end
|
|
|
|
|
2017-03-10 06:49:01 +00:00
|
|
|
def html2html_strict
|
2020-02-18 19:51:31 +00:00
|
|
|
string = dup
|
2017-03-10 06:49:01 +00:00
|
|
|
string = HtmlSanitizer.strict(string, true).strip
|
2017-04-28 10:16:15 +00:00
|
|
|
string = HtmlSanitizer.cleanup(string).strip
|
2017-03-10 06:49:01 +00:00
|
|
|
|
|
|
|
# as fallback, use html2text and text2html
|
|
|
|
if string.blank?
|
|
|
|
string = html2text.text2html
|
|
|
|
string.signature_identify('text')
|
|
|
|
marker_template = '<span class="js-signatureMarker"></span>'
|
2021-05-12 11:37:44 +00:00
|
|
|
string.sub!(%r{######SIGNATURE_MARKER######}, marker_template)
|
|
|
|
string.gsub!(%r{######SIGNATURE_MARKER######}, '')
|
2017-03-10 06:49:01 +00:00
|
|
|
return string.chomp
|
|
|
|
end
|
2017-04-29 09:44:35 +00:00
|
|
|
string.gsub!(%r{(<p>[[:space:]]*</p>([[:space:]]*)){2,}}im, '<p> </p>\2')
|
2017-03-13 18:38:29 +00:00
|
|
|
string.gsub!(%r\<div>[[:space:]]*(<br(|/)>([[:space:]]*)){2,}\im, '<div><br>\3')
|
|
|
|
string.gsub!(%r\[[:space:]]*(<br>[[:space:]]*){3,}[[:space:]]*</div>\im, '<br><br></div>')
|
2017-04-29 09:44:35 +00:00
|
|
|
string.gsub!(%r\<div>[[:space:]]*(<br>[[:space:]]*){1,}[[:space:]]*</div>\im, '<div> </div>')
|
2017-08-14 08:44:48 +00:00
|
|
|
string.gsub!(%r\<div>[[:space:]]*(<div>[[:space:]]*</div>[[:space:]]*){2,}</div>\im, '<div> </div>')
|
2017-03-13 18:38:29 +00:00
|
|
|
string.gsub!(%r\<p>[[:space:]]*</p>(<br(|/)>[[:space:]]*){2,}[[:space:]]*\im, '<p> </p><br>')
|
|
|
|
string.gsub!(%r{<p>[[:space:]]*</p>(<br(|/)>[[:space:]]*)+<p>[[:space:]]*</p>}im, '<p> </p><p> </p>')
|
|
|
|
string.gsub!(%r\(<div>[[:space:]]*</div>[[:space:]]*){2,}\im, '<div> </div>')
|
2017-04-29 09:44:35 +00:00
|
|
|
string.gsub!(%r{<div> </div>[[:space:]]*(<div> </div>){1,}}im, '<div> </div>')
|
2021-05-12 11:37:44 +00:00
|
|
|
string.gsub!(%r{(<br>[[:space:]]*){3,}}im, '<br><br>')
|
2017-03-13 18:38:29 +00:00
|
|
|
string.gsub!(%r\(<br(|/)>[[:space:]]*){3,}\im, '<br/><br/>')
|
2017-03-10 06:49:01 +00:00
|
|
|
string.gsub!(%r{<p>[[:space:]]+</p>}im, '<p> </p>')
|
2020-06-08 07:17:29 +00:00
|
|
|
string.gsub!(%r{\A(<br(|/)>[[:space:]]*)*}i, '')
|
|
|
|
string.gsub!(%r{[[:space:]]*(<br(|/)>[[:space:]]*)*\Z}i, '')
|
2017-04-28 10:16:15 +00:00
|
|
|
string.gsub!(%r{(<p></p>){1,10}\Z}i, '')
|
2017-03-10 06:49:01 +00:00
|
|
|
|
|
|
|
string.signature_identify('html')
|
|
|
|
|
2016-06-28 20:49:38 +00:00
|
|
|
marker_template = '<span class="js-signatureMarker"></span>'
|
2021-05-12 11:37:44 +00:00
|
|
|
string.sub!(%r{######SIGNATURE_MARKER######}, marker_template)
|
|
|
|
string.gsub!(%r{######SIGNATURE_MARKER######}, '')
|
2016-06-21 15:14:15 +00:00
|
|
|
string.chomp
|
|
|
|
end
|
|
|
|
|
2017-03-10 06:49:01 +00:00
|
|
|
def signature_identify(type = 'text', force = false)
|
2016-06-28 20:49:38 +00:00
|
|
|
string = self
|
|
|
|
|
2017-03-10 06:49:01 +00:00
|
|
|
marker = '######SIGNATURE_MARKER######'
|
|
|
|
|
|
|
|
if type == 'html'
|
|
|
|
map = [
|
|
|
|
'<br(|\/)>[[:space:]]*(--|__)',
|
|
|
|
'<\/div>[[:space:]]*(--|__)',
|
|
|
|
'<p>[[:space:]]*(--|__)',
|
2021-01-08 15:02:19 +00:00
|
|
|
'(<br(|\/)>|<p>|<div>)[[:space:]]*<b>(|<span[[:space:]]lang=".{1,6}">)(Von|From|De|от|Z|Od|Ze|Fra|Van|Mistä|Από|Dal|から|Из|од|iz|Från|จาก|з|Từ):[[:space:]]*(|</span>)</b>',
|
2017-03-13 18:38:29 +00:00
|
|
|
'(<br>|<div>)[[:space:]]*<br>[[:space:]]*(Von|From|De|от|Z|Od|Ze|Fra|Van|Mistä|Από|Dal|から|Из|од|iz|Från|จาก|з|Từ):[[:space:]]+',
|
2017-03-18 10:37:56 +00:00
|
|
|
'<blockquote(|.+?)>[[:space:]]*<div>[[:space:]]*(On|Am|Le|El|Den|Dňa|W dniu|Il|Op|Dne|Dana)[[:space:]]',
|
2017-03-21 08:02:15 +00:00
|
|
|
'<div(|.+?)>[[:space:]]*<br>[[:space:]]*(On|Am|Le|El|Den|Dňa|W dniu|Il|Op|Dne|Dana)[[:space:]].{1,500}<blockquote',
|
2017-03-10 06:49:01 +00:00
|
|
|
]
|
2017-10-01 12:25:52 +00:00
|
|
|
map.each do |regexp|
|
2021-05-12 11:37:44 +00:00
|
|
|
string.sub!(%r{#{regexp}}m) do |placeholder|
|
2019-06-28 11:38:49 +00:00
|
|
|
"#{marker}#{placeholder}"
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
|
|
|
end
|
2017-03-10 06:49:01 +00:00
|
|
|
return string
|
|
|
|
end
|
|
|
|
|
2016-06-28 20:49:38 +00:00
|
|
|
# if we do have less then 10 lines and less then 300 chars ignore this
|
|
|
|
if !force
|
|
|
|
lines = string.split("\n")
|
|
|
|
return if lines.count < 10 && string.length < 300
|
|
|
|
end
|
|
|
|
|
2017-01-16 14:12:06 +00:00
|
|
|
# search for signature separator "--\n"
|
2021-05-12 11:37:44 +00:00
|
|
|
string.sub!(%r{^\s{0,2}--\s{0,2}$}) do |placeholder|
|
2019-06-28 11:38:49 +00:00
|
|
|
"#{marker}#{placeholder}"
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-28 20:49:38 +00:00
|
|
|
|
|
|
|
map = {}
|
|
|
|
# Apple Mail
|
|
|
|
# On 01/04/15 10:55, Bob Smith wrote:
|
|
|
|
map['apple-en'] = '^(On)[[:space:]].{6,20}[[:space:]].{3,10}[[:space:]].{1,250}[[:space:]](wrote):'
|
|
|
|
|
|
|
|
# Am 03.04.2015 um 20:58 schrieb Martin Edenhofer <me@znuny.ink>:
|
|
|
|
map['apple-de'] = '^(Am)[[:space:]].{6,20}[[:space:]](um)[[:space:]].{3,10}[[:space:]](schrieb)[[:space:]].{1,250}:'
|
|
|
|
|
|
|
|
# Thunderbird
|
|
|
|
# Am 04.03.2015 um 12:47 schrieb Alf Aardvark:
|
|
|
|
map['thunderbird-de'] = '^(Am)[[:space:]].{6,20}[[:space:]](um)[[:space:]].{3,10}[[:space:]](schrieb)[[:space:]].{1,250}:'
|
|
|
|
|
|
|
|
# Thunderbird default - http://kb.mozillazine.org/Reply_header_settings
|
|
|
|
# On 01-01-2007 11:00 AM, Alf Aardvark wrote:
|
|
|
|
map['thunderbird-en-default'] = '^(On)[[:space:]].{6,20}[[:space:]].{3,10},[[:space:]].{1,250}(wrote):'
|
|
|
|
|
|
|
|
# http://kb.mozillazine.org/Reply_header_settings
|
|
|
|
# Alf Aardvark wrote, on 01-01-2007 11:00 AM:
|
|
|
|
map['thunderbird-en'] = '^.{1,250}[[:space:]](wrote),[[:space:]]on[[:space:]].{3,20}:'
|
|
|
|
|
|
|
|
# otrs
|
|
|
|
# 25.02.2015 10:26 - edv hotline wrote:
|
|
|
|
# 25.02.2015 10:26 - edv hotline schrieb:
|
|
|
|
map['otrs-en-de'] = '^.{6,10}[[:space:]].{3,10}[[:space:]]-[[:space:]].{1,250}[[:space:]](wrote|schrieb):'
|
|
|
|
|
|
|
|
# Ms
|
|
|
|
# From: Martin Edenhofer via Znuny Support [mailto:support@znuny.inc]
|
|
|
|
# Send: Donnerstag, 2. April 2015 10:00
|
|
|
|
# To/Cc/Bcc: xxx
|
|
|
|
# Subject: xxx
|
|
|
|
# - or -
|
|
|
|
# From: xxx
|
|
|
|
# To/Cc/Bcc: xxx
|
|
|
|
# Date: 01.04.2015 12:41
|
|
|
|
# Subject: xxx
|
|
|
|
# - or -
|
|
|
|
# De : xxx
|
|
|
|
# À/?/?: xxx
|
|
|
|
# Envoyé : mercredi 29 avril 2015 17:31
|
|
|
|
# Objet : xxx
|
|
|
|
# en/de/fr | sometimes ms adds a space to "xx : value"
|
2017-03-10 06:49:01 +00:00
|
|
|
map['ms-en-de-fr_from'] = '^(Von|From|De|от|Z|Od|Ze|Fra|Van|Mistä|Από|Dal|から|Из|од|iz|Från|จาก|з|Từ)( ?):[[:space:]].+?'
|
2016-06-28 20:49:38 +00:00
|
|
|
map['ms-en-de-fr_from_html'] = "\n######b######(From|Von|De)([[:space:]]?):([[:space:]]?)(######\/b######)[[:space:]].+?"
|
|
|
|
|
|
|
|
# word 14
|
|
|
|
# edv hotline wrote:
|
|
|
|
# edv hotline schrieb:
|
2021-07-16 13:44:10 +00:00
|
|
|
# map['word-en-de'] = "[^#{marker}].{1,250}\s(wrote|schrieb):"
|
2016-06-28 20:49:38 +00:00
|
|
|
|
2017-11-23 08:09:44 +00:00
|
|
|
map.each_value do |regexp|
|
2021-05-12 11:37:44 +00:00
|
|
|
string.sub!(%r{#{regexp}}) do |placeholder|
|
2019-06-28 11:38:49 +00:00
|
|
|
"#{marker}#{placeholder}"
|
|
|
|
rescue
|
|
|
|
# regexp was not possible because of some string encoding issue, use next
|
|
|
|
Rails.logger.debug { "Invalid string/charset combination with regexp #{regexp} in string" }
|
2017-03-10 06:49:01 +00:00
|
|
|
end
|
2017-10-01 12:25:52 +00:00
|
|
|
end
|
2016-06-28 20:49:38 +00:00
|
|
|
|
|
|
|
string
|
|
|
|
end
|
2018-06-01 11:32:59 +00:00
|
|
|
|
|
|
|
# Returns a copied string whose encoding is UTF-8.
|
|
|
|
# If both the provided and current encodings are invalid,
|
|
|
|
# an auto-detected encoding is tried.
|
|
|
|
#
|
|
|
|
# Supports some fallback strategies if a valid encoding cannot be found.
|
|
|
|
#
|
|
|
|
# Options:
|
|
|
|
#
|
|
|
|
# * from: An encoding to try first.
|
|
|
|
# Takes precedence over the current and auto-detected encodings.
|
|
|
|
#
|
|
|
|
# * fallback: The strategy to follow if no valid encoding can be found.
|
|
|
|
# * `:output_to_binary` returns an ASCII-8BIT-encoded string.
|
|
|
|
# * `:read_as_sanitized_binary` returns a UTF-8-encoded string with all
|
|
|
|
# invalid byte sequences replaced with "?" characters.
|
2021-07-06 07:52:22 +00:00
|
|
|
def utf8_encode(...)
|
|
|
|
dup.utf8_encode!(...)
|
2018-06-01 11:32:59 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def utf8_encode!(**options)
|
2018-08-09 04:05:09 +00:00
|
|
|
return force_encoding('utf-8') if dup.force_encoding('utf-8').valid_encoding?
|
2018-06-01 11:32:59 +00:00
|
|
|
|
2018-11-26 19:47:35 +00:00
|
|
|
# convert string to given charset, if valid_encoding? is true
|
|
|
|
if options[:from].present?
|
|
|
|
begin
|
|
|
|
encoding = Encoding.find(options[:from])
|
|
|
|
if encoding.present? && dup.force_encoding(encoding).valid_encoding?
|
|
|
|
force_encoding(encoding)
|
|
|
|
return encode!('utf-8', encoding)
|
|
|
|
end
|
|
|
|
rescue ArgumentError, EncodingError => e
|
|
|
|
Rails.logger.error { e.inspect }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# try to find valid encodings of string
|
|
|
|
viable_encodings.each do |enc|
|
2019-06-27 18:26:28 +00:00
|
|
|
|
|
|
|
return encode!('utf-8', enc)
|
|
|
|
rescue EncodingError => e
|
|
|
|
Rails.logger.error { e.inspect }
|
|
|
|
|
2018-08-09 04:05:09 +00:00
|
|
|
end
|
2018-06-01 11:32:59 +00:00
|
|
|
|
|
|
|
case options[:fallback]
|
|
|
|
when :output_to_binary
|
|
|
|
force_encoding('ascii-8bit')
|
|
|
|
when :read_as_sanitized_binary
|
|
|
|
encode!('utf-8', 'ascii-8bit', invalid: :replace, undef: :replace, replace: '?')
|
|
|
|
else
|
|
|
|
raise EncodingError, 'could not find a valid input encoding'
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def viable_encodings(try_first: nil)
|
|
|
|
return dup.viable_encodings(try_first: try_first) if frozen?
|
|
|
|
|
|
|
|
provided = Encoding.find(try_first) if try_first.present?
|
|
|
|
original = encoding
|
|
|
|
detected = CharDet.detect(self)['encoding']
|
|
|
|
|
|
|
|
[provided, original, detected]
|
|
|
|
.compact
|
|
|
|
.reject { |e| Encoding.find(e) == Encoding::ASCII_8BIT }
|
2018-08-09 04:05:09 +00:00
|
|
|
.reject { |e| Encoding.find(e) == Encoding::UTF_8 }
|
2018-06-01 11:32:59 +00:00
|
|
|
.select { |e| force_encoding(e).valid_encoding? }
|
|
|
|
.tap { force_encoding(original) } # clean up changes from previous line
|
|
|
|
|
|
|
|
# if `try_first` is not a valid encoding, try_first again without it
|
|
|
|
rescue ArgumentError
|
|
|
|
try_first.present? ? viable_encodings : raise
|
|
|
|
end
|
2015-04-27 14:15:29 +00:00
|
|
|
end
|