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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a5a16792ae36fc27529004d4882e9a79e1df52efd8119c3bcb420ca1aae65288
4
- data.tar.gz: 2a80e0b7e7e45db85af3472fac3cf41f36770a1c506c6fe81e490f7058db16ad
3
+ metadata.gz: 7e9ba4489394af5e199e4fb73c66da31339871c970d1dcfe01e4e97454774905
4
+ data.tar.gz: ec631ce7e5ddbdec7c4b27d6535cd9e819318dd7e8829ea93ad1af1e60fd76d4
5
5
  SHA512:
6
- metadata.gz: 646c8bf3b023cf95deda4a8d391616aa9800e530ca0ba21d704b220b0d0fd95693bc7146bd8875078af2a3d33c600771c6f29f9868127a5118e14f2fc2e2234e
7
- data.tar.gz: 0377a0b6ec94cc2e087ef2251ff03e2e54ac70e1b42e41b593eef34e1f4ba6c5c8527c4e3f7d6be913f93ec3dc1dd524b03db4ce240a8541446cabe6affc777d
6
+ metadata.gz: d966e8b64205b9ddd2eb7e64f09ec3187e0e495decee4fa0dd67fae0c3092200230cd11cf99c55abde47ae3584f9b8198ba6a14491485950a49f7dd5af1436bd
7
+ data.tar.gz: 20e24bae296292d8fcdec5cf21a715db0b16c032a0f83b7ec02e67bb1ca93725ec539075d5f613c7367a41abdd31010aeb2d4dd9e0e963f2469bc92ef4e3f2a0
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- email-fetch-and-process (0.1.1)
4
+ email-fetch-and-process (0.1.2)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
@@ -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
- Array === @args[:fetch][0]
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], {:port => @args[:port], :ssl => {:verify_mode => OpenSSL::SSL::VERIFY_NONE}})
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
- attachment_path = File.expand_path(File.join(@destination, name))
95
- attachment_path = nil unless attachment_path =~ /^#{@destination}/
96
- next unless @destination != attachment_path
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([@destination, job.subdirectory, name].compact)
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
- if system(command_to_run)
112
- File.open("#{file_path}.sha","w+") {|fh| fh.write sha_new }
113
- end
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
- _ids = []
126
- job.fetch.each { |j| _ids += @imap.search(j) }
127
- _ids
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']
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class EmailFetchAndProcess
4
- VERSION = '0.1.1'
4
+ VERSION = '0.1.2'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: email-fetch-and-process
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kirk Haines