mail-jdec 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 925ecb5ca03bce312fbffc21b30f4385cca1efd55461afcce02a7a8ccd46bdd2
4
+ data.tar.gz: 9a8fa9a47b6370d6e289fe53dbb14a053791d14d4fa5ea68160e7448678ed545
5
+ SHA512:
6
+ metadata.gz: 2458f803b38b3ae52ab5b82032c38e2d9c15cfba810eb44df9a320b3ae782463afabba5e2f5029543c2d3cd9c4fc90522db664804de945f96d4edbf1bfdde49e
7
+ data.tar.gz: 8c6e60a7e79a00bbb90c602c44224cff6595afa1b5f13c5df79f93489ae4639b8df9ff2635fc9fec00b8fb2e9764070865d8bcf6059df5cd41009a9cedca8efe
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ .bundle/
2
+ .project
3
+ Gemfile.lock
4
+ coverage/
5
+ pkg/
6
+ tmp/
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.3
4
+ - 2.4
5
+ - 2.5
6
+ - 2.6
7
+ gemfile:
8
+ - gemfiles/mail27.gemfile
9
+ script:
10
+ - bundle exec rspec
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Yoshikazu Kaneta
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # Mail::Jdec
2
+
3
+ A mail patch for decoding some inproper mails.
4
+
5
+ ## Dependencies
6
+
7
+ * ruby 2.3+
8
+ * mail 2.7
9
+ * charlock_holmes (using libicu)
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'mail-jdec'
17
+ ```
18
+
19
+ Then execute:
20
+
21
+ $ bundle
22
+
23
+ ## Usage
24
+
25
+ Use mail as usual. You can enable/disable patched features as follows:
26
+
27
+ ```ruby
28
+ # disable patch
29
+ Mail::Jdec.disable
30
+
31
+ # enable patch
32
+ Mail::Jdec.enable
33
+ ```
34
+
35
+ ## Contributing
36
+
37
+ Pull requests are welcome at https://github.com/kanety/mail-jdec.
38
+
39
+ ## License
40
+
41
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mail/jdec"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem "mail", "~> 2.7.1"
4
+
5
+ gemspec path: "../"
@@ -0,0 +1,60 @@
1
+ module Mail
2
+ module Jdec
3
+ class Decoder
4
+ class << self
5
+ def decode_if_needed(text)
6
+ return text if text.nil? || !text.ascii_only?
7
+
8
+ detected = Detector.detect(text)
9
+ if detected
10
+ charset = detected[:encoding].to_s
11
+ text = Mail::Encodings.transcode_charset(text.dup.force_encoding(charset), charset)
12
+ end
13
+
14
+ text
15
+ end
16
+
17
+ def force_utf8(str)
18
+ str.dup.force_encoding('utf-8').encode('utf-8', invalid: :replace, undef: :replace)
19
+ end
20
+ end
21
+ end
22
+
23
+ class ValueDecoder
24
+ class << self
25
+ include Mail::Constants
26
+
27
+ def value_decode(str)
28
+ # Optimization: If there's no encoded-words in the string, just return it
29
+ return str unless str =~ ENCODED_VALUE
30
+
31
+ lines = Mail::Encodings.send(:collapse_adjacent_encodings, str)
32
+
33
+ lines = lines.chunk do |value|
34
+ if value =~ ENCODED_VALUE
35
+ $1
36
+ else
37
+ ''
38
+ end
39
+ end
40
+
41
+ # Split on white-space boundaries with capture, so we capture the white-space as well
42
+ lines.map do |charset, values|
43
+ content = values.join
44
+ if content =~ ENCODED_VALUE
45
+ bytes = content.scan(/\=\?([^?]+)\?([QB])\?([^?]*?)\?\=/mi).map do |_, encoding, encoded|
46
+ case encoding
47
+ when *B_VALUES then Mail::RubyVer.decode_base64(encoded)
48
+ when *Q_VALUES then Mail::Encodings::QuotedPrintable.decode(encoded)
49
+ end
50
+ end.join('')
51
+ Mail::Encodings.transcode_charset(bytes, charset)
52
+ else
53
+ content
54
+ end
55
+ end.join('')
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,29 @@
1
+ module Mail
2
+ module Jdec
3
+ class Detector
4
+ class << self
5
+ def detect(text)
6
+ return if text.nil?
7
+
8
+ detected = CharlockHolmes::EncodingDetector.detect(text)
9
+ detected if trusty?(detected) && find_encoding(detected)
10
+ end
11
+
12
+ private
13
+
14
+ def find_encoding(detected)
15
+ Encoding.find(detected[:encoding])
16
+ rescue
17
+ nil
18
+ end
19
+
20
+ def trusty?(detected)
21
+ detected.key?(:type) &&
22
+ detected.key?(:encoding) &&
23
+ detected[:confidence].to_i > Jdec.autodetect_confidence &&
24
+ !Jdec.autodetect_skip_charsets.include?(detected[:encoding].downcase)
25
+ end
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,25 @@
1
+ module Mail
2
+ module Jdec
3
+ module ContentDispositionElementPatch
4
+ def initialize(string)
5
+ super
6
+ rescue Mail::Field::ParseError => e
7
+ if Jdec.enabled?
8
+ value = Jdec::Decoder.force_utf8(string)
9
+ if (matched = value.match(/^\s*attachment\s*;\s*(.+)/im))
10
+ @disposition_type = 'attachment'
11
+ @parameters = [{ filename: matched[1].gsub(/(\r\n|\r|\n)\s/, '') }]
12
+ else
13
+ raise e
14
+ end
15
+ else
16
+ raise e
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ unless Mail::ContentDispositionElement.included_modules.include?(Mail::Jdec::ContentDispositionElementPatch)
24
+ Mail::ContentDispositionElement.prepend Mail::Jdec::ContentDispositionElementPatch
25
+ end
@@ -0,0 +1,18 @@
1
+ module Mail
2
+ module Jdec
3
+ module ContentTypeElementPatch
4
+ def initialize(string)
5
+ if Jdec.enabled?
6
+ # Handles ; name=
7
+ string = string.gsub(/^\s*;?\s*name=(.+)$/im) { "application/octet-stream; name=#{$1}" }
8
+ end
9
+
10
+ super
11
+ end
12
+ end
13
+ end
14
+ end
15
+
16
+ unless Mail::ContentTypeElement.included_modules.include?(Mail::Jdec::ContentTypeElementPatch)
17
+ Mail::ContentTypeElement.prepend Mail::Jdec::ContentTypeElementPatch
18
+ end
@@ -0,0 +1,17 @@
1
+ module Mail
2
+ module Jdec
3
+ module EncodingsPatch
4
+ def value_decode(str)
5
+ if Jdec.enabled?
6
+ Jdec::ValueDecoder.value_decode(str)
7
+ else
8
+ super
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
14
+
15
+ unless Mail::Encodings.singleton_class.included_modules.include?(Mail::Jdec::EncodingsPatch)
16
+ Mail::Encodings.singleton_class.prepend Mail::Jdec::EncodingsPatch
17
+ end
@@ -0,0 +1,14 @@
1
+ module Mail
2
+ module Jdec
3
+ module StructuredFieldPatch
4
+ def initialize(name = nil, value = nil, charset = nil)
5
+ value = Decoder.decode_if_needed(value) if Jdec.enabled?
6
+ super
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ unless Mail::StructuredField.included_modules.include?(Mail::Jdec::StructuredFieldPatch)
13
+ Mail::StructuredField.prepend Mail::Jdec::StructuredFieldPatch
14
+ end
@@ -0,0 +1,14 @@
1
+ module Mail
2
+ module Jdec
3
+ module UnstructuredFieldPatch
4
+ def initialize(name = nil, value = nil, charset = nil)
5
+ value = Decoder.decode_if_needed(value) if Jdec.enabled?
6
+ super
7
+ end
8
+ end
9
+ end
10
+ end
11
+
12
+ unless Mail::UnstructuredField.included_modules.include?(Mail::Jdec::UnstructuredFieldPatch)
13
+ Mail::UnstructuredField.prepend Mail::Jdec::UnstructuredFieldPatch
14
+ end
@@ -0,0 +1,25 @@
1
+ module Mail
2
+ module Jdec
3
+ module MessagePatch
4
+ def decoded
5
+ decoded = super
6
+
7
+ if Jdec.enabled? && (!has_content_type? || (!has_charset? && !attachment? && !multipart?))
8
+ detected = Detector.detect(decoded)
9
+ if detected && detected[:type] == :text
10
+ charset = detected[:encoding].downcase
11
+ decoded = Mail::Encodings.transcode_charset(decoded.dup.force_encoding(charset), charset)
12
+ header[:content_type] = 'text/plain'
13
+ header[:content_type].parameters[:charset] = charset
14
+ end
15
+ end
16
+
17
+ decoded
18
+ end
19
+ end
20
+ end
21
+ end
22
+
23
+ unless Mail::Message.included_modules.include?(Mail::Jdec::MessagePatch)
24
+ Mail::Message.prepend Mail::Jdec::MessagePatch
25
+ end
@@ -0,0 +1,20 @@
1
+ module Mail
2
+ module Jdec
3
+ module Ruby19Patch
4
+ def pick_encoding(charset)
5
+ if Jdec.enabled?
6
+ Jdec.preferred_charsets.each do |from, to|
7
+ if charset.to_s.downcase == from
8
+ return Encoding.find(to)
9
+ end
10
+ end
11
+ end
12
+ super
13
+ end
14
+ end
15
+ end
16
+ end
17
+
18
+ unless Mail::Ruby19.singleton_class.included_modules.include?(Mail::Jdec::Ruby19Patch)
19
+ Mail::Ruby19.singleton_class.prepend Mail::Jdec::Ruby19Patch
20
+ end
@@ -0,0 +1,5 @@
1
+ module Mail
2
+ module Jdec
3
+ VERSION = '1.0.0'
4
+ end
5
+ end
data/lib/mail/jdec.rb ADDED
@@ -0,0 +1,44 @@
1
+ require 'mail'
2
+ require 'charlock_holmes'
3
+
4
+ Dir["#{__dir__}/jdec/**/*.rb"].each do |file|
5
+ require file
6
+ end
7
+
8
+ module Mail
9
+ module Jdec
10
+ class << self
11
+ attr_reader :enabled
12
+ attr_accessor :autodetect_confidence
13
+ attr_accessor :autodetect_skip_charsets
14
+ attr_accessor :preferred_charsets
15
+
16
+ def enabled?
17
+ @@enabled
18
+ end
19
+
20
+ def enable
21
+ @@enabled = true
22
+ end
23
+
24
+ def disable
25
+ @@enabled = false
26
+ end
27
+ end
28
+
29
+ self.enable
30
+ self.autodetect_confidence = 50
31
+ self.autodetect_skip_charsets = %w(utf-8 iso-8859-1)
32
+ self.preferred_charsets = {
33
+ 'iso-2022-jp' => 'cp50221',
34
+ 'iso-2022-jp-1' => 'cp50221',
35
+ 'iso-2022-jp-2' => 'cp50221',
36
+ 'iso-2022-jp-3' => 'cp50221',
37
+ 'iso-2022-jp-2004' => 'cp50221',
38
+ 'shift_jis' => 'cp932',
39
+ 'shift-jis' => 'cp932',
40
+ 'x_sjis' => 'cp932',
41
+ 'x-sjis' => 'cp932'
42
+ }
43
+ end
44
+ end
data/mail-jdec.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mail/jdec/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mail-jdec"
8
+ spec.version = Mail::Jdec::VERSION
9
+ spec.authors = ["Yoshikazu Kaneta"]
10
+ spec.email = ["kaneta@sitebridge.co.jp"]
11
+ spec.summary = %q{A mail patch for decoding some inproper mails}
12
+ spec.description = %q{A mail patch for decoding some inproper mails}
13
+ spec.homepage = "https://github.com/kanety/mail-jdec"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "mail", ">= 2.7.1"
21
+ spec.add_dependency "charlock_holmes", ">= 0.7.0"
22
+
23
+ spec.add_development_dependency "rake"
24
+ spec.add_development_dependency "rspec"
25
+ spec.add_development_dependency "simplecov"
26
+ end
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail-jdec
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Yoshikazu Kaneta
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.1
27
+ - !ruby/object:Gem::Dependency
28
+ name: charlock_holmes
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 0.7.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 0.7.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: simplecov
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A mail patch for decoding some inproper mails
84
+ email:
85
+ - kaneta@sitebridge.co.jp
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - LICENSE
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - gemfiles/mail27.gemfile
100
+ - lib/mail/jdec.rb
101
+ - lib/mail/jdec/decoder.rb
102
+ - lib/mail/jdec/detector.rb
103
+ - lib/mail/jdec/elements/content_disposition_element_patch.rb
104
+ - lib/mail/jdec/elements/content_type_element_patch.rb
105
+ - lib/mail/jdec/encodings_patch.rb
106
+ - lib/mail/jdec/fields/structured_field_patch.rb
107
+ - lib/mail/jdec/fields/unstructured_field_patch.rb
108
+ - lib/mail/jdec/message_patch.rb
109
+ - lib/mail/jdec/ruby_1_9_patch.rb
110
+ - lib/mail/jdec/version.rb
111
+ - mail-jdec.gemspec
112
+ homepage: https://github.com/kanety/mail-jdec
113
+ licenses: []
114
+ metadata: {}
115
+ post_install_message:
116
+ rdoc_options: []
117
+ require_paths:
118
+ - lib
119
+ required_ruby_version: !ruby/object:Gem::Requirement
120
+ requirements:
121
+ - - ">="
122
+ - !ruby/object:Gem::Version
123
+ version: '0'
124
+ required_rubygems_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ requirements: []
130
+ rubygems_version: 3.0.1
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: A mail patch for decoding some inproper mails
134
+ test_files: []