mail-iso-2022-jp 1.3.0 → 2.0.0.pre

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
+ mail-iso-2022-jp
2
+ ================
3
+
1
4
  A patch that provides 'mail' gem with iso-2022-jp conversion capability.
2
- ========================================================================
3
5
 
4
6
  Overview
5
7
  --------
@@ -110,11 +112,19 @@ or run this command:
110
112
  Remarks
111
113
  -------
112
114
 
113
- * `①` などのNEC特殊文字や `髙` や `﨑` といったIBM拡張文字を件名、宛先、本文などに含めることができます。
114
- * 全角チルダ(U+FF5E)は波ダッシュ(U+301C)に変換されます。
115
- * 半角カタカナはそのまま維持されます。
116
- * 変換できない文字は疑問符(?)で置換されます。
117
- * マルチパートメールのテキストパートもiso-2022-jpでエンコードされます。
115
+ * (en)
116
+ * NEC special characters like `①` and IBM extended characters like `髙`, `﨑` are allowed in the subject, recipient names and mail body.
117
+ * Fullwidth tildes (U+FF5E) are translated into wave dashes (U+301C).
118
+ * Half-width (Hankaku) katakanas are maintained intact.
119
+ * Characters that cannot be translated into iso-2022-jp encoding are substituted with question marks (`?`).
120
+ * The text part of multipart mail is also encoded with iso-2022-jp.
121
+
122
+ * (ja)
123
+ * `①` などのNEC特殊文字や `髙` や `﨑` といったIBM拡張文字を件名、宛先、本文などに含めることができます。
124
+ * 全角チルダ(U+FF5E)は波ダッシュ(U+301C)に変換されます。
125
+ * 半角カタカナはそのまま維持されます。
126
+ * 変換できない文字は疑問符(`?`)で置換されます。
127
+ * マルチパートメールのテキストパートもiso-2022-jpでエンコードされます。
118
128
 
119
129
  References
120
130
  ----------
@@ -1 +1,14 @@
1
- require 'mail-iso-2022-jp/patches'
1
+ if RUBY_VERSION >= '1.9'
2
+ require 'mail-iso-2022-jp/mail'
3
+ require 'mail-iso-2022-jp/message'
4
+ require 'mail-iso-2022-jp/field'
5
+ require 'mail-iso-2022-jp/field_with_iso_2022_jp_encoding'
6
+ require 'mail-iso-2022-jp/body'
7
+ else
8
+ require 'mail-iso-2022-jp/ruby18/mail'
9
+ require 'mail-iso-2022-jp/ruby18/message'
10
+ require 'mail-iso-2022-jp/ruby18/field_with_iso_2022_jp_encoding'
11
+ end
12
+
13
+ require 'mail-iso-2022-jp/header'
14
+ require 'mail-iso-2022-jp/fields'
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ module Mail
4
+ class Body
5
+ def initialize_with_iso_2022_jp_encoding(string = '')
6
+ if string.respond_to?(:encoding) && string.encoding.to_s == 'ISO-2022-JP'
7
+ string.force_encoding('US-ASCII')
8
+ end
9
+ initialize_without_iso_2022_jp_encoding(string)
10
+ end
11
+ alias_method :initialize_without_iso_2022_jp_encoding, :initialize
12
+ alias_method :initialize, :initialize_with_iso_2022_jp_encoding
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # coding:utf-8
2
+
3
+ require 'mail'
4
+
5
+ module Mail
6
+ class Field
7
+ def initialize_with_iso_2022_jp_encoding(name, value = nil, charset = 'utf-8')
8
+ if charset == 'ISO-2022-JP' && value.kind_of?(String)
9
+ unless [ 'UTF-8', 'US-ASCII' ].include?(value.encoding.to_s)
10
+ raise ::Mail::InvalidEncodingError.new(
11
+ "The '#{name}' field is not encoded in UTF-8 nor in US-ASCII but in #{value.encoding}")
12
+ end
13
+ end
14
+ initialize_without_iso_2022_jp_encoding(name, value, charset)
15
+ end
16
+ alias_method :initialize_without_iso_2022_jp_encoding, :initialize
17
+ alias_method :initialize, :initialize_with_iso_2022_jp_encoding
18
+ end
19
+ end
@@ -0,0 +1,73 @@
1
+ # coding:utf-8
2
+
3
+ require 'mail'
4
+ require 'base64'
5
+
6
+ module Mail
7
+ module FieldWithIso2022JpEncoding
8
+ def self.included(base)
9
+ base.send :alias_method, :initialize_without_iso_2022_jp_encoding, :initialize
10
+ base.send :alias_method, :initialize, :initialize_with_iso_2022_jp_encoding
11
+ base.send :alias_method, :do_decode_without_iso_2022_jp_encoding, :do_decode
12
+ base.send :alias_method, :do_decode, :do_decode_with_iso_2022_jp_encoding
13
+ end
14
+
15
+ def initialize_with_iso_2022_jp_encoding(value = nil, charset = 'utf-8')
16
+ if charset.to_s.downcase == 'iso-2022-jp'
17
+ if value.kind_of?(Array)
18
+ value = value.map { |e| encode_with_iso_2022_jp(e, charset) }
19
+ else
20
+ value = encode_with_iso_2022_jp(value, charset)
21
+ end
22
+ end
23
+ initialize_without_iso_2022_jp_encoding(value, charset)
24
+ end
25
+
26
+ private
27
+ def do_decode_with_iso_2022_jp_encoding
28
+ if charset.to_s.downcase == 'iso-2022-jp'
29
+ value
30
+ else
31
+ do_decode_without_iso_2022_jp_encoding
32
+ end
33
+ end
34
+
35
+ def encode_with_iso_2022_jp(value, charset)
36
+ value = value.to_s.gsub(/#{WAVE_DASH}/, FULLWIDTH_TILDE)
37
+ value = Mail.encoding_to_charset(value, charset)
38
+ value.force_encoding('ascii-8bit')
39
+ value = b_value_encode(value)
40
+ value.force_encoding('ascii-8bit')
41
+ end
42
+
43
+ def b_value_encode(string)
44
+ string.split(' ').map do |s|
45
+ if s =~ /\e/
46
+ encode64(s)
47
+ else
48
+ s
49
+ end
50
+ end.join(" ")
51
+ end
52
+
53
+ private
54
+ def encode(value)
55
+ if charset.to_s.downcase == 'iso-2022-jp'
56
+ value
57
+ else
58
+ super(value)
59
+ end
60
+ end
61
+
62
+ def encode_crlf(value)
63
+ if charset.to_s.downcase == 'iso-2022-jp'
64
+ value.force_encoding('ascii-8bit')
65
+ end
66
+ super(value)
67
+ end
68
+
69
+ def encode64(string)
70
+ "=?ISO-2022-JP?B?#{Base64.encode64(string).gsub("\n", "")}?="
71
+ end
72
+ end
73
+ end
@@ -0,0 +1,48 @@
1
+ # coding:utf-8
2
+
3
+ require 'mail'
4
+
5
+ module Mail
6
+ class SubjectField < UnstructuredField
7
+ include FieldWithIso2022JpEncoding
8
+ def b_value_encode(string)
9
+ encode64(string)
10
+ end
11
+ end
12
+
13
+ class FromField < StructuredField
14
+ include FieldWithIso2022JpEncoding
15
+ end
16
+
17
+ class SenderField < StructuredField
18
+ include FieldWithIso2022JpEncoding
19
+ end
20
+
21
+ class ToField < StructuredField
22
+ include FieldWithIso2022JpEncoding
23
+ end
24
+
25
+ class CcField < StructuredField
26
+ include FieldWithIso2022JpEncoding
27
+ end
28
+
29
+ class ReplyToField < StructuredField
30
+ include FieldWithIso2022JpEncoding
31
+ end
32
+
33
+ class ResentFromField < StructuredField
34
+ include FieldWithIso2022JpEncoding
35
+ end
36
+
37
+ class ResentSenderField < StructuredField
38
+ include FieldWithIso2022JpEncoding
39
+ end
40
+
41
+ class ResentToField < StructuredField
42
+ include FieldWithIso2022JpEncoding
43
+ end
44
+
45
+ class ResentCcField < StructuredField
46
+ include FieldWithIso2022JpEncoding
47
+ end
48
+ end
@@ -0,0 +1,13 @@
1
+ # coding:utf-8
2
+
3
+ module Mail
4
+ class Header
5
+ def encoded
6
+ buffer = ''
7
+ fields.each do |field|
8
+ buffer << field.encoded rescue Encoding::CompatibilityError
9
+ end
10
+ buffer
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ # coding: utf-8
2
+
3
+ # Patches for Mail on Ruby 1.8.7
4
+ module Mail
5
+ WAVE_DASH = "〜" # U+301C
6
+ FULLWIDTH_TILDE = "~" # U+FF5E
7
+ ENCODE = { 'iso-2022-jp' => Encoding::CP50221 }
8
+
9
+ def self.encoding_to_charset(str, charset)
10
+ str.encode(ENCODE[charset.to_s.downcase] || charset, :undef => :replace).force_encoding(charset)
11
+ end
12
+
13
+ class InvalidEncodingError < StandardError; end
14
+ end
@@ -0,0 +1,39 @@
1
+ # coding: utf-8
2
+
3
+ require 'mail'
4
+
5
+ # Patches for Mail::Message on Ruby 1.9.x or above
6
+ module Mail
7
+ class Message
8
+ def body_with_iso_2022_jp_encoding=(value)
9
+ if @charset.to_s.downcase == 'iso-2022-jp'
10
+ if value.respond_to?(:encoding) && value.encoding.to_s != 'UTF-8'
11
+ raise ::Mail::InvalidEncodingError.new(
12
+ "The mail body is not encoded in UTF-8 but in #{value.encoding}")
13
+ end
14
+ end
15
+ self.body_without_iso_2022_jp_encoding = value
16
+ end
17
+ alias_method :body_without_iso_2022_jp_encoding=, :body=
18
+ alias_method :body=, :body_with_iso_2022_jp_encoding=
19
+
20
+ def process_body_raw_with_iso_2022_jp_encoding
21
+ if @charset.to_s.downcase == 'iso-2022-jp'
22
+ @body_raw = @body_raw.to_s.gsub(/#{WAVE_DASH}/, FULLWIDTH_TILDE)
23
+ @body_raw = Mail.encoding_to_charset(@body_raw, @charset)
24
+ end
25
+ process_body_raw_without_iso_2022_jp_encoding
26
+ end
27
+ alias_method :process_body_raw_without_iso_2022_jp_encoding, :process_body_raw
28
+ alias_method :process_body_raw, :process_body_raw_with_iso_2022_jp_encoding
29
+
30
+ def text_part_with_iso_2022_jp_encoding=(msg = nil)
31
+ if @charset.to_s.downcase == 'iso-2022-jp' && msg && msg.charset.nil?
32
+ msg.charset = @charset
33
+ end
34
+ self.text_part_without_iso_2022_jp_encoding = msg
35
+ end
36
+ alias_method :text_part_without_iso_2022_jp_encoding=, :text_part=
37
+ alias_method :text_part=, :text_part_with_iso_2022_jp_encoding=
38
+ end
39
+ end
@@ -0,0 +1,65 @@
1
+ # coding: utf-8
2
+
3
+ require 'mail'
4
+ require 'base64'
5
+ require 'nkf'
6
+
7
+ module Mail
8
+ module FieldWithIso2022JpEncoding
9
+ def self.included(base)
10
+ base.send :alias_method, :initialize_without_iso_2022_jp_encoding, :initialize
11
+ base.send :alias_method, :initialize, :initialize_with_iso_2022_jp_encoding
12
+ base.send :alias_method, :do_decode_without_iso_2022_jp_encoding, :do_decode
13
+ base.send :alias_method, :do_decode, :do_decode_with_iso_2022_jp_encoding
14
+ end
15
+
16
+ def initialize_with_iso_2022_jp_encoding(value = nil, charset = 'utf-8')
17
+ if charset.to_s.downcase == 'iso-2022-jp'
18
+ if value.kind_of?(Array)
19
+ value = value.map { |e| encode_with_iso_2022_jp(e) }
20
+ else
21
+ value = encode_with_iso_2022_jp(value)
22
+ end
23
+ end
24
+ initialize_without_iso_2022_jp_encoding(value, charset)
25
+ end
26
+
27
+ private
28
+ def do_decode_with_iso_2022_jp_encoding
29
+ if charset.to_s.downcase == 'iso-2022-jp'
30
+ value
31
+ else
32
+ do_decode_without_iso_2022_jp_encoding
33
+ end
34
+ end
35
+
36
+ def encode_with_iso_2022_jp(value)
37
+ value = value.to_s.gsub(/#{WAVE_DASH}/, FULLWIDTH_TILDE)
38
+ value = NKF.nkf(NKF_OPTIONS, value)
39
+ b_value_encode(value)
40
+ end
41
+
42
+ def b_value_encode(string)
43
+ string.split(' ').map do |s|
44
+ if s =~ /\e/
45
+ encode64(s)
46
+ else
47
+ s
48
+ end
49
+ end.join(" ")
50
+ end
51
+
52
+ private
53
+ def encode(value)
54
+ if charset.to_s.downcase == 'iso-2022-jp'
55
+ value
56
+ else
57
+ super(value)
58
+ end
59
+ end
60
+
61
+ def encode64(string)
62
+ "=?ISO-2022-JP?B?#{Base64.encode64(string).gsub("\n", "")}?="
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,10 @@
1
+ # coding: utf-8
2
+
3
+ # Patches for Mail on Ruby 1.9.x or above
4
+ module Mail
5
+ WAVE_DASH = "〜" # U+301C
6
+ FULLWIDTH_TILDE = "~" # U+FF5E
7
+ NKF_OPTIONS = "--oc=CP50220 -xjW --fb-subchar"
8
+
9
+ class InvalidEncodingError < StandardError; end
10
+ end
@@ -0,0 +1,34 @@
1
+ # coding:utf-8
2
+
3
+ require 'mail'
4
+ require 'nkf'
5
+
6
+ # Patches for Mail::Message on Ruby 1.8.7
7
+ module Mail
8
+ class Message
9
+ def body_with_iso_2022_jp_encoding=(value)
10
+ self.body_without_iso_2022_jp_encoding = value
11
+ end
12
+ alias_method :body_without_iso_2022_jp_encoding=, :body=
13
+ alias_method :body=, :body_with_iso_2022_jp_encoding=
14
+
15
+ def process_body_raw_with_iso_2022_jp_encoding
16
+ if @charset.to_s.downcase == 'iso-2022-jp'
17
+ @body_raw = @body_raw.to_s.gsub(/#{WAVE_DASH}/, FULLWIDTH_TILDE)
18
+ @body_raw = NKF.nkf(NKF_OPTIONS, @body_raw)
19
+ end
20
+ process_body_raw_without_iso_2022_jp_encoding
21
+ end
22
+ alias_method :process_body_raw_without_iso_2022_jp_encoding, :process_body_raw
23
+ alias_method :process_body_raw, :process_body_raw_with_iso_2022_jp_encoding
24
+
25
+ def text_part_with_iso_2022_jp_encoding=(msg = nil)
26
+ if @charset.to_s.downcase == 'iso-2022-jp' && msg && msg.charset.nil?
27
+ msg.charset = @charset
28
+ end
29
+ self.text_part_without_iso_2022_jp_encoding = msg
30
+ end
31
+ alias_method :text_part_without_iso_2022_jp_encoding=, :text_part=
32
+ alias_method :text_part=, :text_part_with_iso_2022_jp_encoding=
33
+ end
34
+ end
metadata CHANGED
@@ -1,7 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail-iso-2022-jp
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.0.pre
5
+ prerelease: 6
5
6
  platform: ruby
6
7
  authors:
7
8
  - Kohei MATSUSHITA
@@ -14,6 +15,7 @@ dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: mail
16
17
  requirement: !ruby/object:Gem::Requirement
18
+ none: false
17
19
  requirements:
18
20
  - - ! '>='
19
21
  - !ruby/object:Gem::Version
@@ -24,6 +26,7 @@ dependencies:
24
26
  type: :runtime
25
27
  prerelease: false
26
28
  version_requirements: !ruby/object:Gem::Requirement
29
+ none: false
27
30
  requirements:
28
31
  - - ! '>='
29
32
  - !ruby/object:Gem::Version
@@ -34,6 +37,7 @@ dependencies:
34
37
  - !ruby/object:Gem::Dependency
35
38
  name: actionmailer
36
39
  requirement: !ruby/object:Gem::Requirement
40
+ none: false
37
41
  requirements:
38
42
  - - ! '>='
39
43
  - !ruby/object:Gem::Version
@@ -41,6 +45,7 @@ dependencies:
41
45
  type: :development
42
46
  prerelease: false
43
47
  version_requirements: !ruby/object:Gem::Requirement
48
+ none: false
44
49
  requirements:
45
50
  - - ! '>='
46
51
  - !ruby/object:Gem::Version
@@ -48,6 +53,7 @@ dependencies:
48
53
  - !ruby/object:Gem::Dependency
49
54
  name: rdoc
50
55
  requirement: !ruby/object:Gem::Requirement
56
+ none: false
51
57
  requirements:
52
58
  - - ! '>='
53
59
  - !ruby/object:Gem::Version
@@ -55,6 +61,7 @@ dependencies:
55
61
  type: :development
56
62
  prerelease: false
57
63
  version_requirements: !ruby/object:Gem::Requirement
64
+ none: false
58
65
  requirements:
59
66
  - - ! '>='
60
67
  - !ruby/object:Gem::Version
@@ -69,29 +76,39 @@ files:
69
76
  - README.md
70
77
  - Gemfile
71
78
  - Rakefile
72
- - lib/mail-iso-2022-jp/patches.rb
79
+ - lib/mail-iso-2022-jp/field.rb
80
+ - lib/mail-iso-2022-jp/body.rb
81
+ - lib/mail-iso-2022-jp/message.rb
82
+ - lib/mail-iso-2022-jp/ruby18/message.rb
83
+ - lib/mail-iso-2022-jp/ruby18/field_with_iso_2022_jp_encoding.rb
84
+ - lib/mail-iso-2022-jp/ruby18/mail.rb
85
+ - lib/mail-iso-2022-jp/field_with_iso_2022_jp_encoding.rb
86
+ - lib/mail-iso-2022-jp/fields.rb
87
+ - lib/mail-iso-2022-jp/header.rb
88
+ - lib/mail-iso-2022-jp/mail.rb
73
89
  - lib/mail-iso-2022-jp.rb
74
90
  homepage: https://github.com/kuroda/mail-iso-2022-jp
75
91
  licenses: []
76
- metadata: {}
77
92
  post_install_message:
78
93
  rdoc_options: []
79
94
  require_paths:
80
95
  - lib
81
96
  required_ruby_version: !ruby/object:Gem::Requirement
97
+ none: false
82
98
  requirements:
83
99
  - - ! '>='
84
100
  - !ruby/object:Gem::Version
85
101
  version: 1.8.7
86
102
  required_rubygems_version: !ruby/object:Gem::Requirement
103
+ none: false
87
104
  requirements:
88
- - - ! '>='
105
+ - - ! '>'
89
106
  - !ruby/object:Gem::Version
90
- version: '0'
107
+ version: 1.3.1
91
108
  requirements: []
92
109
  rubyforge_project:
93
- rubygems_version: 2.0.0.preview2
110
+ rubygems_version: 1.8.24
94
111
  signing_key:
95
- specification_version: 4
112
+ specification_version: 3
96
113
  summary: A set of patches that provides 'mail' gem with iso-2022-jp conversion capability.
97
114
  test_files: []
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: 3b3f589971254d037f705a5c403463be2f42dd61
4
- data.tar.gz: 73ec4eb877b7de25cce0822679fca797aa1f192a
5
- !binary "U0hBNTEy":
6
- metadata.gz: e155386001bfa04d7152e2b2fed11ec8f4d94fbc2896cdfec023c349d74c84a365c706b15237f9ce3b7cfdfdee6dfbd6a8a1911fbc1c6e1177827c3decf7f49a
7
- data.tar.gz: 3dd6d7de6a208007432ab558e5b9256ac2848f4ec45aa00d3653af19437c777a78127e4ad093c17724a2f29310927a0d3a92c5a484140beac4a3ef64726c68bd
@@ -1,211 +0,0 @@
1
- # coding:utf-8
2
-
3
- require 'mail'
4
- require 'base64'
5
- require 'nkf'
6
-
7
- module Mail
8
- WAVE_DASH = "〜" # U+301C
9
- FULLWIDTH_TILDE = "~" # U+FF5E
10
- if RUBY_VERSION >= '1.9'
11
- ENCODE = {'iso-2022-jp' => Encoding::CP50221}
12
- def self.encoding_to_charset(str, charset)
13
- str.encode(ENCODE[charset.to_s.downcase] || charset, :undef => :replace).force_encoding(charset)
14
- end
15
- else
16
- NKF_OPTIONS = "--oc=CP50220 -xjW --fb-subchar"
17
- end
18
-
19
- class InvalidEncodingError < StandardError; end
20
-
21
- class Message
22
- def body_with_iso_2022_jp_encoding=(value)
23
- if @charset.to_s.downcase == 'iso-2022-jp'
24
- if RUBY_VERSION >= '1.9'
25
- if value.respond_to?(:encoding) && value.encoding.to_s != 'UTF-8'
26
- raise ::Mail::InvalidEncodingError.new(
27
- "The mail body is not encoded in UTF-8 but in #{value.encoding}")
28
- end
29
- end
30
- end
31
- self.body_without_iso_2022_jp_encoding = value
32
- end
33
- alias_method :body_without_iso_2022_jp_encoding=, :body=
34
- alias_method :body=, :body_with_iso_2022_jp_encoding=
35
-
36
- def process_body_raw_with_iso_2022_jp_encoding
37
- if @charset.to_s.downcase == 'iso-2022-jp'
38
- @body_raw = @body_raw.to_s.gsub(/#{WAVE_DASH}/, FULLWIDTH_TILDE)
39
- if RUBY_VERSION >= '1.9'
40
- @body_raw = Mail.encoding_to_charset(@body_raw, @charset)
41
- else
42
- @body_raw = NKF.nkf(NKF_OPTIONS, @body_raw)
43
- end
44
- end
45
- process_body_raw_without_iso_2022_jp_encoding
46
- end
47
- alias_method :process_body_raw_without_iso_2022_jp_encoding, :process_body_raw
48
- alias_method :process_body_raw, :process_body_raw_with_iso_2022_jp_encoding
49
-
50
- def text_part_with_iso_2022_jp_encoding=(msg = nil)
51
- if @charset.to_s.downcase == 'iso-2022-jp' && msg && msg.charset.nil?
52
- msg.charset = @charset
53
- end
54
- self.text_part_without_iso_2022_jp_encoding = msg
55
- end
56
- alias_method :text_part_without_iso_2022_jp_encoding=, :text_part=
57
- alias_method :text_part=, :text_part_with_iso_2022_jp_encoding=
58
- end
59
-
60
- class Header
61
- def encoded
62
- buffer = ''
63
- fields.each do |field|
64
- buffer << field.encoded rescue Encoding::CompatibilityError;
65
- end
66
- buffer
67
- end
68
- end
69
-
70
- class Body
71
- def initialize_with_iso_2022_jp_encoding(string = '')
72
- if string.respond_to?(:encoding) && string.encoding.to_s == 'ISO-2022-JP'
73
- string.force_encoding('US-ASCII')
74
- end
75
- initialize_without_iso_2022_jp_encoding(string)
76
- end
77
- alias_method :initialize_without_iso_2022_jp_encoding, :initialize
78
- alias_method :initialize, :initialize_with_iso_2022_jp_encoding
79
- end
80
-
81
- class Field
82
- def initialize_with_iso_2022_jp_encoding(name, value = nil, charset = 'utf-8')
83
- if charset == 'ISO-2022-JP' && value.kind_of?(String)
84
- if RUBY_VERSION >= '1.9'
85
- unless [ 'UTF-8', 'US-ASCII' ].include?(value.encoding.to_s)
86
- raise ::Mail::InvalidEncodingError.new(
87
- "The '#{name}' field is not encoded in UTF-8 nor in US-ASCII but in #{value.encoding}")
88
- end
89
- end
90
- end
91
- initialize_without_iso_2022_jp_encoding(name, value, charset)
92
- end
93
- alias_method :initialize_without_iso_2022_jp_encoding, :initialize
94
- alias_method :initialize, :initialize_with_iso_2022_jp_encoding
95
- end
96
-
97
- module FieldWithIso2022JpEncoding
98
- def self.included(base)
99
- base.send :alias_method, :initialize_without_iso_2022_jp_encoding, :initialize
100
- base.send :alias_method, :initialize, :initialize_with_iso_2022_jp_encoding
101
- base.send :alias_method, :do_decode_without_iso_2022_jp_encoding, :do_decode
102
- base.send :alias_method, :do_decode, :do_decode_with_iso_2022_jp_encoding
103
- end
104
-
105
- def initialize_with_iso_2022_jp_encoding(value = nil, charset = 'utf-8')
106
- if charset.to_s.downcase == 'iso-2022-jp'
107
- if value.kind_of?(Array)
108
- value = value.map { |e| encode_with_iso_2022_jp(e, charset) }
109
- else
110
- value = encode_with_iso_2022_jp(value, charset)
111
- end
112
- end
113
- initialize_without_iso_2022_jp_encoding(value, charset)
114
- end
115
-
116
- private
117
- def do_decode_with_iso_2022_jp_encoding
118
- if charset.to_s.downcase == 'iso-2022-jp'
119
- value
120
- else
121
- do_decode_without_iso_2022_jp_encoding
122
- end
123
- end
124
-
125
- def encode_with_iso_2022_jp(value, charset)
126
- value = value.to_s.gsub(/#{WAVE_DASH}/, FULLWIDTH_TILDE)
127
- if RUBY_VERSION >= '1.9'
128
- value = Mail.encoding_to_charset(value, charset)
129
- value.force_encoding('ascii-8bit')
130
- value = b_value_encode(value)
131
- value.force_encoding('ascii-8bit')
132
- else
133
- value = NKF.nkf(NKF_OPTIONS, value)
134
- b_value_encode(value)
135
- end
136
- end
137
-
138
- def b_value_encode(string)
139
- string.split(' ').map do |s|
140
- if s =~ /\e/
141
- encode64(s)
142
- else
143
- s
144
- end
145
- end.join(" ")
146
- end
147
-
148
- private
149
- def encode(value)
150
- if charset.to_s.downcase == 'iso-2022-jp'
151
- value
152
- else
153
- super(value)
154
- end
155
- end
156
-
157
- def encode_crlf(value)
158
- if RUBY_VERSION >= '1.9' && charset.to_s.downcase == 'iso-2022-jp'
159
- value.force_encoding('ascii-8bit')
160
- end
161
- super(value)
162
- end
163
-
164
- def encode64(string)
165
- "=?ISO-2022-JP?B?#{Base64.encode64(string).gsub("\n", "")}?="
166
- end
167
- end
168
-
169
- class SubjectField < UnstructuredField
170
- include FieldWithIso2022JpEncoding
171
- def b_value_encode(string)
172
- encode64(string)
173
- end
174
- end
175
-
176
- class FromField < StructuredField
177
- include FieldWithIso2022JpEncoding
178
- end
179
-
180
- class SenderField < StructuredField
181
- include FieldWithIso2022JpEncoding
182
- end
183
-
184
- class ToField < StructuredField
185
- include FieldWithIso2022JpEncoding
186
- end
187
-
188
- class CcField < StructuredField
189
- include FieldWithIso2022JpEncoding
190
- end
191
-
192
- class ReplyToField < StructuredField
193
- include FieldWithIso2022JpEncoding
194
- end
195
-
196
- class ResentFromField < StructuredField
197
- include FieldWithIso2022JpEncoding
198
- end
199
-
200
- class ResentSenderField < StructuredField
201
- include FieldWithIso2022JpEncoding
202
- end
203
-
204
- class ResentToField < StructuredField
205
- include FieldWithIso2022JpEncoding
206
- end
207
-
208
- class ResentCcField < StructuredField
209
- include FieldWithIso2022JpEncoding
210
- end
211
- end