mail-jdec 1.1.2 → 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: 4b79e3c592167bea0b81f1f92959a728b253f27b9a4ac84cbfca9c24af70bf44
4
- data.tar.gz: 5c2205ef8533ade4e48367f0b72cea2a7c39b41e470aa6925bda3122cf8da0b6
3
+ metadata.gz: 930bf110db56ac82ae59462cb3fb161220eea62a665702ea2b7b57ca3510db32
4
+ data.tar.gz: a9558c10bc88225de7539146d358274ee068d30c1f25eeede12e6ceab5c0ad13
5
5
  SHA512:
6
- metadata.gz: a900a989980cd0d098dd5376ddd0c58f02e51b8b781194978d34e6b5febaf4b144ce0c0a2dea4269dad77d27c870780ad901e4e4d9c205d70e806dd525716571
7
- data.tar.gz: 200f37329d5a89d19a8690f13f07dac5eb96d32aba0ee5a4e4f893c75c5ff7dcdfdb3a0b58fcf4cea2077ae83c1dcc4d991861592a3ff10fb580499f8b3e6da9
6
+ metadata.gz: 7e3e301ac8d7aae79297a96c857fd766472e2734872e90347429f78b6cab1026ba3846e46841f016e9564d4917db6c1a24ff03b6912c97d322354bb69d6359a6
7
+ data.tar.gz: 7fad9f6987f65bed5821e5e83bb53381138246058a3157583ad24de9263a93a105781543bf32546c62fce2f86ae3e20634aca451bd7ccb95e371b7176d087911
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
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
+
3
8
  ## 1.1.2
4
9
 
5
10
  * Remove tab characters in references header.
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
@@ -15,6 +15,4 @@ module Mail
15
15
  end
16
16
  end
17
17
 
18
- unless Mail::MessageIdsElement.included_modules.include?(Mail::Jdec::MessageIdsElementPatch)
19
- Mail::MessageIdsElement.prepend Mail::Jdec::MessageIdsElementPatch
20
- end
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.2'
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.2
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-06-23 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,12 +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
107
108
  - lib/mail/jdec/elements/message_ids_element_patch.rb
108
109
  - lib/mail/jdec/encodings_patch.rb
110
+ - lib/mail/jdec/field_list_patch.rb
109
111
  - lib/mail/jdec/fields/common_address_field_patch.rb
110
112
  - lib/mail/jdec/fields/common_date_field_patch.rb
111
113
  - lib/mail/jdec/fields/parameter_hash_patch.rb
@@ -118,7 +120,7 @@ files:
118
120
  homepage: https://github.com/kanety/mail-jdec
119
121
  licenses: []
120
122
  metadata: {}
121
- post_install_message:
123
+ post_install_message:
122
124
  rdoc_options: []
123
125
  require_paths:
124
126
  - lib
@@ -134,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
134
136
  version: '0'
135
137
  requirements: []
136
138
  rubygems_version: 3.3.3
137
- signing_key:
139
+ signing_key:
138
140
  specification_version: 4
139
141
  summary: A mail patch for decoding some improper mails
140
142
  test_files: []