mailhandler 1.0.21 → 1.0.22

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aed334966f4daa868a8533ea9e16b841b68f02b6
4
- data.tar.gz: 69bc39be92c6408806830d8afead62ebd08080da
3
+ metadata.gz: 6ffda69ae4a9be229a9071524a77f71a3b873748
4
+ data.tar.gz: 6b28a918816ee5f8830529792066210b781321d2
5
5
  SHA512:
6
- metadata.gz: 793f2480e28e3a8c9487cd7bdd3b5dade4d40d72306d5056438227711703a538635a11c72ff162578630d699d3ed4da51822ca30bfdd1f10b69e7024f52e2f5c
7
- data.tar.gz: 60ea9caf4a6fe6be1efd66a1267cf05b54318a7a341f32aea1f23a0d8ea3971a443dbcc7d06aa4b6cd992f0acdba495ee53a7db65f11def31acdbeb68e044e3d
6
+ metadata.gz: 836a3e85fe73ca400d881c44b1e520fabb78fdc63859ea98df4f7cfb8d7c34fa6188b911d3aa061e256749da63474723cb4a7fd2b4c59b5fff6a4a7708ea2bd9
7
+ data.tar.gz: 13ad0ea77e75ce066cd71789343f01f49333d764e159921ea120374fcf55823a75e3742cab9bf6e7bfc1739e780d8943e441ce16b4cd644131f7945d04ec095d
@@ -1,6 +1,7 @@
1
1
  require_relative 'receiving/folder'
2
2
  require_relative 'receiving/imap'
3
3
  require_relative 'receiving/observer'
4
+ require_relative 'receiving/mail.rb'
4
5
 
5
6
  module MailHandler
6
7
 
@@ -43,6 +44,7 @@ module MailHandler
43
44
  def find_email(options)
44
45
 
45
46
  init_search_details(options)
47
+ checker.start
46
48
 
47
49
  until search_time_expired?
48
50
 
@@ -57,6 +59,10 @@ module MailHandler
57
59
 
58
60
  checker.search_result
59
61
 
62
+ ensure
63
+
64
+ checker.stop
65
+
60
66
  end
61
67
 
62
68
  private
@@ -22,6 +22,14 @@ module MailHandler
22
22
 
23
23
  end
24
24
 
25
+ def start
26
+
27
+ end
28
+
29
+ def stop
30
+
31
+ end
32
+
25
33
  def find(options)
26
34
 
27
35
  raise MailHandler::InterfaceError, 'Find interface not implemented.'
@@ -57,20 +57,43 @@ module MailHandler
57
57
 
58
58
  end
59
59
 
60
- class ByDate < Base
60
+ module ByDate
61
61
 
62
- def initialize(files, date)
62
+ class Since < Base
63
63
 
64
- super(files)
65
- @date = date
64
+ def initialize(files, date)
65
+
66
+ super(files)
67
+ @date = date
68
+
69
+ end
70
+
71
+ private
72
+
73
+ def meets_expectation?(file)
74
+
75
+ (File.exists? file)? (File.ctime file) > @date : false
76
+
77
+ end
66
78
 
67
79
  end
68
80
 
69
- private
81
+ class Before < Base
82
+
83
+ def initialize(files, date)
84
+
85
+ super(files)
86
+ @date = date
87
+
88
+ end
89
+
90
+ private
70
91
 
71
- def meets_expectation?(file)
92
+ def meets_expectation?(file)
72
93
 
73
- (File.exists? file)? (File.ctime file) > @date : false
94
+ (File.exists? file)? (File.ctime file) < @date : false
95
+
96
+ end
74
97
 
75
98
  end
76
99
 
@@ -44,6 +44,12 @@ module MailHandler
44
44
 
45
45
  end
46
46
 
47
+ def start
48
+
49
+ verify_mailbox_folders
50
+
51
+ end
52
+
47
53
  private
48
54
 
49
55
  # filter options which need to be done by searching files
@@ -51,7 +57,8 @@ module MailHandler
51
57
 
52
58
  :by_subject => FileList::Filter::ByEmailSubject,
53
59
  :by_content => FileList::Filter::ByEmailContent,
54
- :since => FileList::Filter::ByDate,
60
+ :since => FileList::Filter::ByDate::Since,
61
+ :before => FileList::Filter::ByDate::Before,
55
62
  :by_recipient => FileList::Filter::ByEmailRecipient
56
63
  }
57
64
 
@@ -27,15 +27,45 @@ module MailHandler
27
27
  def find(options)
28
28
 
29
29
  verify_and_set_search_options(options)
30
- init_retriever
31
30
  @found_emails = find_emails(search_options)
32
31
 
33
32
  search_result
34
33
 
35
34
  end
36
35
 
36
+ def start
37
+
38
+ init_retriever
39
+ connect
40
+
41
+ end
42
+
43
+ def stop
44
+
45
+ disconnect
46
+
47
+ end
48
+
37
49
  private
38
50
 
51
+ def mailer
52
+
53
+ @mailer ||= Mail.retriever_method
54
+
55
+ end
56
+
57
+ def connect
58
+
59
+ mailer.connect
60
+
61
+ end
62
+
63
+ def disconnect
64
+
65
+ mailer.disconnect
66
+
67
+ end
68
+
39
69
  # search options:
40
70
  # by_subject - String, search by a whole string as part of the subject of the email
41
71
  # by_content - String, search by a whole string as part of the content of the email
@@ -46,6 +76,8 @@ module MailHandler
46
76
 
47
77
  :by_subject,
48
78
  :by_content,
79
+ :since,
80
+ :before,
49
81
  :count,
50
82
  :archive,
51
83
  :by_recipient
@@ -92,7 +124,7 @@ module MailHandler
92
124
 
93
125
  def find_emails(options)
94
126
 
95
- result = Mail.find(:what => :last, :count => search_options[:count], :order => :desc, :keys => imap_filter_keys(options), :delete_after_find => options[:archive])
127
+ result = mailer.find(:what => :last, :count => search_options[:count], :order => :desc, :keys => imap_filter_keys(options), :delete_after_find => options[:archive])
96
128
  (result.kind_of? Array)? result : [result]
97
129
 
98
130
  end
@@ -117,6 +149,14 @@ module MailHandler
117
149
 
118
150
  keys << 'BODY' << options[:by_content].to_s
119
151
 
152
+ when :since
153
+
154
+ keys << 'SINCE' << Net::IMAP.format_date(options[:since])
155
+
156
+ when :before
157
+
158
+ keys << 'BEFORE' << Net::IMAP.format_date(options[:before])
159
+
120
160
  else
121
161
 
122
162
  # do nothing
@@ -0,0 +1,91 @@
1
+ module Mail
2
+
3
+ class IMAP
4
+
5
+ attr_accessor :imap
6
+
7
+ def find(options={}, &block)
8
+
9
+ options = validate_options(options)
10
+
11
+ options[:read_only] ? imap.examine(options[:mailbox]) : imap.select(options[:mailbox])
12
+
13
+ uids = imap.uid_search(options[:keys])
14
+ uids.reverse! if options[:what].to_sym == :last
15
+ uids = uids.first(options[:count]) if options[:count].is_a?(Integer)
16
+ uids.reverse! if (options[:what].to_sym == :last && options[:order].to_sym == :asc) ||
17
+ (options[:what].to_sym != :last && options[:order].to_sym == :desc)
18
+
19
+
20
+ if block_given?
21
+
22
+ uids.each do |uid|
23
+ uid = options[:uid].to_i unless options[:uid].nil?
24
+ fetchdata = imap.uid_fetch(uid, ['RFC822'])[0]
25
+ new_message = Mail.new(fetchdata.attr['RFC822'])
26
+ new_message.mark_for_delete = true if options[:delete_after_find]
27
+ if block.arity == 3
28
+ yield new_message, imap, uid
29
+ else
30
+ yield new_message
31
+ end
32
+ imap.uid_store(uid, "+FLAGS", [Net::IMAP::DELETED]) if options[:delete_after_find] && new_message.is_marked_for_delete?
33
+ break unless options[:uid].nil?
34
+ end
35
+
36
+ imap.expunge if options[:delete_after_find]
37
+
38
+ else
39
+
40
+ emails = []
41
+
42
+ unless uids.nil?
43
+
44
+ uids.each do |uid|
45
+ uid = options[:uid].to_i unless options[:uid].nil?
46
+ fetchdata = imap.uid_fetch(uid, ['RFC822'])[0]
47
+ emails << Mail.new(fetchdata.attr['RFC822'])
48
+ imap.uid_store(uid, "+FLAGS", [Net::IMAP::DELETED]) if options[:delete_after_find]
49
+ break unless options[:uid].nil?
50
+
51
+ end
52
+
53
+ end
54
+
55
+ imap.expunge if options[:delete_after_find]
56
+ emails.size == 1 && options[:count] == 1 ? emails.first : emails
57
+
58
+ end
59
+
60
+ end
61
+
62
+ # Start an IMAP session
63
+ def connect(config=Mail::Configuration.instance)
64
+
65
+ puts "connect"
66
+ @imap = Net::IMAP.new(settings[:address], settings[:port], settings[:enable_ssl], nil, false)
67
+
68
+ if settings[:authentication].nil?
69
+ imap.login(settings[:user_name], settings[:password])
70
+ else
71
+ # Note that Net::IMAP#authenticate('LOGIN', ...) is not equal with Net::IMAP#login(...)!
72
+ # (see also http://www.ensta.fr/~diam/ruby/online/ruby-doc-stdlib/libdoc/net/imap/rdoc/classes/Net/IMAP.html#M000718)
73
+ imap.authenticate(settings[:authentication], settings[:user_name], settings[:password])
74
+ end
75
+
76
+ end
77
+
78
+ def disconnect
79
+
80
+ puts "disconnect"
81
+ if defined?(imap) && imap && !imap.disconnected?
82
+
83
+ imap.disconnect
84
+
85
+ end
86
+
87
+ end
88
+
89
+ end
90
+
91
+ end
@@ -1,5 +1,5 @@
1
1
  module MailHandler
2
2
 
3
- VERSION = '1.0.21'
3
+ VERSION = '1.0.22'
4
4
 
5
5
  end
@@ -14,6 +14,8 @@ describe MailHandler::Receiver do
14
14
  allow(checker).to receive(:search_result) { true }
15
15
  allow(checker).to receive(:found_emails) { [found_email] }
16
16
  allow(checker).to receive(:reset_found_emails) { [] }
17
+ allow(checker).to receive(:start) { nil }
18
+ allow(checker).to receive(:stop) { nil }
17
19
  checker
18
20
 
19
21
  }
@@ -72,6 +74,8 @@ describe MailHandler::Receiver do
72
74
  allow(checker).to receive(:search_result) { false }
73
75
  allow(checker).to receive(:found_emails) { [] }
74
76
  allow(checker).to receive(:reset_found_emails) { [] }
77
+ allow(checker).to receive(:start) { nil }
78
+ allow(checker).to receive(:stop) { nil }
75
79
  checker
76
80
 
77
81
  }
@@ -54,12 +54,6 @@ describe MailHandler::Receiving::IMAPChecker do
54
54
 
55
55
  context 'invalid' do
56
56
 
57
- it 'by date' do
58
-
59
- expect { checker.find({:since => Time.now}) }.to raise_error MailHandler::Error
60
-
61
- end
62
-
63
57
  it 'by_test' do
64
58
 
65
59
  expect { checker.find({:by_test => 'test'}) }.to raise_error MailHandler::Error
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mailhandler
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.21
4
+ version: 1.0.22
5
5
  platform: ruby
6
6
  authors:
7
7
  - Igor Balos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-02-02 00:00:00.000000000 Z
11
+ date: 2016-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mail
@@ -67,6 +67,7 @@ files:
67
67
  - lib/mailhandler/receiving/filelist/filter/email.rb
68
68
  - lib/mailhandler/receiving/folder.rb
69
69
  - lib/mailhandler/receiving/imap.rb
70
+ - lib/mailhandler/receiving/mail.rb
70
71
  - lib/mailhandler/receiving/notification/console.rb
71
72
  - lib/mailhandler/receiving/notification/email.rb
72
73
  - lib/mailhandler/receiving/notification/email/content.rb