Class: Mail::Jdec::ValueDecoder

Inherits:
Object
  • Object
show all
Extended by:
Constants
Defined in:
lib/mail/jdec/decoder.rb

Class Method Summary collapse

Class Method Details

.value_decode(str) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/mail/jdec/decoder.rb', line 42

def value_decode(str)
  # Optimization: If there's no encoded-words in the string, just return it
  return str unless str =~ ENCODED_VALUE

  lines = Mail::Encodings.send(:collapse_adjacent_encodings, str)

  lines = lines.chunk do |value|
    if value =~ ENCODED_VALUE
      $1
    else
      ''
    end
  end

  # Split on white-space boundaries with capture, so we capture the white-space as well
  lines.map do |charset, values|
    content = values.join
    if content =~ ENCODED_VALUE
      bytes = content.scan(/\=\?([^?]+)\?([QB])\?([^?]*?)\?\=/mi).map do |_, encoding, encoded|
        case encoding
        when *B_VALUES then Mail::Utilities.decode_base64(encoded)
        when *Q_VALUES then Mail::Encodings::QuotedPrintable.decode(encoded.gsub(/_/, '=20').sub(/\=$/, ''))
        end
      end.join('')
      Mail::Encodings.transcode_charset(bytes, charset)
    else
      content
    end
  end.join('')
end