Module: Rfc2047

Defined in:
lib/whisper/rfc2047.rb

Constant Summary collapse

WORD =

:nodoc: ‘stupid ruby-mode

%r{=\?([!\#$%&'*+-/0-9A-Z\\^\`a-z{|}~]+)\?([BbQq])\?([!->@-~]+)\?=}
WORDSEQ =
%r{(#{WORD.source})\s+(?=#{WORD.source})}

Class Method Summary collapse

Class Method Details

.decode_to(target, from) ⇒ Object

Decodes a string, from, containing RFC 2047 encoded words into a target character set, target. See iconv_open(3) for information on the supported target encodings. If one of the encoded words cannot be converted to the target encoding, it is left in its encoded form.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/whisper/rfc2047.rb', line 50

def Rfc2047.decode_to(target, from)
  from = from.gsub(WORDSEQ, '\1')
  out = from.gsub(WORD) do |word|
    charset, encoding, text = $1, $2, $3

    # B64 or QP decode, as necessary:
    case encoding
      when 'b', 'B'
        text = text.unpack('m*')[0]

      when 'q', 'Q'
        # RFC 2047 has a variant of quoted printable where a ' ' character
        # can be represented as an '_', rather than =32, so convert
        # any of these that we find before doing the QP decoding.
        text = text.tr("_", " ")
        text = text.unpack('M*')[0]

      # Don't need an else, because no other values can be matched in a
      # WORD.
    end

    begin
      Iconv.easy_decode target, charset, text
    rescue Iconv::InvalidCharacter => e
      puts "ICONV ERROR: #{e.message}"
      text
    end
  end
end

.is_encoded?(s) ⇒ Boolean

Returns:

  • (Boolean)


44
# File 'lib/whisper/rfc2047.rb', line 44

def Rfc2047.is_encoded? s; s =~ WORD end