-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRetrieve.rb
82 lines (75 loc) · 1.98 KB
/
Retrieve.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require 'net/imap'
require 'net/ssh'
require 'uri'
require 'optparse'
require 'cgi'
require 'mail'
require 'time'
require_relative 'Utilities.rb'
include Utilities
class Retrieve
include Utilities
def get_all_email_for_all_rcpts
rcpts = @@options.rcpts
rcpts.each do |rcpt|
getEmail(rcpt)
end
end
def get_email_for_all_rcpts(subjects)
rcpts = @@options.rcpts
rcpts.each do |rcpt|
get_emails_with_subject(rcpt, subjects)
end
end
def get_emails_with_subject(rcpt, subjects_to_search_for)
num_message_count = 0
loop_retry_count = 0
imap = getImap(rcpt)
temp = subjects_to_search_for
puts "temp: #{temp}" if @@options.debug
while loop_retry_count < 10 && !temp.empty? do
mailIds = imap.search(['ALL'])
mailIds.each do |id|
begin
msg = imap.fetch(id, 'RFC822')[0].attr['RFC822']
mail = Mail.read_from_string(msg)
if temp.include?(mail.subject)
next if mail.html_part.nil? || mail.text_part.nil?
do_parsing(mail)
temp.delete(mail.subject)
puts "temp: #{temp}" if @@options.debug
num_message_count += 1
imap.store(id, "+FLAGS", [:Seen])
end
rescue => e
puts "Failed in fetching message"
puts e
end
end
loop_retry_count += 1
end
puts "Got #{num_message_count} messages for #{rcpt}"
imap.logout()
imap.disconnect()
end
def getEmail(rcpt)
count = 0
imap = getImap(rcpt)
mailIds = imap.search(['ALL'])
mailIds.each do |id|
count += 1
begin
msg = imap.fetch(id, 'RFC822')[0].attr['RFC822']
mail = Mail.read_from_string(msg)
next if mail.html_part.nil? || mail.text_part.nil?
imap.store(id, "+FLAGS", [:Seen])
rescue => e
puts "Failed in fetching message"
puts e
end
end
puts "Got #{count} messages for #{rcpt}"
imap.logout()
imap.disconnect()
end
end