mailparser 0.4.22a → 0.5.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,9 +1,8 @@
1
+ # coding: ascii-8bit
1
2
  # Copyright (C) 2006-2010 TOMITA Masahiro
2
3
  # mailto:tommy@tmtm.org
3
4
 
4
5
  require "strscan"
5
- require "iconv"
6
- require "nkf"
7
6
  require "mailparser/conv_charset"
8
7
 
9
8
  module MailParser
@@ -11,26 +10,6 @@ end
11
10
 
12
11
  module MailParser::RFC2047
13
12
 
14
- class String < ::String
15
- @@charset_converter = Proc.new{|f,t,s| MailParser::ConvCharset.conv_charset(f,t,s)}
16
- def initialize(str, charset=nil, raw=nil, charset_converter=nil)
17
- super(str)
18
- @charset = charset
19
- @raw = raw || str
20
- @charset_converter = charset_converter || @@charset_converter
21
- end
22
- attr_reader :charset
23
- attr_reader :raw
24
-
25
- def conv_charset(to_charset)
26
- if @charset and to_charset
27
- @charset_converter.call @charset, to_charset, self
28
- else
29
- self
30
- end
31
- end
32
- end
33
-
34
13
  module_function
35
14
 
36
15
  def decode(str, opt=nil)
@@ -40,35 +19,45 @@ module MailParser::RFC2047
40
19
  else
41
20
  charset = opt
42
21
  end
22
+ charset_converter ||= MailParser::ConvCharset.method(:conv_charset)
43
23
  last_charset = nil
44
24
  ret = ""
45
- split_decode(str, charset_converter).each do |s|
46
- begin
47
- s2 = charset && s.charset ? s.conv_charset(charset) : s
48
- cs = s.charset
49
- rescue Iconv::Failure
50
- s2 = s.raw
51
- cs = nil
25
+ ret.force_encoding(charset) if String.method_defined? :force_encoding and charset
26
+ split_decode(str) do |s, cs, raw|
27
+ if charset
28
+ begin
29
+ s = charset_converter.call(cs || charset, charset, s)
30
+ rescue
31
+ s = raw
32
+ cs = nil
33
+ end
52
34
  end
53
35
  ret << " " if last_charset.nil? or cs.nil?
54
- ret << s2
36
+ ret << s
55
37
  last_charset = cs
56
38
  end
57
39
  return ret.strip
58
40
  end
59
41
 
60
- def split_decode(str, charset_converter=nil)
61
- ret = []
62
- while str =~ /\=\?([^\(\)\<\>\@\,\;\:\"\/\[\]\?\.\=]+)\?([QB])\?([^\? ]+)\?\=/ni do
42
+ def split_decode(str)
43
+ while str =~ /\=\?([^\(\)\<\>\@\,\;\:\"\/\[\]\?\.\=]+)\?([QB])\?([^\? ]+)\?\=/i do
63
44
  raw = $&
64
45
  pre, charset, encoding, enc_text, after = $`, $1.downcase, $2.downcase, $3, $'
65
- ret << String.new(pre.strip) unless pre.strip.empty?
46
+ s = pre.strip
47
+ yield s, nil, raw unless s.empty?
66
48
  s = encoding == "q" ? q_decode(enc_text) : b_decode(enc_text)
67
- ret << String.new(s, charset, raw, charset_converter)
49
+ if String.method_defined? :force_encoding
50
+ begin
51
+ s.force_encoding(charset)
52
+ rescue
53
+ s.force_encoding('ascii-8bit')
54
+ end
55
+ end
56
+ yield s, charset, raw
68
57
  str = after
69
58
  end
70
- ret << String.new(str.strip) unless str.empty?
71
- return ret
59
+ s = str.strip
60
+ yield s, nil, raw unless s.empty?
72
61
  end
73
62
 
74
63
  def q_decode(str)
@@ -1,3 +1,4 @@
1
+ # coding: ascii-8bit
1
2
  #
2
3
  # DO NOT MODIFY!!!!
3
4
  # This file is automatically generated by Racc 1.4.7
@@ -26,7 +27,7 @@ def parse(header_type, value)
26
27
  end
27
28
 
28
29
  def parse_sub(&block)
29
- yield @header_type, nil
30
+ yield [@header_type, nil]
30
31
  @scanner.scan(&block)
31
32
  end
32
33
 
@@ -1,3 +1,4 @@
1
+ # coding: ascii-8bit
1
2
  # Copyright (C) 2006-2010 TOMITA Masahiro
2
3
  # mailto:tommy@tmtm.org
3
4
 
@@ -1,3 +1,4 @@
1
+ # coding: ascii-8bit
1
2
  # Copyright (C) 2006-2010 TOMITA Masahiro
2
3
  # mailto:tommy@tmtm.org
3
4
 
@@ -1,3 +1,4 @@
1
+ # coding: ascii-8bit
1
2
  # Copyright (C) 2006-2010 TOMITA Masahiro
2
3
  # mailto:tommy@tmtm.org
3
4
 
@@ -12,7 +13,7 @@ module MailParser::RFC2231
12
13
  char_lang = {}
13
14
  params.each do |key, value|
14
15
  case key
15
- when /^([^\*]+)(\*0)?\*$/no
16
+ when /^([^\*]+)(\*0)?\*$/
16
17
  name, ord = $1, $2
17
18
  char, lang, v = value.split(/\'/, 3)
18
19
  char_lang[name] = [char, lang]
@@ -20,17 +21,17 @@ module MailParser::RFC2231
20
21
  raise MailParser::ParseError, "#{key}=#{value}" if opt[:strict]
21
22
  v = lang || char
22
23
  end
23
- v = v.gsub(/%([0-9A-F][0-9A-F])/ni){$1.hex.chr}
24
+ v = v.gsub(/%([0-9A-F][0-9A-F])/i){$1.hex.chr}
24
25
  if ord then
25
26
  h[name] << [0, v]
26
27
  else
27
28
  newparams[name] = v
28
29
  end
29
- when /^([^\*]+)\*([1-9]\d*)\*$/no
30
+ when /^([^\*]+)\*([1-9]\d*)\*$/
30
31
  name, ord = $1, $2.to_i
31
- v = value.gsub(/%([0-9A-F][0-9A-F])/ni){$1.hex.chr}
32
+ v = value.gsub(/%([0-9A-F][0-9A-F])/i){$1.hex.chr}
32
33
  h[name] << [ord, v]
33
- when /^([^\*]+)\*([0-9]\d*)$/no
34
+ when /^([^\*]+)\*([0-9]\d*)$/
34
35
  name, ord = $1, $2.to_i
35
36
  h[name] << [ord, value]
36
37
  else