Class: MailAutoLinkObfuscation::AutoLinkObfuscator

Inherits:
Object
  • Object
show all
Defined in:
lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb

Constant Summary collapse

AUTO_LINKED_EMAIL_PATTERN =
/\S+@\w+(?:\.\w+)+/
AUTO_LINKED_URL_PATTERN =
%r{((\w+:)?//\w+(\.\w+)*|\w+(\.\w+)*\.\w{2,})\S*}
AUTO_LINKED_PATTERN =
Regexp.new([AUTO_LINKED_EMAIL_PATTERN, AUTO_LINKED_URL_PATTERN].join('|'))
KEY_CHARS =
%r{[@.:/]+}

Instance Method Summary collapse

Constructor Details

#initialize(mail, options) ⇒ AutoLinkObfuscator

Returns a new instance of AutoLinkObfuscator.



11
12
13
14
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 11

def initialize(mail, options)
  @mail = mail
  @options = options || {}
end

Instance Method Details



31
32
33
34
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 31

def extract_link_whitelist_from(doc)
  return Set.new unless doc
  doc.xpath('//@href').map(&:content).to_set
end


25
26
27
28
29
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 25

def extract_link_whitelist_from_html
  @link_whitelist =
    extract_link_whitelist_from(html_body_doc) +
    extract_link_whitelist_from(html_part_doc)
end

#html_body_docObject



36
37
38
39
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 36

def html_body_doc
  return unless @mail.content_type.include? 'text/html'
  @html_body_doc ||= Nokogiri::HTML(@mail.body.decoded)
end

#html_part_docObject



41
42
43
44
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 41

def html_part_doc
  return unless @mail.html_part
  @html_part_doc ||= Nokogiri::HTML(@mail.html_part.body.decoded)
end

#runObject



16
17
18
19
20
21
22
23
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 16

def run
  extract_link_whitelist_from_html
  transform_html_body if @mail.content_type.include? 'text/html'
  transform_text_body if @mail.content_type.include? 'text/plain'
  transform_html_part if @mail.html_part
  transform_text_part if @mail.text_part
  @mail
end

#transform_auto_linked_pattern(text) ⇒ Object



77
78
79
80
81
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 77

def transform_auto_linked_pattern(text)
  text.gsub(AUTO_LINKED_PATTERN) do |match|
    @link_whitelist.include?(match) ? match : yield(match)
  end
end

#transform_html(doc) ⇒ Object



54
55
56
57
58
59
60
61
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 54

def transform_html(doc)
  doc.xpath('//body/descendant::text()').each do |node|
    text = CGI.escapeHTML(node.content)
    node.replace(transform_text(text))
  end

  doc.to_s
end

#transform_html_bodyObject



46
47
48
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 46

def transform_html_body
  @mail.body = transform_html(html_body_doc)
end

#transform_html_partObject



50
51
52
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 50

def transform_html_part
  @mail.html_part.body = transform_html(html_part_doc)
end

#transform_text(text) ⇒ Object



71
72
73
74
75
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 71

def transform_text(text)
  transform_auto_linked_pattern(text) do |match|
    match.gsub(KEY_CHARS, "\u200C\\0\u200C")
  end
end

#transform_text_bodyObject



63
64
65
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 63

def transform_text_body
  @mail.body = transform_text(@mail.body.decoded)
end

#transform_text_partObject



67
68
69
# File 'lib/mail_auto_link_obfuscation/auto_link_obfuscator.rb', line 67

def transform_text_part
  @mail.text_part.body = transform_text(@mail.text_part.body.decoded)
end