Module: LooseMail

Defined in:
lib/loose_mail.rb,
lib/loose_mail/version.rb

Constant Summary collapse

ENCODED_VALUE =
/\A=\?([^?]+)\?([QB])\?([^?]*)\?=\z/i
VERSION =
"0.1.0"

Class Method Summary collapse

Class Method Details

.decode_data(type, data) ⇒ Object



25
26
27
28
29
30
# File 'lib/loose_mail.rb', line 25

def self.decode_data(type, data)
  case type
  when 'q', 'Q' then data.gsub(/_/, ' ').unpack1('M')
  when 'b', 'B' then data.unpack1('m')
  end
end

.decode_words(words) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/loose_mail.rb', line 8

def self.decode_words(words)
  results = []
  while (word = words.shift)
    if (m = word.match(ENCODED_VALUE))
      charset = m[1].downcase
      word = decode_data(m[2], m[3])
      while (m = words.first&.match(ENCODED_VALUE)) && m[1].downcase == charset
        word.concat decode_data(m[2], m[3])
        words.shift
      end
      word = Mail::Ruby19.charset_encoder.encode(word, charset).encode(Encoding::UTF_8, undef: :replace, invalid: :replace)
    end
    results.push word
  end
  results.join
end