Module: Mastiff::Email

Extended by:
Email
Included in:
Email
Defined in:
lib/mastiff/email.rb,
lib/mastiff/message.rb

Defined Under Namespace

Classes: Message

Constant Summary collapse

@@imap_connection =
nil

Instance Method Summary collapse

Instance Method Details

#all(*args, &block) ⇒ Object

Receive all emails from the default retriever See Mail::Retriever for a complete documentation.



60
61
62
# File 'lib/mastiff/email.rb', line 60

def all(*args, &block)
  Mail.all(*args, &block)
end

#archive(ids = []) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/mastiff/email.rb', line 140

def archive(ids = [])
  unless ids.blank?
    uids         = ids.map{|v| (v.split ':').last.to_i}
    original_vid = ids.first.split(':').first.to_i


    Mail.connection do |imap|
      delim = imap.list("","INBOX").first.delim
      path = ["INBOX","unprocessed"].join(delim)
      imap.select('INBOX')
       if not imap.list('', "INBOX#{delim}unprocessed")
         imap.create("INBOX#{delim}unprocessed")
       end
       uids.each do |uid|
         imap.uid_copy(uid, "INBOX#{delim}unprocessed")
         imap.uid_store(uid, "+FLAGS", [:Deleted])
       end
       imap.expunge
    end
    sync_deleted(ids)
    return ids
  end
  return []
end

#configure {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



54
55
56
# File 'lib/mastiff/email.rb', line 54

def configure(&block)
  yield self
end

#finalize(ids = [], status = 'ok') ⇒ Object



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/mastiff/email.rb', line 164

def finalize(ids = [], status = 'ok')
  unless ids.blank?
    uids         = ids.map{|v| (v.split ':').last.to_i}
    original_vid = ids.first.split(':').first.to_i


    Mail.connection do |imap|
      delim = imap.list("","INBOX").first.delim
      mail_folder = ''
      case status
        when /processed/
          mail_folder = Mastiff.mailbox_folders[:processed] if Mastiff.mailbox_folders[:processed]
        when /rejected/
          mail_folder = Mastiff.mailbox_folders[:rejected] if Mastiff.mailbox_folders[:rejected]
      end
      if mail_folder
        path = ["INBOX", mail_folder].join(delim)
        imap.select('INBOX')
        if not imap.list('', "INBOX#{delim}#{mail_folder}")
         imap.create("INBOX#{delim}#{mail_folder}")
        end

        uids.each do |uid|
          Sidekiq::Logging.logger.info "Copying Mail to folder #{mail_folder} #{uid}"
          imap.uid_copy(uid, "INBOX#{delim}#{mail_folder}")
        end
      end

      uids.each do |uid|
        Sidekiq::Logging.logger.info "Marking Mail for deletion #{uid}"
        imap.uid_store(uid, "+FLAGS", [:Deleted])
      end

      imap.expunge
    end

    sync_deleted(ids)
    return ids
  end
  return []
end

#flush(current_uid_validity = :all) ⇒ Object



206
207
208
209
210
211
212
# File 'lib/mastiff/email.rb', line 206

def flush(current_uid_validity = :all)
  current_uid_validity = :all if current_uid_validity.nil?
  Mastiff.attachment_uploader.flush
  stale_msgs_ids = Message.emails.keys.select {|k| /#{current_uid_validity}:/ !~ k}
  stale_msgs_ids.each {|id| Message.emails.delete id}
  stale_msgs_ids.each {|id| Message.raw.delete id}
end

#handle_mail(ids = []) ⇒ Object



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/mastiff/email.rb', line 116

def handle_mail(ids = [])
  unless ids.blank?
    ids.each do |id|
      msg = Message.get(id)
      Sidekiq::Logging.logger.info "msg #{id} busy state: #{msg.header[:busy]}"
      unless msg.busy?
        msg.lock_and_save
        begin
          Mastiff.process_attachment_worker.perform_async(id)
          ensure
            msg.unlock_and_save
        end
        msg.unlock_and_save
      end
    end
    uids    = ids.map{|v| (v.split ':').last.to_i}
    original_vid = ids.first.split(':').first.to_i

    return ids
  end

  return []
end

#headers(ids = []) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/mastiff/email.rb', line 72

def headers(ids = [])
  ids = u_ids() if ids.blank?
  if ids.blank?
    []
  else
    Message.emails.bulk_values(*ids).map{|msg| msg.header}
  end
end

#messages(ids = []) ⇒ Object



80
81
82
83
84
85
86
87
# File 'lib/mastiff/email.rb', line 80

def messages(ids = [])
  ids = u_ids() if ids.blank?
  if ids.blank?
    []
  else
    Message.emails.bulk_values(*ids)
  end
end

#process_inboxObject

Modification Methods



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/mastiff/email.rb', line 100

def process_inbox
  no_attachments = headers.select{|m| m[:attachment_name].blank?}.map{|m| m[:id]}
  remove(no_attachments)

  grouped = headers.group_by{|m| m[:subject]}
  keep_ids    = []
  grouped.each do |k,l|
   l.sort_by!{|m| m[:date]}.reverse!
   keep_ids << l.shift[:id]
  end
  archive_ids = grouped.map{|k,l| l.map{|m| m[:id]}}.flatten
  archive(archive_ids)

  handle_mail(keep_ids)

end

#raw_messages(ids = []) ⇒ Object



88
89
90
91
92
93
94
95
# File 'lib/mastiff/email.rb', line 88

def raw_messages(ids = [])
  ids = u_ids() if ids.blank?
  if ids.blank?
    []
  else
    Message.raw.bulk_values(*ids).map{|msg| Mail.new(msg)}
  end
end

#remove(ids = []) ⇒ Object



213
214
215
216
217
218
219
220
221
222
# File 'lib/mastiff/email.rb', line 213

def remove(ids = [])
  unless ids.blank?
    uids         = ids.map{|v| (v.split ':').last.to_i}
    original_vid = ids.first.split(':').first.to_i
    Mail.find_and_delete(keys: "UID #{uids.join ','}")
    sync_deleted(ids)
    return ids
  end
  return []
end

#settingsObject



50
51
52
# File 'lib/mastiff/email.rb', line 50

def settings
  Mail.retriever_method.settings
end

#settings=(settings_hash) ⇒ Object

{ :address => “localhost”,

:port                 => 143,
:user_name            => nil,
:password             => nil,
:authentication       => nil,
:enable_ssl           => false }.merge!(values)


45
46
47
48
49
# File 'lib/mastiff/email.rb', line 45

def settings= settings_hash
  Mail.defaults do
    retriever_method :imap, settings_hash
  end
end

#sync_all(options = {}, &block) ⇒ Object



276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/mastiff/email.rb', line 276

def sync_all(options={}, &block)
  self.flush
  Mail.all({keys:["NOT", "DELETED"]}.merge(options)) do |message, imap, uid|
    validity_id = imap.responses["UIDVALIDITY"].last if imap.responses["UIDVALIDITY"]
    msg         = Message.new(uid: uid, validity_id: validity_id, raw_message: message.raw_source)
    msg.save

    if Message.validity != msg.validity_id
      flush(msg.validity_id)
      Message.uid_validity.value = msg.validity_id
    end

    if block_given?
      if block.arity == 3
        yield msg, imap, uid
      else
        yield msg
      end
    end
  end
end

#sync_deleted(ids = []) ⇒ Object



224
225
226
227
228
229
230
231
232
233
234
235
# File 'lib/mastiff/email.rb', line 224

def sync_deleted(ids = [])
  unless ids.blank?
    ids.each do |id|
      message = Message.get(id)
      if message.respond_to?('attachment_name') and not message.attachment_name.blank?
        Mastiff::attachment_uploader.delete(message.attachment_name)
      end
      Message.emails.delete id
      Message.raw.delete id
    end
  end
end

#sync_messagesObject

def sync_new(ids = [])

unless ids.blank?
    fetchdata = imap.uid_fetch(ids, ['RFC822'])
    fetchdata.each do |rec|
      message = Mail.new(rec.attr['RFC822'])
      validity_id = imap.responses["UIDVALIDITY"].last if imap.responses["UIDVALIDITY"]
      msg         = Message.new(uid: rec['UID'], validity_id: validity_id, mail_message: message)
      msg.save
  end
end

end



248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/mastiff/email.rb', line 248

def sync_messages
  Mail.connection do |imap|
    imap.select 'INBOX'
    validity_id = imap.responses["UIDVALIDITY"].last if imap.responses["UIDVALIDITY"]
    if Message.validity.eql? validity_id
      uids        = imap.uid_search(["NOT", "DELETED"]).sort
      local_uids  = Message.ids
      if  uids != local_uids
        Sidekiq::Logging.logger.info "*** Syncing Some ***"
        new_ids     = uids       - local_uids
        deleted_ids = local_uids - uids
        unless new_ids.blank?
          fetchdata = imap.uid_fetch(new_ids, ['RFC822'])
          fetchdata.each do |rec|
              validity_id = imap.responses["UIDVALIDITY"].last if imap.responses["UIDVALIDITY"]
              msg         = Message.new(uid: rec.attr['UID'], validity_id: validity_id, raw_message: rec.attr['RFC822'])
              msg.save
          end
        end
        self.sync_deleted(deleted_ids.map{|id| [validity_id,id].join ':'}) unless deleted_ids.blank?
      end
    else
      self.sync_all
    end
    Message.ids
  end
end

#u_idsObject



67
68
69
70
# File 'lib/mastiff/email.rb', line 67

def u_ids
  prefix = uid_validity
  Message.emails.keys.select{|k| /#{prefix}:/ =~ k }
end

#uid_validityObject



64
65
66
# File 'lib/mastiff/email.rb', line 64

def uid_validity
  Message.validity
end