Class: PhisherPhinder::MailParser::HeaderValueParser

Inherits:
Object
  • Object
show all
Defined in:
lib/phisher_phinder/mail_parser/header_value_parser.rb

Instance Method Summary collapse

Instance Method Details

#parse(raw_value) ⇒ Object



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
# File 'lib/phisher_phinder/mail_parser/header_value_parser.rb', line 6

def parse(raw_value)
  stripped_value = raw_value.strip
  words = stripped_value.split(' ')
  words.map do |word|
    if encoded?(word)
      matches = word.match(/\A=\?(?<character_set>.+)\?(?<encoding>.)\?(?<content>.+)\z/)

      unencoded_content = if matches[:encoding].downcase == 'b'
                            Base64.decode64(matches[:content])
                          elsif matches[:encoding].downcase == 'q'
                            matches[:content].unpack('M').first
                          end

      content = if matches[:character_set] =~ /iso-8859-1/i
                  unencoded_content.force_encoding('ISO-8859-1').encode('UTF-8')
                elsif matches[:character_set] =~ /windows-1251/i
                  unencoded_content.force_encoding('cp1251').encode('UTF-8')
                elsif matches[:character_set] =~ /utf-8/i
                  unencoded_content.force_encoding('UTF-8')
                end
    else
      word
    end
  end.join(' ')
end