Class: Rockdove::CollectMail
- Inherits:
-
Object
- Object
- Rockdove::CollectMail
- Defined in:
- lib/rockdove/collect_mail.rb
Constant Summary collapse
- UNDELIVERABLE =
/Undeliverable/i
- AUTO_REPLY =
/Automatic reply/i
- SPAM =
/SPAM/i
- FAILURE =
/Delivery(.+)Failure/i
Class Attribute Summary collapse
-
.inbox_connection ⇒ Object
Returns the value of attribute inbox_connection.
-
.mail_stack ⇒ Object
Returns the value of attribute mail_stack.
Class Method Summary collapse
Instance Method Summary collapse
- #archive(item) ⇒ Object
- #bounce_type_mail?(item) ⇒ Boolean
- #destination(to_folder) ⇒ Object
- #fetch_from_box ⇒ Object
- #group_of_mails ⇒ Object
- #ignore_mail?(item) ⇒ Boolean
- #inbox ⇒ Object
-
#initialize(mail_stack = nil, inbox = nil) ⇒ CollectMail
constructor
A new instance of CollectMail.
- #no_mail_alert ⇒ Object
- #process ⇒ Object
- #reconnect_and_raise_error ⇒ Object
- #retrieve_mail(fetched_mail) ⇒ Object
- #send_connection_failed_message ⇒ Object
- #send_rockdove_to_watch_mail(&block) ⇒ Object
Constructor Details
#initialize(mail_stack = nil, inbox = nil) ⇒ CollectMail
Returns a new instance of CollectMail.
12 13 14 15 |
# File 'lib/rockdove/collect_mail.rb', line 12 def initialize(mail_stack = nil, inbox = nil) @mail_stack = mail_stack @inbox_connection = inbox end |
Class Attribute Details
.inbox_connection ⇒ Object
Returns the value of attribute inbox_connection.
9 10 11 |
# File 'lib/rockdove/collect_mail.rb', line 9 def inbox_connection @inbox_connection end |
.mail_stack ⇒ Object
Returns the value of attribute mail_stack.
9 10 11 |
# File 'lib/rockdove/collect_mail.rb', line 9 def mail_stack @mail_stack end |
Class Method Details
.watch(&block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/rockdove/collect_mail.rb', line 17 def self.watch &block loop do begin new().send_rockdove_to_watch_mail(&block) rescue Exception => e Rockdove.logger.error [e, *e.backtrace].join("\n") ensure sleep(Rockdove::Config.watch_interval) end end end |
Instance Method Details
#archive(item) ⇒ Object
120 121 122 123 124 125 126 127 128 |
# File 'lib/rockdove/collect_mail.rb', line 120 def archive(item) if @to_folder.blank? item.delete! Rockdove.logger.info "Rockdove delivered & deleted the mail." else item.move!(destination(@to_folder)) Rockdove.logger.info "Rockdove delivered & archived the mail." end end |
#bounce_type_mail?(item) ⇒ Boolean
66 67 68 69 70 71 72 73 74 |
# File 'lib/rockdove/collect_mail.rb', line 66 def bounce_type_mail?(item) case item.subject when UNDELIVERABLE, AUTO_REPLY, SPAM, FAILURE Rockdove.logger.info "Rockdove deleting this mail: #{item.subject}." true else false end end |
#destination(to_folder) ⇒ Object
130 131 132 |
# File 'lib/rockdove/collect_mail.rb', line 130 def destination(to_folder) Viewpoint::EWS::Folder.get_folder_by_name(to_folder) end |
#fetch_from_box ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/rockdove/collect_mail.rb', line 86 def fetch_from_box @inbox_connection = inbox return nil if @inbox_connection.nil? || @inbox_connection == true @mail_stack = @inbox_connection.find_items return nil if @mail_stack.empty? @mail_stack end |
#group_of_mails ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/rockdove/collect_mail.rb', line 39 def group_of_mails return no_mail_alert unless fetch_from_box Rockdove.logger.info "Rockdove collected #{@mail_stack.count} mail(s)." letters = RockdoveCollection.new @mail_stack.reverse.each do |item| if ignore_mail?(item) || bounce_type_mail?(item) item.delete! @mail_stack.delete(item) else letters << retrieve_mail(item) end end letters end |
#ignore_mail?(item) ⇒ Boolean
54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/rockdove/collect_mail.rb', line 54 def ignore_mail?(item) email = item.from.email_address ignore_list = Rockdove::Config.ignore_mails return false unless ignore_list && !(ignore_list.empty?) if ignore_list.include?(email) Rockdove.logger.info "Rockdove detected #{email} under ignore mail list." true else false end end |
#inbox ⇒ Object
94 95 96 97 98 99 |
# File 'lib/rockdove/collect_mail.rb', line 94 def inbox @incoming_folder = Rockdove::Config.incoming_folder Viewpoint::EWS::Folder.get_folder_by_name(@incoming_folder) rescue reconnect_and_raise_error end |
#no_mail_alert ⇒ Object
81 82 83 84 |
# File 'lib/rockdove/collect_mail.rb', line 81 def no_mail_alert Rockdove.logger.info "Rockdove observed no mail yet." nil end |
#process ⇒ Object
114 115 116 117 118 |
# File 'lib/rockdove/collect_mail.rb', line 114 def process @to_folder = Rockdove::Config.archive_folder @mail_stack.each {|item| archive(item) } @mail_stack = nil end |
#reconnect_and_raise_error ⇒ Object
101 102 103 104 105 106 107 |
# File 'lib/rockdove/collect_mail.rb', line 101 def reconnect_and_raise_error Rockdove.logger.info "Reconnecting to the Exchange Server & Fetching the mail now." Rockdove::Config.connect Viewpoint::EWS::Folder.get_folder_by_name(@incoming_folder) rescue Viewpoint::EWS::EwsError end |
#retrieve_mail(fetched_mail) ⇒ Object
76 77 78 79 |
# File 'lib/rockdove/collect_mail.rb', line 76 def retrieve_mail(fetched_mail) @inbox_connection ||= inbox Rockdove::ExchangeMail.new(fetched_mail, @inbox_connection) end |
#send_connection_failed_message ⇒ Object
109 110 111 112 |
# File 'lib/rockdove/collect_mail.rb', line 109 def Rockdove.logger.info "Rockdove unable to connect to the Exchange Server" return nil end |
#send_rockdove_to_watch_mail(&block) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/rockdove/collect_mail.rb', line 29 def send_rockdove_to_watch_mail(&block) Rockdove.logger.info "Rockdove on watch for new mail..." parsed_mails = group_of_mails if parsed_mails Rockdove.logger.info "Rockdove calling App block" block.call(parsed_mails) process() end end |