email-fetch-and-process 0.1.0
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.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.rubocop.yml +18 -0
- data/.travis.yml +7 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +21 -0
- data/README.md +41 -0
- data/Rakefile +12 -0
- data/bin/setup +8 -0
- data/email-fetch-and-process.gemspec +30 -0
- data/lib/email-fetch-and-process.rb +138 -0
- data/lib/email-fetch-and-process/version.rb +5 -0
- metadata +98 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: bd4238e5ce660f30d31bc897a198999950931fe26bdc70d0422bc352bf0f8bf6
|
4
|
+
data.tar.gz: d4f266e248827632948a51d044abad845b9c148d10c7d284bb693947e40d6def
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 2ee4d00950c8672c0d89e0312212beb48c8207515a32d3905a935bd907874c726738507a9ca2f1079819317dc60319c6b2b84e0fcdc430f9e700d8afbf579057
|
7
|
+
data.tar.gz: a7ab8cbed33ea683f9acdf10776efda4256818e655c010c90bf766b707f966405b07d8ae354f8a7b4cf8edc8c33a6ca9dbd8b9474d3e408a481f9649f3c170b9
|
data/.gitignore
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
AllCops:
|
2
|
+
TargetRubyVersion: 2.6
|
3
|
+
Metrics/LineLength:
|
4
|
+
Enabled: false
|
5
|
+
Metrics/MethodLength:
|
6
|
+
Enabled: false
|
7
|
+
Metrics/AbcSize:
|
8
|
+
Enabled: false
|
9
|
+
Style/RescueModifier:
|
10
|
+
Enabled: false
|
11
|
+
Metrics/BlockLength:
|
12
|
+
Enabled: false
|
13
|
+
Metrics/PerceivedComplexity:
|
14
|
+
Enabled: false
|
15
|
+
Metrics/CyclomaticComplexity:
|
16
|
+
Enabled: false
|
17
|
+
Naming/FileName:
|
18
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Kirk Haines
|
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
|
+
# EmailFetchAndProcess
|
2
|
+
|
3
|
+
This little gem encapsulates some logic distilled and extracted from a bunch of different scripts used to access a mailbox, find an email, and get the file attachment from it.
|
4
|
+
|
5
|
+
It is currently very focused and limited to that job and that job only, but with a little TLC it could become a more general email access/extract/process tool.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'email-fetch-and-process'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install email-fetch-and-process
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
```
|
26
|
+
require 'email-fetch-and-process'
|
27
|
+
|
28
|
+
jobs = []
|
29
|
+
jobs << EmailFetchAndProcess::Job.new({fetch: ['SUBJECT', 'Some subject line']})
|
30
|
+
|
31
|
+
r = EmailFetchAndProcess.new({host: 'imap.gmail.com',port: 993, id: 'YOURID', password: 'YOURPASSWORD'})
|
32
|
+
r.run(jobs)
|
33
|
+
```
|
34
|
+
|
35
|
+
## Contributing
|
36
|
+
|
37
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/wyhaines/email-fetch-and-process.
|
38
|
+
|
39
|
+
## License
|
40
|
+
|
41
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'email-fetch-and-process/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'email-fetch-and-process'
|
9
|
+
spec.version = EmailFetchAndProcess::VERSION
|
10
|
+
spec.authors = ['Kirk Haines']
|
11
|
+
spec.email = ['wyhaines@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Simple utility gem for pulling emails and processing them.'
|
14
|
+
spec.description = 'This is an extraction of a useful pattern for pulling emails and their attached files and processing them.'
|
15
|
+
spec.homepage = 'https://github.com/wyhaines/email-fetch-and-process'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
spec.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
spec.bindir = 'exe'
|
24
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
25
|
+
spec.require_paths = ['lib']
|
26
|
+
|
27
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
28
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
29
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
30
|
+
end
|
@@ -0,0 +1,138 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'net/imap'
|
4
|
+
require 'fileutils'
|
5
|
+
require 'mail'
|
6
|
+
require 'time'
|
7
|
+
require 'email-fetch-and-process/version'
|
8
|
+
|
9
|
+
# Wrap up the logic to iterate through a bunch of fetch and handle
|
10
|
+
# jobs. This is the simplest thing that can work code. It could be
|
11
|
+
# generalized a lot pretty easily.
|
12
|
+
class EmailFetchAndProcess
|
13
|
+
# Class tp encapsulate a fetch and handle job.
|
14
|
+
class Job
|
15
|
+
def initialize(args = {})
|
16
|
+
@args = default_args.merge args
|
17
|
+
end
|
18
|
+
|
19
|
+
def default_args
|
20
|
+
{
|
21
|
+
fetch: ['SUBJECT', ''],
|
22
|
+
filename: '',
|
23
|
+
action: 'echo FILEPATH',
|
24
|
+
subdirectory: nil
|
25
|
+
}
|
26
|
+
end
|
27
|
+
|
28
|
+
def fetch
|
29
|
+
@args[:fetch]
|
30
|
+
end
|
31
|
+
|
32
|
+
def filename
|
33
|
+
@args[:filename]
|
34
|
+
end
|
35
|
+
|
36
|
+
def action
|
37
|
+
@args[:action]
|
38
|
+
end
|
39
|
+
|
40
|
+
def subdirectory
|
41
|
+
@args[:subdirectory]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
attr_accessor :destination
|
46
|
+
|
47
|
+
def initialize(args = {})
|
48
|
+
@args = default_args.merge args
|
49
|
+
@destination = '/tmp'
|
50
|
+
end
|
51
|
+
|
52
|
+
def default_args
|
53
|
+
{
|
54
|
+
host: '127.0.0.1',
|
55
|
+
port: 993,
|
56
|
+
tls: true,
|
57
|
+
id: nil,
|
58
|
+
password: nil,
|
59
|
+
mailbox: 'INBOX'
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
def imap_connection
|
64
|
+
imap = Net::IMAP.new(@args[:host], {:port => @args[:port], :ssl => {:verify_mode => OpenSSL::SSL::VERIFY_NONE}})
|
65
|
+
imap.login(@args[:id], @args[:password])
|
66
|
+
imap.examine(@args[:mailbox])
|
67
|
+
imap
|
68
|
+
end
|
69
|
+
|
70
|
+
def handle_parts(parts, job, msg_ids)
|
71
|
+
parts.each do |part|
|
72
|
+
if !part.parts.empty?
|
73
|
+
handle_parts(part.parts, job, msg_ids)
|
74
|
+
else
|
75
|
+
begin
|
76
|
+
name = if job.filename.to_s.empty?
|
77
|
+
part.header[:content_disposition].filename || part.filename rescue part.filename
|
78
|
+
else
|
79
|
+
job.filename
|
80
|
+
end
|
81
|
+
name = Mail::Encodings.decode_encode(name, :decode) if name rescue name
|
82
|
+
rescue StandardError => e
|
83
|
+
puts e, e.backtrace.inspect
|
84
|
+
next
|
85
|
+
end
|
86
|
+
next unless name
|
87
|
+
|
88
|
+
@body_index += 1
|
89
|
+
attachment = part
|
90
|
+
attachment_path = File.expand_path(File.join(@destination, name))
|
91
|
+
attachment_path = nil unless attachment_path =~ /^#{@destination}/
|
92
|
+
next unless @destination != attachment_path
|
93
|
+
|
94
|
+
if attachment && attachment_path
|
95
|
+
file_path = File.join([@destination, job.subdirectory, name].compact)
|
96
|
+
atch = attachment.body.to_s
|
97
|
+
FileUtils.mkdir_p File.dirname(file_path) unless FileTest.exist? File.dirname(file_path)
|
98
|
+
File.open(file_path, 'wb+') do |fh|
|
99
|
+
fh.write atch.respond_to?(:each) ? atch.join : atch
|
100
|
+
end
|
101
|
+
sha_new = `/usr/bin/shasum "#{file_path}"`.split(/\s+/).first
|
102
|
+
sha_old = nil
|
103
|
+
FileTest.exist?("#{file_path}.sha") &&
|
104
|
+
File.open("#{file_path}.sha", 'r') { |fh| sha_old = fh.read.chomp }
|
105
|
+
if sha_new != sha_old
|
106
|
+
command_to_run = job.action.gsub(/FILEPATH/, file_path)
|
107
|
+
if system(command_to_run)
|
108
|
+
File.open("#{file_path}.sha","w+") {|fh| fh.write sha_new }
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
def run(jobs = [])
|
117
|
+
@imap = imap_connection
|
118
|
+
|
119
|
+
jobs.each do |job|
|
120
|
+
msg_ids = @imap.search(job.fetch)
|
121
|
+
next if msg_ids.nil? || msg_ids.empty?
|
122
|
+
begin
|
123
|
+
msgs = @imap.fetch(msg_ids, %w[ENVELOPE RFC822])
|
124
|
+
msg = msgs.max_by { |m| Time.parse(m['attr']['ENVELOPE'].date) }.attr['RFC822']
|
125
|
+
|
126
|
+
@body_index = 1
|
127
|
+
rescue StandardError => err
|
128
|
+
puts "Error: #{err}\n#{err.backtrace.join("\n")}"
|
129
|
+
@imap = imap_connection
|
130
|
+
next
|
131
|
+
end
|
132
|
+
body = Mail.read_from_string msg
|
133
|
+
handle_parts(body.attachments, job, msg_ids) unless body.attachments.empty?
|
134
|
+
end
|
135
|
+
|
136
|
+
@imap.close
|
137
|
+
end
|
138
|
+
end
|
metadata
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: email-fetch-and-process
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kirk Haines
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-01-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.17'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: minitest
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.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: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: This is an extraction of a useful pattern for pulling emails and their
|
56
|
+
attached files and processing them.
|
57
|
+
email:
|
58
|
+
- wyhaines@gmail.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".rubocop.yml"
|
65
|
+
- ".travis.yml"
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/setup
|
71
|
+
- email-fetch-and-process.gemspec
|
72
|
+
- lib/email-fetch-and-process.rb
|
73
|
+
- lib/email-fetch-and-process/version.rb
|
74
|
+
homepage: https://github.com/wyhaines/email-fetch-and-process
|
75
|
+
licenses:
|
76
|
+
- MIT
|
77
|
+
metadata: {}
|
78
|
+
post_install_message:
|
79
|
+
rdoc_options: []
|
80
|
+
require_paths:
|
81
|
+
- lib
|
82
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
83
|
+
requirements:
|
84
|
+
- - ">="
|
85
|
+
- !ruby/object:Gem::Version
|
86
|
+
version: '0'
|
87
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
88
|
+
requirements:
|
89
|
+
- - ">="
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
requirements: []
|
93
|
+
rubyforge_project:
|
94
|
+
rubygems_version: 2.7.8
|
95
|
+
signing_key:
|
96
|
+
specification_version: 4
|
97
|
+
summary: Simple utility gem for pulling emails and processing them.
|
98
|
+
test_files: []
|