mail-jdec 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f4cba29f251d5a8fe9a696f6e965b9fd2b52c622f64aedb23f63567e3ebc15d0
4
- data.tar.gz: ae6a610eee21b581f474fedf651a90e54bc9e5dde5d613daeebd621c0520da86
3
+ metadata.gz: 930bf110db56ac82ae59462cb3fb161220eea62a665702ea2b7b57ca3510db32
4
+ data.tar.gz: a9558c10bc88225de7539146d358274ee068d30c1f25eeede12e6ceab5c0ad13
5
5
  SHA512:
6
- metadata.gz: c5a8e19b16e943c41890c0652433f1607b15c08108440dbbe0fbe4ca6e3360f3aed4d43328e46aa4230b390bac46b9566db890b123f675444972054231af7514
7
- data.tar.gz: 9cd358e45c80af8aab96e8d995d16720028ec657c224a0faeb07ba2eec12b39be15b76f2c778524f4a420b499c7649eea13e751eeefdf55deb53dbf083f1549b
6
+ metadata.gz: 7e3e301ac8d7aae79297a96c857fd766472e2734872e90347429f78b6cab1026ba3846e46841f016e9564d4917db6c1a24ff03b6912c97d322354bb69d6359a6
7
+ data.tar.gz: 7fad9f6987f65bed5821e5e83bb53381138246058a3157583ad24de9263a93a105781543bf32546c62fce2f86ae3e20634aca451bd7ccb95e371b7176d087911
data/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 1.2.0
4
+
5
+ * Add current config which enables specified configs only in a block.
6
+ * Add `keep_field_order` config.
7
+
8
+ ## 1.1.2
9
+
10
+ * Remove tab characters in references header.
11
+
3
12
  ## 1.1.1
4
13
 
5
14
  * Support charset detection including message parts.
data/README.md CHANGED
@@ -26,10 +26,10 @@ Use mail as usual. You can enable/disable patched features as follows:
26
26
 
27
27
  ```ruby
28
28
  # disable patch
29
- Mail::Jdec.disable
29
+ Mail::Jdec.disable!
30
30
 
31
31
  # enable patch
32
- Mail::Jdec.enable
32
+ Mail::Jdec.enable!
33
33
  ```
34
34
 
35
35
  ## Contributing
@@ -16,6 +16,4 @@ module Mail
16
16
  end
17
17
  end
18
18
 
19
- unless Mail::Body.included_modules.include?(Mail::Jdec::BodyPatch)
20
- Mail::Body.prepend Mail::Jdec::BodyPatch
21
- end
19
+ Mail::Body.prepend Mail::Jdec::BodyPatch
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mail
4
+ module Jdec
5
+ class Config
6
+ NAMES = [
7
+ :enabled,
8
+ :autodetect_confidence, :autodetect_skip_charsets, :mime_types_for_autodetect,
9
+ :preferred_charsets, :keep_field_order
10
+ ]
11
+ NAMES.each do |name|
12
+ attr_accessor name
13
+ end
14
+
15
+ def initialize(attrs = {})
16
+ attrs.each do |key, val|
17
+ send("#{key}=", val)
18
+ end
19
+ end
20
+
21
+ def attributes
22
+ NAMES.each_with_object({}) do |name, hash|
23
+ hash[name] = send(name)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -22,8 +22,8 @@ module Mail
22
22
  def trusty?(detected)
23
23
  detected.key?(:type) &&
24
24
  detected.key?(:encoding) &&
25
- detected[:confidence].to_i >= Jdec.autodetect_confidence &&
26
- !Jdec.autodetect_skip_charsets.include?(detected[:encoding].downcase)
25
+ detected[:confidence].to_i >= Jdec.config.autodetect_confidence &&
26
+ !Jdec.config.autodetect_skip_charsets.include?(detected[:encoding].downcase)
27
27
  end
28
28
  end
29
29
  end
@@ -38,6 +38,4 @@ module Mail
38
38
  end
39
39
  end
40
40
 
41
- unless Mail::ContentDispositionElement.included_modules.include?(Mail::Jdec::ContentDispositionElementPatch)
42
- Mail::ContentDispositionElement.prepend Mail::Jdec::ContentDispositionElementPatch
43
- end
41
+ Mail::ContentDispositionElement.prepend Mail::Jdec::ContentDispositionElementPatch
@@ -33,6 +33,4 @@ module Mail
33
33
  end
34
34
  end
35
35
 
36
- unless Mail::ContentTypeElement.included_modules.include?(Mail::Jdec::ContentTypeElementPatch)
37
- Mail::ContentTypeElement.prepend Mail::Jdec::ContentTypeElementPatch
38
- end
36
+ Mail::ContentTypeElement.prepend Mail::Jdec::ContentTypeElementPatch
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mail
4
+ module Jdec
5
+ module MessageIdsElementPatch
6
+ private
7
+
8
+ def parse(string)
9
+ if Jdec.enabled? && string.is_a?(String)
10
+ string = string.gsub(/\t/, ' ')
11
+ end
12
+ super
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ Mail::MessageIdsElement.prepend Mail::Jdec::MessageIdsElementPatch
@@ -14,6 +14,4 @@ module Mail
14
14
  end
15
15
  end
16
16
 
17
- unless Mail::Encodings.singleton_class.included_modules.include?(Mail::Jdec::EncodingsPatch)
18
- Mail::Encodings.singleton_class.prepend Mail::Jdec::EncodingsPatch
19
- end
17
+ Mail::Encodings.singleton_class.prepend Mail::Jdec::EncodingsPatch
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mail
4
+ module Jdec
5
+ module FieldListPatch
6
+ def insert_field(field)
7
+ if Mail::Jdec.enabled? && Mail::Jdec.config.keep_field_order
8
+ push field
9
+ else
10
+ super
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ Mail::FieldList.prepend Mail::Jdec::FieldListPatch
@@ -27,7 +27,5 @@ end
27
27
 
28
28
  klasses = ObjectSpace.each_object(Class).select { |klass| klass < Mail::CommonAddressField }
29
29
  klasses.each do |klass|
30
- unless klass.included_modules.include?(Mail::Jdec::CommonAddressFieldPatch)
31
- klass.prepend Mail::Jdec::CommonAddressFieldPatch
32
- end
30
+ klass.prepend Mail::Jdec::CommonAddressFieldPatch
33
31
  end
@@ -40,7 +40,5 @@ end
40
40
 
41
41
  klasses = ObjectSpace.each_object(Class).select { |klass| klass < Mail::CommonDateField }
42
42
  klasses.each do |klass|
43
- unless klass.included_modules.include?(Mail::Jdec::CommonDateFieldPatch)
44
- klass.prepend Mail::Jdec::CommonDateFieldPatch
45
- end
43
+ klass.prepend Mail::Jdec::CommonDateFieldPatch
46
44
  end
@@ -11,6 +11,4 @@ module Mail
11
11
  end
12
12
  end
13
13
 
14
- unless Mail::StructuredField.included_modules.include?(Mail::Jdec::StructuredFieldPatch)
15
- Mail::StructuredField.prepend Mail::Jdec::StructuredFieldPatch
16
- end
14
+ Mail::StructuredField.prepend Mail::Jdec::StructuredFieldPatch
@@ -11,6 +11,4 @@ module Mail
11
11
  end
12
12
  end
13
13
 
14
- unless Mail::UnstructuredField.included_modules.include?(Mail::Jdec::UnstructuredFieldPatch)
15
- Mail::UnstructuredField.prepend Mail::Jdec::UnstructuredFieldPatch
16
- end
14
+ Mail::UnstructuredField.prepend Mail::Jdec::UnstructuredFieldPatch
@@ -28,7 +28,7 @@ module Mail
28
28
  end
29
29
 
30
30
  def mime_types_for_autodetect?(mime_type)
31
- Jdec.mime_types_for_autodetect.any? do |type|
31
+ Jdec.config.mime_types_for_autodetect.any? do |type|
32
32
  if type.is_a?(Regexp)
33
33
  type.match?(mime_type)
34
34
  else
@@ -41,6 +41,4 @@ module Mail
41
41
  end
42
42
  end
43
43
 
44
- unless Mail::Message.included_modules.include?(Mail::Jdec::MessagePatch)
45
- Mail::Message.prepend Mail::Jdec::MessagePatch
46
- end
44
+ Mail::Message.prepend Mail::Jdec::MessagePatch
@@ -5,7 +5,7 @@ module Mail
5
5
  module UtilitiesPatch
6
6
  def pick_encoding(charset)
7
7
  if Jdec.enabled?
8
- Jdec.preferred_charsets.each do |from, to|
8
+ Jdec.config.preferred_charsets.each do |from, to|
9
9
  if charset.to_s.downcase == from
10
10
  return Encoding.find(to)
11
11
  end
@@ -35,6 +35,4 @@ module Mail
35
35
  end
36
36
  end
37
37
 
38
- unless Mail::Utilities.singleton_class.included_modules.include?(Mail::Jdec::UtilitiesPatch)
39
- Mail::Utilities.singleton_class.prepend Mail::Jdec::UtilitiesPatch
40
- end
38
+ Mail::Utilities.singleton_class.prepend Mail::Jdec::UtilitiesPatch
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Mail
4
4
  module Jdec
5
- VERSION = '1.1.1'
5
+ VERSION = '1.2.0'
6
6
  end
7
7
  end
data/lib/mail/jdec.rb CHANGED
@@ -10,39 +10,54 @@ end
10
10
  module Mail
11
11
  module Jdec
12
12
  class << self
13
- attr_reader :enabled
14
- attr_accessor :autodetect_confidence
15
- attr_accessor :autodetect_skip_charsets
16
- attr_accessor :mime_types_for_autodetect
17
- attr_accessor :preferred_charsets
13
+ @@config = Config.new(
14
+ enabled: true,
15
+ autodetect_confidence: 50,
16
+ autodetect_skip_charsets: %w(),
17
+ mime_types_for_autodetect: [%r{^text/}, 'message/delivery-status', 'message/disposition-notification'],
18
+ preferred_charsets: {
19
+ 'iso-2022-jp' => 'cp50221',
20
+ 'iso-2022-jp-1' => 'cp50221',
21
+ 'iso-2022-jp-2' => 'cp50221',
22
+ 'iso-2022-jp-3' => 'cp50221',
23
+ 'iso-2022-jp-2004' => 'cp50221',
24
+ 'shift_jis' => 'cp932',
25
+ 'shift-jis' => 'cp932',
26
+ 'x_sjis' => 'cp932',
27
+ 'x-sjis' => 'cp932'
28
+ },
29
+ keep_field_order: false
30
+ )
31
+
32
+ def configure
33
+ yield @@config
34
+ end
35
+
36
+ THREAD_KEY = :_mail_jdec
37
+
38
+ def config
39
+ Thread.current[THREAD_KEY] || @@config
40
+ end
41
+
42
+ def with_config(hash = {})
43
+ old = Thread.current[THREAD_KEY]
44
+ Thread.current[THREAD_KEY] = Config.new(config.attributes.merge(hash))
45
+ yield
46
+ ensure
47
+ Thread.current[THREAD_KEY] = old
48
+ end
18
49
 
19
50
  def enabled?
20
- @@enabled
51
+ config.enabled
21
52
  end
22
53
 
23
- def enable
24
- @@enabled = true
54
+ def enable!
55
+ config.enabled = true
25
56
  end
26
57
 
27
- def disable
28
- @@enabled = false
58
+ def disable!
59
+ config.enabled = false
29
60
  end
30
61
  end
31
-
32
- self.enable
33
- self.autodetect_confidence = 50
34
- self.autodetect_skip_charsets = %w()
35
- self.mime_types_for_autodetect = [%r{^text/}, 'message/delivery-status', 'message/disposition-notification']
36
- self.preferred_charsets = {
37
- 'iso-2022-jp' => 'cp50221',
38
- 'iso-2022-jp-1' => 'cp50221',
39
- 'iso-2022-jp-2' => 'cp50221',
40
- 'iso-2022-jp-3' => 'cp50221',
41
- 'iso-2022-jp-2004' => 'cp50221',
42
- 'shift_jis' => 'cp932',
43
- 'shift-jis' => 'cp932',
44
- 'x_sjis' => 'cp932',
45
- 'x-sjis' => 'cp932'
46
- }
47
62
  end
48
63
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mail-jdec
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yoshikazu Kaneta
8
- autorequire:
8
+ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-05-24 00:00:00.000000000 Z
11
+ date: 2023-10-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail
@@ -100,11 +100,14 @@ files:
100
100
  - gemfiles/mail28.gemfile
101
101
  - lib/mail/jdec.rb
102
102
  - lib/mail/jdec/body_patch.rb
103
+ - lib/mail/jdec/config.rb
103
104
  - lib/mail/jdec/decoder.rb
104
105
  - lib/mail/jdec/detector.rb
105
106
  - lib/mail/jdec/elements/content_disposition_element_patch.rb
106
107
  - lib/mail/jdec/elements/content_type_element_patch.rb
108
+ - lib/mail/jdec/elements/message_ids_element_patch.rb
107
109
  - lib/mail/jdec/encodings_patch.rb
110
+ - lib/mail/jdec/field_list_patch.rb
108
111
  - lib/mail/jdec/fields/common_address_field_patch.rb
109
112
  - lib/mail/jdec/fields/common_date_field_patch.rb
110
113
  - lib/mail/jdec/fields/parameter_hash_patch.rb
@@ -117,7 +120,7 @@ files:
117
120
  homepage: https://github.com/kanety/mail-jdec
118
121
  licenses: []
119
122
  metadata: {}
120
- post_install_message:
123
+ post_install_message:
121
124
  rdoc_options: []
122
125
  require_paths:
123
126
  - lib
@@ -133,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
133
136
  version: '0'
134
137
  requirements: []
135
138
  rubygems_version: 3.3.3
136
- signing_key:
139
+ signing_key:
137
140
  specification_version: 4
138
141
  summary: A mail patch for decoding some improper mails
139
142
  test_files: []