email-fetch-and-process 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/email-fetch-and-process.rb +22 -14
- data/lib/email-fetch-and-process/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7e9ba4489394af5e199e4fb73c66da31339871c970d1dcfe01e4e97454774905
|
4
|
+
data.tar.gz: ec631ce7e5ddbdec7c4b27d6535cd9e819318dd7e8829ea93ad1af1e60fd76d4
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d966e8b64205b9ddd2eb7e64f09ec3187e0e495decee4fa0dd67fae0c3092200230cd11cf99c55abde47ae3584f9b8198ba6a14491485950a49f7dd5af1436bd
|
7
|
+
data.tar.gz: 20e24bae296292d8fcdec5cf21a715db0b16c032a0f83b7ec02e67bb1ca93725ec539075d5f613c7367a41abdd31010aeb2d4dd9e0e963f2469bc92ef4e3f2a0
|
data/Gemfile.lock
CHANGED
@@ -4,6 +4,7 @@ require 'net/imap'
|
|
4
4
|
require 'fileutils'
|
5
5
|
require 'mail'
|
6
6
|
require 'time'
|
7
|
+
require 'shellwords'
|
7
8
|
require 'email-fetch-and-process/version'
|
8
9
|
|
9
10
|
# Wrap up the logic to iterate through a bunch of fetch and handle
|
@@ -21,7 +22,8 @@ class EmailFetchAndProcess
|
|
21
22
|
fetch: ['SUBJECT', ''],
|
22
23
|
filename: '',
|
23
24
|
action: 'echo FILEPATH',
|
24
|
-
subdirectory: nil
|
25
|
+
subdirectory: nil,
|
26
|
+
destination: '/tmp'
|
25
27
|
}
|
26
28
|
end
|
27
29
|
|
@@ -30,7 +32,7 @@ class EmailFetchAndProcess
|
|
30
32
|
end
|
31
33
|
|
32
34
|
def multiple_fetch_terms?
|
33
|
-
|
35
|
+
@args[:fetch][0].is_a?(Array)
|
34
36
|
end
|
35
37
|
|
36
38
|
def filename
|
@@ -44,6 +46,10 @@ class EmailFetchAndProcess
|
|
44
46
|
def subdirectory
|
45
47
|
@args[:subdirectory]
|
46
48
|
end
|
49
|
+
|
50
|
+
def destination
|
51
|
+
@args[:destination]
|
52
|
+
end
|
47
53
|
end
|
48
54
|
|
49
55
|
attr_accessor :destination
|
@@ -65,7 +71,7 @@ class EmailFetchAndProcess
|
|
65
71
|
end
|
66
72
|
|
67
73
|
def imap_connection
|
68
|
-
imap = Net::IMAP.new(@args[:host],
|
74
|
+
imap = Net::IMAP.new(@args[:host], port: @args[:port], ssl: { verify_mode: OpenSSL::SSL::VERIFY_NONE })
|
69
75
|
imap.login(@args[:id], @args[:password])
|
70
76
|
imap.examine(@args[:mailbox])
|
71
77
|
imap
|
@@ -91,12 +97,13 @@ class EmailFetchAndProcess
|
|
91
97
|
|
92
98
|
@body_index += 1
|
93
99
|
attachment = part
|
94
|
-
|
95
|
-
attachment_path =
|
96
|
-
|
100
|
+
final_destination = job.destination || @destination
|
101
|
+
attachment_path = File.expand_path(File.join(final_destination, name))
|
102
|
+
attachment_path = nil unless attachment_path =~ /^#{final_destination}/
|
103
|
+
next unless final_destination != attachment_path
|
97
104
|
|
98
105
|
if attachment && attachment_path
|
99
|
-
file_path = File.join([
|
106
|
+
file_path = File.join([final_destination, job.subdirectory, name].compact)
|
100
107
|
atch = attachment.body.to_s
|
101
108
|
FileUtils.mkdir_p File.dirname(file_path) unless FileTest.exist? File.dirname(file_path)
|
102
109
|
File.open(file_path, 'wb+') do |fh|
|
@@ -107,10 +114,10 @@ class EmailFetchAndProcess
|
|
107
114
|
FileTest.exist?("#{file_path}.sha") &&
|
108
115
|
File.open("#{file_path}.sha", 'r') { |fh| sha_old = fh.read.chomp }
|
109
116
|
if sha_new != sha_old
|
110
|
-
command_to_run = job.action.gsub(/FILEPATH/, file_path)
|
111
|
-
|
112
|
-
|
113
|
-
|
117
|
+
command_to_run = job.action.gsub(/FILEPATH/, Shellwords.escape(file_path)).
|
118
|
+
gsub(/DESTINATION/, Shellwords.escape(final_destination))
|
119
|
+
system(command_to_run) &&
|
120
|
+
File.open("#{file_path}.sha", 'w+') { |fh| fh.write sha_new }
|
114
121
|
end
|
115
122
|
end
|
116
123
|
end
|
@@ -122,13 +129,14 @@ class EmailFetchAndProcess
|
|
122
129
|
|
123
130
|
jobs.each do |job|
|
124
131
|
msg_ids = if job.multiple_fetch_terms?
|
125
|
-
|
126
|
-
job.fetch.each { |j|
|
127
|
-
|
132
|
+
all_ids = []
|
133
|
+
job.fetch.each { |j| all_ids += @imap.search(j) }
|
134
|
+
all_ids
|
128
135
|
else
|
129
136
|
@imap.search(job.fetch)
|
130
137
|
end
|
131
138
|
next if msg_ids.nil? || msg_ids.empty?
|
139
|
+
|
132
140
|
begin
|
133
141
|
msgs = @imap.fetch(msg_ids, %w[ENVELOPE RFC822])
|
134
142
|
msg = msgs.max_by { |m| Time.parse(m['attr']['ENVELOPE'].date) }.attr['RFC822']
|