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
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
|