Class: Rockdove::CollectMail

Inherits:
Object
  • Object
show all
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
AUTO_REPLIES_HEADER =
"X-Auto-Response-Suppress"

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mail_stack = nil, inbox = nil) ⇒ CollectMail

Returns a new instance of CollectMail.



13
14
15
16
# File 'lib/rockdove/collect_mail.rb', line 13

def initialize(mail_stack = nil, inbox = nil)
  @mail_stack = mail_stack
  @inbox_connection = inbox
end

Class Attribute Details

.inbox_connectionObject

Returns the value of attribute inbox_connection.



10
11
12
# File 'lib/rockdove/collect_mail.rb', line 10

def inbox_connection
  @inbox_connection
end

.mail_stackObject

Returns the value of attribute mail_stack.



10
11
12
# File 'lib/rockdove/collect_mail.rb', line 10

def mail_stack
  @mail_stack
end

Class Method Details

.watch(&block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
# File 'lib/rockdove/collect_mail.rb', line 18

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
129
# File 'lib/rockdove/collect_mail.rb', line 120

def archive(item)
  @to_folder = Rockdove::Config.archive_folder
  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

Returns:

  • (Boolean)


67
68
69
70
71
72
73
74
75
# File 'lib/rockdove/collect_mail.rb', line 67

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
    item.headers.include?(AUTO_REPLIES_HEADER) ? true : false
  end
end

#destination(to_folder) ⇒ Object



131
132
133
# File 'lib/rockdove/collect_mail.rb', line 131

def destination(to_folder)
  Viewpoint::EWS::Folder.get_folder_by_name(to_folder)
end

#fetch_from_boxObject



87
88
89
90
91
92
93
# File 'lib/rockdove/collect_mail.rb', line 87

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_mailsObject



40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rockdove/collect_mail.rb', line 40

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

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
# File 'lib/rockdove/collect_mail.rb', line 55

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

#inboxObject



95
96
97
98
99
100
# File 'lib/rockdove/collect_mail.rb', line 95

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_alertObject



82
83
84
85
# File 'lib/rockdove/collect_mail.rb', line 82

def no_mail_alert
  Rockdove.logger.info "Rockdove observed no mail yet."
  nil
end

#processObject



115
116
117
118
# File 'lib/rockdove/collect_mail.rb', line 115

def process      
  @mail_stack.each {|item| archive(item) }
  @mail_stack = nil
end

#reconnect_and_raise_errorObject



102
103
104
105
106
107
108
# File 'lib/rockdove/collect_mail.rb', line 102

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
  send_connection_failed_message
end

#retrieve_mail(fetched_mail) ⇒ Object



77
78
79
80
# File 'lib/rockdove/collect_mail.rb', line 77

def retrieve_mail(fetched_mail) 
  @inbox_connection ||= inbox
  Rockdove::ExchangeMail.new(fetched_mail, @inbox_connection)
end

#send_connection_failed_messageObject



110
111
112
113
# File 'lib/rockdove/collect_mail.rb', line 110

def send_connection_failed_message
  Rockdove.logger.info "Rockdove unable to connect to the Exchange Server"
  return nil
end

#send_rockdove_to_watch_mail(&block) ⇒ Object



30
31
32
33
34
35
36
37
38
# File 'lib/rockdove/collect_mail.rb', line 30

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