Module: MailParser::RFC2047

Defined in:
lib/mailparser/rfc2047.rb

Defined Under Namespace

Classes: Encoded, Space

Class Method Summary collapse

Class Method Details

.b_decode(str) ⇒ Object



55
56
57
# File 'lib/mailparser/rfc2047.rb', line 55

def b_decode(str)
  return str.gsub(/[^A-Z0-9\+\/=]/i,"").unpack("m")[0]
end

.decode(str, opt = nil) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/mailparser/rfc2047.rb', line 15

def decode(str, opt=nil)
  if opt.is_a? Hash
    charset = opt[:output_charset]
    charset_converter = opt[:charset_converter]
  else
    charset = opt
  end
  charset_converter ||= MailParser::ConvCharset.method(:conv_charset)
  words = []
  mime_word = false
  ss = StringScanner.new(str.gsub(/\r?\n/, ''))
  until ss.eos?
    if s = ss.scan(/\=\?[^\(\)\<\>\@\,\;\:\"\/\[\]\?\.\=]+\?[QB]\?[^\? ]+\?\=/i)
      begin
        s = Encoded.new s, charset, charset_converter
        words.pop if words.length >= 2 and words[-1].is_a? Space and words[-2].is_a? Encoded
      rescue
        # ignore
      end
      words.push s
    elsif s = ss.scan(/\s+/)
      words.push Space.new(s)
    elsif s = ss.scan(/[^\s=]+/)
      words.push s
    else
      words.push ss.scan(/./)
    end
  end
  begin
    ret = words.join
  rescue
    ret = words.map{|s| s.to_s.force_encoding('binary')}.join
  end
  charset ? charset_converter.call(charset, charset, ret) : ret
end

.q_decode(str) ⇒ Object



51
52
53
# File 'lib/mailparser/rfc2047.rb', line 51

def q_decode(str)
  return str.gsub(/_/," ").gsub(/=\s*?$/,"=").unpack("M")[0]
end